Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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