Dependencies: EthernetNetIf mbed NetServicesMin
main.c
00001 00002 #include "mbed.h" 00003 #include "EthernetNetIf.h" 00004 #include "TCPSocket.h" 00005 #include "message.h" 00006 00007 #define DHCP_ENABLE 1 00008 00009 #define LOCAL_IP_ADDRESS IpAddr(192,168,1,110) 00010 #define NETWORK_MASK IpAddr(255,255,255,0) 00011 #define GATEWAY_ADDRESS IpAddr(192,168,1,1) 00012 #define DNS_ADDRESS IpAddr(8,8,8,8) 00013 00014 DigitalOut led1(LED1); 00015 DigitalOut led2(LED2); 00016 DigitalOut led3(LED3); 00017 DigitalOut led4(LED4); 00018 00019 Serial pc(USBTX, USBRX); 00020 00021 #if DHCP_ENABLE==1 00022 #warning DHCP is enabled 00023 EthernetNetIf eth; 00024 #else 00025 #warning Static IP is used 00026 EthernetNetIf eth( 00027 LOCAL_IP_ADDRESS, 00028 NETWORK_MASK, 00029 GATEWAY_ADDRESS, 00030 DNS_ADDRESS 00031 ); 00032 #endif 00033 00034 #define TCP_LISTENING_PORT 12345 00035 00036 TCPSocket ListeningSock; 00037 TCPSocket* pConnectedSock; // for ConnectedSock 00038 Host client; 00039 TCPSocketErr err; 00040 00041 messageHandlerFunction(ledGetter) 00042 { 00043 char newMsg[5]; 00044 if(led1) 00045 newMsg[0] = '1'; 00046 else 00047 newMsg[0] = '0'; 00048 00049 if(led2) 00050 newMsg[1] = '1'; 00051 else 00052 newMsg[1] = '0'; 00053 00054 if(led3) 00055 newMsg[2] = '1'; 00056 else 00057 newMsg[2] = '0'; 00058 00059 newMsg[3] = '\r'; 00060 newMsg[4] = '\n'; 00061 00062 vSendMessage(pConnectedSocket, newMsg, 5); 00063 } 00064 messageHandlerFunction(ledLighter) 00065 { 00066 led1 = msg[0]&0x01; 00067 led2 = msg[1]&0x02; 00068 led3 = msg[2]&0x03; 00069 ledGetter(pConnectedSocket, msg, len); 00070 } 00071 00072 void onConnectedTCPSocketEvent(TCPSocketEvent e) 00073 { 00074 switch(e) 00075 { 00076 case TCPSOCKET_CONNECTED: 00077 printf("TCP Socket Connected\r\n"); 00078 break; 00079 case TCPSOCKET_WRITEABLE: 00080 //Can now write some data... 00081 //printf("TCP Socket Writable\r\n"); 00082 break; 00083 case TCPSOCKET_READABLE: 00084 //Can now read dome data... 00085 //printf("TCP Socket Readable\r\n"); 00086 // Read in any available data into the buffer 00087 vGetMessage(pConnectedSock); 00088 break; 00089 case TCPSOCKET_CONTIMEOUT: 00090 printf("TCP Socket Timeout\r\n"); 00091 break; 00092 case TCPSOCKET_CONRST: 00093 printf("TCP Socket CONRST\r\n"); 00094 break; 00095 case TCPSOCKET_CONABRT: 00096 printf("TCP Socket CONABRT\r\n"); 00097 break; 00098 case TCPSOCKET_ERROR: 00099 printf("TCP Socket Error\r\n"); 00100 break; 00101 case TCPSOCKET_DISCONNECTED: 00102 //Close socket... 00103 printf("TCP Socket Disconnected\r\n"); 00104 pConnectedSock->close(); 00105 break; 00106 default: 00107 printf("DEFAULT\r\n"); 00108 } 00109 } 00110 00111 00112 void onListeningTCPSocketEvent(TCPSocketEvent e) 00113 { 00114 switch(e) 00115 { 00116 case TCPSOCKET_ACCEPT: 00117 printf("Listening: TCP Socket Accepted\r\n"); 00118 // Accepts connection from client and gets connected socket. 00119 err=ListeningSock.accept(&client, &pConnectedSock); 00120 if (err) { 00121 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); 00122 return; //Error in accept, discard connection 00123 } 00124 // Setup the new socket events 00125 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); 00126 // We can find out from where the connection is coming by looking at the 00127 // Host parameter of the accept() method 00128 IpAddr clientIp = client.getIp(); 00129 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", 00130 clientIp[0], clientIp[1], clientIp[2], clientIp[3]); 00131 break; 00132 // the following cases will not happen 00133 case TCPSOCKET_CONNECTED: 00134 printf("Listening: TCP Socket Connected\r\n"); 00135 break; 00136 case TCPSOCKET_WRITEABLE: 00137 printf("Listening: TCP Socket Writable\r\n"); 00138 break; 00139 case TCPSOCKET_READABLE: 00140 printf("Listening: TCP Socket Readable\r\n"); 00141 break; 00142 case TCPSOCKET_CONTIMEOUT: 00143 printf("Listening: TCP Socket Timeout\r\n"); 00144 break; 00145 case TCPSOCKET_CONRST: 00146 printf("Listening: TCP Socket CONRST\r\n"); 00147 break; 00148 case TCPSOCKET_CONABRT: 00149 printf("Listening: TCP Socket CONABRT\r\n"); 00150 break; 00151 case TCPSOCKET_ERROR: 00152 printf("Listening: TCP Socket Error\r\n"); 00153 break; 00154 case TCPSOCKET_DISCONNECTED: 00155 //Close socket... 00156 printf("Listening: TCP Socket Disconnected\r\n"); 00157 ListeningSock.close(); 00158 break; 00159 default: 00160 printf("DEFAULT\r\n"); 00161 }; 00162 } 00163 00164 00165 int main() { 00166 printf("\r\n"); 00167 printf("Setting up...\r\n"); 00168 00169 return 0; 00170 EthernetErr ethErr = eth.setup();//initialisation of ethernet stack 00171 if(ethErr) 00172 { 00173 printf("Error %d in setup.\r\n", ethErr); 00174 return -1; 00175 } 00176 printf("Setup OK\r\n"); 00177 00178 IpAddr ip = eth.getIp(); 00179 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);//address of server 00180 00181 //Set the get message handler function 00182 vSetMessageHandler(TYPE_POST, ledLighter); 00183 vSetMessageHandler(TYPE_GET, ledGetter); 00184 00185 // Set the callbacks for Listening 00186 ListeningSock.setOnEvent(&onListeningTCPSocketEvent); 00187 00188 // bind and listen on TCP 00189 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); 00190 printf("Binding..\r\n"); 00191 00192 if(err) 00193 { 00194 //Deal with that error... 00195 printf("Binding Error\n"); 00196 } 00197 err=ListeningSock.listen(); // Starts listening 00198 printf("Listening...\r\n"); 00199 if(err) 00200 { 00201 printf("Listening Error\r\n"); 00202 } 00203 00204 Timer tmr; 00205 tmr.start(); 00206 00207 while(true) 00208 { 00209 Net::poll(); 00210 if(tmr.read() > 4) // sec 00211 { 00212 led4=!led4; //Show that we are alive 00213 tmr.reset(); 00214 } 00215 } 00216 }
Generated on Sat Jul 16 2022 21:50:03 by 1.7.2