Learning how to use RPC.

Committer:
MichaelW
Date:
Mon Sep 27 08:32:57 2010 +0000
Revision:
4:05f0d66bee57
Parent:
2:1d5b7485f683
Child:
7:a9e2c45097c8
Initial publish in new library format

Who changed what in which revision?

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