nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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