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_dump.h"
17 #include <inttypes.h>
18 #include "devhost_dump_reg.h"
19 #include "hdf_log.h"
20 #include "sensor_channel.h"
21 #include "sensor_controller.h"
22 #include "sensor_manager.h"
23
24 #define HDF_LOG_TAG uhdf_sensor_service
25 #define STRING_LEN 2048
26
27 static const char *g_helpComment =
28 " Sensor manager dump options:\n"
29 " -h: [sensor command help]\n"
30 " -l: [show sensor list]\n"
31 " -d: [The data information is displayed 10 times]\n";
32
33 struct SensorDevelopmentList {
34 int32_t sensorTypeId;
35 char sensorName[SENSOR_NAME_MAX_LEN];
36 int32_t dataDimension;
37 };
38
39 struct SensorDevelopmentList g_sensorList[] = {
40 { SENSOR_TYPE_NONE, "sensor_test", DATA_X },
41 { SENSOR_TYPE_ACCELEROMETER, "accelerometer", DATA_XYZ },
42 { SENSOR_TYPE_PEDOMETER, "pedometer", DATA_X },
43 { SENSOR_TYPE_PROXIMITY, "proximity", DATA_X },
44 { SENSOR_TYPE_HALL, "hallrometer", DATA_X },
45 { SENSOR_TYPE_BAROMETER, "barometer", DATA_XY },
46 { SENSOR_TYPE_AMBIENT_LIGHT, "als", DATA_X },
47 { SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer", DATA_XYZ },
48 { SENSOR_TYPE_GYROSCOPE, "gyroscope", DATA_XYZ },
49 { SENSOR_TYPE_GRAVITY, "gravity", DATA_XYZ }
50 };
51
SensorShowList(struct HdfSBuf * reply)52 int32_t SensorShowList(struct HdfSBuf *reply)
53 {
54 int32_t index = -1;
55 int32_t ret = 0;
56 int32_t *sensorStatus = NULL;
57 struct SensorDevManager *sensorList = NULL;
58 char sensorInfoDate[STRING_LEN] = { 0 };
59
60 sensorList = GetSensorDevManager();
61 if ((sensorList == NULL) || (sensorList->sensorInfoEntry == NULL) || (sensorList->sensorSum == 0)) {
62 HDF_LOGE("%{publuc}s: sensorList is failed\n", __func__);
63 return DUMP_NULL_PTR;
64 }
65
66 sensorStatus = GetSensorStatus();
67 if (sensorStatus == NULL) {
68 HDF_LOGE("%{publuc}s: sensorStatus is failed\n", __func__);
69 return DUMP_NULL_PTR;
70 }
71
72 for (index = 0; index < sensorList->sensorSum; index++) {
73 ret = memset_s(sensorInfoDate, STRING_LEN, 0, STRING_LEN);
74 if (ret != DUMP_SUCCESS) {
75 HDF_LOGE("%{publuc}s: memset sensorInfoList is failed\n", __func__);
76 return DUMP_FAILURE;
77 }
78
79 ret = sprintf_s(sensorInfoDate, STRING_LEN,
80 "=================================================\n\r \
81 sensorName: %s \n\r \
82 sensorId: %d \n\r \
83 sensorStatus: %d \n\r \
84 maxRange: %f \n\r \
85 accuracy: %f \n\r \
86 power: %f \n\r \
87 minDelay: %" PRId64 "\n\r \
88 maxDelay: %" PRId64 "\n\r",
89 sensorList->sensorInfoEntry[index].sensorName,
90 sensorList->sensorInfoEntry[index].sensorId,
91 sensorStatus[sensorList->sensorInfoEntry[index].sensorId],
92 sensorList->sensorInfoEntry[index].maxRange,
93 sensorList->sensorInfoEntry[index].accuracy,
94 sensorList->sensorInfoEntry[index].power,
95 sensorList->sensorInfoEntry[index].minDelay,
96 sensorList->sensorInfoEntry[index].maxDelay);
97 if (ret < DUMP_SUCCESS) {
98 HDF_LOGE("%{publuc}s: sprintf sensorList is failed\n", __func__);
99 return DUMP_FAILURE;
100 }
101
102 (void)HdfSbufWriteString(reply, sensorInfoDate);
103 }
104
105 return DUMP_SUCCESS;
106 }
107
ShowData(const float * data,int64_t timesTamp,const struct SensorDevelopmentList sensorNode,struct HdfSBuf * reply)108 static void ShowData(const float *data, int64_t timesTamp, const struct SensorDevelopmentList sensorNode,
109 struct HdfSBuf *reply)
110 {
111 int32_t ret = 0;
112 char sensorInfoDate[STRING_LEN] = { 0 };
113
114 if (sensorNode.dataDimension == DATA_X) {
115 ret = sprintf_s(sensorInfoDate, STRING_LEN,
116 "sensor name :[%s], sensor id :[%d], ts=[%0.9f], data= [%f]\n\r",
117 sensorNode.sensorName, sensorNode.sensorTypeId, timesTamp / 1e9, *(data));
118 if (ret < DUMP_SUCCESS) {
119 HDF_LOGE("%{publuc}s: sprintf sensorInfoDate is failed\n", __func__);
120 return;
121 }
122 } else {
123 ret = sprintf_s(sensorInfoDate, STRING_LEN,
124 "sensor name :[%s], sensor id :[%d], ts=[%0.9f], data= [%f], [%f], [%f]\n\r",
125 sensorNode.sensorName, sensorNode.sensorTypeId, timesTamp / 1e9, *(data), *(data + DATA_X),
126 *(data + DATA_XY));
127 if (ret < DUMP_SUCCESS) {
128 HDF_LOGE("%{publuc}s: sprintf ShowData is failed\n", __func__);
129 return;
130 }
131 }
132 (void)HdfSbufWriteString(reply, sensorInfoDate);
133 }
134
SensorShowData(struct HdfSBuf * reply)135 int32_t SensorShowData(struct HdfSBuf *reply)
136 {
137 int32_t len;
138 int32_t ret = 0;
139 struct SensorDatePack *eventDumpList;
140 char sensorInfoDate[STRING_LEN] = { 0 };
141
142 eventDumpList = GetEventData();
143
144 ret = sprintf_s(sensorInfoDate, STRING_LEN, "======The last 10 data records======\n\r");
145 if (ret < DUMP_SUCCESS) {
146 HDF_LOGE("%{publuc}s: sprintf SensorShowData is failed\n", __func__);
147 return DUMP_NULL_PTR;
148 }
149 (void)HdfSbufWriteString(reply, sensorInfoDate);
150
151 if (eventDumpList->count < MAX_DUMP_DATA_SIZE) {
152 for (len = 0; len < eventDumpList->count; len++) {
153 float *data = (float *)(eventDumpList->listDumpArr[len].data);
154 ShowData(data, eventDumpList->listDumpArr[len].timestamp,
155 g_sensorList[eventDumpList->listDumpArr[len].sensorId], reply);
156 }
157 } else {
158 int32_t pos = eventDumpList->pos;
159 for (len = 0; len < eventDumpList->count; len++) {
160 pos = pos + 1 > MAX_DUMP_DATA_SIZE ? 1 : pos + 1;
161 float *data = (float *)(eventDumpList->listDumpArr[pos - 1].data);
162 ShowData(data, eventDumpList->listDumpArr[pos - 1].timestamp,
163 g_sensorList[eventDumpList->listDumpArr[pos - 1].sensorId], reply);
164 }
165 }
166
167 return DUMP_SUCCESS;
168 }
169
DevHostSensorDump(struct HdfSBuf * data,struct HdfSBuf * reply)170 static int32_t DevHostSensorDump(struct HdfSBuf *data, struct HdfSBuf *reply)
171 {
172 uint32_t argv = 0;
173
174 if (data == NULL || reply == NULL) {
175 HDF_LOGE("%{public}s data or reply is invalid", __func__);
176 return HDF_FAILURE;
177 }
178
179 if (!HdfSbufReadUint32(data, &argv)) {
180 HDF_LOGE("%{public}s: read &argv failed!", __func__);
181 return DUMP_FAILURE;
182 }
183
184 if (argv == 0) {
185 (void)HdfSbufWriteString(reply, g_helpComment);
186 return HDF_SUCCESS;
187 }
188
189 for (uint32_t i = 0; i < argv; i++) {
190 const char *value = HdfSbufReadString(data);
191 if (value == NULL) {
192 HDF_LOGE("%{public}s value is invalid", __func__);
193 return HDF_FAILURE;
194 }
195 if (strcmp(value, "-h") == 0) {
196 (void)HdfSbufWriteString(reply, g_helpComment);
197 return HDF_SUCCESS;
198 } else if (strcmp(value, "-l") == 0) {
199 SensorShowList(reply);
200 return HDF_SUCCESS;
201 } else if (strcmp(value, "-d") == 0) {
202 SensorShowData(reply);
203 return HDF_SUCCESS;
204 }
205 }
206
207 return HDF_SUCCESS;
208 }
209
SensorDevRegisterDump(void)210 void SensorDevRegisterDump(void)
211 {
212 DevHostRegisterDumpHost(DevHostSensorDump);
213 }
214