mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #ifndef RPC_H
elessair 0:f269e3021894 17 #define RPC_H
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "mbed.h"
elessair 0:f269e3021894 20 #include "Arguments.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 namespace mbed {
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #define RPC_MAX_STRING 128
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 struct rpc_function {
elessair 0:f269e3021894 27 const char *name;
elessair 0:f269e3021894 28 void (*function_caller)(Arguments*, Reply*);
elessair 0:f269e3021894 29 };
elessair 0:f269e3021894 30
elessair 0:f269e3021894 31 struct rpc_class {
elessair 0:f269e3021894 32 const char *name;
elessair 0:f269e3021894 33 const rpc_function *static_functions;
elessair 0:f269e3021894 34 struct rpc_class *next;
elessair 0:f269e3021894 35 };
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 /* Class RPC
elessair 0:f269e3021894 38 * The RPC class for most things
elessair 0:f269e3021894 39 */
elessair 0:f269e3021894 40 class RPC {
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 public:
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 RPC(const char *name = NULL);
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 virtual ~RPC();
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 /* Function get_rpc_methods
elessair 0:f269e3021894 49 * Returns a pointer to an array describing the rpc methods
elessair 0:f269e3021894 50 * supported by this object, terminated by either
elessair 0:f269e3021894 51 * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).
elessair 0:f269e3021894 52 *
elessair 0:f269e3021894 53 * Example
elessair 0:f269e3021894 54 * > class Example : public RPC {
elessair 0:f269e3021894 55 * > int foo(int a, int b) { return a + b; }
elessair 0:f269e3021894 56 * > virtual const struct rpc_method *get_rpc_methods() {
elessair 0:f269e3021894 57 * > static const rpc_method rpc_methods[] = {
elessair 0:f269e3021894 58 * > { "foo", generic_caller<int, Example, int, int, &Example::foo> },
elessair 0:f269e3021894 59 * > RPC_METHOD_SUPER(RPC)
elessair 0:f269e3021894 60 * > };
elessair 0:f269e3021894 61 * > return rpc_methods;
elessair 0:f269e3021894 62 * > }
elessair 0:f269e3021894 63 * > };
elessair 0:f269e3021894 64 */
elessair 0:f269e3021894 65 virtual const struct rpc_method *get_rpc_methods();
elessair 0:f269e3021894 66
elessair 0:f269e3021894 67 static bool call(const char *buf, char *result);
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 /* Function lookup
elessair 0:f269e3021894 70 * Lookup and return the object that has the given name.
elessair 0:f269e3021894 71 *
elessair 0:f269e3021894 72 * Variables
elessair 0:f269e3021894 73 * name - the name to lookup.
elessair 0:f269e3021894 74 */
elessair 0:f269e3021894 75 static RPC *lookup(const char *name);
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 protected:
elessair 0:f269e3021894 78 static RPC *_head;
elessair 0:f269e3021894 79 RPC *_next;
elessair 0:f269e3021894 80 char *_name;
elessair 0:f269e3021894 81 bool _from_construct;
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 private:
elessair 0:f269e3021894 84 static rpc_class *_classes;
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 static const rpc_function _RPC_funcs[];
elessair 0:f269e3021894 87 static rpc_class _RPC_class;
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 void delete_self();
elessair 0:f269e3021894 90 static void list_objs(Arguments *args, Reply *result);
elessair 0:f269e3021894 91 static void clear(Arguments *args, Reply *result);
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 public:
elessair 0:f269e3021894 94 /* Function add_rpc_class
elessair 0:f269e3021894 95 * Add the class to the list of classes which can have static
elessair 0:f269e3021894 96 * methods called via rpc (the static methods which can be called
elessair 0:f269e3021894 97 * are defined by that class' get_rpc_class() static method).
elessair 0:f269e3021894 98 */
elessair 0:f269e3021894 99 template<class C>
elessair 0:f269e3021894 100 static void add_rpc_class() {
elessair 0:f269e3021894 101 rpc_class *c = C::get_rpc_class();
elessair 0:f269e3021894 102 c->next = _classes;
elessair 0:f269e3021894 103 _classes = c;
elessair 0:f269e3021894 104 }
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 template<class C>
elessair 0:f269e3021894 107 static const char *construct() {
elessair 0:f269e3021894 108 RPC *p = new C();
elessair 0:f269e3021894 109 p->_from_construct = true;
elessair 0:f269e3021894 110 return p->_name;
elessair 0:f269e3021894 111 }
elessair 0:f269e3021894 112
elessair 0:f269e3021894 113 template<class C, typename A1>
elessair 0:f269e3021894 114 static const char *construct(A1 arg1) {
elessair 0:f269e3021894 115 RPC *p = new C(arg1);
elessair 0:f269e3021894 116 p->_from_construct = true;
elessair 0:f269e3021894 117 return p->_name;
elessair 0:f269e3021894 118 }
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 template<class C, typename A1, typename A2>
elessair 0:f269e3021894 121 static const char *construct(A1 arg1, A2 arg2) {
elessair 0:f269e3021894 122 RPC *p = new C(arg1, arg2);
elessair 0:f269e3021894 123 p->_from_construct = true;
elessair 0:f269e3021894 124 return p->_name;
elessair 0:f269e3021894 125 }
elessair 0:f269e3021894 126
elessair 0:f269e3021894 127 template<class C, typename A1, typename A2, typename A3>
elessair 0:f269e3021894 128 static const char *construct(A1 arg1, A2 arg2, A3 arg3) {
elessair 0:f269e3021894 129 RPC *p = new C(arg1, arg2, arg3);
elessair 0:f269e3021894 130 p->_from_construct = true;
elessair 0:f269e3021894 131 return p->_name;
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134 template<class C, typename A1, typename A2, typename A3, typename A4>
elessair 0:f269e3021894 135 static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
elessair 0:f269e3021894 136 RPC *p = new C(arg1, arg2, arg3, arg4);
elessair 0:f269e3021894 137 p->_from_construct = true;
elessair 0:f269e3021894 138 return p->_name;
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140 };
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 /* Macro MBED_OBJECT_NAME_MAX
elessair 0:f269e3021894 143 * The maximum size of object name (including terminating null byte)
elessair 0:f269e3021894 144 * that will be recognised when using fopen to open a FileLike
elessair 0:f269e3021894 145 * object, or when using the rpc function.
elessair 0:f269e3021894 146 */
elessair 0:f269e3021894 147 #define MBED_OBJECT_NAME_MAX 32
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 /* Macro MBED_METHOD_NAME_MAX
elessair 0:f269e3021894 150 * The maximum size of rpc method name (including terminating null
elessair 0:f269e3021894 151 * byte) that will be recognised by the rpc function (in rpc.h).
elessair 0:f269e3021894 152 */
elessair 0:f269e3021894 153 #define MBED_METHOD_NAME_MAX 32
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 /* Function rpc_method_caller
elessair 0:f269e3021894 156 */
elessair 0:f269e3021894 157 template<class T, void(T::*member)(const char *, char *)>
elessair 0:f269e3021894 158 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 159 (static_cast<T*>(this_ptr)->*member)(arguments, result);
elessair 0:f269e3021894 160 }
elessair 0:f269e3021894 161
elessair 0:f269e3021894 162 /* Function rpc_method_caller
elessair 0:f269e3021894 163 */
elessair 0:f269e3021894 164 template<class T, void(T::*member)()>
elessair 0:f269e3021894 165 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 166 (static_cast<T*>(this_ptr)->*member)();
elessair 0:f269e3021894 167 }
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 /* Function rpc_method_caller
elessair 0:f269e3021894 170 */
elessair 0:f269e3021894 171 template<class T, typename A1, void(T::*member)(A1)>
elessair 0:f269e3021894 172 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 173 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 (static_cast<T*>(this_ptr)->*member)(arg1);
elessair 0:f269e3021894 176 }
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 /* Function rpc_method_caller
elessair 0:f269e3021894 179 */
elessair 0:f269e3021894 180 template<class T, typename A1, typename A2, void(T::*member)(A1, A2)>
elessair 0:f269e3021894 181 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 182 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 183 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 184
elessair 0:f269e3021894 185 (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 /* Function rpc_method_caller
elessair 0:f269e3021894 189 */
elessair 0:f269e3021894 190 template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)>
elessair 0:f269e3021894 191 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 192 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 193 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 194 A3 arg3 = arguments->getArg<A3>();
elessair 0:f269e3021894 195
elessair 0:f269e3021894 196 (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
elessair 0:f269e3021894 197 }
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 /* Function rpc_method_caller
elessair 0:f269e3021894 200 */
elessair 0:f269e3021894 201 template<typename R, class T, R(T::*member)()>
elessair 0:f269e3021894 202 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 203 R res = (static_cast<T*>(this_ptr)->*member)();
elessair 0:f269e3021894 204 result->putData<R>(res);
elessair 0:f269e3021894 205 }
elessair 0:f269e3021894 206
elessair 0:f269e3021894 207 /* Function rpc_method_caller
elessair 0:f269e3021894 208 */
elessair 0:f269e3021894 209 template<typename R, class T, typename A1, R(T::*member)(A1)>
elessair 0:f269e3021894 210 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 211 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 212
elessair 0:f269e3021894 213 R res = (static_cast<T*>(this_ptr)->*member)(arg1);
elessair 0:f269e3021894 214 result->putData<R>(res);
elessair 0:f269e3021894 215 }
elessair 0:f269e3021894 216
elessair 0:f269e3021894 217 /* Function rpc_method_caller
elessair 0:f269e3021894 218 */
elessair 0:f269e3021894 219 template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)>
elessair 0:f269e3021894 220 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 221 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 222 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
elessair 0:f269e3021894 225 result->putData<R>(res);
elessair 0:f269e3021894 226 }
elessair 0:f269e3021894 227
elessair 0:f269e3021894 228 /* Function rpc_method_caller
elessair 0:f269e3021894 229 */
elessair 0:f269e3021894 230 template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)>
elessair 0:f269e3021894 231 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 232 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 233 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 234 A3 arg3 = arguments->getArg<A3>();
elessair 0:f269e3021894 235
elessair 0:f269e3021894 236 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
elessair 0:f269e3021894 237 result->putData<R>(res);
elessair 0:f269e3021894 238 }
elessair 0:f269e3021894 239
elessair 0:f269e3021894 240 /* Function rpc_function caller
elessair 0:f269e3021894 241 */
elessair 0:f269e3021894 242 template<typename R, R(*func)()>
elessair 0:f269e3021894 243 void rpc_function_caller(Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 244 R res = (*func)();
elessair 0:f269e3021894 245 result->putData<R>(res);
elessair 0:f269e3021894 246 }
elessair 0:f269e3021894 247
elessair 0:f269e3021894 248 /* Function rpc_function caller
elessair 0:f269e3021894 249 */
elessair 0:f269e3021894 250 template<typename R, typename A1, R(*func)(A1)>
elessair 0:f269e3021894 251 void rpc_function_caller(Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 252 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 253 R res = (*func)(arg1);
elessair 0:f269e3021894 254 result->putData<R>(res);
elessair 0:f269e3021894 255 }
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 /* Function rpc_function caller
elessair 0:f269e3021894 258 */
elessair 0:f269e3021894 259 template<typename R, typename A1, typename A2, R(*func)(A1, A2)>
elessair 0:f269e3021894 260 void rpc_function_caller(Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 261 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 262 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 R res = (*func)(arg1, arg2);
elessair 0:f269e3021894 265 result->putData<R>(res);
elessair 0:f269e3021894 266 }
elessair 0:f269e3021894 267
elessair 0:f269e3021894 268 /* Function rpc_function caller
elessair 0:f269e3021894 269 */
elessair 0:f269e3021894 270 template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)>
elessair 0:f269e3021894 271 void rpc_function_caller(Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 272 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 273 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 274 A3 arg3 = arguments->getArg<A3>();
elessair 0:f269e3021894 275
elessair 0:f269e3021894 276 R res = (*func)(arg1, arg2, arg3);
elessair 0:f269e3021894 277 result->putData<R>(res);
elessair 0:f269e3021894 278 }
elessair 0:f269e3021894 279
elessair 0:f269e3021894 280 /* Function rpc_function caller
elessair 0:f269e3021894 281 */
elessair 0:f269e3021894 282 template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)>
elessair 0:f269e3021894 283 void rpc_function_caller(Arguments *arguments, Reply *result) {
elessair 0:f269e3021894 284 A1 arg1 = arguments->getArg<A1>();
elessair 0:f269e3021894 285 A2 arg2 = arguments->getArg<A2>();
elessair 0:f269e3021894 286 A3 arg3 = arguments->getArg<A3>();
elessair 0:f269e3021894 287 A4 arg4 = arguments->getArg<A4>();
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 R res = (*func)(arg1, arg2, arg3, arg4);
elessair 0:f269e3021894 290 result->putData<R>(res);
elessair 0:f269e3021894 291 }
elessair 0:f269e3021894 292
elessair 0:f269e3021894 293 struct rpc_method {
elessair 0:f269e3021894 294 const char *name;
elessair 0:f269e3021894 295 typedef void (*method_caller_t)(RPC*, Arguments*, Reply*);
elessair 0:f269e3021894 296 typedef const struct rpc_method *(*super_t)(RPC*);
elessair 0:f269e3021894 297 union {
elessair 0:f269e3021894 298 method_caller_t method_caller;
elessair 0:f269e3021894 299 super_t super;
elessair 0:f269e3021894 300 };
elessair 0:f269e3021894 301 };
elessair 0:f269e3021894 302
elessair 0:f269e3021894 303 template<class C>
elessair 0:f269e3021894 304 const struct rpc_method *rpc_super(RPC *this_ptr) {
elessair 0:f269e3021894 305 return static_cast<C*>(this_ptr)->C::get_rpc_methods();
elessair 0:f269e3021894 306 }
elessair 0:f269e3021894 307
elessair 0:f269e3021894 308 #define RPC_METHOD_END { NULL, NULL }
elessair 0:f269e3021894 309 #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> }
elessair 0:f269e3021894 310
elessair 0:f269e3021894 311 } // namespace mbed
elessair 0:f269e3021894 312
elessair 0:f269e3021894 313 #endif