Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rpc.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef RPC_H 00017 #define RPC_H 00018 00019 #include "mbed.h" 00020 #include "Arguments.h" 00021 00022 namespace mbed { 00023 00024 #define RPC_MAX_STRING 128 00025 00026 struct rpc_function { 00027 const char *name; 00028 void (*function_caller)(Arguments*, Reply*); 00029 }; 00030 00031 struct rpc_class { 00032 const char *name; 00033 const rpc_function *static_functions; 00034 struct rpc_class *next; 00035 }; 00036 00037 /* Class RPC 00038 * The RPC class for most things 00039 */ 00040 class RPC { 00041 00042 public: 00043 00044 RPC(const char *name = NULL); 00045 00046 virtual ~RPC(); 00047 00048 /* Function get_rpc_methods 00049 * Returns a pointer to an array describing the rpc methods 00050 * supported by this object, terminated by either 00051 * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass). 00052 * 00053 * Example 00054 * > class Example : public RPC { 00055 * > int foo(int a, int b) { return a + b; } 00056 * > virtual const struct rpc_method *get_rpc_methods() { 00057 * > static const rpc_method rpc_methods[] = { 00058 * > { "foo", generic_caller<int, Example, int, int, &Example::foo> }, 00059 * > RPC_METHOD_SUPER(RPC) 00060 * > }; 00061 * > return rpc_methods; 00062 * > } 00063 * > }; 00064 */ 00065 virtual const struct rpc_method *get_rpc_methods(); 00066 00067 static bool call(const char *buf, char *result); 00068 00069 /* Function lookup 00070 * Lookup and return the object that has the given name. 00071 * 00072 * Variables 00073 * name - the name to lookup. 00074 */ 00075 static RPC *lookup(const char *name); 00076 00077 protected: 00078 static RPC *_head; 00079 RPC *_next; 00080 char *_name; 00081 bool _from_construct; 00082 00083 private: 00084 static rpc_class *_classes; 00085 00086 static const rpc_function _RPC_funcs[]; 00087 static rpc_class _RPC_class; 00088 00089 void delete_self(); 00090 static void list_objs(Arguments *args, Reply *result); 00091 static void clear(Arguments *args, Reply *result); 00092 00093 public: 00094 /* Function add_rpc_class 00095 * Add the class to the list of classes which can have static 00096 * methods called via rpc (the static methods which can be called 00097 * are defined by that class' get_rpc_class() static method). 00098 */ 00099 template<class C> 00100 static void add_rpc_class() { 00101 rpc_class *c = C::get_rpc_class(); 00102 c->next = _classes; 00103 _classes = c; 00104 } 00105 00106 template<class C> 00107 static const char *construct() { 00108 RPC *p = new C(); 00109 p->_from_construct = true; 00110 return p->_name; 00111 } 00112 00113 template<class C, typename A1> 00114 static const char *construct(A1 arg1) { 00115 RPC *p = new C(arg1); 00116 p->_from_construct = true; 00117 return p->_name; 00118 } 00119 00120 template<class C, typename A1, typename A2> 00121 static const char *construct(A1 arg1, A2 arg2) { 00122 RPC *p = new C(arg1, arg2); 00123 p->_from_construct = true; 00124 return p->_name; 00125 } 00126 00127 template<class C, typename A1, typename A2, typename A3> 00128 static const char *construct(A1 arg1, A2 arg2, A3 arg3) { 00129 RPC *p = new C(arg1, arg2, arg3); 00130 p->_from_construct = true; 00131 return p->_name; 00132 } 00133 00134 template<class C, typename A1, typename A2, typename A3, typename A4> 00135 static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { 00136 RPC *p = new C(arg1, arg2, arg3, arg4); 00137 p->_from_construct = true; 00138 return p->_name; 00139 } 00140 }; 00141 00142 /* Macro MBED_OBJECT_NAME_MAX 00143 * The maximum size of object name (including terminating null byte) 00144 * that will be recognised when using fopen to open a FileLike 00145 * object, or when using the rpc function. 00146 */ 00147 #define MBED_OBJECT_NAME_MAX 32 00148 00149 /* Macro MBED_METHOD_NAME_MAX 00150 * The maximum size of rpc method name (including terminating null 00151 * byte) that will be recognised by the rpc function (in rpc.h). 00152 */ 00153 #define MBED_METHOD_NAME_MAX 32 00154 00155 /* Function rpc_method_caller 00156 */ 00157 template<class T, void(T::*member)(const char *, char *)> 00158 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00159 (static_cast<T*>(this_ptr)->*member)(arguments, result); 00160 } 00161 00162 /* Function rpc_method_caller 00163 */ 00164 template<class T, void(T::*member)()> 00165 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00166 (static_cast<T*>(this_ptr)->*member)(); 00167 } 00168 00169 /* Function rpc_method_caller 00170 */ 00171 template<class T, typename A1, void(T::*member)(A1)> 00172 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00173 A1 arg1 = arguments->getArg<A1>(); 00174 00175 (static_cast<T*>(this_ptr)->*member)(arg1); 00176 } 00177 00178 /* Function rpc_method_caller 00179 */ 00180 template<class T, typename A1, typename A2, void(T::*member)(A1, A2)> 00181 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00182 A1 arg1 = arguments->getArg<A1>(); 00183 A2 arg2 = arguments->getArg<A2>(); 00184 00185 (static_cast<T*>(this_ptr)->*member)(arg1, arg2); 00186 } 00187 00188 /* Function rpc_method_caller 00189 */ 00190 template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)> 00191 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00192 A1 arg1 = arguments->getArg<A1>(); 00193 A2 arg2 = arguments->getArg<A2>(); 00194 A3 arg3 = arguments->getArg<A3>(); 00195 00196 (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); 00197 } 00198 00199 /* Function rpc_method_caller 00200 */ 00201 template<typename R, class T, R(T::*member)()> 00202 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00203 R res = (static_cast<T*>(this_ptr)->*member)(); 00204 result->putData<R>(res); 00205 } 00206 00207 /* Function rpc_method_caller 00208 */ 00209 template<typename R, class T, typename A1, R(T::*member)(A1)> 00210 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00211 A1 arg1 = arguments->getArg<A1>(); 00212 00213 R res = (static_cast<T*>(this_ptr)->*member)(arg1); 00214 result->putData<R>(res); 00215 } 00216 00217 /* Function rpc_method_caller 00218 */ 00219 template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)> 00220 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00221 A1 arg1 = arguments->getArg<A1>(); 00222 A2 arg2 = arguments->getArg<A2>(); 00223 00224 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2); 00225 result->putData<R>(res); 00226 } 00227 00228 /* Function rpc_method_caller 00229 */ 00230 template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)> 00231 void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) { 00232 A1 arg1 = arguments->getArg<A1>(); 00233 A2 arg2 = arguments->getArg<A2>(); 00234 A3 arg3 = arguments->getArg<A3>(); 00235 00236 R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3); 00237 result->putData<R>(res); 00238 } 00239 00240 /* Function rpc_function caller 00241 */ 00242 template<typename R, R(*func)()> 00243 void rpc_function_caller(Arguments *arguments, Reply *result) { 00244 R res = (*func)(); 00245 result->putData<R>(res); 00246 } 00247 00248 /* Function rpc_function caller 00249 */ 00250 template<typename R, typename A1, R(*func)(A1)> 00251 void rpc_function_caller(Arguments *arguments, Reply *result) { 00252 A1 arg1 = arguments->getArg<A1>(); 00253 R res = (*func)(arg1); 00254 result->putData<R>(res); 00255 } 00256 00257 /* Function rpc_function caller 00258 */ 00259 template<typename R, typename A1, typename A2, R(*func)(A1, A2)> 00260 void rpc_function_caller(Arguments *arguments, Reply *result) { 00261 A1 arg1 = arguments->getArg<A1>(); 00262 A2 arg2 = arguments->getArg<A2>(); 00263 00264 R res = (*func)(arg1, arg2); 00265 result->putData<R>(res); 00266 } 00267 00268 /* Function rpc_function caller 00269 */ 00270 template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)> 00271 void rpc_function_caller(Arguments *arguments, Reply *result) { 00272 A1 arg1 = arguments->getArg<A1>(); 00273 A2 arg2 = arguments->getArg<A2>(); 00274 A3 arg3 = arguments->getArg<A3>(); 00275 00276 R res = (*func)(arg1, arg2, arg3); 00277 result->putData<R>(res); 00278 } 00279 00280 /* Function rpc_function caller 00281 */ 00282 template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)> 00283 void rpc_function_caller(Arguments *arguments, Reply *result) { 00284 A1 arg1 = arguments->getArg<A1>(); 00285 A2 arg2 = arguments->getArg<A2>(); 00286 A3 arg3 = arguments->getArg<A3>(); 00287 A4 arg4 = arguments->getArg<A4>(); 00288 00289 R res = (*func)(arg1, arg2, arg3, arg4); 00290 result->putData<R>(res); 00291 } 00292 00293 struct rpc_method { 00294 const char *name; 00295 typedef void (*method_caller_t)(RPC*, Arguments*, Reply*); 00296 typedef const struct rpc_method *(*super_t)(RPC*); 00297 union { 00298 method_caller_t method_caller; 00299 super_t super; 00300 }; 00301 }; 00302 00303 template<class C> 00304 const struct rpc_method *rpc_super(RPC *this_ptr) { 00305 return static_cast<C*>(this_ptr)->C::get_rpc_methods(); 00306 } 00307 00308 #define RPC_METHOD_END { NULL, NULL } 00309 #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> } 00310 00311 } // namespace mbed 00312 00313 #endif
Generated on Tue Jul 12 2022 14:24:32 by
