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