RPCInterface

Fork of RPCInterface by Michael Walker

Committer:
MichaelW
Date:
Sat Jan 28 19:12:00 2012 +0000
Revision:
8:682c65afe534
Parent:
7:a9e2c45097c8
Updated to not register AnalogOut class if the LPC11U24 as it doesn\t have AnalogOut

Who changed what in which revision?

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