Committer:
PA
Date:
Thu Jun 14 08:55:27 2012 +0000
Revision:
0:a7276b35b90b

        

Who changed what in which revision?

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