Publishing for Biomimetics.
Fork of mbed-rpc by
rpc.h@0:efe8172b4113, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:23:59 2012 +0000
- Revision:
- 0:efe8172b4113
- Child:
- 1:6919289a5946
mbed RPC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:efe8172b4113 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:efe8172b4113 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:efe8172b4113 | 3 | * |
mbed_official | 0:efe8172b4113 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:efe8172b4113 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:efe8172b4113 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:efe8172b4113 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:efe8172b4113 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:efe8172b4113 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:efe8172b4113 | 10 | * |
mbed_official | 0:efe8172b4113 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:efe8172b4113 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:efe8172b4113 | 13 | * |
mbed_official | 0:efe8172b4113 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:efe8172b4113 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:efe8172b4113 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:efe8172b4113 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:efe8172b4113 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:efe8172b4113 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:efe8172b4113 | 20 | * SOFTWARE. |
mbed_official | 0:efe8172b4113 | 21 | */ |
mbed_official | 0:efe8172b4113 | 22 | #ifndef RPC_H |
mbed_official | 0:efe8172b4113 | 23 | #define RPC_H |
mbed_official | 0:efe8172b4113 | 24 | |
mbed_official | 0:efe8172b4113 | 25 | #include "mbed.h" |
mbed_official | 0:efe8172b4113 | 26 | #include "Arguments.h" |
mbed_official | 0:efe8172b4113 | 27 | |
mbed_official | 0:efe8172b4113 | 28 | namespace mbed { |
mbed_official | 0:efe8172b4113 | 29 | |
mbed_official | 0:efe8172b4113 | 30 | #define RPC_MAX_STRING 128 |
mbed_official | 0:efe8172b4113 | 31 | |
mbed_official | 0:efe8172b4113 | 32 | struct rpc_function { |
mbed_official | 0:efe8172b4113 | 33 | const char *name; |
mbed_official | 0:efe8172b4113 | 34 | void (*function_caller)(Arguments*, Reply*); |
mbed_official | 0:efe8172b4113 | 35 | }; |
mbed_official | 0:efe8172b4113 | 36 | |
mbed_official | 0:efe8172b4113 | 37 | struct rpc_class { |
mbed_official | 0:efe8172b4113 | 38 | const char *name; |
mbed_official | 0:efe8172b4113 | 39 | const rpc_function *static_functions; |
mbed_official | 0:efe8172b4113 | 40 | struct rpc_class *next; |
mbed_official | 0:efe8172b4113 | 41 | }; |
mbed_official | 0:efe8172b4113 | 42 | |
mbed_official | 0:efe8172b4113 | 43 | /* Class RPC |
mbed_official | 0:efe8172b4113 | 44 | * The RPC class for most things |
mbed_official | 0:efe8172b4113 | 45 | */ |
mbed_official | 0:efe8172b4113 | 46 | class RPC { |
mbed_official | 0:efe8172b4113 | 47 | |
mbed_official | 0:efe8172b4113 | 48 | public: |
mbed_official | 0:efe8172b4113 | 49 | |
mbed_official | 0:efe8172b4113 | 50 | RPC(const char *name = NULL); |
mbed_official | 0:efe8172b4113 | 51 | |
mbed_official | 0:efe8172b4113 | 52 | virtual ~RPC(); |
mbed_official | 0:efe8172b4113 | 53 | |
mbed_official | 0:efe8172b4113 | 54 | /* Function get_rpc_methods |
mbed_official | 0:efe8172b4113 | 55 | * Returns a pointer to an array describing the rpc methods |
mbed_official | 0:efe8172b4113 | 56 | * supported by this object, terminated by either |
mbed_official | 0:efe8172b4113 | 57 | * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass). |
mbed_official | 0:efe8172b4113 | 58 | * |
mbed_official | 0:efe8172b4113 | 59 | * Example |
mbed_official | 0:efe8172b4113 | 60 | * > class Example : public RPC { |
mbed_official | 0:efe8172b4113 | 61 | * > int foo(int a, int b) { return a + b; } |
mbed_official | 0:efe8172b4113 | 62 | * > virtual const struct rpc_method *get_rpc_methods() { |
mbed_official | 0:efe8172b4113 | 63 | * > static const rpc_method rpc_methods[] = { |
mbed_official | 0:efe8172b4113 | 64 | * > { "foo", generic_caller<int, Example, int, int, &Example::foo> }, |
mbed_official | 0:efe8172b4113 | 65 | * > RPC_METHOD_SUPER(RPC) |
mbed_official | 0:efe8172b4113 | 66 | * > }; |
mbed_official | 0:efe8172b4113 | 67 | * > return rpc_methods; |
mbed_official | 0:efe8172b4113 | 68 | * > } |
mbed_official | 0:efe8172b4113 | 69 | * > }; |
mbed_official | 0:efe8172b4113 | 70 | */ |
mbed_official | 0:efe8172b4113 | 71 | virtual const struct rpc_method *get_rpc_methods(); |
mbed_official | 0:efe8172b4113 | 72 | |
mbed_official | 0:efe8172b4113 | 73 | static bool call(const char *buf, char *result); |
mbed_official | 0:efe8172b4113 | 74 | |
mbed_official | 0:efe8172b4113 | 75 | /* Function lookup |
mbed_official | 0:efe8172b4113 | 76 | * Lookup and return the object that has the given name. |
mbed_official | 0:efe8172b4113 | 77 | * |
mbed_official | 0:efe8172b4113 | 78 | * Variables |
mbed_official | 0:efe8172b4113 | 79 | * name - the name to lookup. |
mbed_official | 0:efe8172b4113 | 80 | */ |
mbed_official | 0:efe8172b4113 | 81 | static RPC *lookup(const char *name); |
mbed_official | 0:efe8172b4113 | 82 | |
mbed_official | 0:efe8172b4113 | 83 | protected: |
mbed_official | 0:efe8172b4113 | 84 | static RPC *_head; |
mbed_official | 0:efe8172b4113 | 85 | RPC *_next; |
mbed_official | 0:efe8172b4113 | 86 | char *_name; |
mbed_official | 0:efe8172b4113 | 87 | bool _from_construct; |
mbed_official | 0:efe8172b4113 | 88 | |
mbed_official | 0:efe8172b4113 | 89 | private: |
mbed_official | 0:efe8172b4113 | 90 | static rpc_class *_classes; |
mbed_official | 0:efe8172b4113 | 91 | |
mbed_official | 0:efe8172b4113 | 92 | static const rpc_function _RPC_funcs[]; |
mbed_official | 0:efe8172b4113 | 93 | static rpc_class _RPC_class; |
mbed_official | 0:efe8172b4113 | 94 | |
mbed_official | 0:efe8172b4113 | 95 | void delete_self(); |
mbed_official | 0:efe8172b4113 | 96 | static void list_objs(Arguments *args, Reply *result); |
mbed_official | 0:efe8172b4113 | 97 | static void clear(Arguments *args, Reply *result); |
mbed_official | 0:efe8172b4113 | 98 | |
mbed_official | 0:efe8172b4113 | 99 | public: |
mbed_official | 0:efe8172b4113 | 100 | /* Function add_rpc_class |
mbed_official | 0:efe8172b4113 | 101 | * Add the class to the list of classes which can have static |
mbed_official | 0:efe8172b4113 | 102 | * methods called via rpc (the static methods which can be called |
mbed_official | 0:efe8172b4113 | 103 | * are defined by that class' get_rpc_class() static method). |
mbed_official | 0:efe8172b4113 | 104 | */ |
mbed_official | 0:efe8172b4113 | 105 | template<class C> |
mbed_official | 0:efe8172b4113 | 106 | static void add_rpc_class() { |
mbed_official | 0:efe8172b4113 | 107 | rpc_class *c = C::get_rpc_class(); |
mbed_official | 0:efe8172b4113 | 108 | c->next = _classes; |
mbed_official | 0:efe8172b4113 | 109 | _classes = c; |
mbed_official | 0:efe8172b4113 | 110 | } |
mbed_official | 0:efe8172b4113 | 111 | |
mbed_official | 0:efe8172b4113 | 112 | template<class C> |
mbed_official | 0:efe8172b4113 | 113 | static const char *construct() { |
mbed_official | 0:efe8172b4113 | 114 | RPC *p = new C(); |
mbed_official | 0:efe8172b4113 | 115 | p->_from_construct = true; |
mbed_official | 0:efe8172b4113 | 116 | return p->_name; |
mbed_official | 0:efe8172b4113 | 117 | } |
mbed_official | 0:efe8172b4113 | 118 | |
mbed_official | 0:efe8172b4113 | 119 | template<class C, typename A1> |
mbed_official | 0:efe8172b4113 | 120 | static const char *construct(A1 arg1) { |
mbed_official | 0:efe8172b4113 | 121 | RPC *p = new C(arg1); |
mbed_official | 0:efe8172b4113 | 122 | p->_from_construct = true; |
mbed_official | 0:efe8172b4113 | 123 | return p->_name; |
mbed_official | 0:efe8172b4113 | 124 | } |
mbed_official | 0:efe8172b4113 | 125 | |
mbed_official | 0:efe8172b4113 | 126 | template<class C, typename A1, typename A2> |
mbed_official | 0:efe8172b4113 | 127 | static const char *construct(A1 arg1, A2 arg2) { |
mbed_official | 0:efe8172b4113 | 128 | RPC *p = new C(arg1, arg2); |
mbed_official | 0:efe8172b4113 | 129 | p->_from_construct = true; |
mbed_official | 0:efe8172b4113 | 130 | return p->_name; |
mbed_official | 0:efe8172b4113 | 131 | } |
mbed_official | 0:efe8172b4113 | 132 | |
mbed_official | 0:efe8172b4113 | 133 | template<class C, typename A1, typename A2, typename A3> |
mbed_official | 0:efe8172b4113 | 134 | static const char *construct(A1 arg1, A2 arg2, A3 arg3) { |
mbed_official | 0:efe8172b4113 | 135 | RPC *p = new C(arg1, arg2, arg3); |
mbed_official | 0:efe8172b4113 | 136 | p->_from_construct = true; |
mbed_official | 0:efe8172b4113 | 137 | return p->_name; |
mbed_official | 0:efe8172b4113 | 138 | } |
mbed_official | 0:efe8172b4113 | 139 | |
mbed_official | 0:efe8172b4113 | 140 | template<class C, typename A1, typename A2, typename A3, typename A4> |
mbed_official | 0:efe8172b4113 | 141 | static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { |
mbed_official | 0:efe8172b4113 | 142 | RPC *p = new C(arg1, arg2, arg3, arg4); |
mbed_official | 0:efe8172b4113 | 143 | p->_from_construct = true; |
mbed_official | 0:efe8172b4113 | 144 | return p->_name; |
mbed_official | 0:efe8172b4113 | 145 | } |
mbed_official | 0:efe8172b4113 | 146 | }; |
mbed_official | 0:efe8172b4113 | 147 | |
mbed_official | 0:efe8172b4113 | 148 | /* Macro MBED_OBJECT_NAME_MAX |
mbed_official | 0:efe8172b4113 | 149 | * The maximum size of object name (including terminating null byte) |
mbed_official | 0:efe8172b4113 | 150 | * that will be recognised when using fopen to open a FileLike |
mbed_official | 0:efe8172b4113 | 151 | * object, or when using the rpc function. |
mbed_official | 0:efe8172b4113 | 152 | */ |
mbed_official | 0:efe8172b4113 | 153 | #define MBED_OBJECT_NAME_MAX 32 |
mbed_official | 0:efe8172b4113 | 154 | |
mbed_official | 0:efe8172b4113 | 155 | /* Macro MBED_METHOD_NAME_MAX |
mbed_official | 0:efe8172b4113 | 156 | * The maximum size of rpc method name (including terminating null |
mbed_official | 0:efe8172b4113 | 157 | * byte) that will be recognised by the rpc function (in rpc.h). |
mbed_official | 0:efe8172b4113 | 158 | */ |
mbed_official | 0:efe8172b4113 | 159 | #define MBED_METHOD_NAME_MAX 32 |
mbed_official | 0:efe8172b4113 | 160 | |
mbed_official | 0:efe8172b4113 | 161 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 162 | */ |
mbed_official | 0:efe8172b4113 | 163 | template<class T, void(T::*member)(const char *, char *)> |
mbed_official | 0:efe8172b4113 | 164 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 165 | (static_cast<T*>(this_ptr)->*member)(arguments, result); |
mbed_official | 0:efe8172b4113 | 166 | } |
mbed_official | 0:efe8172b4113 | 167 | |
mbed_official | 0:efe8172b4113 | 168 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 169 | */ |
mbed_official | 0:efe8172b4113 | 170 | template<class T, void(T::*member)()> |
mbed_official | 0:efe8172b4113 | 171 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 172 | (static_cast<T*>(this_ptr)->*member)(); |
mbed_official | 0:efe8172b4113 | 173 | } |
mbed_official | 0:efe8172b4113 | 174 | |
mbed_official | 0:efe8172b4113 | 175 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 176 | */ |
mbed_official | 0:efe8172b4113 | 177 | template<class T, typename A1, void(T::*member)(A1)> |
mbed_official | 0:efe8172b4113 | 178 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 179 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 180 | |
mbed_official | 0:efe8172b4113 | 181 | (static_cast<T*>(this_ptr)->*member)(arg1); |
mbed_official | 0:efe8172b4113 | 182 | } |
mbed_official | 0:efe8172b4113 | 183 | |
mbed_official | 0:efe8172b4113 | 184 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 185 | */ |
mbed_official | 0:efe8172b4113 | 186 | template<class T, typename A1, typename A2, void(T::*member)(A1, A2)> |
mbed_official | 0:efe8172b4113 | 187 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 188 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 189 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 190 | |
mbed_official | 0:efe8172b4113 | 191 | (static_cast<T*>(this_ptr)->*member)(arg1, arg2); |
mbed_official | 0:efe8172b4113 | 192 | } |
mbed_official | 0:efe8172b4113 | 193 | |
mbed_official | 0:efe8172b4113 | 194 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 195 | */ |
mbed_official | 0:efe8172b4113 | 196 | template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)> |
mbed_official | 0:efe8172b4113 | 197 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 198 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 199 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 200 | A3 arg3 = arguments->getArg<A3>(); |
mbed_official | 0:efe8172b4113 | 201 | |
mbed_official | 0:efe8172b4113 | 202 | (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); |
mbed_official | 0:efe8172b4113 | 203 | } |
mbed_official | 0:efe8172b4113 | 204 | |
mbed_official | 0:efe8172b4113 | 205 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 206 | */ |
mbed_official | 0:efe8172b4113 | 207 | template<typename R, class T, R(T::*member)()> |
mbed_official | 0:efe8172b4113 | 208 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 209 | R res = (static_cast<T*>(this_ptr)->*member)(); |
mbed_official | 0:efe8172b4113 | 210 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 211 | } |
mbed_official | 0:efe8172b4113 | 212 | |
mbed_official | 0:efe8172b4113 | 213 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 214 | */ |
mbed_official | 0:efe8172b4113 | 215 | template<typename R, class T, typename A1, R(T::*member)(A1)> |
mbed_official | 0:efe8172b4113 | 216 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 217 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 218 | |
mbed_official | 0:efe8172b4113 | 219 | R res = (static_cast<T*>(this_ptr)->*member)(arg1); |
mbed_official | 0:efe8172b4113 | 220 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 221 | } |
mbed_official | 0:efe8172b4113 | 222 | |
mbed_official | 0:efe8172b4113 | 223 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 224 | */ |
mbed_official | 0:efe8172b4113 | 225 | template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)> |
mbed_official | 0:efe8172b4113 | 226 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 227 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 228 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 229 | |
mbed_official | 0:efe8172b4113 | 230 | R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2); |
mbed_official | 0:efe8172b4113 | 231 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 232 | } |
mbed_official | 0:efe8172b4113 | 233 | |
mbed_official | 0:efe8172b4113 | 234 | /* Function rpc_method_caller |
mbed_official | 0:efe8172b4113 | 235 | */ |
mbed_official | 0:efe8172b4113 | 236 | template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)> |
mbed_official | 0:efe8172b4113 | 237 | void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 238 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 239 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 240 | A3 arg3 = arguments->getArg<A3>(); |
mbed_official | 0:efe8172b4113 | 241 | |
mbed_official | 0:efe8172b4113 | 242 | R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); |
mbed_official | 0:efe8172b4113 | 243 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 244 | } |
mbed_official | 0:efe8172b4113 | 245 | |
mbed_official | 0:efe8172b4113 | 246 | /* Function rpc_function caller |
mbed_official | 0:efe8172b4113 | 247 | */ |
mbed_official | 0:efe8172b4113 | 248 | template<typename R, R(*func)()> |
mbed_official | 0:efe8172b4113 | 249 | void rpc_function_caller(Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 250 | R res = (*func)(); |
mbed_official | 0:efe8172b4113 | 251 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 252 | } |
mbed_official | 0:efe8172b4113 | 253 | |
mbed_official | 0:efe8172b4113 | 254 | /* Function rpc_function caller |
mbed_official | 0:efe8172b4113 | 255 | */ |
mbed_official | 0:efe8172b4113 | 256 | template<typename R, typename A1, R(*func)(A1)> |
mbed_official | 0:efe8172b4113 | 257 | void rpc_function_caller(Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 258 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 259 | R res = (*func)(arg1); |
mbed_official | 0:efe8172b4113 | 260 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 261 | } |
mbed_official | 0:efe8172b4113 | 262 | |
mbed_official | 0:efe8172b4113 | 263 | /* Function rpc_function caller |
mbed_official | 0:efe8172b4113 | 264 | */ |
mbed_official | 0:efe8172b4113 | 265 | template<typename R, typename A1, typename A2, R(*func)(A1, A2)> |
mbed_official | 0:efe8172b4113 | 266 | void rpc_function_caller(Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 267 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 268 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 269 | |
mbed_official | 0:efe8172b4113 | 270 | R res = (*func)(arg1, arg2); |
mbed_official | 0:efe8172b4113 | 271 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 272 | } |
mbed_official | 0:efe8172b4113 | 273 | |
mbed_official | 0:efe8172b4113 | 274 | /* Function rpc_function caller |
mbed_official | 0:efe8172b4113 | 275 | */ |
mbed_official | 0:efe8172b4113 | 276 | template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)> |
mbed_official | 0:efe8172b4113 | 277 | void rpc_function_caller(Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 278 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 279 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 280 | A3 arg3 = arguments->getArg<A3>(); |
mbed_official | 0:efe8172b4113 | 281 | |
mbed_official | 0:efe8172b4113 | 282 | R res = (*func)(arg1, arg2, arg3); |
mbed_official | 0:efe8172b4113 | 283 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 284 | } |
mbed_official | 0:efe8172b4113 | 285 | |
mbed_official | 0:efe8172b4113 | 286 | /* Function rpc_function caller |
mbed_official | 0:efe8172b4113 | 287 | */ |
mbed_official | 0:efe8172b4113 | 288 | template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)> |
mbed_official | 0:efe8172b4113 | 289 | void rpc_function_caller(Arguments *arguments, Reply *result) { |
mbed_official | 0:efe8172b4113 | 290 | A1 arg1 = arguments->getArg<A1>(); |
mbed_official | 0:efe8172b4113 | 291 | A2 arg2 = arguments->getArg<A2>(); |
mbed_official | 0:efe8172b4113 | 292 | A3 arg3 = arguments->getArg<A3>(); |
mbed_official | 0:efe8172b4113 | 293 | A4 arg4 = arguments->getArg<A4>(); |
mbed_official | 0:efe8172b4113 | 294 | |
mbed_official | 0:efe8172b4113 | 295 | R res = (*func)(arg1, arg2, arg3, arg4); |
mbed_official | 0:efe8172b4113 | 296 | result->putData<R>(res); |
mbed_official | 0:efe8172b4113 | 297 | } |
mbed_official | 0:efe8172b4113 | 298 | |
mbed_official | 0:efe8172b4113 | 299 | struct rpc_method { |
mbed_official | 0:efe8172b4113 | 300 | const char *name; |
mbed_official | 0:efe8172b4113 | 301 | typedef void (*method_caller_t)(RPC*, Arguments*, Reply*); |
mbed_official | 0:efe8172b4113 | 302 | typedef const struct rpc_method *(*super_t)(RPC*); |
mbed_official | 0:efe8172b4113 | 303 | union { |
mbed_official | 0:efe8172b4113 | 304 | method_caller_t method_caller; |
mbed_official | 0:efe8172b4113 | 305 | super_t super; |
mbed_official | 0:efe8172b4113 | 306 | }; |
mbed_official | 0:efe8172b4113 | 307 | }; |
mbed_official | 0:efe8172b4113 | 308 | |
mbed_official | 0:efe8172b4113 | 309 | template<class C> |
mbed_official | 0:efe8172b4113 | 310 | const struct rpc_method *rpc_super(RPC *this_ptr) { |
mbed_official | 0:efe8172b4113 | 311 | return static_cast<C*>(this_ptr)->C::get_rpc_methods(); |
mbed_official | 0:efe8172b4113 | 312 | } |
mbed_official | 0:efe8172b4113 | 313 | |
mbed_official | 0:efe8172b4113 | 314 | #define RPC_METHOD_END { NULL, NULL } |
mbed_official | 0:efe8172b4113 | 315 | #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> } |
mbed_official | 0:efe8172b4113 | 316 | |
mbed_official | 0:efe8172b4113 | 317 | } // namespace mbed |
mbed_official | 0:efe8172b4113 | 318 | |
mbed_official | 0:efe8172b4113 | 319 | #endif |