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
Diff: config/DeviceConfiguration.cpp
- Revision:
- 101:dbcd3bc51758
- Parent:
- 100:47ea098f8a47
- Child:
- 102:ef2827b2d00a
--- a/config/DeviceConfiguration.cpp Thu May 07 13:56:19 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include "DeviceConfiguration.h" - -DeviceConfiguration::DeviceConfiguration() -{ - for (size_t i = 0; i < DEVICE_CONFIGURATION_SIZE; ++i) { - _items[i].key = NULL; - _items[i].value = NULL; - } -} - -DeviceConfiguration::~DeviceConfiguration() -{ - clear(); -} - -bool DeviceConfiguration::read(const char *str) -{ - DeviceConfiguration::KeyValue items[DEVICE_CONFIGURATION_SIZE]; - size_t i; - for (i = 0; i < DEVICE_CONFIGURATION_SIZE; i++) { - items[i].key = NULL; - items[i].value = NULL; - } - - const char *ptr = str; - const char *ptr2, *ptr3; - int len1, len2; - i = 0; - while (*ptr && i<DEVICE_CONFIGURATION_SIZE) { - if (((ptr2 = strchr(ptr, '=')) == NULL) || - ((ptr3 = strchr(ptr2+1, ';')) == NULL)) - goto failure; - - len1 = ptr2-ptr; - len2 = ptr3-ptr2 - 1; - - if ((memchr(ptr, ';', len1) != NULL) || - (memchr(ptr2+1, '=', len2) != NULL)) - goto failure; - - for (size_t j = 0; j < DEVICE_CONFIGURATION_SIZE; j++) { - if ((items[j].key != NULL) && (strlen(items[j].key) == len1) && (strncmp(items[j].key, ptr, len1) == 0)) - goto failure; - } - - if ((items[i].key = (char*)malloc(len1+1)) == NULL) - goto failure; - if ((items[i].value = (char*)malloc(len2+1)) == NULL) { - free(items[i].key); - items[i].key = NULL; - goto failure; - } - - strncpy(items[i].key, ptr, len1); - strncpy(items[i].value, ptr2+1, len2); - items[i].key[len1] = '\0'; - items[i].value[len2] = '\0'; - - i++; - ptr = ptr3+1; - } - - if (*ptr != '\0') - goto failure; - - clear(); - memcpy(_items, items, sizeof(DeviceConfiguration::KeyValue)*DEVICE_CONFIGURATION_SIZE); - return true; - -failure: - for (i = 0; i < DEVICE_CONFIGURATION_SIZE; i++) { - if (items[i].key != NULL) { - free(items[i].key); - free(items[i].value); - } - } - return false; -} - -bool DeviceConfiguration::write(char *buf, size_t len) -{ - char *ptr= buf; - int ln; - - for (size_t i = 0; i < DEVICE_CONFIGURATION_SIZE; i++) { - if (_items[i].key) { - snprintf(ptr, len, "%s=%s;%n", _items[i].key, _items[i].value, &ln); - ptr += ln; - len -= ln; - } - } - return true; -} - -bool DeviceConfiguration::set(const char *key, const char *value) -{ - KeyValue *item = search(key); - - if (item == NULL) { - for (size_t i = 0; (i < DEVICE_CONFIGURATION_SIZE) && (item == NULL); i++) { - if (_items[i].key == NULL) - item = &_items[i]; - } - } - - if (item == NULL) - return false; - - if ((item->key = (char*)malloc(strlen(key)+1)) == NULL) - return false; - if ((item->value = (char*)malloc(strlen(value)+1)) == NULL) { - free(item->key); - item->key = NULL; - return false; - } - - strcpy(item->key, key); - strcpy(item->value, value); - return true; -} - -const char * DeviceConfiguration::get(const char *key) -{ - KeyValue *item = search(key); - - if (item) - return item->value; - else - return NULL; -} - -bool DeviceConfiguration::unset(const char *key) -{ - KeyValue *item = search(key); - - if (item) { - free(item->key); - free(item->value); - item->key = NULL; - item->value = NULL; - return true; - } else - return false; -} - -bool DeviceConfiguration::has(const char *key) -{ - return (search(key) != NULL); -} - -void DeviceConfiguration::clear() -{ - for (size_t i = 0; i < DEVICE_CONFIGURATION_SIZE; i++) { - if (_items[i].key) { - free(_items[i].key); - free(_items[i].value); - } - } -} - -DeviceConfiguration::KeyValue * DeviceConfiguration::search(const char *key) -{ - for (size_t i = 0; i < DEVICE_CONFIGURATION_SIZE; i++) { - if (strcmp(key, _items[i].key) == 0) - return &_items[i]; - } - return NULL; -}