Accepts RPC commands over bluetooth (RN42)

Dependencies:   mbed m3pi

Committer:
chris
Date:
Thu May 12 15:46:41 2011 +0000
Revision:
3:be428c849e1c

        

Who changed what in which revision?

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