Typical controller demo program based on Seeed Arch Max. Features: - Multi-thread architecture - Inter-thread message communication - Independent command shell using thread - HTTPD with CGI, WS, RPC - Key & value pair configuration load/save

Dependencies:   CMDB EthernetInterface HTTPD dconfig mbed-rpc mbed-rtos mbed storage_on_flash

MainConfig.cpp

Committer:
hillkim7
Date:
2015-07-03
Revision:
3:df8a882e33a6
Parent:
2:d7ffadba49b9

File content as of revision 3:df8a882e33a6:

/**
 * @file MainConfig.cpp
 *
 * @brief main configuration
 *
 */

#include "MainConfig.h"
#include "SOFBlock.h"

// main configuration sector index of flash memory.
const uint8_t config_sector_index = 7;

void MainConfig::reset_default(void)
{
    clear();
    (*this)["eth"] = "dhcp";
    (*this)["ip"] = "";
    (*this)["mask"] = "255.255.255.0";
    (*this)["gw"] = "";
}

bool MainConfig::load_config()
{
    SOFReader reader;

    if (reader.open(config_sector_index) != kSOF_ErrNone) {
        return false;
    }

    return load_from((char *)reader.get_physical_data_addr(), reader.get_data_size());
}

static bool save_func(void *user_data, char c)
{
    SOFWriter *writer = (SOFWriter*)user_data;

    return writer->write_byte_data((uint8_t)c);
}

bool MainConfig::save_config()
{
    size_t need_bytes = estimate_save();
    SOFWriter writer;

    if (writer.open(config_sector_index) != kSOF_ErrNone) {
        printf("open(%d) fail: format\r\n", config_sector_index);
        SOFBlock::format(config_sector_index);
        writer.open(config_sector_index);
    } else {
        if (need_bytes > writer.get_free_size()) {
            printf("too small free size(%u/%u): format\r\n", need_bytes, writer.get_free_size());
            writer.close();
            SOFBlock::format(config_sector_index);
            writer.open(config_sector_index);
        }
    }

    return save_to(save_func, &writer);
}