mbed_controller / Mbed 2 deprecated mbed_controller_demo

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MainConfig.cpp Source File

MainConfig.cpp

Go to the documentation of this file.
00001 /**
00002  * @file MainConfig.cpp
00003  *
00004  * @brief main configuration
00005  *
00006  */
00007 
00008 #include "MainConfig.h"
00009 #include "SOFBlock.h"
00010 
00011 // main configuration sector index of flash memory.
00012 const uint8_t config_sector_index = 7;
00013 
00014 void MainConfig::reset_default(void)
00015 {
00016     clear();
00017     (*this)["eth"] = "dhcp";
00018     (*this)["ip"] = "";
00019     (*this)["mask"] = "255.255.255.0";
00020     (*this)["gw"] = "";
00021 }
00022 
00023 bool MainConfig::load_config()
00024 {
00025     SOFReader reader;
00026 
00027     if (reader.open(config_sector_index) != kSOF_ErrNone) {
00028         return false;
00029     }
00030 
00031     return load_from((char *)reader.get_physical_data_addr(), reader.get_data_size());
00032 }
00033 
00034 static bool save_func(void *user_data, char c)
00035 {
00036     SOFWriter *writer = (SOFWriter*)user_data;
00037 
00038     return writer->write_byte_data((uint8_t)c);
00039 }
00040 
00041 bool MainConfig::save_config()
00042 {
00043     size_t need_bytes = estimate_save();
00044     SOFWriter writer;
00045 
00046     if (writer.open(config_sector_index) != kSOF_ErrNone) {
00047         printf("open(%d) fail: format\r\n", config_sector_index);
00048         SOFBlock::format(config_sector_index);
00049         writer.open(config_sector_index);
00050     } else {
00051         if (need_bytes > writer.get_free_size()) {
00052             printf("too small free size(%u/%u): format\r\n", need_bytes, writer.get_free_size());
00053             writer.close();
00054             SOFBlock::format(config_sector_index);
00055             writer.open(config_sector_index);
00056         }
00057     }
00058 
00059     return save_to(save_func, &writer);
00060 }
00061 
00062 
00063 
00064