Programme d'utilisation des AX12 avec rajout de l'MX12

Fork of test_carteAToutFaire_PR by CRAC Team

Committer:
ClementBreteau
Date:
Thu May 11 11:49:50 2017 +0000
Revision:
1:f3f702086a30
test de la carte ? tout faire du petit robot

Who changed what in which revision?

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