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