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