Daniel Peter / NetServices_HelloWorld
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPClient.h"
00004 #include "NTPClient.h"
00005 #include "HTTPServer.h"
00006 #include "RPCFunction.h"
00007 
00008 #include "AvailableMemory.h"
00009 
00010 #define HOSTNAME "mbedSE"
00011 
00012 EthernetNetIf eth(HOSTNAME);
00013 HTTPClient http;
00014 NTPClient ntp;
00015 HTTPServer svr;
00016 
00017 DigitalOut led1(LED1, "led1");
00018 DigitalOut led2(LED2, "led2");
00019 DigitalOut led3(LED3, "led3");
00020 DigitalOut led4(LED4, "led4");
00021 
00022 void DoEcho(char* input, char* output);
00023 RPCFunction Echo(&DoEcho, "echo");
00024 
00025 void DoEcho(char* input, char* output) {
00026     printf("%s\n", input);
00027     strcpy(output, input);
00028 }
00029 
00030 int main() {
00031 
00032     printf("\n\n/* Segundo Equipo NetServices library demonstration */\n");
00033     printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
00034     EthernetErr ethErr;
00035     int count = 0;
00036     do {
00037         printf("Setting up %d...\n", ++count);
00038         ethErr = eth.setup();
00039         if (ethErr) printf("Timeout\n", ethErr);
00040     } while (ethErr != ETH_OK);
00041 
00042     printf("Connected OK\n");
00043     const char* hwAddr = eth.getHwAddr();
00044     printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
00045            hwAddr[0], hwAddr[1], hwAddr[2],
00046            hwAddr[3], hwAddr[4], hwAddr[5]);
00047 
00048     IpAddr ethIp = eth.getIp();
00049     printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00050     printf("Check router DHCP table for name : %s\n", eth.getHostname());
00051 
00052     printf("\nHTTPClient get...\n");
00053     HTTPText txt;
00054     HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);
00055     if (r==HTTP_OK) {
00056         printf("Result ok : %s\n", txt.gets());
00057     } else {
00058         printf("Error %d\n", r);
00059     }
00060 
00061     time_t ctTime;
00062     ctTime = time(NULL);
00063     printf("\nCurrent time is (UTC): %d %s\n", ctTime, ctime(&ctTime));
00064     printf("NTP setTime...\n");
00065     Host server(IpAddr(), 123, "pool.ntp.org");
00066     printf("Result : %d\n", ntp.setTime(server));
00067 
00068     ctTime = time(NULL);
00069     printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
00070 
00071     printf("NTP setTime to a deliberately incorrect server...\n");
00072     server.setName("www.google.com");
00073     printf("Result : %d\n", ntp.setTime(server));
00074 
00075     ctTime = time(NULL);
00076     printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime));
00077     
00078     char ip[16];
00079     sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00080     
00081 #define PORT 80
00082     svr.addHandler<RPCHandler>("/rpc");
00083     svr.bind(PORT);
00084     printf("Server listening (port %d)\n", PORT);
00085     printf("- LED1 should flash\n");
00086     printf("- type into browser to test url decode : %s/rpc/echo/run \"quoted string with <>\" or http://192.168.1.103/rpc/echo/run%%20%%22quoted%%20string%%20with%%20%%3C%%3E%%22\n", ip);
00087 
00088     printf("Available memory (exact bytes) : %d\n", AvailableMemory(1));
00089     
00090     Timer tm;
00091     tm.start();
00092 
00093     while (true) {
00094         Net::poll();
00095         if (tm.read() > 0.5) {
00096             led1 = !led1;
00097             tm.start();
00098         }
00099     }
00100 }