Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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