Minimalistischer Remote Procedure Call (RPC) HTTP Server V2

Dependencies:   EthernetInterface HttpServer OLEDDisplay mbed-rtos mbed

Fork of RPCHTTPServerSimple by smd.iotkit2.ch

Committer:
stefan1691
Date:
Sat Sep 10 07:01:44 2016 +0000
Revision:
14:ab5b22ac6b42
Parent:
11:43e28c85fd75
Minimalistischer Remote Procedure Call (RPC) HTTP Server V2

Who changed what in which revision?

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