Dependencies: EthernetNetIf mbed NetServicesMin
Revision 0:be9ccd3a915d, committed 2011-09-21
- Comitter:
- alpsayin
- Date:
- Wed Sep 21 15:54:53 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r be9ccd3a915d EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r be9ccd3a915d NetServicesMin.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetServicesMin.lib Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/hlipka/code/NetServicesMin/#8b387bed54c2
diff -r 000000000000 -r be9ccd3a915d main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.c Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,216 @@ + +#include "mbed.h" +#include "EthernetNetIf.h" +#include "TCPSocket.h" +#include "message.h" + +#define DHCP_ENABLE 1 + +#define LOCAL_IP_ADDRESS IpAddr(192,168,1,110) +#define NETWORK_MASK IpAddr(255,255,255,0) +#define GATEWAY_ADDRESS IpAddr(192,168,1,1) +#define DNS_ADDRESS IpAddr(8,8,8,8) + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +Serial pc(USBTX, USBRX); + +#if DHCP_ENABLE==1 +#warning DHCP is enabled +EthernetNetIf eth; +#else +#warning Static IP is used +EthernetNetIf eth( + LOCAL_IP_ADDRESS, + NETWORK_MASK, + GATEWAY_ADDRESS, + DNS_ADDRESS +); +#endif + +#define TCP_LISTENING_PORT 12345 + +TCPSocket ListeningSock; +TCPSocket* pConnectedSock; // for ConnectedSock +Host client; +TCPSocketErr err; + +messageHandlerFunction(ledGetter) +{ + char newMsg[5]; + if(led1) + newMsg[0] = '1'; + else + newMsg[0] = '0'; + + if(led2) + newMsg[1] = '1'; + else + newMsg[1] = '0'; + + if(led3) + newMsg[2] = '1'; + else + newMsg[2] = '0'; + + newMsg[3] = '\r'; + newMsg[4] = '\n'; + + vSendMessage(pConnectedSocket, newMsg, 5); +} +messageHandlerFunction(ledLighter) +{ + led1 = msg[0]&0x01; + led2 = msg[1]&0x02; + led3 = msg[2]&0x03; + ledGetter(pConnectedSocket, msg, len); +} + +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 + vGetMessage(pConnectedSock); + 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"); + + return 0; + EthernetErr ethErr = eth.setup();//initialisation of ethernet stack + 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]);//address of server + + //Set the get message handler function + vSetMessageHandler(TYPE_POST, ledLighter); + vSetMessageHandler(TYPE_GET, ledGetter); + + // 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() > 4) // sec + { + led4=!led4; //Show that we are alive + tmr.reset(); + } + } +}
diff -r 000000000000 -r be9ccd3a915d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r be9ccd3a915d message.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/message.c Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,69 @@ + +#include "message.h" + +void (*getMessageHandler)(TCPSocket* pConnectedSocket, char* msg, int len) = defaultMessageHandler; +void (*postMessageHandler)(TCPSocket* pConnectedSocket,char* msg, int len) = defaultMessageHandler; + +void defaultMessageHandler(TCPSocket* pConnectedSocket, char* msg, int len) +{ + printf(msg); + vSendMessage(pConnectedSocket, "DEFAULT\r\n", 9); +} +void vSendMessage(TCPSocket* pConnectedSocket, char* msg, int len) +{ + pConnectedSocket->send("RESPONSE\r\n",10); + pConnectedSocket->send(msg,len); + pConnectedSocket->send("\r\n\r\n",4); +} +void vGetMessage(TCPSocket* pConnectedSocket) +{ + char line[BUFFER_LENGTH]; + int len; + bool endOfMessage = false; + while(!endOfMessage) + { + len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); + if(strncmp(line,"GET\r\n", 5)==0) + { + len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); + getMessageHandler(pConnectedSocket, line, len); + } + else if(strncmp(line,"POST\r\n",6)==0) + { + len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); + postMessageHandler(pConnectedSocket, line, len); + } + else if(strncmp(line, "\r\n", 2)==0) + { + endOfMessage = true; + } + } +} +int iGetLine(TCPSocket* pConnectedSocket, char* buf, int len) +{ + int counter = 0; + while(counter < len) + { + if(pConnectedSocket->recv(&(buf[counter]),1) > 0) + { + if(counter > 0) + { + if(buf[counter]=='\n' && buf[counter-1]=='\r') + { + return counter+1; + } + } + counter++; + } + } + return -1; +} +void vSetMessageHandler(int messageType, void (*messageHandler)(TCPSocket* pConnectedSocket,char* msg, int len)) +{ + switch(messageType) + { + case TYPE_GET: getMessageHandler = messageHandler; break; + case TYPE_POST: postMessageHandler = messageHandler; break; + default: break; + } +} \ No newline at end of file
diff -r 000000000000 -r be9ccd3a915d message.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/message.h Wed Sep 21 15:54:53 2011 +0000 @@ -0,0 +1,17 @@ + +#include "mbed.h" +#include "TCPSocket.h" + +#define TYPE_GET 1 +#define TYPE_POST 2 +#define TYPE_RESPONSE 3 + +#define BUFFER_LENGTH 256 + +#define messageHandlerFunction(X) void X(TCPSocket* pConnectedSocket, char* msg, int len) + +void defaultMessageHandler(TCPSocket* pConnectedSocket, char* msg, int len); +void vSendMessage(TCPSocket* pConnectedSocket, char* msg, int len); +void vGetMessage(TCPSocket* pConnected); +int iGetLine(TCPSocket* pConnectedSocket, char* buf, int len); +void vSetMessageHandler(int messageType, void (*messageHandler)(TCPSocket*, char*, int)); \ No newline at end of file