support for reading MAC/IP config from CF card
Stefan Schuermans

Stefan Schuermans commited on 2012-05-04 20:06:58
Showing 5 changed files, with 157 additions and 2 deletions.

... ...
@@ -60,7 +60,7 @@ SRC = $(TARGET).c
60 60
 
61 61
 # If there is more than one source file, append them above, or modify and
62 62
 # uncomment the following:
63
-SRC += apps.c \
63
+SRC += app_cfg.c apps.c \
64 64
        arp.c bus.c cf.c checksum.c config.c dhcp.c dosfs.c dosfs_user.c \
65 65
        eeprom.c ethernet.c http.c icmp.c ip.c random.c rtl8019.c \
66 66
        ser62500.c status.c tasks.c tcp.c timing.c uart.c udp.c \
... ...
@@ -0,0 +1,126 @@
1
+/* flaneth - flash and ethernet
2
+   Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info>
3
+   Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html
4
+   a BlinkenArea project - http://www.blinkenarea.org/ */
5
+
6
+#include <stdio.h>
7
+#include <string.h>
8
+
9
+#include "app_cfg.h"
10
+#include "config.h"
11
+#include "debug.h"
12
+#include "dosfs.h"
13
+#include "rtl8019.h"
14
+
15
+/**
16
+ * @brief read string from file
17
+ * @param[in] sectorBuf scratch buffer to store a sector
18
+ * @param[in] vi volume information structure
19
+ * @param[in] filename name of file to read
20
+ * @param[out] buf buffer to put string to
21
+ * @param[in] sz size of buffer
22
+ * @return 0 on success, -1 on error
23
+ */
24
+static char AppCfgReadStr(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi,
25
+                          const char *filename, char *buf, unsigned int sz)
26
+{
27
+  FILEINFO fi;
28
+  uint32_t len;
29
+
30
+  // open file
31
+  if (DFS_OpenFile(vi, (uint8_t *)filename, DFS_READ, sectorBuf, &fi) != 0) {
32
+    debug_app_cfg_printf("cannot open file %s", filename);
33
+    return -1;
34
+  }
35
+
36
+  // read string from file
37
+  len = 0;
38
+  DFS_ReadFile(&fi, sectorBuf, (uint8_t *)buf, &len, sz - 1);
39
+  if (len > sz - 1)
40
+    len = sz - 1;
41
+  buf[len] = 0; // terminate string
42
+  debug_apps_printf("string read from file %s: %s", filename, buf);
43
+  return 0;
44
+}
45
+
46
+/**
47
+ * @brief read MAC address from file
48
+ * @param[in] sectorBuf scratch buffer to store a sector
49
+ * @param[in] vi volume information structure
50
+ * @param[in] filename name of file to read
51
+ * @param[out] mac MAC address read
52
+ * @return 0 on success, -1 on error
53
+ */
54
+static char AppCfgReadMac(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi,
55
+                          const char *filename, unsigned char mac[6])
56
+{
57
+  char bufStr[18];
58
+  unsigned char bufMac[6];
59
+
60
+  // get string from file
61
+  if (AppCfgReadStr(sectorBuf, vi, filename, bufStr, sizeof(bufStr)) != 0)
62
+    return -1;
63
+
64
+  // parse string
65
+  if (sscanf(bufStr, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
66
+             &bufMac[0], &bufMac[1], &bufMac[2],
67
+             &bufMac[3], &bufMac[4], &bufMac[5]) != 6) {
68
+    debug_apps_printf("MAC parse error");
69
+    return -1;
70
+  }
71
+
72
+  // copy MAC to output
73
+  debug_apps_printf("MAC read: %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
74
+                    bufMac[0], bufMac[1], bufMac[2],
75
+                    bufMac[3], bufMac[4], bufMac[5]);
76
+  memcpy(mac, bufMac, 6);
77
+  return 0;
78
+}
79
+
80
+/**
81
+ * @brief read IP address from file
82
+ * @param[in] sectorBuf scratch buffer to store a sector
83
+ * @param[in] vi volume information structure
84
+ * @param[in] filename name of file to read
85
+ * @param[out] ip IP address read
86
+ * @return 0 on success, -1 on error
87
+ */
88
+static char AppCfgReadIp(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi,
89
+                         const char *filename, unsigned char ip[4])
90
+{
91
+  char bufStr[16];
92
+  unsigned char bufIp[4];
93
+
94
+  // get string from file
95
+  if (AppCfgReadStr(sectorBuf, vi, filename, bufStr, sizeof(bufStr)) != 0)
96
+    return -1;
97
+
98
+  // parse string
99
+  if (sscanf(bufStr, "%hhu.%hhu.%hhu.%hhu",
100
+             &bufIp[0], &bufIp[1], &bufIp[2], &bufIp[3]) != 4) {
101
+    debug_apps_printf("IP parse error");
102
+    return -1;
103
+  }
104
+
105
+  // copy IP to output
106
+  debug_apps_printf("IP read: %hhu.%hhu.%hhu.%hhu",
107
+                    bufIp[0], bufIp[1], bufIp[2], bufIp[3]);
108
+  memcpy(ip, bufIp, 4);
109
+  return 0;
110
+}
111
+
112
+/**
113
+ * @brief update configuration
114
+ * @param[in] sectorBuf scratch buffer to store a sector
115
+ * @param[in] vi volume information structure
116
+ */
117
+void AppCfgRun(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi) // (extern)
118
+{
119
+  // load config
120
+  if (AppCfgReadMac(sectorBuf, vi, "cfg/mac", ConfigMac) == 0)
121
+    RtlInit();
122
+  AppCfgReadIp(sectorBuf, vi, "cfg/ip", ConfigIp);
123
+  AppCfgReadIp(sectorBuf, vi, "cfg/mask", ConfigMask);
124
+  AppCfgReadIp(sectorBuf, vi, "cfg/gw", ConfigGw);
125
+}
126
+
... ...
@@ -0,0 +1,19 @@
1
+/* flaneth - flash and ethernet
2
+   Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info>
3
+   Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html
4
+   a BlinkenArea project - http://www.blinkenarea.org/ */
5
+
6
+#ifndef INC_app_cfg
7
+#define INC_app_cfg
8
+
9
+#include "dosfs.h"
10
+
11
+/**
12
+ * @brief update configuration
13
+ * @param[in] sectorBuf scratch buffer to store a sector
14
+ * @param[in] vi volume information structure
15
+ */
16
+extern void AppCfgRun(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi);
17
+
18
+#endif // #ifndef INC_app_cfg
19
+
... ...
@@ -5,11 +5,16 @@
5 5
 
