Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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