Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
vwochnik
Date:
Sun Nov 30 19:34:49 2014 +0000
Revision:
67:c360a2b2c948
Child:
68:0dc778a16d0d
refactor credentials persistence, add factory reset upon fire press button

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vwochnik 67:c360a2b2c948 1 #include "DeviceMemory.h"
vwochnik 67:c360a2b2c948 2 #include <stdlib.h>
vwochnik 67:c360a2b2c948 3 #include <stdio.h>
vwochnik 67:c360a2b2c948 4 #include <string.h>
vwochnik 67:c360a2b2c948 5
vwochnik 67:c360a2b2c948 6 #define PLATFORM_CREDENTIALS_FILE "001_CREDENTIALS"
vwochnik 67:c360a2b2c948 7
vwochnik 67:c360a2b2c948 8 DeviceMemory::DeviceMemory(MDMSerial& mdm) :
vwochnik 67:c360a2b2c948 9 _mdm(mdm)
vwochnik 67:c360a2b2c948 10 {
vwochnik 67:c360a2b2c948 11 }
vwochnik 67:c360a2b2c948 12
vwochnik 67:c360a2b2c948 13 bool DeviceMemory::loadPlatformCredentials(char *username, char *password, size_t len)
vwochnik 67:c360a2b2c948 14 {
vwochnik 67:c360a2b2c948 15 char buffer[len*2+3]; int res, len2;
vwochnik 67:c360a2b2c948 16
vwochnik 67:c360a2b2c948 17 if ((res = _mdm.readFile(PLATFORM_CREDENTIALS_FILE, buffer, sizeof(buffer))) < 0)
vwochnik 67:c360a2b2c948 18 return false;
vwochnik 67:c360a2b2c948 19
vwochnik 67:c360a2b2c948 20 buffer[(size_t)res] = '\0';
vwochnik 67:c360a2b2c948 21 sscanf(buffer, "%s\n%s\n%n", username, password, &len2);
vwochnik 67:c360a2b2c948 22 return res == len2;
vwochnik 67:c360a2b2c948 23 }
vwochnik 67:c360a2b2c948 24
vwochnik 67:c360a2b2c948 25 bool DeviceMemory::savePlatformCredentials(char *username, char *password, size_t len)
vwochnik 67:c360a2b2c948 26 {
vwochnik 67:c360a2b2c948 27 char buffer[len*2+3]; int res;
vwochnik 67:c360a2b2c948 28
vwochnik 67:c360a2b2c948 29 res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password);
vwochnik 67:c360a2b2c948 30 if ((res < 0) || (res >= sizeof(buffer)))
vwochnik 67:c360a2b2c948 31 return false;
vwochnik 67:c360a2b2c948 32
vwochnik 67:c360a2b2c948 33 return res == _mdm.writeFile(PLATFORM_CREDENTIALS_FILE, buffer, res);
vwochnik 67:c360a2b2c948 34 }
vwochnik 67:c360a2b2c948 35
vwochnik 67:c360a2b2c948 36 bool DeviceMemory::resetPlatformCredentials()
vwochnik 67:c360a2b2c948 37 {
vwochnik 67:c360a2b2c948 38 return _mdm.delFile(PLATFORM_CREDENTIALS_FILE);
vwochnik 67:c360a2b2c948 39 }