From b2bd90192dfb9183c0d2b65be23352688782eac4 Mon Sep 17 00:00:00 2001 From: TinyOh Date: Thu, 28 Feb 2019 03:53:50 +0800 Subject: [PATCH] feat(api) sdk/firmware version check --- src/mynteye/api/api.cc | 4 +- src/mynteye/api/version_checker.cc | 123 +++++++++++++++++++++++++---- 2 files changed, 108 insertions(+), 19 deletions(-) diff --git a/src/mynteye/api/api.cc b/src/mynteye/api/api.cc index 5b440a7..3f034be 100644 --- a/src/mynteye/api/api.cc +++ b/src/mynteye/api/api.cc @@ -339,9 +339,7 @@ std::string API::GetInfo(const Info &info) const { LOG(WARNING) << "build.info not found: " << info_path; return "null"; } - std::string vs_main = fs["MYNTEYE_VERSION"]; - int vs_tweak = fs["MYNTEYE_VERSION_TWEAK"]; - return vs_main + std::string(".") + std::to_string(vs_tweak); + return fs["MYNTEYE_VERSION"]; } return device_->GetInfo(info); diff --git a/src/mynteye/api/version_checker.cc b/src/mynteye/api/version_checker.cc index 363deaf..f39c6d5 100644 --- a/src/mynteye/api/version_checker.cc +++ b/src/mynteye/api/version_checker.cc @@ -19,29 +19,120 @@ MYNTEYE_BEGIN_NAMESPACE typedef struct { - const std::string device_type; - const std::string sdk_version; - const std::string firmware_version; + const std::string device_type; + const std::string sdk_version; + const std::string firmware_version; + const std::string status; }firmware_version_match_table_unit; +const char* ERRO_DESCRIPTION_F = + "Please update the firmware at first"; +const char* ERRO_DESCRIPTION_S = + "Please update the SDK at first"; +const char* WARN_DESCRIPTION_F = + "We suggest that you should update the firmware"; +const char* WARN_DESCRIPTION_S = + "We suggest that you should update the SDK"; +const char* PASS_DESCRIPTION = "pass"; + /** firmware/sdk version matched table */ +/**----device type-----sdk version---firmware version-----pass tag-----*/ static const firmware_version_match_table_unit FSVM_TABLE[] ={ - {"MYNT-EYE-S1030", "2.3.1.0", "2.0.0"}, - {"MYNT-EYE-S1030", "2.3.1.0", "2.0.0"}, - {} +/** S1030 */ + {"MYNT-EYE-S1030", ">2.3.0", "2.3.0", PASS_DESCRIPTION}, + {"MYNT-EYE-S1030", ">2.3.0", "2.2.2", PASS_DESCRIPTION}, + {"MYNT-EYE-S1030", ">2.3.0", "2.2.0", WARN_DESCRIPTION_F}, + {"MYNT-EYE-S1030", ">2.3.0", "<2.2.0", ERRO_DESCRIPTION_F}, + {"MYNT-EYE-S1030", "<2.3.1", "<2.2.0", WARN_DESCRIPTION_S}, +/** S2100 */ + {"MYNT-EYE-S2100", ">0.0.0", "1.0", PASS_DESCRIPTION}, +/** S210A */ + {"MYNT-EYE-S210A", ">0.0.0", "1.0", PASS_DESCRIPTION}, }; +void getVersion(const std::string &str, char *version) { + std::string st1(""); + int j = 0; + for (size_t i = 0; i < str.size(); i++) { + if (str[i] == '.') { + version[j++] = atoi(st1.c_str()); + st1 = ""; + } else { + st1 += str[i]; + } + } + version[j++] = atoi(st1.c_str()); +} + +bool conditionMatch(const std::string& condition, const std::string& target) { + char version[4] = {0}; + char version_c[4] = {0}; + getVersion(target, version); + int tag_c = 0; + std::string condition_c; + if (condition[0] == '>') { + tag_c = 1; + condition_c = condition.substr(1); + } else if (condition[0] == '<') { + tag_c = -1; + condition_c = condition.substr(1); + } else { + tag_c = 0; + condition_c = condition; + } + getVersion(condition_c, version_c); + int tag_big = memcmp(version, version_c, 4); + if (tag_big * tag_c > 0 || (tag_big == 0 && tag_c == 0)) return true; + return false; +} + +enum STATUS_UNIT { + ST_PASS, + ST_ERRO_F, + ST_ERRO_S, + ST_NOT_PASS +}; + +STATUS_UNIT checkUnit(const std::string& sdkv, + const std::string& devn, + const std::string& firmv, + const firmware_version_match_table_unit& condition) { + if (condition.device_type == devn && + conditionMatch(condition.sdk_version, sdkv) && + conditionMatch(condition.firmware_version, firmv)) { + if (condition.status == ERRO_DESCRIPTION_F) { + return ST_ERRO_F; + } + if (condition.status == ERRO_DESCRIPTION_S) { + return ST_ERRO_S; + } + if (condition.status == WARN_DESCRIPTION_F || + condition.status == WARN_DESCRIPTION_S) { + LOG(WARNING) << condition.status; + } + return ST_PASS; + } + return ST_NOT_PASS; +} + bool checkFirmwareVersion(const std::shared_ptr api) { - LOG(INFO) << "SDK version: " << api->GetInfo(Info::SDK_VERSION); - LOG(INFO) << "Device name: " << api->GetInfo(Info::DEVICE_NAME); - LOG(INFO) << "Serial number: " << api->GetInfo(Info::SERIAL_NUMBER); - LOG(INFO) << "Firmware version: " << api->GetInfo(Info::FIRMWARE_VERSION); - LOG(INFO) << "Hardware version: " << api->GetInfo(Info::HARDWARE_VERSION); - LOG(INFO) << "Spec version: " << api->GetInfo(Info::SPEC_VERSION); - LOG(INFO) << "Lens type: " << api->GetInfo(Info::LENS_TYPE); - LOG(INFO) << "IMU type: " << api->GetInfo(Info::IMU_TYPE); - LOG(INFO) << "Nominal baseline: " << api->GetInfo(Info::NOMINAL_BASELINE); - return true; + auto sdkv = api->GetInfo(Info::SDK_VERSION); + auto devn = api->GetInfo(Info::DEVICE_NAME); + auto firmv = api->GetInfo(Info::FIRMWARE_VERSION); + + for (size_t i =0; + i < sizeof(FSVM_TABLE)/sizeof(firmware_version_match_table_unit); + i++) { + auto res = checkUnit(sdkv, devn, firmv, FSVM_TABLE[i]); + if (res == ST_PASS) { + return true; + } else if (res == ST_ERRO_S || res == ST_ERRO_F) { + LOG(ERROR) << FSVM_TABLE[i].status; + return false; + } + } + LOG(ERROR) << ERRO_DESCRIPTION_S; + return false; } MYNTEYE_END_NAMESPACE