Learning how to use RPC.

Committer:
MichaelW
Date:
Thu Sep 16 13:27:57 2010 +0000
Revision:
0:9232f9e1178d
Child:
1:67aefdc74b32
Changed to RPCFunction and RPCVariable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 0:9232f9e1178d 1 /**
MichaelW 0:9232f9e1178d 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 0:9232f9e1178d 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 0:9232f9e1178d 72 //Custom rpc method caller for run so that the string will not be delimted by anything, one argument only
MichaelW 0:9232f9e1178d 73 //based on code in rpc.h
MichaelW 0:9232f9e1178d 74 void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) {
MichaelW 0:9232f9e1178d 75 const char *next = arguments;
MichaelW 0:9232f9e1178d 76 char * arg1 = parse_arg_char(next,NULL);
MichaelW 0:9232f9e1178d 77
MichaelW 0:9232f9e1178d 78 (static_cast<RPCFunction*>(this_ptr)->run)(arg1);
MichaelW 0:9232f9e1178d 79
MichaelW 0:9232f9e1178d 80 if(result != NULL) {
MichaelW 0:9232f9e1178d 81 result[0] = '\0';
MichaelW 0:9232f9e1178d 82 }
MichaelW 0:9232f9e1178d 83 }
MichaelW 0:9232f9e1178d 84 //Custom rpc method caller for execute so that the string will not be delimited by anything
MichaelW 0:9232f9e1178d 85 //See line 436 of rpc.h
MichaelW 0:9232f9e1178d 86 void rpc_method_caller_execute(Base *this_ptr, const char *arguments, char *result) {
MichaelW 0:9232f9e1178d 87
MichaelW 0:9232f9e1178d 88 const char *next = arguments;
MichaelW 0:9232f9e1178d 89 char* arg1 = parse_arg_char(next,NULL);
MichaelW 0:9232f9e1178d 90
MichaelW 0:9232f9e1178d 91 char * res = (static_cast<RPCFunction*>(this_ptr)->execute)(arg1);
MichaelW 0:9232f9e1178d 92 if(result != NULL) {
MichaelW 0:9232f9e1178d 93 write_result<char*>(res, result);
MichaelW 0:9232f9e1178d 94 }
MichaelW 0:9232f9e1178d 95 }
MichaelW 0:9232f9e1178d 96
MichaelW 0:9232f9e1178d 97 RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){
MichaelW 0:9232f9e1178d 98 _ftr = f;
MichaelW 0:9232f9e1178d 99 }
MichaelW 0:9232f9e1178d 100
MichaelW 0:9232f9e1178d 101 //Function call which executes the packet
MichaelW 0:9232f9e1178d 102 char* RPCFunction::execute(char * input){
MichaelW 0:9232f9e1178d 103 strcpy(_input, input);
MichaelW 0:9232f9e1178d 104 (*_ftr)(_input,_output);
MichaelW 0:9232f9e1178d 105 return(_output);
MichaelW 0:9232f9e1178d 106 }
MichaelW 0:9232f9e1178d 107
MichaelW 0:9232f9e1178d 108 //Just run the attached function using the string thats in private memory - or just using null values,
MichaelW 0:9232f9e1178d 109 void RPCFunction::run(char * str){
MichaelW 0:9232f9e1178d 110 strcpy(_input, str);
MichaelW 0:9232f9e1178d 111 (*_ftr)(_input,_output);
MichaelW 0:9232f9e1178d 112 }
MichaelW 0:9232f9e1178d 113
MichaelW 0:9232f9e1178d 114 //Just read the output string
MichaelW 0:9232f9e1178d 115 char* RPCFunction::read(){
MichaelW 0:9232f9e1178d 116 return(_output);
MichaelW 0:9232f9e1178d 117 }
MichaelW 0:9232f9e1178d 118 //Just set the input string
MichaelW 0:9232f9e1178d 119 void RPCFunction::write(char * str){
MichaelW 0:9232f9e1178d 120 strcpy(_input, str);
MichaelW 0:9232f9e1178d 121 }
MichaelW 0:9232f9e1178d 122
MichaelW 0:9232f9e1178d 123
MichaelW 0:9232f9e1178d 124 #ifdef MBED_RPC
MichaelW 0:9232f9e1178d 125 const rpc_method *RPCFunction::get_rpc_methods() {
MichaelW 0:9232f9e1178d 126 static const rpc_method rpc_methods[] = {
MichaelW 0:9232f9e1178d 127 {"execute", rpc_method_caller_execute }, //Run using custom caller, all character accepted in string
MichaelW 0:9232f9e1178d 128 { "run", rpc_method_caller_run }, //Run using custom caller, all character accepted in string
MichaelW 0:9232f9e1178d 129 { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> },
MichaelW 0:9232f9e1178d 130 { "write", rpc_method_caller<RPCFunction, char*, &RPCFunction::write> },
MichaelW 0:9232f9e1178d 131 RPC_METHOD_SUPER(Base)
MichaelW 0:9232f9e1178d 132 };
MichaelW 0:9232f9e1178d 133 return rpc_methods;
MichaelW 0:9232f9e1178d 134 }
MichaelW 0:9232f9e1178d 135 //creating a new one has very little meaning over RPC
MichaelW 0:9232f9e1178d 136 rpc_class *RPCFunction::get_rpc_class() {
MichaelW 0:9232f9e1178d 137 static const rpc_function funcs[] = {
MichaelW 0:9232f9e1178d 138 /*"new", rpc_function_caller<const char*, void(*f)(char*, char*), const char*, &Base::construct<Packet,void(*f)(char*, char*),const char*> >,*/
MichaelW 0:9232f9e1178d 139 RPC_METHOD_END
MichaelW 0:9232f9e1178d 140 };
MichaelW 0:9232f9e1178d 141 static rpc_class c = { "RPCFunction", funcs, NULL };
MichaelW 0:9232f9e1178d 142 return &c;
MichaelW 0:9232f9e1178d 143 }
MichaelW 0:9232f9e1178d 144 #endif
MichaelW 0:9232f9e1178d 145