• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sensor_service.h"
16 
17 #include <ohos_init.h>
18 #include "feature.h"
19 #include "iproxy_server.h"
20 #include "samgr_lite.h"
21 #include "sensor_service_impl.h"
22 #include "service.h"
23 
GetAllSensorsInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)24 int32_t GetAllSensorsInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
25 {
26     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
27     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_GetAllSensors);
28     if (defaultApi == NULL) {
29         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
30         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
31         return SENSOR_ERROR_UNKNOWN;
32     }
33     SensorInfo *sensorInfo = NULL;
34     int32_t count = 0;
35     int32_t ret = defaultApi->GetAllSensors(&sensorInfo, &count);
36     if (ret != SENSOR_OK) {
37         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, ret: %d", SENSOR_SERVICE, __func__, ret);
38         IpcIoPushInt32(reply, ret);
39         return ret;
40     }
41     BuffPtr dataBuff = {
42         .buffSz = (uint32_t)(count * sizeof(SensorInfo)),
43         .buff = sensorInfo
44     };
45     IpcIoPushInt32(reply, SENSOR_OK);
46     IpcIoPushInt32(reply, count);
47     IpcIoPushDataBuff(reply, &dataBuff);
48     return SENSOR_OK;
49 }
50 
ActivateSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)51 int32_t ActivateSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
52 {
53     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
54     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_ActivateSensor);
55     int32_t sensorId = IpcIoPopInt32(req);
56     if (defaultApi == NULL) {
57         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
58         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
59         return SENSOR_ERROR_UNKNOWN;
60     } else {
61         SensorUser sensorUser;
62         int32_t ret = defaultApi->ActivateSensor(sensorId, &sensorUser);
63         IpcIoPushInt32(reply, ret);
64         return ret;
65     }
66 }
67 
DeactivateSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)68 int32_t DeactivateSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
69 {
70     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
71     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_DeactivateSensor);
72     int32_t sensorId = IpcIoPopInt32(req);
73     if (defaultApi == NULL) {
74         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
75         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
76         return SENSOR_ERROR_UNKNOWN;
77     } else {
78         SensorUser sensorUser;
79         int32_t ret = defaultApi->DeactivateSensor(sensorId, &sensorUser);
80         IpcIoPushInt32(reply, ret);
81         return ret;
82     }
83 }
84 
SetBatchInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)85 int32_t SetBatchInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
86 {
87     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
88     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_SetBatchs);
89     int32_t sensorId = IpcIoPopInt32(req);
90     int64_t updateInterval = IpcIoPopInt64(req);
91     int64_t maxDelay = IpcIoPopInt64(req);
92     if (defaultApi == NULL) {
93         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
94         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
95         return SENSOR_ERROR_UNKNOWN;
96     } else {
97         SensorUser sensorUser;
98         int32_t ret = defaultApi->SetBatch(sensorId, &sensorUser, updateInterval, maxDelay);
99         IpcIoPushInt32(reply, ret);
100         return ret;
101     }
102 }
103 
SubscribeSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)104 int32_t SubscribeSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
105 {
106     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
107     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_SubscribeSensor);
108     int32_t sensorId = IpcIoPopInt32(req);
109     if (defaultApi == NULL) {
110         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
111         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
112         return SENSOR_ERROR_UNKNOWN;
113     } else {
114         SensorUser sensorUser;
115         int32_t ret = defaultApi->SubscribeSensor(sensorId, &sensorUser);
116         SetSvcIdentity(req, reply);
117         IpcIoPushInt32(reply, ret);
118         return ret;
119     }
120 }
121 
UnsubscribeSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)122 int32_t UnsubscribeSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
123 {
124     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
125     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_UnsubscribeSensor);
126     int32_t sensorId = IpcIoPopInt32(req);
127     if (defaultApi == NULL) {
128         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
129         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
130         return SENSOR_ERROR_UNKNOWN;
131     } else {
132         SensorUser sensorUser;
133         int32_t ret = defaultApi->UnsubscribeSensor(sensorId, &sensorUser);
134         IpcIoPushInt32(reply, ret);
135         return ret;
136     }
137 }
138 
SetModeInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)139 int32_t SetModeInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
140 {
141     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
142     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_SetMode);
143     int32_t sensorId = IpcIoPopInt32(req);
144     int32_t mode = IpcIoPopInt32(req);
145     if (defaultApi == NULL) {
146         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
147         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
148         return SENSOR_ERROR_UNKNOWN;
149     } else {
150         SensorUser sensorUser;
151         int32_t ret = defaultApi->SetMode(sensorId, &sensorUser, mode);
152         IpcIoPushInt32(reply, ret);
153         return ret;
154     }
155 }
156 
SetOptionInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)157 int32_t SetOptionInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
158 {
159     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
160     IpcIoPushInt32(reply, SENSOR_SERVICE_ID_SetOption);
161     int32_t sensorId = IpcIoPopInt32(req);
162     int32_t option = IpcIoPopInt32(req);
163     if (defaultApi == NULL) {
164         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
165         IpcIoPushInt32(reply, SENSOR_ERROR_UNKNOWN);
166         return SENSOR_ERROR_UNKNOWN;
167     } else {
168         SensorUser sensorUser;
169         int32_t ret = defaultApi->SetOption(sensorId, &sensorUser, option);
170         IpcIoPushInt32(reply, ret);
171         return ret;
172     }
173 }
174 
175 static InvokeFunc g_invokeFuncList[] = {
176     GetAllSensorsInvoke,
177     ActivateSensorInvoke,
178     DeactivateSensorInvoke,
179     SetBatchInvoke,
180     SubscribeSensorInvoke,
181     UnsubscribeSensorInvoke,
182     SetModeInvoke,
183     SetOptionInvoke,
184 };
185 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)186 int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
187 {
188     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
189     if ((iProxy == NULL) || (req == NULL) || (reply == NULL)) {
190         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s iProxy or req or reply is NULL",
191             SENSOR_SERVICE, __func__);
192         return SENSOR_ERROR_INVALID_PARAM;
193     }
194     SensorFeatureApi *defaultApi = (SensorFeatureApi *)iProxy;
195     if ((funcId >= 0) && (funcId <= SENSOR_SERVICE_ID_UnsubscribeSensor)) {
196         return g_invokeFuncList[funcId](defaultApi, req, reply);
197     }
198     return SENSOR_ERROR_INVALID_PARAM;
199 }
200 
201 static SensorService g_sensorService = {
202     .GetName = SENSOR_GetName,
203     .Initialize = Initialize,
204     .MessageHandle = MessageHandle,
205     .GetTaskConfig = GetTaskConfig,
206     SERVER_IPROXY_IMPL_BEGIN,
207     .Invoke = Invoke,
208     .GetAllSensors = GetAllSensorsImpl,
209     .ActivateSensor = ActivateSensorImpl,
210     .DeactivateSensor = DeactivateSensorImpl,
211     .SetBatch = SetBatchImpl,
212     .SubscribeSensor = SubscribeSensorImpl,
213     .UnsubscribeSensor = UnsubscribeSensorImpl,
214     .SetMode = SetModeImpl,
215     .SetOption = SetOptionImpl,
216     IPROXY_END,
217 };
218 
Init(void)219 static void Init(void)
220 {
221     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
222         SENSOR_SERVICE, __func__);
223     SAMGR_GetInstance()->RegisterService((Service *)&g_sensorService);
224     SAMGR_GetInstance()->RegisterDefaultFeatureApi(SENSOR_SERVICE, GET_IUNKNOWN(g_sensorService));
225 }
226 SYSEX_SERVICE_INIT(Init);