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