Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ConfigSync.cpp Source File

ConfigSync.cpp

00001 #include <stdio.h>
00002 #include "Storage.h"
00003 #include "ConfigSync.h"
00004 #include "SmartRestConf.h"
00005 #include "logging.h"
00006 
00007 // default connectivity checking interval [minutes].
00008 #define INTERVAL_KEY "interval"
00009 #define DEFAULT_INTERVAL "20"
00010 
00011 static bool validateConfiguration(const Dict& d)
00012 {
00013         const Dict::Item *p = d.get(INTERVAL_KEY);
00014         if (p) {
00015                 int v = 0, n = 0;
00016                 sscanf(p->value, "%d%n", &v, &n);
00017                 if (v > 0 && n == strlen(p->value))
00018                         return true;
00019                 else
00020                         return false;
00021         } else
00022                 return false;
00023 }
00024 
00025 int ConfigSync::read(char *buf, size_t maxLen, char *status, size_t num)
00026 {
00027         static const char *fmt = "130,%ld,\"%s\",%.*s\r\n";
00028         int l = 0;
00029         if (changed) {
00030                 changed = false;
00031                 char s[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*dict.size()+1];
00032                 dict.dump(s);
00033                 const char *p = dict.get("interval")->value;
00034                 l = snprintf(buf, maxLen, fmt, deviceID, s, MAX_VALUE_LEN, p);
00035                 snprintf(status, num, "%s", "Sync Config");
00036         }
00037         return l;
00038 }
00039 
00040 bool ConfigSync::updateConfiguration(const char *buf)
00041 {
00042         // compromise for the platform, which requires a conf report despite
00043         // the conf is updated or NOT. More appropriately would be setting
00044         // `changed=true` inside the `if` block, when the conf is actually updated.
00045         changed = true; 
00046         bool b = cp.parse(buf);
00047         if (b && validateConfiguration(cp.dict)) {
00048                 dict = cp.dict;
00049                 saveConfiguration();
00050                 return true;
00051         } else {
00052                 return false;
00053         }
00054 }
00055 
00056 void ConfigSync::resetConfiguration()
00057 {
00058         dict.clear();
00059         dict.set(INTERVAL_KEY, DEFAULT_INTERVAL);
00060         changed = true;
00061 }
00062 
00063 void ConfigSync::loadConfiguration()
00064 {
00065         char buf[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*MAX_ITEM_SIZE+1];
00066         resetConfiguration();
00067         int l = loadConfigFile(buf, sizeof(buf));
00068         if (l > 0)
00069                 updateConfiguration(buf);
00070         else
00071                 saveConfiguration();
00072 }
00073 
00074 void ConfigSync::saveConfiguration() const
00075 {
00076         char s[(MAX_KEY_LEN+MAX_VALUE_LEN+3)*dict.size()+1];
00077         size_t l = dict.dump(s);
00078         if (l) {
00079                 if (!saveConfigFile(s, l))
00080                         aError("Save config.\n");
00081         }
00082 }