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