Dependencies:   EthernetNetIf mbed NetServicesMin

Revision:
0:be9ccd3a915d
--- /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();
+    }
+  }  
+}