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 #include "osal_mem.h"
18 #include <securec.h>
19 #include <unordered_map>
20
21 #define HDF_LOG_TAG uhdf_sensor_callback_vdi
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Sensor {
26 namespace V2_0 {
27 namespace {
28 constexpr int32_t DATA_LEN = 256;
29 constexpr int64_t REPOPRT_TIME = 60000000000;
30 constexpr int64_t INIT_DATA_COUNT = 1;
31 static std::unordered_map<int32_t, int64_t> firstTimestampMap_;
32 static std::unordered_map<int32_t, int64_t> lastTimestampMap_;
33 }
34
OnDataEventVdi(const OHOS::HDI::Sensor::V1_1::HdfSensorEventsVdi & eventVdi)35 int32_t SensorCallbackVdi::OnDataEventVdi(const OHOS::HDI::Sensor::V1_1::HdfSensorEventsVdi& eventVdi)
36 {
37 HITRACE_METER_FMT(HITRACE_TAG_HDF, "%s: sensorId %d", __func__, eventVdi.sensorId);
38 struct HdfSensorEvents event;
39 event.sensorId = eventVdi.sensorId;
40 event.version = eventVdi.version;
41 event.timestamp = eventVdi.timestamp;
42 event.option = eventVdi.option;
43 event.mode = eventVdi.mode;
44 event.data = eventVdi.data;
45 event.dataLen = eventVdi.dataLen;
46 int32_t ret = OnDataEvent(event);
47 return ret;
48 }
49
OnDataEvent(const V2_0::HdfSensorEvents & event)50 int32_t SensorCallbackVdi::OnDataEvent(const V2_0::HdfSensorEvents& event)
51 {
52 HITRACE_METER_FMT(HITRACE_TAG_HDF, "%s: sensorId %d", __func__, event.sensorId);
53 SensorClientsManager::GetInstance()->CopyEventData(event);
54 const std::string reportResult = SensorClientsManager::GetInstance()->ReportEachClient(event);
55 HDF_LOGD("%{public}s sensorId=%{public}d, %{public}s", __func__, event.sensorId, reportResult.c_str());
56 bool isPrint = SensorClientsManager::GetInstance()->IsSensorNeedPrint(event.sensorId);
57 PrintData(event, reportResult, isPrint);
58 return HDF_SUCCESS;
59 }
60
PrintData(const HdfSensorEvents & event,const std::string & reportResult,bool & isPrint)61 void SensorCallbackVdi::PrintData(const HdfSensorEvents &event, const std::string &reportResult, bool &isPrint)
62 {
63 SENSOR_TRACE;
64 std::unique_lock<std::mutex> lock(timestampMapMutex_);
65 static std::unordered_map<int32_t, int64_t> sensorDataCountMap;
66 auto it = sensorDataCountMap.find(event.sensorId);
67 int64_t dataCount = INIT_DATA_COUNT;
68 if (it == sensorDataCountMap.end()) {
69 sensorDataCountMap[event.sensorId] = INIT_DATA_COUNT;
70 } else {
71 it->second++;
72 dataCount = it->second;
73 }
74 bool result = isPrint;
75 if (!isPrint) {
76 if (firstTimestampMap_[event.sensorId] == 0) {
77 firstTimestampMap_[event.sensorId] = event.timestamp;
78 result = true;
79 } else {
80 lastTimestampMap_[event.sensorId] = event.timestamp;
81 }
82 if (lastTimestampMap_[event.sensorId] - firstTimestampMap_[event.sensorId] >= REPOPRT_TIME) {
83 firstTimestampMap_[event.sensorId] = lastTimestampMap_[event.sensorId];
84 result = true;
85 }
86 }
87
88 if (result) {
89 std::string st = {0};
90 DataToStr(st, event);
91 st += "sensorDataCount=" + std::to_string(dataCount);
92 st += reportResult;
93 HDF_LOGI("%{public}s: %{public}s", __func__, st.c_str());
94 }
95 }
96
DataToStr(std::string & str,const HdfSensorEvents & event)97 void SensorCallbackVdi::DataToStr(std::string &str, const HdfSensorEvents &event)
98 {
99 void *origin = OsalMemCalloc(sizeof(uint8_t) * (event.dataLen));
100 if (origin == nullptr) {
101 HDF_LOGE("%{public}s: OsalMemCalloc failed", __func__);
102 return;
103 }
104
105 uint8_t *eventData = static_cast<uint8_t*>(origin);
106 std::copy(event.data.begin(), event.data.end(), eventData);
107 float *data = reinterpret_cast<float*>(eventData);
108 int32_t dataLen = event.dataLen;
109 int32_t dataDimension = static_cast<int32_t>(dataLen / sizeof(float));
110 std::string dataStr = {0};
111 char arrayStr[DATA_LEN] = {0};
112
113 for (int32_t i = 0; i < dataDimension; i++) {
114 if (sprintf_s(arrayStr + strlen(arrayStr), DATA_LEN, "[%f]", data[i]) < 0) {
115 HDF_LOGE("%{public}s: sprintf_s failed", __func__);
116 OsalMemFree(origin);
117 return;
118 }
119 }
120
121 dataStr = arrayStr;
122 str = "sensorId: " + std::to_string(event.sensorId) + ", ts: " +
123 std::to_string(event.timestamp / 1e9) + ", data: " + dataStr;
124
125 OsalMemFree(origin);
126 return;
127 }
128
HandleCallbackDeath()129 sptr<IRemoteObject> SensorCallbackVdi::HandleCallbackDeath()
130 {
131 sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<ISensorCallback>(sensorCallback_);
132
133 return remote;
134 }
135 } // V2_0
136 } // Sensor
137 } // HDI
138 } // OHOS
139