From bfc20a0ee14c841c75eeb6878dc5e3eced7d8e2d Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Fri, 10 Jan 2020 14:50:07 -0600 Subject: Refectoring --- include/Device/Device.h | 4 +++- src/Device/Device.cpp | 33 +++++++++++++++++---------------- src/Version.cpp | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/Device/Device.h b/include/Device/Device.h index 86e284f..f04be48 100644 --- a/include/Device/Device.h +++ b/include/Device/Device.h @@ -17,7 +17,7 @@ class Device { rapidjson::Document::AllocatorType& alloc = deviceInfo.GetAllocator(); rapidjson::Document::AllocatorType& accessoryCardsAlloc = accessoryCards.GetAllocator(); static std::map capabilityList; - std::map deviceInfoList = {{"deviceId", ""},{"hardwareVersion", ""}, + static std::map deviceInfoList = {{"deviceId", ""},{"hardwareVersion", ""}, {"imei", ""},{"macAddress", "00:00:00:00:00:00"},{"macBluetooth", "00:00:00:00:00:00"}, {"macWifi", "00:00:00:00:00:00"},{"productId", ""},{"uuid", ""},{"vendorId", ""}}; @@ -85,6 +85,8 @@ class Device { Device(); void getSystemTreeJson(const char * dir_name); void init(); + bool isAccessoryCard(const char * d_name, const char * dir_name); + bool isValidDirectory(const struct dirent * entry, std::string fullPath, const char * d_name); void load(); void logInfo(std::string info); void logError(std::string info); diff --git a/src/Device/Device.cpp b/src/Device/Device.cpp index e92ea6e..2498edd 100644 --- a/src/Device/Device.cpp +++ b/src/Device/Device.cpp @@ -34,11 +34,24 @@ std::map Device::capabilityList = {{"adc", false},{"battery", {"nodeRed", false},{"rs232", false},{"rs422", false},{"rs485", false},{"serial", false}, {"wifi", false}}; +std::map Device::deviceInfoList = {{"deviceId", ""},{"hardwareVersion", ""}, + {"imei", ""},{"macAddress", "00:00:00:00:00:00"},{"macBluetooth", "00:00:00:00:00:00"}, + {"macWifi", "00:00:00:00:00:00"},{"productId", ""},{"uuid", ""},{"vendorId", ""}}; + Device::Device() { isRoot = !getuid(); verbose = false; } +bool Device::isAccessoryCard(const char * d_name, const char * dir_name) { + return std::binary_search(apIdentifiers.begin(), apIdentifiers.end(), dir_name) && !regex_match(d_name, apFilters); +} + +bool Device::isValidDirectory(const struct dirent * entry, std::string fullPath, const char * d_name) { + std::string path = fullPath + "/" + std::string(d_name); + return (entry->d_type & DT_DIR) && strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0 && (path.length() <= PATH_MAX); +} + void Device::getSystemTreeJson(const char * dir_name) { std::string fullPath = SYSFS_PLATFORM + std::string(dir_name); DIR * d = opendir (fullPath.c_str()); @@ -57,7 +70,7 @@ void Device::getSystemTreeJson(const char * dir_name) { if (!(entry->d_type & DT_DIR) && (MTS::System::readFile(fullPath + "/" + std::string(d_name), fileData) == 0)) { fileData = MTS::Text::trim(fileData); if (strlen(dir_name) > 0) { - if (std::binary_search(apIdentifiers.begin(), apIdentifiers.end(), dir_name) && !regex_match(d_name, apFilters)) { + if (isAccessoryCard(d_name, dir_name)) { if (accessoryCard.IsNull()) { accessoryCard.SetObject(); } else if (accessoryCard.HasMember(d_name)) { @@ -93,15 +106,8 @@ void Device::getSystemTreeJson(const char * dir_name) { } } } - if (entry->d_type & DT_DIR) { /* Check that the directory is not "d" or d's parent. */ - if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { - std::string path = fullPath + "/" + std::string(d_name); - if (path.length() >= PATH_MAX) { - logError("Path length has got too long.\n"); - exitHandler(99); - } - getSystemTreeJson(d_name); /* Recursively call with the new path */ - } + if (isValidDirectory(entry, fullPath, d_name)) { /* Check that the directory is not "d" or d's parent. */ + getSystemTreeJson(d_name); /* Recursively call with the new path */ } } if (closedir (d)) { /* After going through all the entries, close the directory. */ @@ -215,12 +221,7 @@ void Device::printDir(const std::string dir_name, std::vector &resu result.append(d_name); results.push_back(result); } - if ((entry->d_type & DT_DIR) && strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) { - std::string path = fullPath + "/" + std::string(d_name); - if (path.length() >= PATH_MAX) { - logError("Path length has got too long.\n"); - exitHandler(99); - } + if (isValidDirectory(entry, fullPath, d_name)) { /* Check that the directory is not "d" or d's parent. */ printDir(d_name, results); } } diff --git a/src/Version.cpp b/src/Version.cpp index 393f305..b963c93 100644 --- a/src/Version.cpp +++ b/src/Version.cpp @@ -1,4 +1,4 @@ //Pre-Build Auto-Generated Source #include "Version.h" -const std::string Version::version("v1.0.0-9-g8c7856e"); +const std::string Version::version("v1.0.0-10-gb69c751"); -- cgit v1.2.3