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
config/ConfigurationSynchronization.cpp
- Committer:
- xinlei
- Date:
- 2015-04-27
- Revision:
- 98:9f2de96941c4
- Parent:
- 96:5dfdc8568e9f
File content as of revision 98:9f2de96941c4:
#include <stdio.h>
#include "ConfigurationSynchronization.h"
#include "DeviceMemory.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "logging.h"
ConfigurationSynchronization::ConfigurationSynchronization(
AbstractSmartRest& client, SmartRestTemplate& tpl,
long& deviceId) :
_init (false),
_changed (false),
_tpl(tpl),
_client(client),
_deviceId(deviceId),
_deviceConfiguration(),
_configurationProperties(_deviceConfiguration)
{
}
bool ConfigurationSynchronization::init()
{
if (_init)
return false;
// Update Configuration
// Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY>
if (!_tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING NUMBER,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"},\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n"))
return false;
_init = true;
return true;
}
bool ConfigurationSynchronization::integrate()
{
if ((!loadConfiguration()) || (!_configurationProperties.validateProperties())) {
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)) {
aError("Read device config.\n");
return false;
}
if (!_configurationProperties.validateProperties()) {
loadConfiguration();
return false;
}
_changed = true;
return true;
}
bool ConfigurationSynchronization::updateDeviceObject()
{
char buf[256];
if (!_deviceConfiguration.write(buf, sizeof(buf))) {
aError("Unable to write config.\n");
return false;
}
ComposedRecord record;
ParsedRecord received;
IntegerValue msgId(130);
IntegerValue deviceId(_deviceId);
CharValue config(buf);
IntegerValue responsibility(_configurationProperties.readInterval());
if (!record.add(msgId) || !record.add(deviceId) || !record.add(config) || !record.add(responsibility)) {
return false;
}
if (_client.send(record) != SMARTREST_SUCCESS) {
_client.stop();
return false;
}
if (_client.receive(received) != SMARTREST_SUCCESS) {
_client.stop();
return false;
}
_client.stop();
if (received.values() != 3) {
aError("Invalid config, %d (3) values received.\n", received.values());
return false;
} else if (received.value(0).integerValue() != 201) {
aError("updateDeviceObject: Message ID %ld .\n", received.value(0).integerValue());
return false;
}
return true;
}
bool ConfigurationSynchronization::loadConfiguration()
{
char buf[256];
if (!::loadConfiguration(buf, sizeof(buf))) {
aError("Load device config.\n");
return false;
}
if (!_deviceConfiguration.read(buf)) {
aError("Read device config.\n");
return false;
}
return true;
}
bool ConfigurationSynchronization::saveConfiguration()
{
char buf[256];
if (!_deviceConfiguration.write(buf, sizeof(buf))) {
aError("Write device config.\n");
return false;
}
if (!::saveConfiguration(buf)) {
aError("Save device config.\n");
return false;
}
return true;
}

Cumulocity