![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet
Dependencies: NTPClient_NetServices mbed
NetServices/services/http/server/impl/RPCHandler.cpp@0:05ccbd4f84f1, 2013-12-02 (annotated)
- Committer:
- hendrikvincent
- Date:
- Mon Dec 02 09:01:23 2013 +0000
- Revision:
- 0:05ccbd4f84f1
eerste programma;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hendrikvincent | 0:05ccbd4f84f1 | 1 | #pragma diag_remark 1293 |
hendrikvincent | 0:05ccbd4f84f1 | 2 | /* |
hendrikvincent | 0:05ccbd4f84f1 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
hendrikvincent | 0:05ccbd4f84f1 | 4 | |
hendrikvincent | 0:05ccbd4f84f1 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
hendrikvincent | 0:05ccbd4f84f1 | 6 | of this software and associated documentation files (the "Software"), to deal |
hendrikvincent | 0:05ccbd4f84f1 | 7 | in the Software without restriction, including without limitation the rights |
hendrikvincent | 0:05ccbd4f84f1 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hendrikvincent | 0:05ccbd4f84f1 | 9 | copies of the Software, and to permit persons to whom the Software is |
hendrikvincent | 0:05ccbd4f84f1 | 10 | furnished to do so, subject to the following conditions: |
hendrikvincent | 0:05ccbd4f84f1 | 11 | |
hendrikvincent | 0:05ccbd4f84f1 | 12 | The above copyright notice and this permission notice shall be included in |
hendrikvincent | 0:05ccbd4f84f1 | 13 | all copies or substantial portions of the Software. |
hendrikvincent | 0:05ccbd4f84f1 | 14 | |
hendrikvincent | 0:05ccbd4f84f1 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hendrikvincent | 0:05ccbd4f84f1 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hendrikvincent | 0:05ccbd4f84f1 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hendrikvincent | 0:05ccbd4f84f1 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hendrikvincent | 0:05ccbd4f84f1 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hendrikvincent | 0:05ccbd4f84f1 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hendrikvincent | 0:05ccbd4f84f1 | 21 | THE SOFTWARE. |
hendrikvincent | 0:05ccbd4f84f1 | 22 | */ |
hendrikvincent | 0:05ccbd4f84f1 | 23 | |
hendrikvincent | 0:05ccbd4f84f1 | 24 | #include "RPCHandler.h" |
hendrikvincent | 0:05ccbd4f84f1 | 25 | #include "rpc.h" |
hendrikvincent | 0:05ccbd4f84f1 | 26 | #include "url.h" |
hendrikvincent | 0:05ccbd4f84f1 | 27 | |
hendrikvincent | 0:05ccbd4f84f1 | 28 | //#define __DEBUG |
hendrikvincent | 0:05ccbd4f84f1 | 29 | #include "dbg/dbg.h" |
hendrikvincent | 0:05ccbd4f84f1 | 30 | |
hendrikvincent | 0:05ccbd4f84f1 | 31 | #define RPC_DATA_LEN 128 |
hendrikvincent | 0:05ccbd4f84f1 | 32 | |
hendrikvincent | 0:05ccbd4f84f1 | 33 | RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) |
hendrikvincent | 0:05ccbd4f84f1 | 34 | {} |
hendrikvincent | 0:05ccbd4f84f1 | 35 | |
hendrikvincent | 0:05ccbd4f84f1 | 36 | RPCHandler::~RPCHandler() |
hendrikvincent | 0:05ccbd4f84f1 | 37 | { |
hendrikvincent | 0:05ccbd4f84f1 | 38 | DBG("\r\nHandler destroyed\r\n"); |
hendrikvincent | 0:05ccbd4f84f1 | 39 | } |
hendrikvincent | 0:05ccbd4f84f1 | 40 | |
hendrikvincent | 0:05ccbd4f84f1 | 41 | void RPCHandler::doGet() |
hendrikvincent | 0:05ccbd4f84f1 | 42 | { |
hendrikvincent | 0:05ccbd4f84f1 | 43 | DBG("\r\nIn RPCHandler::doGet()\r\n"); |
hendrikvincent | 0:05ccbd4f84f1 | 44 | char resp[RPC_DATA_LEN] = {0}; |
hendrikvincent | 0:05ccbd4f84f1 | 45 | char req[RPC_DATA_LEN] = {0}; |
hendrikvincent | 0:05ccbd4f84f1 | 46 | |
hendrikvincent | 0:05ccbd4f84f1 | 47 | DBG("\r\nPath : %s\r\n", path().c_str()); |
hendrikvincent | 0:05ccbd4f84f1 | 48 | DBG("\r\nRoot Path : %s\r\n", rootPath().c_str()); |
hendrikvincent | 0:05ccbd4f84f1 | 49 | |
hendrikvincent | 0:05ccbd4f84f1 | 50 | //Remove path |
hendrikvincent | 0:05ccbd4f84f1 | 51 | strncpy(req, path().c_str(), RPC_DATA_LEN-1); |
hendrikvincent | 0:05ccbd4f84f1 | 52 | DBG("\r\nRPC req : %s\r\n", req); |
hendrikvincent | 0:05ccbd4f84f1 | 53 | |
hendrikvincent | 0:05ccbd4f84f1 | 54 | //Remove %20, +, from req |
hendrikvincent | 0:05ccbd4f84f1 | 55 | cleanReq(req); |
hendrikvincent | 0:05ccbd4f84f1 | 56 | DBG("\r\nRPC req : %s\r\n", req); |
hendrikvincent | 0:05ccbd4f84f1 | 57 | |
hendrikvincent | 0:05ccbd4f84f1 | 58 | //Do RPC Call |
hendrikvincent | 0:05ccbd4f84f1 | 59 | mbed::rpc(req, resp); //FIXME: Use bool result |
hendrikvincent | 0:05ccbd4f84f1 | 60 | |
hendrikvincent | 0:05ccbd4f84f1 | 61 | //Response |
hendrikvincent | 0:05ccbd4f84f1 | 62 | setContentLen( strlen(resp) ); |
hendrikvincent | 0:05ccbd4f84f1 | 63 | |
hendrikvincent | 0:05ccbd4f84f1 | 64 | //Make sure that the browser won't cache this request |
hendrikvincent | 0:05ccbd4f84f1 | 65 | respHeaders()["Cache-control"]="no-cache;no-store"; |
hendrikvincent | 0:05ccbd4f84f1 | 66 | // respHeaders()["Cache-control"]="no-store"; |
hendrikvincent | 0:05ccbd4f84f1 | 67 | respHeaders()["Pragma"]="no-cache"; |
hendrikvincent | 0:05ccbd4f84f1 | 68 | respHeaders()["Expires"]="0"; |
hendrikvincent | 0:05ccbd4f84f1 | 69 | |
hendrikvincent | 0:05ccbd4f84f1 | 70 | //Write data |
hendrikvincent | 0:05ccbd4f84f1 | 71 | respHeaders()["Connection"] = "close"; |
hendrikvincent | 0:05ccbd4f84f1 | 72 | writeData(resp, strlen(resp)); |
hendrikvincent | 0:05ccbd4f84f1 | 73 | DBG("\r\nExit RPCHandler::doGet()\r\n"); |
hendrikvincent | 0:05ccbd4f84f1 | 74 | } |
hendrikvincent | 0:05ccbd4f84f1 | 75 | |
hendrikvincent | 0:05ccbd4f84f1 | 76 | void RPCHandler::doPost() |
hendrikvincent | 0:05ccbd4f84f1 | 77 | { |
hendrikvincent | 0:05ccbd4f84f1 | 78 | |
hendrikvincent | 0:05ccbd4f84f1 | 79 | } |
hendrikvincent | 0:05ccbd4f84f1 | 80 | |
hendrikvincent | 0:05ccbd4f84f1 | 81 | void RPCHandler::doHead() |
hendrikvincent | 0:05ccbd4f84f1 | 82 | { |
hendrikvincent | 0:05ccbd4f84f1 | 83 | |
hendrikvincent | 0:05ccbd4f84f1 | 84 | } |
hendrikvincent | 0:05ccbd4f84f1 | 85 | |
hendrikvincent | 0:05ccbd4f84f1 | 86 | |
hendrikvincent | 0:05ccbd4f84f1 | 87 | void RPCHandler::onReadable() //Data has been read |
hendrikvincent | 0:05ccbd4f84f1 | 88 | { |
hendrikvincent | 0:05ccbd4f84f1 | 89 | |
hendrikvincent | 0:05ccbd4f84f1 | 90 | } |
hendrikvincent | 0:05ccbd4f84f1 | 91 | |
hendrikvincent | 0:05ccbd4f84f1 | 92 | void RPCHandler::onWriteable() //Data has been written & buf is free |
hendrikvincent | 0:05ccbd4f84f1 | 93 | { |
hendrikvincent | 0:05ccbd4f84f1 | 94 | DBG("\r\nRPCHandler::onWriteable() event\r\n"); |
hendrikvincent | 0:05ccbd4f84f1 | 95 | close(); //Data written, we can close the connection |
hendrikvincent | 0:05ccbd4f84f1 | 96 | } |
hendrikvincent | 0:05ccbd4f84f1 | 97 | |
hendrikvincent | 0:05ccbd4f84f1 | 98 | void RPCHandler::onClose() //Connection is closing |
hendrikvincent | 0:05ccbd4f84f1 | 99 | { |
hendrikvincent | 0:05ccbd4f84f1 | 100 | //Nothing to do |
hendrikvincent | 0:05ccbd4f84f1 | 101 | } |
hendrikvincent | 0:05ccbd4f84f1 | 102 | |
hendrikvincent | 0:05ccbd4f84f1 | 103 | void RPCHandler::cleanReq(char* data) |
hendrikvincent | 0:05ccbd4f84f1 | 104 | { |
hendrikvincent | 0:05ccbd4f84f1 | 105 | char* decoded = url_decode(data); |
hendrikvincent | 0:05ccbd4f84f1 | 106 | strcpy(data, decoded); |
hendrikvincent | 0:05ccbd4f84f1 | 107 | free(decoded); |
hendrikvincent | 0:05ccbd4f84f1 | 108 | /* |
hendrikvincent | 0:05ccbd4f84f1 | 109 | char* p; |
hendrikvincent | 0:05ccbd4f84f1 | 110 | static const char* lGarbage[2] = {"%20", "+"}; |
hendrikvincent | 0:05ccbd4f84f1 | 111 | for(int i = 0; i < 2; i++) |
hendrikvincent | 0:05ccbd4f84f1 | 112 | { |
hendrikvincent | 0:05ccbd4f84f1 | 113 | while( p = strstr(data, lGarbage[i]) ) |
hendrikvincent | 0:05ccbd4f84f1 | 114 | { |
hendrikvincent | 0:05ccbd4f84f1 | 115 | memset((void*) p, ' ', strlen(lGarbage[i])); |
hendrikvincent | 0:05ccbd4f84f1 | 116 | } |
hendrikvincent | 0:05ccbd4f84f1 | 117 | } |
hendrikvincent | 0:05ccbd4f84f1 | 118 | */ |
hendrikvincent | 0:05ccbd4f84f1 | 119 | } |
hendrikvincent | 0:05ccbd4f84f1 | 120 | |
hendrikvincent | 0:05ccbd4f84f1 | 121 |