Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

io/DeviceMemory.cpp

Committer:
xinlei
Date:
2015-05-08
Revision:
100:dbcd3bc51758
Parent:
98:e369fc75c000
Child:
102:ede6611e064e

File content as of revision 100:dbcd3bc51758:

#include <stdio.h>
#include <string.h>
#include "MDM.h"

#define CREDENTIALS_FILE "001_CREDENTIALS"
#define CONFIGURATION_FILE "002_CONFIGURATION"
extern MDMSerial *pMdm;

bool loadCredential(char *username, char *password, size_t len)
{
    char buffer[len*2+3];
    int res = pMdm->readFile(CREDENTIALS_FILE, buffer, sizeof(buffer));
    if (res < 0) {
        printf("Load credentials.\n");
        return false;
    } else {
        buffer[res] = 0;
        int l = 0;
        sscanf(buffer, "%s\n%s\n%n", username, password, &l);
        printf("%d:%d\nload: %s\n", res, l, buffer);
        return res==l;
    }
}

bool delCredential()
{
    return pMdm->delFile(CREDENTIALS_FILE);
}

bool saveCredential(char *username, char *password, size_t len)
{
    char buffer[len*2+3];
    int res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password);
    printf("save: %s\n", buffer);
    delCredential();
    return pMdm->writeFile(CREDENTIALS_FILE, buffer, res)==res;
}

int loadConfigFile(char *cfg, size_t len)
{
    int res = pMdm->readFile(CONFIGURATION_FILE, cfg, len);
    if (res >= 0)
        cfg[res] = 0;
    return res;
}

bool delConfigFile()
{
    return pMdm->delFile(CONFIGURATION_FILE);
}

bool saveConfigFile(char *cfg, size_t len)
{
    delConfigFile();
    return pMdm->writeFile(CONFIGURATION_FILE, cfg, len)==len;
}