BlinkenArea - GitList
Repositories
Blog
Wiki
flaneth
Code
Commits
Branches
Tags
Search
Tree:
293b941
Branches
Tags
master
flaneth
firmware
app_cfg.c
support for reading MAC/IP config from CF card
Stefan Schuermans
commited
293b941
at 2012-05-04 20:06:58
app_cfg.c
Blame
History
Raw
/* flaneth - flash and ethernet Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info> Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html a BlinkenArea project - http://www.blinkenarea.org/ */ #include <stdio.h> #include <string.h> #include "app_cfg.h" #include "config.h" #include "debug.h" #include "dosfs.h" #include "rtl8019.h" /** * @brief read string from file * @param[in] sectorBuf scratch buffer to store a sector * @param[in] vi volume information structure * @param[in] filename name of file to read * @param[out] buf buffer to put string to * @param[in] sz size of buffer * @return 0 on success, -1 on error */ static char AppCfgReadStr(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi, const char *filename, char *buf, unsigned int sz) { FILEINFO fi; uint32_t len; // open file if (DFS_OpenFile(vi, (uint8_t *)filename, DFS_READ, sectorBuf, &fi) != 0) { debug_app_cfg_printf("cannot open file %s", filename); return -1; } // read string from file len = 0; DFS_ReadFile(&fi, sectorBuf, (uint8_t *)buf, &len, sz - 1); if (len > sz - 1) len = sz - 1; buf[len] = 0; // terminate string debug_apps_printf("string read from file %s: %s", filename, buf); return 0; } /** * @brief read MAC address from file * @param[in] sectorBuf scratch buffer to store a sector * @param[in] vi volume information structure * @param[in] filename name of file to read * @param[out] mac MAC address read * @return 0 on success, -1 on error */ static char AppCfgReadMac(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi, const char *filename, unsigned char mac[6]) { char bufStr[18]; unsigned char bufMac[6]; // get string from file if (AppCfgReadStr(sectorBuf, vi, filename, bufStr, sizeof(bufStr)) != 0) return -1; // parse string if (sscanf(bufStr, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &bufMac[0], &bufMac[1], &bufMac[2], &bufMac[3], &bufMac[4], &bufMac[5]) != 6) { debug_apps_printf("MAC parse error"); return -1; } // copy MAC to output debug_apps_printf("MAC read: %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX", bufMac[0], bufMac[1], bufMac[2], bufMac[3], bufMac[4], bufMac[5]); memcpy(mac, bufMac, 6); return 0; } /** * @brief read IP address from file * @param[in] sectorBuf scratch buffer to store a sector * @param[in] vi volume information structure * @param[in] filename name of file to read * @param[out] ip IP address read * @return 0 on success, -1 on error */ static char AppCfgReadIp(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi, const char *filename, unsigned char ip[4]) { char bufStr[16]; unsigned char bufIp[4]; // get string from file if (AppCfgReadStr(sectorBuf, vi, filename, bufStr, sizeof(bufStr)) != 0) return -1; // parse string if (sscanf(bufStr, "%hhu.%hhu.%hhu.%hhu", &bufIp[0], &bufIp[1], &bufIp[2], &bufIp[3]) != 4) { debug_apps_printf("IP parse error"); return -1; } // copy IP to output debug_apps_printf("IP read: %hhu.%hhu.%hhu.%hhu", bufIp[0], bufIp[1], bufIp[2], bufIp[3]); memcpy(ip, bufIp, 4); return 0; } /** * @brief update configuration * @param[in] sectorBuf scratch buffer to store a sector * @param[in] vi volume information structure */ void AppCfgRun(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi) // (extern) { // load config if (AppCfgReadMac(sectorBuf, vi, "cfg/mac", ConfigMac) == 0) RtlInit(); AppCfgReadIp(sectorBuf, vi, "cfg/ip", ConfigIp); AppCfgReadIp(sectorBuf, vi, "cfg/mask", ConfigMask); AppCfgReadIp(sectorBuf, vi, "cfg/gw", ConfigGw); }