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 23namespace OHOS { 24namespace HDI { 25namespace Sensor { 26namespace V2_0 { 27namespace { 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 35int32_t SensorCallbackVdi::OnDataEventVdi(const OHOS::HDI::Sensor::V1_1::HdfSensorEventsVdi& eventVdi) 36{ 37 SENSOR_TRACE; 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 50int32_t SensorCallbackVdi::OnDataEvent(const V2_0::HdfSensorEvents& event) 51{ 52 SENSOR_TRACE; 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 PrintData(event, reportResult); 57 return HDF_SUCCESS; 58} 59 60void SensorCallbackVdi::PrintData(const HdfSensorEvents &event, const std::string &reportResult) 61{ 62 SENSOR_TRACE; 63 std::unique_lock<std::mutex> lock(timestampMapMutex_); 64 static std::unordered_map<int32_t, int64_t> sensorDataCountMap; 65 auto it = sensorDataCountMap.find(event.sensorId); 66 int64_t dataCount = INIT_DATA_COUNT; 67 if (it == sensorDataCountMap.end()) { 68 sensorDataCountMap[event.sensorId] = INIT_DATA_COUNT; 69 } else { 70 it->second++; 71 dataCount = it->second; 72 } 73 bool result = false; 74 if (firstTimestampMap_[event.sensorId] == 0) { 75 firstTimestampMap_[event.sensorId] = event.timestamp; 76 result = true; 77 } else { 78 lastTimestampMap_[event.sensorId] = event.timestamp; 79 } 80 81 if (lastTimestampMap_[event.sensorId] - firstTimestampMap_[event.sensorId] >= REPOPRT_TIME) { 82 firstTimestampMap_[event.sensorId] = lastTimestampMap_[event.sensorId]; 83 result = true; 84 } 85 86 if (result) { 87 std::string st = {0}; 88 DataToStr(st, event); 89 st += "sensorDataCount=" + std::to_string(dataCount); 90 st += reportResult; 91 HDF_LOGI("%{public}s: %{public}s", __func__, st.c_str()); 92 } 93} 94 95void SensorCallbackVdi::DataToStr(std::string &str, const HdfSensorEvents &event) 96{ 97 void *origin = OsalMemCalloc(sizeof(uint8_t) * (event.dataLen)); 98 if (origin == nullptr) { 99 HDF_LOGE("%{public}s: OsalMemCalloc failed", __func__); 100 return; 101 } 102 103 uint8_t *eventData = static_cast<uint8_t*>(origin); 104 std::copy(event.data.begin(), event.data.end(), eventData); 105 float *data = reinterpret_cast<float*>(eventData); 106 int32_t dataLen = event.dataLen; 107 int32_t dataDimension = static_cast<int32_t>(dataLen / sizeof(float)); 108 std::string dataStr = {0}; 109 char arrayStr[DATA_LEN] = {0}; 110 111 for (int32_t i = 0; i < dataDimension; i++) { 112 if (sprintf_s(arrayStr + strlen(arrayStr), DATA_LEN, "[%f]", data[i]) < 0) { 113 HDF_LOGE("%{public}s: sprintf_s failed", __func__); 114 OsalMemFree(origin); 115 return; 116 } 117 } 118 119 dataStr = arrayStr; 120 str = "sensorId: " + std::to_string(event.sensorId) + ", ts: " + 121 std::to_string(event.timestamp / 1e9) + ", data: " + dataStr; 122 123 OsalMemFree(origin); 124 return; 125} 126 127sptr<IRemoteObject> SensorCallbackVdi::HandleCallbackDeath() 128{ 129 sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<ISensorCallback>(sensorCallback_); 130 131 return remote; 132} 133} // V2_0 134} // Sensor 135} // HDI 136} // OHOS 137