Transfer data between UART ports and Ethernet.

Dependencies:   BufferedSerial

Transfer data between UART ports and Ethernet.

  • Support multiple UART ports.
  • Support fixed IP address and DHCP.
  • Support TCP server and TCP client modes.
  • Support a simple web server for UART and Ethernet configuration.
Committer:
morgandu
Date:
Fri Feb 26 06:30:37 2021 +0000
Revision:
8:e9248126f512
Parent:
7:cbb5a2a2a0d2
Fix code to compliant Mbed OS 6 but still set to Mbed OS 5 as default.; Remove BufferedSerial library if switch to Mbed OS 6 before compile.

Who changed what in which revision?

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