A simple Firmware Updating program.

Dependencies:   FirmwareUpdater NetServices RPCInterface mbed

Fork of NetServices_HelloWorld by Segundo Equipo

Revision:
2:16857d9ab50d
Parent:
1:57f922fe8fb5
Child:
3:aeaa9fa8cda0
--- a/main.cpp	Tue Nov 16 20:04:25 2010 +0000
+++ b/main.cpp	Fri Nov 19 21:54:02 2010 +0000
@@ -2,22 +2,49 @@
 #include "EthernetNetIf.h"
 #include "HTTPClient.h"
 #include "NTPClient.h"
+#include "HTTPServer.h"
+#include "RPCFunction.h"
 
-EthernetNetIf eth("mbedSE");
+#define HOSTNAME "mbedSE"
+
+EthernetNetIf eth(HOSTNAME);
 HTTPClient http;
 NTPClient ntp;
+HTTPServer svr;
+
+DigitalOut led1(LED1, "led1");
+DigitalOut led2(LED2, "led2");
+DigitalOut led3(LED3, "led3");
+DigitalOut led4(LED4, "led4");
+
+void DoEcho(char* input, char* output);
+RPCFunction Echo(&DoEcho, "echo");
+
+void DoEcho(char* input, char* output) {
+    printf("%s\n", input);
+    strcpy(output, input);
+}
 
 int main() {
 
-    printf("Setting up...\n");
-    EthernetErr ethErr = eth.setup();
-    if (ethErr) {
-        printf("Error %d in setup\n", ethErr);
-        return -1;
-    }
+    printf("\n\n/* NetServices tribute library demonstration */\n");
+    printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n");
+    EthernetErr ethErr;
+    do {
+        printf("Setting up...\n");
+        ethErr = eth.setup();
+        if (ethErr) printf("Timeout\n", ethErr);
+    } while (ethErr != ETH_OK);
+
+    printf("Connected OK\n");
+    const char* hwAddr = eth.getHwAddr();
+    printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
+           hwAddr[0], hwAddr[1], hwAddr[2],
+           hwAddr[3], hwAddr[4], hwAddr[5]);
 
     IpAddr ethIp = eth.getIp();
-    printf("Connected ok, IP : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
+    printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
+    printf("Check router DHCP table for name : %s\n", HOSTNAME);
 
     printf("\nHTTPClient get...\n");
     HTTPText txt;
@@ -38,6 +65,31 @@
     ctTime = time(NULL);
     printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
 
-    printf("Done!\n");
-    return 0;
+    printf("NTP setTime to a deliberately incorrect server...\n");
+    server.setName("www.google.com");
+    printf("Result : %d\n", ntp.setTime(server));
+
+    ctTime = time(NULL);
+    printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime));
+    
+    char ip[16];
+    sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
+    
+#define PORT 80
+    svr.addHandler<RPCHandler>("/rpc");
+    svr.bind(PORT);
+    printf("Server listening (port %d)\n", PORT);
+    printf("- LED1 should flash\n");
+    printf("- type into browser to test url decode : %s/rpc/echo/run \"quoted string with <>\"\n", ip);
+
+    Timer tm;
+    tm.start();
+
+    while (true) {
+        Net::poll();
+        if (tm.read() > 0.5) {
+            led1 = !led1;
+            tm.start();
+        }
+    }
 }
\ No newline at end of file