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.
RPCFunction.cpp
00001 /** 00002 * @section LICENSE 00003 *Copyright (c) 2010 ARM Ltd. 00004 * 00005 *Permission is hereby granted, free of charge, to any person obtaining a copy 00006 *of this software and associated documentation files (the "Software"), to deal 00007 *in the Software without restriction, including without limitation the rights 00008 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 *copies of the Software, and to permit persons to whom the Software is 00010 *furnished to do so, subject to the following conditions: 00011 * 00012 *The above copyright notice and this permission notice shall be included in 00013 *all copies or substantial portions of the Software. 00014 * 00015 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 *THE SOFTWARE. 00022 * 00023 * @section Description 00024 *This class provides an object which can be called over RPC to run the function which is attached to it. 00025 * 00026 */ 00027 #include "RPCFunction.h" 00028 #include "rpc.h" 00029 00030 //Parse a char argument without delimiting by anything that is non alphanumeric - based on version in rpc.h line 153 00031 char *parse_arg_char(const char *arg, const char **next) { 00032 const char *ptr = arg; 00033 char *res = NULL; 00034 if(*arg == '"') { 00035 /* quoted string */ 00036 ptr = ++arg; 00037 int len = 0; 00038 /* find the end (and length) of the quoted string */ 00039 for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) { 00040 len++; 00041 if(c == '\\') { 00042 ptr++; 00043 } 00044 } 00045 /* copy the quoted string, and unescape characters */ 00046 if(len != 0) { 00047 res = new char[len+1]; 00048 char *resptr = res; 00049 while(arg != ptr) { 00050 *resptr++ = parse_char(arg, &arg); 00051 } 00052 *resptr = 0; 00053 } 00054 } else { 00055 /* unquoted string */ 00056 while(isalnum(*ptr) || isgraph(*ptr) || *ptr=='_' || *ptr == ' ') { //Edit this line to change which types of characters are allowed and which delimit 00057 ptr++; 00058 } 00059 int len = ptr-arg; 00060 if(len!=0) { //Chnages made to just pass whole string with no next arg or delimiters, these changes just removes space at the beginning 00061 res = new char[len]; //was len+1 00062 memcpy(res, arg + 1, len - 1); // was arg, len 00063 res[len-1] = 0; //was len 00064 } 00065 } 00066 00067 if(next != NULL) { 00068 *next = ptr; 00069 } 00070 return res; 00071 } 00072 00073 //Custom rpc method caller for execute so that the string will not be delimited by anything 00074 //See line 436 of rpc.h 00075 void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) { 00076 00077 const char *next = arguments; 00078 char* arg1 = parse_arg_char(next,NULL); 00079 00080 char * res = (static_cast<RPCFunction*>(this_ptr)->run)(arg1); 00081 if(result != NULL) { 00082 write_result<char*>(res, result); 00083 } 00084 delete arg1; // Seems to stop a memory leak issue //From Hexley Ball 00085 } 00086 00087 RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){ 00088 _ftr = f; 00089 } 00090 00091 00092 //Just run the attached function using the string thats in private memory - or just using null values, 00093 char * RPCFunction::run(char * input){ 00094 strcpy(_input, input); 00095 (*_ftr)(_input,_output); 00096 return(_output); 00097 } 00098 00099 //Just read the output string 00100 char* RPCFunction::read(){ 00101 return(_output); 00102 } 00103 00104 00105 #ifdef MBED_RPC 00106 const rpc_method *RPCFunction::get_rpc_methods() { 00107 static const rpc_method rpc_methods[] = { 00108 { "run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string 00109 { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> }, 00110 RPC_METHOD_SUPER(Base) 00111 }; 00112 return rpc_methods; 00113 } 00114 00115 #endif
Generated on Thu Jul 14 2022 02:09:49 by
