• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_callback_vdi.h"
17 
18 #define HDF_LOG_TAG uhdf_sensor_callback_vdi
19 
20 namespace OHOS {
21 namespace HDI {
22 namespace Sensor {
23 namespace V2_0 {
24 
OnDataEventVdi(const OHOS::HDI::Sensor::V1_1::HdfSensorEventsVdi & eventVdi)25 int32_t SensorCallbackVdi::OnDataEventVdi(const OHOS::HDI::Sensor::V1_1::HdfSensorEventsVdi& eventVdi)
26 {
27     struct HdfSensorEvents event;
28     int32_t ret;
29     if (sensorCallback_ == nullptr) {
30         HDF_LOGD("%{public}s sensorCallback_ is NULL", __func__);
31         return HDF_FAILURE;
32     }
33 
34     event.sensorId = eventVdi.sensorId;
35     event.version = eventVdi.version;
36     event.timestamp = eventVdi.timestamp;
37     event.option = eventVdi.option;
38     event.mode = eventVdi.mode;
39     event.data = eventVdi.data;
40     event.dataLen = eventVdi.dataLen;
41     std::unordered_map<int, std::set<int>> sensorEnabled = SensorClientsManager::GetInstance()->GetSensorUsed();
42     std::unordered_map<int, SensorClientInfo> client;
43     if (!SensorClientsManager::GetInstance()->GetClients(HDF_TRADITIONAL_SENSOR_TYPE, client)) {
44         HDF_LOGD("%{public}s groupId %{public}d is not used by anyone", __func__, HDF_TRADITIONAL_SENSOR_TYPE);
45         return HDF_FAILURE;
46     }
47     sptr<ISensorCallback> callback;
48     if (sensorEnabled.find(event.sensorId) == sensorEnabled.end()) {
49         HDF_LOGD("%{public}s sensor %{public}d is not enabled by anyone", __func__, event.sensorId);
50         return HDF_FAILURE;
51     }
52     for (auto it = sensorEnabled[event.sensorId].begin(); it != sensorEnabled[event.sensorId].end(); ++it) {
53         if (client.find(*it) == client.end()) {
54             continue;
55         }
56         sensorClientInfo_ = client[*it];
57         if (SensorClientsManager::GetInstance()->IsNotNeedReportData(*it, event.sensorId)) {
58             continue;
59         }
60         callback = sensorClientInfo_.GetReportDataCb();
61         if (callback == nullptr) {
62             HDF_LOGD("%{public}s the callback of %{public}d is nullptr", __func__, *it);
63             continue;
64         }
65         ret = callback->OnDataEvent(event);
66         if (ret != HDF_SUCCESS) {
67             HDF_LOGD("%{public}s Sensor OnDataEvent failed, error code is %{public}d", __func__, ret);
68         }
69     }
70     return HDF_SUCCESS;
71 }
72 
HandleCallbackDeath()73 sptr<IRemoteObject> SensorCallbackVdi::HandleCallbackDeath()
74 {
75     sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<ISensorCallback>(sensorCallback_);
76 
77     return remote;
78 }
79 } // V2_0
80 } // Sensor
81 } // HDI
82 } // OHOS
83