Simple code to demonstrate how to map an RPC global function that receives 3 parameters via REST HTTP Get request. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed HTTPServer
main.cpp@0:3abfd4f28df8, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 23:11:50 2011 +0000
- Revision:
- 0:3abfd4f28df8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:3abfd4f28df8 | 1 | /* //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 2 | // RPC DemoCmd |
botdream | 0:3abfd4f28df8 | 3 | http://192.168.1.100/rpc/democmd/run%201000%20100%201 |
botdream | 0:3abfd4f28df8 | 4 | http://192.168.1.100/rpc/democmd/run 1000 100 1 |
botdream | 0:3abfd4f28df8 | 5 | |
botdream | 0:3abfd4f28df8 | 6 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 7 | // resources |
botdream | 0:3abfd4f28df8 | 8 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 9 | http://mbed.org/handbook/C-Data-Types |
botdream | 0:3abfd4f28df8 | 10 | http://mbed.org/cookbook/RPC-Interface-Library |
botdream | 0:3abfd4f28df8 | 11 | http://mbed.org/cookbook/HTTP-Server |
botdream | 0:3abfd4f28df8 | 12 | http://mbed.org/cookbook/Ethernet |
botdream | 0:3abfd4f28df8 | 13 | http://mbed.org/handbook/Ticker |
botdream | 0:3abfd4f28df8 | 14 | //--------------------------------------------------------------------------------------------- */ |
botdream | 0:3abfd4f28df8 | 15 | #include "mbed.h" |
botdream | 0:3abfd4f28df8 | 16 | #include "EthernetNetIf.h" |
botdream | 0:3abfd4f28df8 | 17 | #include "HTTPServer.h" |
botdream | 0:3abfd4f28df8 | 18 | #include "RPCFunction.h" |
botdream | 0:3abfd4f28df8 | 19 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 20 | DigitalOut myled(LED1); |
botdream | 0:3abfd4f28df8 | 21 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:3abfd4f28df8 | 22 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 23 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:3abfd4f28df8 | 24 | //#define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:3abfd4f28df8 | 25 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 26 | // Timer Interrupt - NetPool |
botdream | 0:3abfd4f28df8 | 27 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 28 | Ticker netpool; |
botdream | 0:3abfd4f28df8 | 29 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 30 | // Ethernet Object Setup |
botdream | 0:3abfd4f28df8 | 31 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 32 | #ifdef dhcpenable |
botdream | 0:3abfd4f28df8 | 33 | EthernetNetIf eth; |
botdream | 0:3abfd4f28df8 | 34 | #else |
botdream | 0:3abfd4f28df8 | 35 | EthernetNetIf eth( |
botdream | 0:3abfd4f28df8 | 36 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:3abfd4f28df8 | 37 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:3abfd4f28df8 | 38 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:3abfd4f28df8 | 39 | IpAddr(192,168,1,254) //DNS |
botdream | 0:3abfd4f28df8 | 40 | ); |
botdream | 0:3abfd4f28df8 | 41 | #endif |
botdream | 0:3abfd4f28df8 | 42 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 43 | // HTTP Server |
botdream | 0:3abfd4f28df8 | 44 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 45 | HTTPServer httpserver; |
botdream | 0:3abfd4f28df8 | 46 | LocalFileSystem fs("webfs"); |
botdream | 0:3abfd4f28df8 | 47 | |
botdream | 0:3abfd4f28df8 | 48 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 49 | |
botdream | 0:3abfd4f28df8 | 50 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 51 | // ISR -> Pool Ethernet - will be triggered by netpool ticker |
botdream | 0:3abfd4f28df8 | 52 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 53 | void netpoolupdate() |
botdream | 0:3abfd4f28df8 | 54 | { |
botdream | 0:3abfd4f28df8 | 55 | Net::poll(); |
botdream | 0:3abfd4f28df8 | 56 | } |
botdream | 0:3abfd4f28df8 | 57 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 58 | |
botdream | 0:3abfd4f28df8 | 59 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 60 | // RPC TList Commands (Task/Job List) |
botdream | 0:3abfd4f28df8 | 61 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 62 | void rpc_democmd(char *input, char *output) |
botdream | 0:3abfd4f28df8 | 63 | { |
botdream | 0:3abfd4f28df8 | 64 | int iarg1 = 0; |
botdream | 0:3abfd4f28df8 | 65 | int iarg2 = 0; |
botdream | 0:3abfd4f28df8 | 66 | int iarg3 = 0; |
botdream | 0:3abfd4f28df8 | 67 | |
botdream | 0:3abfd4f28df8 | 68 | sscanf(input, "%i %i %i", &iarg1, &iarg2, &iarg3); |
botdream | 0:3abfd4f28df8 | 69 | |
botdream | 0:3abfd4f28df8 | 70 | #ifdef internaldebug |
botdream | 0:3abfd4f28df8 | 71 | printf("Calling RCP Function DemoCmd.\r\n"); |
botdream | 0:3abfd4f28df8 | 72 | printf("INPUT: %s.\r\n", input); |
botdream | 0:3abfd4f28df8 | 73 | printf("OUTPUT: %s.\r\n", output); |
botdream | 0:3abfd4f28df8 | 74 | printf("ARG1: %d\r\n", iarg1); |
botdream | 0:3abfd4f28df8 | 75 | printf("ARG2: %d\r\n", iarg2); |
botdream | 0:3abfd4f28df8 | 76 | printf("ARG3: %d\r\n", iarg3); |
botdream | 0:3abfd4f28df8 | 77 | #endif |
botdream | 0:3abfd4f28df8 | 78 | |
botdream | 0:3abfd4f28df8 | 79 | sprintf(output, "<html><body>RCP Demo Cmd Completed!</body></html>"); |
botdream | 0:3abfd4f28df8 | 80 | } |
botdream | 0:3abfd4f28df8 | 81 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 82 | |
botdream | 0:3abfd4f28df8 | 83 | |
botdream | 0:3abfd4f28df8 | 84 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 85 | // MAIN |
botdream | 0:3abfd4f28df8 | 86 | //--------------------------------------------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 87 | int main() |
botdream | 0:3abfd4f28df8 | 88 | { |
botdream | 0:3abfd4f28df8 | 89 | // Set Serial Port Transfer Rate |
botdream | 0:3abfd4f28df8 | 90 | pc.baud(115200); |
botdream | 0:3abfd4f28df8 | 91 | |
botdream | 0:3abfd4f28df8 | 92 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 93 | // Setting Ethernet |
botdream | 0:3abfd4f28df8 | 94 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 95 | #ifdef internaldebug |
botdream | 0:3abfd4f28df8 | 96 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:3abfd4f28df8 | 97 | #endif |
botdream | 0:3abfd4f28df8 | 98 | // Create return object for error check |
botdream | 0:3abfd4f28df8 | 99 | EthernetErr ethErr = eth.setup(); |
botdream | 0:3abfd4f28df8 | 100 | if(ethErr) |
botdream | 0:3abfd4f28df8 | 101 | { |
botdream | 0:3abfd4f28df8 | 102 | #ifdef internaldebug |
botdream | 0:3abfd4f28df8 | 103 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:3abfd4f28df8 | 104 | #endif |
botdream | 0:3abfd4f28df8 | 105 | return -1; |
botdream | 0:3abfd4f28df8 | 106 | } |
botdream | 0:3abfd4f28df8 | 107 | #ifdef internaldebug |
botdream | 0:3abfd4f28df8 | 108 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:3abfd4f28df8 | 109 | #endif |
botdream | 0:3abfd4f28df8 | 110 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 111 | |
botdream | 0:3abfd4f28df8 | 112 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 113 | // adding RPC functions |
botdream | 0:3abfd4f28df8 | 114 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 115 | RPCFunction RPCdemocmd(&rpc_democmd, "democmd"); |
botdream | 0:3abfd4f28df8 | 116 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 117 | |
botdream | 0:3abfd4f28df8 | 118 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 119 | // adding Handlers |
botdream | 0:3abfd4f28df8 | 120 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 121 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
botdream | 0:3abfd4f28df8 | 122 | |
botdream | 0:3abfd4f28df8 | 123 | httpserver.addHandler<RPCHandler>("/rpc"); |
botdream | 0:3abfd4f28df8 | 124 | httpserver.addHandler<FSHandler>("/"); //Default handler |
botdream | 0:3abfd4f28df8 | 125 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 126 | |
botdream | 0:3abfd4f28df8 | 127 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 128 | // bind http server to port 80 (Listen) |
botdream | 0:3abfd4f28df8 | 129 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 130 | httpserver.bind(80); |
botdream | 0:3abfd4f28df8 | 131 | #ifdef internaldebug |
botdream | 0:3abfd4f28df8 | 132 | printf("Listening on port 80!\r\n"); |
botdream | 0:3abfd4f28df8 | 133 | #endif |
botdream | 0:3abfd4f28df8 | 134 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 135 | |
botdream | 0:3abfd4f28df8 | 136 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 137 | // ISR -> attach timer interrupt to update Net::Pool(); |
botdream | 0:3abfd4f28df8 | 138 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 139 | netpool.attach(&netpoolupdate, 0.1); |
botdream | 0:3abfd4f28df8 | 140 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 141 | |
botdream | 0:3abfd4f28df8 | 142 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 143 | // main loop |
botdream | 0:3abfd4f28df8 | 144 | //-------------------------------------------------------- |
botdream | 0:3abfd4f28df8 | 145 | while(1) |
botdream | 0:3abfd4f28df8 | 146 | { |
botdream | 0:3abfd4f28df8 | 147 | myled = 1; |
botdream | 0:3abfd4f28df8 | 148 | wait(0.5); |
botdream | 0:3abfd4f28df8 | 149 | myled = 0; |
botdream | 0:3abfd4f28df8 | 150 | wait(0.5); |
botdream | 0:3abfd4f28df8 | 151 | } |
botdream | 0:3abfd4f28df8 | 152 | } |
botdream | 0:3abfd4f28df8 | 153 | //--------------------------------------------------------------------------------------------- |