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 "sensor_hdi_dump.h"
17 #include "osal_mem.h"
18 #include <securec.h>
19 #include <unordered_map>
20 #include "sensor_uhdf_log.h"
21
22 #define HDF_LOG_TAG dump
23
24 constexpr int32_t GET_SENSORINFO = 0;
25 constexpr int32_t DATA_LEN = 256;
26
27 namespace OHOS {
28 namespace HDI {
29 namespace Sensor {
30 namespace V3_0 {
31
32 static const char *SENSOR_HELP =
33 " Sensor manager dump options:\n"
34 " -h: [sensor command help]\n"
35 " -l: [show sensor list]\n"
36 " -d: [The data information is displayed 10 times]\n"
37 " -c: [show sensor client information]\n";
38
SensorHdiDump()39 SensorHdiDump::SensorHdiDump()
40 {}
41
~SensorHdiDump()42 SensorHdiDump::~SensorHdiDump()
43 {}
44
SensorShowList(struct HdfSBuf * reply)45 int32_t SensorHdiDump::SensorShowList(struct HdfSBuf *reply)
46 {
47 std::vector<V3_0::HdfSensorInformation> sensorInfoList;
48
49 SensorClientsManager::GetInstance()->CopySensorInfo(sensorInfoList, GET_SENSORINFO);
50
51 if (sensorInfoList.empty()) {
52 HDF_LOGE("%{public}s: no sensor info in list", __func__);
53 return HDF_FAILURE;
54 }
55
56 for (const auto &it : sensorInfoList) {
57 std::string st = {0};
58 st = "sensorName: " + it.sensorName + "\n" +
59 "deviceSensorInfo: " + SENSOR_HANDLE_TO_STRING(it.deviceSensorInfo) + "\n" +
60 "maxRange: " + std::to_string(it.maxRange) + "\n" +
61 "accuracy: " + std::to_string(it.accuracy) + "\n" +
62 "power: " + std::to_string(it.power) + "\n" +
63 "minDelay: " + std::to_string(it.minDelay) + "\n" +
64 "maxDelay: " + std::to_string(it.maxDelay) + "\n" +
65 "fifoMaxEventCount: " + std::to_string(it.fifoMaxEventCount) + "\n" +
66 "============================================\n";
67 (void)HdfSbufWriteString(reply, st.c_str());
68 }
69 return HDF_SUCCESS;
70 }
71
SensorInfoDataToString(const float * data,const int64_t timesTamp,const int32_t dataDimension,const SensorHandle sensorHandle)72 std::string SensorHdiDump::SensorInfoDataToString(const float *data,
73 const int64_t timesTamp,
74 const int32_t dataDimension,
75 const SensorHandle sensorHandle)
76 {
77 std::string dataStr = {0};
78 std::string st = {0};
79 char arrayStr[DATA_LEN] = {0};
80
81 for (int32_t i = 0; i < dataDimension; i++) {
82 int32_t ilen = DATA_LEN - strlen(arrayStr) - 1;
83 if (ilen <= 0) {
84 HDF_LOGE("%{public}s: bufferover failed", __func__);
85 return st;
86 }
87 if (sprintf_s(arrayStr + strlen(arrayStr), ilen, "[%f]", data[i]) < 0) {
88 HDF_LOGE("%{public}s: sprintf_s failed", __func__);
89 return st;
90 }
91 }
92
93 dataStr = arrayStr;
94 st = "sensorHandle id: " + SENSOR_HANDLE_TO_STRING(sensorHandle) + ", ts = " +
95 std::to_string(timesTamp / 1e9) + ", data = " + dataStr + "\n";
96
97 return st;
98 }
99
ShowData(const float * data,const int64_t timesTamp,const int32_t dataDimension,const SensorHandle sensorHandle,struct HdfSBuf * reply)100 int32_t SensorHdiDump::ShowData(const float *data,
101 const int64_t timesTamp,
102 const int32_t dataDimension,
103 const SensorHandle sensorHandle,
104 struct HdfSBuf *reply)
105 {
106 std::string sensorInfoData = {0};
107
108 switch (dataDimension) {
109 case MEM_X:
110 case MEM_XY:
111 case MEM_XYZ:
112 case MEM_UNCALIBRATED:
113 case MEM_POSTURE:
114 case MEM_SPE_RGB:
115 sensorInfoData = SensorInfoDataToString(data, timesTamp, dataDimension, sensorHandle);
116 break;
117 default:
118 HDF_LOGE("%{public}s: unsupported dimension, dimension is %{public}d", __func__, dataDimension);
119 break;
120 }
121
122 if (sensorInfoData.empty()) {
123 HDF_LOGE("%{public}s: sensor infomation data is empty!", __func__);
124 return HDF_FAILURE;
125 }
126
127 (void)HdfSbufWriteString(reply, sensorInfoData.c_str());
128 return HDF_SUCCESS;
129 }
130
SensorShowData(struct HdfSBuf * reply)131 int32_t SensorHdiDump::SensorShowData(struct HdfSBuf *reply)
132 {
133 struct SensorsDataPack eventDumpList;
134 uint8_t *eventData = nullptr;
135
136 SensorClientsManager::GetInstance()->GetEventData(eventDumpList);
137
138 (void)HdfSbufWriteString(reply, "============== The last 10 data records ==============\n");
139
140 for (int32_t i = 0; i < eventDumpList.count; i++) {
141 int32_t index = static_cast<const uint32_t>(eventDumpList.pos + i) < MAX_DUMP_DATA_SIZE ?
142 (eventDumpList.pos + i) : (eventDumpList.pos + i - MAX_DUMP_DATA_SIZE);
143 uint32_t dataLen = eventDumpList.listDumpArray[index].dataLen;
144 eventData = static_cast<uint8_t*>(OsalMemCalloc(dataLen));
145 if (eventData == nullptr) {
146 HDF_LOGE("%{public}s: malloc failed!", __func__);
147 return HDF_FAILURE;
148 }
149
150 std::copy(eventDumpList.listDumpArray[index].data.begin(),
151 eventDumpList.listDumpArray[index].data.end(), eventData);
152 float *data = reinterpret_cast<float*>(eventData);
153
154 int32_t dataDimension = static_cast<int32_t>(dataLen / sizeof(float));
155 SensorHandle sensorHandle = {
156 eventDumpList.listDumpArray[index].deviceSensorInfo.deviceId,
157 eventDumpList.listDumpArray[index].deviceSensorInfo.sensorType,
158 eventDumpList.listDumpArray[index].deviceSensorInfo.sensorId,
159 eventDumpList.listDumpArray[index].deviceSensorInfo.location
160 };
161 int32_t ret = ShowData(data, eventDumpList.listDumpArray[index].timestamp, dataDimension,
162 sensorHandle, reply);
163 if (ret != HDF_SUCCESS) {
164 OsalMemFree(eventData);
165 HDF_LOGE("%{public}s: print sensor infomation data failed!", __func__);
166 return HDF_FAILURE;
167 }
168
169 OsalMemFree(eventData);
170 eventData = nullptr;
171 }
172
173 return HDF_SUCCESS;
174 }
175
SensorShowClient(struct HdfSBuf * reply)176 int32_t SensorHdiDump::SensorShowClient(struct HdfSBuf *reply)
177 {
178 std::unordered_map<int, SensorClientInfo> sensorClientInfoMap;
179 if (!SensorClientsManager::GetInstance()->GetClients(HDF_TRADITIONAL_SENSOR_TYPE, sensorClientInfoMap)) {
180 HDF_LOGD("%{public}s groupId %{public}d is not used by anyone", __func__, HDF_TRADITIONAL_SENSOR_TYPE);
181 return HDF_FAILURE;
182 }
183 std::unordered_map<SensorHandle, struct BestSensorConfig> bestSensorConfigMap;
184 (void)SensorClientsManager::GetInstance()->GetBestSensorConfigMap(bestSensorConfigMap);
185 std::unordered_map<SensorHandle, std::set<int32_t>> sensorEnabled =
186 SensorClientsManager::GetInstance()->GetSensorUsed();
187 (void)HdfSbufWriteString(reply, "============== all clients information ==============\n\n");
188 std::string sensorInfoData = "";
189 sensorInfoData += "bestSensorConfigMap={\n";
190 for (const auto &entry2 : bestSensorConfigMap) {
191 auto sensorHandle = entry2.first;
192 auto bestSensorConfig = entry2.second;
193 sensorInfoData += "{sensorHandle=" + SENSOR_HANDLE_TO_STRING(sensorHandle) + ",";
194 sensorInfoData += "bestSensorConfig={";
195 sensorInfoData += "samplingInterval=" + std::to_string(bestSensorConfig.samplingInterval) + ",";
196 sensorInfoData += "reportInterval=" + std::to_string(bestSensorConfig.reportInterval);
197 sensorInfoData += "}}\n";
198 }
199 sensorInfoData += "}\n\n";
200 for (const auto &entry : sensorClientInfoMap) {
201 auto serviceId = entry.first;
202 auto sensorClientInfo = entry.second;
203 sensorInfoData += "serviceId=" + std::to_string(serviceId) + " ";
204 sensorInfoData += "sensorConfigMap_={\n";
205 for (const auto &entry2 : sensorClientInfo.sensorConfigMap_) {
206 auto sensorHandle = entry2.first;
207 auto sensorConfig = entry2.second;
208 sensorInfoData += "{sensorHandle=" + SENSOR_HANDLE_TO_STRING(sensorHandle) + ",";
209 sensorInfoData += "sensorConfig={";
210 sensorInfoData += "samplingInterval=" + std::to_string(sensorConfig.samplingInterval) + ",";
211 sensorInfoData += "reportInterval=" + std::to_string(sensorConfig.reportInterval) + ",";
212 sensorInfoData += "curCount/periodCount=" +
213 std::to_string(sensorClientInfo.curCountMap_[sensorHandle]) + "/" +
214 std::to_string(sensorClientInfo.periodCountMap_[sensorHandle]) + ",";
215 if (sensorEnabled.find(sensorHandle) != sensorEnabled.end() &&
216 sensorEnabled.find(sensorHandle)->second.find(serviceId) !=
217 sensorEnabled.find(sensorHandle)->second.end()) {
218 sensorInfoData += "enable";
219 }
220 sensorInfoData += "}}\n";
221 }
222 sensorInfoData += "}\n\n";
223 }
224 (void)HdfSbufWriteString(reply, sensorInfoData.c_str());
225 return HDF_SUCCESS;
226 }
227
DevHostSensorHdiDump(struct HdfSBuf * data,struct HdfSBuf * reply)228 int32_t SensorHdiDump::DevHostSensorHdiDump(struct HdfSBuf *data, struct HdfSBuf *reply)
229 {
230 uint32_t argc = 0;
231
232 if (data == nullptr || reply == nullptr) {
233 HDF_LOGE("%{public}s: data or reply is nullptr", __func__);
234 return HDF_FAILURE;
235 }
236
237 if (!HdfSbufReadUint32(data, &argc)) {
238 HDF_LOGE("%{public}s: read &argc failed", __func__);
239 return HDF_FAILURE;
240 }
241
242 if (argc == 0) {
243 (void)HdfSbufWriteString(reply, SENSOR_HELP);
244 return HDF_SUCCESS;
245 }
246
247 for (uint32_t i = 0; i < argc; i++) {
248 const char *value = HdfSbufReadString(data);
249 if (value == nullptr) {
250 HDF_LOGE("%{public}s: arg is invalid", __func__);
251 return HDF_FAILURE;
252 }
253 if (strcmp(value, "-h") == 0) {
254 (void)HdfSbufWriteString(reply, SENSOR_HELP);
255 return HDF_SUCCESS;
256 } else if (strcmp(value, "-l") == 0) {
257 SensorShowList(reply);
258 return HDF_SUCCESS;
259 } else if (strcmp(value, "-d") == 0) {
260 SensorShowData(reply);
261 return HDF_SUCCESS;
262 } else if (strcmp(value, "-c") == 0) {
263 SensorShowClient(reply);
264 return HDF_SUCCESS;
265 }
266 }
267
268 return HDF_SUCCESS;
269 }
270
GetSensorDump(struct HdfSBuf * data,struct HdfSBuf * reply)271 int32_t GetSensorDump(struct HdfSBuf *data, struct HdfSBuf *reply)
272 {
273 SensorHdiDump::DevHostSensorHdiDump(data, reply);
274 return HDF_SUCCESS;
275 }
276
277 } // V3_0
278 } // Sensor
279 } // HDI
280 } // OHOS