1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "sensor_agent_proxy.h"
17 
18 #include <log.h>
19 #include <securec.h>
20 #include <stdlib.h>
21 #include "sensor_agent.h"
22 #include "sensor_service.h"
23 
24 static SensorInfo *g_sensorLists;
25 static SensorEvent *g_sensorEvent;
26 static int32_t g_sensorListsLength;
27 static SvcIdentity g_svcIdentity = {
28     .handle = 0,
29     .token = 0,
30 };
31 static IpcObjectStub g_objectStub;
32 
33 typedef struct CallbackNode {
34     RecordSensorCallback callback;
35     void *next;
36 } CallbackNode;
37 
38 CallbackNode g_callbackNodes[(int32_t)SENSOR_TYPE_ID_MAX] = {
39     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
40     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
41     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
42     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
43     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
44     {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL},
45 };
46 
IsRegisterCallback()47 int32_t IsRegisterCallback()
48 {
49     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
50     int32_t isRegisterCallback = 0;
51     for (int32_t sensorTypeId = 0; sensorTypeId < SENSOR_TYPE_ID_MAX; sensorTypeId++) {
52         if (g_callbackNodes[sensorTypeId].next != NULL) {
53             isRegisterCallback = 1;
54             break;
55         }
56     }
57     return isRegisterCallback;
58 }
59 
InsertCallbackNode(int32_t sensorTypeId,const SensorUser * user)60 static int32_t InsertCallbackNode(int32_t sensorTypeId, const SensorUser *user)
61 {
62     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
63     if ((sensorTypeId >= (int32_t)SENSOR_TYPE_ID_MAX) || (sensorTypeId < 0)) {
64         HILOG_ERROR(HILOG_MODULE_SEN, "%s invalid sensor type id", __func__);
65         return SENSOR_ERROR_INVALID_PARAM;
66     }
67     CallbackNode *pre = &(g_callbackNodes[sensorTypeId]);
68     CallbackNode *node = (CallbackNode *)(g_callbackNodes[sensorTypeId].next);
69     while (node != NULL) {
70         if (node->callback == user->callback) {
71             HILOG_DEBUG(HILOG_MODULE_SEN, "%s callback has been inserted", __func__);
72             return SENSOR_OK;
73         }
74         pre = node;
75         node = (CallbackNode *)(node->next);
76     }
77     CallbackNode *nd = (CallbackNode *)malloc(sizeof(CallbackNode));
78     if (nd == NULL) {
79         HILOG_ERROR(HILOG_MODULE_SEN, "%s malloc failed", __func__);
80         return SENSOR_ERROR_INVALID_PARAM;
81     }
82     nd->callback = user->callback;
83     nd->next = NULL;
84     pre->next = nd;
85     return SENSOR_OK;
86 }
87 
DeleteCallbackNode(int32_t sensorTypeId,const SensorUser * user)88 static int32_t DeleteCallbackNode(int32_t sensorTypeId, const SensorUser *user)
89 {
90     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
91     if ((sensorTypeId >= (int32_t)SENSOR_TYPE_ID_MAX) || (sensorTypeId < 0)) {
92         HILOG_ERROR(HILOG_MODULE_SEN, "%s invalid sensor id", __func__);
93         return SENSOR_ERROR_INVALID_PARAM;
94     }
95     CallbackNode *pre = &(g_callbackNodes[sensorTypeId]);
96     CallbackNode *node = (CallbackNode *)(g_callbackNodes[sensorTypeId].next);
97     while (node != NULL) {
98         if (node->callback == user->callback) {
99             pre->next = (CallbackNode *)(node->next);
100             free(node);
101             return SENSOR_OK;
102         }
103         node = (CallbackNode *)(node->next);
104     }
105     return SENSOR_ERROR_INVALID_PARAM;
106 }
107 
GetServiceProxy()108 void *GetServiceProxy()
109 {
110     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SENSOR_SERVICE);
111     if (iUnknown == NULL) {
112         HILOG_ERROR(HILOG_MODULE_APP, "%s get sensor service failed", __func__);
113         return NULL;
114     }
115     IClientProxy *sensorApi = NULL;
116     (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&sensorApi);
117     return sensorApi;
118 }
119 
120 typedef struct SensorNotifyBuffer {
121     int32_t retCode;
122     int32_t count;
123     SensorInfo **sensorInfo;
124 } SensorNotifyBuffer;
125 
GetSensorInfos(IOwner owner,IpcIo * reply)126 int32_t GetSensorInfos(IOwner owner, IpcIo *reply)
127 {
128     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
129     SensorNotifyBuffer *notify = (SensorNotifyBuffer *)owner;
130     if (notify == NULL) {
131         HILOG_ERROR(HILOG_MODULE_APP, "%s notify is null", __func__);
132         return SENSOR_ERROR_INVALID_PARAM;
133     } else {
134         ReadInt32(reply, &(notify->retCode));
135         if (notify->retCode < 0) {
136             HILOG_ERROR(HILOG_MODULE_APP, "%s failed, retCode: %d", __func__, notify->retCode);
137             return SENSOR_ERROR_INVALID_PARAM;
138         }
139         ReadInt32(reply, &(notify->count));
140         uint32_t len = 0;
141         ReadUint32(reply, &len);
142         uint8_t *data = (uint8_t *)ReadBuffer(reply, (size_t)len);
143         if ((notify->count <= 0) || (data == NULL)) {
144             HILOG_ERROR(HILOG_MODULE_APP, "%s failed, count is incorrect or dataBuf is NULL or buff is NULL", __func__);
145             notify->retCode = SENSOR_ERROR_INVALID_PARAM;
146             return SENSOR_ERROR_INVALID_PARAM;
147         }
148         SensorInfo *sensorInfo = (SensorInfo *)(data);
149         *(notify->sensorInfo) = (SensorInfo *)malloc(sizeof(SensorInfo) * notify->count);
150         if (*(notify->sensorInfo) == NULL) {
151             HILOG_ERROR(HILOG_MODULE_APP, "%s malloc sensorInfo failed", __func__);
152             notify->retCode = SENSOR_ERROR_INVALID_PARAM;
153             return SENSOR_ERROR_INVALID_PARAM;
154         }
155         for (int32_t i = 0; i < notify->count; i++) {
156             if (memcpy_s((*(notify->sensorInfo) + i), sizeof(SensorInfo), (sensorInfo + i),
157                 sizeof(SensorInfo))) {
158                 HILOG_ERROR(HILOG_MODULE_APP, "%s copy sensorInfo failed", __func__);
159                 free(*(notify->sensorInfo));
160                 *(notify->sensorInfo) = NULL;
161                 notify->retCode = SENSOR_ERROR_INVALID_PARAM;
162                 return SENSOR_ERROR_INVALID_PARAM;
163             }
164         }
165         return notify->retCode;
166     }
167 }
168 
Notify(IOwner owner,int32_t code,IpcIo * reply)169 int32_t Notify(IOwner owner, int32_t code, IpcIo *reply)
170 {
171     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
172     int32_t functionId = -1;
173     ReadInt32(reply, &functionId);
174     if (functionId == SENSOR_SERVICE_ID_GET_ALL_SENSORS) {
175         return GetSensorInfos(owner, reply);
176     }
177     int32_t *ret = (int32_t *)owner;
178     if (ret == NULL) {
179         HILOG_ERROR(HILOG_MODULE_APP, "%s ret is null", __func__);
180         return SENSOR_ERROR_INVALID_PARAM;
181     } else {
182         if ((functionId > SENSOR_SERVICE_ID_GET_ALL_SENSORS) && (functionId < SENSORMGR_LISTENER_NAME_LEN)) {
183             ReadInt32(reply, ret);
184             HILOG_DEBUG(HILOG_MODULE_APP, "%s ret: %d", __func__, *ret);
185         } else {
186             *ret = SENSOR_ERROR_INVALID_PARAM;
187             HILOG_ERROR(HILOG_MODULE_APP, "%s functionId: %d is invalid", __func__, functionId);
188         }
189         return *ret;
190     }
191 }
192 
DispatchData(SensorEvent * sensorEvent)193 void DispatchData(SensorEvent *sensorEvent)
194 {
195     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
196     if (sensorEvent == NULL) {
197         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, sensorEvent is NULL", __func__);
198         return;
199     }
200     int32_t sensorId = sensorEvent->sensorTypeId;
201     CallbackNode *node = (CallbackNode *)(g_callbackNodes[sensorId].next);
202     while (node != NULL) {
203         node->callback(sensorEvent);
204         node = (CallbackNode *)(node->next);
205     }
206 }
207 
SensorChannelCallback(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)208 int32_t SensorChannelCallback(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
209 {
210     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
211     if (data == NULL) {
212         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, data is NULL", __func__);
213         return SENSOR_ERROR_INVALID_PARAM;
214     }
215     uint32_t len1 = 0;
216     ReadUint32(data, &len1);
217     uint8_t *eventData = (uint8_t *)ReadBuffer(data, (size_t)len1);
218     uint32_t len2 = 0;
219     ReadUint32(data, &len2);
220     uint8_t *sensorData = (uint8_t *)ReadBuffer(data, (size_t)len2);
221     if ((eventData == NULL) || (sensorData == NULL)) {
222         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, eventBuff or sensorDataBuff or buff is NULL", __func__);
223         return SENSOR_ERROR_INVALID_PARAM;
224     }
225     SensorEvent *event = (SensorEvent *)(eventData);
226     g_sensorEvent->dataLen = event->dataLen;
227     g_sensorEvent->timestamp = event->timestamp;
228     g_sensorEvent->mode = event->mode;
229     g_sensorEvent->option = event->option;
230     g_sensorEvent->sensorTypeId = event->sensorTypeId;
231     g_sensorEvent->version = event->version;
232     g_sensorEvent->data = sensorData;
233     DispatchData(g_sensorEvent);
234     return SENSOR_OK;
235 }
236 
RegisterSensorChannel(const void * proxy,int32_t sensorId)237 int32_t RegisterSensorChannel(const void *proxy, int32_t sensorId)
238 {
239     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
240     if (IsRegisterCallback()) {
241         HILOG_DEBUG(HILOG_MODULE_APP, "%s sensorChannel has been registered ", __func__);
242         return SENSOR_OK;
243     }
244     g_objectStub.func = SensorChannelCallback;
245     g_objectStub.args = NULL;
246     g_objectStub.isRemote = false;
247 
248     g_svcIdentity.handle = IPC_INVALID_HANDLE;
249     g_svcIdentity.token = SERVICE_TYPE_ANONYMOUS;
250     g_svcIdentity.cookie = (uintptr_t)&g_objectStub;
251     IpcIo request;
252     char data[MAX_IO_SIZE];
253     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_MAX_OBJECTS);
254     WriteInt32(&request, sensorId);
255     bool writeRemote = WriteRemoteObject(&request, &g_svcIdentity);
256     if (!writeRemote) {
257         HILOG_ERROR(HILOG_MODULE_APP, "%s WriteRemoteObject failed.", __func__);
258         return SENSOR_ERROR_INVALID_PARAM;
259     }
260     IClientProxy *client = (IClientProxy *)proxy;
261     if (client == NULL) {
262         HILOG_ERROR(HILOG_MODULE_APP, "%s client is null", __func__);
263         return SENSOR_ERROR_INVALID_PARAM;
264     }
265     int32_t retCode = -1;
266     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_SUBSCRIBE_SENSOR, &request, &retCode, Notify);
267     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
268         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
269         return SENSOR_ERROR_INVALID_PARAM;
270     }
271     if (g_sensorEvent == NULL) {
272         g_sensorEvent = (SensorEvent *)malloc(sizeof(SensorEvent));
273         if (g_sensorEvent == NULL) {
274             HILOG_ERROR(HILOG_MODULE_APP, "%s malloc failed", __func__);
275             return SENSOR_ERROR_INVALID_PARAM;
276         }
277     }
278     return SENSOR_OK;
279 }
280 
UnregisterSensorChannel(const void * proxy,int32_t sensorId)281 int32_t UnregisterSensorChannel(const void *proxy, int32_t sensorId)
282 {
283     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
284     if (!IsRegisterCallback()) {
285         IpcIo request;
286         char data[MAX_IO_SIZE];
287         IpcIoInit(&request, data, MAX_IO_SIZE, 0);
288         WriteInt32(&request, sensorId);
289         IClientProxy *client = (IClientProxy *)proxy;
290         if (client == NULL) {
291             HILOG_ERROR(HILOG_MODULE_APP, "%s client is null", __func__);
292             return SENSOR_ERROR_INVALID_PARAM;
293         } else {
294             int32_t retCode = -1;
295             int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_UN_SUBSCRIBE_SENSOR, &request, &retCode, Notify);
296             if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
297                 HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
298                 return SENSOR_ERROR_INVALID_PARAM;
299             }
300             if (g_sensorEvent != NULL) {
301                 free(g_sensorEvent);
302                 g_sensorEvent = NULL;
303             }
304             HILOG_DEBUG(HILOG_MODULE_APP, "%s sensorChannel has been destroyed ", __func__);
305         }
306     }
307     return SENSOR_OK;
308 }
309 
InitSensorList(const void * proxy)310 int32_t InitSensorList(const void *proxy)
311 {
312     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
313     if (proxy == NULL) {
314         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy is NULL", __func__);
315         return SENSOR_ERROR_INVALID_PARAM;
316     }
317     SensorNotifyBuffer owner = {
318         .retCode = -1,
319         .count = 0,
320         .sensorInfo = &g_sensorLists,
321     };
322     IpcIo request;
323     char data[MAX_IO_SIZE];
324     IpcIoInit(&request, data, MAX_IO_SIZE, IPC_MAX_OBJECTS);
325     IClientProxy *client = (IClientProxy *)proxy;
326     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_GET_ALL_SENSORS, &request, &owner, Notify);
327     if ((ret != SENSOR_OK) || (owner.retCode != SENSOR_OK)) {
328         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, owner.retCode);
329         return SENSOR_ERROR_INVALID_PARAM;
330     }
331     g_sensorListsLength = owner.count;
332     return SENSOR_OK;
333 }
334 
GetAllSensorsByProxy(const void * proxy,SensorInfo ** sensorInfo,int32_t * count)335 int32_t GetAllSensorsByProxy(const void *proxy, SensorInfo **sensorInfo, int32_t *count)
336 {
337     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
338     if ((sensorInfo == NULL) || (count == NULL)) {
339         HILOG_ERROR(HILOG_MODULE_APP, "%s sensorInfo or count is NULL", __func__);
340         return SENSOR_ERROR_INVALID_PARAM;
341     }
342     if (g_sensorLists == NULL) {
343         int32_t ret = InitSensorList(proxy);
344         if (ret != SENSOR_OK) {
345             HILOG_ERROR(HILOG_MODULE_APP, "%s init sensorList failed, ret: %d", __func__, ret);
346             return SENSOR_ERROR_INVALID_PARAM;
347         }
348     }
349     *count = g_sensorListsLength;
350     *sensorInfo = g_sensorLists;
351     return SENSOR_OK;
352 }
353 
ActivateSensorByProxy(const void * proxy,int32_t sensorId,const SensorUser * user)354 int32_t ActivateSensorByProxy(const void *proxy, int32_t sensorId, const SensorUser *user)
355 {
356     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
357     if ((proxy == NULL) || (user == NULL)) {
358         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user is NULL", __func__);
359         return SENSOR_ERROR_INVALID_PARAM;
360     }
361     IpcIo request;
362     char data[MAX_IO_SIZE];
363     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
364     WriteInt32(&request, sensorId);
365     int32_t retCode = -1;
366     IClientProxy *client = (IClientProxy *)proxy;
367     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_ACTIVATE_SENSOR, &request, &retCode, Notify);
368     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
369         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
370         return SENSOR_ERROR_INVALID_PARAM;
371     }
372     return retCode;
373 }
374 
DeactivateSensorByProxy(const void * proxy,int32_t sensorId,const SensorUser * user)375 int32_t DeactivateSensorByProxy(const void *proxy, int32_t sensorId, const SensorUser *user)
376 {
377     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
378     if ((proxy == NULL) || (user == NULL)) {
379         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user is NULL", __func__);
380         return SENSOR_ERROR_INVALID_PARAM;
381     }
382     IpcIo request;
383     char data[MAX_IO_SIZE];
384     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
385     WriteInt32(&request, sensorId);
386     int32_t retCode = -1;
387     IClientProxy *client = (IClientProxy *)proxy;
388     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_DEACTIVATE_SENSOR, &request, &retCode, Notify);
389     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
390         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
391         return SENSOR_ERROR_INVALID_PARAM;
392     }
393     return retCode;
394 }
395 
SetBatchByProxy(const void * proxy,int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)396 int32_t SetBatchByProxy(const void *proxy, int32_t sensorId, const SensorUser *user, int64_t samplingInterval,
397     int64_t reportInterval)
398 {
399     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
400     if ((proxy == NULL) || (user == NULL)) {
401         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user is NULL", __func__);
402         return SENSOR_ERROR_INVALID_PARAM;
403     }
404     IpcIo request;
405     char data[MAX_IO_SIZE];
406     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
407     WriteInt32(&request, sensorId);
408     WriteInt64(&request, samplingInterval);
409     WriteInt64(&request, reportInterval);
410     int32_t retCode = -1;
411     IClientProxy *client = (IClientProxy *)proxy;
412     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_SET_BATCHS, &request, &retCode, Notify);
413     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
414         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
415         return SENSOR_ERROR_INVALID_PARAM;
416     }
417     return retCode;
418 }
419 
CheckSensorTypeId(int32_t sensorTypeId)420 int32_t CheckSensorTypeId(int32_t sensorTypeId)
421 {
422     if ((g_sensorLists == NULL) || (g_sensorListsLength == 0)) {
423         HILOG_ERROR(HILOG_MODULE_APP, "%s g_sensorLists or g_sensorListsLength is invalid", __func__);
424         return SENSOR_ERROR_INVALID_PARAM;
425     }
426     for (int32_t i = 0; i < g_sensorListsLength; i++) {
427         if ((g_sensorLists + i)->sensorId == sensorTypeId) {
428             return SENSOR_OK;
429         }
430     }
431     return SENSOR_ERROR_INVALID_PARAM;
432 }
433 
SubscribeSensorByProxy(const void * proxy,int32_t sensorId,const SensorUser * user)434 int32_t SubscribeSensorByProxy(const void *proxy, int32_t sensorId, const SensorUser *user)
435 {
436     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
437     if ((proxy == NULL) || (user == NULL) || (user->callback == NULL)) {
438         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user or callback is NULL", __func__);
439         return SENSOR_ERROR_INVALID_PARAM;
440     }
441     if (CheckSensorTypeId(sensorId) != SENSOR_OK) {
442         HILOG_ERROR(HILOG_MODULE_APP, "%s sensorid: %d is invalid", __func__, sensorId);
443         return SENSOR_ERROR_INVALID_PARAM;
444     }
445     int32_t ret = RegisterSensorChannel(proxy, sensorId);
446     if (ret != SENSOR_OK) {
447         HILOG_ERROR(HILOG_MODULE_APP, "%s register sensor channel failed, ret: %d", __func__, ret);
448         return SENSOR_ERROR_INVALID_PARAM;
449     }
450     ret = InsertCallbackNode(sensorId, user);
451     if (ret != SENSOR_OK) {
452         HILOG_ERROR(HILOG_MODULE_APP, "%s insert callback node failed, ret: %d", __func__, ret);
453         return SENSOR_ERROR_INVALID_PARAM;
454     }
455     return SENSOR_OK;
456 }
457 
UnsubscribeSensorByProxy(const void * proxy,int32_t sensorId,const SensorUser * user)458 int32_t UnsubscribeSensorByProxy(const void *proxy, int32_t sensorId, const SensorUser *user)
459 {
460     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
461     if ((proxy == NULL) || (user == NULL) || (user->callback == NULL)) {
462         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user or callback is NULL", __func__);
463         return SENSOR_ERROR_INVALID_PARAM;
464     }
465     if (CheckSensorTypeId(sensorId) != SENSOR_OK) {
466         HILOG_ERROR(HILOG_MODULE_APP, "%s sensorid: %d is invalid", __func__, sensorId);
467         return SENSOR_ERROR_INVALID_PARAM;
468     }
469     int32_t ret = DeleteCallbackNode(sensorId, user);
470     if (ret != SENSOR_OK) {
471         HILOG_ERROR(HILOG_MODULE_APP, "%s delete callback node failed, ret: %d", __func__, ret);
472         return SENSOR_ERROR_INVALID_PARAM;
473     }
474 
475     ret = UnregisterSensorChannel(proxy, sensorId);
476     if (ret != SENSOR_OK) {
477         HILOG_ERROR(HILOG_MODULE_APP, "%s delete callback node failed, ret: %d", __func__, ret);
478         return SENSOR_ERROR_INVALID_PARAM;
479     }
480     return SENSOR_OK;
481 }
482 
SetModeByProxy(const void * proxy,int32_t sensorId,const SensorUser * user,int32_t mode)483 int32_t SetModeByProxy(const void *proxy, int32_t sensorId, const SensorUser *user, int32_t mode)
484 {
485     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
486     if ((proxy == NULL) || (user == NULL)) {
487         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user is NULL", __func__);
488         return SENSOR_ERROR_INVALID_PARAM;
489     }
490     IpcIo request;
491     char data[MAX_IO_SIZE];
492     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
493     WriteInt32(&request, sensorId);
494     WriteInt32(&request, mode);
495     int32_t retCode = -1;
496     IClientProxy *client = (IClientProxy *)proxy;
497     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_SET_MODE, &request, &retCode, Notify);
498     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
499         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
500         return SENSOR_ERROR_INVALID_PARAM;
501     }
502     return retCode;
503 }
504 
SetOptionByProxy(const void * proxy,int32_t sensorId,const SensorUser * user,int32_t option)505 int32_t SetOptionByProxy(const void *proxy, int32_t sensorId, const SensorUser *user, int32_t option)
506 {
507     HILOG_DEBUG(HILOG_MODULE_APP, "%s begin", __func__);
508     if ((proxy == NULL) || (user == NULL)) {
509         HILOG_ERROR(HILOG_MODULE_APP, "%s proxy or user is NULL", __func__);
510         return SENSOR_ERROR_INVALID_PARAM;
511     }
512     IpcIo request;
513     char data[MAX_IO_SIZE];
514     IpcIoInit(&request, data, MAX_IO_SIZE, 0);
515     WriteInt32(&request, sensorId);
516     WriteInt32(&request, option);
517     int32_t retCode = -1;
518     IClientProxy *client = (IClientProxy *)proxy;
519     int32_t ret = client->Invoke(client, SENSOR_SERVICE_ID_SET_OPTION, &request, &retCode, Notify);
520     if ((ret != SENSOR_OK) || (retCode != SENSOR_OK)) {
521         HILOG_ERROR(HILOG_MODULE_APP, "%s failed, ret: %d, retCode: %d", __func__, ret, retCode);
522         return SENSOR_ERROR_INVALID_PARAM;
523     }
524     return retCode;
525 }
526