Test HTTP Server, including RPCFunction

Dependencies:   EthernetNetIf mbed HTTPServer

Committer:
pbaston
Date:
Thu Feb 03 20:23:51 2011 +0000
Revision:
0:dacaf456d264

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbaston 0:dacaf456d264 1 #include "mbed.h"
pbaston 0:dacaf456d264 2 #include "EthernetNetIf.h"
pbaston 0:dacaf456d264 3 #include "HTTPServer.h"
pbaston 0:dacaf456d264 4 #include "RPCFunction.h"
pbaston 0:dacaf456d264 5
pbaston 0:dacaf456d264 6 EthernetNetIf eth;
pbaston 0:dacaf456d264 7
pbaston 0:dacaf456d264 8 HTTPServer svr;
pbaston 0:dacaf456d264 9
pbaston 0:dacaf456d264 10 LocalFileSystem fs("webfs");
pbaston 0:dacaf456d264 11
pbaston 0:dacaf456d264 12 //Create a function of the required format
pbaston 0:dacaf456d264 13 void testFunc(char * input, char * output);
pbaston 0:dacaf456d264 14 //Attach it to an RPC object
pbaston 0:dacaf456d264 15 RPCFunction rpcTestFunc(&testFunc, "testFunc");
pbaston 0:dacaf456d264 16
pbaston 0:dacaf456d264 17 int main() {
pbaston 0:dacaf456d264 18
pbaston 0:dacaf456d264 19 Base::add_rpc_class<DigitalOut>();
pbaston 0:dacaf456d264 20 Base::add_rpc_class<PwmOut>();
pbaston 0:dacaf456d264 21 Base::add_rpc_class<AnalogIn>();
pbaston 0:dacaf456d264 22
pbaston 0:dacaf456d264 23
pbaston 0:dacaf456d264 24 printf("Setting up...\r\n");
pbaston 0:dacaf456d264 25 EthernetErr ethErr = eth.setup();
pbaston 0:dacaf456d264 26 if (ethErr) {
pbaston 0:dacaf456d264 27 printf("Error %d in setup.\r\n", ethErr);
pbaston 0:dacaf456d264 28 return -1;
pbaston 0:dacaf456d264 29 }
pbaston 0:dacaf456d264 30 printf("Setup OK\r\n");
pbaston 0:dacaf456d264 31
pbaston 0:dacaf456d264 32 FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
pbaston 0:dacaf456d264 33
pbaston 0:dacaf456d264 34 svr.addHandler<SimpleHandler>("/hello"); //Default handler
pbaston 0:dacaf456d264 35 svr.addHandler<RPCHandler>("/rpc");
pbaston 0:dacaf456d264 36 svr.addHandler<FSHandler>("/"); //Default handler
pbaston 0:dacaf456d264 37
pbaston 0:dacaf456d264 38 svr.bind(80);
pbaston 0:dacaf456d264 39
pbaston 0:dacaf456d264 40 printf("Listening...\r\n");
pbaston 0:dacaf456d264 41
pbaston 0:dacaf456d264 42 Timer tm;
pbaston 0:dacaf456d264 43 tm.start();
pbaston 0:dacaf456d264 44 //Listen indefinitely
pbaston 0:dacaf456d264 45 while (true) {
pbaston 0:dacaf456d264 46 Net::poll();
pbaston 0:dacaf456d264 47 if (tm.read()>.5) {
pbaston 0:dacaf456d264 48 tm.start();
pbaston 0:dacaf456d264 49 printf("alive!\r\n");
pbaston 0:dacaf456d264 50 }
pbaston 0:dacaf456d264 51 }
pbaston 0:dacaf456d264 52
pbaston 0:dacaf456d264 53 return 0;
pbaston 0:dacaf456d264 54 }
pbaston 0:dacaf456d264 55
pbaston 0:dacaf456d264 56 void testFunc(char * input, char * output) {
pbaston 0:dacaf456d264 57
pbaston 0:dacaf456d264 58 static int toggle=0;
pbaston 0:dacaf456d264 59
pbaston 0:dacaf456d264 60 if (toggle != 0) {
pbaston 0:dacaf456d264 61 sprintf(output, "Hello");
pbaston 0:dacaf456d264 62 toggle = 0;
pbaston 0:dacaf456d264 63 } else {
pbaston 0:dacaf456d264 64 sprintf(output, "Bye !");
pbaston 0:dacaf456d264 65 toggle = 1;
pbaston 0:dacaf456d264 66 }
pbaston 0:dacaf456d264 67 }
pbaston 0:dacaf456d264 68