From ca851add76af468b74e4bc79c37f64abc7a523d6 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 21 Apr 2015 23:19:45 -0400 Subject: [PATCH] Improved config code / xml format --- src/AppConfig.cpp | 95 +++++++++++++++++++++++++++++-------------- src/AppConfig.h | 27 ++++++++++-- src/CubicSDR.cpp | 8 ++-- src/sdr/SDRThread.cpp | 2 +- 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/src/AppConfig.cpp b/src/AppConfig.cpp index 41e92a5..88df676 100644 --- a/src/AppConfig.cpp +++ b/src/AppConfig.cpp @@ -1,7 +1,53 @@ #include "AppConfig.h" -std::string AppConfig::getConfigDir() { +DeviceConfig::DeviceConfig() : ppm(0), deviceId("") { +} + +DeviceConfig::DeviceConfig(std::string deviceId) : ppm(0) { + this->deviceId = deviceId; +} + +void DeviceConfig::setPPM(int ppm) { + this->ppm = ppm; +} + +int DeviceConfig::getPPM() { + return ppm; +} + +void DeviceConfig::setDeviceId(std::string deviceId) { + this->deviceId = deviceId; +} + +std::string DeviceConfig::getDeviceId() { + return deviceId; +} + +void DeviceConfig::save(DataNode *node) { + node->newChild("id")->element()->set(deviceId); + DataNode *ppm_node = node->newChild("ppm"); + ppm_node->element()->set((int)ppm); +} + +void DeviceConfig::load(DataNode *node) { + if (node->hasAnother("ppm")) { + DataNode *ppm_node = node->getNext("ppm"); + int ppmValue = 0; + ppm_node->element()->get(ppmValue); + setPPM(ppmValue); + std::cout << "Loaded PPM for device '" << deviceId << "' at " << ppmValue << "ppm" << std::endl; + } +} + + +DeviceConfig *AppConfig::getDevice(std::string deviceId) { + DeviceConfig *conf = &deviceConfig[deviceId]; + conf->setDeviceId(deviceId); + return conf; +} + +std::string AppConfig::getConfigDir() { std::string dataDir = wxStandardPaths::Get().GetUserDataDir().ToStdString(); bool mkStatus = false; @@ -19,33 +65,21 @@ std::string AppConfig::getConfigDir() { return dataDir; } -void AppConfig::setPPM(std::string deviceId, int ppm) { - device_ppm[deviceId] = ppm; -} - -int AppConfig::getPPM(std::string deviceId) { - if (device_ppm.find(deviceId) == device_ppm.end()) { - return 0; - } - return device_ppm[deviceId]; -} - bool AppConfig::save() { DataTree cfg; cfg.rootNode()->setName("cubicsdr_config"); - DataNode *ppm_data = cfg.rootNode()->newChild("ppm"); + DataNode *devices_node = cfg.rootNode()->newChild("devices"); - std::map::iterator device_ppm_i; - for (device_ppm_i = device_ppm.begin(); device_ppm_i != device_ppm.end(); device_ppm_i++) { - DataNode *ppm_ent = ppm_data->newChild("device"); - ppm_ent->newChild("id")->element()->set(device_ppm_i->first); - ppm_ent->newChild("value")->element()->set((int)device_ppm_i->second); + std::map::iterator device_config_i; + for (device_config_i = deviceConfig.begin(); device_config_i != deviceConfig.end(); device_config_i++) { + DataNode *device_node = devices_node->newChild("device"); + device_config_i->second.save(device_node); } std::string cfgFileDir = getConfigDir(); - wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml"); + wxFileName cfgFile = wxFileName(cfgFileDir, "config.xml"); std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString(); if (!cfg.SaveToFileXML(cfgFileName)) { @@ -60,7 +94,7 @@ bool AppConfig::load() { DataTree cfg; std::string cfgFileDir = getConfigDir(); - wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml"); + wxFileName cfgFile = wxFileName(cfgFileDir, "config.xml"); std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString(); if (!cfgFile.Exists()) { @@ -68,25 +102,24 @@ bool AppConfig::load() { } if (cfgFile.IsFileReadable()) { + std::cout << "Loading:: configuration file '" << cfgFileName << "'" << std::endl; + cfg.LoadFromFileXML(cfgFileName); } else { std::cout << "Error loading:: configuration file '" << cfgFileName << "' is not readable!" << std::endl; return false; } - if (cfg.rootNode()->hasAnother("ppm")) { - device_ppm.clear(); + if (cfg.rootNode()->hasAnother("devices")) { + DataNode *devices_node = cfg.rootNode()->getNext("devices"); - DataNode *ppm_data = cfg.rootNode()->getNext("ppm"); + while (devices_node->hasAnother("device")) { + DataNode *device_node = devices_node->getNext("device"); + if (device_node->hasAnother("id")) { + std::string deviceId; + device_node->getNext("id")->element()->get(deviceId); - while (ppm_data->hasAnother("device")) { - DataNode *ppm_ent = ppm_data->getNext("device"); - - if (ppm_ent->hasAnother("id") && ppm_ent->hasAnother("value")) { - std::string deviceId(*ppm_ent->getNext("id")); - int ppmValue = *ppm_ent->getNext("value"); - setPPM(deviceId, ppmValue); - std::cout << "Loaded PPM for device '" << deviceId << "' at " << ppmValue << "ppm" << std::endl; + getDevice(deviceId)->load(device_node); } } } diff --git a/src/AppConfig.h b/src/AppConfig.h index d613e0e..13e5ce1 100644 --- a/src/AppConfig.h +++ b/src/AppConfig.h @@ -6,16 +6,35 @@ #include "DataTree.h" + +class DeviceConfig { +public: + DeviceConfig(); + DeviceConfig(std::string deviceId); + + void setPPM(int ppm); + int getPPM(); + + void setDeviceId(std::string deviceId); + std::string getDeviceId(); + + void save(DataNode *node); + void load(DataNode *node); + +private: + std::string deviceId; + int ppm; +}; + class AppConfig { public: - std::string getConfigDir(); + DeviceConfig *getDevice(std::string deviceId); - void setPPM(std::string deviceId, int ppm); - int getPPM(std::string deviceId); bool save(); bool load(); bool reset(); + private: - std::map device_ppm; + std::map deviceConfig; }; diff --git a/src/CubicSDR.cpp b/src/CubicSDR.cpp index daac11a..7966ae0 100644 --- a/src/CubicSDR.cpp +++ b/src/CubicSDR.cpp @@ -38,7 +38,7 @@ bool CubicSDR::OnInit() { return false; } - wxApp::SetAppName("cubicsdr"); + wxApp::SetAppName("CubicSDR"); config.load(); @@ -226,7 +226,7 @@ void CubicSDR::setDevice(int deviceId) { SDRDeviceInfo *dev = (*getDevices())[deviceId]; SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM); - ppm = config.getPPM(dev->getDeviceId()); + ppm = config.getDevice(dev->getDeviceId())->getPPM(); command_ppm.llong_value = ppm; threadCmdQueueSDR->push(command_ppm); } @@ -251,7 +251,7 @@ void CubicSDR::setPPM(int ppm_in) { SDRDeviceInfo *dev = (*getDevices())[getDevice()]; - config.setPPM(dev->getDeviceId(), ppm_in); + config.getDevice(dev->getDeviceId())->setPPM(ppm_in); config.save(); } @@ -262,7 +262,7 @@ int CubicSDR::getPPM() { SDRDeviceInfo *dev = (*getDevices())[getDevice()]; SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM); - ppm = config.getPPM(dev->getDeviceId()); + ppm = config.getDevice(dev->getDeviceId())->getPPM(); return ppm; } diff --git a/src/sdr/SDRThread.cpp b/src/sdr/SDRThread.cpp index 22ae5e7..0df57a0 100644 --- a/src/sdr/SDRThread.cpp +++ b/src/sdr/SDRThread.cpp @@ -137,7 +137,7 @@ void SDRThread::threadMain() { signed char buf[BUF_SIZE]; long long frequency = DEFAULT_FREQ; - int ppm = wxGetApp().getConfig()->getPPM(devs[deviceId]->getDeviceId()); + int ppm = wxGetApp().getConfig()->getDevice(devs[deviceId]->getDeviceId())->getPPM(); rtlsdr_open(&dev, deviceId); rtlsdr_set_sample_rate(dev, sampleRate);