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