HTTP RPC Server mit vordefinierten Objekten

Dependencies:   EthernetInterface HttpServer Servo mbed-rtos mbed

Fork of RPCHTTPServerVariable by th.iotkit2.ch

Mittels RPCVariable lassen sich lokale Variablen setzen. Diese Variablen können gesetzt write oder gelesen read werden.

Mittels Ticker u.ä. Varianten lassen sich damit auch Objektwerte setzen, welche von RPC nicht unterstützt werden, z.B. Servo's.

Client

Wert setzen: http://<IP-Adresse mbed>/rpc/servo2/write+0.5

Committer:
stefan1691
Date:
Sun Mar 20 07:39:09 2016 +0000
Revision:
17:752e17b9aab4
Parent:
14:3835863bc412
https statt http verhindert Fehler

Who changed what in which revision?

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