Capstone project for Bachelor's in Mechanical Engineering 2011

Dependencies:   FatFileSystem MAX3100 MODGPS MODSERIAL SDFileSystem mbed

Committer:
lhiggs
Date:
Wed May 29 00:45:41 2013 +0000
Revision:
0:0529d2d7762f
Broken, after updating all the libraries. RPC has issues with new mbed libraries.

Who changed what in which revision?

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