1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "Arguments.h"
24 #define RPC_MAX_STRING 128
28 void (*function_caller)(Arguments*, Reply*);
33 const rpc_function *static_functions;
34 struct rpc_class *next;
38 * The RPC class for most things
44 RPC(const char *name = NULL);
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).
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)
61 * > return rpc_methods;
65 virtual const struct rpc_method *get_rpc_methods();
67 static bool call(const char *buf, char *result);
70 * Lookup and return the object that has the given name.
73 * name - the name to lookup.
75 static RPC *lookup(const char *name);
84 static rpc_class *_classes;
86 static const rpc_function _RPC_funcs[];
87 static rpc_class _RPC_class;
90 static void list_objs(Arguments *args, Reply *result);
91 static void clear(Arguments *args, Reply *result);
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).
100 static void add_rpc_class() {
101 rpc_class *c = C::get_rpc_class();
107 static const char *construct() {
109 p->_from_construct = true;
113 template<class C, typename A1>
114 static const char *construct(A1 arg1) {
115 RPC *p = new C(arg1);
116 p->_from_construct = true;
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;
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;
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;
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.
147 #define MBED_OBJECT_NAME_MAX 32
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).
153 #define MBED_METHOD_NAME_MAX 32
155 /* Function rpc_method_caller
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);
162 /* Function rpc_method_caller
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)();
169 /* Function rpc_method_caller
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>();
175 (static_cast<T*>(this_ptr)->*member)(arg1);
178 /* Function rpc_method_caller
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>();
185 (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
188 /* Function rpc_method_caller
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>();
196 (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
199 /* Function rpc_method_caller
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);
207 /* Function rpc_method_caller
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>();
213 R res = (static_cast<T*>(this_ptr)->*member)(arg1);
214 result->putData<R>(res);
217 /* Function rpc_method_caller
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>();
224 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
225 result->putData<R>(res);
228 /* Function rpc_method_caller
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>();
236 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
237 result->putData<R>(res);
240 /* Function rpc_function caller
242 template<typename R, R(*func)()>
243 void rpc_function_caller(Arguments *arguments, Reply *result) {
245 result->putData<R>(res);
248 /* Function rpc_function caller
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);
257 /* Function rpc_function caller
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>();
264 R res = (*func)(arg1, arg2);
265 result->putData<R>(res);
268 /* Function rpc_function caller
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>();
276 R res = (*func)(arg1, arg2, arg3);
277 result->putData<R>(res);
280 /* Function rpc_function caller
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>();
289 R res = (*func)(arg1, arg2, arg3, arg4);
290 result->putData<R>(res);
295 typedef void (*method_caller_t)(RPC*, Arguments*, Reply*);
296 typedef const struct rpc_method *(*super_t)(RPC*);
298 method_caller_t method_caller;
304 const struct rpc_method *rpc_super(RPC *this_ptr) {
305 return static_cast<C*>(this_ptr)->C::get_rpc_methods();
308 #define RPC_METHOD_END { NULL, NULL }
309 #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> }