Lab 1 Program C

Dependents:   Lab1C

Fork of mbed by -deleted-

Committer:
simon.ford@mbed.co.uk
Date:
Thu Jan 22 18:32:40 2009 +0000
Revision:
5:62573be585e9
Parent:
4:5d1359a283bc
Child:
6:3fd6a337c7cc
* Added initial RPC release
* Added RTC and helper functions
* Added read_u16()/write_u16() to AnalogIn/Out
* Ticker/Timeout timing fixed!
* mbedinfo() helper added
* error() and printf() added to replace DEBUG() and ERROR()
* DigitalIn supports methods on rise/fall
* SPI and Serial support NC
* LED1-4 also map to 1-4
* Timer object reset fixed
* SPI uses single mode
* SPI3 added

Who changed what in which revision?

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