SmartCard reader. PC is interface through USB or TCPport. SmartCard is interfaced through UART@ 1MHz, DIV372
Dependencies: EthernetNetIf mbed
TcpServer.cpp@0:5bf6fcf71548, 2011-02-27 (annotated)
- Committer:
- bcalin1984
- Date:
- Sun Feb 27 22:20:40 2011 +0000
- Revision:
- 0:5bf6fcf71548
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcalin1984 | 0:5bf6fcf71548 | 1 | /* |
bcalin1984 | 0:5bf6fcf71548 | 2 | A Tcp server based on the : |
bcalin1984 | 0:5bf6fcf71548 | 3 | // TCP Echo server |
bcalin1984 | 0:5bf6fcf71548 | 4 | // 2010/9/7 |
bcalin1984 | 0:5bf6fcf71548 | 5 | |
bcalin1984 | 0:5bf6fcf71548 | 6 | Modified by Calin Bira |
bcalin1984 | 0:5bf6fcf71548 | 7 | Date: 28.02.2011 |
bcalin1984 | 0:5bf6fcf71548 | 8 | */ |
bcalin1984 | 0:5bf6fcf71548 | 9 | |
bcalin1984 | 0:5bf6fcf71548 | 10 | |
bcalin1984 | 0:5bf6fcf71548 | 11 | |
bcalin1984 | 0:5bf6fcf71548 | 12 | |
bcalin1984 | 0:5bf6fcf71548 | 13 | #include "mbed.h" |
bcalin1984 | 0:5bf6fcf71548 | 14 | #include "TcpServer.h" |
bcalin1984 | 0:5bf6fcf71548 | 15 | #include <stdarg.h> |
bcalin1984 | 0:5bf6fcf71548 | 16 | |
bcalin1984 | 0:5bf6fcf71548 | 17 | //#define DEBUG |
bcalin1984 | 0:5bf6fcf71548 | 18 | |
bcalin1984 | 0:5bf6fcf71548 | 19 | #ifdef DEBUG |
bcalin1984 | 0:5bf6fcf71548 | 20 | #define dprintf printf |
bcalin1984 | 0:5bf6fcf71548 | 21 | #else |
bcalin1984 | 0:5bf6fcf71548 | 22 | #define dprintf noprintf |
bcalin1984 | 0:5bf6fcf71548 | 23 | #endif |
bcalin1984 | 0:5bf6fcf71548 | 24 | TcpServer::TcpServer(int port) |
bcalin1984 | 0:5bf6fcf71548 | 25 | { |
bcalin1984 | 0:5bf6fcf71548 | 26 | Setup(port); |
bcalin1984 | 0:5bf6fcf71548 | 27 | } |
bcalin1984 | 0:5bf6fcf71548 | 28 | |
bcalin1984 | 0:5bf6fcf71548 | 29 | void TcpServer::onConnectedTCPSocketEvent(TCPSocketEvent e) |
bcalin1984 | 0:5bf6fcf71548 | 30 | { |
bcalin1984 | 0:5bf6fcf71548 | 31 | switch(e) |
bcalin1984 | 0:5bf6fcf71548 | 32 | { |
bcalin1984 | 0:5bf6fcf71548 | 33 | case TCPSOCKET_CONNECTED: |
bcalin1984 | 0:5bf6fcf71548 | 34 | dprintf("TCP Socket Connected\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 35 | break; |
bcalin1984 | 0:5bf6fcf71548 | 36 | case TCPSOCKET_WRITEABLE: |
bcalin1984 | 0:5bf6fcf71548 | 37 | //Can now write some data... |
bcalin1984 | 0:5bf6fcf71548 | 38 | dprintf("TCP Socket Writable\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 39 | break; |
bcalin1984 | 0:5bf6fcf71548 | 40 | case TCPSOCKET_READABLE: |
bcalin1984 | 0:5bf6fcf71548 | 41 | //Can now read dome data... |
bcalin1984 | 0:5bf6fcf71548 | 42 | dprintf("TCP Socket Readable\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 43 | // Read in any available data into the buffer |
bcalin1984 | 0:5bf6fcf71548 | 44 | |
bcalin1984 | 0:5bf6fcf71548 | 45 | while ( int len = pConnectedSock->recv(buff, 256) ) |
bcalin1984 | 0:5bf6fcf71548 | 46 | { |
bcalin1984 | 0:5bf6fcf71548 | 47 | onReceive(buff,len); |
bcalin1984 | 0:5bf6fcf71548 | 48 | //pConnectedSock->send(buff, len); |
bcalin1984 | 0:5bf6fcf71548 | 49 | //buff[len]=0; // make terminater |
bcalin1984 | 0:5bf6fcf71548 | 50 | //dprintf("Received&Wrote:%s\r\n",buff); |
bcalin1984 | 0:5bf6fcf71548 | 51 | } |
bcalin1984 | 0:5bf6fcf71548 | 52 | break; |
bcalin1984 | 0:5bf6fcf71548 | 53 | case TCPSOCKET_CONTIMEOUT: |
bcalin1984 | 0:5bf6fcf71548 | 54 | dprintf("TCP Socket Timeout\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 55 | break; |
bcalin1984 | 0:5bf6fcf71548 | 56 | case TCPSOCKET_CONRST: |
bcalin1984 | 0:5bf6fcf71548 | 57 | dprintf("TCP Socket CONRST\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 58 | break; |
bcalin1984 | 0:5bf6fcf71548 | 59 | case TCPSOCKET_CONABRT: |
bcalin1984 | 0:5bf6fcf71548 | 60 | dprintf("TCP Socket CONABRT\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 61 | break; |
bcalin1984 | 0:5bf6fcf71548 | 62 | case TCPSOCKET_ERROR: |
bcalin1984 | 0:5bf6fcf71548 | 63 | dprintf("TCP Socket Error\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 64 | break; |
bcalin1984 | 0:5bf6fcf71548 | 65 | case TCPSOCKET_DISCONNECTED: |
bcalin1984 | 0:5bf6fcf71548 | 66 | //Close socket... |
bcalin1984 | 0:5bf6fcf71548 | 67 | dprintf("TCP Socket Disconnected\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 68 | pConnectedSock->close(); |
bcalin1984 | 0:5bf6fcf71548 | 69 | break; |
bcalin1984 | 0:5bf6fcf71548 | 70 | default: |
bcalin1984 | 0:5bf6fcf71548 | 71 | dprintf("DEFAULT\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 72 | } |
bcalin1984 | 0:5bf6fcf71548 | 73 | } |
bcalin1984 | 0:5bf6fcf71548 | 74 | |
bcalin1984 | 0:5bf6fcf71548 | 75 | |
bcalin1984 | 0:5bf6fcf71548 | 76 | void TcpServer::onListeningTCPSocketEvent(TCPSocketEvent e) |
bcalin1984 | 0:5bf6fcf71548 | 77 | { |
bcalin1984 | 0:5bf6fcf71548 | 78 | switch(e) |
bcalin1984 | 0:5bf6fcf71548 | 79 | { |
bcalin1984 | 0:5bf6fcf71548 | 80 | case TCPSOCKET_ACCEPT: |
bcalin1984 | 0:5bf6fcf71548 | 81 | { |
bcalin1984 | 0:5bf6fcf71548 | 82 | dprintf("Listening: TCP Socket Accepted\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 83 | // Accepts connection from client and gets connected socket. |
bcalin1984 | 0:5bf6fcf71548 | 84 | err=ListeningSock.accept(&client, &pConnectedSock); |
bcalin1984 | 0:5bf6fcf71548 | 85 | if (err) { |
bcalin1984 | 0:5bf6fcf71548 | 86 | dprintf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 87 | return; //Error in accept, discard connection |
bcalin1984 | 0:5bf6fcf71548 | 88 | } |
bcalin1984 | 0:5bf6fcf71548 | 89 | // Setup the new socket events |
bcalin1984 | 0:5bf6fcf71548 | 90 | pConnectedSock->setOnEvent(this, &TcpServer::onConnectedTCPSocketEvent); |
bcalin1984 | 0:5bf6fcf71548 | 91 | // We can find out from where the connection is coming by looking at the |
bcalin1984 | 0:5bf6fcf71548 | 92 | // Host parameter of the accept() method |
bcalin1984 | 0:5bf6fcf71548 | 93 | IpAddr clientIp = client.getIp(); |
bcalin1984 | 0:5bf6fcf71548 | 94 | dprintf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", |
bcalin1984 | 0:5bf6fcf71548 | 95 | clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
bcalin1984 | 0:5bf6fcf71548 | 96 | break; |
bcalin1984 | 0:5bf6fcf71548 | 97 | } |
bcalin1984 | 0:5bf6fcf71548 | 98 | // the following cases will not happen |
bcalin1984 | 0:5bf6fcf71548 | 99 | case TCPSOCKET_CONNECTED: |
bcalin1984 | 0:5bf6fcf71548 | 100 | dprintf("Listening: TCP Socket Connected\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 101 | break; |
bcalin1984 | 0:5bf6fcf71548 | 102 | case TCPSOCKET_WRITEABLE: |
bcalin1984 | 0:5bf6fcf71548 | 103 | dprintf("Listening: TCP Socket Writable\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 104 | break; |
bcalin1984 | 0:5bf6fcf71548 | 105 | case TCPSOCKET_READABLE: |
bcalin1984 | 0:5bf6fcf71548 | 106 | dprintf("Listening: TCP Socket Readable\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 107 | break; |
bcalin1984 | 0:5bf6fcf71548 | 108 | case TCPSOCKET_CONTIMEOUT: |
bcalin1984 | 0:5bf6fcf71548 | 109 | dprintf("Listening: TCP Socket Timeout\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 110 | break; |
bcalin1984 | 0:5bf6fcf71548 | 111 | case TCPSOCKET_CONRST: |
bcalin1984 | 0:5bf6fcf71548 | 112 | dprintf("Listening: TCP Socket CONRST\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 113 | break; |
bcalin1984 | 0:5bf6fcf71548 | 114 | case TCPSOCKET_CONABRT: |
bcalin1984 | 0:5bf6fcf71548 | 115 | dprintf("Listening: TCP Socket CONABRT\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 116 | break; |
bcalin1984 | 0:5bf6fcf71548 | 117 | case TCPSOCKET_ERROR: |
bcalin1984 | 0:5bf6fcf71548 | 118 | dprintf("Listening: TCP Socket Error\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 119 | break; |
bcalin1984 | 0:5bf6fcf71548 | 120 | case TCPSOCKET_DISCONNECTED: |
bcalin1984 | 0:5bf6fcf71548 | 121 | //Close socket... |
bcalin1984 | 0:5bf6fcf71548 | 122 | dprintf("Listening: TCP Socket Disconnected\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 123 | ListeningSock.close(); |
bcalin1984 | 0:5bf6fcf71548 | 124 | break; |
bcalin1984 | 0:5bf6fcf71548 | 125 | default: |
bcalin1984 | 0:5bf6fcf71548 | 126 | dprintf("DEFAULT\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 127 | }; |
bcalin1984 | 0:5bf6fcf71548 | 128 | } |
bcalin1984 | 0:5bf6fcf71548 | 129 | |
bcalin1984 | 0:5bf6fcf71548 | 130 | int TcpServer::Setup(int port) { |
bcalin1984 | 0:5bf6fcf71548 | 131 | dprintf("\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 132 | dprintf("Setting up...\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 133 | EthernetErr ethErr = eth.setup(); |
bcalin1984 | 0:5bf6fcf71548 | 134 | if(ethErr) |
bcalin1984 | 0:5bf6fcf71548 | 135 | { |
bcalin1984 | 0:5bf6fcf71548 | 136 | dprintf("Error %d in setup.\r\n", ethErr); |
bcalin1984 | 0:5bf6fcf71548 | 137 | return -1; |
bcalin1984 | 0:5bf6fcf71548 | 138 | } |
bcalin1984 | 0:5bf6fcf71548 | 139 | dprintf("Setup OK\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 140 | |
bcalin1984 | 0:5bf6fcf71548 | 141 | IpAddr ip = eth.getIp(); |
bcalin1984 | 0:5bf6fcf71548 | 142 | dprintf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); |
bcalin1984 | 0:5bf6fcf71548 | 143 | |
bcalin1984 | 0:5bf6fcf71548 | 144 | |
bcalin1984 | 0:5bf6fcf71548 | 145 | // Set the callbacks for Listening |
bcalin1984 | 0:5bf6fcf71548 | 146 | ListeningSock.setOnEvent(this,&TcpServer::onListeningTCPSocketEvent); |
bcalin1984 | 0:5bf6fcf71548 | 147 | |
bcalin1984 | 0:5bf6fcf71548 | 148 | // bind and listen on TCP |
bcalin1984 | 0:5bf6fcf71548 | 149 | err=ListeningSock.bind(Host(IpAddr(), port)); |
bcalin1984 | 0:5bf6fcf71548 | 150 | dprintf("Binding..\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 151 | if(err) |
bcalin1984 | 0:5bf6fcf71548 | 152 | { |
bcalin1984 | 0:5bf6fcf71548 | 153 | //Deal with that error... |
bcalin1984 | 0:5bf6fcf71548 | 154 | dprintf("Binding Error\n"); |
bcalin1984 | 0:5bf6fcf71548 | 155 | } |
bcalin1984 | 0:5bf6fcf71548 | 156 | err=ListeningSock.listen(); // Starts listening |
bcalin1984 | 0:5bf6fcf71548 | 157 | dprintf("Listening...\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 158 | if(err) |
bcalin1984 | 0:5bf6fcf71548 | 159 | { |
bcalin1984 | 0:5bf6fcf71548 | 160 | dprintf("Listening Error\r\n"); |
bcalin1984 | 0:5bf6fcf71548 | 161 | } |
bcalin1984 | 0:5bf6fcf71548 | 162 | return 0; |
bcalin1984 | 0:5bf6fcf71548 | 163 | } |
bcalin1984 | 0:5bf6fcf71548 | 164 | |
bcalin1984 | 0:5bf6fcf71548 | 165 | void TcpServer::doEvents() |
bcalin1984 | 0:5bf6fcf71548 | 166 | { |
bcalin1984 | 0:5bf6fcf71548 | 167 | Net::poll(); |
bcalin1984 | 0:5bf6fcf71548 | 168 | } |
bcalin1984 | 0:5bf6fcf71548 | 169 | |
bcalin1984 | 0:5bf6fcf71548 | 170 | void TcpServer::SetReceiveCallback(void (*f)(char*buf, int len)) |
bcalin1984 | 0:5bf6fcf71548 | 171 | { |
bcalin1984 | 0:5bf6fcf71548 | 172 | onReceive = f; |
bcalin1984 | 0:5bf6fcf71548 | 173 | } |
bcalin1984 | 0:5bf6fcf71548 | 174 | |
bcalin1984 | 0:5bf6fcf71548 | 175 | |
bcalin1984 | 0:5bf6fcf71548 | 176 | void TcpServer::Tprintf(const char * format, ...) |
bcalin1984 | 0:5bf6fcf71548 | 177 | { |
bcalin1984 | 0:5bf6fcf71548 | 178 | va_list ap; |
bcalin1984 | 0:5bf6fcf71548 | 179 | va_start(ap, format); |
bcalin1984 | 0:5bf6fcf71548 | 180 | vsprintf(buff, format, ap); |
bcalin1984 | 0:5bf6fcf71548 | 181 | va_end(ap); |
bcalin1984 | 0:5bf6fcf71548 | 182 | |
bcalin1984 | 0:5bf6fcf71548 | 183 | pConnectedSock->send(buff, strlen(buff)); |
bcalin1984 | 0:5bf6fcf71548 | 184 | } |
bcalin1984 | 0:5bf6fcf71548 | 185 | |
bcalin1984 | 0:5bf6fcf71548 | 186 | void TcpServer::noprintf(const char * format, ...) |
bcalin1984 | 0:5bf6fcf71548 | 187 | { |
bcalin1984 | 0:5bf6fcf71548 | 188 | |
bcalin1984 | 0:5bf6fcf71548 | 189 | } |
bcalin1984 | 0:5bf6fcf71548 | 190 |