Dependencies:   EthernetNetIf mbed NetServicesMin

Committer:
alpsayin
Date:
Wed Sep 21 15:54:53 2011 +0000
Revision:
0:be9ccd3a915d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alpsayin 0:be9ccd3a915d 1
alpsayin 0:be9ccd3a915d 2 #include "mbed.h"
alpsayin 0:be9ccd3a915d 3 #include "EthernetNetIf.h"
alpsayin 0:be9ccd3a915d 4 #include "TCPSocket.h"
alpsayin 0:be9ccd3a915d 5 #include "message.h"
alpsayin 0:be9ccd3a915d 6
alpsayin 0:be9ccd3a915d 7 #define DHCP_ENABLE 1
alpsayin 0:be9ccd3a915d 8
alpsayin 0:be9ccd3a915d 9 #define LOCAL_IP_ADDRESS IpAddr(192,168,1,110)
alpsayin 0:be9ccd3a915d 10 #define NETWORK_MASK IpAddr(255,255,255,0)
alpsayin 0:be9ccd3a915d 11 #define GATEWAY_ADDRESS IpAddr(192,168,1,1)
alpsayin 0:be9ccd3a915d 12 #define DNS_ADDRESS IpAddr(8,8,8,8)
alpsayin 0:be9ccd3a915d 13
alpsayin 0:be9ccd3a915d 14 DigitalOut led1(LED1);
alpsayin 0:be9ccd3a915d 15 DigitalOut led2(LED2);
alpsayin 0:be9ccd3a915d 16 DigitalOut led3(LED3);
alpsayin 0:be9ccd3a915d 17 DigitalOut led4(LED4);
alpsayin 0:be9ccd3a915d 18
alpsayin 0:be9ccd3a915d 19 Serial pc(USBTX, USBRX);
alpsayin 0:be9ccd3a915d 20
alpsayin 0:be9ccd3a915d 21 #if DHCP_ENABLE==1
alpsayin 0:be9ccd3a915d 22 #warning DHCP is enabled
alpsayin 0:be9ccd3a915d 23 EthernetNetIf eth;
alpsayin 0:be9ccd3a915d 24 #else
alpsayin 0:be9ccd3a915d 25 #warning Static IP is used
alpsayin 0:be9ccd3a915d 26 EthernetNetIf eth(
alpsayin 0:be9ccd3a915d 27 LOCAL_IP_ADDRESS,
alpsayin 0:be9ccd3a915d 28 NETWORK_MASK,
alpsayin 0:be9ccd3a915d 29 GATEWAY_ADDRESS,
alpsayin 0:be9ccd3a915d 30 DNS_ADDRESS
alpsayin 0:be9ccd3a915d 31 );
alpsayin 0:be9ccd3a915d 32 #endif
alpsayin 0:be9ccd3a915d 33
alpsayin 0:be9ccd3a915d 34 #define TCP_LISTENING_PORT 12345
alpsayin 0:be9ccd3a915d 35
alpsayin 0:be9ccd3a915d 36 TCPSocket ListeningSock;
alpsayin 0:be9ccd3a915d 37 TCPSocket* pConnectedSock; // for ConnectedSock
alpsayin 0:be9ccd3a915d 38 Host client;
alpsayin 0:be9ccd3a915d 39 TCPSocketErr err;
alpsayin 0:be9ccd3a915d 40
alpsayin 0:be9ccd3a915d 41 messageHandlerFunction(ledGetter)
alpsayin 0:be9ccd3a915d 42 {
alpsayin 0:be9ccd3a915d 43 char newMsg[5];
alpsayin 0:be9ccd3a915d 44 if(led1)
alpsayin 0:be9ccd3a915d 45 newMsg[0] = '1';
alpsayin 0:be9ccd3a915d 46 else
alpsayin 0:be9ccd3a915d 47 newMsg[0] = '0';
alpsayin 0:be9ccd3a915d 48
alpsayin 0:be9ccd3a915d 49 if(led2)
alpsayin 0:be9ccd3a915d 50 newMsg[1] = '1';
alpsayin 0:be9ccd3a915d 51 else
alpsayin 0:be9ccd3a915d 52 newMsg[1] = '0';
alpsayin 0:be9ccd3a915d 53
alpsayin 0:be9ccd3a915d 54 if(led3)
alpsayin 0:be9ccd3a915d 55 newMsg[2] = '1';
alpsayin 0:be9ccd3a915d 56 else
alpsayin 0:be9ccd3a915d 57 newMsg[2] = '0';
alpsayin 0:be9ccd3a915d 58
alpsayin 0:be9ccd3a915d 59 newMsg[3] = '\r';
alpsayin 0:be9ccd3a915d 60 newMsg[4] = '\n';
alpsayin 0:be9ccd3a915d 61
alpsayin 0:be9ccd3a915d 62 vSendMessage(pConnectedSocket, newMsg, 5);
alpsayin 0:be9ccd3a915d 63 }
alpsayin 0:be9ccd3a915d 64 messageHandlerFunction(ledLighter)
alpsayin 0:be9ccd3a915d 65 {
alpsayin 0:be9ccd3a915d 66 led1 = msg[0]&0x01;
alpsayin 0:be9ccd3a915d 67 led2 = msg[1]&0x02;
alpsayin 0:be9ccd3a915d 68 led3 = msg[2]&0x03;
alpsayin 0:be9ccd3a915d 69 ledGetter(pConnectedSocket, msg, len);
alpsayin 0:be9ccd3a915d 70 }
alpsayin 0:be9ccd3a915d 71
alpsayin 0:be9ccd3a915d 72 void onConnectedTCPSocketEvent(TCPSocketEvent e)
alpsayin 0:be9ccd3a915d 73 {
alpsayin 0:be9ccd3a915d 74 switch(e)
alpsayin 0:be9ccd3a915d 75 {
alpsayin 0:be9ccd3a915d 76 case TCPSOCKET_CONNECTED:
alpsayin 0:be9ccd3a915d 77 printf("TCP Socket Connected\r\n");
alpsayin 0:be9ccd3a915d 78 break;
alpsayin 0:be9ccd3a915d 79 case TCPSOCKET_WRITEABLE:
alpsayin 0:be9ccd3a915d 80 //Can now write some data...
alpsayin 0:be9ccd3a915d 81 //printf("TCP Socket Writable\r\n");
alpsayin 0:be9ccd3a915d 82 break;
alpsayin 0:be9ccd3a915d 83 case TCPSOCKET_READABLE:
alpsayin 0:be9ccd3a915d 84 //Can now read dome data...
alpsayin 0:be9ccd3a915d 85 //printf("TCP Socket Readable\r\n");
alpsayin 0:be9ccd3a915d 86 // Read in any available data into the buffer
alpsayin 0:be9ccd3a915d 87 vGetMessage(pConnectedSock);
alpsayin 0:be9ccd3a915d 88 break;
alpsayin 0:be9ccd3a915d 89 case TCPSOCKET_CONTIMEOUT:
alpsayin 0:be9ccd3a915d 90 printf("TCP Socket Timeout\r\n");
alpsayin 0:be9ccd3a915d 91 break;
alpsayin 0:be9ccd3a915d 92 case TCPSOCKET_CONRST:
alpsayin 0:be9ccd3a915d 93 printf("TCP Socket CONRST\r\n");
alpsayin 0:be9ccd3a915d 94 break;
alpsayin 0:be9ccd3a915d 95 case TCPSOCKET_CONABRT:
alpsayin 0:be9ccd3a915d 96 printf("TCP Socket CONABRT\r\n");
alpsayin 0:be9ccd3a915d 97 break;
alpsayin 0:be9ccd3a915d 98 case TCPSOCKET_ERROR:
alpsayin 0:be9ccd3a915d 99 printf("TCP Socket Error\r\n");
alpsayin 0:be9ccd3a915d 100 break;
alpsayin 0:be9ccd3a915d 101 case TCPSOCKET_DISCONNECTED:
alpsayin 0:be9ccd3a915d 102 //Close socket...
alpsayin 0:be9ccd3a915d 103 printf("TCP Socket Disconnected\r\n");
alpsayin 0:be9ccd3a915d 104 pConnectedSock->close();
alpsayin 0:be9ccd3a915d 105 break;
alpsayin 0:be9ccd3a915d 106 default:
alpsayin 0:be9ccd3a915d 107 printf("DEFAULT\r\n");
alpsayin 0:be9ccd3a915d 108 }
alpsayin 0:be9ccd3a915d 109 }
alpsayin 0:be9ccd3a915d 110
alpsayin 0:be9ccd3a915d 111
alpsayin 0:be9ccd3a915d 112 void onListeningTCPSocketEvent(TCPSocketEvent e)
alpsayin 0:be9ccd3a915d 113 {
alpsayin 0:be9ccd3a915d 114 switch(e)
alpsayin 0:be9ccd3a915d 115 {
alpsayin 0:be9ccd3a915d 116 case TCPSOCKET_ACCEPT:
alpsayin 0:be9ccd3a915d 117 printf("Listening: TCP Socket Accepted\r\n");
alpsayin 0:be9ccd3a915d 118 // Accepts connection from client and gets connected socket.
alpsayin 0:be9ccd3a915d 119 err=ListeningSock.accept(&client, &pConnectedSock);
alpsayin 0:be9ccd3a915d 120 if (err) {
alpsayin 0:be9ccd3a915d 121 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n");
alpsayin 0:be9ccd3a915d 122 return; //Error in accept, discard connection
alpsayin 0:be9ccd3a915d 123 }
alpsayin 0:be9ccd3a915d 124 // Setup the new socket events
alpsayin 0:be9ccd3a915d 125 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent);
alpsayin 0:be9ccd3a915d 126 // We can find out from where the connection is coming by looking at the
alpsayin 0:be9ccd3a915d 127 // Host parameter of the accept() method
alpsayin 0:be9ccd3a915d 128 IpAddr clientIp = client.getIp();
alpsayin 0:be9ccd3a915d 129 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n",
alpsayin 0:be9ccd3a915d 130 clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
alpsayin 0:be9ccd3a915d 131 break;
alpsayin 0:be9ccd3a915d 132 // the following cases will not happen
alpsayin 0:be9ccd3a915d 133 case TCPSOCKET_CONNECTED:
alpsayin 0:be9ccd3a915d 134 printf("Listening: TCP Socket Connected\r\n");
alpsayin 0:be9ccd3a915d 135 break;
alpsayin 0:be9ccd3a915d 136 case TCPSOCKET_WRITEABLE:
alpsayin 0:be9ccd3a915d 137 printf("Listening: TCP Socket Writable\r\n");
alpsayin 0:be9ccd3a915d 138 break;
alpsayin 0:be9ccd3a915d 139 case TCPSOCKET_READABLE:
alpsayin 0:be9ccd3a915d 140 printf("Listening: TCP Socket Readable\r\n");
alpsayin 0:be9ccd3a915d 141 break;
alpsayin 0:be9ccd3a915d 142 case TCPSOCKET_CONTIMEOUT:
alpsayin 0:be9ccd3a915d 143 printf("Listening: TCP Socket Timeout\r\n");
alpsayin 0:be9ccd3a915d 144 break;
alpsayin 0:be9ccd3a915d 145 case TCPSOCKET_CONRST:
alpsayin 0:be9ccd3a915d 146 printf("Listening: TCP Socket CONRST\r\n");
alpsayin 0:be9ccd3a915d 147 break;
alpsayin 0:be9ccd3a915d 148 case TCPSOCKET_CONABRT:
alpsayin 0:be9ccd3a915d 149 printf("Listening: TCP Socket CONABRT\r\n");
alpsayin 0:be9ccd3a915d 150 break;
alpsayin 0:be9ccd3a915d 151 case TCPSOCKET_ERROR:
alpsayin 0:be9ccd3a915d 152 printf("Listening: TCP Socket Error\r\n");
alpsayin 0:be9ccd3a915d 153 break;
alpsayin 0:be9ccd3a915d 154 case TCPSOCKET_DISCONNECTED:
alpsayin 0:be9ccd3a915d 155 //Close socket...
alpsayin 0:be9ccd3a915d 156 printf("Listening: TCP Socket Disconnected\r\n");
alpsayin 0:be9ccd3a915d 157 ListeningSock.close();
alpsayin 0:be9ccd3a915d 158 break;
alpsayin 0:be9ccd3a915d 159 default:
alpsayin 0:be9ccd3a915d 160 printf("DEFAULT\r\n");
alpsayin 0:be9ccd3a915d 161 };
alpsayin 0:be9ccd3a915d 162 }
alpsayin 0:be9ccd3a915d 163
alpsayin 0:be9ccd3a915d 164
alpsayin 0:be9ccd3a915d 165 int main() {
alpsayin 0:be9ccd3a915d 166 printf("\r\n");
alpsayin 0:be9ccd3a915d 167 printf("Setting up...\r\n");
alpsayin 0:be9ccd3a915d 168
alpsayin 0:be9ccd3a915d 169 return 0;
alpsayin 0:be9ccd3a915d 170 EthernetErr ethErr = eth.setup();//initialisation of ethernet stack
alpsayin 0:be9ccd3a915d 171 if(ethErr)
alpsayin 0:be9ccd3a915d 172 {
alpsayin 0:be9ccd3a915d 173 printf("Error %d in setup.\r\n", ethErr);
alpsayin 0:be9ccd3a915d 174 return -1;
alpsayin 0:be9ccd3a915d 175 }
alpsayin 0:be9ccd3a915d 176 printf("Setup OK\r\n");
alpsayin 0:be9ccd3a915d 177
alpsayin 0:be9ccd3a915d 178 IpAddr ip = eth.getIp();
alpsayin 0:be9ccd3a915d 179 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);//address of server
alpsayin 0:be9ccd3a915d 180
alpsayin 0:be9ccd3a915d 181 //Set the get message handler function
alpsayin 0:be9ccd3a915d 182 vSetMessageHandler(TYPE_POST, ledLighter);
alpsayin 0:be9ccd3a915d 183 vSetMessageHandler(TYPE_GET, ledGetter);
alpsayin 0:be9ccd3a915d 184
alpsayin 0:be9ccd3a915d 185 // Set the callbacks for Listening
alpsayin 0:be9ccd3a915d 186 ListeningSock.setOnEvent(&onListeningTCPSocketEvent);
alpsayin 0:be9ccd3a915d 187
alpsayin 0:be9ccd3a915d 188 // bind and listen on TCP
alpsayin 0:be9ccd3a915d 189 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT));
alpsayin 0:be9ccd3a915d 190 printf("Binding..\r\n");
alpsayin 0:be9ccd3a915d 191
alpsayin 0:be9ccd3a915d 192 if(err)
alpsayin 0:be9ccd3a915d 193 {
alpsayin 0:be9ccd3a915d 194 //Deal with that error...
alpsayin 0:be9ccd3a915d 195 printf("Binding Error\n");
alpsayin 0:be9ccd3a915d 196 }
alpsayin 0:be9ccd3a915d 197 err=ListeningSock.listen(); // Starts listening
alpsayin 0:be9ccd3a915d 198 printf("Listening...\r\n");
alpsayin 0:be9ccd3a915d 199 if(err)
alpsayin 0:be9ccd3a915d 200 {
alpsayin 0:be9ccd3a915d 201 printf("Listening Error\r\n");
alpsayin 0:be9ccd3a915d 202 }
alpsayin 0:be9ccd3a915d 203
alpsayin 0:be9ccd3a915d 204 Timer tmr;
alpsayin 0:be9ccd3a915d 205 tmr.start();
alpsayin 0:be9ccd3a915d 206
alpsayin 0:be9ccd3a915d 207 while(true)
alpsayin 0:be9ccd3a915d 208 {
alpsayin 0:be9ccd3a915d 209 Net::poll();
alpsayin 0:be9ccd3a915d 210 if(tmr.read() > 4) // sec
alpsayin 0:be9ccd3a915d 211 {
alpsayin 0:be9ccd3a915d 212 led4=!led4; //Show that we are alive
alpsayin 0:be9ccd3a915d 213 tmr.reset();
alpsayin 0:be9ccd3a915d 214 }
alpsayin 0:be9ccd3a915d 215 }
alpsayin 0:be9ccd3a915d 216 }