• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->GetAllSensors);
114 
115     struct SensorInformation *sensorInfo = nullptr;
116     struct SensorInformation *tmp = nullptr;
117     int32_t count = 0;
118 
119     StartTrace(HITRACE_TAG_SENSORS, "GetAllSensorInfo");
120     int32_t ret = sensorInterface->GetAllSensors(&sensorInfo, &count);
121     FinishTrace(HITRACE_TAG_SENSORS);
122     if (ret != SENSOR_SUCCESS) {
123         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
124         return ret;
125     }
126 
127     if (count <= 0) {
128         HDF_LOGE("%{public}s failed, count<=0", __func__);
129         return HDF_FAILURE;
130     }
131 
132     tmp = sensorInfo;
133     while (count--) {
134         HdfSensorInformationVdi hdfSensorInfo;
135         std::string sensorName(tmp->sensorName);
136         hdfSensorInfo.sensorName = sensorName;
137         std::string vendorName(tmp->vendorName);
138         hdfSensorInfo.vendorName = vendorName;
139         std::string firmwareVersion(tmp->firmwareVersion);
140         hdfSensorInfo.firmwareVersion = firmwareVersion;
141         std::string hardwareVersion(tmp->hardwareVersion);
142         hdfSensorInfo.hardwareVersion = hardwareVersion;
143         hdfSensorInfo.sensorTypeId = tmp->sensorTypeId;
144         hdfSensorInfo.sensorId = tmp->sensorId;
145         hdfSensorInfo.maxRange = tmp->maxRange;
146         hdfSensorInfo.accuracy = tmp->accuracy;
147         hdfSensorInfo.power = tmp->power;
148         hdfSensorInfo.minDelay = tmp->minDelay;
149         hdfSensorInfo.maxDelay = tmp->maxDelay;
150         hdfSensorInfo.fifoMaxEventCount = tmp->fifoMaxEventCount;
151         info.push_back(std::move(hdfSensorInfo));
152         tmp++;
153     }
154 
155     return HDF_SUCCESS;
156 }
157 
Enable(int32_t sensorId)158 int32_t SensorImpl::Enable(int32_t sensorId)
159 {
160     HDF_LOGI("%{public}s: Enter the Enable function, sensorId is %{public}d", __func__, sensorId);
161     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->Enable);
162 
163     StartTrace(HITRACE_TAG_SENSORS, "Enable");
164     int32_t ret = sensorInterface->Enable(sensorId);
165     FinishTrace(HITRACE_TAG_SENSORS);
166     if (ret != SENSOR_SUCCESS) {
167         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
168     }
169 
170     return ret;
171 }
172 
Disable(int32_t sensorId)173 int32_t SensorImpl::Disable(int32_t sensorId)
174 {
175     HDF_LOGI("%{public}s: Enter the Disable function, sensorId is %{public}d", __func__, sensorId);
176     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->Disable);
177 
178     StartTrace(HITRACE_TAG_SENSORS, "Disable");
179     int32_t ret = sensorInterface->Disable(sensorId);
180     FinishTrace(HITRACE_TAG_SENSORS);
181     if (ret != SENSOR_SUCCESS) {
182         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
183     }
184 
185     return ret;
186 }
187 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)188 int32_t SensorImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
189 {
190     HDF_LOGI("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "], \
191         reportInterval is [%{public}" PRId64 "].", __func__, sensorId, samplingInterval, reportInterval);
192     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->SetBatch);
193 
194     StartTrace(HITRACE_TAG_SENSORS, "SetBatch");
195     int32_t ret = sensorInterface->SetBatch(sensorId, samplingInterval, reportInterval);
196     FinishTrace(HITRACE_TAG_SENSORS);
197     if (ret != SENSOR_SUCCESS) {
198         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
199     }
200 
201     return ret;
202 }
203 
SetMode(int32_t sensorId,int32_t mode)204 int32_t SensorImpl::SetMode(int32_t sensorId, int32_t mode)
205 {
206     HDF_LOGI("%{public}s: Enter the SetMode function, sensorId is %{public}d, mode is %{public}d",
207         __func__, sensorId, mode);
208     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->SetMode);
209 
210     StartTrace(HITRACE_TAG_SENSORS, "SetMode");
211     int32_t ret = sensorInterface->SetMode(sensorId, mode);
212     FinishTrace(HITRACE_TAG_SENSORS);
213     if (ret != SENSOR_SUCCESS) {
214         HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
215     }
216 
217     return ret;
218 }
219 
SetOption(int32_t sensorId,uint32_t option)220 int32_t SensorImpl::SetOption(int32_t sensorId, uint32_t option)
221 {
222     HDF_LOGI("%{public}s: Enter the SetOption function, sensorId is %{public}d, option is %{public}u",
223         __func__, sensorId, option);
224     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->SetOption);
225 
226     StartTrace(HITRACE_TAG_SENSORS, "SetOption");
227     int32_t ret = sensorInterface->SetOption(sensorId, option);
228     FinishTrace(HITRACE_TAG_SENSORS);
229     if (ret != SENSOR_SUCCESS) {
230         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
231     }
232 
233     return ret;
234 }
235 
Register(int32_t groupId,const sptr<ISensorCallbackVdi> & callbackObj)236 int32_t SensorImpl::Register(int32_t groupId, const sptr<ISensorCallbackVdi> &callbackObj)
237 {
238     HDF_LOGI("%{public}s: Enter the Register function, groupId is %{public}d", __func__, groupId);
239     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->Register);
240 
241     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
242         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
243         return SENSOR_INVALID_PARAM;
244     }
245 
246     std::lock_guard<std::mutex> lock(g_mutex);
247     auto groupCallBackIter = g_groupIdCallBackMap.find(groupId);
248     if (groupCallBackIter != g_groupIdCallBackMap.end()) {
249         auto callBackIter =
250             find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
251             [&callbackObj](const sptr<ISensorCallbackVdi> &callbackRegistered) {
252                 const sptr<IRemoteObject> &lhs = callbackObj->HandleCallbackDeath();
253                 const sptr<IRemoteObject> &rhs = callbackRegistered->HandleCallbackDeath();
254                 return lhs == rhs;
255             });
256         if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
257             g_groupIdCallBackMap[groupId].push_back(callbackObj);
258         }
259         return SENSOR_SUCCESS;
260     }
261 
262     int32_t ret = HDF_FAILURE;
263     StartTrace(HITRACE_TAG_SENSORS, "Register");
264     if (groupId == TRADITIONAL_SENSOR_TYPE) {
265         ret = sensorInterface->Register(groupId, TradtionalSensorDataCallback);
266     } else if (groupId == MEDICAL_SENSOR_TYPE) {
267         ret = sensorInterface->Register(groupId, MedicalSensorDataCallback);
268     }
269 
270     FinishTrace(HITRACE_TAG_SENSORS);
271     if (ret != SENSOR_SUCCESS) {
272         HDF_LOGE("%{public}s: Register fail, groupId[%{public}d]", __func__, groupId);
273         return ret;
274     }
275     std::vector<sptr<ISensorCallbackVdi>> remoteVec;
276     remoteVec.push_back(callbackObj);
277     g_groupIdCallBackMap[groupId] = remoteVec;
278     return ret;
279 }
280 
Unregister(int32_t groupId,const sptr<ISensorCallbackVdi> & callbackObj)281 int32_t SensorImpl::Unregister(int32_t groupId, const sptr<ISensorCallbackVdi> &callbackObj)
282 {
283     HDF_LOGI("%{public}s: Enter the Unregister function, groupId is %{public}d", __func__, groupId);
284     std::lock_guard<std::mutex> lock(g_mutex);
285     const sptr<IRemoteObject> &remote = callbackObj->HandleCallbackDeath();
286     StartTrace(HITRACE_TAG_SENSORS, "Unregister");
287     int32_t ret = UnregisterImpl(groupId, remote.GetRefPtr());
288     FinishTrace(HITRACE_TAG_SENSORS);
289     if (ret != SENSOR_SUCCESS) {
290         HDF_LOGE("%{public}s: Unregister failed groupId[%{public}d]", __func__, groupId);
291     }
292 
293     return ret;
294 }
295 
GetSdcSensorInfo(std::vector<SdcSensorInfoVdi> & sdcSensorInfoVdi)296 int32_t SensorImpl::GetSdcSensorInfo(std::vector<SdcSensorInfoVdi> &sdcSensorInfoVdi)
297 {
298     HDF_LOGI("%{public}s: Enter the GetSdcSensorInfo function", __func__);
299     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->GetSdcSensorInfo);
300 
301     StartTrace(HITRACE_TAG_SENSORS, "GetSdcSensorInfo");
302     struct SdcSensorInfo sdcSensorInfo[DEFAULT_SDC_SENSOR_INFO_SIZE];
303     int32_t ret = sensorInterface->GetSdcSensorInfo(sdcSensorInfo);
304     FinishTrace(HITRACE_TAG_SENSORS);
305     if (ret != SENSOR_SUCCESS) {
306         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
307     }
308 
309     for (auto info : sdcSensorInfo) {
310         SdcSensorInfoVdi infoVdi;
311         infoVdi.offset = info.offset;
312         infoVdi.sensorId = info.sensorId;
313         infoVdi.ddrSize = info.ddrSize;
314         infoVdi.minRateLevel = info.minRateLevel;
315         infoVdi.maxRateLevel = info.maxRateLevel;
316         infoVdi.memAddr = info.memAddr;
317         infoVdi.reserved = info.reserved;
318         sdcSensorInfoVdi.push_back(std::move(infoVdi));
319     }
320 
321     return ret;
322 }
323 
UnregisterImpl(int32_t groupId,IRemoteObject * callbackObj)324 int32_t SensorImpl::UnregisterImpl(int32_t groupId, IRemoteObject *callbackObj)
325 {
326     CHECK_SENSOR_MODULE_INSTANCE(sensorInterface, sensorInterface->Unregister);
327 
328     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
329         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
330         return SENSOR_INVALID_PARAM;
331     }
332 
333     auto groupIdCallBackIter = g_groupIdCallBackMap.find(groupId);
334     if (groupIdCallBackIter == g_groupIdCallBackMap.end()) {
335         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
336         return HDF_FAILURE;
337     }
338 
339     auto callBackIter =
340         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
341         [&callbackObj](const sptr<ISensorCallbackVdi> &callbackRegistered) {
342             return callbackObj == (callbackRegistered->HandleCallbackDeath()).GetRefPtr();
343         });
344     if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
345         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
346         return HDF_FAILURE;
347     }
348 
349     /**
350      * when there is only one item in the vector,can call the Unregister function.
351      * when there is more than one item in the vector, only need to remove the  callbackObj
352      * from the vector
353      */
354     if (g_groupIdCallBackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
355         g_groupIdCallBackMap[groupId].erase(callBackIter);
356         return SENSOR_SUCCESS;
357     }
358 
359     int32_t ret = HDF_FAILURE;
360     if (groupId == TRADITIONAL_SENSOR_TYPE) {
361         ret = sensorInterface->Unregister(groupId, TradtionalSensorDataCallback);
362     } else if (groupId == MEDICAL_SENSOR_TYPE) {
363         ret = sensorInterface->Unregister(groupId, MedicalSensorDataCallback);
364     }
365 
366     if (ret != SENSOR_SUCCESS) {
367         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
368         return ret;
369     }
370 
371     g_groupIdCallBackMap.erase(groupId);
372 
373     return ret;
374 }
375 
CreateSensorVdiInstance(struct HdfVdiBase * vdiBase)376 static int32_t CreateSensorVdiInstance(struct HdfVdiBase *vdiBase)
377 {
378     HDF_LOGI("%{public}s", __func__);
379     if (vdiBase == nullptr) {
380         HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
381         return HDF_FAILURE;
382     }
383 
384     struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
385     sensorVdi->sensorModule = new SensorImpl();
386     if (sensorVdi->sensorModule == nullptr) {
387         HDF_LOGI("%{public}s: new sensorModule failed!", __func__);
388         return HDF_FAILURE;
389     }
390     return HDF_SUCCESS;
391 }
392 
DestorySensorVdiInstance(struct HdfVdiBase * vdiBase)393 static int32_t DestorySensorVdiInstance(struct HdfVdiBase *vdiBase)
394 {
395     HDF_LOGI("%{public}s", __func__);
396     if (vdiBase == nullptr) {
397         HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
398         return HDF_FAILURE;
399     }
400 
401     struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
402     SensorImpl *impl = reinterpret_cast<SensorImpl *>(sensorVdi->sensorModule);
403     if (impl != nullptr) {
404         delete impl;
405         sensorVdi->sensorModule = nullptr;
406     }
407     return HDF_SUCCESS;
408 }
409 
410 static struct WrapperSensorVdi g_sensorVdi = {
411     .base = {
412         .moduleVersion = 1,
413         .moduleName = "sensor_Service",
414         .CreateVdiInstance = CreateSensorVdiInstance,
415         .DestoryVdiInstance = DestorySensorVdiInstance,
416     },
417     .sensorModule = nullptr,
418 };
419 
420 extern "C" HDF_VDI_INIT(g_sensorVdi);
421 
422 } // namespace V1_1
423 } // namespace Sensor
424 } // namespace HDI
425 } // namespace OHOS
426