This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /* mbed Microcontroller Library - RPC
Anaesthetix 0:978110f7f027 2 * Copyright (c) 2008-2009 ARM Limited. All rights reserved.
Anaesthetix 0:978110f7f027 3 */
Anaesthetix 0:978110f7f027 4
Anaesthetix 0:978110f7f027 5 #ifndef MBED_RPC_H
Anaesthetix 0:978110f7f027 6 #define MBED_RPC_H
Anaesthetix 0:978110f7f027 7
Anaesthetix 0:978110f7f027 8 /* Section rpc
Anaesthetix 0:978110f7f027 9 * Helpers for rpc handling.
Anaesthetix 0:978110f7f027 10 */
Anaesthetix 0:978110f7f027 11
Anaesthetix 0:978110f7f027 12 #include <stdlib.h>
Anaesthetix 0:978110f7f027 13 #include <stdio.h>
Anaesthetix 0:978110f7f027 14 #include <string.h>
Anaesthetix 0:978110f7f027 15 #include <ctype.h>
Anaesthetix 0:978110f7f027 16 #include "Base.h"
Anaesthetix 0:978110f7f027 17
Anaesthetix 0:978110f7f027 18 #include "PinNames.h"
Anaesthetix 0:978110f7f027 19 #include <stdint.h>
Anaesthetix 0:978110f7f027 20
Anaesthetix 0:978110f7f027 21 namespace mbed {
Anaesthetix 0:978110f7f027 22
Anaesthetix 0:978110f7f027 23 /* Function parse_arg
Anaesthetix 0:978110f7f027 24 * Parses and returns a value from a string.
Anaesthetix 0:978110f7f027 25 *
Anaesthetix 0:978110f7f027 26 * Variable
Anaesthetix 0:978110f7f027 27 * arg - The string to pase
Anaesthetix 0:978110f7f027 28 * next - If not NULL a pointer to after the last
Anaesthetix 0:978110f7f027 29 * character parsed is written here
Anaesthetix 0:978110f7f027 30 */
Anaesthetix 0:978110f7f027 31 template<typename T> T parse_arg(const char *arg, const char **next);
Anaesthetix 0:978110f7f027 32
Anaesthetix 0:978110f7f027 33 inline char parse_char(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 34 char c = *arg++;
Anaesthetix 0:978110f7f027 35 if(c == '\\') {
Anaesthetix 0:978110f7f027 36 c = *arg++;
Anaesthetix 0:978110f7f027 37 switch(c) {
Anaesthetix 0:978110f7f027 38 case 'a': c = '\a'; break;
Anaesthetix 0:978110f7f027 39 case 'b': c = '\b'; break;
Anaesthetix 0:978110f7f027 40 case 't': c = '\t'; break;
Anaesthetix 0:978110f7f027 41 case 'n': c = '\n'; break;
Anaesthetix 0:978110f7f027 42 case 'v': c = '\v'; break;
Anaesthetix 0:978110f7f027 43 case 'f': c = '\f'; break;
Anaesthetix 0:978110f7f027 44 case 'r': c = '\r'; break;
Anaesthetix 0:978110f7f027 45 case 'x':
Anaesthetix 0:978110f7f027 46 {
Anaesthetix 0:978110f7f027 47 /* two-character hexadecimal */
Anaesthetix 0:978110f7f027 48 char buf[3];
Anaesthetix 0:978110f7f027 49 buf[0] = *arg++;
Anaesthetix 0:978110f7f027 50 buf[1] = *arg++;
Anaesthetix 0:978110f7f027 51 buf[2] = 0;
Anaesthetix 0:978110f7f027 52 c = strtol(buf, NULL, 16);
Anaesthetix 0:978110f7f027 53 }
Anaesthetix 0:978110f7f027 54 break;
Anaesthetix 0:978110f7f027 55 default:
Anaesthetix 0:978110f7f027 56 if(isdigit(c)) {
Anaesthetix 0:978110f7f027 57 /* three-character octal */
Anaesthetix 0:978110f7f027 58 char buf[4];
Anaesthetix 0:978110f7f027 59 buf[0] = c;
Anaesthetix 0:978110f7f027 60 buf[1] = *arg++;
Anaesthetix 0:978110f7f027 61 buf[2] = *arg++;
Anaesthetix 0:978110f7f027 62 buf[3] = 0;
Anaesthetix 0:978110f7f027 63 c = strtol(buf, NULL, 8);
Anaesthetix 0:978110f7f027 64 }
Anaesthetix 0:978110f7f027 65 break;
Anaesthetix 0:978110f7f027 66 }
Anaesthetix 0:978110f7f027 67 }
Anaesthetix 0:978110f7f027 68 *next = arg;
Anaesthetix 0:978110f7f027 69 return c;
Anaesthetix 0:978110f7f027 70 }
Anaesthetix 0:978110f7f027 71
Anaesthetix 0:978110f7f027 72 /* signed integer types */
Anaesthetix 0:978110f7f027 73
Anaesthetix 0:978110f7f027 74 template<> inline int parse_arg<int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 75 if(arg[0] == '\'') {
Anaesthetix 0:978110f7f027 76 char c = parse_char(arg+1, &arg);
Anaesthetix 0:978110f7f027 77 if(next != NULL) *next = arg+1;
Anaesthetix 0:978110f7f027 78 return c;
Anaesthetix 0:978110f7f027 79 } else {
Anaesthetix 0:978110f7f027 80 return strtol(arg, const_cast<char**>(next), 0);
Anaesthetix 0:978110f7f027 81 }
Anaesthetix 0:978110f7f027 82 }
Anaesthetix 0:978110f7f027 83
Anaesthetix 0:978110f7f027 84 template<> inline char parse_arg<char>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 85 return parse_arg<int>(arg,next);
Anaesthetix 0:978110f7f027 86 }
Anaesthetix 0:978110f7f027 87
Anaesthetix 0:978110f7f027 88 template<> inline short int parse_arg<short int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 89 return parse_arg<int>(arg,next);
Anaesthetix 0:978110f7f027 90 }
Anaesthetix 0:978110f7f027 91
Anaesthetix 0:978110f7f027 92 template<> inline long int parse_arg<long int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 93 return parse_arg<int>(arg,next);
Anaesthetix 0:978110f7f027 94 }
Anaesthetix 0:978110f7f027 95
Anaesthetix 0:978110f7f027 96 template<> inline long long parse_arg<long long>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 97 return strtoll(arg, const_cast<char**>(next), 0);
Anaesthetix 0:978110f7f027 98 }
Anaesthetix 0:978110f7f027 99
Anaesthetix 0:978110f7f027 100 /* unsigned integer types */
Anaesthetix 0:978110f7f027 101
Anaesthetix 0:978110f7f027 102 template<> inline unsigned int parse_arg<unsigned int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 103 if(arg[0] == '\'') {
Anaesthetix 0:978110f7f027 104 char c = parse_char(arg+1, &arg);
Anaesthetix 0:978110f7f027 105 if(next != NULL) *next = arg+1;
Anaesthetix 0:978110f7f027 106 return c;
Anaesthetix 0:978110f7f027 107 } else {
Anaesthetix 0:978110f7f027 108 return strtoul(arg, const_cast<char**>(next), 0);
Anaesthetix 0:978110f7f027 109 }
Anaesthetix 0:978110f7f027 110 }
Anaesthetix 0:978110f7f027 111
Anaesthetix 0:978110f7f027 112 template<> inline unsigned char parse_arg<unsigned char>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 113 return parse_arg<unsigned int>(arg,next);
Anaesthetix 0:978110f7f027 114 }
Anaesthetix 0:978110f7f027 115
Anaesthetix 0:978110f7f027 116 template<> inline unsigned short int parse_arg<unsigned short int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 117 return parse_arg<unsigned int>(arg,next);
Anaesthetix 0:978110f7f027 118 }
Anaesthetix 0:978110f7f027 119
Anaesthetix 0:978110f7f027 120 template<> inline unsigned long int parse_arg<unsigned long int>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 121 return parse_arg<unsigned int>(arg,next);
Anaesthetix 0:978110f7f027 122 }
Anaesthetix 0:978110f7f027 123
Anaesthetix 0:978110f7f027 124 template<> inline unsigned long long parse_arg<unsigned long long>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 125 return strtoull(arg, const_cast<char**>(next), 0);
Anaesthetix 0:978110f7f027 126 }
Anaesthetix 0:978110f7f027 127
Anaesthetix 0:978110f7f027 128 /* floating types */
Anaesthetix 0:978110f7f027 129
Anaesthetix 0:978110f7f027 130 template<> inline float parse_arg<float>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 131 #if !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 410000
Anaesthetix 0:978110f7f027 132 return strtof(arg,const_cast<char**>(next));
Anaesthetix 0:978110f7f027 133 #elif __ARMCC_VERSION >= 310000
Anaesthetix 0:978110f7f027 134 /* bug in header means no using declaration for strtof */
Anaesthetix 0:978110f7f027 135 return std::strtof(arg,const_cast<char**>(next));
Anaesthetix 0:978110f7f027 136 #else
Anaesthetix 0:978110f7f027 137 /* strtof not supported */
Anaesthetix 0:978110f7f027 138 return strtod(arg,const_cast<char**>(next));
Anaesthetix 0:978110f7f027 139 #endif
Anaesthetix 0:978110f7f027 140 }
Anaesthetix 0:978110f7f027 141
Anaesthetix 0:978110f7f027 142 template<> inline double parse_arg<double>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 143 return strtod(arg,const_cast<char**>(next));
Anaesthetix 0:978110f7f027 144 }
Anaesthetix 0:978110f7f027 145
Anaesthetix 0:978110f7f027 146 template<> inline long double parse_arg<long double>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 147 return strtod(arg,const_cast<char**>(next));
Anaesthetix 0:978110f7f027 148 }
Anaesthetix 0:978110f7f027 149
Anaesthetix 0:978110f7f027 150 /* string */
Anaesthetix 0:978110f7f027 151
Anaesthetix 0:978110f7f027 152 template<> inline char *parse_arg<char*>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 153 const char *ptr = arg;
Anaesthetix 0:978110f7f027 154 char *res = NULL;
Anaesthetix 0:978110f7f027 155 if(*arg == '"') {
Anaesthetix 0:978110f7f027 156 /* quoted string */
Anaesthetix 0:978110f7f027 157 ptr = ++arg;
Anaesthetix 0:978110f7f027 158 int len = 0;
Anaesthetix 0:978110f7f027 159 /* find the end (and length) of the quoted string */
Anaesthetix 0:978110f7f027 160 for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) {
Anaesthetix 0:978110f7f027 161 len++;
Anaesthetix 0:978110f7f027 162 if(c == '\\') {
Anaesthetix 0:978110f7f027 163 ptr++;
Anaesthetix 0:978110f7f027 164 }
Anaesthetix 0:978110f7f027 165 }
Anaesthetix 0:978110f7f027 166 /* copy the quoted string, and unescape characters */
Anaesthetix 0:978110f7f027 167 if(len != 0) {
Anaesthetix 0:978110f7f027 168 res = new char[len+1];
Anaesthetix 0:978110f7f027 169 char *resptr = res;
Anaesthetix 0:978110f7f027 170 while(arg != ptr) {
Anaesthetix 0:978110f7f027 171 *resptr++ = parse_char(arg, &arg);
Anaesthetix 0:978110f7f027 172 }
Anaesthetix 0:978110f7f027 173 *resptr = 0;
Anaesthetix 0:978110f7f027 174 }
Anaesthetix 0:978110f7f027 175 } else {
Anaesthetix 0:978110f7f027 176 /* unquoted string */
Anaesthetix 0:978110f7f027 177 while(isalnum(*ptr) || *ptr=='_') {
Anaesthetix 0:978110f7f027 178 ptr++;
Anaesthetix 0:978110f7f027 179 }
Anaesthetix 0:978110f7f027 180 int len = ptr-arg;
Anaesthetix 0:978110f7f027 181 if(len!=0) {
Anaesthetix 0:978110f7f027 182 res = new char[len+1];
Anaesthetix 0:978110f7f027 183 memcpy(res, arg, len);
Anaesthetix 0:978110f7f027 184 res[len] = 0;
Anaesthetix 0:978110f7f027 185 }
Anaesthetix 0:978110f7f027 186 }
Anaesthetix 0:978110f7f027 187
Anaesthetix 0:978110f7f027 188 if(next != NULL) {
Anaesthetix 0:978110f7f027 189 *next = ptr;
Anaesthetix 0:978110f7f027 190 }
Anaesthetix 0:978110f7f027 191 return res;
Anaesthetix 0:978110f7f027 192 }
Anaesthetix 0:978110f7f027 193
Anaesthetix 0:978110f7f027 194 template<> inline const char *parse_arg<const char*>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 195 return parse_arg<char*>(arg,next);
Anaesthetix 0:978110f7f027 196 }
Anaesthetix 0:978110f7f027 197
Anaesthetix 0:978110f7f027 198 /* Pins */
Anaesthetix 0:978110f7f027 199
Anaesthetix 0:978110f7f027 200
Anaesthetix 0:978110f7f027 201 inline PinName parse_pins(const char *str) {
Anaesthetix 0:978110f7f027 202 const PinName pin_names[] = {p5, p6, p7, p8, p9, p10, p11, p12, p13, p14
Anaesthetix 0:978110f7f027 203 , p15, p16, p17, p18, p19, p20, p21, p22, p23
Anaesthetix 0:978110f7f027 204 , p24, p25, p26, p27, p28, p29, p30};
Anaesthetix 0:978110f7f027 205
Anaesthetix 0:978110f7f027 206 if(str[0] == 'P') { // Pn_n
Anaesthetix 0:978110f7f027 207 uint32_t port = str[1] - '0';
Anaesthetix 0:978110f7f027 208 uint32_t pin = str[3] - '0'; // Pn_n
Anaesthetix 0:978110f7f027 209 uint32_t pin2 = str[4] - '0'; // Pn_nn
Anaesthetix 0:978110f7f027 210 if(pin2 <= 9) {
Anaesthetix 0:978110f7f027 211 pin = pin * 10 + pin2;
Anaesthetix 0:978110f7f027 212 }
Anaesthetix 0:978110f7f027 213 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
Anaesthetix 0:978110f7f027 214 return (PinName)(LPC_GPIO0_BASE + port * 32 + pin);
Anaesthetix 0:978110f7f027 215 #elif defined(TARGET_LPC11U24)
Anaesthetix 0:978110f7f027 216 return (PinName)(port * 32 + pin);
Anaesthetix 0:978110f7f027 217 #endif
Anaesthetix 0:978110f7f027 218 } else if(str[0] == 'p') { // pn
Anaesthetix 0:978110f7f027 219 uint32_t pin = str[1] - '0'; // pn
Anaesthetix 0:978110f7f027 220 uint32_t pin2 = str[2] - '0'; // pnn
Anaesthetix 0:978110f7f027 221 if(pin2 <= 9) {
Anaesthetix 0:978110f7f027 222 pin = pin * 10 + pin2;
Anaesthetix 0:978110f7f027 223 }
Anaesthetix 0:978110f7f027 224 if(pin < 5 || pin > 30) {
Anaesthetix 0:978110f7f027 225 return NC;
Anaesthetix 0:978110f7f027 226 }
Anaesthetix 0:978110f7f027 227 return pin_names[pin - 5];
Anaesthetix 0:978110f7f027 228 } else if(str[0] == 'L') { // LEDn
Anaesthetix 0:978110f7f027 229 switch(str[3]) {
Anaesthetix 0:978110f7f027 230 case '1' : return LED1;
Anaesthetix 0:978110f7f027 231 case '2' : return LED2;
Anaesthetix 0:978110f7f027 232 case '3' : return LED3;
Anaesthetix 0:978110f7f027 233 case '4' : return LED4;
Anaesthetix 0:978110f7f027 234 }
Anaesthetix 0:978110f7f027 235 } else if(str[0] == 'U') { // USB?X
Anaesthetix 0:978110f7f027 236 switch(str[3]) {
Anaesthetix 0:978110f7f027 237 case 'T' : return USBTX;
Anaesthetix 0:978110f7f027 238 case 'R' : return USBRX;
Anaesthetix 0:978110f7f027 239 }
Anaesthetix 0:978110f7f027 240 }
Anaesthetix 0:978110f7f027 241 return NC;
Anaesthetix 0:978110f7f027 242 }
Anaesthetix 0:978110f7f027 243
Anaesthetix 0:978110f7f027 244 template<> inline PinName parse_arg<PinName>(const char *arg, const char **next) {
Anaesthetix 0:978110f7f027 245 const char *ptr = arg;
Anaesthetix 0:978110f7f027 246 PinName pinname = NC;
Anaesthetix 0:978110f7f027 247 while(isalnum(*ptr) || *ptr=='_') {
Anaesthetix 0:978110f7f027 248 ptr++;
Anaesthetix 0:978110f7f027 249 }
Anaesthetix 0:978110f7f027 250 int len = ptr-arg;
Anaesthetix 0:978110f7f027 251 if(len!=0) {
Anaesthetix 0:978110f7f027 252 pinname = parse_pins(arg);
Anaesthetix 0:978110f7f027 253
Anaesthetix 0:978110f7f027 254 }
Anaesthetix 0:978110f7f027 255 if(next != NULL) {
Anaesthetix 0:978110f7f027 256 *next = ptr;
Anaesthetix 0:978110f7f027 257 }
Anaesthetix 0:978110f7f027 258 return pinname;
Anaesthetix 0:978110f7f027 259 }
Anaesthetix 0:978110f7f027 260
Anaesthetix 0:978110f7f027 261
Anaesthetix 0:978110f7f027 262 /* Function write_result
Anaesthetix 0:978110f7f027 263 * Writes a value in to a result string in an appropriate manner
Anaesthetix 0:978110f7f027 264 *
Anaesthetix 0:978110f7f027 265 * Variable
Anaesthetix 0:978110f7f027 266 * val - The value to write
Anaesthetix 0:978110f7f027 267 * result - A pointer to the array to write the value into
Anaesthetix 0:978110f7f027 268 */
Anaesthetix 0:978110f7f027 269 template<typename T> void write_result(T val, char *result);
Anaesthetix 0:978110f7f027 270
Anaesthetix 0:978110f7f027 271 /* signed integer types */
Anaesthetix 0:978110f7f027 272
Anaesthetix 0:978110f7f027 273 template<> inline void write_result<char>(char val, char *result) {
Anaesthetix 0:978110f7f027 274 result[0] = val;
Anaesthetix 0:978110f7f027 275 result[1] = '\0';
Anaesthetix 0:978110f7f027 276 }
Anaesthetix 0:978110f7f027 277
Anaesthetix 0:978110f7f027 278 template<> inline void write_result<short int>(short int val, char *result) {
Anaesthetix 0:978110f7f027 279 sprintf(result, "%hi", val);
Anaesthetix 0:978110f7f027 280 }
Anaesthetix 0:978110f7f027 281
Anaesthetix 0:978110f7f027 282 template<> inline void write_result<int>(int val, char *result) {
Anaesthetix 0:978110f7f027 283 sprintf(result, "%i", val);
Anaesthetix 0:978110f7f027 284 }
Anaesthetix 0:978110f7f027 285
Anaesthetix 0:978110f7f027 286 template<> inline void write_result<long int>(long int val, char *result) {
Anaesthetix 0:978110f7f027 287 sprintf(result, "%li", val);
Anaesthetix 0:978110f7f027 288 }
Anaesthetix 0:978110f7f027 289
Anaesthetix 0:978110f7f027 290 template<> inline void write_result<long long int>(long long int val, char *result) {
Anaesthetix 0:978110f7f027 291 sprintf(result, "%lli", val);
Anaesthetix 0:978110f7f027 292 }
Anaesthetix 0:978110f7f027 293
Anaesthetix 0:978110f7f027 294 /* unsigned integer types */
Anaesthetix 0:978110f7f027 295
Anaesthetix 0:978110f7f027 296 template<> inline void write_result<unsigned char>(unsigned char val, char *result) {
Anaesthetix 0:978110f7f027 297 result[0] = val;
Anaesthetix 0:978110f7f027 298 result[1] = '\0';
Anaesthetix 0:978110f7f027 299 }
Anaesthetix 0:978110f7f027 300
Anaesthetix 0:978110f7f027 301 template<> inline void write_result<unsigned short int>(unsigned short int val, char *result) {
Anaesthetix 0:978110f7f027 302 sprintf(result, "%hu", val);
Anaesthetix 0:978110f7f027 303 }
Anaesthetix 0:978110f7f027 304
Anaesthetix 0:978110f7f027 305 template<> inline void write_result<unsigned int>(unsigned int val, char *result) {
Anaesthetix 0:978110f7f027 306 sprintf(result, "%u", val);
Anaesthetix 0:978110f7f027 307 }
Anaesthetix 0:978110f7f027 308
Anaesthetix 0:978110f7f027 309 template<> inline void write_result<unsigned long int>(unsigned long int val, char *result) {
Anaesthetix 0:978110f7f027 310 sprintf(result, "%lu", val);
Anaesthetix 0:978110f7f027 311 }
Anaesthetix 0:978110f7f027 312
Anaesthetix 0:978110f7f027 313 template<> inline void write_result<unsigned long long int>(unsigned long long int val, char *result) {
Anaesthetix 0:978110f7f027 314 sprintf(result, "%llu", val);
Anaesthetix 0:978110f7f027 315 }
Anaesthetix 0:978110f7f027 316
Anaesthetix 0:978110f7f027 317 /* floating types */
Anaesthetix 0:978110f7f027 318
Anaesthetix 0:978110f7f027 319 template<> inline void write_result<float>(float val, char *result) {
Anaesthetix 0:978110f7f027 320 sprintf(result, "%.17g", val);
Anaesthetix 0:978110f7f027 321 }
Anaesthetix 0:978110f7f027 322
Anaesthetix 0:978110f7f027 323 template<> inline void write_result<double>(double val, char *result) {
Anaesthetix 0:978110f7f027 324 sprintf(result, "%.17g", val);
Anaesthetix 0:978110f7f027 325 }
Anaesthetix 0:978110f7f027 326
Anaesthetix 0:978110f7f027 327 template<> inline void write_result<long double>(long double val, char *result) {
Anaesthetix 0:978110f7f027 328 sprintf(result, "%.17Lg", val);
Anaesthetix 0:978110f7f027 329 }
Anaesthetix 0:978110f7f027 330
Anaesthetix 0:978110f7f027 331
Anaesthetix 0:978110f7f027 332 /* string */
Anaesthetix 0:978110f7f027 333
Anaesthetix 0:978110f7f027 334 template<> inline void write_result<char*>(char *val, char *result) {
Anaesthetix 0:978110f7f027 335 if(val==NULL) {
Anaesthetix 0:978110f7f027 336 result[0] = 0;
Anaesthetix 0:978110f7f027 337 } else {
Anaesthetix 0:978110f7f027 338 strcpy(result, val);
Anaesthetix 0:978110f7f027 339 }
Anaesthetix 0:978110f7f027 340 }
Anaesthetix 0:978110f7f027 341
Anaesthetix 0:978110f7f027 342 template<> inline void write_result<const char*>(const char *val, char *result) {
Anaesthetix 0:978110f7f027 343 if(val==NULL) {
Anaesthetix 0:978110f7f027 344 result[0] = 0;
Anaesthetix 0:978110f7f027 345 } else {
Anaesthetix 0:978110f7f027 346 strcpy(result, val);
Anaesthetix 0:978110f7f027 347 }
Anaesthetix 0:978110f7f027 348 }
Anaesthetix 0:978110f7f027 349
Anaesthetix 0:978110f7f027 350
Anaesthetix 0:978110f7f027 351 inline const char *next_arg(const char* next) {
Anaesthetix 0:978110f7f027 352 while(*next == ' ') next++;
Anaesthetix 0:978110f7f027 353 if(*next == ',' || *next == '?') next++;
Anaesthetix 0:978110f7f027 354 while(*next == ' ') next++;
Anaesthetix 0:978110f7f027 355 return next;
Anaesthetix 0:978110f7f027 356 }
Anaesthetix 0:978110f7f027 357
Anaesthetix 0:978110f7f027 358
Anaesthetix 0:978110f7f027 359 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 360 */
Anaesthetix 0:978110f7f027 361 template<class T, void (T::*member)(const char *,char *)>
Anaesthetix 0:978110f7f027 362 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 363 (static_cast<T*>(this_ptr)->*member)(arguments,result);
Anaesthetix 0:978110f7f027 364 }
Anaesthetix 0:978110f7f027 365
Anaesthetix 0:978110f7f027 366
Anaesthetix 0:978110f7f027 367 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 368 */
Anaesthetix 0:978110f7f027 369 template<class T, void (T::*member)()>
Anaesthetix 0:978110f7f027 370 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 371 (static_cast<T*>(this_ptr)->*member)();
Anaesthetix 0:978110f7f027 372 if(result != NULL) {
Anaesthetix 0:978110f7f027 373 result[0] = '\0';
Anaesthetix 0:978110f7f027 374 }
Anaesthetix 0:978110f7f027 375 }
Anaesthetix 0:978110f7f027 376
Anaesthetix 0:978110f7f027 377
Anaesthetix 0:978110f7f027 378 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 379 */
Anaesthetix 0:978110f7f027 380 template<class T, typename A1, void (T::*member)(A1)>
Anaesthetix 0:978110f7f027 381 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 382
Anaesthetix 0:978110f7f027 383 const char *next = arguments;
Anaesthetix 0:978110f7f027 384 A1 arg1 = parse_arg<A1>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 385
Anaesthetix 0:978110f7f027 386 (static_cast<T*>(this_ptr)->*member)(arg1);
Anaesthetix 0:978110f7f027 387 if(result != NULL) {
Anaesthetix 0:978110f7f027 388 result[0] = '\0';
Anaesthetix 0:978110f7f027 389 }
Anaesthetix 0:978110f7f027 390 }
Anaesthetix 0:978110f7f027 391
Anaesthetix 0:978110f7f027 392
Anaesthetix 0:978110f7f027 393 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 394 */
Anaesthetix 0:978110f7f027 395 template<class T, typename A1, typename A2, void (T::*member)(A1,A2)>
Anaesthetix 0:978110f7f027 396 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 397
Anaesthetix 0:978110f7f027 398 const char *next = arguments;
Anaesthetix 0:978110f7f027 399 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 400 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 401
Anaesthetix 0:978110f7f027 402 (static_cast<T*>(this_ptr)->*member)(arg1,arg2);
Anaesthetix 0:978110f7f027 403 if(result != NULL) {
Anaesthetix 0:978110f7f027 404 result[0] = '\0';
Anaesthetix 0:978110f7f027 405 }
Anaesthetix 0:978110f7f027 406 }
Anaesthetix 0:978110f7f027 407
Anaesthetix 0:978110f7f027 408
Anaesthetix 0:978110f7f027 409 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 410 */
Anaesthetix 0:978110f7f027 411 template<class T, typename A1, typename A2, typename A3, void (T::*member)(A1,A2,A3)>
Anaesthetix 0:978110f7f027 412 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 413
Anaesthetix 0:978110f7f027 414 const char *next = arguments;
Anaesthetix 0:978110f7f027 415 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 416 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 417 A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 418
Anaesthetix 0:978110f7f027 419 (static_cast<T*>(this_ptr)->*member)(arg1,arg2,arg3);
Anaesthetix 0:978110f7f027 420 if(result != NULL) {
Anaesthetix 0:978110f7f027 421 result[0] = '\0';
Anaesthetix 0:978110f7f027 422 }
Anaesthetix 0:978110f7f027 423 }
Anaesthetix 0:978110f7f027 424
Anaesthetix 0:978110f7f027 425
Anaesthetix 0:978110f7f027 426 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 427 */
Anaesthetix 0:978110f7f027 428 template<typename R, class T, R (T::*member)()>
Anaesthetix 0:978110f7f027 429 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 430 R res = (static_cast<T*>(this_ptr)->*member)();
Anaesthetix 0:978110f7f027 431 if(result != NULL) {
Anaesthetix 0:978110f7f027 432 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 433 }
Anaesthetix 0:978110f7f027 434 }
Anaesthetix 0:978110f7f027 435
Anaesthetix 0:978110f7f027 436
Anaesthetix 0:978110f7f027 437 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 438 */
Anaesthetix 0:978110f7f027 439 template<typename R, class T, typename A1, R (T::*member)(A1)>
Anaesthetix 0:978110f7f027 440 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 441
Anaesthetix 0:978110f7f027 442 const char *next = arguments;
Anaesthetix 0:978110f7f027 443 A1 arg1 = parse_arg<A1>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 444
Anaesthetix 0:978110f7f027 445 R res = (static_cast<T*>(this_ptr)->*member)(arg1);
Anaesthetix 0:978110f7f027 446 if(result != NULL) {
Anaesthetix 0:978110f7f027 447 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 448 }
Anaesthetix 0:978110f7f027 449 }
Anaesthetix 0:978110f7f027 450
Anaesthetix 0:978110f7f027 451
Anaesthetix 0:978110f7f027 452 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 453 */
Anaesthetix 0:978110f7f027 454 template<typename R, class T, typename A1, typename A2, R (T::*member)(A1,A2)>
Anaesthetix 0:978110f7f027 455 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 456
Anaesthetix 0:978110f7f027 457 const char *next = arguments;
Anaesthetix 0:978110f7f027 458 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 459 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 460
Anaesthetix 0:978110f7f027 461 R res = (static_cast<T*>(this_ptr)->*member)(arg1,arg2);
Anaesthetix 0:978110f7f027 462 if(result != NULL) {
Anaesthetix 0:978110f7f027 463 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 464 }
Anaesthetix 0:978110f7f027 465 }
Anaesthetix 0:978110f7f027 466
Anaesthetix 0:978110f7f027 467
Anaesthetix 0:978110f7f027 468 /* Function rpc_method_caller
Anaesthetix 0:978110f7f027 469 */
Anaesthetix 0:978110f7f027 470 template<typename R, class T, typename A1, typename A2, typename A3, R (T::*member)(A1,A2,A3)>
Anaesthetix 0:978110f7f027 471 void rpc_method_caller(Base *this_ptr, const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 472
Anaesthetix 0:978110f7f027 473 const char *next = arguments;
Anaesthetix 0:978110f7f027 474 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 475 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 476 A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 477
Anaesthetix 0:978110f7f027 478 R res = (static_cast<T*>(this_ptr)->*member)(arg1,arg2,arg3);
Anaesthetix 0:978110f7f027 479 if(result != NULL) {
Anaesthetix 0:978110f7f027 480 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 481 }
Anaesthetix 0:978110f7f027 482 }
Anaesthetix 0:978110f7f027 483
Anaesthetix 0:978110f7f027 484
Anaesthetix 0:978110f7f027 485 /* Function rpc_function caller
Anaesthetix 0:978110f7f027 486 */
Anaesthetix 0:978110f7f027 487 template<typename R, R (*func)()>
Anaesthetix 0:978110f7f027 488 void rpc_function_caller(const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 489 R res = (*func)();
Anaesthetix 0:978110f7f027 490 if(result != NULL) {
Anaesthetix 0:978110f7f027 491 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 492 }
Anaesthetix 0:978110f7f027 493 }
Anaesthetix 0:978110f7f027 494
Anaesthetix 0:978110f7f027 495
Anaesthetix 0:978110f7f027 496 /* Function rpc_function caller
Anaesthetix 0:978110f7f027 497 */
Anaesthetix 0:978110f7f027 498 template<typename R, typename A1, R (*func)(A1)>
Anaesthetix 0:978110f7f027 499 void rpc_function_caller(const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 500 A1 arg1 = parse_arg<A1>(next_arg(arguments),NULL);
Anaesthetix 0:978110f7f027 501 R res = (*func)(arg1);
Anaesthetix 0:978110f7f027 502 if(result != NULL) {
Anaesthetix 0:978110f7f027 503 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 504 }
Anaesthetix 0:978110f7f027 505 }
Anaesthetix 0:978110f7f027 506
Anaesthetix 0:978110f7f027 507
Anaesthetix 0:978110f7f027 508 /* Function rpc_function caller
Anaesthetix 0:978110f7f027 509 */
Anaesthetix 0:978110f7f027 510 template<typename R, typename A1, typename A2, R (*func)(A1,A2)>
Anaesthetix 0:978110f7f027 511 void rpc_function_caller(const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 512
Anaesthetix 0:978110f7f027 513 const char *next = arguments;
Anaesthetix 0:978110f7f027 514 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 515 A2 arg2 = parse_arg<A2>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 516
Anaesthetix 0:978110f7f027 517 R res = (*func)(arg1,arg2);
Anaesthetix 0:978110f7f027 518 if(result != NULL) {
Anaesthetix 0:978110f7f027 519 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 520 }
Anaesthetix 0:978110f7f027 521 }
Anaesthetix 0:978110f7f027 522
Anaesthetix 0:978110f7f027 523
Anaesthetix 0:978110f7f027 524 /* Function rpc_function caller
Anaesthetix 0:978110f7f027 525 */
Anaesthetix 0:978110f7f027 526 template<typename R, typename A1, typename A2, typename A3, R (*func)(A1,A2,A3)>
Anaesthetix 0:978110f7f027 527 void rpc_function_caller(const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 528
Anaesthetix 0:978110f7f027 529 const char *next = arguments;
Anaesthetix 0:978110f7f027 530 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 531 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 532 A3 arg3 = parse_arg<A3>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 533
Anaesthetix 0:978110f7f027 534 R res = (*func)(arg1,arg2,arg3);
Anaesthetix 0:978110f7f027 535 if(result != NULL) {
Anaesthetix 0:978110f7f027 536 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 537 }
Anaesthetix 0:978110f7f027 538 }
Anaesthetix 0:978110f7f027 539
Anaesthetix 0:978110f7f027 540
Anaesthetix 0:978110f7f027 541 /* Function rpc_function caller
Anaesthetix 0:978110f7f027 542 */
Anaesthetix 0:978110f7f027 543 template<typename R, typename A1, typename A2, typename A3, typename A4, R (*func)(A1,A2,A3,A4)>
Anaesthetix 0:978110f7f027 544 void rpc_function_caller(const char *arguments, char *result) {
Anaesthetix 0:978110f7f027 545
Anaesthetix 0:978110f7f027 546 const char *next = arguments;
Anaesthetix 0:978110f7f027 547 A1 arg1 = parse_arg<A1>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 548 A2 arg2 = parse_arg<A2>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 549 A3 arg3 = parse_arg<A3>(next_arg(next),&next);
Anaesthetix 0:978110f7f027 550 A4 arg4 = parse_arg<A4>(next_arg(next),NULL);
Anaesthetix 0:978110f7f027 551
Anaesthetix 0:978110f7f027 552 R res = (*func)(arg1,arg2,arg3,arg4);
Anaesthetix 0:978110f7f027 553 if(result != NULL) {
Anaesthetix 0:978110f7f027 554 write_result<R>(res, result);
Anaesthetix 0:978110f7f027 555 }
Anaesthetix 0:978110f7f027 556 }
Anaesthetix 0:978110f7f027 557
Anaesthetix 0:978110f7f027 558
Anaesthetix 0:978110f7f027 559 struct rpc_method {
Anaesthetix 0:978110f7f027 560 const char *name;
Anaesthetix 0:978110f7f027 561 typedef void (*caller_t)(Base*, const char*, char*);
Anaesthetix 0:978110f7f027 562 typedef const struct rpc_method *(*super_t)(Base*);
Anaesthetix 0:978110f7f027 563 union {
Anaesthetix 0:978110f7f027 564 caller_t caller;
Anaesthetix 0:978110f7f027 565 super_t super;
Anaesthetix 0:978110f7f027 566 };
Anaesthetix 0:978110f7f027 567 };
Anaesthetix 0:978110f7f027 568
Anaesthetix 0:978110f7f027 569 template<class C>
Anaesthetix 0:978110f7f027 570 const struct rpc_method *rpc_super(Base *this_ptr) {
Anaesthetix 0:978110f7f027 571 return static_cast<C*>(this_ptr)->C::get_rpc_methods();
Anaesthetix 0:978110f7f027 572 }
Anaesthetix 0:978110f7f027 573
Anaesthetix 0:978110f7f027 574 #define RPC_METHOD_END { NULL, NULL }
Anaesthetix 0:978110f7f027 575 #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::caller_t)(rpc_method::super_t)rpc_super<C> }
Anaesthetix 0:978110f7f027 576
Anaesthetix 0:978110f7f027 577 /* Function rpc
Anaesthetix 0:978110f7f027 578 * Parse a string describing a call and then do it
Anaesthetix 0:978110f7f027 579 *
Anaesthetix 0:978110f7f027 580 * Variables
Anaesthetix 0:978110f7f027 581 * call - A pointer to a string describing the call, which has
Anaesthetix 0:978110f7f027 582 * the form /object/method arg ... argn. Arguments are
Anaesthetix 0:978110f7f027 583 * delimited by space characters, and the string is terminated
Anaesthetix 0:978110f7f027 584 * by a null character.
Anaesthetix 0:978110f7f027 585 * result - A pointer to an array to write the result into.
Anaesthetix 0:978110f7f027 586 */
Anaesthetix 0:978110f7f027 587 bool rpc(const char *buf, char *result = 0);
Anaesthetix 0:978110f7f027 588
Anaesthetix 0:978110f7f027 589
Anaesthetix 0:978110f7f027 590 } // namespace mbed
Anaesthetix 0:978110f7f027 591
Anaesthetix 0:978110f7f027 592 #endif