![](/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
RPCInterface/RPCFunction.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 | /** |
hendrikvincent | 0:05ccbd4f84f1 | 2 | * @section LICENSE |
hendrikvincent | 0:05ccbd4f84f1 | 3 | *Copyright (c) 2010 ARM Ltd. |
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 | * @section Description |
hendrikvincent | 0:05ccbd4f84f1 | 24 | *This class provides an object which can be called over RPC to run the function which is attached to it. |
hendrikvincent | 0:05ccbd4f84f1 | 25 | * |
hendrikvincent | 0:05ccbd4f84f1 | 26 | */ |
hendrikvincent | 0:05ccbd4f84f1 | 27 | #include "RPCFunction.h" |
hendrikvincent | 0:05ccbd4f84f1 | 28 | #include "rpc.h" |
hendrikvincent | 0:05ccbd4f84f1 | 29 | |
hendrikvincent | 0:05ccbd4f84f1 | 30 | //Parse a char argument without delimiting by anything that is non alphanumeric - based on version in rpc.h line 153 |
hendrikvincent | 0:05ccbd4f84f1 | 31 | char *parse_arg_char(const char *arg, const char **next) { |
hendrikvincent | 0:05ccbd4f84f1 | 32 | const char *ptr = arg; |
hendrikvincent | 0:05ccbd4f84f1 | 33 | char *res = NULL; |
hendrikvincent | 0:05ccbd4f84f1 | 34 | if(*arg == '"') { |
hendrikvincent | 0:05ccbd4f84f1 | 35 | /* quoted string */ |
hendrikvincent | 0:05ccbd4f84f1 | 36 | ptr = ++arg; |
hendrikvincent | 0:05ccbd4f84f1 | 37 | int len = 0; |
hendrikvincent | 0:05ccbd4f84f1 | 38 | /* find the end (and length) of the quoted string */ |
hendrikvincent | 0:05ccbd4f84f1 | 39 | for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) { |
hendrikvincent | 0:05ccbd4f84f1 | 40 | len++; |
hendrikvincent | 0:05ccbd4f84f1 | 41 | if(c == '\\') { |
hendrikvincent | 0:05ccbd4f84f1 | 42 | ptr++; |
hendrikvincent | 0:05ccbd4f84f1 | 43 | } |
hendrikvincent | 0:05ccbd4f84f1 | 44 | } |
hendrikvincent | 0:05ccbd4f84f1 | 45 | /* copy the quoted string, and unescape characters */ |
hendrikvincent | 0:05ccbd4f84f1 | 46 | if(len != 0) { |
hendrikvincent | 0:05ccbd4f84f1 | 47 | res = new char[len+1]; |
hendrikvincent | 0:05ccbd4f84f1 | 48 | char *resptr = res; |
hendrikvincent | 0:05ccbd4f84f1 | 49 | while(arg != ptr) { |
hendrikvincent | 0:05ccbd4f84f1 | 50 | *resptr++ = parse_char(arg, &arg); |
hendrikvincent | 0:05ccbd4f84f1 | 51 | } |
hendrikvincent | 0:05ccbd4f84f1 | 52 | *resptr = 0; |
hendrikvincent | 0:05ccbd4f84f1 | 53 | } |
hendrikvincent | 0:05ccbd4f84f1 | 54 | } else { |
hendrikvincent | 0:05ccbd4f84f1 | 55 | /* unquoted string */ |
hendrikvincent | 0:05ccbd4f84f1 | 56 | while(isalnum(*ptr) || isgraph(*ptr) || *ptr=='_' || *ptr == ' ') { //Edit this line to change which types of characters are allowed and which delimit |
hendrikvincent | 0:05ccbd4f84f1 | 57 | ptr++; |
hendrikvincent | 0:05ccbd4f84f1 | 58 | } |
hendrikvincent | 0:05ccbd4f84f1 | 59 | int len = ptr-arg; |
hendrikvincent | 0:05ccbd4f84f1 | 60 | if(len!=0) { //Chnages made to just pass whole string with no next arg or delimiters, these changes just removes space at the beginning |
hendrikvincent | 0:05ccbd4f84f1 | 61 | res = new char[len]; //was len+1 |
hendrikvincent | 0:05ccbd4f84f1 | 62 | memcpy(res, arg + 1, len - 1); // was arg, len |
hendrikvincent | 0:05ccbd4f84f1 | 63 | res[len-1] = 0; //was len |
hendrikvincent | 0:05ccbd4f84f1 | 64 | } |
hendrikvincent | 0:05ccbd4f84f1 | 65 | } |
hendrikvincent | 0:05ccbd4f84f1 | 66 | |
hendrikvincent | 0:05ccbd4f84f1 | 67 | if(next != NULL) { |
hendrikvincent | 0:05ccbd4f84f1 | 68 | *next = ptr; |
hendrikvincent | 0:05ccbd4f84f1 | 69 | } |
hendrikvincent | 0:05ccbd4f84f1 | 70 | return res; |
hendrikvincent | 0:05ccbd4f84f1 | 71 | } |
hendrikvincent | 0:05ccbd4f84f1 | 72 | |
hendrikvincent | 0:05ccbd4f84f1 | 73 | //Custom rpc method caller for execute so that the string will not be delimited by anything |
hendrikvincent | 0:05ccbd4f84f1 | 74 | //See line 436 of rpc.h |
hendrikvincent | 0:05ccbd4f84f1 | 75 | void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) { |
hendrikvincent | 0:05ccbd4f84f1 | 76 | |
hendrikvincent | 0:05ccbd4f84f1 | 77 | const char *next = arguments; |
hendrikvincent | 0:05ccbd4f84f1 | 78 | char* arg1 = parse_arg_char(next,NULL); |
hendrikvincent | 0:05ccbd4f84f1 | 79 | |
hendrikvincent | 0:05ccbd4f84f1 | 80 | char * res = (static_cast<RPCFunction*>(this_ptr)->run)(arg1); |
hendrikvincent | 0:05ccbd4f84f1 | 81 | if(result != NULL) { |
hendrikvincent | 0:05ccbd4f84f1 | 82 | write_result<char*>(res, result); |
hendrikvincent | 0:05ccbd4f84f1 | 83 | } |
hendrikvincent | 0:05ccbd4f84f1 | 84 | } |
hendrikvincent | 0:05ccbd4f84f1 | 85 | |
hendrikvincent | 0:05ccbd4f84f1 | 86 | RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){ |
hendrikvincent | 0:05ccbd4f84f1 | 87 | _ftr = f; |
hendrikvincent | 0:05ccbd4f84f1 | 88 | } |
hendrikvincent | 0:05ccbd4f84f1 | 89 | |
hendrikvincent | 0:05ccbd4f84f1 | 90 | |
hendrikvincent | 0:05ccbd4f84f1 | 91 | //Just run the attached function using the string thats in private memory - or just using null values, |
hendrikvincent | 0:05ccbd4f84f1 | 92 | char * RPCFunction::run(char * input){ |
hendrikvincent | 0:05ccbd4f84f1 | 93 | strcpy(_input, input); |
hendrikvincent | 0:05ccbd4f84f1 | 94 | (*_ftr)(_input,_output); |
hendrikvincent | 0:05ccbd4f84f1 | 95 | return(_output); |
hendrikvincent | 0:05ccbd4f84f1 | 96 | } |
hendrikvincent | 0:05ccbd4f84f1 | 97 | |
hendrikvincent | 0:05ccbd4f84f1 | 98 | //Just read the output string |
hendrikvincent | 0:05ccbd4f84f1 | 99 | char* RPCFunction::read(){ |
hendrikvincent | 0:05ccbd4f84f1 | 100 | return(_output); |
hendrikvincent | 0:05ccbd4f84f1 | 101 | } |
hendrikvincent | 0:05ccbd4f84f1 | 102 | |
hendrikvincent | 0:05ccbd4f84f1 | 103 | |
hendrikvincent | 0:05ccbd4f84f1 | 104 | #ifdef MBED_RPC |
hendrikvincent | 0:05ccbd4f84f1 | 105 | const rpc_method *RPCFunction::get_rpc_methods() { |
hendrikvincent | 0:05ccbd4f84f1 | 106 | static const rpc_method rpc_methods[] = { |
hendrikvincent | 0:05ccbd4f84f1 | 107 | { "run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string |
hendrikvincent | 0:05ccbd4f84f1 | 108 | { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> }, |
hendrikvincent | 0:05ccbd4f84f1 | 109 | RPC_METHOD_SUPER(Base) |
hendrikvincent | 0:05ccbd4f84f1 | 110 | }; |
hendrikvincent | 0:05ccbd4f84f1 | 111 | return rpc_methods; |
hendrikvincent | 0:05ccbd4f84f1 | 112 | } |
hendrikvincent | 0:05ccbd4f84f1 | 113 | |
hendrikvincent | 0:05ccbd4f84f1 | 114 | #endif |