From 95e00f919b5019d2f927e7dc34722683d5b5146c Mon Sep 17 00:00:00 2001 From: pandx Date: Sun, 17 Nov 2024 16:32:53 +0800 Subject: [PATCH] add protection for upgrade file. --- dbaccess/sql_db.cpp | 10 +++++++--- scheduler/schedule.cpp | 26 ++++++++++++++++---------- scheduler/schedule.hpp | 6 ++++-- scheduler/upgrade_cfg.cpp | 10 +++++++++- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/dbaccess/sql_db.cpp b/dbaccess/sql_db.cpp index 15bb1ee..fb05c5c 100644 --- a/dbaccess/sql_db.cpp +++ b/dbaccess/sql_db.cpp @@ -1351,6 +1351,12 @@ int SqliteDB::CalculateData() { int planCount = 0, planCountStatistic = 0, StatisticCountTotal = 0, SixCountTotal = 0, TimingCountTotal = 0, CountWaveXTotal = 0, waveInterVal = 0, featureInterVal = 0; int CountWaveYTotal = 0, CountWaveZTotal = 0; for (size_t i = 0; i < vecRet.size(); i++) { + featureInterVal = atoi(vecRet[i][3].c_str()); + waveInterVal = atoi(vecRet[i][4].c_str()); + if (featureInterVal == 0 || waveInterVal == 0) { + zlog_error(zct, "featureInterVal:%d or waveInterVal:%d is 0", featureInterVal, waveInterVal); + continue; + } planCount = 1440 / atoi(vecRet[i][4].c_str()); planCountStatistic = 1440 / atoi(vecRet[i][3].c_str()); int rate = readIntValue("config", "waveRate", (char *)GlobalConfig::Config_G.c_str()); @@ -1472,9 +1478,7 @@ int SqliteDB::CalculateData() { } zlog_info(zct, "3 update ZigbeePower "); } - } - featureInterVal = atoi(vecRet[i][3].c_str()); - waveInterVal = atoi(vecRet[i][4].c_str()); + } } zlog_info(zct, "Node Count = %d , featureInterVal = %d , waveInterVal = %d", vecRet.size(), featureInterVal, waveInterVal); zlog_info(zct, "plan Statistic Count = %d , 06 Count = %d , Timing Count = %d , CountWaveX = %d ", planCountStatistic * vecRet.size(), planCount * vecRet.size(), planCount * vecRet.size(), planCount * vecRet.size()); diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index aa115b4..0f7b78e 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -24,7 +24,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { } current_ts_ = GetLocalTs(); - CleanIdleOccupiedSet(current_ts_); + CleanIdleOccupiedSet(); nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_; current_wave_start_ts_ = nth_wave_start_slice_ * wave_form_send_interval_ + start_timestamp_; @@ -167,9 +167,9 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { if (slice_sensor_id_[i+forward_wave_slice_num] == 0) { // 判断此空闲位置是否被占用 long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + i * seconds_per_wave_slice_; - if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) { + if (!IdleSliceOccupied(i+forward_wave_slice_num)) { available_ts = current_wave_slice_ts; - free_slice_ocuppied_.insert(available_ts); + free_slice_ocuppied_.push_back(i+forward_wave_slice_num); zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to upgrade, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); break; } @@ -641,12 +641,18 @@ void SensorScheduler::ClearScheduleCfg(int short_addr) { } -void SensorScheduler::CleanIdleOccupiedSet(long ts) { - if (free_slice_ocuppied_.size() > 5) { - for (auto it = free_slice_ocuppied_.begin(); it != free_slice_ocuppied_.end();) { - if ((*it) < ts) { - it = free_slice_ocuppied_.erase(it); - } else ++it; +void SensorScheduler::CleanIdleOccupiedSet() { + int min_num = 0; + while (free_slice_ocuppied_.size() > free_slice_ && free_slice_ > min_num) { + free_slice_ocuppied_.pop_front(); + } +} + +bool SensorScheduler::IdleSliceOccupied(int slice_id) { + for (auto item : free_slice_ocuppied_) { + if (slice_id == item) { + return true; } } -} \ No newline at end of file + return false; +} diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index f3b72ad..e9939ad 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -101,7 +102,8 @@ public: private: void UpdateUpgradeInfo(int id); - void CleanIdleOccupiedSet(long ts); + void CleanIdleOccupiedSet(); + bool IdleSliceOccupied(int slice_id); // 索引号 // user config int eigen_value_send_interval_; @@ -125,7 +127,7 @@ private: std::map short_addr_map_; // base_relation.json // 空闲时间戳被占用 - std::unordered_set free_slice_ocuppied_; + std::list free_slice_ocuppied_; // sensor config update std::unordered_set update_; diff --git a/scheduler/upgrade_cfg.cpp b/scheduler/upgrade_cfg.cpp index 09eb1c6..00f137a 100644 --- a/scheduler/upgrade_cfg.cpp +++ b/scheduler/upgrade_cfg.cpp @@ -7,12 +7,20 @@ extern zlog_category_t *zct; extern zlog_category_t *zbt; int UpgradeCfg::ReadCfg(std::map &upgrade) { - std::ifstream upgrade_file(UPGRADE_CONFIG); + std::ifstream upgrade_file(UPGRADE_CONFIG); if (!upgrade_file.good()) { zlog_info(zbt, "[UpgradeCfg] no such file"); return 0; } + upgrade_file.seekg(0, std::ios::end); + std::streampos file_size = upgrade_file.tellg(); + if (file_size > 256000) { + zlog_error(zct, "upgrade file exception, will remove it"); + ClearCfg(); + return 1; + } + upgrade_file.seekg(0, std::ios::beg); Json::Reader reader; Json::Value root; if (!reader.parse(upgrade_file, root, false)) {