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_service_impl.h"
17 
18 #include <securec.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "ipc_skeleton.h"
22 #include "sensor_service.h"
23 #include "sensor_type.h"
24 
25 static struct SensorInformation *g_sensorLists;
26 static int32_t g_sensorListsLength;
27 const struct SensorInterface *g_sensorDevice;
28 static SvcIdentity g_svcIdentity = {
29     .handle = 0,
30     .token = 0,
31 };
32 
InitSensorList()33 int32_t InitSensorList()
34 {
35     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
36 #ifdef HAS_HDI_SENSOR_LITE_PRAT
37     if (g_sensorDevice == NULL) {
38         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
39             SENSOR_SERVICE, __func__);
40         return SENSOR_ERROR_INVALID_PARAM;
41     }
42     int32_t ret = g_sensorDevice->GetAllSensors(&g_sensorLists, &g_sensorListsLength);
43     if ((ret != 0) || (g_sensorLists == NULL)) {
44         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s getAllSensors failed, ret: %d",
45             SENSOR_SERVICE, __func__, ret);
46         return SENSOR_ERROR_INVALID_PARAM;
47     }
48 #endif // HAS_HDI_SENSOR_LITE_PRAT
49     return SENSOR_OK;
50 }
51 
SENSOR_GetName(Service * service)52 const char *SENSOR_GetName(Service *service)
53 {
54     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
55         SENSOR_SERVICE, __func__);
56     return SENSOR_SERVICE;
57 }
58 
SensorDataCallback(const struct SensorEvent * event)59 static int SensorDataCallback(const struct SensorEvent *event)
60 {
61     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
62     if ((event == NULL) || (event->dataLen == 0)) {
63         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s event is NULL",
64             SENSOR_SERVICE, __func__);
65         return SENSOR_ERROR_INVALID_ID;
66     }
67     IpcIo io;
68     uint8_t data[MAX_IO_SIZE];
69     IpcIoInit(&io, data, MAX_IO_SIZE, IPC_MAX_OBJECTS);
70 
71     WriteUint32(&io, (uint32_t)(sizeof(struct SensorEvent)));
72     WriteBuffer(&io, (void *)(event), (uint32_t)(sizeof(struct SensorEvent)));
73     WriteUint32(&io, (uint32_t)(event->dataLen));
74     WriteBuffer(&io, (void *)(event->data), (uint32_t)(event->dataLen));
75     MessageOption option;
76     MessageOptionInit(&option);
77     option.flags = TF_OP_ASYNC;
78     SendRequest(g_svcIdentity, 0, &io, NULL, option, NULL);
79     return SENSOR_OK;
80 }
81 
SetSvcIdentity(IpcIo * req,const IpcIo * reply)82 void SetSvcIdentity(IpcIo *req, const IpcIo *reply)
83 {
84     SvcIdentity sid;
85     bool ret = ReadRemoteObject(req, &sid);
86     if (!ret) {
87         HILOG_ERROR(HILOG_MODULE_APP, "%s ReadRemoteObject failed ", __func__);
88         return;
89     }
90     g_svcIdentity.handle = sid.handle;
91     g_svcIdentity.token = sid.token;
92 }
93 
Initialize(Service * service,Identity identity)94 BOOL Initialize(Service *service, Identity identity)
95 {
96     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
97 #ifdef HAS_HDI_SENSOR_LITE_PRAT
98     g_sensorDevice = NewSensorInterfaceInstance();
99     if (g_sensorDevice == NULL) {
100         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
101             SENSOR_SERVICE, __func__);
102     }
103 #endif // HAS_HDI_SENSOR_LITE_PRAT
104     return TRUE;
105 }
106 
MessageHandle(Service * service,Request * msg)107 BOOL MessageHandle(Service *service, Request *msg)
108 {
109     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
110         SENSOR_SERVICE, __func__);
111     return TRUE;
112 }
113 
GetTaskConfig(Service * service)114 TaskConfig GetTaskConfig(Service *service)
115 {
116     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
117     TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, TASK_CONFIG_STACK_SIZE, TASK_CONFIG_QUEUE_SIZE, SHARED_TASK};
118     return config;
119 }
120 
GetAllSensorsImpl(SensorInfo ** sensorInfo,int32_t * count)121 int32_t GetAllSensorsImpl(SensorInfo **sensorInfo, int32_t *count)
122 {
123     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
124     if ((sensorInfo == NULL) || (count == NULL)) {
125         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorInfo or count is null",
126             SENSOR_SERVICE, __func__);
127         return SENSOR_ERROR_INVALID_PARAM;
128     }
129     if ((g_sensorLists == NULL) || (g_sensorListsLength <= 0)) {
130         if (InitSensorList() != 0) {
131             HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s initSensorList failed!",
132                 SENSOR_SERVICE, __func__);
133             return SENSOR_ERROR_INVALID_PARAM;
134         }
135     }
136     *sensorInfo = (SensorInfo *)g_sensorLists;
137     *count = g_sensorListsLength;
138     return SENSOR_OK;
139 }
140 
ActivateSensorImpl(int32_t sensorId,const SensorUser * user)141 int32_t ActivateSensorImpl(int32_t sensorId, const SensorUser *user)
142 {
143     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
144         SENSOR_SERVICE, __func__);
145     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
146         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
147             SENSOR_SERVICE, __func__, sensorId);
148         return SENSOR_ERROR_INVALID_ID;
149     }
150     if (user == NULL) {
151         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s user is NULL",
152             SENSOR_SERVICE, __func__);
153         return SENSOR_ERROR_INVALID_PARAM;
154     }
155 #ifdef HAS_HDI_SENSOR_LITE_PRAT
156     if (g_sensorDevice == NULL) {
157         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
158             SENSOR_SERVICE, __func__);
159         return SENSOR_ERROR_INVALID_PARAM;
160     }
161     int32_t ret = g_sensorDevice->Enable(sensorId);
162     if (ret != 0) {
163         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s ActivateSensor sensor failed, ret: %d",
164             SENSOR_SERVICE, __func__, ret);
165         return ret;
166     }
167 #endif // HAS_HDI_SENSOR_LITE_PRAT
168     return SENSOR_OK;
169 }
170 
DeactivateSensorImpl(int32_t sensorId,const SensorUser * user)171 int32_t DeactivateSensorImpl(int32_t sensorId, const SensorUser *user)
172 {
173     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
174         SENSOR_SERVICE, __func__);
175     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
176         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
177             SENSOR_SERVICE, __func__, sensorId);
178         return SENSOR_ERROR_INVALID_ID;
179     }
180     if (user == NULL) {
181         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s user is NULL",
182             SENSOR_SERVICE, __func__);
183         return SENSOR_ERROR_INVALID_PARAM;
184     }
185 #ifdef HAS_HDI_SENSOR_LITE_PRAT
186     if (g_sensorDevice == NULL) {
187         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
188             SENSOR_SERVICE, __func__);
189         return SENSOR_ERROR_INVALID_PARAM;
190     }
191     int32_t ret = g_sensorDevice->Disable(sensorId);
192     if (ret != 0) {
193         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s DeactivateSensor sensor failed, ret: %d",
194             SENSOR_SERVICE, __func__, ret);
195         return ret;
196     }
197 #endif // HAS_HDI_SENSOR_LITE_PRAT
198     return SENSOR_OK;
199 }
200 
SetBatchImpl(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)201 int32_t SetBatchImpl(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval)
202 {
203     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
204         SENSOR_SERVICE, __func__);
205     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
206         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
207             SENSOR_SERVICE, __func__, sensorId);
208         return SENSOR_ERROR_INVALID_ID;
209     }
210     if ((samplingInterval < 0) || (reportInterval < 0)) {
211         HILOG_ERROR(HILOG_MODULE_APP,
212             "[SERVICE:%s]: %s samplingInterval: %lld or reportInterval: %lld is invalid",
213             SENSOR_SERVICE, __func__, samplingInterval, reportInterval);
214         return SENSOR_ERROR_INVALID_PARAM;
215     }
216     return SENSOR_OK;
217 }
218 
SubscribeSensorImpl(int32_t sensorId,const SensorUser * user)219 int32_t SubscribeSensorImpl(int32_t sensorId, const SensorUser *user)
220 {
221     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
222         SENSOR_SERVICE, __func__);
223     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
224         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
225             SENSOR_SERVICE, __func__, sensorId);
226         return SENSOR_ERROR_INVALID_ID;
227     }
228     if (user == NULL) {
229         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s user is NULL",
230             SENSOR_SERVICE, __func__);
231         return SENSOR_ERROR_INVALID_PARAM;
232     }
233 #ifdef HAS_HDI_SENSOR_LITE_PRAT
234     if (g_sensorDevice == NULL) {
235         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
236             SENSOR_SERVICE, __func__);
237         return SENSOR_ERROR_INVALID_PARAM;
238     }
239     int32_t ret = g_sensorDevice->Register(0, (RecordDataCallback)SensorDataCallback);
240     if (ret != 0) {
241         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s register sensor user failed, ret: %d",
242             SENSOR_SERVICE, __func__, ret);
243         return SENSOR_ERROR_INVALID_PARAM;
244     }
245 #endif // HAS_HDI_SENSOR_LITE_PRAT
246     return SENSOR_OK;
247 }
248 
UnsubscribeSensorImpl(int32_t sensorId,const SensorUser * user)249 int32_t UnsubscribeSensorImpl(int32_t sensorId, const SensorUser *user)
250 {
251     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
252         SENSOR_SERVICE, __func__);
253     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
254         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
255             SENSOR_SERVICE, __func__, sensorId);
256         return SENSOR_ERROR_INVALID_ID;
257     }
258     if (user == NULL) {
259         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s user is NULL",
260             SENSOR_SERVICE, __func__);
261         return SENSOR_ERROR_INVALID_PARAM;
262     }
263 #ifdef HAS_HDI_SENSOR_LITE_PRAT
264     if (g_sensorDevice == NULL) {
265         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s g_sensorDevice is NULL",
266             SENSOR_SERVICE, __func__);
267         return SENSOR_ERROR_INVALID_PARAM;
268     }
269     int32_t ret = g_sensorDevice->Unregister(0, (RecordDataCallback)SensorDataCallback);
270     if (ret != 0) {
271         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s unregister sensor failed, ret: %d",
272             SENSOR_SERVICE, __func__, ret);
273         return SENSOR_ERROR_INVALID_PARAM;
274     }
275 #endif // HAS_HDI_SENSOR_LITE_PRAT
276     return SENSOR_OK;
277 }
278 
SetModeImpl(int32_t sensorId,const SensorUser * user,int32_t mode)279 int32_t SetModeImpl(int32_t sensorId, const SensorUser *user, int32_t mode)
280 {
281     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
282         SENSOR_SERVICE, __func__);
283     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
284         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
285             SENSOR_SERVICE, __func__, sensorId);
286         return SENSOR_ERROR_INVALID_ID;
287     }
288     return SENSOR_OK;
289 }
290 
SetOptionImpl(int32_t sensorId,const SensorUser * user,int32_t option)291 int32_t SetOptionImpl(int32_t sensorId, const SensorUser *user, int32_t option)
292 {
293     HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
294         SENSOR_SERVICE, __func__);
295     if ((sensorId >= SENSOR_TYPE_ID_MAX) || (sensorId < 0)) {
296         HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s sensorId: %d is invalid",
297             SENSOR_SERVICE, __func__, sensorId);
298         return SENSOR_ERROR_INVALID_ID;
299     }
300     return SENSOR_OK;
301 }
302