• 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     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     return SENSOR_SUCCESS;
325 }
326 
UnregisterImpl(int32_t groupId,IRemoteObject * callbackObj)327 int32_t SensorImpl::UnregisterImpl(int32_t groupId, IRemoteObject *callbackObj)
328 {
329     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
330         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
331         return SENSOR_INVALID_PARAM;
332     }
333 
334     auto groupIdCallBackIter = g_groupIdCallBackMap.find(groupId);
335     if (groupIdCallBackIter == g_groupIdCallBackMap.end()) {
336         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
337         return HDF_FAILURE;
338     }
339 
340     auto callBackIter =
341         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
342         [&callbackObj](const sptr<ISensorCallbackVdi> &callbackRegistered) {
343             return callbackObj == (callbackRegistered->HandleCallbackDeath()).GetRefPtr();
344         });
345     if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
346         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
347         return HDF_FAILURE;
348     }
349 
350     /**
351      * when there is only one item in the vector,can call the Unregister function.
352      * when there is more than one item in the vector, only need to remove the  callbackObj
353      * from the vector
354      */
355     if (g_groupIdCallBackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
356         g_groupIdCallBackMap[groupId].erase(callBackIter);
357         return SENSOR_SUCCESS;
358     }
359 
360     int32_t ret = HDF_FAILURE;
361     if (groupId == TRADITIONAL_SENSOR_TYPE) {
362         ret = sensorInterface->Unregister(groupId, TradtionalSensorDataCallback);
363     } else if (groupId == MEDICAL_SENSOR_TYPE) {
364         ret = sensorInterface->Unregister(groupId, MedicalSensorDataCallback);
365     }
366 
367     if (ret != SENSOR_SUCCESS) {
368         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
369         return ret;
370     }
371 
372     g_groupIdCallBackMap.erase(groupId);
373 
374     return ret;
375 }
376 
CreateSensorVdiInstance(struct HdfVdiBase * vdiBase)377 static int32_t CreateSensorVdiInstance(struct HdfVdiBase *vdiBase)
378 {
379     HDF_LOGI("%{public}s", __func__);
380     if (vdiBase == nullptr) {
381         HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
382         return HDF_FAILURE;
383     }
384 
385     struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
386     sensorVdi->sensorModule = new SensorImpl();
387     if (sensorVdi->sensorModule == nullptr) {
388         HDF_LOGI("%{public}s: new sensorModule failed!", __func__);
389         return HDF_FAILURE;
390     }
391     return HDF_SUCCESS;
392 }
393 
DestorySensorVdiInstance(struct HdfVdiBase * vdiBase)394 static int32_t DestorySensorVdiInstance(struct HdfVdiBase *vdiBase)
395 {
396     HDF_LOGI("%{public}s", __func__);
397     if (vdiBase == nullptr) {
398         HDF_LOGI("%{public}s: parameter vdiBase is NULL", __func__);
399         return HDF_FAILURE;
400     }
401 
402     struct WrapperSensorVdi *sensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdiBase);
403     SensorImpl *impl = reinterpret_cast<SensorImpl *>(sensorVdi->sensorModule);
404     if (impl != nullptr) {
405         delete impl;
406         sensorVdi->sensorModule = nullptr;
407     }
408     return HDF_SUCCESS;
409 }
410 
411 static struct WrapperSensorVdi g_sensorVdi = {
412     .base = {
413         .moduleVersion = 1,
414         .moduleName = "sensor_Service",
415         .CreateVdiInstance = CreateSensorVdiInstance,
416         .DestoryVdiInstance = DestorySensorVdiInstance,
417     },
418     .sensorModule = nullptr,
419 };
420 
421 extern "C" HDF_VDI_INIT(g_sensorVdi);
422 
423 } // namespace V1_1
424 } // namespace Sensor
425 } // namespace HDI
426 } // namespace OHOS
427