Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: config/ConfigurationSynchronization.cpp
- Revision:
- 68:0dc778a16d0d
- Child:
- 69:237c01eb36c2
diff -r c360a2b2c948 -r 0dc778a16d0d config/ConfigurationSynchronization.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/config/ConfigurationSynchronization.cpp Sat Dec 06 19:47:23 2014 +0000
@@ -0,0 +1,133 @@
+#include "ConfigurationSynchronization.h"
+#include <stdio.h>
+#include "ComposedRecord.h"
+#include "CharValue.h"
+#include "IntegerValue.h"
+
+ConfigurationSynchronization::ConfigurationSynchronization(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceMemory& deviceMemory, DeviceConfiguration& deviceConfiguration, ConfigurationProperties& configurationProperties) :
+ _client(client),
+ _tpl(tpl),
+ _deviceId(deviceId),
+ _deviceMemory(deviceMemory),
+ _deviceConfiguration(deviceConfiguration),
+ _configurationProperties(configurationProperties)
+{
+ _init = false;
+ _changed = false;
+}
+
+bool ConfigurationSynchronization::init()
+{
+ if (_init)
+ return false;
+
+ // Update Configuration
+ // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>
+ if (!_tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"}}\"\r\n"))
+ return false;
+
+ _init = true;
+ return true;
+}
+
+bool ConfigurationSynchronization::integrate()
+{
+ if (!loadConfiguration()) {
+ if ((!_configurationProperties.resetConfiguration()) || (!updateDeviceObject()) || (!saveConfiguration()))
+ return false;
+ } else {
+ if (!updateDeviceObject())
+ return false;
+ }
+
+ return true;
+}
+
+bool ConfigurationSynchronization::run()
+{
+ if (!_changed)
+ return true;
+
+ if ((!updateDeviceObject()) || (!saveConfiguration()))
+ return false;
+
+ _changed = false;
+ return true;
+}
+
+bool ConfigurationSynchronization::updateConfiguration(const char *cfg)
+{
+ if (!_deviceConfiguration.read(cfg))
+ return false;
+
+ _changed = true;
+ return true;
+}
+
+bool ConfigurationSynchronization::updateDeviceObject()
+{
+ char buf[256];
+
+ if (!_deviceConfiguration.write(buf, sizeof(buf)))
+ return false;
+
+ ComposedRecord record;
+ ParsedRecord received;
+
+ IntegerValue msgId(130);
+ IntegerValue deviceId(_deviceId);
+ CharValue config(buf);
+ if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(config)))
+ return false;
+
+ if (_client.send(record) != SMARTREST_SUCCESS) {
+ puts("Send failed.");
+ _client.stop();
+ return false;
+ }
+
+ if (_client.receive(received) != SMARTREST_SUCCESS) {
+ puts("Update failed.");
+ _client.stop();
+ return false;
+ }
+ _client.stop();
+
+ if (received.values() != 3) {
+ puts("Bad received data.");
+ return false;
+ }
+
+ if (received.value(0).integerValue() != 201) {
+ puts("Bad received data.");
+ return false;
+ }
+
+ return true;
+}
+
+bool ConfigurationSynchronization::loadConfiguration()
+{
+ char buf[256];
+
+ if (!_deviceMemory.loadConfiguration(buf, sizeof(buf)))
+ return false;
+
+ if (!_deviceConfiguration.read(buf))
+ return false;
+
+ return true;
+}
+
+bool ConfigurationSynchronization::saveConfiguration()
+{
+ char buf[256];
+
+ if (!_deviceConfiguration.write(buf, sizeof(buf)))
+ return false;
+
+ if (!_deviceMemory.saveConfiguration(buf))
+ return false;
+
+ return true;
+}

Cumulocity