Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Aug 08 11:05:57 2016 +0000
Revision:
138:f8ab852e83e7
Parent:
136:a52821cdb108
Etisalat and Teleena APN.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 98:e369fc75c000 1 #include <stdio.h>
xinlei 104:fd3571349e5d 2 #include "Storage.h"
xinlei 100:dbcd3bc51758 3 #include "ConfigSync.h"
xinlei 100:dbcd3bc51758 4 #include "SmartRestConf.h"
xinlei 98:e369fc75c000 5 #include "logging.h"
xinlei 98:e369fc75c000 6
xinlei 100:dbcd3bc51758 7 // default connectivity checking interval [minutes].
xinlei 100:dbcd3bc51758 8 #define INTERVAL_KEY "interval"
xinlei 100:dbcd3bc51758 9 #define DEFAULT_INTERVAL "20"
xinlei 100:dbcd3bc51758 10
xinlei 110:ee9ae4a5e55c 11 static bool validateConfiguration(const Dict& d)
xinlei 98:e369fc75c000 12 {
xinlei 105:c61f0d62b625 13 const Dict::Item *p = d.get(INTERVAL_KEY);
xinlei 105:c61f0d62b625 14 if (p) {
xinlei 105:c61f0d62b625 15 int v = 0, n = 0;
xinlei 105:c61f0d62b625 16 sscanf(p->value, "%d%n", &v, &n);
xinlei 105:c61f0d62b625 17 if (v > 0 && n == strlen(p->value))
xinlei 105:c61f0d62b625 18 return true;
xinlei 105:c61f0d62b625 19 else
xinlei 105:c61f0d62b625 20 return false;
xinlei 105:c61f0d62b625 21 } else
xinlei 105:c61f0d62b625 22 return false;
xinlei 98:e369fc75c000 23 }
xinlei 98:e369fc75c000 24
xinlei 136:a52821cdb108 25 int ConfigSync::read(char *buf, size_t maxLen, char *status, size_t num)
xinlei 98:e369fc75c000 26 {
xinlei 105:c61f0d62b625 27 static const char *fmt = "130,%ld,\"%s\",%.*s\r\n";
xinlei 100:dbcd3bc51758 28 int l = 0;
xinlei 100:dbcd3bc51758 29 if (changed) {
xinlei 100:dbcd3bc51758 30 changed = false;
xinlei 102:ede6611e064e 31 char s[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*dict.size()+1];
xinlei 100:dbcd3bc51758 32 dict.dump(s);
xinlei 100:dbcd3bc51758 33 const char *p = dict.get("interval")->value;
xinlei 100:dbcd3bc51758 34 l = snprintf(buf, maxLen, fmt, deviceID, s, MAX_VALUE_LEN, p);
xinlei 102:ede6611e064e 35 snprintf(status, num, "%s", "Sync Config");
xinlei 100:dbcd3bc51758 36 }
xinlei 100:dbcd3bc51758 37 return l;
xinlei 98:e369fc75c000 38 }
xinlei 98:e369fc75c000 39
xinlei 100:dbcd3bc51758 40 bool ConfigSync::updateConfiguration(const char *buf)
xinlei 98:e369fc75c000 41 {
xinlei 116:5de54f09f754 42 // compromise for the platform, which requires a conf report despite
xinlei 116:5de54f09f754 43 // the conf is updated or NOT. More appropriately would be setting
xinlei 116:5de54f09f754 44 // `changed=true` inside the `if` block, when the conf is actually updated.
xinlei 116:5de54f09f754 45 changed = true;
xinlei 100:dbcd3bc51758 46 bool b = cp.parse(buf);
xinlei 100:dbcd3bc51758 47 if (b && validateConfiguration(cp.dict)) {
xinlei 100:dbcd3bc51758 48 dict = cp.dict;
xinlei 100:dbcd3bc51758 49 saveConfiguration();
xinlei 102:ede6611e064e 50 return true;
xinlei 102:ede6611e064e 51 } else {
xinlei 102:ede6611e064e 52 return false;
xinlei 100:dbcd3bc51758 53 }
xinlei 100:dbcd3bc51758 54 }
xinlei 100:dbcd3bc51758 55
xinlei 100:dbcd3bc51758 56 void ConfigSync::resetConfiguration()
xinlei 100:dbcd3bc51758 57 {
xinlei 100:dbcd3bc51758 58 dict.clear();
xinlei 100:dbcd3bc51758 59 dict.set(INTERVAL_KEY, DEFAULT_INTERVAL);
xinlei 100:dbcd3bc51758 60 changed = true;
xinlei 98:e369fc75c000 61 }
xinlei 98:e369fc75c000 62
xinlei 100:dbcd3bc51758 63 void ConfigSync::loadConfiguration()
xinlei 98:e369fc75c000 64 {
xinlei 102:ede6611e064e 65 char buf[(MAX_KEY_LEN+MAX_VALUE_LEN+4)*MAX_ITEM_SIZE+1];
xinlei 110:ee9ae4a5e55c 66 resetConfiguration();
xinlei 100:dbcd3bc51758 67 int l = loadConfigFile(buf, sizeof(buf));
xinlei 110:ee9ae4a5e55c 68 if (l > 0)
xinlei 100:dbcd3bc51758 69 updateConfiguration(buf);
xinlei 110:ee9ae4a5e55c 70 else
xinlei 110:ee9ae4a5e55c 71 saveConfiguration();
xinlei 98:e369fc75c000 72 }
xinlei 98:e369fc75c000 73
xinlei 100:dbcd3bc51758 74 void ConfigSync::saveConfiguration() const
xinlei 98:e369fc75c000 75 {
xinlei 102:ede6611e064e 76 char s[(MAX_KEY_LEN+MAX_VALUE_LEN+3)*dict.size()+1];
xinlei 100:dbcd3bc51758 77 size_t l = dict.dump(s);
xinlei 100:dbcd3bc51758 78 if (l) {
xinlei 110:ee9ae4a5e55c 79 if (!saveConfigFile(s, l))
xinlei 100:dbcd3bc51758 80 aError("Save config.\n");
xinlei 100:dbcd3bc51758 81 }
xinlei 100:dbcd3bc51758 82 }