
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
config/ConfigSync.cpp
- Committer:
- xinlei
- Date:
- 2015-05-08
- Revision:
- 102:ede6611e064e
- Parent:
- 100:dbcd3bc51758
- Child:
- 104:fd3571349e5d
File content as of revision 102:ede6611e064e:
#include <stdio.h> #include "DeviceMemory.h" #include "ConfigSync.h" #include "SmartRestConf.h" #include "logging.h" // default connectivity checking interval [minutes]. #define INTERVAL_KEY "interval" #define DEFAULT_INTERVAL "20" bool validateConfiguration(Dict& d) { return d.get(INTERVAL_KEY); } size_t ConfigSync::read(char *buf, size_t maxLen, char *status, size_t num) { static const char *fmt = "130,%ld,%s,%.*s\r\n"; int l = 0; if (changed) { changed = false; char s[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*dict.size()+1]; dict.dump(s); const char *p = dict.get("interval")->value; l = snprintf(buf, maxLen, fmt, deviceID, s, MAX_VALUE_LEN, p); snprintf(status, num, "%s", "Sync Config"); } return l; } bool ConfigSync::updateConfiguration(const char *buf) { bool b = cp.parse(buf); if (b && validateConfiguration(cp.dict)) { dict = cp.dict; changed = true; saveConfiguration(); return true; } else { return false; } } void ConfigSync::resetConfiguration() { dict.clear(); dict.set(INTERVAL_KEY, DEFAULT_INTERVAL); aDebug("Reset conf\n"); changed = true; } void ConfigSync::loadConfiguration() { char buf[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*MAX_ITEM_SIZE+1]; int l = loadConfigFile(buf, sizeof(buf)); if (l > 0) { updateConfiguration(buf); } } void ConfigSync::saveConfiguration() const { char s[(MAX_KEY_LEN+MAX_VALUE_LEN+3)*dict.size()+1]; size_t l = dict.dump(s); if (l) { if (!saveConfigFile(s, l)) { aError("Save config.\n"); } } }