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
Revision 30:15e23257e786, committed 2015-01-11
- Comitter:
- olympux
- Date:
- Sun Jan 11 13:53:53 2015 +0000
- Parent:
- 29:bc052f283ada
- Child:
- 31:2e4b6de6c2f3
- Commit message:
- Clean code
Changed in this revision
--- a/main.cpp Thu Jan 08 23:21:40 2015 +0000 +++ b/main.cpp Sun Jan 11 13:53:53 2015 +0000 @@ -1,7 +1,6 @@ /* -* -* Alarm and Monitoring application -*/ + * Firmware supports NNIO and RPC protocols + */ #include "mbed.h" #include "rtos.h" #include "mbed_rpc.h" @@ -35,15 +34,93 @@ /* * Hardware defines */ -#define ST_NUCLEO // hardware pin mapping - -#ifdef ST_NUCLEO // Ethernet SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk EthernetInterface eth(&spi, PA_4, PC_9); // spi, cs, reset -#endif + +/* +* EEPROM section +*/ +// Virtual address defined by the user: 0xFFFF value is prohibited +uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr + 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet + 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway + 0x4212, // TCP server port, not used + 0x5212, // UDP server port, not used + 0x8888, // 1st run? 0xA5A5 = configured + 0x6212, 0x6313, 0x6414, // MAC + + // this section is for the TCP server that this device connects to in TCP client mode + 0x7212, 0x7313, // 0xA5A5 = auto transmit status, time period + 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address + 0x9212, // TCP server port + + // this section is for selecting protocol, not used + 0xA212, // 0xA5A5 = enable TCP server + 0xA313, // 0xA5A5 = eanble TCP client + 0xA414 // 0xA5A5 = enable UDP server + }; + + +/* +* Network configuration +*/ +#define TCP_SERVER +#define TCP_CLIENT +#define UDP_SERVER +//#define UDP_CLIENT +#define NTP + +#define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 // timeout for local tcp server wait for a remote client +#define TCP_SERVER_RECEIVE_TIMEOUT 2000 // timeout for local tcp server wait to receive from remote client +#define TCP_CLIENT_RECEIVE_TIMEOUT 200 // timeout for local tcp client try to connect remote server +#define UDP_SERVER_RECEIVE_TIMEOUT 100 // timeout for checking config command + -//Use the RPC enabled wrapped class - see RpcClasses.h for more info +// TCP server function +TCPSocketServer tcp_server; +TCPSocketConnection tcp_client; +// TCP client function +TCPSocketConnection tcp_sock; +// UDP server +UDPSocket udp_server; +Endpoint ep_udp_client; +// NTP +NTPClient ntp; + + +/* +* Variables for network configuration, TCP server +*/ +uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits +uint16_t u16mac_addr[3], u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; // 16-bits, directly loaded from eeprom +char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx +uint16_t configured_ip = 0; // static ip configured flag + +const uint16_t tcp_server_local_port = 10000; // fixed, change to 7000 if internet required +const uint16_t udp_server_local_port = 11000; // fixed + +// TCP client: this section is used for the TCP server that this device connects to in TCP client mode +// this device will transmit status every transmit_time_period +uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s +uint16_t u16server_ip_addr[4]; // directly loaded from eeprom +uint8_t u8server_ip_addr[4]; // server ip address in 8-bits +char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr +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 + +#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 + + +/* + * RPC Protocol + * Use the RPC enabled wrapped class - see RpcClasses.h for more info + */ // DigitalIn RpcDigitalIn di0(PB_14, "din0"); RpcDigitalIn di1(PB_12, "din1"); @@ -96,87 +173,8 @@ /* -* EEPROM section -*/ -// Virtual address defined by the user: 0xFFFF value is prohibited -uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr - 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet - 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway - 0x4212, // TCP server port, not used - 0x5212, // UDP server port, not used - 0x8888, // 1st run? 0xA5A5 = configured - 0x6212, 0x6313, 0x6414, // MAC - - // this section is for the TCP server that this device connects to in TCP client mode - 0x7212, 0x7313, // 0xA5 = auto transmit status, time period - 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address - 0x9212, // TCP server port - - // this section is for selecting protocol - 0xA212, // 0xA5A5 = enable TCP server - 0xA313, // 0xA5A5 = eanble TCP client - 0xA414 // 0xA5A5 = enable UDP server - }; - -/* -* Network configuration -*/ -#define TCP_SERVER -#define TCP_CLIENT -#define UDP_SERVER -//#define UDP_CLIENT -#define NTP - -#define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 // timeout for local tcp server wait for a remote client -#define TCP_SERVER_RECEIVE_TIMEOUT 2000 // timeout for local tcp server wait to receive from remote client -#define TCP_CLIENT_RECEIVE_TIMEOUT 200 // timeout for local tcp client try to connect remote server -#define UDP_SERVER_RECEIVE_TIMEOUT 100 // timeout for checking config command - - -// TCP server function -TCPSocketServer tcp_server; -TCPSocketConnection tcp_client; -// TCP client function -TCPSocketConnection tcp_sock; -// UDP server -UDPSocket udp_server; -Endpoint ep_udp_client; -// NTP -NTPClient ntp; - - - -/* -* Variables for network configuration, server -*/ -uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits -uint16_t u16mac_addr[3], u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; // 16-bits, directly loaded from eeprom -char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx -uint16_t configured_ip = 0; // static ip configured flag - -const uint16_t tcp_server_local_port = 10000; // fixed, change to 7000 if internet required -const uint16_t udp_server_local_port = 11000; // fixed - -// TCP client: this section is used for the TCP server that this device connects to in TCP client mode -// this device will transmit status every transmit_time_period -uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s -uint16_t u16server_ip_addr[4]; // directly loaded from eeprom -uint8_t u8server_ip_addr[4]; // server ip address in 8-bits -char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr -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 - -#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 -*/ + * NNIO Protocol + */ // Commands #define DEVICE_DESCRIPTION "NNIO" #define DEVICE_CONFIG_CODE "NNCF" @@ -336,9 +334,7 @@ * Network loop processor */ while (true) { -#ifdef TCP_CLIENT - // FOR AUTO TRANSMIT DEVICE STATUS - //if ((u16enable_tcp_client == 0xA5A5) && (auto_transmit_flag == 0xA5A5)) { +#ifdef TCP_CLIENT // auto update device status to a remote TCP server if (auto_transmit_flag == 0xA5A5) { // connect to TCP server if required if (!tcp_sock.is_connected()) { @@ -373,11 +369,8 @@ #endif -// FOR INTERFACING -#ifdef TCP_SERVER - // no tcp client connected - //if ((u16enable_tcp_server == 0xA5A5) && (!tcp_client.is_connected())) { - //if (!tcp_client.is_connected()) { +#ifdef TCP_SERVER // control and monitor from a remote TCP client, both NNIO and RPC-style + // no tcp client connected{ if (1) { // wait for client within timeout ret = tcp_server.accept(tcp_client); @@ -407,7 +400,7 @@ } // if tcp server enabled && no client connected #endif -#ifdef UDP_SERVER +#ifdef UDP_SERVER // configuration and control, both NNIO and RPC-style bool discovery_mode_flag, config_mode_flag; n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer)); @@ -462,14 +455,13 @@ { DBG("Processing configuration command"); - // process received data // a configuration command always starts with NN if ((received_buffer[0] == 'N') && (received_buffer[1] == 'N') && (received_buffer[2] == 'C') && (received_buffer[3] == 'F')) { switch (len) { // length = 6, a QUERY command (discovery command, TCP port, or UDP port) // Format: NNCFDS, NNCFTP, NNCFUP, NNCFTM - case QUERY_NETWORK_CONFIG_CMD_LENGTH: + case QUERY_NETWORK_CONFIG_CMD_LENGTH: { if (strstr(received_buffer, QUERY_IP_CMD) != NULL) { udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress())); } // NNCFIP @@ -518,11 +510,11 @@ udp_server.sendTo(ep_udp_client, str_time, strlen(str_time)); #endif } // NNCFTM - break; - // length = 19, SET NETWORK CONFIGURATION - // Format: 4E 4E 43 46 C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01 - // (NNCF; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1) + } + // length = 19, SET NETWORK CONFIGURATION + // Format: 4E 4E 43 46 C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01 + // (NNCF; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1) case SET_NETWORK_CONFIG_CMD_LENGTH: { // check device id char* id = strstr(received_buffer, DEVICE_CONFIG_CODE); @@ -554,24 +546,33 @@ break; } // switch (n), check configuration command length } // if starts with NNCF, a config command - else { // if not a command, check to see if it is a 6-byte data package - // process 6-byte data package + else { // if not a configuration command } } /* - * Procedure to process receiving protocol, which includes command to control outputs - * Return: 0 if NNIO protocol; length of rpc output buffer if rpc; -1 if rpc failed + * Procedure to process receiving protocol, which includes command to control outputs. + * Support both NNIO and RPC-style commands. + * RPC always starts with '/'. + * Return: + * 0 if NNIO protocol; + * length of rpc output buffer if rpc; + * -1 if rpc failed */ int process_control_command(char* received_buffer, int len) { char* received_frame; + bool rpc_style; int pos; char inbuf[RPC_BUF_LEN]; DBG("Processing control command"); - // check RPC protocol + /* + * This section is for RPC-style command + */ + // check if it is a RPC-style command + rpc_style = false; strncpy(inbuf, received_buffer, len); inbuf[len] = '\r'; // use inbuf for RPC protocol inbuf[len+1] = '\n'; @@ -580,6 +581,7 @@ char obj_name[32]; bool result; + rpc_style = true; // find RPC object name for (int i = 1; i < strlen(inbuf); i++) { if (inbuf[i] != '/') { @@ -615,7 +617,7 @@ /* * This section below is for NNIO protocol */ - while (len >= RECEIVING_PROTOCOL_LENGTH) { + while ((!rpc_style) && (len >= RECEIVING_PROTOCOL_LENGTH)) { // find device ID DBG("Checking device ID..."); char* id = strstr(received_buffer, DEVICE_CONTROL_CODE);
--- a/my_eeprom_funcs.lib Thu Jan 08 23:21:40 2015 +0000 +++ b/my_eeprom_funcs.lib Sun Jan 11 13:53:53 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/olympux/code/my_eeprom_funcs/#b5fbb35bbc02 +http://mbed.org/users/olympux/code/my_eeprom_funcs/#613ab276bf37
--- a/readme.txt Thu Jan 08 23:21:40 2015 +0000 +++ b/readme.txt Sun Jan 11 13:53:53 2015 +0000 @@ -11,4 +11,5 @@ + Tested: working with ConfigurationTool v2.0 and AlarmMonitoring v1.1. v1.0 (06/01/2014) - + Added: RPC command replies as following object_name:reply_value \ No newline at end of file + + Added: RPC command replies as following object_name:reply_value + + Modified: clean code in my_eeprom_funcs and main.cpp \ No newline at end of file