1bae4d13cSopenharmony_ci/*
2bae4d13cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License.
5bae4d13cSopenharmony_ci * You may obtain a copy of the License at
6bae4d13cSopenharmony_ci *
7bae4d13cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bae4d13cSopenharmony_ci *
9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and
13bae4d13cSopenharmony_ci * limitations under the License.
14bae4d13cSopenharmony_ci */
15bae4d13cSopenharmony_ci
16bae4d13cSopenharmony_ci#include "sensor_dump.h"
17bae4d13cSopenharmony_ci
18bae4d13cSopenharmony_ci#include <getopt.h>
19bae4d13cSopenharmony_ci
20bae4d13cSopenharmony_ci#include <cinttypes>
21bae4d13cSopenharmony_ci#include <cstring>
22bae4d13cSopenharmony_ci#include <ctime>
23bae4d13cSopenharmony_ci#include <queue>
24bae4d13cSopenharmony_ci
25bae4d13cSopenharmony_ci#include "securec.h"
26bae4d13cSopenharmony_ci#include "sensor_agent_type.h"
27bae4d13cSopenharmony_ci#include "sensor_errors.h"
28bae4d13cSopenharmony_ci
29bae4d13cSopenharmony_ci#undef LOG_TAG
30bae4d13cSopenharmony_ci#define LOG_TAG "SensorDump"
31bae4d13cSopenharmony_ci
32bae4d13cSopenharmony_cinamespace OHOS {
33bae4d13cSopenharmony_cinamespace Sensors {
34bae4d13cSopenharmony_ciusing namespace OHOS::HiviewDFX;
35bae4d13cSopenharmony_cinamespace {
36bae4d13cSopenharmony_ciconstexpr int32_t MAX_DUMP_PARAMETERS = 32;
37bae4d13cSopenharmony_ci#ifdef BUILD_VARIANT_ENG
38bae4d13cSopenharmony_ciconstexpr uint32_t MAX_DUMP_DATA_SIZE = 10;
39bae4d13cSopenharmony_ci#endif // BUILD_VARIANT_ENG
40bae4d13cSopenharmony_ciconstexpr uint32_t MS_NS = 1000000;
41bae4d13cSopenharmony_ci
42bae4d13cSopenharmony_cienum {
43bae4d13cSopenharmony_ci    SOLITARIES_DIMENSION = 1,
44bae4d13cSopenharmony_ci    TWO_DIMENSION = 2,
45bae4d13cSopenharmony_ci    COMMON_DIMENSION = 3,
46bae4d13cSopenharmony_ci    VECTOR_DIMENSION = 4,
47bae4d13cSopenharmony_ci    UNCALIBRATED_DIMENSION = 6,
48bae4d13cSopenharmony_ci    SEVEN_DIMENSION = 7,
49bae4d13cSopenharmony_ci    POSE_6DOF_DIMENSION = 15,
50bae4d13cSopenharmony_ci    DEFAULT_DIMENSION = 16,
51bae4d13cSopenharmony_ci};
52bae4d13cSopenharmony_ci} // namespace
53bae4d13cSopenharmony_ci
54bae4d13cSopenharmony_cistd::unordered_map<int32_t, std::string> SensorDump::sensorMap_ = {
55bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_ACCELEROMETER, "ACCELEROMETER" },
56bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, "ACCELEROMETER UNCALIBRATED" },
57bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_LINEAR_ACCELERATION, "LINEAR ACCELERATION" },
58bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GRAVITY, "GRAVITY" },
59bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GYROSCOPE, "GYROSCOPE" },
60bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_CAPACITIVE, "CAPACITIVE"},
61bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_TEMPERATURE, "TEMPERATURE"},
62bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GESTURE, "GESTURE"},
63bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, "GYROSCOPE UNCALIBRATED" },
64bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_SIGNIFICANT_MOTION, "SIGNIFICANT MOTION" },
65bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_PEDOMETER_DETECTION, "PEDOMETER DETECTION" },
66bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_PEDOMETER, "PEDOMETER" },
67bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, "AMBIENT TEMPERATURE" },
68bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_MAGNETIC_FIELD, "MAGNETIC FIELD" },
69bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, "MAGNETIC FIELD UNCALIBRATED" },
70bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_HUMIDITY, "HUMIDITY" },
71bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_BAROMETER, "BAROMETER" },
72bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_DEVICE_ORIENTATION, "DEVICE ORIENTATION" },
73bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_ORIENTATION, "ORIENTATION" },
74bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_ROTATION_VECTOR, "ROTATION VECTOR" },
75bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GAME_ROTATION_VECTOR, "GAME ROTATION VECTOR" },
76bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_GEOMAGNETIC_ROTATION_VECTOR, "GEOMAGNETIC ROTATION VECTOR" },
77bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_PROXIMITY, "PROXIMITY" },
78bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_PROXIMITY1, "SECONDARY PROXIMITY" },
79bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_AMBIENT_LIGHT, "AMBIENT LIGHT" },
80bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_AMBIENT_LIGHT1, "SECONDARY AMBIENT LIGHT" },
81bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_HALL, "HALL" },
82bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_HALL_EXT, "EXTENDED HALL" },
83bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_HEART_RATE, "HEART RATE" },
84bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_WEAR_DETECTION, "WEAR DETECTION" },
85bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_COLOR, "COLOR" },
86bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_SAR, "SAR" },
87bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_POSTURE, "POSTURE" },
88bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_HEADPOSTURE, "HEAD POSTURE" },
89bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_DROP_DETECTION, "DROP DETECTION" },
90bae4d13cSopenharmony_ci    { SENSOR_TYPE_ID_RPC, "RPC" },
91bae4d13cSopenharmony_ci};
92bae4d13cSopenharmony_ci
93bae4d13cSopenharmony_civoid SensorDump::RunSensorDump(int32_t fd, int32_t optionIndex, const std::vector<std::string> &args, char **argv)
94bae4d13cSopenharmony_ci{
95bae4d13cSopenharmony_ci    struct option dumpOptions[] = {
96bae4d13cSopenharmony_ci        {"channel", no_argument, 0, 'c'},
97bae4d13cSopenharmony_ci#ifdef BUILD_VARIANT_ENG
98bae4d13cSopenharmony_ci        {"data", no_argument, 0, 'd'},
99bae4d13cSopenharmony_ci#endif // BUILD_VARIANT_ENG
100bae4d13cSopenharmony_ci        {"open", no_argument, 0, 'o'},
101bae4d13cSopenharmony_ci        {"help", no_argument, 0, 'h'},
102bae4d13cSopenharmony_ci        {"list", no_argument, 0, 'l'},
103bae4d13cSopenharmony_ci        {NULL, 0, 0, 0}
104bae4d13cSopenharmony_ci    };
105bae4d13cSopenharmony_ci    optind = 1;
106bae4d13cSopenharmony_ci    int32_t c;
107bae4d13cSopenharmony_ci    while ((c = getopt_long(args.size(), argv, "cdohl", dumpOptions, &optionIndex)) != -1) {
108bae4d13cSopenharmony_ci        switch (c) {
109bae4d13cSopenharmony_ci            case 'c': {
110bae4d13cSopenharmony_ci                DumpSensorChannel(fd, clientInfo_);
111bae4d13cSopenharmony_ci                break;
112bae4d13cSopenharmony_ci            }
113bae4d13cSopenharmony_ci#ifdef BUILD_VARIANT_ENG
114bae4d13cSopenharmony_ci            case 'd': {
115bae4d13cSopenharmony_ci                DumpSensorData(fd, clientInfo_);
116bae4d13cSopenharmony_ci                break;
117bae4d13cSopenharmony_ci            }
118bae4d13cSopenharmony_ci#endif // BUILD_VARIANT_ENG
119bae4d13cSopenharmony_ci            case 'o': {
120bae4d13cSopenharmony_ci                DumpOpeningSensor(fd, sensors_, clientInfo_);
121bae4d13cSopenharmony_ci                break;
122bae4d13cSopenharmony_ci            }
123bae4d13cSopenharmony_ci            case 'h': {
124bae4d13cSopenharmony_ci                DumpHelp(fd);
125bae4d13cSopenharmony_ci                break;
126bae4d13cSopenharmony_ci            }
127bae4d13cSopenharmony_ci            case 'l': {
128bae4d13cSopenharmony_ci                DumpSensorList(fd, sensors_);
129bae4d13cSopenharmony_ci                break;
130bae4d13cSopenharmony_ci            }
131bae4d13cSopenharmony_ci            default: {
132bae4d13cSopenharmony_ci                dprintf(fd, "Unrecognized option, More info with: \"hidumper -s 3601 -a -h\"\n");
133bae4d13cSopenharmony_ci                break;
134bae4d13cSopenharmony_ci            }
135bae4d13cSopenharmony_ci        }
136bae4d13cSopenharmony_ci    }
137bae4d13cSopenharmony_ci}
138bae4d13cSopenharmony_ci
139bae4d13cSopenharmony_civoid SensorDump::ParseCommand(int32_t fd, const std::vector<std::string> &args, const std::vector<Sensor> &sensors,
140bae4d13cSopenharmony_ci    ClientInfo &clientInfo)
141bae4d13cSopenharmony_ci{
142bae4d13cSopenharmony_ci    int32_t count = 0;
143bae4d13cSopenharmony_ci    for (const auto &str : args) {
144bae4d13cSopenharmony_ci        if (str.find("--") == 0) {
145bae4d13cSopenharmony_ci            ++count;
146bae4d13cSopenharmony_ci            continue;
147bae4d13cSopenharmony_ci        }
148bae4d13cSopenharmony_ci        if (str.find("-") == 0) {
149bae4d13cSopenharmony_ci            count += static_cast<int32_t>(str.size()) - 1;
150bae4d13cSopenharmony_ci            continue;
151bae4d13cSopenharmony_ci        }
152bae4d13cSopenharmony_ci    }
153bae4d13cSopenharmony_ci    if (count > MAX_DUMP_PARAMETERS) {
154bae4d13cSopenharmony_ci        SEN_HILOGE("Cmd param number not more than 32");
155bae4d13cSopenharmony_ci        dprintf(fd, "Cmd param number not more than 32\n");
156bae4d13cSopenharmony_ci        return;
157bae4d13cSopenharmony_ci    }
158bae4d13cSopenharmony_ci    int32_t optionIndex = 0;
159bae4d13cSopenharmony_ci    char **argv = new (std::nothrow) char *[args.size()];
160bae4d13cSopenharmony_ci    CHKPV(argv);
161bae4d13cSopenharmony_ci    if (memset_s(argv, args.size() * sizeof(char *), 0, args.size() * sizeof(char *)) != EOK) {
162bae4d13cSopenharmony_ci        SEN_HILOGE("memset_s failed");
163bae4d13cSopenharmony_ci        delete[] argv;
164bae4d13cSopenharmony_ci        return;
165bae4d13cSopenharmony_ci    }
166bae4d13cSopenharmony_ci    for (size_t i = 0; i < args.size(); ++i) {
167bae4d13cSopenharmony_ci        argv[i] = new (std::nothrow) char[args[i].size() + 1];
168bae4d13cSopenharmony_ci        if (argv[i] == nullptr) {
169bae4d13cSopenharmony_ci            SEN_HILOGE("Alloc failure");
170bae4d13cSopenharmony_ci            goto RELEASE_RES;
171bae4d13cSopenharmony_ci        }
172bae4d13cSopenharmony_ci        if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
173bae4d13cSopenharmony_ci            SEN_HILOGE("strcpy_s error");
174bae4d13cSopenharmony_ci            goto RELEASE_RES;
175bae4d13cSopenharmony_ci        }
176bae4d13cSopenharmony_ci    }
177bae4d13cSopenharmony_ci    sensors_ = sensors;
178bae4d13cSopenharmony_ci    RunSensorDump(fd, optionIndex, args, argv);
179bae4d13cSopenharmony_ci    RELEASE_RES:
180bae4d13cSopenharmony_ci    for (size_t i = 0; i < args.size(); ++i) {
181bae4d13cSopenharmony_ci        if (argv[i] != nullptr) {
182bae4d13cSopenharmony_ci            delete[] argv[i];
183bae4d13cSopenharmony_ci        }
184bae4d13cSopenharmony_ci    }
185bae4d13cSopenharmony_ci    delete[] argv;
186bae4d13cSopenharmony_ci}
187bae4d13cSopenharmony_ci
188bae4d13cSopenharmony_civoid SensorDump::DumpHelp(int32_t fd)
189bae4d13cSopenharmony_ci{
190bae4d13cSopenharmony_ci    dprintf(fd, "Usage:\n");
191bae4d13cSopenharmony_ci    dprintf(fd, "      -h, --help: dump help\n");
192bae4d13cSopenharmony_ci    dprintf(fd, "      -l, --list: dump the sensor list\n");
193bae4d13cSopenharmony_ci    dprintf(fd, "      -c, --channel: dump the sensor data channel info\n");
194bae4d13cSopenharmony_ci    dprintf(fd, "      -o, --open: dump the opening sensors\n");
195bae4d13cSopenharmony_ci#ifdef BUILD_VARIANT_ENG
196bae4d13cSopenharmony_ci    dprintf(fd, "      -d, --data: dump the last 10 packages sensor data\n");
197bae4d13cSopenharmony_ci#endif // BUILD_VARIANT_ENG
198bae4d13cSopenharmony_ci}
199bae4d13cSopenharmony_ci
200bae4d13cSopenharmony_cibool SensorDump::DumpSensorList(int32_t fd, const std::vector<Sensor> &sensors)
201bae4d13cSopenharmony_ci{
202bae4d13cSopenharmony_ci    DumpCurrentTime(fd);
203bae4d13cSopenharmony_ci    dprintf(fd, "Total sensor:%d, Sensor list:\n", int32_t { sensors.size() });
204bae4d13cSopenharmony_ci    for (const auto &sensor : sensors) {
205bae4d13cSopenharmony_ci        auto sensorId = sensor.GetSensorId();
206bae4d13cSopenharmony_ci        if (sensorMap_.find(sensorId) == sensorMap_.end()) {
207bae4d13cSopenharmony_ci            continue;
208bae4d13cSopenharmony_ci        }
209bae4d13cSopenharmony_ci        dprintf(fd,
210bae4d13cSopenharmony_ci                "sensorId:%8u | sensorType:%s | sensorName:%s | vendorName:%s | maxRange:%f"
211bae4d13cSopenharmony_ci                "| fifoMaxEventCount:%d | minSamplePeriodNs:%" PRId64 " | maxSamplePeriodNs:%" PRId64 "\n",
212bae4d13cSopenharmony_ci                sensorId, sensorMap_[sensorId].c_str(), sensor.GetSensorName().c_str(), sensor.GetVendorName().c_str(),
213bae4d13cSopenharmony_ci                sensor.GetMaxRange(), sensor.GetFifoMaxEventCount(), sensor.GetMinSamplePeriodNs(),
214bae4d13cSopenharmony_ci                sensor.GetMaxSamplePeriodNs());
215bae4d13cSopenharmony_ci    }
216bae4d13cSopenharmony_ci    return true;
217bae4d13cSopenharmony_ci}
218bae4d13cSopenharmony_ci
219bae4d13cSopenharmony_cibool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo)
220bae4d13cSopenharmony_ci{
221bae4d13cSopenharmony_ci    DumpCurrentTime(fd);
222bae4d13cSopenharmony_ci    dprintf(fd, "Sensor channel info:\n");
223bae4d13cSopenharmony_ci    std::vector<SensorChannelInfo> channelInfo;
224bae4d13cSopenharmony_ci    clientInfo.GetSensorChannelInfo(channelInfo);
225bae4d13cSopenharmony_ci    for (const auto &channel : channelInfo) {
226bae4d13cSopenharmony_ci        auto sensorId = channel.GetSensorId();
227bae4d13cSopenharmony_ci        if (sensorMap_.find(sensorId) == sensorMap_.end()) {
228bae4d13cSopenharmony_ci            continue;
229bae4d13cSopenharmony_ci        }
230bae4d13cSopenharmony_ci        dprintf(fd,
231bae4d13cSopenharmony_ci                "uid:%d | packageName:%s | sensorId:%8u | sensorType:%s | samplingPeriodNs:%" PRId64 ""
232bae4d13cSopenharmony_ci                "| fifoCount:%u\n",
233bae4d13cSopenharmony_ci                channel.GetUid(), channel.GetPackageName().c_str(), sensorId, sensorMap_[sensorId].c_str(),
234bae4d13cSopenharmony_ci                channel.GetSamplingPeriodNs(), channel.GetFifoCount());
235bae4d13cSopenharmony_ci    }
236bae4d13cSopenharmony_ci    return true;
237bae4d13cSopenharmony_ci}
238bae4d13cSopenharmony_ci
239bae4d13cSopenharmony_cibool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector<Sensor> &sensors, ClientInfo &clientInfo)
240bae4d13cSopenharmony_ci{
241bae4d13cSopenharmony_ci    DumpCurrentTime(fd);
242bae4d13cSopenharmony_ci    dprintf(fd, "Opening sensors:\n");
243bae4d13cSopenharmony_ci    for (const auto &sensor : sensors) {
244bae4d13cSopenharmony_ci        int32_t sensorId = sensor.GetSensorId();
245bae4d13cSopenharmony_ci        if (sensorMap_.find(sensorId) == sensorMap_.end()) {
246bae4d13cSopenharmony_ci            continue;
247bae4d13cSopenharmony_ci        }
248bae4d13cSopenharmony_ci        if (clientInfo.GetSensorState(sensorId)) {
249bae4d13cSopenharmony_ci            dprintf(fd, "sensorId: %8u | sensorType: %s | channelSize: %lu\n",
250bae4d13cSopenharmony_ci                sensorId, sensorMap_[sensorId].c_str(), clientInfo.GetSensorChannel(sensorId).size());
251bae4d13cSopenharmony_ci        }
252bae4d13cSopenharmony_ci    }
253bae4d13cSopenharmony_ci    return true;
254bae4d13cSopenharmony_ci}
255bae4d13cSopenharmony_ci
256bae4d13cSopenharmony_ci#ifdef BUILD_VARIANT_ENG
257bae4d13cSopenharmony_cibool SensorDump::DumpSensorData(int32_t fd, ClientInfo &clientInfo)
258bae4d13cSopenharmony_ci{
259bae4d13cSopenharmony_ci    dprintf(fd, "Last 10 packages sensor data:\n");
260bae4d13cSopenharmony_ci    auto dataMap = clientInfo.GetDumpQueue();
261bae4d13cSopenharmony_ci    int32_t j = 0;
262bae4d13cSopenharmony_ci    for (auto &sensorData : dataMap) {
263bae4d13cSopenharmony_ci        int32_t sensorId = sensorData.first;
264bae4d13cSopenharmony_ci        if (sensorMap_.find(sensorId) == sensorMap_.end()) {
265bae4d13cSopenharmony_ci            continue;
266bae4d13cSopenharmony_ci        }
267bae4d13cSopenharmony_ci        dprintf(fd, "sensorId: %8u | sensorType: %s:\n", sensorId, sensorMap_[sensorId].c_str());
268bae4d13cSopenharmony_ci        for (uint32_t i = 0; i < MAX_DUMP_DATA_SIZE && (!sensorData.second.empty()); i++) {
269bae4d13cSopenharmony_ci            auto data = sensorData.second.front();
270bae4d13cSopenharmony_ci            sensorData.second.pop();
271bae4d13cSopenharmony_ci            timespec time = { 0, 0 };
272bae4d13cSopenharmony_ci            clock_gettime(CLOCK_REALTIME, &time);
273bae4d13cSopenharmony_ci            struct tm *timeinfo = localtime(&(time.tv_sec));
274bae4d13cSopenharmony_ci            CHKPF(timeinfo);
275bae4d13cSopenharmony_ci            dprintf(fd, "      %2d (ts=%.9f, time=%02d:%02d:%02d.%03d) | data:%s", ++j, data.timestamp / 1e9,
276bae4d13cSopenharmony_ci                    timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, int32_t { (time.tv_nsec / MS_NS) },
277bae4d13cSopenharmony_ci                    GetDataBySensorId(sensorId, data).c_str());
278bae4d13cSopenharmony_ci        }
279bae4d13cSopenharmony_ci    }
280bae4d13cSopenharmony_ci    return true;
281bae4d13cSopenharmony_ci}
282bae4d13cSopenharmony_ci#endif // BUILD_VARIANT_ENG
283bae4d13cSopenharmony_ci
284bae4d13cSopenharmony_civoid SensorDump::DumpCurrentTime(int32_t fd)
285bae4d13cSopenharmony_ci{
286bae4d13cSopenharmony_ci    timespec curTime = { 0, 0 };
287bae4d13cSopenharmony_ci    clock_gettime(CLOCK_REALTIME, &curTime);
288bae4d13cSopenharmony_ci    struct tm *timeinfo = localtime(&(curTime.tv_sec));
289bae4d13cSopenharmony_ci    CHKPV(timeinfo);
290bae4d13cSopenharmony_ci    dprintf(fd, "Current time: %02d:%02d:%02d.%03d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
291bae4d13cSopenharmony_ci            int32_t { (curTime.tv_nsec / MS_NS) });
292bae4d13cSopenharmony_ci}
293bae4d13cSopenharmony_ci
294bae4d13cSopenharmony_ciint32_t SensorDump::GetDataDimension(int32_t sensorId)
295bae4d13cSopenharmony_ci{
296bae4d13cSopenharmony_ci    switch (sensorId) {
297bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_BAROMETER:
298bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_HALL:
299bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_HALL_EXT:
300bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_TEMPERATURE:
301bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_PROXIMITY:
302bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_PROXIMITY1:
303bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_HUMIDITY:
304bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_AMBIENT_TEMPERATURE:
305bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_SIGNIFICANT_MOTION:
306bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_PEDOMETER_DETECTION:
307bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_PEDOMETER:
308bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_HEART_RATE:
309bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_WEAR_DETECTION:
310bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_SAR:
311bae4d13cSopenharmony_ci            return SOLITARIES_DIMENSION;
312bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_COLOR:
313bae4d13cSopenharmony_ci            return TWO_DIMENSION;
314bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_ROTATION_VECTOR:
315bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_HEADPOSTURE:
316bae4d13cSopenharmony_ci            return VECTOR_DIMENSION;
317bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED:
318bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED:
319bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED:
320bae4d13cSopenharmony_ci            return UNCALIBRATED_DIMENSION;
321bae4d13cSopenharmony_ci        case SENSOR_TYPE_ID_POSTURE:
322bae4d13cSopenharmony_ci            return SEVEN_DIMENSION;
323bae4d13cSopenharmony_ci        default:
324bae4d13cSopenharmony_ci            SEN_HILOGW("Unknown sensorId:%{public}d, size:%{public}d", sensorId, COMMON_DIMENSION);
325bae4d13cSopenharmony_ci            return COMMON_DIMENSION;
326bae4d13cSopenharmony_ci    }
327bae4d13cSopenharmony_ci}
328bae4d13cSopenharmony_ci
329bae4d13cSopenharmony_cistd::string SensorDump::GetDataBySensorId(int32_t sensorId, SensorData &sensorData)
330bae4d13cSopenharmony_ci{
331bae4d13cSopenharmony_ci    SEN_HILOGD("sensorId:%{public}u", sensorId);
332bae4d13cSopenharmony_ci    std::string str;
333bae4d13cSopenharmony_ci    int32_t dataLen = GetDataDimension(sensorId);
334bae4d13cSopenharmony_ci    if (sensorData.dataLen < sizeof(float)) {
335bae4d13cSopenharmony_ci        SEN_HILOGE("SensorData dataLen less than float size");
336bae4d13cSopenharmony_ci        return str;
337bae4d13cSopenharmony_ci    }
338bae4d13cSopenharmony_ci    auto data = reinterpret_cast<float *>(sensorData.data);
339bae4d13cSopenharmony_ci    for (int32_t i = 0; i < dataLen; ++i) {
340bae4d13cSopenharmony_ci        str.append(std::to_string(*data));
341bae4d13cSopenharmony_ci        if (i != dataLen - 1) {
342bae4d13cSopenharmony_ci            str.append(",");
343bae4d13cSopenharmony_ci        }
344bae4d13cSopenharmony_ci        ++data;
345bae4d13cSopenharmony_ci    }
346bae4d13cSopenharmony_ci    str.append("\n");
347bae4d13cSopenharmony_ci    return str;
348bae4d13cSopenharmony_ci}
349bae4d13cSopenharmony_ci} // namespace Sensors
350bae4d13cSopenharmony_ci} // namespace OHOS
351