Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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