1 /*
2 * Copyright (c) 2024 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 "print_sensor_data.h"
17
18 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
19 #include "hisysevent.h"
20 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
21
22 #include "sensor_errors.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "PrintSensorData"
26
27 namespace OHOS {
28 namespace Sensors {
29 using namespace OHOS::HiviewDFX;
30 namespace {
31 enum {
32 ONE_DIMENSION = 1,
33 TWO_DIMENSION = 2,
34 THREE_DIMENSION = 3,
35 FOUR_DIMENSION = 4,
36 SEVEN_DIMENSION = 7,
37 DEFAULT_DIMENSION = 16
38 };
39 constexpr int64_t LOG_INTERVAL = 60000000000L;
40 constexpr int32_t FIRST_PRINT_TIMES = 20;
41 constexpr float LOG_FORMAT_DIVIDER = 1e9f;
42
43 const std::vector<int32_t> g_triggerSensorType = {
44 SENSOR_TYPE_ID_HALL_EXT,
45 SENSOR_TYPE_ID_PROXIMITY,
46 SENSOR_TYPE_ID_HALL,
47 SENSOR_TYPE_ID_WEAR_DETECTION,
48 SENSOR_TYPE_ID_PROXIMITY1,
49 };
50 const std::vector<int32_t> g_continuousSensorType = {
51 SENSOR_TYPE_ID_ACCELEROMETER,
52 SENSOR_TYPE_ID_POSTURE,
53 SENSOR_TYPE_ID_AMBIENT_LIGHT,
54 SENSOR_TYPE_ID_AMBIENT_LIGHT1,
55 SENSOR_TYPE_ID_GYROSCOPE,
56 SENSOR_TYPE_ID_MAGNETIC_FIELD,
57 SENSOR_TYPE_ID_ORIENTATION,
58 SENSOR_TYPE_ID_ROTATION_VECTOR,
59 };
60 }
61
ControlSensorHdiPrint(const SensorData & sensorData)62 void PrintSensorData::ControlSensorHdiPrint(const SensorData &sensorData)
63 {
64 auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), sensorData.sensorTypeId);
65 if (triggerIt != g_triggerSensorType.end()) {
66 PrintHdiData(sensorData);
67 ProcessHdiDFX(sensorData);
68 }
69 std::lock_guard<std::mutex> hdiLoginfoLock(hdiLoginfoMutex_);
70 auto it = hdiLoginfo_.find(sensorData.sensorTypeId);
71 if (it == hdiLoginfo_.end()) {
72 return;
73 }
74 if (it->second.count < FIRST_PRINT_TIMES) {
75 PrintHdiData(sensorData);
76 if (it->second.count == FIRST_PRINT_TIMES - 1) {
77 it->second.lastTime = sensorData.timestamp;
78 }
79 it->second.count++;
80 } else {
81 if (sensorData.timestamp - it->second.lastTime >= LOG_INTERVAL) {
82 PrintHdiData(sensorData);
83 it->second.lastTime = sensorData.timestamp;
84 }
85 }
86 }
87
PrintHdiData(const SensorData & sensorData)88 void PrintSensorData::PrintHdiData(const SensorData &sensorData)
89 {
90 std::string str;
91 str += "sensorId: " + std::to_string(sensorData.sensorTypeId) + ", ";
92 str += "timestamp: " + std::to_string(sensorData.timestamp / LOG_FORMAT_DIVIDER) + ", ";
93 int32_t dataDim = GetDataDimension(sensorData.sensorTypeId);
94 auto data = reinterpret_cast<const float *>(sensorData.data);
95 for (int32_t i = 0; i < dataDim; ++i) {
96 CHKPV(data);
97 str.append(std::to_string(*data));
98 if (i != dataDim - 1) {
99 str.append(", ");
100 }
101 ++data;
102 }
103 str.append("\n");
104 SEN_HILOGI("SensorData: %{public}s", str.c_str());
105 }
106
ProcessHdiDFX(const SensorData & sensorData)107 void PrintSensorData::ProcessHdiDFX(const SensorData &sensorData)
108 {
109 std::string strData;
110 auto data = reinterpret_cast<const float *>(sensorData.data);
111 int32_t dataDim = GetDataDimension(sensorData.sensorTypeId);
112 for (int32_t i = 0; i < dataDim; i++) {
113 CHKPV(data);
114 strData.append(std::to_string(*data));
115 if (i != dataDim - 1) {
116 strData.append(", ");
117 }
118 ++data;
119 }
120 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
121 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "EVENT_REPORT",
122 HiSysEvent::EventType::BEHAVIOR, "SENSOR_ID", sensorData.sensorTypeId, "TIMESTAMP",
123 std::to_string(sensorData.timestamp), "DATA", strData);
124 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
125 }
126
GetDataDimension(int32_t sensorId)127 int32_t PrintSensorData::GetDataDimension(int32_t sensorId)
128 {
129 switch (sensorId) {
130 case SENSOR_TYPE_ID_HALL:
131 case SENSOR_TYPE_ID_PROXIMITY:
132 case SENSOR_TYPE_ID_WEAR_DETECTION:
133 return ONE_DIMENSION;
134 case SENSOR_TYPE_ID_HALL_EXT:
135 return TWO_DIMENSION;
136 case SENSOR_TYPE_ID_POSTURE:
137 return SEVEN_DIMENSION;
138 case SENSOR_TYPE_ID_AMBIENT_LIGHT:
139 case SENSOR_TYPE_ID_AMBIENT_LIGHT1:
140 case SENSOR_TYPE_ID_ACCELEROMETER:
141 case SENSOR_TYPE_ID_GYROSCOPE:
142 case SENSOR_TYPE_ID_MAGNETIC_FIELD:
143 case SENSOR_TYPE_ID_ORIENTATION:
144 return THREE_DIMENSION;
145 case SENSOR_TYPE_ID_ROTATION_VECTOR:
146 return FOUR_DIMENSION;
147 default:
148 SEN_HILOGW("Unknown sensorId:%{public}d, size:%{public}d", sensorId, DEFAULT_DIMENSION);
149 return DEFAULT_DIMENSION;
150 }
151 }
152
ControlSensorClientPrint(const RecordSensorCallback callback,const SensorEvent & event)153 void PrintSensorData::ControlSensorClientPrint(const RecordSensorCallback callback, const SensorEvent &event)
154 {
155 auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), event.sensorTypeId);
156 if (triggerIt != g_triggerSensorType.end()) {
157 PrintClientData(event);
158 ProcessClientDFX(event);
159 }
160
161 auto continuosIt = std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(), event.sensorTypeId);
162 if (continuosIt == g_continuousSensorType.end()) {
163 return;
164 }
165 std::lock_guard<std::mutex> clientLoginfoLock(clientLoginfoMutex_);
166 auto it = clientLoginfo_.find(callback);
167 if (it == clientLoginfo_.end()) {
168 return;
169 }
170 if (it->second.count < FIRST_PRINT_TIMES) {
171 PrintClientData(event);
172 if (it->second.count == FIRST_PRINT_TIMES - 1) {
173 it->second.lastTime = event.timestamp;
174 }
175 it->second.count++;
176 } else {
177 if (event.timestamp - it->second.lastTime >= LOG_INTERVAL) {
178 PrintClientData(event);
179 it->second.lastTime = event.timestamp;
180 }
181 }
182 }
183
PrintClientData(const SensorEvent & event)184 void PrintSensorData::PrintClientData(const SensorEvent &event)
185 {
186 std::string str;
187 str += "sensorId: " + std::to_string(event.sensorTypeId) + ", ";
188 str += "timestamp: " + std::to_string(event.timestamp / LOG_FORMAT_DIVIDER) + ", ";
189 int32_t dataDim = GetDataDimension(event.sensorTypeId);
190 auto data = reinterpret_cast<const float *>(event.data);
191 for (int32_t i = 0; i < dataDim; ++i) {
192 CHKPV(data);
193 str.append(std::to_string(*data));
194 if (i != dataDim - 1) {
195 str.append(", ");
196 }
197 ++data;
198 }
199 str.append("\n");
200 SEN_HILOGI("SensorData: %{public}s", str.c_str());
201 }
202
ProcessClientDFX(const SensorEvent & event)203 void PrintSensorData::ProcessClientDFX(const SensorEvent &event)
204 {
205 std::string strData;
206 auto data = reinterpret_cast<const float *>(event.data);
207 int32_t dataDim = GetDataDimension(event.sensorTypeId);
208 for (int32_t i = 0; i < dataDim; i++) {
209 CHKPV(data);
210 strData.append(std::to_string(*data));
211 if (i != dataDim - 1) {
212 strData.append(", ");
213 }
214 ++data;
215 }
216 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
217 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "EVENT_REPORT",
218 HiSysEvent::EventType::BEHAVIOR, "SENSOR_ID", event.sensorTypeId, "TIMESTAMP",
219 std::to_string(event.timestamp), "DATA", strData);
220 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
221 }
222
IsContinuousType(int32_t sensorId)223 bool PrintSensorData::IsContinuousType(int32_t sensorId)
224 {
225 return std::find(g_continuousSensorType.begin(), g_continuousSensorType.end(),
226 sensorId) != g_continuousSensorType.end();
227 }
228
SavePrintUserInfo(const RecordSensorCallback callback)229 void PrintSensorData::SavePrintUserInfo(const RecordSensorCallback callback)
230 {
231 CHKPV(callback);
232 std::lock_guard<std::mutex> clientLoginfoLock(clientLoginfoMutex_);
233 if (clientLoginfo_.find(callback) != clientLoginfo_.end()) {
234 return;
235 }
236 LogPrintInfo info;
237 auto status = clientLoginfo_.insert(std::make_pair(callback, info));
238 if (!status.second) {
239 SEN_HILOGD("callback has been saved");
240 }
241 }
242
RemovePrintUserInfo(const RecordSensorCallback callback)243 void PrintSensorData::RemovePrintUserInfo(const RecordSensorCallback callback)
244 {
245 CHKPV(callback);
246 std::lock_guard<std::mutex> clientLoginfoLock(clientLoginfoMutex_);
247 if (clientLoginfo_.find(callback) == clientLoginfo_.end()) {
248 return;
249 }
250 clientLoginfo_.erase(callback);
251 }
252
ResetHdiCounter(int32_t sensorId)253 void PrintSensorData::ResetHdiCounter(int32_t sensorId)
254 {
255 std::lock_guard<std::mutex> hdiLoginfoLock(hdiLoginfoMutex_);
256 auto it = hdiLoginfo_.find(sensorId);
257 if (it == hdiLoginfo_.end()) {
258 return;
259 }
260 it->second.count = 0;
261 it->second.lastTime = 0;
262 }
263
PrintSensorDataLog(const std::string & name,const SensorData & data)264 void PrintSensorData::PrintSensorDataLog(const std::string &name, const SensorData &data)
265 {
266 std::string str;
267 str += "sensorId: " + std::to_string(data.sensorTypeId) + ", ";
268 str += "timestamp: " + std::to_string(data.timestamp / LOG_FORMAT_DIVIDER) + ", ";
269 int32_t dataDim = GetDataDimension(data.sensorTypeId);
270 auto tempData = reinterpret_cast<const float *>(data.data);
271 for (int32_t i = 0; i < dataDim; ++i) {
272 CHKPV(tempData);
273 str.append(std::to_string(*tempData));
274 if (i != dataDim - 1) {
275 str.append(", ");
276 }
277 ++tempData;
278 }
279 str.append("\n");
280 SEN_HILOGI("%{public}s SensorData: %{public}s", name.c_str(), str.c_str());
281 }
282
PrintSensorInfo(SensorInfo * sensorInfos,int32_t sensorInfoCount)283 void PrintSensorData::PrintSensorInfo(SensorInfo *sensorInfos, int32_t sensorInfoCount)
284 {
285 std::string combineSensorIds = "";
286 for (int32_t i = 0; i < sensorInfoCount; ++i) {
287 combineSensorIds = combineSensorIds + std::to_string(sensorInfos[i].sensorTypeId) + " ";
288 }
289 SEN_HILOGI("PrintSensorInfo success, sensorIds:%{public}s, sensorInfoCount:%{public}d", combineSensorIds.c_str(),
290 sensorInfoCount);
291 }
292 } // namespace Sensors
293 } // namespace OHOS
294