Webserver only w/o any other functions, single thread. Running on STM32F013+W5500
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
Diff: main.cpp
- Revision:
- 29:bc052f283ada
- Parent:
- 28:00c0c20d03c1
- Child:
- 30:15e23257e786
--- a/main.cpp Mon Dec 29 23:38:26 2014 +0000 +++ b/main.cpp Thu Jan 08 23:21:40 2015 +0000 @@ -166,16 +166,19 @@ uint16_t u16tcp_server_port; // directly loaded from eeprom uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server -char tcp_receiving_buffer[256]; -char tcp_sending_buffer[256]; // socket buffer -char udp_receiving_buffer[256]; -char rpc_outbuf[256]; // rpc output buffer +#define NET_BUF_LEN 256 +#define RPC_BUF_LEN 256 +char tcp_receiving_buffer[NET_BUF_LEN]; +char tcp_sending_buffer[NET_BUF_LEN]; // socket buffer +char udp_receiving_buffer[NET_BUF_LEN]; +char rpc_outbuf[RPC_BUF_LEN]; // rpc output buffer /* * Protocol */ // Commands +#define DEVICE_DESCRIPTION "NNIO" #define DEVICE_CONFIG_CODE "NNCF" #define DEVICE_CONTROL_CODE "NNIO" @@ -395,6 +398,7 @@ n = process_control_command(tcp_receiving_buffer, n); // send rpc reply back to client, NNIO protocol always returns 0 if (n > 0) { + rpc_outbuf[n] = '\0'; tcp_client.send_all(rpc_outbuf, strlen(rpc_outbuf)); } } // end loop if no data received within timeout @@ -416,7 +420,7 @@ discovery_mode_flag = true; DBG("Received discovery command"); char str[50]; - sprintf(str, "%s%s%s", DEVICE_CONFIG_CODE, eth.getMACAddress(), eth.getIPAddress()); + sprintf(str, "%s%s%s", DEVICE_DESCRIPTION, eth.getMACAddress(), eth.getIPAddress()); udp_server.sendTo(ep_udp_client, str, strlen(str)); } // NNCFDS else if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL)) { @@ -437,10 +441,12 @@ n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer)); } // while (n > 0), config loop } // if (config_mode_flag) - else if ((n > 0) && (!discovery_mode_flag)) { // process control packages sent using UDP + // process control packages sent using UDP + else if ((n > 0) && (!discovery_mode_flag)) { n = process_control_command(udp_receiving_buffer, n); // send rpc reply back to client, NNIO protocol always returns 0 if (n > 0) { + rpc_outbuf[n] = '\0'; udp_server.sendTo(ep_udp_client, rpc_outbuf, strlen(rpc_outbuf)); } } @@ -561,7 +567,7 @@ { char* received_frame; int pos; - char inbuf[256]; + char inbuf[RPC_BUF_LEN]; DBG("Processing control command"); @@ -571,14 +577,34 @@ inbuf[len+1] = '\n'; inbuf[len+2] = '\0'; // add CR-LF if ((len > 0) && (inbuf[0] == '/')) { + char obj_name[32]; bool result; + + // find RPC object name + for (int i = 1; i < strlen(inbuf); i++) { + if (inbuf[i] != '/') { + obj_name[i-1] = inbuf[i]; + } + else { + obj_name[i-1] = '\0'; + break; + } + } DBG("Rpc command = %s", inbuf); + /* + * execute RPC command, return reply length and reply in rpc_outbuf + */ result = RPC::call(inbuf, rpc_outbuf); if (result) { - // calculate length of rpc_outbuf - int i = strlen(rpc_outbuf); - DBG("Rpc reply (%d bytes): %s", i, rpc_outbuf); - return i; // return length of rpc_outbuf + // re-arrange output buffer as following: object_name:output_value + strcpy(inbuf, rpc_outbuf); // use inbuf as temp + strcpy(rpc_outbuf, obj_name); // rpc object name + strcat(rpc_outbuf, ":"); + strcat(rpc_outbuf, inbuf); // concat rpc reply + strcat(rpc_outbuf, "\r\n"); // CR-LF + int j = strlen(rpc_outbuf); + DBG("Reply of rpc command on \"%s\" (%d bytes): %s", obj_name, j, rpc_outbuf); + return j; // return length of rpc_outbuf } else { ERR("Failed: %s", inbuf);