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