Nelson Neves / GSL_08-HttpServer-RPC-Funcs

Dependencies:   EthernetNetIf mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* //---------------------------------------------------------------------------------------------
00002 // RPC DemoCmd
00003 http://192.168.1.100/rpc/democmd/run%201000%20100%201
00004 http://192.168.1.100/rpc/democmd/run 1000 100 1
00005 
00006 //---------------------------------------------------------------------------------------------
00007 // resources
00008 //---------------------------------------------------------------------------------------------
00009 http://mbed.org/handbook/C-Data-Types
00010 http://mbed.org/cookbook/RPC-Interface-Library
00011 http://mbed.org/cookbook/HTTP-Server
00012 http://mbed.org/cookbook/Ethernet
00013 http://mbed.org/handbook/Ticker
00014 //--------------------------------------------------------------------------------------------- */
00015 #include "mbed.h"
00016 #include "EthernetNetIf.h"
00017 #include "HTTPServer.h"
00018 #include "RPCFunction.h"
00019 //---------------------------------------------------------------------------------------------
00020 DigitalOut myled(LED1);
00021 Serial pc(USBTX, USBRX); // tx, rx
00022 //---------------------------------------------------------------------------------------------
00023 #define internaldebug // send debug messages to USB Serial port (9600,1,N)
00024 //#define dhcpenable    // auto-setup IP Address from DHCP router
00025 //---------------------------------------------------------------------------------------------
00026 // Timer Interrupt - NetPool
00027 //---------------------------------------------------------------------------------------------
00028 Ticker netpool;
00029 //---------------------------------------------------------------------------------------------
00030 // Ethernet Object Setup
00031 //---------------------------------------------------------------------------------------------
00032 #ifdef dhcpenable
00033   EthernetNetIf eth;  
00034 #else
00035   EthernetNetIf eth(
00036     IpAddr(192,168,1,100), //IP Address
00037     IpAddr(255,255,255,0), //Network Mask
00038     IpAddr(192,168,1,254), //Gateway
00039     IpAddr(192,168,1,254)  //DNS
00040   );
00041 #endif
00042 //---------------------------------------------------------------------------------------------
00043 // HTTP Server
00044 //---------------------------------------------------------------------------------------------
00045 HTTPServer httpserver;
00046 LocalFileSystem fs("webfs");
00047 
00048 //---------------------------------------------------------------------------------------------
00049 
00050 //---------------------------------------------------------------------------------------------
00051 // ISR -> Pool Ethernet - will be triggered by netpool ticker
00052 //---------------------------------------------------------------------------------------------
00053 void netpoolupdate()
00054 {
00055     Net::poll();
00056 }
00057 //---------------------------------------------------------------------------------------------
00058 
00059 //---------------------------------------------------------------------------------------------
00060 // RPC TList Commands (Task/Job List)
00061 //---------------------------------------------------------------------------------------------
00062 void rpc_democmd(char *input, char *output)
00063 {
00064   int iarg1 = 0;
00065   int iarg2 = 0;
00066   int iarg3 = 0;
00067   
00068   sscanf(input, "%i %i %i", &iarg1, &iarg2, &iarg3);
00069     
00070   #ifdef internaldebug
00071     printf("Calling RCP Function DemoCmd.\r\n");
00072     printf("INPUT: %s.\r\n", input);
00073     printf("OUTPUT: %s.\r\n", output);
00074     printf("ARG1: %d\r\n", iarg1);
00075     printf("ARG2: %d\r\n", iarg2);
00076     printf("ARG3: %d\r\n", iarg3);
00077   #endif 
00078   
00079   sprintf(output, "<html><body>RCP Demo Cmd Completed!</body></html>");
00080 }
00081 //---------------------------------------------------------------------------------------------
00082 
00083 
00084 //---------------------------------------------------------------------------------------------
00085 // MAIN
00086 //---------------------------------------------------------------------------------------------
00087 int main() 
00088 {
00089   // Set Serial Port Transfer Rate
00090   pc.baud(115200);  
00091 
00092   //--------------------------------------------------------
00093   // Setting Ethernet
00094   //--------------------------------------------------------    
00095   #ifdef internaldebug
00096     printf("\r\nSetting up Ethernet interface!\r\n");
00097   #endif
00098   // Create return object for error check
00099   EthernetErr ethErr = eth.setup(); 
00100   if(ethErr)
00101   {
00102     #ifdef internaldebug
00103       printf("\r\nError %d in Ethernet setup.\r\n", ethErr);
00104     #endif
00105     return -1;
00106   }
00107   #ifdef internaldebug
00108     printf("\r\nEthernet setup completed with success!\r\n");
00109   #endif  
00110   //--------------------------------------------------------    
00111 
00112   //--------------------------------------------------------
00113   // adding RPC functions
00114   //--------------------------------------------------------
00115   RPCFunction RPCdemocmd(&rpc_democmd, "democmd");  
00116   //--------------------------------------------------------
00117 
00118   //--------------------------------------------------------
00119   // adding Handlers 
00120   //--------------------------------------------------------  
00121   FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00122  
00123   httpserver.addHandler<RPCHandler>("/rpc");
00124   httpserver.addHandler<FSHandler>("/"); //Default handler  
00125   //--------------------------------------------------------
00126   
00127   //--------------------------------------------------------
00128   // bind http server to port 80 (Listen)
00129   //--------------------------------------------------------       
00130   httpserver.bind(80);  
00131   #ifdef internaldebug
00132     printf("Listening on port 80!\r\n");
00133   #endif
00134   //--------------------------------------------------------         
00135     
00136   //--------------------------------------------------------
00137   // ISR -> attach timer interrupt to update Net::Pool();
00138   //--------------------------------------------------------
00139   netpool.attach(&netpoolupdate, 0.1);
00140   //--------------------------------------------------------
00141 
00142   //--------------------------------------------------------    
00143   // main loop
00144   //--------------------------------------------------------    
00145   while(1) 
00146   {    
00147     myled = 1;
00148     wait(0.5);
00149     myled = 0;
00150     wait(0.5);     
00151   }
00152 }
00153 //---------------------------------------------------------------------------------------------