Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
io/DeviceMemory.cpp@69:237c01eb36c2, 2014-12-13 (annotated)
- Committer:
- vwochnik
- Date:
- Sat Dec 13 16:07:07 2014 +0000
- Revision:
- 69:237c01eb36c2
- Parent:
- 68:0dc778a16d0d
- Child:
- 70:f489ca11f254
delFile bug fixed
Who changed what in which revision?
User | Revision | Line number | New 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 | 68:0dc778a16d0d | 7 | #define CONFIGURATION_FILE "002_CONFIGURATION" |
vwochnik | 67:c360a2b2c948 | 8 | |
vwochnik | 67:c360a2b2c948 | 9 | DeviceMemory::DeviceMemory(MDMSerial& mdm) : |
vwochnik | 67:c360a2b2c948 | 10 | _mdm(mdm) |
vwochnik | 67:c360a2b2c948 | 11 | { |
vwochnik | 67:c360a2b2c948 | 12 | } |
vwochnik | 67:c360a2b2c948 | 13 | |
vwochnik | 67:c360a2b2c948 | 14 | bool DeviceMemory::loadPlatformCredentials(char *username, char *password, size_t len) |
vwochnik | 67:c360a2b2c948 | 15 | { |
vwochnik | 67:c360a2b2c948 | 16 | char buffer[len*2+3]; int res, len2; |
vwochnik | 67:c360a2b2c948 | 17 | |
vwochnik | 67:c360a2b2c948 | 18 | if ((res = _mdm.readFile(PLATFORM_CREDENTIALS_FILE, buffer, sizeof(buffer))) < 0) |
vwochnik | 67:c360a2b2c948 | 19 | return false; |
vwochnik | 69:237c01eb36c2 | 20 | |
vwochnik | 67:c360a2b2c948 | 21 | buffer[(size_t)res] = '\0'; |
vwochnik | 67:c360a2b2c948 | 22 | sscanf(buffer, "%s\n%s\n%n", username, password, &len2); |
vwochnik | 67:c360a2b2c948 | 23 | return res == len2; |
vwochnik | 67:c360a2b2c948 | 24 | } |
vwochnik | 67:c360a2b2c948 | 25 | |
vwochnik | 67:c360a2b2c948 | 26 | bool DeviceMemory::savePlatformCredentials(char *username, char *password, size_t len) |
vwochnik | 67:c360a2b2c948 | 27 | { |
vwochnik | 67:c360a2b2c948 | 28 | char buffer[len*2+3]; int res; |
vwochnik | 67:c360a2b2c948 | 29 | |
vwochnik | 67:c360a2b2c948 | 30 | res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password); |
vwochnik | 67:c360a2b2c948 | 31 | if ((res < 0) || (res >= sizeof(buffer))) |
vwochnik | 67:c360a2b2c948 | 32 | return false; |
vwochnik | 67:c360a2b2c948 | 33 | |
vwochnik | 69:237c01eb36c2 | 34 | resetPlatformCredentials(); |
vwochnik | 69:237c01eb36c2 | 35 | return (res == _mdm.writeFile(PLATFORM_CREDENTIALS_FILE, buffer, res)); |
vwochnik | 67:c360a2b2c948 | 36 | } |
vwochnik | 67:c360a2b2c948 | 37 | |
vwochnik | 67:c360a2b2c948 | 38 | bool DeviceMemory::resetPlatformCredentials() |
vwochnik | 67:c360a2b2c948 | 39 | { |
vwochnik | 67:c360a2b2c948 | 40 | return _mdm.delFile(PLATFORM_CREDENTIALS_FILE); |
vwochnik | 67:c360a2b2c948 | 41 | } |
vwochnik | 68:0dc778a16d0d | 42 | |
vwochnik | 68:0dc778a16d0d | 43 | |
vwochnik | 68:0dc778a16d0d | 44 | bool DeviceMemory::loadConfiguration(char *cfg, size_t len) |
vwochnik | 68:0dc778a16d0d | 45 | { |
vwochnik | 68:0dc778a16d0d | 46 | int res; |
vwochnik | 68:0dc778a16d0d | 47 | |
vwochnik | 68:0dc778a16d0d | 48 | if ((res = _mdm.readFile(CONFIGURATION_FILE, cfg, len)) < 0) |
vwochnik | 68:0dc778a16d0d | 49 | return false; |
vwochnik | 68:0dc778a16d0d | 50 | |
vwochnik | 68:0dc778a16d0d | 51 | cfg[(size_t)res] = '\0'; |
vwochnik | 68:0dc778a16d0d | 52 | return true; |
vwochnik | 68:0dc778a16d0d | 53 | } |
vwochnik | 68:0dc778a16d0d | 54 | |
vwochnik | 68:0dc778a16d0d | 55 | bool DeviceMemory::saveConfiguration(char *cfg) |
vwochnik | 68:0dc778a16d0d | 56 | { |
vwochnik | 68:0dc778a16d0d | 57 | size_t len; |
vwochnik | 68:0dc778a16d0d | 58 | |
vwochnik | 68:0dc778a16d0d | 59 | len = strlen(cfg); |
vwochnik | 69:237c01eb36c2 | 60 | |
vwochnik | 69:237c01eb36c2 | 61 | resetConfiguration(); |
vwochnik | 69:237c01eb36c2 | 62 | return (_mdm.writeFile(CONFIGURATION_FILE, cfg, len) == len); |
vwochnik | 68:0dc778a16d0d | 63 | } |
vwochnik | 68:0dc778a16d0d | 64 | |
vwochnik | 68:0dc778a16d0d | 65 | bool DeviceMemory::resetConfiguration() |
vwochnik | 68:0dc778a16d0d | 66 | { |
vwochnik | 68:0dc778a16d0d | 67 | return _mdm.delFile(CONFIGURATION_FILE); |
vwochnik | 68:0dc778a16d0d | 68 | } |