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 WriteInt32(reply, SENSOR_SERVICE_ID_GET_ALL_SENSORS);
28 if (defaultApi == NULL) {
29 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
30 WriteInt32(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 WriteInt32(reply, ret);
39 return ret;
40 }
41 WriteInt32(reply, SENSOR_OK);
42 WriteInt32(reply, count);
43 WriteUint32(reply, (uint32_t)(count * sizeof(SensorInfo)));
44 WriteBuffer(reply, (void *)(sensorInfo), (uint32_t)(count * sizeof(SensorInfo)));
45 return SENSOR_OK;
46 }
47
ActivateSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)48 int32_t ActivateSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
49 {
50 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
51 WriteInt32(reply, SENSOR_SERVICE_ID_ACTIVATE_SENSOR);
52 int32_t sensorId = -1;
53 ReadInt32(req, &sensorId);
54 if (defaultApi == NULL) {
55 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
56 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
57 return SENSOR_ERROR_UNKNOWN;
58 } else {
59 SensorUser sensorUser;
60 int32_t ret = defaultApi->ActivateSensor(sensorId, &sensorUser);
61 WriteInt32(reply, ret);
62 return ret;
63 }
64 }
65
DeactivateSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)66 int32_t DeactivateSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
67 {
68 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
69 WriteInt32(reply, SENSOR_SERVICE_ID_DEACTIVATE_SENSOR);
70 int32_t sensorId = -1;
71 ReadInt32(req, &sensorId);
72 if (defaultApi == NULL) {
73 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
74 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
75 return SENSOR_ERROR_UNKNOWN;
76 } else {
77 SensorUser sensorUser;
78 int32_t ret = defaultApi->DeactivateSensor(sensorId, &sensorUser);
79 WriteInt32(reply, ret);
80 return ret;
81 }
82 }
83
SetBatchInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)84 int32_t SetBatchInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
85 {
86 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
87 WriteInt32(reply, SENSOR_SERVICE_ID_SET_BATCHS);
88 int32_t sensorId = -1;
89 ReadInt32(req, &sensorId);
90 int64_t updateInterval;
91 ReadInt64(req, &updateInterval);
92 int64_t maxDelay;
93 ReadInt64(req, &maxDelay);
94 if (defaultApi == NULL) {
95 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
96 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
97 return SENSOR_ERROR_UNKNOWN;
98 } else {
99 SensorUser sensorUser;
100 int32_t ret = defaultApi->SetBatch(sensorId, &sensorUser, updateInterval, maxDelay);
101 WriteInt32(reply, ret);
102 return ret;
103 }
104 }
105
SubscribeSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)106 int32_t SubscribeSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
107 {
108 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
109 WriteInt32(reply, SENSOR_SERVICE_ID_SUBSCRIBE_SENSOR);
110 int32_t sensorId = -1;
111 ReadInt32(req, &sensorId);
112 if (defaultApi == NULL) {
113 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
114 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
115 return SENSOR_ERROR_UNKNOWN;
116 } else {
117 SensorUser sensorUser;
118 int32_t ret = defaultApi->SubscribeSensor(sensorId, &sensorUser);
119 SetSvcIdentity(req, reply);
120 WriteInt32(reply, ret);
121 return ret;
122 }
123 }
124
UnsubscribeSensorInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)125 int32_t UnsubscribeSensorInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
126 {
127 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
128 WriteInt32(reply, SENSOR_SERVICE_ID_UN_SUBSCRIBE_SENSOR);
129 int32_t sensorId = -1;
130 ReadInt32(req, &sensorId);
131 if (defaultApi == NULL) {
132 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
133 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
134 return SENSOR_ERROR_UNKNOWN;
135 } else {
136 SensorUser sensorUser;
137 int32_t ret = defaultApi->UnsubscribeSensor(sensorId, &sensorUser);
138 WriteInt32(reply, ret);
139 return ret;
140 }
141 }
142
SetModeInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)143 int32_t SetModeInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
144 {
145 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
146 WriteInt32(reply, SENSOR_SERVICE_ID_SET_MODE);
147 int32_t sensorId = -1;
148 ReadInt32(req, &sensorId);
149 int32_t mode = -1;
150 ReadInt32(req, &mode);
151 if (defaultApi == NULL) {
152 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
153 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
154 return SENSOR_ERROR_UNKNOWN;
155 } else {
156 SensorUser sensorUser;
157 int32_t ret = defaultApi->SetMode(sensorId, &sensorUser, mode);
158 WriteInt32(reply, ret);
159 return ret;
160 }
161 }
162
SetOptionInvoke(SensorFeatureApi * defaultApi,IpcIo * req,IpcIo * reply)163 int32_t SetOptionInvoke(SensorFeatureApi *defaultApi, IpcIo *req, IpcIo *reply)
164 {
165 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
166 WriteInt32(reply, SENSOR_SERVICE_ID_SET_OPTION);
167 int32_t sensorId = -1;
168 ReadInt32(req, &sensorId);
169 int32_t option = -1;
170 ReadInt32(req, &option);
171 if (defaultApi == NULL) {
172 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s failed, defaultApi is null", SENSOR_SERVICE, __func__);
173 WriteInt32(reply, SENSOR_ERROR_UNKNOWN);
174 return SENSOR_ERROR_UNKNOWN;
175 } else {
176 SensorUser sensorUser;
177 int32_t ret = defaultApi->SetOption(sensorId, &sensorUser, option);
178 WriteInt32(reply, ret);
179 return ret;
180 }
181 }
182
183 static InvokeFunc g_invokeFuncList[] = {
184 GetAllSensorsInvoke,
185 ActivateSensorInvoke,
186 DeactivateSensorInvoke,
187 SetBatchInvoke,
188 SubscribeSensorInvoke,
189 UnsubscribeSensorInvoke,
190 SetModeInvoke,
191 SetOptionInvoke,
192 };
193
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)194 int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
195 {
196 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin", SENSOR_SERVICE, __func__);
197 if ((iProxy == NULL) || (req == NULL) || (reply == NULL)) {
198 HILOG_ERROR(HILOG_MODULE_APP, "[SERVICE:%s]: %s iProxy or req or reply is NULL",
199 SENSOR_SERVICE, __func__);
200 return SENSOR_ERROR_INVALID_PARAM;
201 }
202 SensorFeatureApi *defaultApi = (SensorFeatureApi *)iProxy;
203 if ((funcId >= 0) && (funcId <= SENSOR_SERVICE_ID_UN_SUBSCRIBE_SENSOR)) {
204 return g_invokeFuncList[funcId](defaultApi, req, reply);
205 }
206 return SENSOR_ERROR_INVALID_PARAM;
207 }
208
209 static SensorService g_sensorService = {
210 .GetName = SENSOR_GetName,
211 .Initialize = Initialize,
212 .MessageHandle = MessageHandle,
213 .GetTaskConfig = GetTaskConfig,
214 SERVER_IPROXY_IMPL_BEGIN,
215 .Invoke = Invoke,
216 .GetAllSensors = GetAllSensorsImpl,
217 .ActivateSensor = ActivateSensorImpl,
218 .DeactivateSensor = DeactivateSensorImpl,
219 .SetBatch = SetBatchImpl,
220 .SubscribeSensor = SubscribeSensorImpl,
221 .UnsubscribeSensor = UnsubscribeSensorImpl,
222 .SetMode = SetModeImpl,
223 .SetOption = SetOptionImpl,
224 IPROXY_END,
225 };
226
Init(void)227 static void Init(void)
228 {
229 HILOG_DEBUG(HILOG_MODULE_APP, "[SERVICE:%s]: %s begin",
230 SENSOR_SERVICE, __func__);
231 SAMGR_GetInstance()->RegisterService((Service *)&g_sensorService);
232 SAMGR_GetInstance()->RegisterDefaultFeatureApi(SENSOR_SERVICE, GET_IUNKNOWN(g_sensorService));
233 }
234 SYSEX_SERVICE_INIT(Init);