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 Chau Vo

Committer:
olympux
Date:
Mon Jun 13 23:06:38 2016 +0000
Revision:
38:f8735ae519aa
Parent:
37:94b847fea94e
Child:
39:083cf93121a9
Supported RPCVariable and RPCFunction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olympux 0:c2eac797face 1 /*
olympux 30:15e23257e786 2 * Firmware supports NNIO and RPC protocols
olympux 30:15e23257e786 3 */
olympux 0:c2eac797face 4 #include "mbed.h"
olympux 27:22f289beceb8 5 #include "rtos.h"
olympux 27:22f289beceb8 6 #include "mbed_rpc.h"
olympux 27:22f289beceb8 7 #include "Arguments.h"
olympux 27:22f289beceb8 8
olympux 12:7c152c0ca4d8 9 #include "eeprom.h"
olympux 0:c2eac797face 10 #include "EthernetInterface.h"
olympux 9:d2534ecf88c6 11 #include "NTPClient.h"
olympux 0:c2eac797face 12
olympux 12:7c152c0ca4d8 13 #include "my_eeprom_funcs.h"
olympux 20:71c7950fdd91 14 #include "Watchdog.h"
olympux 12:7c152c0ca4d8 15
olympux 0:c2eac797face 16
olympux 31:2e4b6de6c2f3 17 /** Debug option
olympux 31:2e4b6de6c2f3 18 *
olympux 31:2e4b6de6c2f3 19 */
olympux 25:48dd18cc147c 20 #if 1
olympux 16:84a5bf7285d0 21 //Enable debug
olympux 16:84a5bf7285d0 22 #include <cstdio>
olympux 31:2e4b6de6c2f3 23 #define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__);
olympux 31:2e4b6de6c2f3 24 #define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__);
olympux 31:2e4b6de6c2f3 25 #define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__);
olympux 16:84a5bf7285d0 26 #else
olympux 16:84a5bf7285d0 27 //Disable debug
olympux 31:2e4b6de6c2f3 28 #define DBG(x, ...)
olympux 16:84a5bf7285d0 29 #define WARN(x, ...)
olympux 31:2e4b6de6c2f3 30 #define ERR(x, ...)
olympux 16:84a5bf7285d0 31 #endif
olympux 16:84a5bf7285d0 32
olympux 16:84a5bf7285d0 33
olympux 2:18f10e7209f4 34 /*
olympux 2:18f10e7209f4 35 * Hardware defines
olympux 0:c2eac797face 36 */
olympux 9:d2534ecf88c6 37 // Ethernet
olympux 0:c2eac797face 38 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
olympux 11:709f90a3b599 39 EthernetInterface eth(&spi, PA_4, PC_9); // spi, cs, reset
olympux 30:15e23257e786 40
olympux 30:15e23257e786 41 /*
olympux 30:15e23257e786 42 * EEPROM section
olympux 30:15e23257e786 43 */
olympux 30:15e23257e786 44 // Virtual address defined by the user: 0xFFFF value is prohibited
olympux 30:15e23257e786 45 uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr
olympux 30:15e23257e786 46 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet
olympux 30:15e23257e786 47 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway
olympux 30:15e23257e786 48 0x4212, // TCP server port, not used
olympux 30:15e23257e786 49 0x5212, // UDP server port, not used
olympux 30:15e23257e786 50 0x8888, // 1st run? 0xA5A5 = configured
olympux 30:15e23257e786 51 0x6212, 0x6313, 0x6414, // MAC
olympux 31:2e4b6de6c2f3 52
olympux 30:15e23257e786 53 // this section is for the TCP server that this device connects to in TCP client mode
olympux 30:15e23257e786 54 0x7212, 0x7313, // 0xA5A5 = auto transmit status, time period
olympux 30:15e23257e786 55 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address
olympux 30:15e23257e786 56 0x9212, // TCP server port
olympux 31:2e4b6de6c2f3 57
olympux 30:15e23257e786 58 // this section is for selecting protocol, not used
olympux 30:15e23257e786 59 0xA212, // 0xA5A5 = enable TCP server
olympux 30:15e23257e786 60 0xA313, // 0xA5A5 = eanble TCP client
olympux 30:15e23257e786 61 0xA414 // 0xA5A5 = enable UDP server
olympux 31:2e4b6de6c2f3 62 };
olympux 31:2e4b6de6c2f3 63
olympux 30:15e23257e786 64
olympux 30:15e23257e786 65 /*
olympux 30:15e23257e786 66 * Network configuration
olympux 30:15e23257e786 67 */
olympux 30:15e23257e786 68 #define TCP_SERVER
olympux 30:15e23257e786 69 #define TCP_CLIENT
olympux 30:15e23257e786 70 #define UDP_SERVER
olympux 30:15e23257e786 71 //#define UDP_CLIENT
olympux 30:15e23257e786 72 #define NTP
olympux 30:15e23257e786 73
olympux 30:15e23257e786 74 #define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 // timeout for local tcp server wait for a remote client
olympux 30:15e23257e786 75 #define TCP_SERVER_RECEIVE_TIMEOUT 2000 // timeout for local tcp server wait to receive from remote client
olympux 30:15e23257e786 76 #define TCP_CLIENT_RECEIVE_TIMEOUT 200 // timeout for local tcp client try to connect remote server
olympux 30:15e23257e786 77 #define UDP_SERVER_RECEIVE_TIMEOUT 100 // timeout for checking config command
olympux 30:15e23257e786 78
olympux 0:c2eac797face 79
olympux 30:15e23257e786 80 // TCP server function
olympux 30:15e23257e786 81 TCPSocketServer tcp_server;
olympux 30:15e23257e786 82 TCPSocketConnection tcp_client;
olympux 30:15e23257e786 83 // TCP client function
olympux 30:15e23257e786 84 TCPSocketConnection tcp_sock;
olympux 30:15e23257e786 85 // UDP server
olympux 30:15e23257e786 86 UDPSocket udp_server;
olympux 30:15e23257e786 87 Endpoint ep_udp_client;
olympux 30:15e23257e786 88 // NTP
olympux 30:15e23257e786 89 NTPClient ntp;
olympux 30:15e23257e786 90
olympux 30:15e23257e786 91
olympux 30:15e23257e786 92 /*
olympux 30:15e23257e786 93 * Variables for network configuration, TCP server
olympux 30:15e23257e786 94 */
olympux 30:15e23257e786 95 uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits
olympux 30:15e23257e786 96 uint16_t u16mac_addr[3], u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; // 16-bits, directly loaded from eeprom
olympux 30:15e23257e786 97 char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx
olympux 30:15e23257e786 98 uint16_t configured_ip = 0; // static ip configured flag
olympux 30:15e23257e786 99
olympux 30:15e23257e786 100 const uint16_t tcp_server_local_port = 10000; // fixed, change to 7000 if internet required
olympux 30:15e23257e786 101 const uint16_t udp_server_local_port = 11000; // fixed
olympux 30:15e23257e786 102
olympux 30:15e23257e786 103 // TCP client: this section is used for the TCP server that this device connects to in TCP client mode
olympux 30:15e23257e786 104 // this device will transmit status every transmit_time_period
olympux 30:15e23257e786 105 uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s
olympux 30:15e23257e786 106 uint16_t u16server_ip_addr[4]; // directly loaded from eeprom
olympux 30:15e23257e786 107 uint8_t u8server_ip_addr[4]; // server ip address in 8-bits
olympux 30:15e23257e786 108 char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr
olympux 30:15e23257e786 109 uint16_t u16tcp_server_port; // directly loaded from eeprom
olympux 30:15e23257e786 110 uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server
olympux 30:15e23257e786 111
olympux 30:15e23257e786 112 #define NET_BUF_LEN 256
olympux 30:15e23257e786 113 char tcp_receiving_buffer[NET_BUF_LEN];
olympux 30:15e23257e786 114 char udp_receiving_buffer[NET_BUF_LEN];
olympux 31:2e4b6de6c2f3 115 char network_output_buffer[NET_BUF_LEN]; // output buffer for TCP/UDP control command
olympux 30:15e23257e786 116
olympux 30:15e23257e786 117
olympux 30:15e23257e786 118 /*
olympux 30:15e23257e786 119 * RPC Protocol
olympux 30:15e23257e786 120 * Use the RPC enabled wrapped class - see RpcClasses.h for more info
olympux 30:15e23257e786 121 */
olympux 27:22f289beceb8 122 // DigitalIn
olympux 35:f5c98e2d6aad 123 RpcDigitalIn di0(PB_14, "di0");
olympux 35:f5c98e2d6aad 124 RpcDigitalIn di1(PB_12, "di1");
olympux 35:f5c98e2d6aad 125 RpcDigitalIn di2(PB_10, "di2");
olympux 35:f5c98e2d6aad 126 RpcDigitalIn di3(PB_1, "di3");
olympux 35:f5c98e2d6aad 127 RpcDigitalIn di4(PB_15, "di4");
olympux 35:f5c98e2d6aad 128 RpcDigitalIn di5(PB_13, "di5");
olympux 35:f5c98e2d6aad 129 RpcDigitalIn di6(PB_11, "di6");
olympux 35:f5c98e2d6aad 130 RpcDigitalIn di7(PB_2, "di7");
olympux 11:709f90a3b599 131 DigitalIn din0(PB_14);
olympux 11:709f90a3b599 132 DigitalIn din1(PB_12);
olympux 11:709f90a3b599 133 DigitalIn din2(PB_10);
olympux 11:709f90a3b599 134 DigitalIn din3(PB_1);
olympux 11:709f90a3b599 135 DigitalIn din4(PB_15);
olympux 11:709f90a3b599 136 DigitalIn din5(PB_13);
olympux 11:709f90a3b599 137 DigitalIn din6(PB_11);
olympux 11:709f90a3b599 138 DigitalIn din7(PB_2);
olympux 27:22f289beceb8 139 // DigitalOut
olympux 35:f5c98e2d6aad 140 RpcDigitalOut do0(PB_3, "do0");
olympux 35:f5c98e2d6aad 141 RpcDigitalOut do1(PB_5, "do1");
olympux 35:f5c98e2d6aad 142 RpcDigitalOut do2(PB_7, "do2");
olympux 35:f5c98e2d6aad 143 RpcDigitalOut do3(PB_9, "do3");
olympux 35:f5c98e2d6aad 144 RpcDigitalOut do4(PD_2, "do4");
olympux 35:f5c98e2d6aad 145 RpcDigitalOut do5(PB_4, "do5");
olympux 35:f5c98e2d6aad 146 RpcDigitalOut do6(PB_6, "do6");
olympux 35:f5c98e2d6aad 147 RpcDigitalOut do7(PB_8, "do7");
olympux 11:709f90a3b599 148 DigitalOut dout0(PB_3);
olympux 11:709f90a3b599 149 DigitalOut dout1(PB_5);
olympux 11:709f90a3b599 150 DigitalOut dout2(PB_7);
olympux 11:709f90a3b599 151 DigitalOut dout3(PB_9);
olympux 11:709f90a3b599 152 DigitalOut dout4(PD_2);
olympux 11:709f90a3b599 153 DigitalOut dout5(PB_4);
olympux 11:709f90a3b599 154 DigitalOut dout6(PB_6);
olympux 11:709f90a3b599 155 DigitalOut dout7(PB_8);
olympux 27:22f289beceb8 156 // AnalogIn
olympux 35:f5c98e2d6aad 157 RpcAnalogIn adc10(PC_0, "ai0"); // adc10
olympux 35:f5c98e2d6aad 158 RpcAnalogIn adc11(PC_1, "ai1"); // adc11
olympux 11:709f90a3b599 159 AnalogIn ain0(PC_0);
olympux 11:709f90a3b599 160 AnalogIn ain1(PC_1);
olympux 27:22f289beceb8 161 // AnalogOut, PWM
olympux 27:22f289beceb8 162 RpcPwmOut pwm11(PA_8, "pwm0"); // pwm11
olympux 27:22f289beceb8 163 RpcPwmOut pwm21(PA_15, "pwm1"); // pwm21
olympux 27:22f289beceb8 164 // Serial
olympux 27:22f289beceb8 165 RpcSerial usart2(USBTX, USBRX, "uart"); // usart2
olympux 27:22f289beceb8 166 Serial uart(USBTX,USBRX);
olympux 27:22f289beceb8 167 // Timer
olympux 35:f5c98e2d6aad 168 RpcTimer timer1("tmr1");
olympux 20:71c7950fdd91 169 // Watchdog
olympux 20:71c7950fdd91 170 Watchdog wdt;
olympux 20:71c7950fdd91 171
olympux 13:bcf840da68fd 172 /*
olympux 30:15e23257e786 173 * NNIO Protocol
olympux 30:15e23257e786 174 */
olympux 9:d2534ecf88c6 175 // Commands
olympux 29:bc052f283ada 176 #define DEVICE_DESCRIPTION "NNIO"
olympux 27:22f289beceb8 177 #define DEVICE_CONFIG_CODE "NNCF"
olympux 27:22f289beceb8 178 #define DEVICE_CONTROL_CODE "NNIO"
olympux 17:88ef7a078095 179
olympux 17:88ef7a078095 180 #define RECEIVING_PROTOCOL_LENGTH 58
olympux 17:88ef7a078095 181 #define SENDING_PROTOCOL_LENGTH 39
olympux 26:09e0dd020900 182 #define QUERY_NETWORK_CONFIG_CMD_LENGTH 6
olympux 17:88ef7a078095 183 #define SET_NETWORK_CONFIG_CMD_LENGTH 19
olympux 26:09e0dd020900 184 #define UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH 12
olympux 17:88ef7a078095 185
olympux 27:22f289beceb8 186 #define QUERY_DISCOVERY_CMD "NNCFDS"
olympux 27:22f289beceb8 187 #define QUERY_IP_CMD "NNCFIP"
olympux 27:22f289beceb8 188 #define QUERY_SUBNET_CMD "NNCFSN"
olympux 27:22f289beceb8 189 #define QUERY_GATEWAY_CMD "NNCFGW"
olympux 27:22f289beceb8 190 #define QUERY_MAC_CMD "NNCFMC"
olympux 27:22f289beceb8 191 #define QUERY_UDP_PORT_CMD "NNCFUP"
olympux 27:22f289beceb8 192 #define QUERY_TCP_PORT_CMD "NNCFTP"
olympux 27:22f289beceb8 193 #define QUERY_UPDATE_TIME_CMD "NNCFTM"
olympux 11:709f90a3b599 194 #define RECEIVING_PROTOCOL_ENABLE_OUTPUT 'O'
olympux 11:709f90a3b599 195 #define QUERY_STATUS_COMMAND 'Q'
olympux 11:709f90a3b599 196 #define DIGITAL_HIGH 'H'
olympux 11:709f90a3b599 197 #define DIGITAL_LOW 'L'
olympux 9:d2534ecf88c6 198
olympux 9:d2534ecf88c6 199
olympux 9:d2534ecf88c6 200 // Positions
olympux 11:709f90a3b599 201 #define RECEIVING_PROTOCOL_ID_POS 0
olympux 9:d2534ecf88c6 202 #define RECEIVING_PROTOCOL_OP_POS 4
olympux 9:d2534ecf88c6 203 #define RECEIVING_PROTOCOL_EN_DO_POS RECEIVING_PROTOCOL_OP_POS + 0
olympux 9:d2534ecf88c6 204 #define RECEIVING_PROTOCOL_EN_A0O_POS RECEIVING_PROTOCOL_OP_POS + 1
olympux 9:d2534ecf88c6 205 #define RECEIVING_PROTOCOL_EN_A1O_POS RECEIVING_PROTOCOL_OP_POS + 2
olympux 9:d2534ecf88c6 206 #define RECEIVING_PROTOCOL_EN_UART_POS RECEIVING_PROTOCOL_OP_POS + 3
olympux 9:d2534ecf88c6 207 #define RECEIVING_PROTOCOL_COMMAND_POS RECEIVING_PROTOCOL_OP_POS + 4
olympux 11:709f90a3b599 208
olympux 11:709f90a3b599 209 #define RECEIVING_PROTOCOL_IP_POS 9
olympux 11:709f90a3b599 210 #define RECEIVING_PROTOCOL_DO_POS 13
olympux 11:709f90a3b599 211 #define RECEIVING_PROTOCOL_A0O_POS 21
olympux 11:709f90a3b599 212 #define RECEIVING_PROTOCOL_A01_POS 23
olympux 11:709f90a3b599 213 #define RECEIVING_PROTOCOL_UART_POS 25
olympux 9:d2534ecf88c6 214
olympux 9:d2534ecf88c6 215
olympux 11:709f90a3b599 216 #define SENDING_PROTOCOL_ID_POS 0
olympux 11:709f90a3b599 217 #define SENDING_PROTOCOL_MAC_POS 4
olympux 11:709f90a3b599 218 #define SENDING_PROTOCOL_IP_POS 10
olympux 11:709f90a3b599 219 #define SENDING_PROTOCOL_DI_POS 14
olympux 11:709f90a3b599 220 #define SENDING_PROTOCOL_DO_POS 22
olympux 11:709f90a3b599 221 #define SENDING_PROTOCOL_AI0_POS 30
olympux 11:709f90a3b599 222 #define SENDING_PROTOCOL_AI1_POS 32
olympux 11:709f90a3b599 223 #define SENDING_PROTOCOL_AO0_POS 34
olympux 11:709f90a3b599 224 #define SENDING_PROTOCOL_AO1_POS 36
olympux 11:709f90a3b599 225 #define SENDING_PROTOCOL_CR_POS 38
olympux 0:c2eac797face 226
olympux 2:18f10e7209f4 227
olympux 2:18f10e7209f4 228 /*
olympux 2:18f10e7209f4 229 * RTOS
olympux 2:18f10e7209f4 230 */
olympux 2:18f10e7209f4 231 struct message_t {
olympux 2:18f10e7209f4 232 int len;
olympux 2:18f10e7209f4 233 char *msg;
olympux 2:18f10e7209f4 234 };
olympux 3:972ed747474c 235 Queue<message_t, 16> uart_queue;
olympux 15:edeb0aed160d 236 Queue<bool, 1> auto_update_queue;
olympux 2:18f10e7209f4 237
olympux 2:18f10e7209f4 238
olympux 26:09e0dd020900 239
olympux 26:09e0dd020900 240 // Prototypes
olympux 26:09e0dd020900 241 int ethernet_init(void);
olympux 28:00c0c20d03c1 242 int process_control_command(char* received_buffer, int len);
olympux 26:09e0dd020900 243 void process_config_command(char* received_buffer, int len);
olympux 26:09e0dd020900 244 void update_digital_outputs(char* buf);
olympux 26:09e0dd020900 245 void update_sending_frame(char* buf);
olympux 26:09e0dd020900 246
olympux 26:09e0dd020900 247
olympux 2:18f10e7209f4 248 /*
olympux 2:18f10e7209f4 249 * Threads
olympux 2:18f10e7209f4 250 */
olympux 31:2e4b6de6c2f3 251 // Timer thread for auto update in TCP client function
olympux 31:2e4b6de6c2f3 252 void auto_update_timer_thread(void const* args)
olympux 31:2e4b6de6c2f3 253 {
olympux 18:ca499a2e7da6 254 bool update_flag = true;
olympux 31:2e4b6de6c2f3 255
olympux 15:edeb0aed160d 256 Thread::wait(500);
olympux 15:edeb0aed160d 257 while(true) {
olympux 15:edeb0aed160d 258 auto_update_queue.put(&update_flag);
olympux 18:ca499a2e7da6 259 Thread::wait(1000*transmit_time_period); // Thread::wait() in ms
olympux 15:edeb0aed160d 260 }
olympux 15:edeb0aed160d 261 }
olympux 15:edeb0aed160d 262
olympux 15:edeb0aed160d 263
olympux 20:71c7950fdd91 264 // WDT reset
olympux 31:2e4b6de6c2f3 265 void wdt_reset_thread(void const* args)
olympux 31:2e4b6de6c2f3 266 {
olympux 20:71c7950fdd91 267 while (true)
olympux 20:71c7950fdd91 268 wdt.Service();
olympux 20:71c7950fdd91 269 }
olympux 20:71c7950fdd91 270
olympux 20:71c7950fdd91 271
olympux 38:f8735ae519aa 272 // These are examples of some variable types that can be modified through RPC.
olympux 38:f8735ae519aa 273 int wheelsOn;
olympux 38:f8735ae519aa 274 char lcdBannerMessage;
olympux 38:f8735ae519aa 275 float speed;
olympux 38:f8735ae519aa 276
olympux 38:f8735ae519aa 277 RPCVariable<int> rpcLights(&wheelsOn, "wheels");
olympux 38:f8735ae519aa 278 RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner");
olympux 38:f8735ae519aa 279 RPCVariable<float> rpcSpeed(&speed, "speed");
olympux 38:f8735ae519aa 280
olympux 38:f8735ae519aa 281 /**
olympux 38:f8735ae519aa 282 * RPC function definitions
olympux 38:f8735ae519aa 283 */
olympux 38:f8735ae519aa 284 // Create a function of the required format
olympux 38:f8735ae519aa 285 void do_blink(Arguments* args, Reply* rep);
olympux 38:f8735ae519aa 286 void do_blink(Arguments* args, Reply* rep){
olympux 38:f8735ae519aa 287 DBG("RPC function called");
olympux 38:f8735ae519aa 288 }
olympux 38:f8735ae519aa 289 // Attach it to an RPC object
olympux 38:f8735ae519aa 290 RPCFunction blink(&do_blink, "blink");
olympux 38:f8735ae519aa 291
olympux 38:f8735ae519aa 292 // Main code
olympux 4:568c97f2a407 293 int main()
olympux 4:568c97f2a407 294 {
olympux 9:d2534ecf88c6 295 int n, ret;
olympux 31:2e4b6de6c2f3 296
olympux 20:71c7950fdd91 297 Thread::wait(500); // turn on delay
olympux 31:2e4b6de6c2f3 298
olympux 4:568c97f2a407 299 /*
olympux 9:d2534ecf88c6 300 * Configure
olympux 4:568c97f2a407 301 */
olympux 11:709f90a3b599 302 uart.baud(115200);
olympux 36:dc6f079777bb 303 DBG("Starting...");
olympux 31:2e4b6de6c2f3 304
olympux 31:2e4b6de6c2f3 305 // check watchdog
olympux 20:71c7950fdd91 306 if (wdt.WatchdogCausedReset())
olympux 20:71c7950fdd91 307 DBG("Watchdog caused reset.");
olympux 20:71c7950fdd91 308 wdt.Configure(4);
olympux 31:2e4b6de6c2f3 309
olympux 6:d054e394fba3 310 /*
olympux 6:d054e394fba3 311 * FLASH
olympux 6:d054e394fba3 312 */
olympux 12:7c152c0ca4d8 313 load_eeprom_network();
olympux 15:edeb0aed160d 314 load_eeprom_tcpserver();
olympux 31:2e4b6de6c2f3 315
olympux 20:71c7950fdd91 316 /*
olympux 20:71c7950fdd91 317 * UI threads
olympux 20:71c7950fdd91 318 */
olympux 33:c906ccc220ba 319 Thread t2(auto_update_timer_thread);
olympux 20:71c7950fdd91 320 Thread t3(wdt_reset_thread);
olympux 31:2e4b6de6c2f3 321
olympux 4:568c97f2a407 322 /*
olympux 4:568c97f2a407 323 * Ethernet
olympux 4:568c97f2a407 324 */
olympux 6:d054e394fba3 325 ret = ethernet_init();
olympux 4:568c97f2a407 326 if (ret) {
olympux 18:ca499a2e7da6 327 ERR("Ethernet initialisation failed. App halted.");
olympux 4:568c97f2a407 328 while (true) {};
olympux 4:568c97f2a407 329 }
olympux 31:2e4b6de6c2f3 330
olympux 23:47ee805435b1 331 Thread::wait(2000); // TCP/UDP stack delay
olympux 12:7c152c0ca4d8 332
olympux 31:2e4b6de6c2f3 333 /*
olympux 31:2e4b6de6c2f3 334 * UDP server
olympux 31:2e4b6de6c2f3 335 * TCP server/client
olympux 31:2e4b6de6c2f3 336 */
olympux 20:71c7950fdd91 337 #ifdef UDP_SERVER
olympux 20:71c7950fdd91 338 ret = udp_server.bind(udp_server_local_port);
olympux 20:71c7950fdd91 339 DBG("UDP server started (sock.bind = %d)...", ret);
olympux 20:71c7950fdd91 340 udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
olympux 20:71c7950fdd91 341 #endif
olympux 20:71c7950fdd91 342
olympux 3:972ed747474c 343 #ifdef TCP_SERVER
olympux 12:7c152c0ca4d8 344 tcp_server.bind(tcp_server_local_port);
olympux 3:972ed747474c 345 tcp_server.listen();
olympux 18:ca499a2e7da6 346 DBG("TCP server started...");
olympux 4:568c97f2a407 347 tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
olympux 3:972ed747474c 348 #endif
olympux 14:18eda020a589 349
olympux 14:18eda020a589 350 #ifdef TCP_CLIENT
olympux 18:ca499a2e7da6 351
olympux 14:18eda020a589 352 #endif
olympux 31:2e4b6de6c2f3 353
olympux 12:7c152c0ca4d8 354 /*
olympux 19:05934ee9ee67 355 * Network loop processor
olympux 12:7c152c0ca4d8 356 */
olympux 0:c2eac797face 357 while (true) {
olympux 30:15e23257e786 358 #ifdef TCP_CLIENT // auto update device status to a remote TCP server
olympux 33:c906ccc220ba 359 if (auto_transmit_flag == DEFAULT_ENABLE_FLAG_VALUE) {
olympux 18:ca499a2e7da6 360 // connect to TCP server if required
olympux 18:ca499a2e7da6 361 if (!tcp_sock.is_connected()) {
olympux 20:71c7950fdd91 362 ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port); // timeout is default in connect() in W5500.h
olympux 18:ca499a2e7da6 363 if (ret > -1) {
olympux 18:ca499a2e7da6 364 DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 31:2e4b6de6c2f3 365 } else {
olympux 18:ca499a2e7da6 366 ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 18:ca499a2e7da6 367 }
olympux 16:84a5bf7285d0 368 }
olympux 31:2e4b6de6c2f3 369
olympux 18:ca499a2e7da6 370 // transmit data if connected
olympux 18:ca499a2e7da6 371 if (tcp_sock.is_connected()) {
olympux 18:ca499a2e7da6 372 osEvent evt = auto_update_queue.get(1); // timeout after 1ms
olympux 18:ca499a2e7da6 373 if (evt.status == osEventMessage) {
olympux 18:ca499a2e7da6 374 DBG("Updating...");
olympux 31:2e4b6de6c2f3 375 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 376 tcp_sock.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 19:05934ee9ee67 377 }
olympux 31:2e4b6de6c2f3 378
olympux 19:05934ee9ee67 379 // check to receive or timeout
olympux 19:05934ee9ee67 380 tcp_sock.set_blocking(false, TCP_CLIENT_RECEIVE_TIMEOUT);
olympux 19:05934ee9ee67 381 n = tcp_sock.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 19:05934ee9ee67 382 if (n > 0) {
olympux 19:05934ee9ee67 383 // got some data, test it
olympux 19:05934ee9ee67 384 DBG("TCP client received %d bytes: %s", n, tcp_receiving_buffer);
olympux 32:db2e8ea06ee1 385 n = process_control_command(tcp_receiving_buffer, n);
olympux 32:db2e8ea06ee1 386 // send reply back to client, NNIO protocol always returns 0
olympux 32:db2e8ea06ee1 387 // RPC-style protocol
olympux 32:db2e8ea06ee1 388 if (n > 0) {
olympux 32:db2e8ea06ee1 389 network_output_buffer[n] = '\0';
olympux 32:db2e8ea06ee1 390 tcp_sock.send_all(network_output_buffer, strlen(network_output_buffer));
olympux 32:db2e8ea06ee1 391 } // RPC-style protocol
olympux 32:db2e8ea06ee1 392 else if (n == 0) {
olympux 32:db2e8ea06ee1 393 // then, check query status command and sending protocol if required
olympux 32:db2e8ea06ee1 394 if (tcp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 32:db2e8ea06ee1 395 DBG("Requested to send device status through TCP");
olympux 32:db2e8ea06ee1 396 // sending protocol
olympux 32:db2e8ea06ee1 397 update_sending_frame(network_output_buffer);
olympux 32:db2e8ea06ee1 398 tcp_sock.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 32:db2e8ea06ee1 399 DBG("Sent");
olympux 32:db2e8ea06ee1 400 }
olympux 32:db2e8ea06ee1 401 } // NNIO protocol
olympux 18:ca499a2e7da6 402 }
olympux 16:84a5bf7285d0 403 }
olympux 18:ca499a2e7da6 404 } // if tcp client enabled && auto transmit
olympux 15:edeb0aed160d 405 #endif
olympux 15:edeb0aed160d 406
olympux 15:edeb0aed160d 407
olympux 30:15e23257e786 408 #ifdef TCP_SERVER // control and monitor from a remote TCP client, both NNIO and RPC-style
olympux 30:15e23257e786 409 // no tcp client connected{
olympux 20:71c7950fdd91 410 if (1) {
olympux 4:568c97f2a407 411 // wait for client within timeout
olympux 4:568c97f2a407 412 ret = tcp_server.accept(tcp_client);
olympux 31:2e4b6de6c2f3 413
olympux 4:568c97f2a407 414 // tcp client connected
olympux 4:568c97f2a407 415 if (ret > -1) {
olympux 18:ca499a2e7da6 416 DBG("Connection from: %s", tcp_client.get_address());
olympux 31:2e4b6de6c2f3 417
olympux 4:568c97f2a407 418 // loop waiting and receiving data within timeout
olympux 4:568c97f2a407 419 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
olympux 31:2e4b6de6c2f3 420 while (tcp_client.is_connected()) {
olympux 19:05934ee9ee67 421 n = tcp_client.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 4:568c97f2a407 422 if (n <= 0) break;
olympux 31:2e4b6de6c2f3 423
olympux 28:00c0c20d03c1 424 // got some data, process it
olympux 27:22f289beceb8 425 tcp_receiving_buffer[n] = '\0'; // for debugging purpose
olympux 19:05934ee9ee67 426 DBG("TCP server received: %s", tcp_receiving_buffer);
olympux 28:00c0c20d03c1 427 n = process_control_command(tcp_receiving_buffer, n);
olympux 31:2e4b6de6c2f3 428 // send reply back to client, NNIO protocol always returns 0
olympux 31:2e4b6de6c2f3 429 // RPC-style protocol
olympux 28:00c0c20d03c1 430 if (n > 0) {
olympux 31:2e4b6de6c2f3 431 network_output_buffer[n] = '\0';
olympux 31:2e4b6de6c2f3 432 tcp_client.send_all(network_output_buffer, strlen(network_output_buffer));
olympux 31:2e4b6de6c2f3 433 } // RPC-style protocol
olympux 31:2e4b6de6c2f3 434 else if (n == 0) {
olympux 31:2e4b6de6c2f3 435 // then, check query status command and sending protocol if required
olympux 31:2e4b6de6c2f3 436 if (tcp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 31:2e4b6de6c2f3 437 DBG("Requested to send device status through TCP");
olympux 31:2e4b6de6c2f3 438 // sending protocol
olympux 31:2e4b6de6c2f3 439 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 440 tcp_client.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 31:2e4b6de6c2f3 441 DBG("Sent");
olympux 31:2e4b6de6c2f3 442 }
olympux 31:2e4b6de6c2f3 443 } // NNIO protocol
olympux 4:568c97f2a407 444 } // end loop if no data received within timeout
olympux 4:568c97f2a407 445 } // if client connected
olympux 20:71c7950fdd91 446 tcp_client.close();
olympux 18:ca499a2e7da6 447 } // if tcp server enabled && no client connected
olympux 3:972ed747474c 448 #endif
olympux 20:71c7950fdd91 449
olympux 30:15e23257e786 450 #ifdef UDP_SERVER // configuration and control, both NNIO and RPC-style
olympux 27:22f289beceb8 451 bool discovery_mode_flag, config_mode_flag;
olympux 31:2e4b6de6c2f3 452
olympux 19:05934ee9ee67 453 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 31:2e4b6de6c2f3 454
olympux 25:48dd18cc147c 455 // check to see if it is a query command
olympux 25:48dd18cc147c 456 // if yes, is it a discovery command or an ip query to enter config mode
olympux 27:22f289beceb8 457 discovery_mode_flag = false;
olympux 25:48dd18cc147c 458 config_mode_flag = false;
olympux 26:09e0dd020900 459 if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL)) {
olympux 27:22f289beceb8 460 discovery_mode_flag = true;
olympux 25:48dd18cc147c 461 DBG("Received discovery command");
olympux 25:48dd18cc147c 462 char str[50];
olympux 29:bc052f283ada 463 sprintf(str, "%s%s%s", DEVICE_DESCRIPTION, eth.getMACAddress(), eth.getIPAddress());
olympux 25:48dd18cc147c 464 udp_server.sendTo(ep_udp_client, str, strlen(str));
olympux 27:22f289beceb8 465 } // NNCFDS
olympux 26:09e0dd020900 466 else if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL)) {
olympux 25:48dd18cc147c 467 config_mode_flag = true;
olympux 27:22f289beceb8 468 DBG("Entered configuration mode...");
olympux 20:71c7950fdd91 469 DBG("!!! RESET when finished");
olympux 27:22f289beceb8 470 } // NNCFIP
olympux 31:2e4b6de6c2f3 471
olympux 27:22f289beceb8 472 // if received NNCFIP, enter config mode
olympux 25:48dd18cc147c 473 if (config_mode_flag) {
olympux 31:2e4b6de6c2f3 474 while (n > 0) {
olympux 20:71c7950fdd91 475 // got some data, test it
olympux 27:22f289beceb8 476 DBG("UDP received (%s) from (%s:%d)", udp_receiving_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
olympux 26:09e0dd020900 477 process_config_command(udp_receiving_buffer, n);
olympux 20:71c7950fdd91 478 // wait to receive new config command
olympux 20:71c7950fdd91 479 udp_server.set_blocking(true);
olympux 20:71c7950fdd91 480 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 26:09e0dd020900 481 } // while (n > 0), config loop
olympux 25:48dd18cc147c 482 } // if (config_mode_flag)
olympux 29:bc052f283ada 483 // process control packages sent using UDP
olympux 29:bc052f283ada 484 else if ((n > 0) && (!discovery_mode_flag)) {
olympux 28:00c0c20d03c1 485 n = process_control_command(udp_receiving_buffer, n);
olympux 28:00c0c20d03c1 486 // send rpc reply back to client, NNIO protocol always returns 0
olympux 31:2e4b6de6c2f3 487 // RPC-style protocol
olympux 28:00c0c20d03c1 488 if (n > 0) {
olympux 31:2e4b6de6c2f3 489 network_output_buffer[n] = '\0';
olympux 31:2e4b6de6c2f3 490 udp_server.sendTo(ep_udp_client, network_output_buffer, strlen(network_output_buffer));
olympux 31:2e4b6de6c2f3 491 } // RPC-style protocol
olympux 31:2e4b6de6c2f3 492 else if (n == 0) {
olympux 31:2e4b6de6c2f3 493 // then, check query status command and sending protocol if required
olympux 31:2e4b6de6c2f3 494 if (udp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 31:2e4b6de6c2f3 495 DBG("Requested to send device status through UDP");
olympux 31:2e4b6de6c2f3 496 // sending protocol
olympux 31:2e4b6de6c2f3 497 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 498 udp_server.sendTo(ep_udp_client, network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 31:2e4b6de6c2f3 499 DBG("Sent");
olympux 31:2e4b6de6c2f3 500 }
olympux 32:db2e8ea06ee1 501 } // NNIO protocol
olympux 25:48dd18cc147c 502 }
olympux 3:972ed747474c 503 #endif
olympux 4:568c97f2a407 504 } // network processor
olympux 11:709f90a3b599 505 }
olympux 11:709f90a3b599 506
olympux 26:09e0dd020900 507
olympux 28:00c0c20d03c1 508 /*
olympux 28:00c0c20d03c1 509 * Process NNCF commands
olympux 28:00c0c20d03c1 510 */
olympux 26:09e0dd020900 511 void process_config_command(char* received_buffer, int len)
olympux 26:09e0dd020900 512 {
olympux 27:22f289beceb8 513 DBG("Processing configuration command");
olympux 31:2e4b6de6c2f3 514
olympux 26:09e0dd020900 515 // a configuration command always starts with NN
olympux 27:22f289beceb8 516 if ((received_buffer[0] == 'N') && (received_buffer[1] == 'N') &&
olympux 31:2e4b6de6c2f3 517 (received_buffer[2] == 'C') && (received_buffer[3] == 'F')) {
olympux 26:09e0dd020900 518 switch (len) {
olympux 31:2e4b6de6c2f3 519 // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
olympux 31:2e4b6de6c2f3 520 // Format: NNCFDS, NNCFTP, NNCFUP, NNCFTM
olympux 30:15e23257e786 521 case QUERY_NETWORK_CONFIG_CMD_LENGTH: {
olympux 26:09e0dd020900 522 if (strstr(received_buffer, QUERY_IP_CMD) != NULL) {
olympux 26:09e0dd020900 523 udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress()));
olympux 27:22f289beceb8 524 } // NNCFIP
olympux 26:09e0dd020900 525 else if (strstr(received_buffer, QUERY_SUBNET_CMD) != NULL) {
olympux 26:09e0dd020900 526 udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask()));
olympux 27:22f289beceb8 527 } // NNCFSN
olympux 26:09e0dd020900 528 else if (strstr(received_buffer, QUERY_GATEWAY_CMD) != NULL) {
olympux 26:09e0dd020900 529 udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway()));
olympux 27:22f289beceb8 530 } // NNCFGW
olympux 26:09e0dd020900 531 else if (strstr(received_buffer, QUERY_MAC_CMD) != NULL) {
olympux 26:09e0dd020900 532 udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress()));
olympux 27:22f289beceb8 533 } // NNCFMC
olympux 26:09e0dd020900 534 // ask for TCP server port
olympux 26:09e0dd020900 535 else if (strstr(received_buffer, QUERY_TCP_PORT_CMD) != NULL) {
olympux 26:09e0dd020900 536 char port[5];
olympux 26:09e0dd020900 537 sprintf(port, "%5d", tcp_server_local_port);
olympux 26:09e0dd020900 538 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 27:22f289beceb8 539 } // NNCFTP
olympux 26:09e0dd020900 540 // ask for UDP server port
olympux 26:09e0dd020900 541 else if (strstr(received_buffer, QUERY_UDP_PORT_CMD) != NULL) {
olympux 26:09e0dd020900 542 char port[5];
olympux 26:09e0dd020900 543 sprintf(port, "%5d", udp_server_local_port);
olympux 26:09e0dd020900 544 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 27:22f289beceb8 545 } // NNCFUP
olympux 26:09e0dd020900 546 else if (strstr(received_buffer, QUERY_UPDATE_TIME_CMD) != NULL) {
olympux 26:09e0dd020900 547 #ifdef NTP
olympux 26:09e0dd020900 548 char str_time[50];
olympux 26:09e0dd020900 549
olympux 26:09e0dd020900 550 DBG("Trying to update time...");
olympux 26:09e0dd020900 551 if (ntp.setTime("0.pool.ntp.org") == 0) {
olympux 26:09e0dd020900 552 DBG("Set time successfully");
olympux 26:09e0dd020900 553 time_t ctTime;
olympux 26:09e0dd020900 554 ctTime = time(NULL);
olympux 26:09e0dd020900 555
olympux 26:09e0dd020900 556 DBG("Time is set to (UTC): %s", ctime(&ctTime));
olympux 26:09e0dd020900 557 sprintf(str_time, "%s", ctime(&ctTime));
olympux 26:09e0dd020900 558 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 559 } else {
olympux 26:09e0dd020900 560 WARN("Error");
olympux 26:09e0dd020900 561 sprintf(str_time, "ERR");
olympux 26:09e0dd020900 562 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 563 }
olympux 37:94b847fea94e 564 #else
olympux 26:09e0dd020900 565 WARN("NTP disabled");
olympux 26:09e0dd020900 566 sprintf(str_time, "DIS");
olympux 26:09e0dd020900 567 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 568 #endif
olympux 27:22f289beceb8 569 } // NNCFTM
olympux 26:09e0dd020900 570 break;
olympux 30:15e23257e786 571 }
olympux 30:15e23257e786 572 // length = 19, SET NETWORK CONFIGURATION
olympux 30:15e23257e786 573 // Format: 4E 4E 43 46 C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01
olympux 30:15e23257e786 574 // (NNCF; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1)
olympux 26:09e0dd020900 575 case SET_NETWORK_CONFIG_CMD_LENGTH: {
olympux 26:09e0dd020900 576 // check device id
olympux 27:22f289beceb8 577 char* id = strstr(received_buffer, DEVICE_CONFIG_CODE);
olympux 26:09e0dd020900 578 if (id == NULL)
olympux 26:09e0dd020900 579 break;
olympux 26:09e0dd020900 580 else if ((id - received_buffer) > 0)
olympux 26:09e0dd020900 581 break;
olympux 26:09e0dd020900 582
olympux 26:09e0dd020900 583 DBG("Received user configuration");
olympux 27:22f289beceb8 584 write_eeprom_network(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char, 15-bytes
olympux 33:c906ccc220ba 585 NVIC_SystemReset();
olympux 26:09e0dd020900 586 break;
olympux 26:09e0dd020900 587 }
olympux 26:09e0dd020900 588 // length = 12, SET TCP SERVER CONFIGURATION
olympux 26:09e0dd020900 589 // auto update & its time period, TCP server configuration (IP & port)
olympux 27:22f289beceb8 590 // Format: 4E 4E 43 46 'Y' 01 C0 A8 00 09 E0 2E (LSB MSB)
olympux 27:22f289beceb8 591 // NNCF Auto 1s 192.168.0.9 12000
olympux 26:09e0dd020900 592 case UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH: {
olympux 27:22f289beceb8 593 char* id = strstr(received_buffer, DEVICE_CONFIG_CODE);
olympux 26:09e0dd020900 594 if (id == NULL)
olympux 26:09e0dd020900 595 break;
olympux 26:09e0dd020900 596 else if ((id - received_buffer) > 0)
olympux 26:09e0dd020900 597 break;
olympux 26:09e0dd020900 598
olympux 26:09e0dd020900 599 DBG("Received TCP server configuration");
olympux 27:22f289beceb8 600 write_eeprom_tcpserver(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char
olympux 33:c906ccc220ba 601 NVIC_SystemReset();
olympux 26:09e0dd020900 602 break;
olympux 26:09e0dd020900 603 }
olympux 26:09e0dd020900 604 default:
olympux 26:09e0dd020900 605 break;
olympux 26:09e0dd020900 606 } // switch (n), check configuration command length
olympux 27:22f289beceb8 607 } // if starts with NNCF, a config command
olympux 30:15e23257e786 608 else { // if not a configuration command
olympux 26:09e0dd020900 609 }
olympux 26:09e0dd020900 610 }
olympux 26:09e0dd020900 611
olympux 12:7c152c0ca4d8 612 /*
olympux 30:15e23257e786 613 * Procedure to process receiving protocol, which includes command to control outputs.
olympux 30:15e23257e786 614 * Support both NNIO and RPC-style commands.
olympux 30:15e23257e786 615 * RPC always starts with '/'.
olympux 30:15e23257e786 616 * Return:
olympux 30:15e23257e786 617 * 0 if NNIO protocol;
olympux 30:15e23257e786 618 * length of rpc output buffer if rpc;
olympux 30:15e23257e786 619 * -1 if rpc failed
olympux 28:00c0c20d03c1 620 */
olympux 28:00c0c20d03c1 621 int process_control_command(char* received_buffer, int len)
olympux 26:09e0dd020900 622 {
olympux 26:09e0dd020900 623 char* received_frame;
olympux 30:15e23257e786 624 bool rpc_style;
olympux 26:09e0dd020900 625 int pos;
olympux 31:2e4b6de6c2f3 626 char inbuf[NET_BUF_LEN];
olympux 31:2e4b6de6c2f3 627
olympux 27:22f289beceb8 628 DBG("Processing control command");
olympux 31:2e4b6de6c2f3 629
olympux 30:15e23257e786 630 /*
olympux 30:15e23257e786 631 * This section is for RPC-style command
olympux 30:15e23257e786 632 */
olympux 30:15e23257e786 633 // check if it is a RPC-style command
olympux 30:15e23257e786 634 rpc_style = false;
olympux 27:22f289beceb8 635 strncpy(inbuf, received_buffer, len);
olympux 27:22f289beceb8 636 inbuf[len] = '\r'; // use inbuf for RPC protocol
olympux 27:22f289beceb8 637 inbuf[len+1] = '\n';
olympux 27:22f289beceb8 638 inbuf[len+2] = '\0'; // add CR-LF
olympux 27:22f289beceb8 639 if ((len > 0) && (inbuf[0] == '/')) {
olympux 34:32299b819067 640 char obj_name[16];
olympux 34:32299b819067 641 bool ok;
olympux 31:2e4b6de6c2f3 642
olympux 30:15e23257e786 643 rpc_style = true;
olympux 29:bc052f283ada 644 // find RPC object name
olympux 29:bc052f283ada 645 for (int i = 1; i < strlen(inbuf); i++) {
olympux 29:bc052f283ada 646 if (inbuf[i] != '/') {
olympux 29:bc052f283ada 647 obj_name[i-1] = inbuf[i];
olympux 31:2e4b6de6c2f3 648 } else {
olympux 29:bc052f283ada 649 obj_name[i-1] = '\0';
olympux 29:bc052f283ada 650 break;
olympux 29:bc052f283ada 651 }
olympux 29:bc052f283ada 652 }
olympux 28:00c0c20d03c1 653 DBG("Rpc command = %s", inbuf);
olympux 29:bc052f283ada 654 /*
olympux 29:bc052f283ada 655 * execute RPC command, return reply length and reply in rpc_outbuf
olympux 29:bc052f283ada 656 */
olympux 34:32299b819067 657 ok = RPC::call(inbuf, network_output_buffer);
olympux 34:32299b819067 658 if (ok) {
olympux 29:bc052f283ada 659 // re-arrange output buffer as following: object_name:output_value
olympux 31:2e4b6de6c2f3 660 strcpy(inbuf, network_output_buffer); // use inbuf as temp
olympux 31:2e4b6de6c2f3 661 strcpy(network_output_buffer, obj_name); // rpc object name
olympux 31:2e4b6de6c2f3 662 strcat(network_output_buffer, ":");
olympux 31:2e4b6de6c2f3 663 strcat(network_output_buffer, inbuf); // concat rpc reply
olympux 31:2e4b6de6c2f3 664 strcat(network_output_buffer, "\r\n"); // CR-LF
olympux 31:2e4b6de6c2f3 665 int j = strlen(network_output_buffer);
olympux 31:2e4b6de6c2f3 666 DBG("Reply of rpc command on \"%s\" (%d bytes): %s", obj_name, j, network_output_buffer);
olympux 29:bc052f283ada 667 return j; // return length of rpc_outbuf
olympux 31:2e4b6de6c2f3 668 } else {
olympux 27:22f289beceb8 669 ERR("Failed: %s", inbuf);
olympux 28:00c0c20d03c1 670 return -1;
olympux 27:22f289beceb8 671 }
olympux 27:22f289beceb8 672 }
olympux 31:2e4b6de6c2f3 673
olympux 28:00c0c20d03c1 674 /*
olympux 28:00c0c20d03c1 675 * This section below is for NNIO protocol
olympux 28:00c0c20d03c1 676 */
olympux 30:15e23257e786 677 while ((!rpc_style) && (len >= RECEIVING_PROTOCOL_LENGTH)) {
olympux 26:09e0dd020900 678 // find device ID
olympux 26:09e0dd020900 679 DBG("Checking device ID...");
olympux 27:22f289beceb8 680 char* id = strstr(received_buffer, DEVICE_CONTROL_CODE);
olympux 26:09e0dd020900 681 if (id == NULL) {
olympux 26:09e0dd020900 682 DBG("No device ID found");
olympux 26:09e0dd020900 683 break;
olympux 26:09e0dd020900 684 }
olympux 26:09e0dd020900 685 pos = id - received_buffer;
olympux 26:09e0dd020900 686 DBG("Found a frame at %d", pos);
olympux 31:2e4b6de6c2f3 687
olympux 26:09e0dd020900 688 // extract this frame
olympux 26:09e0dd020900 689 received_frame = &received_buffer[pos];
olympux 26:09e0dd020900 690 // calculate the rest
olympux 26:09e0dd020900 691 received_buffer = &received_buffer[pos + RECEIVING_PROTOCOL_LENGTH];
olympux 26:09e0dd020900 692 len -= RECEIVING_PROTOCOL_LENGTH;
olympux 31:2e4b6de6c2f3 693
olympux 26:09e0dd020900 694 // process this received frame
olympux 26:09e0dd020900 695 // firstly, update outputs if required
olympux 26:09e0dd020900 696 // digital outputs
olympux 26:09e0dd020900 697 if (received_frame[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 698 DBG("Update digital outputs");
olympux 26:09e0dd020900 699 char str_dout[9];
olympux 26:09e0dd020900 700 memcpy(str_dout, &received_frame[RECEIVING_PROTOCOL_DO_POS], 8);
olympux 26:09e0dd020900 701 str_dout[8] = '\0';
olympux 26:09e0dd020900 702 update_digital_outputs(str_dout);
olympux 26:09e0dd020900 703 }
olympux 26:09e0dd020900 704 // analog output 0
olympux 26:09e0dd020900 705 if (received_frame[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 706 DBG("Update analog output 0");
olympux 26:09e0dd020900 707 //TODO Update analog output
olympux 26:09e0dd020900 708 }
olympux 26:09e0dd020900 709 // analog output 1
olympux 26:09e0dd020900 710 if (received_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 711 DBG("Update analog output 1");
olympux 26:09e0dd020900 712 //TODO Update analog output
olympux 26:09e0dd020900 713 }
olympux 26:09e0dd020900 714 // UART
olympux 26:09e0dd020900 715 if (received_frame[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 716 DBG("UART data: ");
olympux 26:09e0dd020900 717 char str_uart[33];
olympux 26:09e0dd020900 718 memcpy(str_uart, &received_frame[RECEIVING_PROTOCOL_UART_POS], 32);
olympux 26:09e0dd020900 719 str_uart[32] = '\0';
olympux 26:09e0dd020900 720 uart.printf("%s\r\n", str_uart);
olympux 26:09e0dd020900 721 }
olympux 26:09e0dd020900 722 }
olympux 31:2e4b6de6c2f3 723
olympux 27:22f289beceb8 724 DBG("Successfully processed.");
olympux 28:00c0c20d03c1 725 return 0;
olympux 26:09e0dd020900 726 }
olympux 26:09e0dd020900 727
olympux 26:09e0dd020900 728
olympux 26:09e0dd020900 729 /*
olympux 26:09e0dd020900 730 * W5500 Ethernet init
olympux 26:09e0dd020900 731 */
olympux 31:2e4b6de6c2f3 732 int ethernet_init(void)
olympux 31:2e4b6de6c2f3 733 {
olympux 26:09e0dd020900 734 int dhcp_ret, ret;
olympux 31:2e4b6de6c2f3 735
olympux 27:22f289beceb8 736 DBG("Initialising ethernet...");
olympux 31:2e4b6de6c2f3 737
olympux 26:09e0dd020900 738 // if not configured, try dhcp
olympux 26:09e0dd020900 739 dhcp_ret = -1;
olympux 26:09e0dd020900 740 if (configured_ip != DEFAULT_ENABLE_FLAG_VALUE) {
olympux 27:22f289beceb8 741 DBG("Connecting to DHCP server...");
olympux 26:09e0dd020900 742 dhcp_ret = eth.init(u8mac);
olympux 26:09e0dd020900 743 if (dhcp_ret == 0)
olympux 26:09e0dd020900 744 dhcp_ret = eth.connect();
olympux 26:09e0dd020900 745 }
olympux 31:2e4b6de6c2f3 746
olympux 26:09e0dd020900 747 if (dhcp_ret != 0) {
olympux 26:09e0dd020900 748 DBG("No DHCP, load static IP configuration");
olympux 26:09e0dd020900 749 ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
olympux 26:09e0dd020900 750 } else {
olympux 26:09e0dd020900 751 snprintf(str_ip_addr, 16, "%s", eth.getIPAddress());
olympux 26:09e0dd020900 752 snprintf(str_ip_subnet, 16, "%s", eth.getNetworkMask());
olympux 26:09e0dd020900 753 snprintf(str_ip_gateway, 16, "%s", eth.getGateway());
olympux 26:09e0dd020900 754 ret = 0;
olympux 26:09e0dd020900 755 }
olympux 26:09e0dd020900 756
olympux 26:09e0dd020900 757 if (ret == 0) {
olympux 26:09e0dd020900 758 DBG("Initialized, MAC: %s", eth.getMACAddress());
olympux 26:09e0dd020900 759 } else {
olympux 26:09e0dd020900 760 ERR("Error eth.init() - ret = %d", ret);
olympux 26:09e0dd020900 761 return -1;
olympux 26:09e0dd020900 762 }
olympux 26:09e0dd020900 763
olympux 26:09e0dd020900 764 ret = eth.connect();
olympux 26:09e0dd020900 765 if (!ret) {
olympux 26:09e0dd020900 766 DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
olympux 26:09e0dd020900 767 } else {
olympux 26:09e0dd020900 768 ERR("Error eth.connect() - ret = %d", ret);
olympux 26:09e0dd020900 769 return -1;
olympux 26:09e0dd020900 770 }
olympux 31:2e4b6de6c2f3 771
olympux 26:09e0dd020900 772 return 0;
olympux 26:09e0dd020900 773 }
olympux 26:09e0dd020900 774
olympux 26:09e0dd020900 775
olympux 26:09e0dd020900 776 /*
olympux 26:09e0dd020900 777 * Update digital outputs according to receiving protocol
olympux 12:7c152c0ca4d8 778 */
olympux 31:2e4b6de6c2f3 779 void update_digital_outputs(char* buf)
olympux 31:2e4b6de6c2f3 780 {
olympux 18:ca499a2e7da6 781 DBG("Digital outputs: %s", buf);
olympux 31:2e4b6de6c2f3 782
olympux 12:7c152c0ca4d8 783 dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 784 dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 785 dout2 = (buf[2] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 786 dout3 = (buf[3] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 787 dout4 = (buf[4] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 788 dout5 = (buf[5] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 789 dout6 = (buf[6] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 790 dout7 = (buf[7] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 791 }
olympux 12:7c152c0ca4d8 792
olympux 26:09e0dd020900 793 /*
olympux 26:09e0dd020900 794 * Prepare a frame for sending protocol, which includes status of I/Os
olympux 26:09e0dd020900 795 */
olympux 31:2e4b6de6c2f3 796 void update_sending_frame(char* buf)
olympux 31:2e4b6de6c2f3 797 {
olympux 27:22f289beceb8 798 memcpy(&buf[SENDING_PROTOCOL_ID_POS], DEVICE_CONTROL_CODE, 4); // device id
olympux 12:7c152c0ca4d8 799 memcpy(&buf[SENDING_PROTOCOL_MAC_POS], &u8mac, 6);
olympux 12:7c152c0ca4d8 800 memcpy(&buf[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4);
olympux 31:2e4b6de6c2f3 801
olympux 12:7c152c0ca4d8 802 buf[SENDING_PROTOCOL_DI_POS+0] = (din0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 803 buf[SENDING_PROTOCOL_DI_POS+1] = (din1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 804 buf[SENDING_PROTOCOL_DI_POS+2] = (din2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 805 buf[SENDING_PROTOCOL_DI_POS+3] = (din3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 806 buf[SENDING_PROTOCOL_DI_POS+4] = (din4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 807 buf[SENDING_PROTOCOL_DI_POS+5] = (din5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 808 buf[SENDING_PROTOCOL_DI_POS+6] = (din6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 809 buf[SENDING_PROTOCOL_DI_POS+7] = (din7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 31:2e4b6de6c2f3 810
olympux 12:7c152c0ca4d8 811 buf[SENDING_PROTOCOL_DO_POS+0] = (dout0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 812 buf[SENDING_PROTOCOL_DO_POS+1] = (dout1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 813 buf[SENDING_PROTOCOL_DO_POS+2] = (dout2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 814 buf[SENDING_PROTOCOL_DO_POS+3] = (dout3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 815 buf[SENDING_PROTOCOL_DO_POS+4] = (dout4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 816 buf[SENDING_PROTOCOL_DO_POS+5] = (dout5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 817 buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 818 buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 31:2e4b6de6c2f3 819
olympux 15:edeb0aed160d 820 uint16_t val = ain0.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 821 memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB
olympux 15:edeb0aed160d 822 val = ain1.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 823 memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB
olympux 23:47ee805435b1 824 val = 0x1234;
olympux 15:edeb0aed160d 825 memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB
olympux 23:47ee805435b1 826 val = 0x5678;
olympux 15:edeb0aed160d 827 memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB
olympux 12:7c152c0ca4d8 828 buf[SENDING_PROTOCOL_CR_POS] = 0x0D;
olympux 12:7c152c0ca4d8 829 buf[SENDING_PROTOCOL_CR_POS+1] = '\0';
olympux 4:568c97f2a407 830 }