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:
Sat Jan 24 20:49:24 2015 +0000
Revision:
31:2e4b6de6c2f3
Parent:
30:15e23257e786
Child:
32:db2e8ea06ee1
Process both RPC and NNIO control commands with UDP and TCP

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 27:22f289beceb8 123 RpcDigitalIn di0(PB_14, "din0");
olympux 27:22f289beceb8 124 RpcDigitalIn di1(PB_12, "din1");
olympux 27:22f289beceb8 125 RpcDigitalIn di2(PB_10, "din2");
olympux 27:22f289beceb8 126 RpcDigitalIn di3(PB_1, "din3");
olympux 27:22f289beceb8 127 RpcDigitalIn di4(PB_15, "din4");
olympux 27:22f289beceb8 128 RpcDigitalIn di5(PB_13, "din5");
olympux 27:22f289beceb8 129 RpcDigitalIn di6(PB_11, "din6");
olympux 27:22f289beceb8 130 RpcDigitalIn di7(PB_2, "din7");
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 27:22f289beceb8 140 RpcDigitalOut do0(PB_3, "dout0");
olympux 27:22f289beceb8 141 RpcDigitalOut do1(PB_5, "dout1");
olympux 27:22f289beceb8 142 RpcDigitalOut do2(PB_7, "dout2");
olympux 27:22f289beceb8 143 RpcDigitalOut do3(PB_9, "dout3");
olympux 27:22f289beceb8 144 RpcDigitalOut do4(PD_2, "dout4");
olympux 27:22f289beceb8 145 RpcDigitalOut do5(PB_4, "dout5");
olympux 27:22f289beceb8 146 RpcDigitalOut do6(PB_6, "dout6");
olympux 27:22f289beceb8 147 RpcDigitalOut do7(PB_8, "dout7");
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 27:22f289beceb8 157 RpcAnalogIn adc10(PC_0, "ain0"); // adc10
olympux 27:22f289beceb8 158 RpcAnalogIn adc11(PC_1, "ain1"); // 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 27:22f289beceb8 168 RpcTimer timer1("timer1");
olympux 20:71c7950fdd91 169 // Watchdog
olympux 20:71c7950fdd91 170 Watchdog wdt;
olympux 20:71c7950fdd91 171
olympux 20:71c7950fdd91 172
olympux 13:bcf840da68fd 173 /*
olympux 30:15e23257e786 174 * NNIO Protocol
olympux 30:15e23257e786 175 */
olympux 9:d2534ecf88c6 176 // Commands
olympux 29:bc052f283ada 177 #define DEVICE_DESCRIPTION "NNIO"
olympux 27:22f289beceb8 178 #define DEVICE_CONFIG_CODE "NNCF"
olympux 27:22f289beceb8 179 #define DEVICE_CONTROL_CODE "NNIO"
olympux 17:88ef7a078095 180
olympux 17:88ef7a078095 181 #define RECEIVING_PROTOCOL_LENGTH 58
olympux 17:88ef7a078095 182 #define SENDING_PROTOCOL_LENGTH 39
olympux 26:09e0dd020900 183 #define QUERY_NETWORK_CONFIG_CMD_LENGTH 6
olympux 17:88ef7a078095 184 #define SET_NETWORK_CONFIG_CMD_LENGTH 19
olympux 26:09e0dd020900 185 #define UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH 12
olympux 17:88ef7a078095 186
olympux 27:22f289beceb8 187 #define QUERY_DISCOVERY_CMD "NNCFDS"
olympux 27:22f289beceb8 188 #define QUERY_IP_CMD "NNCFIP"
olympux 27:22f289beceb8 189 #define QUERY_SUBNET_CMD "NNCFSN"
olympux 27:22f289beceb8 190 #define QUERY_GATEWAY_CMD "NNCFGW"
olympux 27:22f289beceb8 191 #define QUERY_MAC_CMD "NNCFMC"
olympux 27:22f289beceb8 192 #define QUERY_UDP_PORT_CMD "NNCFUP"
olympux 27:22f289beceb8 193 #define QUERY_TCP_PORT_CMD "NNCFTP"
olympux 27:22f289beceb8 194 #define QUERY_UPDATE_TIME_CMD "NNCFTM"
olympux 11:709f90a3b599 195 #define RECEIVING_PROTOCOL_ENABLE_OUTPUT 'O'
olympux 11:709f90a3b599 196 #define QUERY_STATUS_COMMAND 'Q'
olympux 11:709f90a3b599 197 #define DIGITAL_HIGH 'H'
olympux 11:709f90a3b599 198 #define DIGITAL_LOW 'L'
olympux 9:d2534ecf88c6 199
olympux 9:d2534ecf88c6 200
olympux 9:d2534ecf88c6 201 // Positions
olympux 11:709f90a3b599 202 #define RECEIVING_PROTOCOL_ID_POS 0
olympux 9:d2534ecf88c6 203 #define RECEIVING_PROTOCOL_OP_POS 4
olympux 9:d2534ecf88c6 204 #define RECEIVING_PROTOCOL_EN_DO_POS RECEIVING_PROTOCOL_OP_POS + 0
olympux 9:d2534ecf88c6 205 #define RECEIVING_PROTOCOL_EN_A0O_POS RECEIVING_PROTOCOL_OP_POS + 1
olympux 9:d2534ecf88c6 206 #define RECEIVING_PROTOCOL_EN_A1O_POS RECEIVING_PROTOCOL_OP_POS + 2
olympux 9:d2534ecf88c6 207 #define RECEIVING_PROTOCOL_EN_UART_POS RECEIVING_PROTOCOL_OP_POS + 3
olympux 9:d2534ecf88c6 208 #define RECEIVING_PROTOCOL_COMMAND_POS RECEIVING_PROTOCOL_OP_POS + 4
olympux 11:709f90a3b599 209
olympux 11:709f90a3b599 210 #define RECEIVING_PROTOCOL_IP_POS 9
olympux 11:709f90a3b599 211 #define RECEIVING_PROTOCOL_DO_POS 13
olympux 11:709f90a3b599 212 #define RECEIVING_PROTOCOL_A0O_POS 21
olympux 11:709f90a3b599 213 #define RECEIVING_PROTOCOL_A01_POS 23
olympux 11:709f90a3b599 214 #define RECEIVING_PROTOCOL_UART_POS 25
olympux 9:d2534ecf88c6 215
olympux 9:d2534ecf88c6 216
olympux 11:709f90a3b599 217 #define SENDING_PROTOCOL_ID_POS 0
olympux 11:709f90a3b599 218 #define SENDING_PROTOCOL_MAC_POS 4
olympux 11:709f90a3b599 219 #define SENDING_PROTOCOL_IP_POS 10
olympux 11:709f90a3b599 220 #define SENDING_PROTOCOL_DI_POS 14
olympux 11:709f90a3b599 221 #define SENDING_PROTOCOL_DO_POS 22
olympux 11:709f90a3b599 222 #define SENDING_PROTOCOL_AI0_POS 30
olympux 11:709f90a3b599 223 #define SENDING_PROTOCOL_AI1_POS 32
olympux 11:709f90a3b599 224 #define SENDING_PROTOCOL_AO0_POS 34
olympux 11:709f90a3b599 225 #define SENDING_PROTOCOL_AO1_POS 36
olympux 11:709f90a3b599 226 #define SENDING_PROTOCOL_CR_POS 38
olympux 0:c2eac797face 227
olympux 2:18f10e7209f4 228
olympux 2:18f10e7209f4 229 /*
olympux 2:18f10e7209f4 230 * RTOS
olympux 2:18f10e7209f4 231 */
olympux 2:18f10e7209f4 232 struct message_t {
olympux 2:18f10e7209f4 233 int len;
olympux 2:18f10e7209f4 234 char *msg;
olympux 2:18f10e7209f4 235 };
olympux 3:972ed747474c 236 Queue<message_t, 16> uart_queue;
olympux 15:edeb0aed160d 237 Queue<bool, 1> auto_update_queue;
olympux 2:18f10e7209f4 238
olympux 2:18f10e7209f4 239
olympux 26:09e0dd020900 240
olympux 26:09e0dd020900 241 // Prototypes
olympux 26:09e0dd020900 242 int ethernet_init(void);
olympux 28:00c0c20d03c1 243 int process_control_command(char* received_buffer, int len);
olympux 26:09e0dd020900 244 void process_config_command(char* received_buffer, int len);
olympux 26:09e0dd020900 245 void update_digital_outputs(char* buf);
olympux 26:09e0dd020900 246 void update_sending_frame(char* buf);
olympux 26:09e0dd020900 247
olympux 26:09e0dd020900 248
olympux 2:18f10e7209f4 249 /*
olympux 2:18f10e7209f4 250 * Threads
olympux 2:18f10e7209f4 251 */
olympux 31:2e4b6de6c2f3 252 // Timer thread for auto update in TCP client function
olympux 31:2e4b6de6c2f3 253 void auto_update_timer_thread(void const* args)
olympux 31:2e4b6de6c2f3 254 {
olympux 18:ca499a2e7da6 255 bool update_flag = true;
olympux 31:2e4b6de6c2f3 256
olympux 15:edeb0aed160d 257 Thread::wait(500);
olympux 15:edeb0aed160d 258 while(true) {
olympux 15:edeb0aed160d 259 auto_update_queue.put(&update_flag);
olympux 18:ca499a2e7da6 260 Thread::wait(1000*transmit_time_period); // Thread::wait() in ms
olympux 15:edeb0aed160d 261 }
olympux 15:edeb0aed160d 262 }
olympux 15:edeb0aed160d 263
olympux 15:edeb0aed160d 264
olympux 20:71c7950fdd91 265 // WDT reset
olympux 31:2e4b6de6c2f3 266 void wdt_reset_thread(void const* args)
olympux 31:2e4b6de6c2f3 267 {
olympux 20:71c7950fdd91 268 while (true)
olympux 20:71c7950fdd91 269 wdt.Service();
olympux 20:71c7950fdd91 270 }
olympux 20:71c7950fdd91 271
olympux 20:71c7950fdd91 272
olympux 4:568c97f2a407 273 int main()
olympux 4:568c97f2a407 274 {
olympux 9:d2534ecf88c6 275 int n, ret;
olympux 31:2e4b6de6c2f3 276
olympux 20:71c7950fdd91 277 Thread::wait(500); // turn on delay
olympux 31:2e4b6de6c2f3 278
olympux 4:568c97f2a407 279 /*
olympux 9:d2534ecf88c6 280 * Configure
olympux 4:568c97f2a407 281 */
olympux 11:709f90a3b599 282 uart.baud(115200);
olympux 18:ca499a2e7da6 283 DBG("\r\nStarting...");
olympux 31:2e4b6de6c2f3 284
olympux 31:2e4b6de6c2f3 285 // check watchdog
olympux 20:71c7950fdd91 286 if (wdt.WatchdogCausedReset())
olympux 20:71c7950fdd91 287 DBG("Watchdog caused reset.");
olympux 20:71c7950fdd91 288 wdt.Configure(4);
olympux 31:2e4b6de6c2f3 289
olympux 6:d054e394fba3 290 /*
olympux 6:d054e394fba3 291 * FLASH
olympux 6:d054e394fba3 292 */
olympux 12:7c152c0ca4d8 293 load_eeprom_network();
olympux 15:edeb0aed160d 294 load_eeprom_tcpserver();
olympux 31:2e4b6de6c2f3 295
olympux 20:71c7950fdd91 296 /*
olympux 20:71c7950fdd91 297 * UI threads
olympux 20:71c7950fdd91 298 */
olympux 20:71c7950fdd91 299 Thread t2(auto_update_timer_thread);
olympux 20:71c7950fdd91 300 Thread t3(wdt_reset_thread);
olympux 31:2e4b6de6c2f3 301
olympux 4:568c97f2a407 302 /*
olympux 4:568c97f2a407 303 * Ethernet
olympux 4:568c97f2a407 304 */
olympux 6:d054e394fba3 305 ret = ethernet_init();
olympux 4:568c97f2a407 306 if (ret) {
olympux 18:ca499a2e7da6 307 ERR("Ethernet initialisation failed. App halted.");
olympux 4:568c97f2a407 308 while (true) {};
olympux 4:568c97f2a407 309 }
olympux 31:2e4b6de6c2f3 310
olympux 23:47ee805435b1 311 Thread::wait(2000); // TCP/UDP stack delay
olympux 12:7c152c0ca4d8 312
olympux 31:2e4b6de6c2f3 313 /*
olympux 31:2e4b6de6c2f3 314 * UDP server
olympux 31:2e4b6de6c2f3 315 * TCP server/client
olympux 31:2e4b6de6c2f3 316 */
olympux 20:71c7950fdd91 317 #ifdef UDP_SERVER
olympux 20:71c7950fdd91 318 ret = udp_server.bind(udp_server_local_port);
olympux 20:71c7950fdd91 319 DBG("UDP server started (sock.bind = %d)...", ret);
olympux 20:71c7950fdd91 320 udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
olympux 20:71c7950fdd91 321 #endif
olympux 20:71c7950fdd91 322
olympux 3:972ed747474c 323 #ifdef TCP_SERVER
olympux 12:7c152c0ca4d8 324 tcp_server.bind(tcp_server_local_port);
olympux 3:972ed747474c 325 tcp_server.listen();
olympux 18:ca499a2e7da6 326 DBG("TCP server started...");
olympux 4:568c97f2a407 327 tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
olympux 3:972ed747474c 328 #endif
olympux 14:18eda020a589 329
olympux 14:18eda020a589 330 #ifdef TCP_CLIENT
olympux 18:ca499a2e7da6 331
olympux 14:18eda020a589 332 #endif
olympux 31:2e4b6de6c2f3 333
olympux 12:7c152c0ca4d8 334 /*
olympux 19:05934ee9ee67 335 * Network loop processor
olympux 12:7c152c0ca4d8 336 */
olympux 0:c2eac797face 337 while (true) {
olympux 30:15e23257e786 338 #ifdef TCP_CLIENT // auto update device status to a remote TCP server
olympux 18:ca499a2e7da6 339 if (auto_transmit_flag == 0xA5A5) {
olympux 18:ca499a2e7da6 340 // connect to TCP server if required
olympux 18:ca499a2e7da6 341 if (!tcp_sock.is_connected()) {
olympux 20:71c7950fdd91 342 ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port); // timeout is default in connect() in W5500.h
olympux 18:ca499a2e7da6 343 if (ret > -1) {
olympux 18:ca499a2e7da6 344 DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 31:2e4b6de6c2f3 345 } else {
olympux 18:ca499a2e7da6 346 ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port);
olympux 18:ca499a2e7da6 347 }
olympux 16:84a5bf7285d0 348 }
olympux 31:2e4b6de6c2f3 349
olympux 18:ca499a2e7da6 350 // transmit data if connected
olympux 18:ca499a2e7da6 351 if (tcp_sock.is_connected()) {
olympux 18:ca499a2e7da6 352 osEvent evt = auto_update_queue.get(1); // timeout after 1ms
olympux 18:ca499a2e7da6 353 if (evt.status == osEventMessage) {
olympux 18:ca499a2e7da6 354 DBG("Updating...");
olympux 31:2e4b6de6c2f3 355 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 356 tcp_sock.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 19:05934ee9ee67 357 }
olympux 31:2e4b6de6c2f3 358
olympux 19:05934ee9ee67 359 // check to receive or timeout
olympux 19:05934ee9ee67 360 tcp_sock.set_blocking(false, TCP_CLIENT_RECEIVE_TIMEOUT);
olympux 19:05934ee9ee67 361 n = tcp_sock.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 19:05934ee9ee67 362 if (n > 0) {
olympux 19:05934ee9ee67 363 // got some data, test it
olympux 19:05934ee9ee67 364 DBG("TCP client received %d bytes: %s", n, tcp_receiving_buffer);
olympux 26:09e0dd020900 365 process_control_command(tcp_receiving_buffer, n);
olympux 18:ca499a2e7da6 366 }
olympux 16:84a5bf7285d0 367 }
olympux 18:ca499a2e7da6 368 } // if tcp client enabled && auto transmit
olympux 15:edeb0aed160d 369 #endif
olympux 15:edeb0aed160d 370
olympux 15:edeb0aed160d 371
olympux 30:15e23257e786 372 #ifdef TCP_SERVER // control and monitor from a remote TCP client, both NNIO and RPC-style
olympux 30:15e23257e786 373 // no tcp client connected{
olympux 20:71c7950fdd91 374 if (1) {
olympux 4:568c97f2a407 375 // wait for client within timeout
olympux 4:568c97f2a407 376 ret = tcp_server.accept(tcp_client);
olympux 31:2e4b6de6c2f3 377
olympux 4:568c97f2a407 378 // tcp client connected
olympux 4:568c97f2a407 379 if (ret > -1) {
olympux 18:ca499a2e7da6 380 DBG("Connection from: %s", tcp_client.get_address());
olympux 31:2e4b6de6c2f3 381
olympux 4:568c97f2a407 382 // loop waiting and receiving data within timeout
olympux 4:568c97f2a407 383 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
olympux 31:2e4b6de6c2f3 384 while (tcp_client.is_connected()) {
olympux 19:05934ee9ee67 385 n = tcp_client.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
olympux 4:568c97f2a407 386 if (n <= 0) break;
olympux 31:2e4b6de6c2f3 387
olympux 28:00c0c20d03c1 388 // got some data, process it
olympux 27:22f289beceb8 389 tcp_receiving_buffer[n] = '\0'; // for debugging purpose
olympux 19:05934ee9ee67 390 DBG("TCP server received: %s", tcp_receiving_buffer);
olympux 28:00c0c20d03c1 391 n = process_control_command(tcp_receiving_buffer, n);
olympux 31:2e4b6de6c2f3 392 // send reply back to client, NNIO protocol always returns 0
olympux 31:2e4b6de6c2f3 393 // RPC-style protocol
olympux 28:00c0c20d03c1 394 if (n > 0) {
olympux 31:2e4b6de6c2f3 395 network_output_buffer[n] = '\0';
olympux 31:2e4b6de6c2f3 396 tcp_client.send_all(network_output_buffer, strlen(network_output_buffer));
olympux 31:2e4b6de6c2f3 397 } // RPC-style protocol
olympux 31:2e4b6de6c2f3 398 else if (n == 0) {
olympux 31:2e4b6de6c2f3 399 // then, check query status command and sending protocol if required
olympux 31:2e4b6de6c2f3 400 if (tcp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 31:2e4b6de6c2f3 401 DBG("Requested to send device status through TCP");
olympux 31:2e4b6de6c2f3 402 // sending protocol
olympux 31:2e4b6de6c2f3 403 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 404 tcp_client.send_all(network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 31:2e4b6de6c2f3 405 DBG("Sent");
olympux 31:2e4b6de6c2f3 406 }
olympux 31:2e4b6de6c2f3 407 } // NNIO protocol
olympux 4:568c97f2a407 408 } // end loop if no data received within timeout
olympux 4:568c97f2a407 409 } // if client connected
olympux 20:71c7950fdd91 410 tcp_client.close();
olympux 18:ca499a2e7da6 411 } // if tcp server enabled && no client connected
olympux 3:972ed747474c 412 #endif
olympux 20:71c7950fdd91 413
olympux 30:15e23257e786 414 #ifdef UDP_SERVER // configuration and control, both NNIO and RPC-style
olympux 27:22f289beceb8 415 bool discovery_mode_flag, config_mode_flag;
olympux 31:2e4b6de6c2f3 416
olympux 19:05934ee9ee67 417 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 31:2e4b6de6c2f3 418
olympux 25:48dd18cc147c 419 // check to see if it is a query command
olympux 25:48dd18cc147c 420 // if yes, is it a discovery command or an ip query to enter config mode
olympux 27:22f289beceb8 421 discovery_mode_flag = false;
olympux 25:48dd18cc147c 422 config_mode_flag = false;
olympux 26:09e0dd020900 423 if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL)) {
olympux 27:22f289beceb8 424 discovery_mode_flag = true;
olympux 25:48dd18cc147c 425 DBG("Received discovery command");
olympux 25:48dd18cc147c 426 char str[50];
olympux 29:bc052f283ada 427 sprintf(str, "%s%s%s", DEVICE_DESCRIPTION, eth.getMACAddress(), eth.getIPAddress());
olympux 25:48dd18cc147c 428 udp_server.sendTo(ep_udp_client, str, strlen(str));
olympux 27:22f289beceb8 429 } // NNCFDS
olympux 26:09e0dd020900 430 else if ((n == QUERY_NETWORK_CONFIG_CMD_LENGTH) && (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL)) {
olympux 25:48dd18cc147c 431 config_mode_flag = true;
olympux 27:22f289beceb8 432 DBG("Entered configuration mode...");
olympux 20:71c7950fdd91 433 DBG("!!! RESET when finished");
olympux 27:22f289beceb8 434 } // NNCFIP
olympux 31:2e4b6de6c2f3 435
olympux 27:22f289beceb8 436 // if received NNCFIP, enter config mode
olympux 25:48dd18cc147c 437 if (config_mode_flag) {
olympux 31:2e4b6de6c2f3 438 while (n > 0) {
olympux 20:71c7950fdd91 439 // got some data, test it
olympux 27:22f289beceb8 440 DBG("UDP received (%s) from (%s:%d)", udp_receiving_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
olympux 26:09e0dd020900 441 process_config_command(udp_receiving_buffer, n);
olympux 20:71c7950fdd91 442 // wait to receive new config command
olympux 20:71c7950fdd91 443 udp_server.set_blocking(true);
olympux 20:71c7950fdd91 444 n = udp_server.receiveFrom(ep_udp_client, udp_receiving_buffer, sizeof(udp_receiving_buffer));
olympux 26:09e0dd020900 445 } // while (n > 0), config loop
olympux 25:48dd18cc147c 446 } // if (config_mode_flag)
olympux 29:bc052f283ada 447 // process control packages sent using UDP
olympux 29:bc052f283ada 448 else if ((n > 0) && (!discovery_mode_flag)) {
olympux 28:00c0c20d03c1 449 n = process_control_command(udp_receiving_buffer, n);
olympux 28:00c0c20d03c1 450 // send rpc reply back to client, NNIO protocol always returns 0
olympux 31:2e4b6de6c2f3 451 // RPC-style protocol
olympux 28:00c0c20d03c1 452 if (n > 0) {
olympux 31:2e4b6de6c2f3 453 network_output_buffer[n] = '\0';
olympux 31:2e4b6de6c2f3 454 udp_server.sendTo(ep_udp_client, network_output_buffer, strlen(network_output_buffer));
olympux 31:2e4b6de6c2f3 455 } // RPC-style protocol
olympux 31:2e4b6de6c2f3 456 else if (n == 0) {
olympux 31:2e4b6de6c2f3 457 // then, check query status command and sending protocol if required
olympux 31:2e4b6de6c2f3 458 if (udp_receiving_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
olympux 31:2e4b6de6c2f3 459 DBG("Requested to send device status through UDP");
olympux 31:2e4b6de6c2f3 460 // sending protocol
olympux 31:2e4b6de6c2f3 461 update_sending_frame(network_output_buffer);
olympux 31:2e4b6de6c2f3 462 udp_server.sendTo(ep_udp_client, network_output_buffer, SENDING_PROTOCOL_LENGTH);
olympux 31:2e4b6de6c2f3 463 DBG("Sent");
olympux 31:2e4b6de6c2f3 464 }
olympux 28:00c0c20d03c1 465 }
olympux 25:48dd18cc147c 466 }
olympux 3:972ed747474c 467 #endif
olympux 4:568c97f2a407 468 } // network processor
olympux 11:709f90a3b599 469 }
olympux 11:709f90a3b599 470
olympux 26:09e0dd020900 471
olympux 28:00c0c20d03c1 472 /*
olympux 28:00c0c20d03c1 473 * Process NNCF commands
olympux 28:00c0c20d03c1 474 */
olympux 26:09e0dd020900 475 void process_config_command(char* received_buffer, int len)
olympux 26:09e0dd020900 476 {
olympux 27:22f289beceb8 477 DBG("Processing configuration command");
olympux 31:2e4b6de6c2f3 478
olympux 26:09e0dd020900 479 // a configuration command always starts with NN
olympux 27:22f289beceb8 480 if ((received_buffer[0] == 'N') && (received_buffer[1] == 'N') &&
olympux 31:2e4b6de6c2f3 481 (received_buffer[2] == 'C') && (received_buffer[3] == 'F')) {
olympux 26:09e0dd020900 482 switch (len) {
olympux 31:2e4b6de6c2f3 483 // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
olympux 31:2e4b6de6c2f3 484 // Format: NNCFDS, NNCFTP, NNCFUP, NNCFTM
olympux 30:15e23257e786 485 case QUERY_NETWORK_CONFIG_CMD_LENGTH: {
olympux 26:09e0dd020900 486 if (strstr(received_buffer, QUERY_IP_CMD) != NULL) {
olympux 26:09e0dd020900 487 udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress()));
olympux 27:22f289beceb8 488 } // NNCFIP
olympux 26:09e0dd020900 489 else if (strstr(received_buffer, QUERY_SUBNET_CMD) != NULL) {
olympux 26:09e0dd020900 490 udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask()));
olympux 27:22f289beceb8 491 } // NNCFSN
olympux 26:09e0dd020900 492 else if (strstr(received_buffer, QUERY_GATEWAY_CMD) != NULL) {
olympux 26:09e0dd020900 493 udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway()));
olympux 27:22f289beceb8 494 } // NNCFGW
olympux 26:09e0dd020900 495 else if (strstr(received_buffer, QUERY_MAC_CMD) != NULL) {
olympux 26:09e0dd020900 496 udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress()));
olympux 27:22f289beceb8 497 } // NNCFMC
olympux 26:09e0dd020900 498 // ask for TCP server port
olympux 26:09e0dd020900 499 else if (strstr(received_buffer, QUERY_TCP_PORT_CMD) != NULL) {
olympux 26:09e0dd020900 500 char port[5];
olympux 26:09e0dd020900 501 sprintf(port, "%5d", tcp_server_local_port);
olympux 26:09e0dd020900 502 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 27:22f289beceb8 503 } // NNCFTP
olympux 26:09e0dd020900 504 // ask for UDP server port
olympux 26:09e0dd020900 505 else if (strstr(received_buffer, QUERY_UDP_PORT_CMD) != NULL) {
olympux 26:09e0dd020900 506 char port[5];
olympux 26:09e0dd020900 507 sprintf(port, "%5d", udp_server_local_port);
olympux 26:09e0dd020900 508 udp_server.sendTo(ep_udp_client, port, strlen(port));
olympux 27:22f289beceb8 509 } // NNCFUP
olympux 26:09e0dd020900 510 else if (strstr(received_buffer, QUERY_UPDATE_TIME_CMD) != NULL) {
olympux 26:09e0dd020900 511 #ifdef NTP
olympux 26:09e0dd020900 512 char str_time[50];
olympux 26:09e0dd020900 513
olympux 26:09e0dd020900 514 DBG("Trying to update time...");
olympux 26:09e0dd020900 515 if (ntp.setTime("0.pool.ntp.org") == 0) {
olympux 26:09e0dd020900 516 DBG("Set time successfully");
olympux 26:09e0dd020900 517 time_t ctTime;
olympux 26:09e0dd020900 518 ctTime = time(NULL);
olympux 26:09e0dd020900 519
olympux 26:09e0dd020900 520 DBG("Time is set to (UTC): %s", ctime(&ctTime));
olympux 26:09e0dd020900 521 sprintf(str_time, "%s", ctime(&ctTime));
olympux 26:09e0dd020900 522 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 523 } else {
olympux 26:09e0dd020900 524 WARN("Error");
olympux 26:09e0dd020900 525 sprintf(str_time, "ERR");
olympux 26:09e0dd020900 526 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 527 }
olympux 26:09e0dd020900 528 #elif
olympux 26:09e0dd020900 529 WARN("NTP disabled");
olympux 26:09e0dd020900 530 sprintf(str_time, "DIS");
olympux 26:09e0dd020900 531 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
olympux 26:09e0dd020900 532 #endif
olympux 27:22f289beceb8 533 } // NNCFTM
olympux 26:09e0dd020900 534 break;
olympux 30:15e23257e786 535 }
olympux 30:15e23257e786 536 // length = 19, SET NETWORK CONFIGURATION
olympux 30:15e23257e786 537 // Format: 4E 4E 43 46 C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01
olympux 30:15e23257e786 538 // (NNCF; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1)
olympux 26:09e0dd020900 539 case SET_NETWORK_CONFIG_CMD_LENGTH: {
olympux 26:09e0dd020900 540 // check device id
olympux 27:22f289beceb8 541 char* id = strstr(received_buffer, DEVICE_CONFIG_CODE);
olympux 26:09e0dd020900 542 if (id == NULL)
olympux 26:09e0dd020900 543 break;
olympux 26:09e0dd020900 544 else if ((id - received_buffer) > 0)
olympux 26:09e0dd020900 545 break;
olympux 26:09e0dd020900 546
olympux 26:09e0dd020900 547 DBG("Received user configuration");
olympux 27:22f289beceb8 548 write_eeprom_network(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char, 15-bytes
olympux 26:09e0dd020900 549 break;
olympux 26:09e0dd020900 550 }
olympux 26:09e0dd020900 551 // length = 12, SET TCP SERVER CONFIGURATION
olympux 26:09e0dd020900 552 // auto update & its time period, TCP server configuration (IP & port)
olympux 27:22f289beceb8 553 // Format: 4E 4E 43 46 'Y' 01 C0 A8 00 09 E0 2E (LSB MSB)
olympux 27:22f289beceb8 554 // NNCF Auto 1s 192.168.0.9 12000
olympux 26:09e0dd020900 555 case UPDATE_TCP_SERVER_INFO_COMMAND_LENGTH: {
olympux 27:22f289beceb8 556 char* id = strstr(received_buffer, DEVICE_CONFIG_CODE);
olympux 26:09e0dd020900 557 if (id == NULL)
olympux 26:09e0dd020900 558 break;
olympux 26:09e0dd020900 559 else if ((id - received_buffer) > 0)
olympux 26:09e0dd020900 560 break;
olympux 26:09e0dd020900 561
olympux 26:09e0dd020900 562 DBG("Received TCP server configuration");
olympux 27:22f289beceb8 563 write_eeprom_tcpserver(&received_buffer[strlen(DEVICE_CONFIG_CODE)]); // parameters from 5th char
olympux 26:09e0dd020900 564 break;
olympux 26:09e0dd020900 565 }
olympux 26:09e0dd020900 566 default:
olympux 26:09e0dd020900 567 break;
olympux 26:09e0dd020900 568 } // switch (n), check configuration command length
olympux 27:22f289beceb8 569 } // if starts with NNCF, a config command
olympux 30:15e23257e786 570 else { // if not a configuration command
olympux 26:09e0dd020900 571 }
olympux 26:09e0dd020900 572 }
olympux 26:09e0dd020900 573
olympux 12:7c152c0ca4d8 574 /*
olympux 30:15e23257e786 575 * Procedure to process receiving protocol, which includes command to control outputs.
olympux 30:15e23257e786 576 * Support both NNIO and RPC-style commands.
olympux 30:15e23257e786 577 * RPC always starts with '/'.
olympux 30:15e23257e786 578 * Return:
olympux 30:15e23257e786 579 * 0 if NNIO protocol;
olympux 30:15e23257e786 580 * length of rpc output buffer if rpc;
olympux 30:15e23257e786 581 * -1 if rpc failed
olympux 28:00c0c20d03c1 582 */
olympux 28:00c0c20d03c1 583 int process_control_command(char* received_buffer, int len)
olympux 26:09e0dd020900 584 {
olympux 26:09e0dd020900 585 char* received_frame;
olympux 30:15e23257e786 586 bool rpc_style;
olympux 26:09e0dd020900 587 int pos;
olympux 31:2e4b6de6c2f3 588 char inbuf[NET_BUF_LEN];
olympux 31:2e4b6de6c2f3 589
olympux 27:22f289beceb8 590 DBG("Processing control command");
olympux 31:2e4b6de6c2f3 591
olympux 30:15e23257e786 592 /*
olympux 30:15e23257e786 593 * This section is for RPC-style command
olympux 30:15e23257e786 594 */
olympux 30:15e23257e786 595 // check if it is a RPC-style command
olympux 30:15e23257e786 596 rpc_style = false;
olympux 27:22f289beceb8 597 strncpy(inbuf, received_buffer, len);
olympux 27:22f289beceb8 598 inbuf[len] = '\r'; // use inbuf for RPC protocol
olympux 27:22f289beceb8 599 inbuf[len+1] = '\n';
olympux 27:22f289beceb8 600 inbuf[len+2] = '\0'; // add CR-LF
olympux 27:22f289beceb8 601 if ((len > 0) && (inbuf[0] == '/')) {
olympux 29:bc052f283ada 602 char obj_name[32];
olympux 27:22f289beceb8 603 bool result;
olympux 31:2e4b6de6c2f3 604
olympux 30:15e23257e786 605 rpc_style = true;
olympux 29:bc052f283ada 606 // find RPC object name
olympux 29:bc052f283ada 607 for (int i = 1; i < strlen(inbuf); i++) {
olympux 29:bc052f283ada 608 if (inbuf[i] != '/') {
olympux 29:bc052f283ada 609 obj_name[i-1] = inbuf[i];
olympux 31:2e4b6de6c2f3 610 } else {
olympux 29:bc052f283ada 611 obj_name[i-1] = '\0';
olympux 29:bc052f283ada 612 break;
olympux 29:bc052f283ada 613 }
olympux 29:bc052f283ada 614 }
olympux 28:00c0c20d03c1 615 DBG("Rpc command = %s", inbuf);
olympux 29:bc052f283ada 616 /*
olympux 29:bc052f283ada 617 * execute RPC command, return reply length and reply in rpc_outbuf
olympux 29:bc052f283ada 618 */
olympux 31:2e4b6de6c2f3 619 result = RPC::call(inbuf, network_output_buffer);
olympux 27:22f289beceb8 620 if (result) {
olympux 29:bc052f283ada 621 // re-arrange output buffer as following: object_name:output_value
olympux 31:2e4b6de6c2f3 622 strcpy(inbuf, network_output_buffer); // use inbuf as temp
olympux 31:2e4b6de6c2f3 623 strcpy(network_output_buffer, obj_name); // rpc object name
olympux 31:2e4b6de6c2f3 624 strcat(network_output_buffer, ":");
olympux 31:2e4b6de6c2f3 625 strcat(network_output_buffer, inbuf); // concat rpc reply
olympux 31:2e4b6de6c2f3 626 strcat(network_output_buffer, "\r\n"); // CR-LF
olympux 31:2e4b6de6c2f3 627 int j = strlen(network_output_buffer);
olympux 31:2e4b6de6c2f3 628 DBG("Reply of rpc command on \"%s\" (%d bytes): %s", obj_name, j, network_output_buffer);
olympux 29:bc052f283ada 629 return j; // return length of rpc_outbuf
olympux 31:2e4b6de6c2f3 630 } else {
olympux 27:22f289beceb8 631 ERR("Failed: %s", inbuf);
olympux 28:00c0c20d03c1 632 return -1;
olympux 27:22f289beceb8 633 }
olympux 27:22f289beceb8 634 }
olympux 31:2e4b6de6c2f3 635
olympux 28:00c0c20d03c1 636 /*
olympux 28:00c0c20d03c1 637 * This section below is for NNIO protocol
olympux 28:00c0c20d03c1 638 */
olympux 30:15e23257e786 639 while ((!rpc_style) && (len >= RECEIVING_PROTOCOL_LENGTH)) {
olympux 26:09e0dd020900 640 // find device ID
olympux 26:09e0dd020900 641 DBG("Checking device ID...");
olympux 27:22f289beceb8 642 char* id = strstr(received_buffer, DEVICE_CONTROL_CODE);
olympux 26:09e0dd020900 643 if (id == NULL) {
olympux 26:09e0dd020900 644 DBG("No device ID found");
olympux 26:09e0dd020900 645 break;
olympux 26:09e0dd020900 646 }
olympux 26:09e0dd020900 647 pos = id - received_buffer;
olympux 26:09e0dd020900 648 DBG("Found a frame at %d", pos);
olympux 31:2e4b6de6c2f3 649
olympux 26:09e0dd020900 650 // extract this frame
olympux 26:09e0dd020900 651 received_frame = &received_buffer[pos];
olympux 26:09e0dd020900 652 // calculate the rest
olympux 26:09e0dd020900 653 received_buffer = &received_buffer[pos + RECEIVING_PROTOCOL_LENGTH];
olympux 26:09e0dd020900 654 len -= RECEIVING_PROTOCOL_LENGTH;
olympux 31:2e4b6de6c2f3 655
olympux 26:09e0dd020900 656 // process this received frame
olympux 26:09e0dd020900 657 // firstly, update outputs if required
olympux 26:09e0dd020900 658 // digital outputs
olympux 26:09e0dd020900 659 if (received_frame[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 660 DBG("Update digital outputs");
olympux 26:09e0dd020900 661 char str_dout[9];
olympux 26:09e0dd020900 662 memcpy(str_dout, &received_frame[RECEIVING_PROTOCOL_DO_POS], 8);
olympux 26:09e0dd020900 663 str_dout[8] = '\0';
olympux 26:09e0dd020900 664 update_digital_outputs(str_dout);
olympux 26:09e0dd020900 665 }
olympux 26:09e0dd020900 666 // analog output 0
olympux 26:09e0dd020900 667 if (received_frame[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 668 DBG("Update analog output 0");
olympux 26:09e0dd020900 669 //TODO Update analog output
olympux 26:09e0dd020900 670 }
olympux 26:09e0dd020900 671 // analog output 1
olympux 26:09e0dd020900 672 if (received_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 673 DBG("Update analog output 1");
olympux 26:09e0dd020900 674 //TODO Update analog output
olympux 26:09e0dd020900 675 }
olympux 26:09e0dd020900 676 // UART
olympux 26:09e0dd020900 677 if (received_frame[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
olympux 26:09e0dd020900 678 DBG("UART data: ");
olympux 26:09e0dd020900 679 char str_uart[33];
olympux 26:09e0dd020900 680 memcpy(str_uart, &received_frame[RECEIVING_PROTOCOL_UART_POS], 32);
olympux 26:09e0dd020900 681 str_uart[32] = '\0';
olympux 26:09e0dd020900 682 uart.printf("%s\r\n", str_uart);
olympux 26:09e0dd020900 683 }
olympux 26:09e0dd020900 684 }
olympux 31:2e4b6de6c2f3 685
olympux 27:22f289beceb8 686 DBG("Successfully processed.");
olympux 28:00c0c20d03c1 687 return 0;
olympux 26:09e0dd020900 688 }
olympux 26:09e0dd020900 689
olympux 26:09e0dd020900 690
olympux 26:09e0dd020900 691 /*
olympux 26:09e0dd020900 692 * W5500 Ethernet init
olympux 26:09e0dd020900 693 */
olympux 31:2e4b6de6c2f3 694 int ethernet_init(void)
olympux 31:2e4b6de6c2f3 695 {
olympux 26:09e0dd020900 696 int dhcp_ret, ret;
olympux 31:2e4b6de6c2f3 697
olympux 27:22f289beceb8 698 DBG("Initialising ethernet...");
olympux 31:2e4b6de6c2f3 699
olympux 26:09e0dd020900 700 // if not configured, try dhcp
olympux 26:09e0dd020900 701 dhcp_ret = -1;
olympux 26:09e0dd020900 702 if (configured_ip != DEFAULT_ENABLE_FLAG_VALUE) {
olympux 27:22f289beceb8 703 DBG("Connecting to DHCP server...");
olympux 26:09e0dd020900 704 dhcp_ret = eth.init(u8mac);
olympux 26:09e0dd020900 705 if (dhcp_ret == 0)
olympux 26:09e0dd020900 706 dhcp_ret = eth.connect();
olympux 26:09e0dd020900 707 }
olympux 31:2e4b6de6c2f3 708
olympux 26:09e0dd020900 709 if (dhcp_ret != 0) {
olympux 26:09e0dd020900 710 DBG("No DHCP, load static IP configuration");
olympux 26:09e0dd020900 711 ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
olympux 26:09e0dd020900 712 } else {
olympux 26:09e0dd020900 713 snprintf(str_ip_addr, 16, "%s", eth.getIPAddress());
olympux 26:09e0dd020900 714 snprintf(str_ip_subnet, 16, "%s", eth.getNetworkMask());
olympux 26:09e0dd020900 715 snprintf(str_ip_gateway, 16, "%s", eth.getGateway());
olympux 26:09e0dd020900 716 ret = 0;
olympux 26:09e0dd020900 717 }
olympux 26:09e0dd020900 718
olympux 26:09e0dd020900 719 if (ret == 0) {
olympux 26:09e0dd020900 720 DBG("Initialized, MAC: %s", eth.getMACAddress());
olympux 26:09e0dd020900 721 } else {
olympux 26:09e0dd020900 722 ERR("Error eth.init() - ret = %d", ret);
olympux 26:09e0dd020900 723 return -1;
olympux 26:09e0dd020900 724 }
olympux 26:09e0dd020900 725
olympux 26:09e0dd020900 726 ret = eth.connect();
olympux 26:09e0dd020900 727 if (!ret) {
olympux 26:09e0dd020900 728 DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
olympux 26:09e0dd020900 729 } else {
olympux 26:09e0dd020900 730 ERR("Error eth.connect() - ret = %d", ret);
olympux 26:09e0dd020900 731 return -1;
olympux 26:09e0dd020900 732 }
olympux 31:2e4b6de6c2f3 733
olympux 26:09e0dd020900 734 return 0;
olympux 26:09e0dd020900 735 }
olympux 26:09e0dd020900 736
olympux 26:09e0dd020900 737
olympux 26:09e0dd020900 738 /*
olympux 26:09e0dd020900 739 * Update digital outputs according to receiving protocol
olympux 12:7c152c0ca4d8 740 */
olympux 31:2e4b6de6c2f3 741 void update_digital_outputs(char* buf)
olympux 31:2e4b6de6c2f3 742 {
olympux 18:ca499a2e7da6 743 DBG("Digital outputs: %s", buf);
olympux 31:2e4b6de6c2f3 744
olympux 12:7c152c0ca4d8 745 dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 746 dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 747 dout2 = (buf[2] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 748 dout3 = (buf[3] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 749 dout4 = (buf[4] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 750 dout5 = (buf[5] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 751 dout6 = (buf[6] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 752 dout7 = (buf[7] == DIGITAL_HIGH)? 1 : 0;
olympux 12:7c152c0ca4d8 753 }
olympux 12:7c152c0ca4d8 754
olympux 26:09e0dd020900 755 /*
olympux 26:09e0dd020900 756 * Prepare a frame for sending protocol, which includes status of I/Os
olympux 26:09e0dd020900 757 */
olympux 31:2e4b6de6c2f3 758 void update_sending_frame(char* buf)
olympux 31:2e4b6de6c2f3 759 {
olympux 27:22f289beceb8 760 memcpy(&buf[SENDING_PROTOCOL_ID_POS], DEVICE_CONTROL_CODE, 4); // device id
olympux 12:7c152c0ca4d8 761 memcpy(&buf[SENDING_PROTOCOL_MAC_POS], &u8mac, 6);
olympux 12:7c152c0ca4d8 762 memcpy(&buf[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4);
olympux 31:2e4b6de6c2f3 763
olympux 12:7c152c0ca4d8 764 buf[SENDING_PROTOCOL_DI_POS+0] = (din0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 765 buf[SENDING_PROTOCOL_DI_POS+1] = (din1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 766 buf[SENDING_PROTOCOL_DI_POS+2] = (din2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 767 buf[SENDING_PROTOCOL_DI_POS+3] = (din3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 768 buf[SENDING_PROTOCOL_DI_POS+4] = (din4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 769 buf[SENDING_PROTOCOL_DI_POS+5] = (din5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 770 buf[SENDING_PROTOCOL_DI_POS+6] = (din6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 771 buf[SENDING_PROTOCOL_DI_POS+7] = (din7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 31:2e4b6de6c2f3 772
olympux 12:7c152c0ca4d8 773 buf[SENDING_PROTOCOL_DO_POS+0] = (dout0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 774 buf[SENDING_PROTOCOL_DO_POS+1] = (dout1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 775 buf[SENDING_PROTOCOL_DO_POS+2] = (dout2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 776 buf[SENDING_PROTOCOL_DO_POS+3] = (dout3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 777 buf[SENDING_PROTOCOL_DO_POS+4] = (dout4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 778 buf[SENDING_PROTOCOL_DO_POS+5] = (dout5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 779 buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 12:7c152c0ca4d8 780 buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW;
olympux 31:2e4b6de6c2f3 781
olympux 15:edeb0aed160d 782 uint16_t val = ain0.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 783 memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB
olympux 15:edeb0aed160d 784 val = ain1.read_u16(); // 16-bits normalised
olympux 15:edeb0aed160d 785 memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB
olympux 23:47ee805435b1 786 val = 0x1234;
olympux 15:edeb0aed160d 787 memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB
olympux 23:47ee805435b1 788 val = 0x5678;
olympux 15:edeb0aed160d 789 memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB
olympux 12:7c152c0ca4d8 790 buf[SENDING_PROTOCOL_CR_POS] = 0x0D;
olympux 12:7c152c0ca4d8 791 buf[SENDING_PROTOCOL_CR_POS+1] = '\0';
olympux 4:568c97f2a407 792 }