![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Based on Shigeki KOMATSU program
Dependencies: mbed EthernetNetIf
main.cpp@3:3a5e5819010c, 2015-04-30 (annotated)
- Committer:
- nenad
- Date:
- Thu Apr 30 13:25:24 2015 +0000
- Revision:
- 3:3a5e5819010c
- Parent:
- 2:2d3631e15544
azurirani portovi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nenad | 0:92055da1fc21 | 1 | |
nenad | 0:92055da1fc21 | 2 | #include "mbed.h" |
nenad | 0:92055da1fc21 | 3 | #include "EthernetNetIf.h" |
nenad | 0:92055da1fc21 | 4 | #include "TCPSocket.h" |
nenad | 0:92055da1fc21 | 5 | |
nenad | 0:92055da1fc21 | 6 | DigitalOut led4(LED4, "led4"); |
nenad | 0:92055da1fc21 | 7 | |
nenad | 0:92055da1fc21 | 8 | //EthernetNetIf eth; |
nenad | 0:92055da1fc21 | 9 | EthernetNetIf eth( |
nenad | 0:92055da1fc21 | 10 | IpAddr(192,168,1,110), //IP Address |
nenad | 0:92055da1fc21 | 11 | IpAddr(255,255,255,0), //Network Mask |
nenad | 0:92055da1fc21 | 12 | IpAddr(192,168,1,1), //Gateway |
nenad | 0:92055da1fc21 | 13 | IpAddr(192,168,1,1) //DNS |
nenad | 0:92055da1fc21 | 14 | ); |
nenad | 0:92055da1fc21 | 15 | |
nenad | 0:92055da1fc21 | 16 | #define TCP_LISTENING_PORT 12345 |
nenad | 0:92055da1fc21 | 17 | |
nenad | 0:92055da1fc21 | 18 | TCPSocket ListeningSock; |
nenad | 0:92055da1fc21 | 19 | TCPSocket* pConnectedSock; // for ConnectedSock |
nenad | 0:92055da1fc21 | 20 | Host client; |
nenad | 0:92055da1fc21 | 21 | TCPSocketErr err; |
nenad | 0:92055da1fc21 | 22 | |
nenad | 0:92055da1fc21 | 23 | void onConnectedTCPSocketEvent(TCPSocketEvent e) |
nenad | 0:92055da1fc21 | 24 | { |
nenad | 0:92055da1fc21 | 25 | switch(e) |
nenad | 0:92055da1fc21 | 26 | { |
nenad | 0:92055da1fc21 | 27 | case TCPSOCKET_CONNECTED: |
nenad | 0:92055da1fc21 | 28 | printf("TCP Socket Connected\r\n"); |
nenad | 0:92055da1fc21 | 29 | break; |
nenad | 0:92055da1fc21 | 30 | case TCPSOCKET_WRITEABLE: |
nenad | 0:92055da1fc21 | 31 | //Can now write some data... |
nenad | 0:92055da1fc21 | 32 | //printf("TCP Socket Writable\r\n"); |
nenad | 0:92055da1fc21 | 33 | break; |
nenad | 0:92055da1fc21 | 34 | case TCPSOCKET_READABLE: |
nenad | 0:92055da1fc21 | 35 | //Can now read dome data... |
nenad | 0:92055da1fc21 | 36 | //printf("TCP Socket Readable\r\n"); |
nenad | 0:92055da1fc21 | 37 | // Read in any available data into the buffer |
nenad | 0:92055da1fc21 | 38 | char buff[128]; |
nenad | 0:92055da1fc21 | 39 | while ( int len = pConnectedSock->recv(buff, 128) ) { |
nenad | 0:92055da1fc21 | 40 | // And send straight back out again |
nenad | 0:92055da1fc21 | 41 | pConnectedSock->send(buff, len); |
nenad | 0:92055da1fc21 | 42 | buff[len]=0; // make terminater |
nenad | 0:92055da1fc21 | 43 | printf("Received from and sent back to client:%s\r\n",buff); |
nenad | 0:92055da1fc21 | 44 | } |
nenad | 0:92055da1fc21 | 45 | break; |
nenad | 0:92055da1fc21 | 46 | case TCPSOCKET_CONTIMEOUT: |
nenad | 0:92055da1fc21 | 47 | printf("TCP Socket Timeout\r\n"); |
nenad | 0:92055da1fc21 | 48 | break; |
nenad | 0:92055da1fc21 | 49 | case TCPSOCKET_CONRST: |
nenad | 0:92055da1fc21 | 50 | printf("TCP Socket CONRST\r\n"); |
nenad | 0:92055da1fc21 | 51 | break; |
nenad | 0:92055da1fc21 | 52 | case TCPSOCKET_CONABRT: |
nenad | 0:92055da1fc21 | 53 | printf("TCP Socket CONABRT\r\n"); |
nenad | 0:92055da1fc21 | 54 | break; |
nenad | 0:92055da1fc21 | 55 | case TCPSOCKET_ERROR: |
nenad | 0:92055da1fc21 | 56 | printf("TCP Socket Error\r\n"); |
nenad | 0:92055da1fc21 | 57 | break; |
nenad | 0:92055da1fc21 | 58 | case TCPSOCKET_DISCONNECTED: |
nenad | 0:92055da1fc21 | 59 | //Close socket... |
nenad | 0:92055da1fc21 | 60 | printf("TCP Socket Disconnected\r\n"); |
nenad | 0:92055da1fc21 | 61 | pConnectedSock->close(); |
nenad | 0:92055da1fc21 | 62 | break; |
nenad | 0:92055da1fc21 | 63 | default: |
nenad | 0:92055da1fc21 | 64 | printf("DEFAULT\r\n"); |
nenad | 0:92055da1fc21 | 65 | } |
nenad | 0:92055da1fc21 | 66 | } |
nenad | 0:92055da1fc21 | 67 | |
nenad | 0:92055da1fc21 | 68 | |
nenad | 0:92055da1fc21 | 69 | void onListeningTCPSocketEvent(TCPSocketEvent e) |
nenad | 0:92055da1fc21 | 70 | { |
nenad | 0:92055da1fc21 | 71 | switch(e) |
nenad | 0:92055da1fc21 | 72 | { |
nenad | 0:92055da1fc21 | 73 | case TCPSOCKET_ACCEPT: |
nenad | 0:92055da1fc21 | 74 | printf("Listening: TCP Socket Accepted\r\n"); |
nenad | 0:92055da1fc21 | 75 | // Accepts connection from client and gets connected socket. |
nenad | 0:92055da1fc21 | 76 | err=ListeningSock.accept(&client, &pConnectedSock); |
nenad | 0:92055da1fc21 | 77 | if (err) { |
nenad | 0:92055da1fc21 | 78 | printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); |
nenad | 0:92055da1fc21 | 79 | return; //Error in accept, discard connection |
nenad | 0:92055da1fc21 | 80 | } |
nenad | 0:92055da1fc21 | 81 | // Setup the new socket events |
nenad | 0:92055da1fc21 | 82 | pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); |
nenad | 0:92055da1fc21 | 83 | // We can find out from where the connection is coming by looking at the |
nenad | 0:92055da1fc21 | 84 | // Host parameter of the accept() method |
nenad | 0:92055da1fc21 | 85 | IpAddr clientIp = client.getIp(); |
nenad | 0:92055da1fc21 | 86 | printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", |
nenad | 0:92055da1fc21 | 87 | clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
nenad | 0:92055da1fc21 | 88 | break; |
nenad | 0:92055da1fc21 | 89 | // the following cases will not happen |
nenad | 0:92055da1fc21 | 90 | case TCPSOCKET_CONNECTED: |
nenad | 0:92055da1fc21 | 91 | printf("Listening: TCP Socket Connected\r\n"); |
nenad | 0:92055da1fc21 | 92 | break; |
nenad | 0:92055da1fc21 | 93 | case TCPSOCKET_WRITEABLE: |
nenad | 0:92055da1fc21 | 94 | printf("Listening: TCP Socket Writable\r\n"); |
nenad | 0:92055da1fc21 | 95 | break; |
nenad | 0:92055da1fc21 | 96 | case TCPSOCKET_READABLE: |
nenad | 0:92055da1fc21 | 97 | printf("Listening: TCP Socket Readable\r\n"); |
nenad | 0:92055da1fc21 | 98 | break; |
nenad | 0:92055da1fc21 | 99 | case TCPSOCKET_CONTIMEOUT: |
nenad | 0:92055da1fc21 | 100 | printf("Listening: TCP Socket Timeout\r\n"); |
nenad | 0:92055da1fc21 | 101 | break; |
nenad | 0:92055da1fc21 | 102 | case TCPSOCKET_CONRST: |
nenad | 0:92055da1fc21 | 103 | printf("Listening: TCP Socket CONRST\r\n"); |
nenad | 0:92055da1fc21 | 104 | break; |
nenad | 0:92055da1fc21 | 105 | case TCPSOCKET_CONABRT: |
nenad | 0:92055da1fc21 | 106 | printf("Listening: TCP Socket CONABRT\r\n"); |
nenad | 0:92055da1fc21 | 107 | break; |
nenad | 0:92055da1fc21 | 108 | case TCPSOCKET_ERROR: |
nenad | 0:92055da1fc21 | 109 | printf("Listening: TCP Socket Error\r\n"); |
nenad | 0:92055da1fc21 | 110 | break; |
nenad | 0:92055da1fc21 | 111 | case TCPSOCKET_DISCONNECTED: |
nenad | 0:92055da1fc21 | 112 | //Close socket... |
nenad | 0:92055da1fc21 | 113 | printf("Listening: TCP Socket Disconnected\r\n"); |
nenad | 0:92055da1fc21 | 114 | ListeningSock.close(); |
nenad | 0:92055da1fc21 | 115 | break; |
nenad | 0:92055da1fc21 | 116 | default: |
nenad | 0:92055da1fc21 | 117 | printf("DEFAULT\r\n"); |
nenad | 0:92055da1fc21 | 118 | }; |
nenad | 0:92055da1fc21 | 119 | } |
nenad | 0:92055da1fc21 | 120 | |
nenad | 0:92055da1fc21 | 121 | |
nenad | 0:92055da1fc21 | 122 | int main() { |
nenad | 0:92055da1fc21 | 123 | printf("\r\n"); |
nenad | 0:92055da1fc21 | 124 | printf("Setting up...\r\n"); |
nenad | 0:92055da1fc21 | 125 | EthernetErr ethErr = eth.setup();//initialisation of ethernet stack |
nenad | 0:92055da1fc21 | 126 | if(ethErr) |
nenad | 0:92055da1fc21 | 127 | { |
nenad | 0:92055da1fc21 | 128 | printf("Error %d in setup.\r\n", ethErr); |
nenad | 0:92055da1fc21 | 129 | return -1; |
nenad | 0:92055da1fc21 | 130 | } |
nenad | 0:92055da1fc21 | 131 | printf("Setup OK\r\n"); |
nenad | 0:92055da1fc21 | 132 | |
nenad | 0:92055da1fc21 | 133 | IpAddr ip = eth.getIp(); |
nenad | 0:92055da1fc21 | 134 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);//address of server |
nenad | 0:92055da1fc21 | 135 | |
nenad | 0:92055da1fc21 | 136 | |
nenad | 0:92055da1fc21 | 137 | // Set the callbacks for Listening |
nenad | 0:92055da1fc21 | 138 | ListeningSock.setOnEvent(&onListeningTCPSocketEvent); |
nenad | 0:92055da1fc21 | 139 | |
nenad | 0:92055da1fc21 | 140 | // bind and listen on TCP |
nenad | 0:92055da1fc21 | 141 | err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); |
nenad | 0:92055da1fc21 | 142 | printf("Binding..\r\n"); |
nenad | 0:92055da1fc21 | 143 | |
nenad | 0:92055da1fc21 | 144 | if(err) |
nenad | 0:92055da1fc21 | 145 | { |
nenad | 0:92055da1fc21 | 146 | //Deal with that error... |
nenad | 0:92055da1fc21 | 147 | printf("Binding Error\n"); |
nenad | 0:92055da1fc21 | 148 | } |
nenad | 0:92055da1fc21 | 149 | err=ListeningSock.listen(); // Starts listening |
nenad | 0:92055da1fc21 | 150 | printf("Listening...\r\n"); |
nenad | 0:92055da1fc21 | 151 | if(err) |
nenad | 0:92055da1fc21 | 152 | { |
nenad | 0:92055da1fc21 | 153 | printf("Listening Error\r\n"); |
nenad | 0:92055da1fc21 | 154 | } |
nenad | 0:92055da1fc21 | 155 | |
nenad | 0:92055da1fc21 | 156 | Timer tmr; |
nenad | 0:92055da1fc21 | 157 | tmr.start(); |
nenad | 0:92055da1fc21 | 158 | |
nenad | 0:92055da1fc21 | 159 | while(true) |
nenad | 0:92055da1fc21 | 160 | { |
nenad | 0:92055da1fc21 | 161 | Net::poll(); |
nenad | 0:92055da1fc21 | 162 | if(tmr.read() > 4) // sec |
nenad | 0:92055da1fc21 | 163 | { |
nenad | 0:92055da1fc21 | 164 | led4=!led4; //Show that we are alive |
nenad | 0:92055da1fc21 | 165 | tmr.reset(); |
nenad | 0:92055da1fc21 | 166 | } |
nenad | 0:92055da1fc21 | 167 | } |
nenad | 0:92055da1fc21 | 168 | } |