![](/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
Diff: main.cpp
- Revision:
- 0:beb124b81ef5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 02 02:58:40 2011 +0000 @@ -0,0 +1,202 @@ + +// TCP Echo server +// 2010/9/7 + + +/* + +(Execute Sample) +PC side: +telnet 192.168.0.25 12345 +Trying 192.168.0.25... +Connected to 192.168.0.25. +Escape character is '^]'. + +mbed side: +Setup OK +mbed IP Address is 192.168.0.25 +Binding.. +Listening... +Listening: TCP Socket Accepted +Listening: Incoming TCP connection from 192.168.0.7 +TCP Socket Readable +Received&Wrote:test text + +TCP Socket Writable + + +*/ + +#include "mbed.h" +#include "EthernetNetIf.h" +#include "TCPSocket.h" +#include "USBMouse.h" +#include "USBKeyboard.h" + + +DigitalOut hipwr(p21); +USBMouse mouse; +USBKeyboard keyboard; +DigitalOut led4(LED4, "led4"); + + EthernetNetIf eth; +/*EthernetNetIf eth( + IpAddr(192,168,0,25), //IP Address + IpAddr(255,255,255,0), //Network Mask + IpAddr(192,168,0,1), //Gateway + IpAddr(192,168,0,1) //DNS +); +*/ +#define TCP_LISTENING_PORT 12345 + +TCPSocket ListeningSock; +TCPSocket* pConnectedSock; // for ConnectedSock +Host client; +TCPSocketErr err; + +void onConnectedTCPSocketEvent(TCPSocketEvent e) +{ + switch(e) + { + case TCPSOCKET_CONNECTED: + printf("TCP Socket Connected\r\n"); + break; + case TCPSOCKET_WRITEABLE: + //Can now write some data... + printf("TCP Socket Writable\r\n"); + break; + case TCPSOCKET_READABLE: + //Can now read dome data... + printf("TCP Socket Readable\r\n"); + // Read in any available data into the buffer + char buff[128]; + while ( int len = pConnectedSock->recv(buff, 128) ) { + // And send straight back out again + pConnectedSock->send(buff, len); + buff[len]=0; // make terminater + printf("Received&Wrote:%s\r\n",buff); + keyboard.sendKeys(buff); + } + break; + case TCPSOCKET_CONTIMEOUT: + printf("TCP Socket Timeout\r\n"); + break; + case TCPSOCKET_CONRST: + printf("TCP Socket CONRST\r\n"); + break; + case TCPSOCKET_CONABRT: + printf("TCP Socket CONABRT\r\n"); + break; + case TCPSOCKET_ERROR: + printf("TCP Socket Error\r\n"); + break; + case TCPSOCKET_DISCONNECTED: + //Close socket... + printf("TCP Socket Disconnected\r\n"); + pConnectedSock->close(); + break; + default: + printf("DEFAULT\r\n"); + } +} + + +void onListeningTCPSocketEvent(TCPSocketEvent e) +{ + switch(e) + { + case TCPSOCKET_ACCEPT: + printf("Listening: TCP Socket Accepted\r\n"); + // Accepts connection from client and gets connected socket. + err=ListeningSock.accept(&client, &pConnectedSock); + if (err) { + printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); + return; //Error in accept, discard connection + } + // Setup the new socket events + pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); + // We can find out from where the connection is coming by looking at the + // Host parameter of the accept() method + IpAddr clientIp = client.getIp(); + printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", + clientIp[0], clientIp[1], clientIp[2], clientIp[3]); + break; + // the following cases will not happen + case TCPSOCKET_CONNECTED: + printf("Listening: TCP Socket Connected\r\n"); + break; + case TCPSOCKET_WRITEABLE: + printf("Listening: TCP Socket Writable\r\n"); + break; + case TCPSOCKET_READABLE: + printf("Listening: TCP Socket Readable\r\n"); + break; + case TCPSOCKET_CONTIMEOUT: + printf("Listening: TCP Socket Timeout\r\n"); + break; + case TCPSOCKET_CONRST: + printf("Listening: TCP Socket CONRST\r\n"); + break; + case TCPSOCKET_CONABRT: + printf("Listening: TCP Socket CONABRT\r\n"); + break; + case TCPSOCKET_ERROR: + printf("Listening: TCP Socket Error\r\n"); + break; + case TCPSOCKET_DISCONNECTED: + //Close socket... + printf("Listening: TCP Socket Disconnected\r\n"); + ListeningSock.close(); + break; + default: + printf("DEFAULT\r\n"); + }; +} + + +int main() { + printf("\r\n"); + printf("Setting up...\r\n"); + EthernetErr ethErr = eth.setup(); + if(ethErr) + { + printf("Error %d in setup.\r\n", ethErr); + return -1; + } + printf("Setup OK\r\n"); + + IpAddr ip = eth.getIp(); + printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); + + + // Set the callbacks for Listening + ListeningSock.setOnEvent(&onListeningTCPSocketEvent); + + // bind and listen on TCP + err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); + printf("Binding..\r\n"); + if(err) + { + //Deal with that error... + printf("Binding Error\n"); + } + err=ListeningSock.listen(); // Starts listening + printf("Listening...\r\n"); + if(err) + { + printf("Listening Error\r\n"); + } + + Timer tmr; + tmr.start(); + + while(true) + { + Net::poll(); + if(tmr.read() > 0.2) // sec + { + led4=!led4; //Show that we are alive + tmr.reset(); + } + } +}