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