Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Fri May 08 12:19:57 2015 +0000
Revision:
100:dbcd3bc51758
Parent:
98:e369fc75c000
Child:
102:ede6611e064e
v2.1rc3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vwochnik 67:c360a2b2c948 1 #include <stdio.h>
vwochnik 67:c360a2b2c948 2 #include <string.h>
xinlei 96:ea056f6be2e8 3 #include "MDM.h"
vwochnik 67:c360a2b2c948 4
xinlei 100:dbcd3bc51758 5 #define CREDENTIALS_FILE "001_CREDENTIALS"
vwochnik 68:0dc778a16d0d 6 #define CONFIGURATION_FILE "002_CONFIGURATION"
xinlei 96:ea056f6be2e8 7 extern MDMSerial *pMdm;
vwochnik 67:c360a2b2c948 8
xinlei 100:dbcd3bc51758 9 bool loadCredential(char *username, char *password, size_t len)
vwochnik 67:c360a2b2c948 10 {
xinlei 98:e369fc75c000 11 char buffer[len*2+3];
xinlei 100:dbcd3bc51758 12 int res = pMdm->readFile(CREDENTIALS_FILE, buffer, sizeof(buffer));
xinlei 100:dbcd3bc51758 13 if (res < 0) {
xinlei 100:dbcd3bc51758 14 printf("Load credentials.\n");
vwochnik 67:c360a2b2c948 15 return false;
xinlei 100:dbcd3bc51758 16 } else {
xinlei 98:e369fc75c000 17 buffer[res] = 0;
xinlei 100:dbcd3bc51758 18 int l = 0;
xinlei 100:dbcd3bc51758 19 sscanf(buffer, "%s\n%s\n%n", username, password, &l);
xinlei 100:dbcd3bc51758 20 printf("%d:%d\nload: %s\n", res, l, buffer);
xinlei 100:dbcd3bc51758 21 return res==l;
xinlei 98:e369fc75c000 22 }
vwochnik 67:c360a2b2c948 23 }
vwochnik 67:c360a2b2c948 24
xinlei 100:dbcd3bc51758 25 bool delCredential()
xinlei 97:9f2de96941c4 26 {
xinlei 100:dbcd3bc51758 27 return pMdm->delFile(CREDENTIALS_FILE);
xinlei 97:9f2de96941c4 28 }
xinlei 97:9f2de96941c4 29
xinlei 100:dbcd3bc51758 30 bool saveCredential(char *username, char *password, size_t len)
vwochnik 67:c360a2b2c948 31 {
xinlei 100:dbcd3bc51758 32 char buffer[len*2+3];
xinlei 93:61d44636f020 33 int res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password);
xinlei 100:dbcd3bc51758 34 printf("save: %s\n", buffer);
xinlei 100:dbcd3bc51758 35 delCredential();
xinlei 100:dbcd3bc51758 36 return pMdm->writeFile(CREDENTIALS_FILE, buffer, res)==res;
vwochnik 67:c360a2b2c948 37 }
vwochnik 68:0dc778a16d0d 38
xinlei 100:dbcd3bc51758 39 int loadConfigFile(char *cfg, size_t len)
vwochnik 68:0dc778a16d0d 40 {
xinlei 96:ea056f6be2e8 41 int res = pMdm->readFile(CONFIGURATION_FILE, cfg, len);
xinlei 100:dbcd3bc51758 42 if (res >= 0)
xinlei 98:e369fc75c000 43 cfg[res] = 0;
xinlei 100:dbcd3bc51758 44 return res;
vwochnik 68:0dc778a16d0d 45 }
vwochnik 68:0dc778a16d0d 46
xinlei 98:e369fc75c000 47 bool delConfigFile()
vwochnik 68:0dc778a16d0d 48 {
xinlei 96:ea056f6be2e8 49 return pMdm->delFile(CONFIGURATION_FILE);
vwochnik 68:0dc778a16d0d 50 }
xinlei 97:9f2de96941c4 51
xinlei 98:e369fc75c000 52 bool saveConfigFile(char *cfg, size_t len)
xinlei 97:9f2de96941c4 53 {
xinlei 98:e369fc75c000 54 delConfigFile();
xinlei 98:e369fc75c000 55 return pMdm->writeFile(CONFIGURATION_FILE, cfg, len)==len;
xinlei 97:9f2de96941c4 56 }