
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@2:d7ffadba49b9, 2015-06-17 (annotated)
- Committer:
- hillkim7
- Date:
- Wed Jun 17 01:55:19 2015 +0000
- Revision:
- 2:d7ffadba49b9
- Parent:
- 0:2ffd10976643
Fix stack overflow problem of the console task.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hillkim7 | 0:2ffd10976643 | 1 | /** |
hillkim7 | 0:2ffd10976643 | 2 | * @file MainConfig.cpp |
hillkim7 | 0:2ffd10976643 | 3 | * |
hillkim7 | 0:2ffd10976643 | 4 | * @brief main configuration |
hillkim7 | 0:2ffd10976643 | 5 | * |
hillkim7 | 0:2ffd10976643 | 6 | */ |
hillkim7 | 0:2ffd10976643 | 7 | |
hillkim7 | 0:2ffd10976643 | 8 | #include "MainConfig.h" |
hillkim7 | 0:2ffd10976643 | 9 | #include "SOFBlock.h" |
hillkim7 | 0:2ffd10976643 | 10 | |
hillkim7 | 0:2ffd10976643 | 11 | // main configuration sector index of flash memory. |
hillkim7 | 0:2ffd10976643 | 12 | const uint8_t config_sector_index = 7; |
hillkim7 | 0:2ffd10976643 | 13 | |
hillkim7 | 0:2ffd10976643 | 14 | void MainConfig::reset_default(void) |
hillkim7 | 0:2ffd10976643 | 15 | { |
hillkim7 | 2:d7ffadba49b9 | 16 | clear(); |
hillkim7 | 0:2ffd10976643 | 17 | (*this)["eth"] = "dhcp"; |
hillkim7 | 0:2ffd10976643 | 18 | (*this)["ip"] = ""; |
hillkim7 | 0:2ffd10976643 | 19 | (*this)["mask"] = "255.255.255.0"; |
hillkim7 | 0:2ffd10976643 | 20 | (*this)["gw"] = ""; |
hillkim7 | 0:2ffd10976643 | 21 | } |
hillkim7 | 0:2ffd10976643 | 22 | |
hillkim7 | 0:2ffd10976643 | 23 | bool MainConfig::load_config() |
hillkim7 | 0:2ffd10976643 | 24 | { |
hillkim7 | 0:2ffd10976643 | 25 | SOFReader reader; |
hillkim7 | 0:2ffd10976643 | 26 | |
hillkim7 | 0:2ffd10976643 | 27 | if (reader.open(config_sector_index) != kSOF_ErrNone) { |
hillkim7 | 0:2ffd10976643 | 28 | return false; |
hillkim7 | 0:2ffd10976643 | 29 | } |
hillkim7 | 0:2ffd10976643 | 30 | |
hillkim7 | 0:2ffd10976643 | 31 | return load_from((char *)reader.get_physical_data_addr(), reader.get_data_size()); |
hillkim7 | 0:2ffd10976643 | 32 | } |
hillkim7 | 0:2ffd10976643 | 33 | |
hillkim7 | 0:2ffd10976643 | 34 | static bool save_func(void *user_data, char c) |
hillkim7 | 0:2ffd10976643 | 35 | { |
hillkim7 | 0:2ffd10976643 | 36 | SOFWriter *writer = (SOFWriter*)user_data; |
hillkim7 | 0:2ffd10976643 | 37 | |
hillkim7 | 0:2ffd10976643 | 38 | return writer->write_byte_data((uint8_t)c); |
hillkim7 | 0:2ffd10976643 | 39 | } |
hillkim7 | 0:2ffd10976643 | 40 | |
hillkim7 | 0:2ffd10976643 | 41 | bool MainConfig::save_config() |
hillkim7 | 0:2ffd10976643 | 42 | { |
hillkim7 | 0:2ffd10976643 | 43 | size_t need_bytes = estimate_save(); |
hillkim7 | 0:2ffd10976643 | 44 | SOFWriter writer; |
hillkim7 | 0:2ffd10976643 | 45 | |
hillkim7 | 0:2ffd10976643 | 46 | if (writer.open(config_sector_index) != kSOF_ErrNone) { |
hillkim7 | 0:2ffd10976643 | 47 | printf("open(%d) fail: format\r\n", config_sector_index); |
hillkim7 | 0:2ffd10976643 | 48 | SOFBlock::format(config_sector_index); |
hillkim7 | 0:2ffd10976643 | 49 | writer.open(config_sector_index); |
hillkim7 | 0:2ffd10976643 | 50 | } else { |
hillkim7 | 0:2ffd10976643 | 51 | if (need_bytes > writer.get_free_size()) { |
hillkim7 | 0:2ffd10976643 | 52 | printf("too small free size(%u/%u): format\r\n", need_bytes, writer.get_free_size()); |
hillkim7 | 0:2ffd10976643 | 53 | writer.close(); |
hillkim7 | 0:2ffd10976643 | 54 | SOFBlock::format(config_sector_index); |
hillkim7 | 0:2ffd10976643 | 55 | writer.open(config_sector_index); |
hillkim7 | 0:2ffd10976643 | 56 | } |
hillkim7 | 0:2ffd10976643 | 57 | } |
hillkim7 | 0:2ffd10976643 | 58 | |
hillkim7 | 0:2ffd10976643 | 59 | return save_to(save_func, &writer); |
hillkim7 | 0:2ffd10976643 | 60 | } |
hillkim7 | 0:2ffd10976643 | 61 | |
hillkim7 | 0:2ffd10976643 | 62 | |
hillkim7 | 0:2ffd10976643 | 63 | |
hillkim7 | 2:d7ffadba49b9 | 64 |