6 6
 #include <string.h>
7 7
 
8
+#include "app_cfg.h"
8 9
 #include "apps.h"
9 10
 #include "debug.h"
10 11
 #include "dosfs.h"
11 12
 
12
-// list files and
13
+/**
14
+ * @brief list directory and dump file
15
+ * @param[in] sectorBuf scratch buffer to store a sector
16
+ * @param[in] vi volume information structure
17
+ */
13 18
 static void AppsListDump(uint8_t sectorBuf[SECTOR_SIZE], VOLINFO *vi)
14 19
 {
15 20
   DIRINFO di;
... ...
@@ -96,5 +101,8 @@ void AppsRun(void) // (extern)
96 101
 
97 102
   // list directory, dump file
98 103
   AppsListDump(sectorBuf, &vi);
104
+
105
+  // run one-short applications
106
+  AppCfgRun(sectorBuf, &vi);
99 107
 }
100 108
 
... ...
@@ -23,6 +23,7 @@
23 23
 #define DEBUG_TCP 1
24 24
 #define DEBUG_HTTP 1
25 25
 #define DEBUG_APPS 1
26
+#define DEBUG_APP_CFG 1
26 27
 
27 28
 // debug version of printf
28 29
 #ifdef DEBUG
... ...
@@ -53,5 +54,6 @@
53 54
 #define debug_tcp_printf(fmt, arg...) debug_specialized_printf(DEBUG_TCP, "tcp: "fmt, ##arg)
54 55
 #define debug_http_printf(fmt, arg...) debug_specialized_printf(DEBUG_HTTP, "http: "fmt, ##arg)
55 56
 #define debug_apps_printf(fmt, arg...) debug_specialized_printf(DEBUG_APPS, "apps: "fmt, ##arg)
57
+#define debug_app_cfg_printf(fmt, arg...) debug_specialized_printf(DEBUG_APP_CFG, "app_cfg: "fmt, ##arg)
56 58
 
57 59
 #endif // #ifndef INC_debug
58 60