Peter Mertens
/
analogintest3
Analoog inlezen met pot 1 en 2 plus alarmen
RPCInterface/RPCFunction.cpp@0:e4782112c3fd, 2015-04-28 (annotated)
- Committer:
- u0068206
- Date:
- Tue Apr 28 11:41:25 2015 +0000
- Revision:
- 0:e4782112c3fd
Analoog in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
u0068206 | 0:e4782112c3fd | 1 | /** |
u0068206 | 0:e4782112c3fd | 2 | * @section LICENSE |
u0068206 | 0:e4782112c3fd | 3 | *Copyright (c) 2010 ARM Ltd. |
u0068206 | 0:e4782112c3fd | 4 | * |
u0068206 | 0:e4782112c3fd | 5 | *Permission is hereby granted, free of charge, to any person obtaining a copy |
u0068206 | 0:e4782112c3fd | 6 | *of this software and associated documentation files (the "Software"), to deal |
u0068206 | 0:e4782112c3fd | 7 | *in the Software without restriction, including without limitation the rights |
u0068206 | 0:e4782112c3fd | 8 | *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
u0068206 | 0:e4782112c3fd | 9 | *copies of the Software, and to permit persons to whom the Software is |
u0068206 | 0:e4782112c3fd | 10 | *furnished to do so, subject to the following conditions: |
u0068206 | 0:e4782112c3fd | 11 | * |
u0068206 | 0:e4782112c3fd | 12 | *The above copyright notice and this permission notice shall be included in |
u0068206 | 0:e4782112c3fd | 13 | *all copies or substantial portions of the Software. |
u0068206 | 0:e4782112c3fd | 14 | * |
u0068206 | 0:e4782112c3fd | 15 | *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
u0068206 | 0:e4782112c3fd | 16 | *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
u0068206 | 0:e4782112c3fd | 17 | *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
u0068206 | 0:e4782112c3fd | 18 | *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
u0068206 | 0:e4782112c3fd | 19 | *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
u0068206 | 0:e4782112c3fd | 20 | *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
u0068206 | 0:e4782112c3fd | 21 | *THE SOFTWARE. |
u0068206 | 0:e4782112c3fd | 22 | * |
u0068206 | 0:e4782112c3fd | 23 | * @section Description |
u0068206 | 0:e4782112c3fd | 24 | *This class provides an object which can be called over RPC to run the function which is attached to it. |
u0068206 | 0:e4782112c3fd | 25 | * |
u0068206 | 0:e4782112c3fd | 26 | */ |
u0068206 | 0:e4782112c3fd | 27 | #include "RPCFunction.h" |
u0068206 | 0:e4782112c3fd | 28 | #include "rpc.h" |
u0068206 | 0:e4782112c3fd | 29 | |
u0068206 | 0:e4782112c3fd | 30 | //Parse a char argument without delimiting by anything that is non alphanumeric - based on version in rpc.h line 153 |
u0068206 | 0:e4782112c3fd | 31 | char *parse_arg_char(const char *arg, const char **next) { |
u0068206 | 0:e4782112c3fd | 32 | const char *ptr = arg; |
u0068206 | 0:e4782112c3fd | 33 | char *res = NULL; |
u0068206 | 0:e4782112c3fd | 34 | if(*arg == '"') { |
u0068206 | 0:e4782112c3fd | 35 | /* quoted string */ |
u0068206 | 0:e4782112c3fd | 36 | ptr = ++arg; |
u0068206 | 0:e4782112c3fd | 37 | int len = 0; |
u0068206 | 0:e4782112c3fd | 38 | /* find the end (and length) of the quoted string */ |
u0068206 | 0:e4782112c3fd | 39 | for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) { |
u0068206 | 0:e4782112c3fd | 40 | len++; |
u0068206 | 0:e4782112c3fd | 41 | if(c == '\\') { |
u0068206 | 0:e4782112c3fd | 42 | ptr++; |
u0068206 | 0:e4782112c3fd | 43 | } |
u0068206 | 0:e4782112c3fd | 44 | } |
u0068206 | 0:e4782112c3fd | 45 | /* copy the quoted string, and unescape characters */ |
u0068206 | 0:e4782112c3fd | 46 | if(len != 0) { |
u0068206 | 0:e4782112c3fd | 47 | res = new char[len+1]; |
u0068206 | 0:e4782112c3fd | 48 | char *resptr = res; |
u0068206 | 0:e4782112c3fd | 49 | while(arg != ptr) { |
u0068206 | 0:e4782112c3fd | 50 | *resptr++ = parse_char(arg, &arg); |
u0068206 | 0:e4782112c3fd | 51 | } |
u0068206 | 0:e4782112c3fd | 52 | *resptr = 0; |
u0068206 | 0:e4782112c3fd | 53 | } |
u0068206 | 0:e4782112c3fd | 54 | } else { |
u0068206 | 0:e4782112c3fd | 55 | /* unquoted string */ |
u0068206 | 0:e4782112c3fd | 56 | while(isalnum(*ptr) || isgraph(*ptr) || *ptr=='_' || *ptr == ' ') { //Edit this line to change which types of characters are allowed and which delimit |
u0068206 | 0:e4782112c3fd | 57 | ptr++; |
u0068206 | 0:e4782112c3fd | 58 | } |
u0068206 | 0:e4782112c3fd | 59 | int len = ptr-arg; |
u0068206 | 0:e4782112c3fd | 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 |
u0068206 | 0:e4782112c3fd | 61 | res = new char[len]; //was len+1 |
u0068206 | 0:e4782112c3fd | 62 | memcpy(res, arg + 1, len - 1); // was arg, len |
u0068206 | 0:e4782112c3fd | 63 | res[len-1] = 0; //was len |
u0068206 | 0:e4782112c3fd | 64 | } |
u0068206 | 0:e4782112c3fd | 65 | } |
u0068206 | 0:e4782112c3fd | 66 | |
u0068206 | 0:e4782112c3fd | 67 | if(next != NULL) { |
u0068206 | 0:e4782112c3fd | 68 | *next = ptr; |
u0068206 | 0:e4782112c3fd | 69 | } |
u0068206 | 0:e4782112c3fd | 70 | return res; |
u0068206 | 0:e4782112c3fd | 71 | } |
u0068206 | 0:e4782112c3fd | 72 | |
u0068206 | 0:e4782112c3fd | 73 | //Custom rpc method caller for execute so that the string will not be delimited by anything |
u0068206 | 0:e4782112c3fd | 74 | //See line 436 of rpc.h |
u0068206 | 0:e4782112c3fd | 75 | void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) { |
u0068206 | 0:e4782112c3fd | 76 | |
u0068206 | 0:e4782112c3fd | 77 | const char *next = arguments; |
u0068206 | 0:e4782112c3fd | 78 | char* arg1 = parse_arg_char(next,NULL); |
u0068206 | 0:e4782112c3fd | 79 | |
u0068206 | 0:e4782112c3fd | 80 | char * res = (static_cast<RPCFunction*>(this_ptr)->run)(arg1); |
u0068206 | 0:e4782112c3fd | 81 | if(result != NULL) { |
u0068206 | 0:e4782112c3fd | 82 | write_result<char*>(res, result); |
u0068206 | 0:e4782112c3fd | 83 | } |
u0068206 | 0:e4782112c3fd | 84 | delete arg1; // Seems to stop a memory leak issue which prevented an RPCFunction being run more than ~500 times |
u0068206 | 0:e4782112c3fd | 85 | } |
u0068206 | 0:e4782112c3fd | 86 | |
u0068206 | 0:e4782112c3fd | 87 | RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){ |
u0068206 | 0:e4782112c3fd | 88 | _ftr = f; |
u0068206 | 0:e4782112c3fd | 89 | } |
u0068206 | 0:e4782112c3fd | 90 | |
u0068206 | 0:e4782112c3fd | 91 | |
u0068206 | 0:e4782112c3fd | 92 | //Just run the attached function using the string thats in private memory - or just using null values, |
u0068206 | 0:e4782112c3fd | 93 | char * RPCFunction::run(char * input){ |
u0068206 | 0:e4782112c3fd | 94 | strcpy(_input, input); |
u0068206 | 0:e4782112c3fd | 95 | (*_ftr)(_input,_output); |
u0068206 | 0:e4782112c3fd | 96 | return(_output); |
u0068206 | 0:e4782112c3fd | 97 | } |
u0068206 | 0:e4782112c3fd | 98 | |
u0068206 | 0:e4782112c3fd | 99 | //Just read the output string |
u0068206 | 0:e4782112c3fd | 100 | char* RPCFunction::read(){ |
u0068206 | 0:e4782112c3fd | 101 | return(_output); |
u0068206 | 0:e4782112c3fd | 102 | } |
u0068206 | 0:e4782112c3fd | 103 | |
u0068206 | 0:e4782112c3fd | 104 | |
u0068206 | 0:e4782112c3fd | 105 | #ifdef MBED_RPC |
u0068206 | 0:e4782112c3fd | 106 | const rpc_method *RPCFunction::get_rpc_methods() { |
u0068206 | 0:e4782112c3fd | 107 | static const rpc_method rpc_methods[] = { |
u0068206 | 0:e4782112c3fd | 108 | { "run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string |
u0068206 | 0:e4782112c3fd | 109 | { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> }, |
u0068206 | 0:e4782112c3fd | 110 | RPC_METHOD_SUPER(Base) |
u0068206 | 0:e4782112c3fd | 111 | }; |
u0068206 | 0:e4782112c3fd | 112 | return rpc_methods; |
u0068206 | 0:e4782112c3fd | 113 | } |
u0068206 | 0:e4782112c3fd | 114 | |
u0068206 | 0:e4782112c3fd | 115 | #endif |