Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format

Dependents:   NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more

Fork of mbed by mbed official

Committer:
simon.ford@mbed.co.uk
Date:
Fri Jan 23 14:15:56 2009 +0000
Revision:
6:3fd6a337c7cc
Parent:
5:62573be585e9
Child:
8:00a04e5cd407
Improved RPC handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon.ford@mbed.co.uk 4:5d1359a283bc 1 /* mbed Microcontroller Library
simon.ford@mbed.co.uk 4:5d1359a283bc 2 * Copyright (c) 2008 ARM Limited. All rights reserved.
simon.ford@mbed.co.uk 4:5d1359a283bc 3 */
simon.ford@mbed.co.uk 4:5d1359a283bc 4
simon.ford@mbed.co.uk 4:5d1359a283bc 5 #ifndef MBED_RPC_H
simon.ford@mbed.co.uk 4:5d1359a283bc 6 #define MBED_RPC_H
simon.ford@mbed.co.uk 4:5d1359a283bc 7
simon.ford@mbed.co.uk 4:5d1359a283bc 8 /* Section rpc
simon.ford@mbed.co.uk 4:5d1359a283bc 9 * Helpers for rpc handling.
simon.ford@mbed.co.uk 4:5d1359a283bc 10 */
simon.ford@mbed.co.uk 4:5d1359a283bc 11
simon.ford@mbed.co.uk 4:5d1359a283bc 12 #include <stdlib.h>
simon.ford@mbed.co.uk 4:5d1359a283bc 13 #include <stdio.h>
simon.ford@mbed.co.uk 4:5d1359a283bc 14 #include <string.h>
simon.ford@mbed.co.uk 4:5d1359a283bc 15 #include "Base.h"
simon.ford@mbed.co.uk 4:5d1359a283bc 16
simon.ford@mbed.co.uk 4:5d1359a283bc 17 namespace mbed {
simon.ford@mbed.co.uk 4:5d1359a283bc 18
simon.ford@mbed.co.uk 4:5d1359a283bc 19 /* Function parse_arg
simon.ford@mbed.co.uk 4:5d1359a283bc 20 * Parses and returns a value from a string.
simon.ford@mbed.co.uk 4:5d1359a283bc 21 *
simon.ford@mbed.co.uk 4:5d1359a283bc 22 * Variable
simon.ford@mbed.co.uk 4:5d1359a283bc 23 * arg - The string to pase
simon.ford@mbed.co.uk 4:5d1359a283bc 24 * next - If not NULL a pointer to after the last
simon.ford@mbed.co.uk 4:5d1359a283bc 25 * character parsed is written here
simon.ford@mbed.co.uk 4:5d1359a283bc 26 */
simon.ford@mbed.co.uk 4:5d1359a283bc 27 template<typename T> T parse_arg(const char *arg, const char **next);
simon.ford@mbed.co.uk 4:5d1359a283bc 28
simon.ford@mbed.co.uk 4:5d1359a283bc 29 /* signed integer types */
simon.ford@mbed.co.uk 4:5d1359a283bc 30
simon.ford@mbed.co.uk 4:5d1359a283bc 31 template<> inline char parse_arg<char>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 32 if(next != NULL) *next = arg+1;
simon.ford@mbed.co.uk 4:5d1359a283bc 33 return *arg;
simon.ford@mbed.co.uk 4:5d1359a283bc 34 }
simon.ford@mbed.co.uk 4:5d1359a283bc 35
simon.ford@mbed.co.uk 4:5d1359a283bc 36 template<> inline short int parse_arg<short int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 37 return strtol(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 38 }
simon.ford@mbed.co.uk 4:5d1359a283bc 39
simon.ford@mbed.co.uk 4:5d1359a283bc 40 template<> inline long int parse_arg<long int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 41 return strtol(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 42 }
simon.ford@mbed.co.uk 4:5d1359a283bc 43
simon.ford@mbed.co.uk 4:5d1359a283bc 44 template<> inline int parse_arg<int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 45 return strtol(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 46 }
simon.ford@mbed.co.uk 4:5d1359a283bc 47
simon.ford@mbed.co.uk 4:5d1359a283bc 48 template<> inline long long parse_arg<long long>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 49 return strtoll(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 50 }
simon.ford@mbed.co.uk 4:5d1359a283bc 51
simon.ford@mbed.co.uk 4:5d1359a283bc 52 /* unsigned integer types */
simon.ford@mbed.co.uk 4:5d1359a283bc 53
simon.ford@mbed.co.uk 4:5d1359a283bc 54 template<> inline unsigned char parse_arg<unsigned char>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 55 if(next != NULL) *next = arg+1;
simon.ford@mbed.co.uk 4:5d1359a283bc 56 return *arg;
simon.ford@mbed.co.uk 4:5d1359a283bc 57 }
simon.ford@mbed.co.uk 4:5d1359a283bc 58
simon.ford@mbed.co.uk 4:5d1359a283bc 59 template<> inline unsigned short int parse_arg<unsigned short int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 60 return strtoul(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 61 }
simon.ford@mbed.co.uk 4:5d1359a283bc 62
simon.ford@mbed.co.uk 4:5d1359a283bc 63 template<> inline unsigned long int parse_arg<unsigned long int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 64 return strtoul(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 65 }
simon.ford@mbed.co.uk 4:5d1359a283bc 66
simon.ford@mbed.co.uk 4:5d1359a283bc 67 template<> inline unsigned int parse_arg<unsigned int>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 68 return strtoul(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 69 }
simon.ford@mbed.co.uk 4:5d1359a283bc 70
simon.ford@mbed.co.uk 4:5d1359a283bc 71 template<> inline unsigned long long parse_arg<unsigned long long>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 72 return strtoull(arg, const_cast<char**>(next), 10);
simon.ford@mbed.co.uk 4:5d1359a283bc 73 }
simon.ford@mbed.co.uk 4:5d1359a283bc 74
simon.ford@mbed.co.uk 4:5d1359a283bc 75 /* floating types */
simon.ford@mbed.co.uk 4:5d1359a283bc 76
simon.ford@mbed.co.uk 4:5d1359a283bc 77 template<> inline float parse_arg<float>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 5:62573be585e9 78 #if !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 410000
simon.ford@mbed.co.uk 4:5d1359a283bc 79 return strtof(arg,const_cast<char**>(next));
simon.ford@mbed.co.uk 5:62573be585e9 80 #elif __ARMCC_VERSION >= 310000
simon.ford@mbed.co.uk 5:62573be585e9 81 /* bug in header means no using declaration for strtof */
simon.ford@mbed.co.uk 5:62573be585e9 82 return std::strtof(arg,const_cast<char**>(next));
simon.ford@mbed.co.uk 4:5d1359a283bc 83 #else
simon.ford@mbed.co.uk 5:62573be585e9 84 /* strtof not supported */
simon.ford@mbed.co.uk 4:5d1359a283bc 85 return strtod(arg,const_cast<char**>(next));
simon.ford@mbed.co.uk 4:5d1359a283bc 86 #endif
simon.ford@mbed.co.uk 4:5d1359a283bc 87 }
simon.ford@mbed.co.uk 4:5d1359a283bc 88
simon.ford@mbed.co.uk 4:5d1359a283bc 89 template<> inline double parse_arg<double>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 90 return strtod(arg,const_cast<char**>(next));
simon.ford@mbed.co.uk 4:5d1359a283bc 91 }
simon.ford@mbed.co.uk 4:5d1359a283bc 92
simon.ford@mbed.co.uk 4:5d1359a283bc 93 template<> inline long double parse_arg<long double>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 4:5d1359a283bc 94 return strtod(arg,const_cast<char**>(next));
simon.ford@mbed.co.uk 4:5d1359a283bc 95 }
simon.ford@mbed.co.uk 4:5d1359a283bc 96
simon.ford@mbed.co.uk 5:62573be585e9 97 /* string */
simon.ford@mbed.co.uk 5:62573be585e9 98
simon.ford@mbed.co.uk 5:62573be585e9 99 template<> inline char *parse_arg<char*>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 5:62573be585e9 100 const char *ptr = arg;
simon.ford@mbed.co.uk 6:3fd6a337c7cc 101 while(*ptr >= '!' && *ptr != ',') {
simon.ford@mbed.co.uk 5:62573be585e9 102 ptr++;
simon.ford@mbed.co.uk 5:62573be585e9 103 }
simon.ford@mbed.co.uk 5:62573be585e9 104 int len = ptr-arg;
simon.ford@mbed.co.uk 5:62573be585e9 105 char *p;
simon.ford@mbed.co.uk 5:62573be585e9 106 if(len==0) {
simon.ford@mbed.co.uk 5:62573be585e9 107 p = NULL;
simon.ford@mbed.co.uk 5:62573be585e9 108 } else {
simon.ford@mbed.co.uk 5:62573be585e9 109 p = new char[len+1];
simon.ford@mbed.co.uk 5:62573be585e9 110 memcpy(p, arg, len);
simon.ford@mbed.co.uk 5:62573be585e9 111 p[len] = 0;
simon.ford@mbed.co.uk 5:62573be585e9 112 }
simon.ford@mbed.co.uk 5:62573be585e9 113 if(next != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 114 *next = ptr;
simon.ford@mbed.co.uk 5:62573be585e9 115 }
simon.ford@mbed.co.uk 5:62573be585e9 116 return p;
simon.ford@mbed.co.uk 5:62573be585e9 117 }
simon.ford@mbed.co.uk 5:62573be585e9 118
simon.ford@mbed.co.uk 5:62573be585e9 119 template<> inline const char *parse_arg<const char*>(const char *arg, const char **next) {
simon.ford@mbed.co.uk 5:62573be585e9 120 return parse_arg<char*>(arg,next);
simon.ford@mbed.co.uk 5:62573be585e9 121 }
simon.ford@mbed.co.uk 5:62573be585e9 122
simon.ford@mbed.co.uk 4:5d1359a283bc 123
simon.ford@mbed.co.uk 4:5d1359a283bc 124 /* Function write_result
simon.ford@mbed.co.uk 4:5d1359a283bc 125 * Writes a value in to a result string in an appropriate manner
simon.ford@mbed.co.uk 4:5d1359a283bc 126 *
simon.ford@mbed.co.uk 4:5d1359a283bc 127 * Variable
simon.ford@mbed.co.uk 4:5d1359a283bc 128 * val - The value to write
simon.ford@mbed.co.uk 4:5d1359a283bc 129 * result - A pointer to the array to write the value into
simon.ford@mbed.co.uk 4:5d1359a283bc 130 */
simon.ford@mbed.co.uk 4:5d1359a283bc 131 template<typename T> void write_result(T val, char *result);
simon.ford@mbed.co.uk 4:5d1359a283bc 132
simon.ford@mbed.co.uk 4:5d1359a283bc 133 /* signed integer types */
simon.ford@mbed.co.uk 4:5d1359a283bc 134
simon.ford@mbed.co.uk 4:5d1359a283bc 135 template<> inline void write_result<char>(char val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 136 result[0] = val;
simon.ford@mbed.co.uk 4:5d1359a283bc 137 result[1] = '\0';
simon.ford@mbed.co.uk 4:5d1359a283bc 138 }
simon.ford@mbed.co.uk 4:5d1359a283bc 139
simon.ford@mbed.co.uk 4:5d1359a283bc 140 template<> inline void write_result<short int>(short int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 141 sprintf(result, "%hi", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 142 }
simon.ford@mbed.co.uk 4:5d1359a283bc 143
simon.ford@mbed.co.uk 4:5d1359a283bc 144 template<> inline void write_result<int>(int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 145 sprintf(result, "%i", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 146 }
simon.ford@mbed.co.uk 4:5d1359a283bc 147
simon.ford@mbed.co.uk 4:5d1359a283bc 148 template<> inline void write_result<long int>(long int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 149 sprintf(result, "%li", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 150 }
simon.ford@mbed.co.uk 4:5d1359a283bc 151
simon.ford@mbed.co.uk 4:5d1359a283bc 152 template<> inline void write_result<long long int>(long long int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 153 sprintf(result, "%lli", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 154 }
simon.ford@mbed.co.uk 4:5d1359a283bc 155
simon.ford@mbed.co.uk 4:5d1359a283bc 156 /* unsigned integer types */
simon.ford@mbed.co.uk 4:5d1359a283bc 157
simon.ford@mbed.co.uk 4:5d1359a283bc 158 template<> inline void write_result<unsigned char>(unsigned char val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 159 result[0] = val;
simon.ford@mbed.co.uk 4:5d1359a283bc 160 result[1] = '\0';
simon.ford@mbed.co.uk 4:5d1359a283bc 161 }
simon.ford@mbed.co.uk 4:5d1359a283bc 162
simon.ford@mbed.co.uk 4:5d1359a283bc 163 template<> inline void write_result<unsigned short int>(unsigned short int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 164 sprintf(result, "%hu", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 165 }
simon.ford@mbed.co.uk 4:5d1359a283bc 166
simon.ford@mbed.co.uk 4:5d1359a283bc 167 template<> inline void write_result<unsigned int>(unsigned int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 168 sprintf(result, "%u", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 169 }
simon.ford@mbed.co.uk 4:5d1359a283bc 170
simon.ford@mbed.co.uk 4:5d1359a283bc 171 template<> inline void write_result<unsigned long int>(unsigned long int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 172 sprintf(result, "%lu", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 173 }
simon.ford@mbed.co.uk 4:5d1359a283bc 174
simon.ford@mbed.co.uk 4:5d1359a283bc 175 template<> inline void write_result<unsigned long long int>(unsigned long long int val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 176 sprintf(result, "%llu", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 177 }
simon.ford@mbed.co.uk 4:5d1359a283bc 178
simon.ford@mbed.co.uk 4:5d1359a283bc 179 /* floating types */
simon.ford@mbed.co.uk 4:5d1359a283bc 180
simon.ford@mbed.co.uk 4:5d1359a283bc 181 template<> inline void write_result<float>(float val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 182 sprintf(result, "%g", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 183 }
simon.ford@mbed.co.uk 4:5d1359a283bc 184
simon.ford@mbed.co.uk 4:5d1359a283bc 185 template<> inline void write_result<double>(double val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 186 sprintf(result, "%g", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 187 }
simon.ford@mbed.co.uk 4:5d1359a283bc 188
simon.ford@mbed.co.uk 4:5d1359a283bc 189 template<> inline void write_result<long double>(long double val, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 190 sprintf(result, "%Lg", val);
simon.ford@mbed.co.uk 4:5d1359a283bc 191 }
simon.ford@mbed.co.uk 4:5d1359a283bc 192
simon.ford@mbed.co.uk 4:5d1359a283bc 193
simon.ford@mbed.co.uk 5:62573be585e9 194 /* string */
simon.ford@mbed.co.uk 5:62573be585e9 195
simon.ford@mbed.co.uk 5:62573be585e9 196 template<> inline void write_result<char*>(char *val, char *result) {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 197 if(val==NULL) {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 198 result[0] = 0;
simon.ford@mbed.co.uk 6:3fd6a337c7cc 199 } else {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 200 strcpy(result, val);
simon.ford@mbed.co.uk 6:3fd6a337c7cc 201 }
simon.ford@mbed.co.uk 5:62573be585e9 202 }
simon.ford@mbed.co.uk 5:62573be585e9 203
simon.ford@mbed.co.uk 5:62573be585e9 204 template<> inline void write_result<const char*>(const char *val, char *result) {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 205 if(val==NULL) {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 206 result[0] = 0;
simon.ford@mbed.co.uk 6:3fd6a337c7cc 207 } else {
simon.ford@mbed.co.uk 6:3fd6a337c7cc 208 strcpy(result, val);
simon.ford@mbed.co.uk 6:3fd6a337c7cc 209 }
simon.ford@mbed.co.uk 5:62573be585e9 210 }
simon.ford@mbed.co.uk 5:62573be585e9 211
simon.ford@mbed.co.uk 5:62573be585e9 212
simon.ford@mbed.co.uk 5:62573be585e9 213 inline const char *next_arg(const char* next) {
simon.ford@mbed.co.uk 5:62573be585e9 214 if(*next == ',' || *next == ' ') next++;
simon.ford@mbed.co.uk 5:62573be585e9 215 return next;
simon.ford@mbed.co.uk 5:62573be585e9 216 }
simon.ford@mbed.co.uk 5:62573be585e9 217
simon.ford@mbed.co.uk 5:62573be585e9 218
simon.ford@mbed.co.uk 5:62573be585e9 219 /* Function rpc_method_caller
simon.ford@mbed.co.uk 5:62573be585e9 220 */
simon.ford@mbed.co.uk 5:62573be585e9 221 template<class T, void (T::*member)(const char *,char *)>
simon.ford@mbed.co.uk 5:62573be585e9 222 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 223 (static_cast<T*>(this_ptr)->*member)(arguments,result);
simon.ford@mbed.co.uk 5:62573be585e9 224 }
simon.ford@mbed.co.uk 5:62573be585e9 225
simon.ford@mbed.co.uk 5:62573be585e9 226
simon.ford@mbed.co.uk 5:62573be585e9 227 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 228 */
simon.ford@mbed.co.uk 4:5d1359a283bc 229 template<class T, void (T::*member)()>
simon.ford@mbed.co.uk 5:62573be585e9 230 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 231 (static_cast<T*>(this_ptr)->*member)();
simon.ford@mbed.co.uk 4:5d1359a283bc 232 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 233 result[0] = '\0';
simon.ford@mbed.co.uk 4:5d1359a283bc 234 }
simon.ford@mbed.co.uk 4:5d1359a283bc 235 }
simon.ford@mbed.co.uk 4:5d1359a283bc 236
simon.ford@mbed.co.uk 4:5d1359a283bc 237
simon.ford@mbed.co.uk 5:62573be585e9 238 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 239 */
simon.ford@mbed.co.uk 4:5d1359a283bc 240 template<class T, typename A1, void (T::*member)(A1)>
simon.ford@mbed.co.uk 5:62573be585e9 241 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 242
simon.ford@mbed.co.uk 5:62573be585e9 243 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 244 A1 arg1 = parse_arg<A1>(next_arg(next),NULL);
simon.ford@mbed.co.uk 4:5d1359a283bc 245
simon.ford@mbed.co.uk 4:5d1359a283bc 246 (static_cast<T*>(this_ptr)->*member)(arg1);
simon.ford@mbed.co.uk 4:5d1359a283bc 247 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 248 result[0] = '\0';
simon.ford@mbed.co.uk 4:5d1359a283bc 249 }
simon.ford@mbed.co.uk 4:5d1359a283bc 250 }
simon.ford@mbed.co.uk 4:5d1359a283bc 251
simon.ford@mbed.co.uk 4:5d1359a283bc 252
simon.ford@mbed.co.uk 5:62573be585e9 253 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 254 */
simon.ford@mbed.co.uk 4:5d1359a283bc 255 template<class T, typename A1, typename A2, void (T::*member)(A1,A2)>
simon.ford@mbed.co.uk 5:62573be585e9 256 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 257
simon.ford@mbed.co.uk 5:62573be585e9 258 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 259 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 260 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
simon.ford@mbed.co.uk 4:5d1359a283bc 261
simon.ford@mbed.co.uk 4:5d1359a283bc 262 (static_cast<T*>(this_ptr)->*member)(arg1,arg2);
simon.ford@mbed.co.uk 4:5d1359a283bc 263 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 264 result[0] = '\0';
simon.ford@mbed.co.uk 4:5d1359a283bc 265 }
simon.ford@mbed.co.uk 4:5d1359a283bc 266 }
simon.ford@mbed.co.uk 4:5d1359a283bc 267
simon.ford@mbed.co.uk 4:5d1359a283bc 268
simon.ford@mbed.co.uk 5:62573be585e9 269 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 270 */
simon.ford@mbed.co.uk 4:5d1359a283bc 271 template<typename R, class T, R (T::*member)()>
simon.ford@mbed.co.uk 5:62573be585e9 272 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 273 R res = (static_cast<T*>(this_ptr)->*member)();
simon.ford@mbed.co.uk 4:5d1359a283bc 274 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 275 write_result<R>(res, result);
simon.ford@mbed.co.uk 4:5d1359a283bc 276 }
simon.ford@mbed.co.uk 4:5d1359a283bc 277 }
simon.ford@mbed.co.uk 4:5d1359a283bc 278
simon.ford@mbed.co.uk 4:5d1359a283bc 279
simon.ford@mbed.co.uk 5:62573be585e9 280 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 281 */
simon.ford@mbed.co.uk 4:5d1359a283bc 282 template<typename R, class T, typename A1, R (T::*member)(A1)>
simon.ford@mbed.co.uk 5:62573be585e9 283 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 284
simon.ford@mbed.co.uk 5:62573be585e9 285 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 286 A1 arg1 = parse_arg<A1>(next_arg(next),NULL);
simon.ford@mbed.co.uk 4:5d1359a283bc 287
simon.ford@mbed.co.uk 4:5d1359a283bc 288 R res = (static_cast<T*>(this_ptr)->*member)(arg1);
simon.ford@mbed.co.uk 4:5d1359a283bc 289 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 290 write_result<R>(res, result);
simon.ford@mbed.co.uk 4:5d1359a283bc 291 }
simon.ford@mbed.co.uk 4:5d1359a283bc 292 }
simon.ford@mbed.co.uk 4:5d1359a283bc 293
simon.ford@mbed.co.uk 4:5d1359a283bc 294
simon.ford@mbed.co.uk 5:62573be585e9 295 /* Function rpc_method_caller
simon.ford@mbed.co.uk 4:5d1359a283bc 296 */
simon.ford@mbed.co.uk 4:5d1359a283bc 297 template<typename R, class T, typename A1, typename A2, R (T::*member)(A1,A2)>
simon.ford@mbed.co.uk 5:62573be585e9 298 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 4:5d1359a283bc 299
simon.ford@mbed.co.uk 5:62573be585e9 300 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 301 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 302 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
simon.ford@mbed.co.uk 4:5d1359a283bc 303
simon.ford@mbed.co.uk 4:5d1359a283bc 304 R res = (static_cast<T*>(this_ptr)->*member)(arg1,arg2);
simon.ford@mbed.co.uk 4:5d1359a283bc 305 if(result != NULL) {
simon.ford@mbed.co.uk 4:5d1359a283bc 306 write_result<R>(res, result);
simon.ford@mbed.co.uk 4:5d1359a283bc 307 }
simon.ford@mbed.co.uk 4:5d1359a283bc 308 }
simon.ford@mbed.co.uk 4:5d1359a283bc 309
simon.ford@mbed.co.uk 5:62573be585e9 310
simon.ford@mbed.co.uk 5:62573be585e9 311 /* Function rpc_method_caller
simon.ford@mbed.co.uk 5:62573be585e9 312 */
simon.ford@mbed.co.uk 5:62573be585e9 313 template<typename R, class T, typename A1, typename A2, typename A3, R (T::*member)(A1,A2,A3)>
simon.ford@mbed.co.uk 5:62573be585e9 314 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 315
simon.ford@mbed.co.uk 5:62573be585e9 316 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 317 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 318 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 319 A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
simon.ford@mbed.co.uk 5:62573be585e9 320
simon.ford@mbed.co.uk 5:62573be585e9 321 R res = (static_cast<T*>(this_ptr)->*member)(arg1,arg2,arg3);
simon.ford@mbed.co.uk 5:62573be585e9 322 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 323 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 324 }
simon.ford@mbed.co.uk 5:62573be585e9 325 }
simon.ford@mbed.co.uk 5:62573be585e9 326
simon.ford@mbed.co.uk 5:62573be585e9 327
simon.ford@mbed.co.uk 5:62573be585e9 328 /* Function rpc_function caller
simon.ford@mbed.co.uk 5:62573be585e9 329 */
simon.ford@mbed.co.uk 5:62573be585e9 330 template<typename R, R (*func)()>
simon.ford@mbed.co.uk 5:62573be585e9 331 void rpc_function_caller(const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 332 R res = (*func)();
simon.ford@mbed.co.uk 5:62573be585e9 333 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 334 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 335 }
simon.ford@mbed.co.uk 5:62573be585e9 336 }
simon.ford@mbed.co.uk 5:62573be585e9 337
simon.ford@mbed.co.uk 5:62573be585e9 338
simon.ford@mbed.co.uk 5:62573be585e9 339 /* Function rpc_function caller
simon.ford@mbed.co.uk 5:62573be585e9 340 */
simon.ford@mbed.co.uk 5:62573be585e9 341 template<typename R, typename A1, R (*func)(A1)>
simon.ford@mbed.co.uk 5:62573be585e9 342 void rpc_function_caller(const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 343 A1 arg1 = parse_arg<A1>(next_arg(arguments),NULL);
simon.ford@mbed.co.uk 5:62573be585e9 344 R res = (*func)(arg1);
simon.ford@mbed.co.uk 5:62573be585e9 345 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 346 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 347 }
simon.ford@mbed.co.uk 5:62573be585e9 348 }
simon.ford@mbed.co.uk 5:62573be585e9 349
simon.ford@mbed.co.uk 5:62573be585e9 350
simon.ford@mbed.co.uk 5:62573be585e9 351 /* Function rpc_function caller
simon.ford@mbed.co.uk 5:62573be585e9 352 */
simon.ford@mbed.co.uk 5:62573be585e9 353 template<typename R, typename A1, typename A2, R (*func)(A1,A2)>
simon.ford@mbed.co.uk 5:62573be585e9 354 void rpc_function_caller(const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 355
simon.ford@mbed.co.uk 5:62573be585e9 356 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 357 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 358 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
simon.ford@mbed.co.uk 5:62573be585e9 359
simon.ford@mbed.co.uk 5:62573be585e9 360 R res = (*func)(arg1,arg2);
simon.ford@mbed.co.uk 5:62573be585e9 361 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 362 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 363 }
simon.ford@mbed.co.uk 5:62573be585e9 364 }
simon.ford@mbed.co.uk 5:62573be585e9 365
simon.ford@mbed.co.uk 5:62573be585e9 366
simon.ford@mbed.co.uk 5:62573be585e9 367 /* Function rpc_function caller
simon.ford@mbed.co.uk 5:62573be585e9 368 */
simon.ford@mbed.co.uk 5:62573be585e9 369 template<typename R, typename A1, typename A2, typename A3, R (*func)(A1,A2,A3)>
simon.ford@mbed.co.uk 5:62573be585e9 370 void rpc_function_caller(const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 371
simon.ford@mbed.co.uk 5:62573be585e9 372 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 373 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 374 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 375 A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
simon.ford@mbed.co.uk 5:62573be585e9 376
simon.ford@mbed.co.uk 5:62573be585e9 377 R res = (*func)(arg1,arg2,arg3);
simon.ford@mbed.co.uk 5:62573be585e9 378 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 379 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 380 }
simon.ford@mbed.co.uk 5:62573be585e9 381 }
simon.ford@mbed.co.uk 5:62573be585e9 382
simon.ford@mbed.co.uk 5:62573be585e9 383
simon.ford@mbed.co.uk 5:62573be585e9 384 /* Function rpc_function caller
simon.ford@mbed.co.uk 5:62573be585e9 385 */
simon.ford@mbed.co.uk 5:62573be585e9 386 template<typename R, typename A1, typename A2, typename A3, typename A4, R (*func)(A1,A2,A3,A4)>
simon.ford@mbed.co.uk 5:62573be585e9 387 void rpc_function_caller(const char *arguments, char *result) {
simon.ford@mbed.co.uk 5:62573be585e9 388
simon.ford@mbed.co.uk 5:62573be585e9 389 const char *next = arguments;
simon.ford@mbed.co.uk 5:62573be585e9 390 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 391 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 392 A3 arg3 = parse_arg<A3>(next_arg(next),&next);
simon.ford@mbed.co.uk 5:62573be585e9 393 A4 arg4 = parse_arg<A4>(next_arg(next),NULL);
simon.ford@mbed.co.uk 5:62573be585e9 394
simon.ford@mbed.co.uk 5:62573be585e9 395 R res = (*func)(arg1,arg2,arg3,arg4);
simon.ford@mbed.co.uk 5:62573be585e9 396 if(result != NULL) {
simon.ford@mbed.co.uk 5:62573be585e9 397 write_result<R>(res, result);
simon.ford@mbed.co.uk 5:62573be585e9 398 }
simon.ford@mbed.co.uk 5:62573be585e9 399 }
simon.ford@mbed.co.uk 5:62573be585e9 400
simon.ford@mbed.co.uk 5:62573be585e9 401
simon.ford@mbed.co.uk 4:5d1359a283bc 402 struct rpc_method {
simon.ford@mbed.co.uk 4:5d1359a283bc 403 const char *name;
simon.ford@mbed.co.uk 5:62573be585e9 404 typedef void (*caller_t)(Base*, const char*, char*);
simon.ford@mbed.co.uk 5:62573be585e9 405 typedef const struct rpc_method *(*super_t)(Base*);
simon.ford@mbed.co.uk 5:62573be585e9 406 union {
simon.ford@mbed.co.uk 5:62573be585e9 407 caller_t caller;
simon.ford@mbed.co.uk 5:62573be585e9 408 super_t super;
simon.ford@mbed.co.uk 5:62573be585e9 409 };
simon.ford@mbed.co.uk 4:5d1359a283bc 410 };
simon.ford@mbed.co.uk 5:62573be585e9 411
simon.ford@mbed.co.uk 5:62573be585e9 412 template<class C>
simon.ford@mbed.co.uk 5:62573be585e9 413 const struct rpc_method *rpc_super(Base *this_ptr) {
simon.ford@mbed.co.uk 5:62573be585e9 414 return static_cast<C*>(this_ptr)->C::get_rpc_methods();
simon.ford@mbed.co.uk 5:62573be585e9 415 }
simon.ford@mbed.co.uk 5:62573be585e9 416
simon.ford@mbed.co.uk 4:5d1359a283bc 417 #define RPC_METHOD_END { NULL, NULL }
simon.ford@mbed.co.uk 5:62573be585e9 418 #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::caller_t)(rpc_method::super_t)rpc_super<C> }
simon.ford@mbed.co.uk 4:5d1359a283bc 419
simon.ford@mbed.co.uk 4:5d1359a283bc 420 /* Function rpc
simon.ford@mbed.co.uk 4:5d1359a283bc 421 * Parse a string describing a call and then do it
simon.ford@mbed.co.uk 4:5d1359a283bc 422 *
simon.ford@mbed.co.uk 4:5d1359a283bc 423 * Variables
simon.ford@mbed.co.uk 4:5d1359a283bc 424 * call - A pointer to a string describing the call, which has
simon.ford@mbed.co.uk 4:5d1359a283bc 425 * the form /object/method arg ... argn. Arguments are
simon.ford@mbed.co.uk 4:5d1359a283bc 426 * delimited by space characters, and the string is terminated
simon.ford@mbed.co.uk 4:5d1359a283bc 427 * by a null character.
simon.ford@mbed.co.uk 4:5d1359a283bc 428 * result - A pointer to an array to write the result into.
simon.ford@mbed.co.uk 4:5d1359a283bc 429 */
simon.ford@mbed.co.uk 4:5d1359a283bc 430 bool rpc(const char *buf, char *result = 0);
simon.ford@mbed.co.uk 4:5d1359a283bc 431
simon.ford@mbed.co.uk 5:62573be585e9 432
simon.ford@mbed.co.uk 4:5d1359a283bc 433 } /* namespace mbed */
simon.ford@mbed.co.uk 4:5d1359a283bc 434
simon.ford@mbed.co.uk 4:5d1359a283bc 435 #endif