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