Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
io/DeviceMemory.cpp@93:61d44636f020, 2015-04-20 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 20 15:04:23 2015 +0000
- Revision:
- 93:61d44636f020
- Parent:
- 70:f489ca11f254
- Child:
- 96:ea056f6be2e8
Integration of new device push code base.
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 | { |
xinlei | 93:61d44636f020 | 16 | char buffer[len*2+3]; |
xinlei | 93:61d44636f020 | 17 | int res = _mdm.readFile(PLATFORM_CREDENTIALS_FILE, buffer, sizeof(buffer)); |
xinlei | 93:61d44636f020 | 18 | if (res < 0) |
vwochnik | 67:c360a2b2c948 | 19 | return false; |
vwochnik | 69:237c01eb36c2 | 20 | |
vwochnik | 67:c360a2b2c948 | 21 | buffer[(size_t)res] = '\0'; |
xinlei | 93:61d44636f020 | 22 | int len2=0; |
vwochnik | 67:c360a2b2c948 | 23 | sscanf(buffer, "%s\n%s\n%n", username, password, &len2); |
vwochnik | 67:c360a2b2c948 | 24 | return res == len2; |
vwochnik | 67:c360a2b2c948 | 25 | } |
vwochnik | 67:c360a2b2c948 | 26 | |
vwochnik | 67:c360a2b2c948 | 27 | bool DeviceMemory::savePlatformCredentials(char *username, char *password, size_t len) |
vwochnik | 67:c360a2b2c948 | 28 | { |
xinlei | 93:61d44636f020 | 29 | char buffer[len*2+3]; |
xinlei | 93:61d44636f020 | 30 | int res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password); |
xinlei | 93:61d44636f020 | 31 | if (res<0 || res>=sizeof(buffer)) |
vwochnik | 67:c360a2b2c948 | 32 | return false; |
vwochnik | 67:c360a2b2c948 | 33 | |
vwochnik | 70:f489ca11f254 | 34 | return ((resetPlatformCredentials()) && (res == _mdm.writeFile(PLATFORM_CREDENTIALS_FILE, buffer, res))); |
vwochnik | 67:c360a2b2c948 | 35 | } |
vwochnik | 67:c360a2b2c948 | 36 | |
vwochnik | 67:c360a2b2c948 | 37 | bool DeviceMemory::resetPlatformCredentials() |
vwochnik | 67:c360a2b2c948 | 38 | { |
vwochnik | 67:c360a2b2c948 | 39 | return _mdm.delFile(PLATFORM_CREDENTIALS_FILE); |
vwochnik | 67:c360a2b2c948 | 40 | } |
vwochnik | 68:0dc778a16d0d | 41 | |
vwochnik | 68:0dc778a16d0d | 42 | |
vwochnik | 68:0dc778a16d0d | 43 | bool DeviceMemory::loadConfiguration(char *cfg, size_t len) |
vwochnik | 68:0dc778a16d0d | 44 | { |
xinlei | 93:61d44636f020 | 45 | int res = _mdm.readFile(CONFIGURATION_FILE, cfg, len); |
xinlei | 93:61d44636f020 | 46 | if (res < 0) |
vwochnik | 68:0dc778a16d0d | 47 | return false; |
vwochnik | 68:0dc778a16d0d | 48 | |
vwochnik | 68:0dc778a16d0d | 49 | cfg[(size_t)res] = '\0'; |
vwochnik | 68:0dc778a16d0d | 50 | return true; |
vwochnik | 68:0dc778a16d0d | 51 | } |
vwochnik | 68:0dc778a16d0d | 52 | |
vwochnik | 68:0dc778a16d0d | 53 | bool DeviceMemory::saveConfiguration(char *cfg) |
vwochnik | 68:0dc778a16d0d | 54 | { |
xinlei | 93:61d44636f020 | 55 | size_t len = strlen(cfg); |
vwochnik | 70:f489ca11f254 | 56 | return ((resetConfiguration()) && (_mdm.writeFile(CONFIGURATION_FILE, cfg, len) == len)); |
vwochnik | 68:0dc778a16d0d | 57 | } |
vwochnik | 68:0dc778a16d0d | 58 | |
vwochnik | 68:0dc778a16d0d | 59 | bool DeviceMemory::resetConfiguration() |
vwochnik | 68:0dc778a16d0d | 60 | { |
vwochnik | 68:0dc778a16d0d | 61 | return _mdm.delFile(CONFIGURATION_FILE); |
vwochnik | 68:0dc778a16d0d | 62 | } |