Donatien Garnier / Mbed 2 deprecated CPPSocketsTest

Dependencies:   mbed EthernetInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* cpp_sockets_test.cpp */
00002 /*
00003 Copyright (C) 2012 ARM Limited.
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of
00006 this software and associated documentation files (the "Software"), to deal in
00007 the Software without restriction, including without limitation the rights to
00008 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
00009 of the Software, and to permit persons to whom the Software is furnished to do
00010 so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in all
00013 copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021 SOFTWARE.
00022 */
00023 
00024 #define __DEBUG__ 4 //Maximum verbosity
00025 #ifndef __MODULE__
00026 #define __MODULE__ "cpp_sockets_test.cpp"
00027 #endif
00028 
00029 #include "core/fwk.h"
00030 #include "mbed.h"
00031 
00032 #include "rtos.h"
00033 
00034 #include "EthernetInterface.h"
00035 #include "TCPSocket.h"
00036 #include "UDPSocket.h"
00037 
00038 #define CHECK_ERR(ret) if(ret < 0) break;
00039 
00040 DigitalOut led1(LED1);
00041 DigitalOut led2(LED2);
00042 DigitalOut led3(LED3);
00043 DigitalOut led4(LED4);
00044 void notify(bool a, bool b, bool c)
00045 {
00046   led1 = a;
00047   led2 = b;
00048   led3 = c;
00049 }
00050 
00051 extern "C" void HardFault_Handler()
00052 {
00053   error("Hard Fault!\n");
00054 }
00055 
00056 void test(void const*)
00057 {
00058   EthernetInterface eth;
00059   DBG("Hello!");
00060   
00061   int count = 0;
00062   eth.init(); //Use DHCP
00063 
00064   count++;
00065   DBG("iteration #%d", count);
00066   
00067   char in_buf[256];
00068 
00069   notify(0, 1, 1);
00070   int ret = eth.connect();
00071   notify(0, 1, 0);
00072   if (ret == OK)
00073   {
00074     //TCP Socket test
00075     do
00076     {
00077       DBG("TCP Client test");
00078       TCPSocket sock;
00079       //http://mbed.org/media/uploads/donatien/hello.txt
00080       ret = sock.connect("mbed.org", 80);
00081       CHECK_ERR(ret);
00082       const char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n";
00083       ret = sock.send((std::uint8_t*)http_cmd, sizeof(http_cmd) - 1, 3000);
00084       CHECK_ERR(ret);
00085       
00086       bool firstIt = true;
00087       do
00088       {
00089         ret = sock.receive((std::uint8_t*)in_buf, 255, firstIt?3000:0);
00090         CHECK_ERR(ret);
00091         in_buf[ret] = '\0';
00092         DBG("Received %d chars from server: %s", ret, in_buf);
00093         firstIt = false;
00094       } while( ret > 0 );
00095       CHECK_ERR(ret);
00096       ret = sock.close();  
00097       CHECK_ERR(ret);
00098     } while(0);
00099     if(ret < 0)
00100     {
00101       WARN("TCP Client test failed");
00102     }
00103     else
00104     {
00105       DBG("TCP Client test succeeded");
00106     }
00107     
00108     do
00109     {
00110       DBG("TCP Server test");
00111       TCPSocket sock;
00112       ret = sock.bind(80); //Listen on all interfaces
00113       CHECK_ERR(ret);
00114       
00115       TCPSocket hdlrSock;
00116       ret = sock.listen(1);
00117       CHECK_ERR(ret);
00118       DBG("Now listening on port 80, open a browser and try to fetch the page");
00119       
00120       char* inHost;
00121       int inPort;
00122       ret = sock.accept(hdlrSock, &inHost, &inPort, 150000);
00123       CHECK_ERR(ret);
00124       DBG("Connection from %s on port %d", inHost, inPort);
00125       bool firstIt = true;
00126       do
00127       {
00128         ret = hdlrSock.receive((std::uint8_t*)in_buf, 255, firstIt?3000:0);
00129         CHECK_ERR(ret);
00130         in_buf[ret] = '\0';
00131         DBG("Received %d chars from client: %s", ret, in_buf);
00132         firstIt = false;
00133       } while( ret > 0 );
00134       CHECK_ERR(ret);
00135       
00136       const char http_resp[] = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nHello world!";
00137       ret = hdlrSock.send((std::uint8_t*)http_resp, sizeof(http_resp) - 1, 3000);
00138       CHECK_ERR(ret); 
00139       
00140       ret = hdlrSock.close();
00141       CHECK_ERR(ret);
00142       
00143       ret = sock.close();  
00144       CHECK_ERR(ret);
00145     } while(0);
00146     if(ret < 0)
00147     {
00148       WARN("TCP Server test failed");
00149     }
00150     else
00151     {
00152       DBG("TCP Server test succeeded");
00153     }
00154     
00155     //UDP Socket test
00156     do
00157     {
00158       DBG("UDP Client test");
00159       UDPSocket sock;
00160       sock.bind(0); //Use a random port
00161       const char daytime_cmd[] = "plop"; //Does not matter
00162       ret = sock.sendTo((std::uint8_t*)daytime_cmd, sizeof(daytime_cmd) - 1, "utcnist.colorado.edu", 37, 3000);
00163       CHECK_ERR(ret);
00164 
00165       char* inHost;
00166       int inPort;
00167       ret = sock.receiveFrom((std::uint8_t*)in_buf, 4, &inHost, &inPort, 3000);
00168       CHECK_ERR(ret);
00169       
00170       std::uint32_t timeRes = ntohl( *((std::uint32_t*)in_buf));
00171       
00172       DBG("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT", ret, inHost, inPort, timeRes);
00173         
00174       ret = sock.close();  
00175       CHECK_ERR(ret);
00176     } while(0);
00177     if(ret < 0)
00178     {
00179       WARN("UDP Client test failed");
00180     }
00181     else
00182     {
00183       DBG("UDP Client test succeeded");
00184     }
00185     
00186   }
00187   eth.disconnect();
00188   DBG("Disconnected");
00189 
00190   notify(1, 1, 1);
00191 
00192   while (1)
00193   {
00194     Thread::wait(100);
00195   }
00196 }
00197 
00198 void keepAlive(void const*)
00199 {
00200   while (1)
00201   {
00202     led1 = !led1;
00203     Thread::wait(500);
00204   }
00205 }
00206 
00207 void tick()
00208 {
00209   led4 = !led4;
00210 }
00211 
00212 int main()
00213 {
00214   Ticker t;
00215   t.attach(tick, 1);
00216   DBG_INIT();
00217   notify(1, 0, 0);
00218 
00219   Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
00220   keepAlive(NULL);
00221 
00222   return 0;
00223 }