Example program for debugging failing event queue.

Committer:
sjalloq
Date:
Sun Apr 07 14:13:13 2019 +0000
Revision:
0:c9670ffe21d4
First release of TCPSocket example.  Client sigio() never gets called.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjalloq 0:c9670ffe21d4 1 /* mbed Microcontroller Library
sjalloq 0:c9670ffe21d4 2 * Copyright (c) 2018 ARM Limited
sjalloq 0:c9670ffe21d4 3 * SPDX-License-Identifier: Apache-2.0
sjalloq 0:c9670ffe21d4 4 */
sjalloq 0:c9670ffe21d4 5
sjalloq 0:c9670ffe21d4 6 /*
sjalloq 0:c9670ffe21d4 7 */
sjalloq 0:c9670ffe21d4 8
sjalloq 0:c9670ffe21d4 9 #include "mbed.h"
sjalloq 0:c9670ffe21d4 10 #include "EthernetInterface.h"
sjalloq 0:c9670ffe21d4 11 #include "TCPSocket.h"
sjalloq 0:c9670ffe21d4 12 #include "SocketAddress.h"
sjalloq 0:c9670ffe21d4 13 #include <string.h>
sjalloq 0:c9670ffe21d4 14
sjalloq 0:c9670ffe21d4 15 #define PORT 7070
sjalloq 0:c9670ffe21d4 16 #define FLAG_SOCKET_ACCEPT (1<<0)
sjalloq 0:c9670ffe21d4 17 #define FLAG_SOCKET_CLOSED (1<<1)
sjalloq 0:c9670ffe21d4 18 #define FLAG_CLIENT_CONNECT (1<<2)
sjalloq 0:c9670ffe21d4 19
sjalloq 0:c9670ffe21d4 20 Serial Uart(USBTX,USBRX);
sjalloq 0:c9670ffe21d4 21 BusOut Leds(LED1,LED2,LED3,LED4);
sjalloq 0:c9670ffe21d4 22
sjalloq 0:c9670ffe21d4 23 EventQueue *queue = mbed_event_queue();
sjalloq 0:c9670ffe21d4 24 EventFlags evntFlags;
sjalloq 0:c9670ffe21d4 25 EthernetInterface eth;
sjalloq 0:c9670ffe21d4 26 TCPSocket socket;
sjalloq 0:c9670ffe21d4 27 TCPSocket *client;
sjalloq 0:c9670ffe21d4 28
sjalloq 0:c9670ffe21d4 29
sjalloq 0:c9670ffe21d4 30 void set_leds(uint32_t leds) {
sjalloq 0:c9670ffe21d4 31 Leds = leds & 0xF;
sjalloq 0:c9670ffe21d4 32 }
sjalloq 0:c9670ffe21d4 33
sjalloq 0:c9670ffe21d4 34 void client_sigio(TCPSocket *client, EventFlags *evnt) {
sjalloq 0:c9670ffe21d4 35
sjalloq 0:c9670ffe21d4 36 static enum {
sjalloq 0:c9670ffe21d4 37 RECEIVE,
sjalloq 0:c9670ffe21d4 38 CLOSE
sjalloq 0:c9670ffe21d4 39 } next_state = RECEIVE;
sjalloq 0:c9670ffe21d4 40
sjalloq 0:c9670ffe21d4 41 static char buf[4];
sjalloq 0:c9670ffe21d4 42 static char *buf_p = buf;
sjalloq 0:c9670ffe21d4 43 static uint32_t remaining = 4;
sjalloq 0:c9670ffe21d4 44
sjalloq 0:c9670ffe21d4 45 nsapi_size_or_error_t szerr;
sjalloq 0:c9670ffe21d4 46
sjalloq 0:c9670ffe21d4 47 Uart.printf("client_sigio: state=%d\n\r", next_state);
sjalloq 0:c9670ffe21d4 48
sjalloq 0:c9670ffe21d4 49 switch(next_state) {
sjalloq 0:c9670ffe21d4 50 case CLOSE: {
sjalloq 0:c9670ffe21d4 51 client->close();
sjalloq 0:c9670ffe21d4 52 evnt->clear(FLAG_CLIENT_CONNECT);
sjalloq 0:c9670ffe21d4 53 next_state = RECEIVE;
sjalloq 0:c9670ffe21d4 54 break;
sjalloq 0:c9670ffe21d4 55 }
sjalloq 0:c9670ffe21d4 56 case RECEIVE: {
sjalloq 0:c9670ffe21d4 57 szerr = client->recv(buf_p, remaining);
sjalloq 0:c9670ffe21d4 58 Uart.printf("Received %0d bytes\n\r", szerr);
sjalloq 0:c9670ffe21d4 59 if(szerr >= 0) {
sjalloq 0:c9670ffe21d4 60 buf_p += szerr;
sjalloq 0:c9670ffe21d4 61 remaining -= szerr;
sjalloq 0:c9670ffe21d4 62 if(0 == remaining) {
sjalloq 0:c9670ffe21d4 63 // Send transaction
sjalloq 0:c9670ffe21d4 64 Uart.printf("Received transaction: buf[3]=0x%0x buf[2]=0x%0x buf[1]=0x%0x buf[0]=0x%0x ",
sjalloq 0:c9670ffe21d4 65 buf[3], buf[2], buf[1], buf[0]);
sjalloq 0:c9670ffe21d4 66 }
sjalloq 0:c9670ffe21d4 67 break;
sjalloq 0:c9670ffe21d4 68 } else {
sjalloq 0:c9670ffe21d4 69 if(NSAPI_ERROR_WOULD_BLOCK == szerr) {
sjalloq 0:c9670ffe21d4 70 break;
sjalloq 0:c9670ffe21d4 71 } else if(NSAPI_ERROR_NO_SOCKET == szerr) {
sjalloq 0:c9670ffe21d4 72 next_state = CLOSE;
sjalloq 0:c9670ffe21d4 73 // now fall through to default
sjalloq 0:c9670ffe21d4 74 } else {
sjalloq 0:c9670ffe21d4 75 Uart.printf("client_sigio: unhandled error on socket->recv(): %d\n\r", szerr);
sjalloq 0:c9670ffe21d4 76 }
sjalloq 0:c9670ffe21d4 77 }
sjalloq 0:c9670ffe21d4 78 }
sjalloq 0:c9670ffe21d4 79 default: {
sjalloq 0:c9670ffe21d4 80 // something went wrong so reset the buffer
sjalloq 0:c9670ffe21d4 81 buf_p = buf;
sjalloq 0:c9670ffe21d4 82 remaining = 4;
sjalloq 0:c9670ffe21d4 83 }
sjalloq 0:c9670ffe21d4 84 }
sjalloq 0:c9670ffe21d4 85 }
sjalloq 0:c9670ffe21d4 86
sjalloq 0:c9670ffe21d4 87
sjalloq 0:c9670ffe21d4 88 void server_sigio(TCPSocket *server, TCPSocket *client, EventFlags *evnt) {
sjalloq 0:c9670ffe21d4 89
sjalloq 0:c9670ffe21d4 90 static enum {
sjalloq 0:c9670ffe21d4 91 LISTENING,
sjalloq 0:c9670ffe21d4 92 ACCEPT,
sjalloq 0:c9670ffe21d4 93 CLOSE
sjalloq 0:c9670ffe21d4 94 } next_state = LISTENING;
sjalloq 0:c9670ffe21d4 95
sjalloq 0:c9670ffe21d4 96 nsapi_error_t err;
sjalloq 0:c9670ffe21d4 97
sjalloq 0:c9670ffe21d4 98 Uart.printf("server_sigio: state=%d\n\r", next_state);
sjalloq 0:c9670ffe21d4 99
sjalloq 0:c9670ffe21d4 100 switch (next_state) {
sjalloq 0:c9670ffe21d4 101 case LISTENING: {
sjalloq 0:c9670ffe21d4 102 client = server->accept(&err);
sjalloq 0:c9670ffe21d4 103 switch(err) {
sjalloq 0:c9670ffe21d4 104 case NSAPI_ERROR_WOULD_BLOCK:
sjalloq 0:c9670ffe21d4 105 // Continue to listen
sjalloq 0:c9670ffe21d4 106 break;
sjalloq 0:c9670ffe21d4 107 case NSAPI_ERROR_OK: {
sjalloq 0:c9670ffe21d4 108 // Accepted connection
sjalloq 0:c9670ffe21d4 109 evnt->set(FLAG_CLIENT_CONNECT);
sjalloq 0:c9670ffe21d4 110 Event<void()> client_handler = queue->event(client_sigio, client, evnt);
sjalloq 0:c9670ffe21d4 111 client->set_blocking(false);
sjalloq 0:c9670ffe21d4 112 client->sigio(client_handler);
sjalloq 0:c9670ffe21d4 113 Uart.printf("calling client_handler()\n\r");
sjalloq 0:c9670ffe21d4 114 client_handler.post();
sjalloq 0:c9670ffe21d4 115 next_state = ACCEPT;
sjalloq 0:c9670ffe21d4 116 Uart.printf("Just set state=%d\n\r", next_state);
sjalloq 0:c9670ffe21d4 117 break;
sjalloq 0:c9670ffe21d4 118 }
sjalloq 0:c9670ffe21d4 119 default:
sjalloq 0:c9670ffe21d4 120 // Error in connection phase
sjalloq 0:c9670ffe21d4 121 Uart.printf("server_sigio: err = %d\n\r", err);
sjalloq 0:c9670ffe21d4 122 //next_state = CLOSE;
sjalloq 0:c9670ffe21d4 123 break;
sjalloq 0:c9670ffe21d4 124 }
sjalloq 0:c9670ffe21d4 125 break;
sjalloq 0:c9670ffe21d4 126 }
sjalloq 0:c9670ffe21d4 127 case ACCEPT: {
sjalloq 0:c9670ffe21d4 128 //if (err == NSAPI_ERROR_WOULD_BLOCK)
sjalloq 0:c9670ffe21d4 129 // break;
sjalloq 0:c9670ffe21d4 130 //next_state = CLOSE;
sjalloq 0:c9670ffe21d4 131 Uart.printf("server_sigio: evntFlags=0x%0x\n\r", evnt->get());
sjalloq 0:c9670ffe21d4 132 break;
sjalloq 0:c9670ffe21d4 133 }
sjalloq 0:c9670ffe21d4 134 case CLOSE: {
sjalloq 0:c9670ffe21d4 135 Uart.printf("server_sigio: CLOSE state\n\r");
sjalloq 0:c9670ffe21d4 136 //server->close();
sjalloq 0:c9670ffe21d4 137 //flgs->set(FLAG_SOCKET_CLOSED);
sjalloq 0:c9670ffe21d4 138 next_state = LISTENING;
sjalloq 0:c9670ffe21d4 139 break;
sjalloq 0:c9670ffe21d4 140 }
sjalloq 0:c9670ffe21d4 141 default: {
sjalloq 0:c9670ffe21d4 142 break;
sjalloq 0:c9670ffe21d4 143 }
sjalloq 0:c9670ffe21d4 144 }
sjalloq 0:c9670ffe21d4 145 }
sjalloq 0:c9670ffe21d4 146
sjalloq 0:c9670ffe21d4 147
sjalloq 0:c9670ffe21d4 148 // main() runs in its own thread in the OS
sjalloq 0:c9670ffe21d4 149 int main()
sjalloq 0:c9670ffe21d4 150 {
sjalloq 0:c9670ffe21d4 151 nsapi_error_t err;
sjalloq 0:c9670ffe21d4 152 nsapi_size_or_error_t szerr;
sjalloq 0:c9670ffe21d4 153
sjalloq 0:c9670ffe21d4 154 Event<void()> server_handler = queue->event(server_sigio, &socket, client, &evntFlags);
sjalloq 0:c9670ffe21d4 155
sjalloq 0:c9670ffe21d4 156 //Eventing evnt(&evntFlags);
sjalloq 0:c9670ffe21d4 157 //Uart.attach(&evnt, &Eventing::print_event_flags);
sjalloq 0:c9670ffe21d4 158
sjalloq 0:c9670ffe21d4 159 if(eth.connect() != NSAPI_ERROR_OK) {
sjalloq 0:c9670ffe21d4 160 Uart.printf("Failed to connect to ethernet\n\r");
sjalloq 0:c9670ffe21d4 161 while(1) ;
sjalloq 0:c9670ffe21d4 162 } else {
sjalloq 0:c9670ffe21d4 163 Uart.printf("Connected to ethernet, IP address = %s\n\r", eth.get_ip_address());
sjalloq 0:c9670ffe21d4 164 }
sjalloq 0:c9670ffe21d4 165
sjalloq 0:c9670ffe21d4 166 err = socket.open(&eth);
sjalloq 0:c9670ffe21d4 167 if(err != NSAPI_ERROR_OK) {
sjalloq 0:c9670ffe21d4 168 Uart.printf("Socket.open() failed with error: %d\n\r", err);
sjalloq 0:c9670ffe21d4 169 }
sjalloq 0:c9670ffe21d4 170
sjalloq 0:c9670ffe21d4 171 err = socket.bind(PORT);
sjalloq 0:c9670ffe21d4 172 if(err != NSAPI_ERROR_OK) {
sjalloq 0:c9670ffe21d4 173 Uart.printf("Socket.bind() failed with error: %d\n\r", err);
sjalloq 0:c9670ffe21d4 174 }
sjalloq 0:c9670ffe21d4 175
sjalloq 0:c9670ffe21d4 176 err = socket.listen();
sjalloq 0:c9670ffe21d4 177 if(err != NSAPI_ERROR_OK) {
sjalloq 0:c9670ffe21d4 178 Uart.printf("Socket.listen() failed with error: %d\n\r", err);
sjalloq 0:c9670ffe21d4 179 }
sjalloq 0:c9670ffe21d4 180
sjalloq 0:c9670ffe21d4 181 socket.set_blocking(false);
sjalloq 0:c9670ffe21d4 182 socket.sigio(server_handler);
sjalloq 0:c9670ffe21d4 183 server_handler.post();
sjalloq 0:c9670ffe21d4 184
sjalloq 0:c9670ffe21d4 185 while(1) {
sjalloq 0:c9670ffe21d4 186 // All functionality handled within sigio() callbacks.
sjalloq 0:c9670ffe21d4 187 }
sjalloq 0:c9670ffe21d4 188 }