Modify the file main.cpp for M487

Dependencies:   BufferedSerial

Committer:
shliu1
Date:
Fri Sep 29 05:45:43 2017 +0000
Revision:
0:c89ccc69a48b
main.cpp adds the setting of TARGET_NUMAKER_PFM_M487 for M487

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shliu1 0:c89ccc69a48b 1 /*
shliu1 0:c89ccc69a48b 2 * Copyright (c) 2017 Nuvoton Tecnology Corp. All rights reserved.
shliu1 0:c89ccc69a48b 3 *
shliu1 0:c89ccc69a48b 4 * micro web server for serial to Ethernet configuration.
shliu1 0:c89ccc69a48b 5 *
shliu1 0:c89ccc69a48b 6 *
shliu1 0:c89ccc69a48b 7 */
shliu1 0:c89ccc69a48b 8
shliu1 0:c89ccc69a48b 9 #include <string.h>
shliu1 0:c89ccc69a48b 10 #include "ste_config.h"
shliu1 0:c89ccc69a48b 11
shliu1 0:c89ccc69a48b 12 #ifdef ENABLE_WEB_CONFIG
shliu1 0:c89ccc69a48b 13
shliu1 0:c89ccc69a48b 14 #define PAGE_HEADER \
shliu1 0:c89ccc69a48b 15 "<html><head>\r\n" \
shliu1 0:c89ccc69a48b 16 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n" \
shliu1 0:c89ccc69a48b 17 "<title>Serial to Ethernet Configuration</title>\r\n" \
shliu1 0:c89ccc69a48b 18 "</head>\r\n"
shliu1 0:c89ccc69a48b 19
shliu1 0:c89ccc69a48b 20 #define PAGE_HEADER_FOR_REFRESH \
shliu1 0:c89ccc69a48b 21 "<html><head>\r\n" \
shliu1 0:c89ccc69a48b 22 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n" \
shliu1 0:c89ccc69a48b 23 "<meta http-equiv=\"refresh\" content=\"10; url=index.html\" />\r\n" \
shliu1 0:c89ccc69a48b 24 "<title>Serial to Ethernet Configuration</title>\r\n" \
shliu1 0:c89ccc69a48b 25 "</head>\r\n"
shliu1 0:c89ccc69a48b 26
shliu1 0:c89ccc69a48b 27 #define PAGE_RESTART_FORM \
shliu1 0:c89ccc69a48b 28 "<form action=\"/config.html\" method=\"GET\">\r\n" \
shliu1 0:c89ccc69a48b 29 "<input type=\"hidden\" name=\"restart\" value=\"1\">\r\n" \
shliu1 0:c89ccc69a48b 30 "<input type=\"submit\" value=\"Restart\"> </form>\r\n"
shliu1 0:c89ccc69a48b 31
shliu1 0:c89ccc69a48b 32 #define PAGE_RESETCONF_FORM \
shliu1 0:c89ccc69a48b 33 "<form action=\"/config.html\" method=\"GET\">\r\n" \
shliu1 0:c89ccc69a48b 34 "<input type=\"hidden\" name=\"resetconf\" value=\"1\">\r\n" \
shliu1 0:c89ccc69a48b 35 "<input type=\"submit\" value=\"Reset Config\"> </form>\r\n"
shliu1 0:c89ccc69a48b 36
shliu1 0:c89ccc69a48b 37 #define PAGE_CONFIG_FORM_HEAD \
shliu1 0:c89ccc69a48b 38 "<form action=\"/config.html\" method=\"GET\">\r\n"
shliu1 0:c89ccc69a48b 39
shliu1 0:c89ccc69a48b 40 #define PAGE_CONFIG_SERIAL_FORM \
shliu1 0:c89ccc69a48b 41 "<input type=\"hidden\" name=\"uart_port\" value=\"%d\">\r\n" \
shliu1 0:c89ccc69a48b 42 " Baud <select type=\"text\" name=\"baud\">\r\n \
shliu1 0:c89ccc69a48b 43 <option value=\"115200\"%s>115200</option>\r\n \
shliu1 0:c89ccc69a48b 44 <option value=\"9600\"%s>9600</option>\r\n \
shliu1 0:c89ccc69a48b 45 <option value=\"4800\"%s>4800</option>\r\n \
shliu1 0:c89ccc69a48b 46 <option value=\"2400\"%s>2400</option>\r\n \
shliu1 0:c89ccc69a48b 47 </select> &nbsp&nbsp;\r\n \
shliu1 0:c89ccc69a48b 48 Data bits <select type=\"text\" name=\"databits\">\r\n \
shliu1 0:c89ccc69a48b 49 <option value=\"8\"%s>8</option>\r\n \
shliu1 0:c89ccc69a48b 50 <option value=\"7\"%s>7</option>\r\n \
shliu1 0:c89ccc69a48b 51 <option value=\"6\"%s>6</option>\r\n \
shliu1 0:c89ccc69a48b 52 <option value=\"5\"%s>5</option>\r\n \
shliu1 0:c89ccc69a48b 53 </select> &nbsp;&nbsp;\r\n \
shliu1 0:c89ccc69a48b 54 Parity <select type=\"text\" name=\"parity\">\r\n \
shliu1 0:c89ccc69a48b 55 <option value=\"0\"%s>None</option>\r\n \
shliu1 0:c89ccc69a48b 56 <option value=\"1\"%s>Odd</option>\r\n \
shliu1 0:c89ccc69a48b 57 <option value=\"2\"%s>Even</option>\r\n \
shliu1 0:c89ccc69a48b 58 </select> &nbsp;&nbsp;\r\n \
shliu1 0:c89ccc69a48b 59 Stop bits <select type=\"text\" name=\"stopbits\">\r\n \
shliu1 0:c89ccc69a48b 60 <option value=\"1\"%s>1</option>\r\n \
shliu1 0:c89ccc69a48b 61 <option value=\"2\"%s>2</option>\r\n \
shliu1 0:c89ccc69a48b 62 </select> <br>\r\n \
shliu1 0:c89ccc69a48b 63 Network mode <select type=\"text\" name=\"netmode\">\r\n \
shliu1 0:c89ccc69a48b 64 <option value=\"0\"%s>Server</option>\r\n \
shliu1 0:c89ccc69a48b 65 <option value=\"1\"%s>Client</option>\r\n \
shliu1 0:c89ccc69a48b 66 </select> <br> Set following fields for Client only. <br>\r\n \
shliu1 0:c89ccc69a48b 67 Server address <input type=\"text\" name=\"sipaddr\" size=\"16\" value=\"%s\"> &nbsp&nbsp;\r\n \
shliu1 0:c89ccc69a48b 68 Server port <input type=\"text\" name=\"sport\" size=\"5\" value=\"%d\">\r\n"
shliu1 0:c89ccc69a48b 69
shliu1 0:c89ccc69a48b 70 #define PAGE_CONFIG_NET_FORM \
shliu1 0:c89ccc69a48b 71 "<input type=\"hidden\" name=\"net_config\" value=\"1\">\r\n \
shliu1 0:c89ccc69a48b 72 IP Address Mode <select type=\"text\" name=\"ipmode\">\r\n \
shliu1 0:c89ccc69a48b 73 <option value=\"0\"%s>Static</option>\r\n \
shliu1 0:c89ccc69a48b 74 <option value=\"1\"%s>DHCP</option>\r\n \
shliu1 0:c89ccc69a48b 75 </select> <br> Set following fields for Static IP only. <br>\r\n \
shliu1 0:c89ccc69a48b 76 IP Address <input type=\"text\" name=\"ipaddr\" size=\"16\" value=\"%s\">\r\n \
shliu1 0:c89ccc69a48b 77 Netmask <input type=\"text\" name=\"mask\" size=\"16\" value=\"%s\">\r\n \
shliu1 0:c89ccc69a48b 78 Gateway Address <input type=\"text\" name=\"gwaddr\" size=\"16\" value=\"%s\">\r\n"
shliu1 0:c89ccc69a48b 79
shliu1 0:c89ccc69a48b 80 typedef void (*PROC_PAGE_PFN)(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 81
shliu1 0:c89ccc69a48b 82 typedef struct {
shliu1 0:c89ccc69a48b 83 const char *vhtmlpage; // virtual page name, eg. index.html ...
shliu1 0:c89ccc69a48b 84 PROC_PAGE_PFN pfn;
shliu1 0:c89ccc69a48b 85 } S_WEB_PAGE_SETTING;
shliu1 0:c89ccc69a48b 86
shliu1 0:c89ccc69a48b 87 extern void process_http_index_html(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 88 extern void process_http_restart_html(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 89 extern void process_http_resetconf_html(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 90 extern void process_http_config_html(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 91 extern void process_http_err_html(TCPSocket *, char *);
shliu1 0:c89ccc69a48b 92
shliu1 0:c89ccc69a48b 93 #define MAX_WEB_GET_PAGE_SETTINGS 5
shliu1 0:c89ccc69a48b 94 S_WEB_PAGE_SETTING web_get_pages_setings[MAX_WEB_GET_PAGE_SETTINGS] = {
shliu1 0:c89ccc69a48b 95 {"/ ", process_http_index_html},
shliu1 0:c89ccc69a48b 96 {"/index.html ", process_http_index_html},
shliu1 0:c89ccc69a48b 97 {"/config.html?restart=1 ", process_http_restart_html},
shliu1 0:c89ccc69a48b 98 {"/config.html?resetconf=1 ", process_http_resetconf_html},
shliu1 0:c89ccc69a48b 99 {"/config.html?", process_http_config_html}
shliu1 0:c89ccc69a48b 100 };
shliu1 0:c89ccc69a48b 101
shliu1 0:c89ccc69a48b 102 #define MAX_WEB_POST_PAGE_SETTINGS 1
shliu1 0:c89ccc69a48b 103 S_WEB_PAGE_SETTING web_post_pages_setings[MAX_WEB_POST_PAGE_SETTINGS] = {
shliu1 0:c89ccc69a48b 104 {"/config.html ", process_http_config_html},
shliu1 0:c89ccc69a48b 105 };
shliu1 0:c89ccc69a48b 106
shliu1 0:c89ccc69a48b 107 typedef struct {
shliu1 0:c89ccc69a48b 108 int type; // 0: integer, 1:string
shliu1 0:c89ccc69a48b 109 int value; // integer value for integer type
shliu1 0:c89ccc69a48b 110 char *str; // point to string for string type
shliu1 0:c89ccc69a48b 111 int min;
shliu1 0:c89ccc69a48b 112 int max; // max value of integer or max length of string
shliu1 0:c89ccc69a48b 113 const char *cmd;
shliu1 0:c89ccc69a48b 114 int cmd_len;
shliu1 0:c89ccc69a48b 115 } S_WEB_CMD_PARAMS;
shliu1 0:c89ccc69a48b 116
shliu1 0:c89ccc69a48b 117 #define MAX_WEB_CMD_PARAMS 13
shliu1 0:c89ccc69a48b 118 S_WEB_CMD_PARAMS web_cmd_params[MAX_WEB_CMD_PARAMS] = {
shliu1 0:c89ccc69a48b 119 {0, 0, NULL, 1, MAX_UART_PORTS, "uart_port=", 10},
shliu1 0:c89ccc69a48b 120 {0, 0, NULL, 2400, 115200, "baud=", 5},
shliu1 0:c89ccc69a48b 121 {0, 0, NULL, 5, 8, "databits=", 9},
shliu1 0:c89ccc69a48b 122 {0, 0, NULL, 0, 2, "parity=", 7},
shliu1 0:c89ccc69a48b 123 {0, 0, NULL, 1, 2, "stopbits=", 9},
shliu1 0:c89ccc69a48b 124 {0, 0, NULL, 0, 1, "netmode=", 8},
shliu1 0:c89ccc69a48b 125 {1, 0, NULL, 0, 16, "sipaddr=", 8},
shliu1 0:c89ccc69a48b 126 {0, 0, NULL, 0, 65535, "sport=", 6},
shliu1 0:c89ccc69a48b 127 {0, 0, NULL, 1, 1, "net_config=", 11},
shliu1 0:c89ccc69a48b 128 {0, 0, NULL, 0, 1, "ipmode=", 7},
shliu1 0:c89ccc69a48b 129 {1, 0, NULL, 0, 1, "ipaddr=", 7},
shliu1 0:c89ccc69a48b 130 {1, 0, NULL, 0, 1, "mask=", 5},
shliu1 0:c89ccc69a48b 131 {1, 0, NULL, 0, 1, "gwaddr=", 7}
shliu1 0:c89ccc69a48b 132 };
shliu1 0:c89ccc69a48b 133
shliu1 0:c89ccc69a48b 134 const char *parity_string[] = {"None", "Odd", "Even"};
shliu1 0:c89ccc69a48b 135 const char *netmode_string[] = {"Server", "Client"};
shliu1 0:c89ccc69a48b 136 const char *ipmode_string[] = {"Static", "DHCP"};
shliu1 0:c89ccc69a48b 137
shliu1 0:c89ccc69a48b 138
shliu1 0:c89ccc69a48b 139 int inspect_each_setting(char *pbuf)
shliu1 0:c89ccc69a48b 140 {
shliu1 0:c89ccc69a48b 141 int i;
shliu1 0:c89ccc69a48b 142 int v;
shliu1 0:c89ccc69a48b 143
shliu1 0:c89ccc69a48b 144 for(i=0; i<MAX_WEB_CMD_PARAMS; i++)
shliu1 0:c89ccc69a48b 145 {
shliu1 0:c89ccc69a48b 146 web_cmd_params[i].value = 0;
shliu1 0:c89ccc69a48b 147 web_cmd_params[i].str = NULL;
shliu1 0:c89ccc69a48b 148 }
shliu1 0:c89ccc69a48b 149
shliu1 0:c89ccc69a48b 150 while(*pbuf != '\0')
shliu1 0:c89ccc69a48b 151 {
shliu1 0:c89ccc69a48b 152 for(i=0; i<MAX_WEB_CMD_PARAMS; i++)
shliu1 0:c89ccc69a48b 153 {
shliu1 0:c89ccc69a48b 154 if (strncmp(pbuf, web_cmd_params[i].cmd, web_cmd_params[i].cmd_len) == 0)
shliu1 0:c89ccc69a48b 155 {
shliu1 0:c89ccc69a48b 156 pbuf += web_cmd_params[i].cmd_len;
shliu1 0:c89ccc69a48b 157
shliu1 0:c89ccc69a48b 158 if (web_cmd_params[i].type == 0)
shliu1 0:c89ccc69a48b 159 {
shliu1 0:c89ccc69a48b 160 v = atoi(pbuf);
shliu1 0:c89ccc69a48b 161 if (v >= web_cmd_params[i].min && v <= web_cmd_params[i].max)
shliu1 0:c89ccc69a48b 162 web_cmd_params[i].value = v;
shliu1 0:c89ccc69a48b 163 }
shliu1 0:c89ccc69a48b 164 else
shliu1 0:c89ccc69a48b 165 {
shliu1 0:c89ccc69a48b 166 web_cmd_params[i].str = pbuf;
shliu1 0:c89ccc69a48b 167 }
shliu1 0:c89ccc69a48b 168
shliu1 0:c89ccc69a48b 169 for(; *pbuf != '\0'; pbuf++)
shliu1 0:c89ccc69a48b 170 {
shliu1 0:c89ccc69a48b 171 if (*pbuf == '&')
shliu1 0:c89ccc69a48b 172 {
shliu1 0:c89ccc69a48b 173 *pbuf++ = '\0';
shliu1 0:c89ccc69a48b 174 break;
shliu1 0:c89ccc69a48b 175 }
shliu1 0:c89ccc69a48b 176 else if (*pbuf == ' ')
shliu1 0:c89ccc69a48b 177 {
shliu1 0:c89ccc69a48b 178 *pbuf = '\0';
shliu1 0:c89ccc69a48b 179 break;
shliu1 0:c89ccc69a48b 180 }
shliu1 0:c89ccc69a48b 181 // else if (*pbuf == '\r')
shliu1 0:c89ccc69a48b 182 // {
shliu1 0:c89ccc69a48b 183 // *pbuf++ = '\0';
shliu1 0:c89ccc69a48b 184 // if (*pbuf == '\n')
shliu1 0:c89ccc69a48b 185 // *pbuf++ = '\0';
shliu1 0:c89ccc69a48b 186 // break;
shliu1 0:c89ccc69a48b 187 // }
shliu1 0:c89ccc69a48b 188 // else if (*pbuf == '\n' || *pbuf == ' ')
shliu1 0:c89ccc69a48b 189 // {
shliu1 0:c89ccc69a48b 190 // *pbuf++ = '\0';
shliu1 0:c89ccc69a48b 191 // break;
shliu1 0:c89ccc69a48b 192 // }
shliu1 0:c89ccc69a48b 193 }
shliu1 0:c89ccc69a48b 194
shliu1 0:c89ccc69a48b 195 break;
shliu1 0:c89ccc69a48b 196 }
shliu1 0:c89ccc69a48b 197 }
shliu1 0:c89ccc69a48b 198
shliu1 0:c89ccc69a48b 199 if (i >= MAX_WEB_CMD_PARAMS)
shliu1 0:c89ccc69a48b 200 return -1;
shliu1 0:c89ccc69a48b 201 }
shliu1 0:c89ccc69a48b 202
shliu1 0:c89ccc69a48b 203 return 0;
shliu1 0:c89ccc69a48b 204 }
shliu1 0:c89ccc69a48b 205
shliu1 0:c89ccc69a48b 206 int process_settings(char *pbuf)
shliu1 0:c89ccc69a48b 207 {
shliu1 0:c89ccc69a48b 208 FILE *fp;
shliu1 0:c89ccc69a48b 209
shliu1 0:c89ccc69a48b 210 printf("Verify Configure setting...\r\n");
shliu1 0:c89ccc69a48b 211 printf("---Dump Settings---\r\n%s\r\n---End of dump---\r\n", pbuf);
shliu1 0:c89ccc69a48b 212
shliu1 0:c89ccc69a48b 213 if (SD_Card_Mounted == FALSE)
shliu1 0:c89ccc69a48b 214 {
shliu1 0:c89ccc69a48b 215 printf("SD card doesn't exist. Ignore settings.\r\n");
shliu1 0:c89ccc69a48b 216 return -1;
shliu1 0:c89ccc69a48b 217 }
shliu1 0:c89ccc69a48b 218
shliu1 0:c89ccc69a48b 219 if (inspect_each_setting(pbuf) < 0)
shliu1 0:c89ccc69a48b 220 {
shliu1 0:c89ccc69a48b 221 printf("Unsupported parameters.\r\n");
shliu1 0:c89ccc69a48b 222 return -1;
shliu1 0:c89ccc69a48b 223 }
shliu1 0:c89ccc69a48b 224
shliu1 0:c89ccc69a48b 225 if (web_cmd_params[0].value > 0)
shliu1 0:c89ccc69a48b 226 {
shliu1 0:c89ccc69a48b 227 int port = web_cmd_params[0].value - 1;
shliu1 0:c89ccc69a48b 228 port_config[port].baud = web_cmd_params[1].value;
shliu1 0:c89ccc69a48b 229 port_config[port].data = web_cmd_params[2].value;
shliu1 0:c89ccc69a48b 230 port_config[port].stop = web_cmd_params[4].value;
shliu1 0:c89ccc69a48b 231 port_config[port].parity = (mbed::SerialBase::Parity) web_cmd_params[3].value;
shliu1 0:c89ccc69a48b 232 port_config[port].mode = (E_NetMode) web_cmd_params[5].value;
shliu1 0:c89ccc69a48b 233
shliu1 0:c89ccc69a48b 234 if (web_cmd_params[6].str != NULL)
shliu1 0:c89ccc69a48b 235 {
shliu1 0:c89ccc69a48b 236 strncpy(port_config[port].server_addr, web_cmd_params[6].str, MAX_SERVER_ADDRESS_SIZE);
shliu1 0:c89ccc69a48b 237 port_config[port].server_addr[MAX_SERVER_ADDRESS_SIZE] = '\0';
shliu1 0:c89ccc69a48b 238 }
shliu1 0:c89ccc69a48b 239
shliu1 0:c89ccc69a48b 240 port_config[port].server_port = web_cmd_params[7].value;
shliu1 0:c89ccc69a48b 241
shliu1 0:c89ccc69a48b 242 printf("Save Serial Config setting...\r\n");
shliu1 0:c89ccc69a48b 243 fp = fopen(SER_CONFIG_FILE, "w");
shliu1 0:c89ccc69a48b 244 if (fp != NULL)
shliu1 0:c89ccc69a48b 245 {
shliu1 0:c89ccc69a48b 246 char pBuf[2] = {'N', 'T'};
shliu1 0:c89ccc69a48b 247 printf("Write config file to SD card.\r\n");
shliu1 0:c89ccc69a48b 248 fwrite(pBuf, 2, 1, fp);
shliu1 0:c89ccc69a48b 249 fwrite(port_config, sizeof(port_config), 1, fp);
shliu1 0:c89ccc69a48b 250 fclose(fp);
shliu1 0:c89ccc69a48b 251 }
shliu1 0:c89ccc69a48b 252 else
shliu1 0:c89ccc69a48b 253 {
shliu1 0:c89ccc69a48b 254 printf("Can't write to SD card.\r\n");
shliu1 0:c89ccc69a48b 255 }
shliu1 0:c89ccc69a48b 256 }
shliu1 0:c89ccc69a48b 257 else if (web_cmd_params[8].value > 0)
shliu1 0:c89ccc69a48b 258 {
shliu1 0:c89ccc69a48b 259 net_config.mode = (E_IPMode)web_cmd_params[9].value;
shliu1 0:c89ccc69a48b 260 if (web_cmd_params[10].str != NULL)
shliu1 0:c89ccc69a48b 261 {
shliu1 0:c89ccc69a48b 262 strncpy(net_config.ip, web_cmd_params[10].str, MAX_IPV4_ADDRESS_SIZE);
shliu1 0:c89ccc69a48b 263 net_config.ip[MAX_IPV4_ADDRESS_SIZE] = '\0';
shliu1 0:c89ccc69a48b 264 }
shliu1 0:c89ccc69a48b 265 if (web_cmd_params[11].str != NULL)
shliu1 0:c89ccc69a48b 266 {
shliu1 0:c89ccc69a48b 267 strncpy(net_config.mask, web_cmd_params[11].str, MAX_IPV4_ADDRESS_SIZE);
shliu1 0:c89ccc69a48b 268 net_config.mask[MAX_IPV4_ADDRESS_SIZE] = '\0';
shliu1 0:c89ccc69a48b 269 }
shliu1 0:c89ccc69a48b 270 if (web_cmd_params[12].str != NULL)
shliu1 0:c89ccc69a48b 271 {
shliu1 0:c89ccc69a48b 272 strncpy(net_config.gateway, web_cmd_params[12].str, MAX_IPV4_ADDRESS_SIZE);
shliu1 0:c89ccc69a48b 273 net_config.gateway[MAX_IPV4_ADDRESS_SIZE] = '\0';
shliu1 0:c89ccc69a48b 274 }
shliu1 0:c89ccc69a48b 275 printf("Save Network Config setting...\r\n");
shliu1 0:c89ccc69a48b 276 fp = fopen(NET_CONFIG_FILE, "w");
shliu1 0:c89ccc69a48b 277 if (fp != NULL)
shliu1 0:c89ccc69a48b 278 {
shliu1 0:c89ccc69a48b 279 char pBuf[2] = {'N', 'T'};
shliu1 0:c89ccc69a48b 280 printf("Write config file to SD card.\r\n");
shliu1 0:c89ccc69a48b 281 fwrite(pBuf, 2, 1, fp);
shliu1 0:c89ccc69a48b 282 fwrite(&net_config, sizeof(net_config), 1, fp);
shliu1 0:c89ccc69a48b 283 fclose(fp);
shliu1 0:c89ccc69a48b 284 }
shliu1 0:c89ccc69a48b 285 else
shliu1 0:c89ccc69a48b 286 {
shliu1 0:c89ccc69a48b 287 printf("Can't write to SD card.\r\n");
shliu1 0:c89ccc69a48b 288 }
shliu1 0:c89ccc69a48b 289 }
shliu1 0:c89ccc69a48b 290
shliu1 0:c89ccc69a48b 291 return 0;
shliu1 0:c89ccc69a48b 292 }
shliu1 0:c89ccc69a48b 293
shliu1 0:c89ccc69a48b 294
shliu1 0:c89ccc69a48b 295 void send_web_data(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 296 {
shliu1 0:c89ccc69a48b 297 char *p = pbuf;
shliu1 0:c89ccc69a48b 298 int size = strlen(pbuf);
shliu1 0:c89ccc69a48b 299
shliu1 0:c89ccc69a48b 300 while(size > 0)
shliu1 0:c89ccc69a48b 301 {
shliu1 0:c89ccc69a48b 302 int ret = socket->send(p, size);
shliu1 0:c89ccc69a48b 303 if (ret < 0)
shliu1 0:c89ccc69a48b 304 break;
shliu1 0:c89ccc69a48b 305
shliu1 0:c89ccc69a48b 306 p += ret;
shliu1 0:c89ccc69a48b 307 size -= ret;
shliu1 0:c89ccc69a48b 308 }
shliu1 0:c89ccc69a48b 309 }
shliu1 0:c89ccc69a48b 310
shliu1 0:c89ccc69a48b 311 void send_web_header(TCPSocket *socket, int num)
shliu1 0:c89ccc69a48b 312 {
shliu1 0:c89ccc69a48b 313 static const char *webpage_header[] = {
shliu1 0:c89ccc69a48b 314 PAGE_HEADER,
shliu1 0:c89ccc69a48b 315 PAGE_HEADER_FOR_REFRESH
shliu1 0:c89ccc69a48b 316 };
shliu1 0:c89ccc69a48b 317
shliu1 0:c89ccc69a48b 318 send_web_data(socket, (char *)webpage_header[num]);
shliu1 0:c89ccc69a48b 319 }
shliu1 0:c89ccc69a48b 320
shliu1 0:c89ccc69a48b 321 const char *status_messages[] = {
shliu1 0:c89ccc69a48b 322 "\r\n",
shliu1 0:c89ccc69a48b 323 "Configuration update successfully. </p>\r\n",
shliu1 0:c89ccc69a48b 324 "Configuration setting error.</p>\r\n",
shliu1 0:c89ccc69a48b 325 "Reset Configuration successfully. </p>\r\n",
shliu1 0:c89ccc69a48b 326 "Web page will reload automatically after 10 seconds. </p>\r\n",
shliu1 0:c89ccc69a48b 327 "Unknown settings. </p>\r\n"
shliu1 0:c89ccc69a48b 328 };
shliu1 0:c89ccc69a48b 329
shliu1 0:c89ccc69a48b 330 void send_web_body(TCPSocket *socket, int num)
shliu1 0:c89ccc69a48b 331 {
shliu1 0:c89ccc69a48b 332 int i;
shliu1 0:c89ccc69a48b 333 static char pbuf[3072];
shliu1 0:c89ccc69a48b 334
shliu1 0:c89ccc69a48b 335 /* send page body */
shliu1 0:c89ccc69a48b 336 send_web_data(socket, (char *)"<body>Serial to Ethernet Configuration </p>\r\n");
shliu1 0:c89ccc69a48b 337
shliu1 0:c89ccc69a48b 338 /* send status message */
shliu1 0:c89ccc69a48b 339 send_web_data(socket, (char *)status_messages[num]);
shliu1 0:c89ccc69a48b 340
shliu1 0:c89ccc69a48b 341 send_web_data(socket, (char *)PAGE_RESTART_FORM);
shliu1 0:c89ccc69a48b 342 send_web_data(socket, (char *)PAGE_RESETCONF_FORM);
shliu1 0:c89ccc69a48b 343
shliu1 0:c89ccc69a48b 344 /* send network configuration form */
shliu1 0:c89ccc69a48b 345 send_web_data(socket, (char *)PAGE_CONFIG_FORM_HEAD);
shliu1 0:c89ccc69a48b 346
shliu1 0:c89ccc69a48b 347 sprintf(pbuf, PAGE_CONFIG_NET_FORM,
shliu1 0:c89ccc69a48b 348 (net_config.mode == IP_STATIC_MODE)?" selected":"",
shliu1 0:c89ccc69a48b 349 (net_config.mode == IP_DHCP_MODE)?" selected":"",
shliu1 0:c89ccc69a48b 350 net_config.ip,
shliu1 0:c89ccc69a48b 351 net_config.mask,
shliu1 0:c89ccc69a48b 352 net_config.gateway);
shliu1 0:c89ccc69a48b 353
shliu1 0:c89ccc69a48b 354 send_web_data(socket, pbuf);
shliu1 0:c89ccc69a48b 355 send_web_data(socket, (char *)"<br><input type=\"submit\" value=\"Config Net\"> </form><br>\r\n");
shliu1 0:c89ccc69a48b 356
shliu1 0:c89ccc69a48b 357 /* send serial ports configuration form */
shliu1 0:c89ccc69a48b 358 send_web_data(socket, (char *)PAGE_CONFIG_FORM_HEAD);
shliu1 0:c89ccc69a48b 359 for(i=0; i<MAX_UART_PORTS; i++)
shliu1 0:c89ccc69a48b 360 {
shliu1 0:c89ccc69a48b 361 S_PORT_CONFIG *s = &port_config[i];
shliu1 0:c89ccc69a48b 362
shliu1 0:c89ccc69a48b 363 sprintf(pbuf, "Serial port #%d<br>\r\n", i+1);
shliu1 0:c89ccc69a48b 364 send_web_data(socket, pbuf);
shliu1 0:c89ccc69a48b 365 sprintf(pbuf, PAGE_CONFIG_SERIAL_FORM,
shliu1 0:c89ccc69a48b 366 i+1,
shliu1 0:c89ccc69a48b 367 (s->baud == 115200)?" selected":"",
shliu1 0:c89ccc69a48b 368 (s->baud == 9600)?" selected":"",
shliu1 0:c89ccc69a48b 369 (s->baud == 4800)?" selected":"",
shliu1 0:c89ccc69a48b 370 (s->baud == 2400)?" selected":"",
shliu1 0:c89ccc69a48b 371 (s->data == 8)?" selected":"",
shliu1 0:c89ccc69a48b 372 (s->data == 7)?" selected":"",
shliu1 0:c89ccc69a48b 373 (s->data == 6)?" selected":"",
shliu1 0:c89ccc69a48b 374 (s->data == 5)?" selected":"",
shliu1 0:c89ccc69a48b 375 (s->parity == 0)?" selected":"",
shliu1 0:c89ccc69a48b 376 (s->parity == 1)?" selected":"",
shliu1 0:c89ccc69a48b 377 (s->parity == 2)?" selected":"",
shliu1 0:c89ccc69a48b 378 (s->stop == 1)?" selected":"",
shliu1 0:c89ccc69a48b 379 (s->stop == 2)?" selected":"",
shliu1 0:c89ccc69a48b 380 (s->mode == 0)?" selected":"",
shliu1 0:c89ccc69a48b 381 (s->mode == 1)?" selected":"",
shliu1 0:c89ccc69a48b 382 s->server_addr,
shliu1 0:c89ccc69a48b 383 s->server_port);
shliu1 0:c89ccc69a48b 384
shliu1 0:c89ccc69a48b 385 send_web_data(socket, pbuf);
shliu1 0:c89ccc69a48b 386
shliu1 0:c89ccc69a48b 387 #if MAX_UART_PORTS == 1
shliu1 0:c89ccc69a48b 388 sprintf(pbuf, "<br><input type=\"submit\" value=\"Config Port\"> </form><br>\r\n");
shliu1 0:c89ccc69a48b 389 #else
shliu1 0:c89ccc69a48b 390 sprintf(pbuf, "<br><input type=\"submit\" value=\"Config Port #%d\"> </form><br>\r\n", i);
shliu1 0:c89ccc69a48b 391 #endif
shliu1 0:c89ccc69a48b 392 send_web_data(socket, pbuf);
shliu1 0:c89ccc69a48b 393 }
shliu1 0:c89ccc69a48b 394
shliu1 0:c89ccc69a48b 395 /* send end of page body */
shliu1 0:c89ccc69a48b 396 send_web_data(socket, (char *)"</body></html>\r\n");
shliu1 0:c89ccc69a48b 397 }
shliu1 0:c89ccc69a48b 398
shliu1 0:c89ccc69a48b 399 void process_http_index_html(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 400 {
shliu1 0:c89ccc69a48b 401 send_web_header(socket, 0);
shliu1 0:c89ccc69a48b 402 send_web_body(socket, 0);
shliu1 0:c89ccc69a48b 403 }
shliu1 0:c89ccc69a48b 404
shliu1 0:c89ccc69a48b 405 void process_http_restart_html(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 406 {
shliu1 0:c89ccc69a48b 407 send_web_header(socket, 1);
shliu1 0:c89ccc69a48b 408 send_web_body(socket, 4);
shliu1 0:c89ccc69a48b 409
shliu1 0:c89ccc69a48b 410 printf("Restart system...\r\n");
shliu1 0:c89ccc69a48b 411 wait(1); // wait 1 second
shliu1 0:c89ccc69a48b 412 // SYS_ResetCPU();
shliu1 0:c89ccc69a48b 413 SYS_ResetChip();
shliu1 0:c89ccc69a48b 414 wait(100);
shliu1 0:c89ccc69a48b 415 }
shliu1 0:c89ccc69a48b 416
shliu1 0:c89ccc69a48b 417 void process_http_resetconf_html(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 418 {
shliu1 0:c89ccc69a48b 419 if (SD_Card_Mounted)
shliu1 0:c89ccc69a48b 420 {
shliu1 0:c89ccc69a48b 421 remove(SER_CONFIG_FILE);
shliu1 0:c89ccc69a48b 422 remove(NET_CONFIG_FILE);
shliu1 0:c89ccc69a48b 423 printf("Deleted config files.\r\n");
shliu1 0:c89ccc69a48b 424 }
shliu1 0:c89ccc69a48b 425 else
shliu1 0:c89ccc69a48b 426 {
shliu1 0:c89ccc69a48b 427 printf("SD card doesn't exist.\r\n");
shliu1 0:c89ccc69a48b 428 }
shliu1 0:c89ccc69a48b 429
shliu1 0:c89ccc69a48b 430 send_web_header(socket, 0);
shliu1 0:c89ccc69a48b 431 send_web_body(socket, 3);
shliu1 0:c89ccc69a48b 432 }
shliu1 0:c89ccc69a48b 433
shliu1 0:c89ccc69a48b 434 void process_http_config_html(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 435 {
shliu1 0:c89ccc69a48b 436 send_web_header(socket, 0);
shliu1 0:c89ccc69a48b 437
shliu1 0:c89ccc69a48b 438 if (process_settings(pbuf) >= 0)
shliu1 0:c89ccc69a48b 439 send_web_body(socket, 1);
shliu1 0:c89ccc69a48b 440 else
shliu1 0:c89ccc69a48b 441 send_web_body(socket, 2);
shliu1 0:c89ccc69a48b 442 }
shliu1 0:c89ccc69a48b 443
shliu1 0:c89ccc69a48b 444 void process_http_err_html(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 445 {
shliu1 0:c89ccc69a48b 446 send_web_header(socket, 0);
shliu1 0:c89ccc69a48b 447 send_web_body(socket, 5);
shliu1 0:c89ccc69a48b 448 }
shliu1 0:c89ccc69a48b 449
shliu1 0:c89ccc69a48b 450 void process_http_get_request(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 451 {
shliu1 0:c89ccc69a48b 452 int i;
shliu1 0:c89ccc69a48b 453
shliu1 0:c89ccc69a48b 454 for(i=0; i<MAX_WEB_GET_PAGE_SETTINGS; i++)
shliu1 0:c89ccc69a48b 455 {
shliu1 0:c89ccc69a48b 456 int len = strlen(web_get_pages_setings[i].vhtmlpage);
shliu1 0:c89ccc69a48b 457 if (strncmp(pbuf, web_get_pages_setings[i].vhtmlpage, len) == 0)
shliu1 0:c89ccc69a48b 458 {
shliu1 0:c89ccc69a48b 459 if (web_get_pages_setings[i].pfn != NULL)
shliu1 0:c89ccc69a48b 460 {
shliu1 0:c89ccc69a48b 461 web_get_pages_setings[i].pfn(socket, pbuf+len);
shliu1 0:c89ccc69a48b 462 break;
shliu1 0:c89ccc69a48b 463 }
shliu1 0:c89ccc69a48b 464 }
shliu1 0:c89ccc69a48b 465 }
shliu1 0:c89ccc69a48b 466 }
shliu1 0:c89ccc69a48b 467
shliu1 0:c89ccc69a48b 468 void process_http_post_request(TCPSocket *socket, char *pbuf)
shliu1 0:c89ccc69a48b 469 {
shliu1 0:c89ccc69a48b 470 }
shliu1 0:c89ccc69a48b 471
shliu1 0:c89ccc69a48b 472 void process_http_request(TCPSocket *socket)
shliu1 0:c89ccc69a48b 473 {
shliu1 0:c89ccc69a48b 474 char w_buf[2048];
shliu1 0:c89ccc69a48b 475 int w_len;
shliu1 0:c89ccc69a48b 476
shliu1 0:c89ccc69a48b 477 w_len = socket->recv(w_buf, 2047);
shliu1 0:c89ccc69a48b 478 if (w_len <= 0)
shliu1 0:c89ccc69a48b 479 return;
shliu1 0:c89ccc69a48b 480
shliu1 0:c89ccc69a48b 481 w_buf[w_len] = '\0';
shliu1 0:c89ccc69a48b 482 printf("\n\r---DUMP HTTP DATA (%d)---\r\n%s\r\n---END OF DUMP---\r\n", w_len, w_buf);
shliu1 0:c89ccc69a48b 483
shliu1 0:c89ccc69a48b 484 if (strncmp(w_buf, "GET ", 4) == 0)
shliu1 0:c89ccc69a48b 485 {
shliu1 0:c89ccc69a48b 486 process_http_get_request(socket, w_buf+4);
shliu1 0:c89ccc69a48b 487 }
shliu1 0:c89ccc69a48b 488 else if (strncmp(w_buf, "POST ", 5) == 0)
shliu1 0:c89ccc69a48b 489 {
shliu1 0:c89ccc69a48b 490 process_http_post_request(socket, w_buf+5);
shliu1 0:c89ccc69a48b 491 }
shliu1 0:c89ccc69a48b 492 else
shliu1 0:c89ccc69a48b 493 {
shliu1 0:c89ccc69a48b 494 process_http_get_request(socket, (char *)"/ ");
shliu1 0:c89ccc69a48b 495 }
shliu1 0:c89ccc69a48b 496 }
shliu1 0:c89ccc69a48b 497
shliu1 0:c89ccc69a48b 498 void start_httpd(void)
shliu1 0:c89ccc69a48b 499 {
shliu1 0:c89ccc69a48b 500 TCPServer http_server;
shliu1 0:c89ccc69a48b 501 TCPSocket http_socket;
shliu1 0:c89ccc69a48b 502 SocketAddress http_address;
shliu1 0:c89ccc69a48b 503
shliu1 0:c89ccc69a48b 504 if (http_server.open(&eth) < 0)
shliu1 0:c89ccc69a48b 505 {
shliu1 0:c89ccc69a48b 506 printf("http server can't open.\r\n");
shliu1 0:c89ccc69a48b 507 return;
shliu1 0:c89ccc69a48b 508 }
shliu1 0:c89ccc69a48b 509 if (http_server.bind(eth.get_ip_address(), 80) < 0)
shliu1 0:c89ccc69a48b 510 {
shliu1 0:c89ccc69a48b 511 printf("http server can't bind address and port.\r\n");
shliu1 0:c89ccc69a48b 512 return;
shliu1 0:c89ccc69a48b 513 }
shliu1 0:c89ccc69a48b 514 if (http_server.listen(1) < 0)
shliu1 0:c89ccc69a48b 515 {
shliu1 0:c89ccc69a48b 516 printf("http server can't listen.\r\n");
shliu1 0:c89ccc69a48b 517 return;
shliu1 0:c89ccc69a48b 518 }
shliu1 0:c89ccc69a48b 519
shliu1 0:c89ccc69a48b 520 printf("Start http server...\r\n");
shliu1 0:c89ccc69a48b 521
shliu1 0:c89ccc69a48b 522 while(1)
shliu1 0:c89ccc69a48b 523 {
shliu1 0:c89ccc69a48b 524 if (http_server.accept(&http_socket, &http_address) < 0)
shliu1 0:c89ccc69a48b 525 {
shliu1 0:c89ccc69a48b 526 printf("http server fail to accept connection.\r\n");
shliu1 0:c89ccc69a48b 527 return;
shliu1 0:c89ccc69a48b 528 }
shliu1 0:c89ccc69a48b 529
shliu1 0:c89ccc69a48b 530 printf("http from %s:%d ...\r\n", http_address.get_ip_address(), http_address.get_port());
shliu1 0:c89ccc69a48b 531 process_http_request(&http_socket);
shliu1 0:c89ccc69a48b 532 http_socket.close();
shliu1 0:c89ccc69a48b 533 }
shliu1 0:c89ccc69a48b 534 }
shliu1 0:c89ccc69a48b 535
shliu1 0:c89ccc69a48b 536 #endif