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