Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "main.h" 00002 00003 int main() 00004 { 00005 Led = eth_status; 00006 queue = mbed_event_queue(); 00007 while(1) if(eth_connect()) Led = recv(); 00008 } 00009 00010 bool eth_connect(void) 00011 { 00012 switch(eth.get_connection_status()) 00013 { 00014 case NSAPI_STATUS_DISCONNECTED: 00015 eth.set_blocking(false); 00016 eth.set_network(SocketAddress(IP_SERVER), SocketAddress("255.255.255.0"), SocketAddress("192.168.1.1")); // if not static eth.set_dhcp(true); 00017 eth.attach(callback(ð_event)); 00018 eth.connect(); break; 00019 case NSAPI_STATUS_GLOBAL_UP: return CONNECT; break; 00020 default: break; 00021 } 00022 return false; 00023 } 00024 00025 void eth_event(nsapi_event_t status, intptr_t param) 00026 { 00027 switch(param) 00028 { 00029 case NSAPI_STATUS_DISCONNECTED: eth_status = RED_DISCONNECTED; break; 00030 case NSAPI_STATUS_CONNECTING:if(eth_status == BLUE_CLIENT) clientTCP->close(); 00031 eth_status = YELLOW_CONNECTING; break; 00032 case NSAPI_STATUS_GLOBAL_UP: eth_status = GREEN_GLOBAL_UP; 00033 if(!CONNECT) serverTCP_connect(); 00034 else serverTCP_event(); break; 00035 default: break; 00036 } 00037 } 00038 00039 void eth_state(void) 00040 { 00041 switch(eth.get_connection_status()) 00042 { 00043 case NSAPI_STATUS_DISCONNECTED: eth_status = RED_DISCONNECTED; break; 00044 case NSAPI_STATUS_CONNECTING: eth_status = YELLOW_CONNECTING; break; 00045 case NSAPI_STATUS_GLOBAL_UP: eth_status = GREEN_GLOBAL_UP; break; 00046 default: break; 00047 } 00048 } 00049 00050 bool serverTCP_connect(void) 00051 { 00052 if(!CONNECT) 00053 if(serverTCP.open(ð) == NSAPI_ERROR_OK) 00054 if(serverTCP.bind(PORT_SERVER) == NSAPI_ERROR_OK) 00055 if(serverTCP.listen() == NSAPI_ERROR_OK) 00056 { 00057 serverTCP.set_blocking(false); 00058 serverTCP.sigio(callback(&serverTCP_event)); 00059 CONNECT = true; 00060 } 00061 return CONNECT; 00062 } 00063 00064 void serverTCP_event(void) 00065 { 00066 queue->call(&serverTCP_accept); 00067 } 00068 00069 void serverTCP_accept(void) 00070 { 00071 if(eth_status == GREEN_GLOBAL_UP) 00072 { 00073 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; 00074 eth_status = MAGENTA_ACCEPT; 00075 clientTCP = serverTCP.accept(&ack); 00076 switch(ack) 00077 { 00078 case NSAPI_ERROR_OK: 00079 clientTCP->set_timeout(TIMEOUT_SERVER); // config client bloquante avec timeout sinon limite de transmission a 1072 octets 00080 eth_status = BLUE_CLIENT; 00081 break; 00082 case NSAPI_ERROR_NO_CONNECTION: 00083 eth_state(); 00084 serverTCP_event(); 00085 break; 00086 default: 00087 eth_state(); 00088 break; 00089 } 00090 } 00091 } 00092 00093 enum_status recv(void) 00094 { 00095 if(eth_connect() && (eth_status == BLUE_CLIENT)) 00096 { 00097 char buffer[BUFFER_SIZE] = {0}; 00098 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK, size = 0; 00099 while((ack = clientTCP->recv(&buffer[size], BUFFER_SIZE-size)) > NSAPI_ERROR_OK) size += ack; 00100 if(!size && (ack == NSAPI_ERROR_OK) || (ack == NSAPI_ERROR_NO_CONNECTION)) 00101 { 00102 clientTCP->close(); 00103 eth_state(); 00104 serverTCP_event(); 00105 } 00106 if(size) 00107 { 00108 string cmd(buffer); 00109 for(char &c : cmd) if((c >= 'a') && (c <= 'z')) c += 'A'-'a'; 00110 if(!cmd.empty()) 00111 { 00112 ostringstream ssend; 00113 if(cmd.find("GET / HTTP") != string::npos) 00114 { 00115 ssend << "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nAccess-Control-Allow-Origin: *\r\n\r\n"; 00116 ssend << "<!DOCTYPE html>\r\n<html>\r\n\t<head>\r\n\t\t<title>" << MBED_PROJECT << "</title>\r\n\t\t<meta http-equiv=refresh content=10>\r\n\t</head>"; 00117 ssend << "\r\n\t<body style=background-color:dimgray>\r\n\t\t<center>\r\n\t\t\t<h1>" << MBED_PROJECT << "</h1>\r\n\t\t</center>\r\n\t</body>\r\n</html>"; 00118 } 00119 else ssend << "HTTP/1.1 204 No Content\r\n"; 00120 send(ssend.str()); 00121 } 00122 } 00123 } 00124 return eth_status; 00125 } 00126 00127 nsapi_error_t send(const string& buff) 00128 { 00129 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; 00130 string ssend(buff+"\n"); 00131 if((eth_status == BLUE_CLIENT) && !buff.empty()) 00132 ack = clientTCP->send(ssend.c_str(), ssend.size()); 00133 //------------------- HTTP request must close 00134 clientTCP->close(); 00135 eth_state(); 00136 serverTCP_event(); 00137 //------------------- 00138 return ack; 00139 }
Generated on Wed Jul 27 2022 23:12:10 by
1.7.2