1 /*
2 * Copyright (c) 2021-2023 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_impl.h"
17 #include <cinttypes>
18 #include <unordered_map>
19 #include <mutex>
20 #include <iproxy_broker.h>
21 #include "sensor_uhdf_log.h"
22 #include "hitrace_meter.h"
23 #include "sensor_dump.h"
24
25 #define HDF_LOG_TAG uhdf_sensor_service
26 #define DEFAULT_SDC_SENSOR_INFO_SIZE 2
27
28 namespace OHOS {
29 namespace HDI {
30 namespace Sensor {
31 namespace V1_1 {
32 namespace {
33 constexpr int32_t CALLBACK_CTOUNT_THRESHOLD = 1;
34 using GroupIdCallBackMap = std::unordered_map<int32_t, std::vector<sptr<ISensorCallbackVdi>>>;
35 GroupIdCallBackMap g_groupIdCallBackMap;
36 std::mutex g_mutex;
37 } // namespace
38
ReportSensorEventsData(int32_t sensorType,const struct SensorEvents * event)39 int32_t ReportSensorEventsData(int32_t sensorType, const struct SensorEvents *event)
40 {
41 std::lock_guard<std::mutex> lock(g_mutex);
42 auto groupCallBackIter = g_groupIdCallBackMap.find(sensorType);
43 if (groupCallBackIter == g_groupIdCallBackMap.end()) {
44 return SENSOR_SUCCESS;
45 }
46
47 HdfSensorEventsVdi hdfSensorEvents;
48 hdfSensorEvents.sensorId = event->sensorId;
49 hdfSensorEvents.version = event->version;
50 hdfSensorEvents.timestamp = event->timestamp;
51 hdfSensorEvents.option = event->option;
52 hdfSensorEvents.mode = event->mode;
53 hdfSensorEvents.dataLen = event->dataLen;
54 uint32_t len = event->dataLen;
55 uint8_t *tmp = event->data;
56
57 while ((len--) != 0) {
58 hdfSensorEvents.data.push_back(*tmp);
59 tmp++;
60 }
61
62 for (auto callBack : g_groupIdCallBackMap[sensorType]) {
63 callBack->OnDataEventVdi(hdfSensorEvents);
64 }
65
66 return SENSOR_SUCCESS;
67 }
68
TradtionalSensorDataCallback(const struct SensorEvents * event)69 int32_t TradtionalSensorDataCallback(const struct SensorEvents *event)
70 {
71 if (event == nullptr || event->data == nullptr) {
72 HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
73 return SENSOR_FAILURE;
74 }
75
76 (void)ReportSensorEventsData(TRADITIONAL_SENSOR_TYPE, event);
77
78 return SENSOR_SUCCESS;
79 }
80
MedicalSensorDataCallback(const struct SensorEvents * event)81 int32_t MedicalSensorDataCallback(const struct SensorEvents *event)
82 {
83 if (event == nullptr || event->data == nullptr) {
84 HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
85 return SENSOR_FAILURE;
86 }
87
88 (void)ReportSensorEventsData(MEDICAL_SENSOR_TYPE, event);
89
90 return SENSOR_SUCCESS;
91 }
92
~SensorImpl()93 SensorImpl::~SensorImpl()
94 {
95 FreeSensorInterfaceInstance();
96 }
97
Init()98 int32_t SensorImpl::Init()
99 {
100 sensorInterface = NewSensorInterfaceInstance();
101 if (sensorInterface == nullptr) {
102 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
103 return HDF_FAILURE;
104 }
105
106 SensorDevRegisterDump();
107 return HDF_SUCCESS;
108 }
109
GetAllSensorInfo(std::vector<HdfSensorInformationVdi> & info)110 int32_t SensorImpl::GetAllSensorInfo(std::vector<HdfSensorInformationVdi> &info)
111 {
112 HDF_LOGI("%{public}s: Enter the GetAllSensorInfo function.", __func__);
113 if (sensorInterface == nullptr || sensorInterface->GetAllSensors == nullptr) {
114 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
115 return HDF_FAILURE;
116 }
117
118 struct SensorInformation *sensorInfo = nullptr;
119 struct SensorInformation *tmp = nullptr;
120 int32_t count = 0;
121
122 StartTrace(HITRACE_TAG_SENSORS, "GetAllSensorInfo");
123 int32_t ret = sensorInterface->GetAllSensors(&sensorInfo, &count);
124 FinishTrace(HITRACE_TAG_SENSORS);
125 if (ret != SENSOR_SUCCESS) {
126 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
127 return ret;
128 }
129
130 if (count <= 0) {
131 HDF_LOGE("%{public}s failed, count<=0", __func__);
132 return HDF_FAILURE;
133 }
134
135 tmp = sensorInfo;
136 while (count--) {
137 HdfSensorInformationVdi hdfSensorInfo;
138 std::string sensorName(tmp->sensorName);
139 hdfSensorInfo.sensorName = sensorName;
140 std::string vendorName(tmp->vendorName);
141 hdfSensorInfo.vendorName = vendorName;
142 std::string firmwareVersion(tmp->firmwareVersion);
143 hdfSensorInfo.firmwareVersion = firmwareVersion;
144 std::string hardwareVersion(tmp->hardwareVersion);
145 hdfSensorInfo.hardwareVersion = hardwareVersion;
146 hdfSensorInfo.sensorTypeId = tmp->sensorTypeId;
147 hdfSensorInfo.sensorId = tmp->sensorId;
148 hdfSensorInfo.maxRange = tmp->maxRange;
149 hdfSensorInfo.accuracy = tmp->accuracy;
150 hdfSensorInfo.power = tmp->power;
151 hdfSensorInfo.minDelay = tmp->minDelay;
152 hdfSensorInfo.maxDelay = tmp->maxDelay;
153 hdfSensorInfo.fifoMaxEventCount = tmp->fifoMaxEventCount;
154 info.push_back(std::move(hdfSensorInfo));
155 tmp++;
156 }
157
158 return HDF_SUCCESS;
159 }
160
Enable(int32_t sensorId)161 int32_t SensorImpl::Enable(int32_t sensorId)
162 {
163 HDF_LOGI("%{public}s: Enter the Enable function, sensorId is %{public}d", __func__, sensorId);
164 if (sensorInterface == nullptr || sensorInterface->Enable == nullptr) {
165 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
166 return HDF_FAILURE;
167 }
168
169 StartTrace(HITRACE_TAG_SENSORS, "Enable");
170 int32_t ret = sensorInterface->Enable(sensorId);
171 FinishTrace(HITRACE_TAG_SENSORS);
172 if (ret != SENSOR_SUCCESS) {
173 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
174 }
175
176 return ret;
177 }
178
Disable(int32_t sensorId)179 int32_t SensorImpl::Disable(int32_t sensorId)
180 {
181 HDF_LOGI("%{public}s: Enter the Disable function, sensorId is %{public}d", __func__, sensorId);
182 if (sensorInterface == nullptr || sensorInterface->Disable == nullptr) {
183 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
184 return HDF_FAILURE;
185 }
186
187 StartTrace(HITRACE_TAG_SENSORS, "Disable");
188 int32_t ret = sensorInterface->Disable(sensorId);
189 FinishTrace(HITRACE_TAG_SENSORS);
190 if (ret != SENSOR_SUCCESS) {
191 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
192 }
193
194 return ret;
195 }
196
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)197 int32_t SensorImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
198 {
199 HDF_LOGI("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "], \
200 reportInterval is [%{public}" PRId64 "].", __func__, sensorId, samplingInterval, reportInterval);
201 if (sensorInterface == nullptr || sensorInterface->SetBatch == nullptr) {
202 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
203 return HDF_FAILURE;
204 }
205
206 StartTrace(HITRACE_TAG_SENSORS, "SetBatch");
207 int32_t ret = sensorInterface->SetBatch(sensorId, samplingInterval, reportInterval);
208 FinishTrace(HITRACE_TAG_SENSORS);
209 if (ret != SENSOR_SUCCESS) {
210 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
211 }
212
213 return ret;
214 }
215
SetMode(int32_t sensorId,int32_t mode)216 int32_t SensorImpl::SetMode(int32_t sensorId, int32_t mode)
217 {
218 HDF_LOGI("%{public}s: Enter the SetMode function, sensorId is %{public}d, mode is %{public}d",
219 __func__, sensorId, mode);
220 if (sensorInterface == nullptr || sensorInterface->SetMode == nullptr) {
221 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
222 return HDF_FAILURE;
223 }
224
225 StartTrace(HITRACE_TAG_SENSORS, "SetMode");
226 int32_t ret = sensorInterface->SetMode(sensorId, mode);
227 FinishTrace(HITRACE_TAG_SENSORS);
228 if (ret != SENSOR_SUCCESS) {
229 HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
230 }
231
232 return ret;
233 }
234
SetOption(int32_t sensorId,uint32_t option)235 int32_t SensorImpl::SetOption(int32_t sensorId, uint32_t option)
236 {
237 HDF_LOGI("%{public}s: Enter the SetOption function, sensorId is %{public}d, option is %{public}u",
238 __func__, sensorId, option);
239 if (sensorInterface == nullptr || sensorInterface->SetOption == nullptr) {
240 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
241 return HDF_FAILURE;
242 }
243
244 StartTrace(HITRACE_TAG_SENSORS, "SetOption");
245 int32_t ret = sensorInterface->SetOption(sensorId, option);
246 FinishTrace(HITRACE_TAG_SENSORS);
247 if (ret != SENSOR_SUCCESS) {
248 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
249 }
250
251 return ret;
252 }
253
Register(int32_t groupId,const sptr<ISensorCallbackVdi> & callbackObj)254 int32_t SensorImpl::Register(int32_t groupId, const sptr<ISensorCallbackVdi> &callbackObj)
255 {
256 HDF_LOGI("%{public}s: Enter the Register function, groupId is %{public}d", __func__, groupId);
257 if (sensorInterface == nullptr || sensorInterface->Register == nullptr) {
258 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
259 return HDF_FAILURE;
260 }
261
262 if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
263 HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
264 return SENSOR_INVALID_PARAM;
265 }
266
267 std::lock_guard<std::mutex> lock(g_mutex);
268 auto groupCallBackIter = g_groupIdCallBackMap.find(groupId);
269 if (groupCallBackIter != g_groupIdCallBackMap.end()) {
270 auto callBackIter =
271 find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
272 [&callbackObj](const sptr<ISensorCallbackVdi> &callbackRegistered) {
273 const sptr<IRemoteObject> &lhs = callbackObj->HandleCallbackDeath();
274 const sptr<IRemoteObject> &rhs = callbackRegistered->HandleCallbackDeath();
275 return lhs == rhs;
276 });
277 if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
278 g_groupIdCallBackMap[groupId].push_back(callbackObj);
279 }
280 return SENSOR_SUCCESS;
281 }
282
283 int32_t ret = HDF_FAILURE;
284 StartTrace(HITRACE_TAG_SENSORS, "Register");
285 if (groupId == TRADITIONAL_SENSOR_TYPE) {
286 ret = sensorInterface->Register(groupId, TradtionalSensorDataCallback);
287 } else if (groupId == MEDICAL_SENSOR_TYPE) {
288 ret = sensorInterface->Register(groupId, MedicalSensorDataCallback);
289 }
290
291 FinishTrace(HITRACE_TAG_SENSORS);
292 if (ret != SENSOR_SUCCESS) {
293 HDF_LOGE("%{public}s: Register fail, groupId[%{public}d]", __func__, groupId);
294 return ret;
295 }
296 std::vector<sptr<ISensorCallbackVdi>> remoteVec;
297 remoteVec.push_back(callbackObj);
298 g_groupIdCallBackMap[groupId] = remoteVec;
299 return ret;
300 }
301
Unregister(int32_t groupId,const sptr<ISensorCallbackVdi> & callbackObj)302 int32_t SensorImpl::Unregister(int32_t groupId, const sptr<ISensorCallbackVdi> &callbackObj)
303 {
304 HDF_LOGI("%{public}s: Enter the Unregister function, groupId is %{public}d", __func__, groupId);
305 if (sensorInterface == nullptr || sensorInterface->Unregister == nullptr) {
306 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
307 return HDF_FAILURE;
308 }
309 std::lock_guard<std::mutex> lock(g_mutex);
310 const sptr<IRemoteObject> &remote = callbackObj->HandleCallbackDeath();
311 StartTrace(HITRACE_TAG_SENSORS, "Unregister");
312 int32_t ret = UnregisterImpl(groupId, remote.GetRefPtr());
313 FinishTrace(HITRACE_TAG_SENSORS);
314 if (ret != SENSOR_SUCCESS) {
315 HDF_LOGE("%{public}s: Unregister failed groupId[%{public}d]", __func__, groupId);
316 }
317
318 return ret;
319 }
320
GetSdcSensorInfo(std::vector<SdcSensorInfoVdi> & sdcSensorInfoVdi)321 int32_t SensorImpl::GetSdcSensorInfo(std::vector<SdcSensorInfoVdi> &sdcSensorInfoVdi)
322 {
323 HDF_LOGI("%{public}s: Enter the GetSdcSensorInfo function", __func__);
324 if (sensorInterface == nullptr || sensorInterface->GetSdcSensorInfo == nullptr) {
325 HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
326 return HDF_FAILURE;
327 }
328
329 StartTrace(HITRACE_TAG_SENSORS, "GetSdcSensorInfo");
330 struct SdcSensorInfo sdcSensorInfo[DEFAULT_SDC_SENSOR_INFO_SIZE];
331 int32_t ret = sensorInterface->GetSdcSensorInfo(sdcSensorInfo);
332 FinishTrace(HITRACE_TAG_SENSORS);
333 if (ret != SENSOR_SUCCESS) {
334 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
335 }
336
337 for (auto info : sdcSensorInfo) {
338 SdcSensorInfoVdi infoVdi;
339 infoVdi.offset = info.offset;
340 infoVdi.sensorId = info.sensorId;
341 infoVdi.ddrSize = info.ddrSize;
342 infoVdi.minRateLevel = info.minRateLevel;
343 infoVdi.maxRateLevel = info.maxRateLevel;
344 infoVdi.memAddr = info.memAddr;
345 infoVdi.reserved = info.reserved;
346 sdcSensorInfoVdi.push_back(std::move(infoVdi));
347 }
348
349 return ret;
350 }
351
UnregisterImpl(int32_t groupId,IRemoteObject * callbackObj)352 int32_t SensorImpl::UnregisterImpl(int32_t groupId, IRemoteObject *callbackObj)
353 {
354 if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
355 HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
356 return SENSOR_INVALID_PARAM;
357 }
358
359 auto groupIdCallBackIter = g_groupIdCallBackMap.find(groupId);
360 if (groupIdCallBackIter == g_groupIdCallBackMap.end()) {
361 HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
362 return HDF_FAILURE;
363 }
364
365 auto callBackIter =
366 find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
367 [&callbackObj](const sptr<ISensorCallbackVdi> &callbackRegistered) {
368 return callbackObj == (callbackRegistered->HandleCallbackDeath()).GetRefPtr();
369 });
370 if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
371 HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
372 return HDF_FAILURE;
373 }
374
375 /**
376 * when there is only one item in the vector,can call the Unregister function.
377 * when there is more than one item in the vector, only need to remove the callbackObj
378 * from the vector
379 */
380 if (g_groupIdCallBackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
381 g_groupIdCallBackMap[groupId].erase(callBackIter);
382 return SENSOR_SUCCESS;
383 }
384
385 int32_t ret = HDF_FAILURE;
386 if (groupId == TRADITIONAL_SENSOR_TYPE) {
387 ret = sensorInterface->Unregister(groupId, TradtionalSensorDataCallback);
388 } else if (groupId == MEDICAL_SENSOR_TYPE) {
389 ret = sensorInterface->Unregister(groupId, MedicalSensorDataCallback);
390 }
391
392 if (ret != SENSOR_SUCCESS) {
393 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
394 return ret;
395 }
396
397 g_groupIdCallBackMap.erase(groupId);
398
399 return ret;
400 }
401
CreateSensorVdiInstance(struct HdfVdiBase * vdiBase)402 static int32_t CreateSensorVdiInstance(struct HdfVdiBase *vdiBase)
403 {
404 HDF_LOGI("%{public}s", __func__);
405 if (vdiBase == nullptr) {
406 HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
407 return HDF_FAILURE;
408 }
409
410 struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
411 sensorVdi->sensorModule = new SensorImpl();
412 if (sensorVdi->sensorModule == nullptr) {
413 HDF_LOGI("%{public}s: new sensorModule failed!", __func__);
414 return HDF_FAILURE;
415 }
416 return HDF_SUCCESS;
417 }
418
DestorySensorVdiInstance(struct HdfVdiBase * vdiBase)419 static int32_t DestorySensorVdiInstance(struct HdfVdiBase *vdiBase)
420 {
421 HDF_LOGI("%{public}s", __func__);
422 if (vdiBase == nullptr) {
423 HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
424 return HDF_FAILURE;
425 }
426
427 struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
428 SensorImpl *impl = reinterpret_cast<SensorImpl *>(sensorVdi->sensorModule);
429 if (impl != nullptr) {
430 delete impl;
431 sensorVdi->sensorModule = nullptr;
432 }
433 return HDF_SUCCESS;
434 }
435
436 static struct WrapperSensorVdi g_sensorVdi = {
437 .base = {
438 .moduleVersion = 1,
439 .moduleName = "sensor_Service",
440 .CreateVdiInstance = CreateSensorVdiInstance,
441 .DestoryVdiInstance = DestorySensorVdiInstance,
442 },
443 .sensorModule = nullptr,
444 };
445
446 extern "C" HDF_VDI_INIT(g_sensorVdi);
447
448 } // namespace V1_1
449 } // namespace Sensor
450 } // namespace HDI
451 } // namespace OHOS
452