diff --git a/services/sensor/include/sensor_dump.h b/services/sensor/include/sensor_dump.h index b44b5a6a7425feb7fd2385b5923a0f44b038ffca..b296d8f2f00cce39d23b9fde349d7a9d1203d412 100644 --- a/services/sensor/include/sensor_dump.h +++ b/services/sensor/include/sensor_dump.h @@ -32,13 +32,13 @@ class SensorDump : public Singleton { public: SensorDump() = default; virtual ~SensorDump() = default; + void ParseCommand(int32_t fd, const std::vector &args, const std::vector &sensors, + ClientInfo &clientInfo); void DumpHelp(int32_t fd); - bool DumpSensorHelp(int32_t fd, const std::vector &args); - bool DumpSensorList(int32_t fd, const std::vector &sensors, const std::vector &args); - bool DumpSensorChannel(int32_t fd, ClientInfo &clientInfo, const std::vector &args); - bool DumpOpeningSensor(int32_t fd, const std::vector &sensors, ClientInfo &clientInfo, - const std::vector &args); - bool DumpSensorData(int32_t fd, ClientInfo &clientInfo, const std::vector &args); + bool DumpSensorList(int32_t fd, const std::vector &sensors); + bool DumpSensorChannel(int32_t fd, ClientInfo &clientInfo); + bool DumpOpeningSensor(int32_t fd, const std::vector &sensors, ClientInfo &clientInfo); + bool DumpSensorData(int32_t fd, ClientInfo &clientInfo); private: DISALLOW_COPY_AND_MOVE(SensorDump); diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index c3933c041118de9c2a621d06558b8c23d22d4d87..7d031184c8d8ea709021f58909a1468018db7df7 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -15,10 +15,14 @@ #include "sensor_dump.h" +#include + #include +#include #include #include +#include "securec.h" #include "sensors_errors.h" namespace OHOS { @@ -90,32 +94,77 @@ std::unordered_map SensorDump::sensorMap_ = { { WEAR_DETECTION, "WEAR DETECTION" }, }; -bool SensorDump::DumpSensorHelp(int32_t fd, const std::vector &args) +void SensorDump::ParseCommand(int32_t fd, const std::vector &args, const std::vector &sensors, + ClientInfo &clientInfo) { - if ((args.empty()) || (args[0].compare(u"-h") != 0)) { - SEN_HILOGE("args cannot be empty or invalid"); - return false; + int32_t optionIndex = 0; + struct option dumpOptions[] = { + {"channel", no_argument, 0, 'c'}, + {"data", no_argument, 0, 'd'}, + {"open", no_argument, 0, 'o'}, + {"help", no_argument, 0, 'h'}, + {"list", no_argument, 0, 'l'}, + {NULL, 0, 0, 0} + }; + char **argv = new char *[args.size()]; + for (size_t i = 0; i < args.size(); ++i) { + argv[i] = new char[args[i].size() + 1]; + if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) { + SEN_HILOGE("strcpy_s error"); + goto RELEASE_RES; + return; + } } - DumpHelp(fd); - return true; + optind = 1; + int32_t c; + while ((c = getopt_long(args.size(), argv, "cdohl", dumpOptions, &optionIndex)) != -1) { + switch (c) { + case 'c': { + DumpSensorChannel(fd, clientInfo); + break; + } + case 'd': { + DumpSensorData(fd, clientInfo); + break; + } + case 'o': { + DumpOpeningSensor(fd, sensors, clientInfo); + break; + } + case 'h': { + DumpHelp(fd); + break; + } + case 'l': { + DumpSensorList(fd, sensors); + break; + } + default: { + dprintf(fd, "cmd param is error\n"); + DumpHelp(fd); + break; + } + } + } + RELEASE_RES: + for (size_t i = 0; i < args.size(); ++i) { + delete[] argv[i]; + } + delete[] argv; } void SensorDump::DumpHelp(int32_t fd) { dprintf(fd, "Usage:\n"); - dprintf(fd, " -h: dump help\n"); - dprintf(fd, " -l: dump the sensor list\n"); - dprintf(fd, " -c: dump the sensor data channel info\n"); - dprintf(fd, " -o: dump the opening sensors\n"); - dprintf(fd, " -d: dump the last 10 packages sensor data\n"); + dprintf(fd, " -h, --help: dump help\n"); + dprintf(fd, " -l, --list: dump the sensor list\n"); + dprintf(fd, " -c, --channel: dump the sensor data channel info\n"); + dprintf(fd, " -o, --open: dump the opening sensors\n"); + dprintf(fd, " -d, --data: dump the last 10 packages sensor data\n"); } -bool SensorDump::DumpSensorList(int32_t fd, const std::vector &sensors, const std::vector &args) +bool SensorDump::DumpSensorList(int32_t fd, const std::vector &sensors) { - if ((args.empty()) || (args[0].compare(u"-l") != 0)) { - SEN_HILOGE("args cannot be empty or invalid"); - return false; - } DumpCurrentTime(fd); dprintf(fd, "Total sensor:%d, Sensor list:\n", int32_t { sensors.size() }); for (const auto &sensor : sensors) { @@ -130,12 +179,8 @@ bool SensorDump::DumpSensorList(int32_t fd, const std::vector &sensors, return true; } -bool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo, const std::vector &args) +bool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo) { - if ((args.empty()) || (args[0].compare(u"-c") != 0)) { - SEN_HILOGE("args cannot be empty or invalid"); - return false; - } DumpCurrentTime(fd); dprintf(fd, "Sensor channel info:\n"); std::vector channelInfo; @@ -151,13 +196,8 @@ bool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo, const std return true; } -bool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector &sensors, ClientInfo &clientInfo, - const std::vector &args) +bool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector &sensors, ClientInfo &clientInfo) { - if ((args.empty()) || (args[0].compare(u"-o") != 0)) { - SEN_HILOGE("args cannot be empty or invalid"); - return false; - } DumpCurrentTime(fd); dprintf(fd, "Opening sensors:\n"); for (const auto &sensor : sensors) { @@ -170,12 +210,8 @@ bool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector &sensor return true; } -bool SensorDump::DumpSensorData(int32_t fd, ClientInfo &clientInfo, const std::vector &args) +bool SensorDump::DumpSensorData(int32_t fd, ClientInfo &clientInfo) { - if ((args.empty()) || (args[0].compare(u"-d") != 0)) { - SEN_HILOGE("args cannot be empty or invalid"); - return false; - } dprintf(fd, "Last 10 packages sensor data:\n"); auto dataMap = clientInfo.GetDumpQueue(); int32_t j = 0; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 5c1b26b5cef793a9c8d1719577373c3e1ec5ce6a..19bad2560e429c17849049bcf13dc0c028d25922 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -16,6 +16,7 @@ #include "sensor_service.h" #include +#include #include #include @@ -35,7 +36,6 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorService" }; constexpr uint32_t INVALID_SENSOR_ID = -1; -constexpr int32_t MAX_DMUP_PARAM = 2; constexpr int32_t INVALID_PID = -1; constexpr int64_t MAX_EVENT_COUNT = 1000; enum { @@ -429,24 +429,23 @@ void SensorService::UnregisterClientDeathRecipient(sptr sensorCli int32_t SensorService::Dump(int32_t fd, const std::vector &args) { CALL_LOG_ENTER; - SensorDump &sensorDump = SensorDump::GetInstance(); - if ((args.empty()) || (args[0].size() != MAX_DMUP_PARAM)) { - SEN_HILOGE("param cannot be empty or the length is not 2"); - dprintf(fd, "cmd param number is not equal to 2\n"); - sensorDump.DumpHelp(fd); + if (fd < 0) { + SEN_HILOGE("fd is invalid"); return DUMP_PARAM_ERR; } - bool helpRet = sensorDump.DumpSensorHelp(fd, args); - bool listRet = sensorDump.DumpSensorList(fd, sensors_, args); - bool channelRet = sensorDump.DumpSensorChannel(fd, clientInfo_, args); - bool openRet = sensorDump.DumpOpeningSensor(fd, sensors_, clientInfo_, args); - bool dataRet = sensorDump.DumpSensorData(fd, clientInfo_, args); - bool total = helpRet + listRet + channelRet + openRet + dataRet; - if (!total) { - dprintf(fd, "cmd param is error\n"); + SensorDump &sensorDump = SensorDump::GetInstance(); + if (args.empty()) { + SEN_HILOGE("param cannot be empty"); + dprintf(fd, "param cannot be empty\n"); sensorDump.DumpHelp(fd); return DUMP_PARAM_ERR; } + std::vector argList = { "" }; + std::transform(args.begin(), args.end(), std::back_inserter(argList), + [](const std::u16string &arg) { + return Str16ToStr8(arg); + }); + sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_); return ERR_OK; } } // namespace Sensors