Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpc.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "mbed.h"
  19. #include "Arguments.h"
  20. namespace mbed {
  21. #define RPC_MAX_STRING 128
  22. struct rpc_function {
  23. const char *name;
  24. void (*function_caller)(Arguments*, Reply*);
  25. };
  26. struct rpc_class {
  27. const char *name;
  28. const rpc_function *static_functions;
  29. struct rpc_class *next;
  30. };
  31. /* Class RPC
  32. * The RPC class for most things
  33. */
  34. class RPC {
  35. public:
  36. RPC(const char *name = NULL);
  37. virtual ~RPC();
  38. /* Function get_rpc_methods
  39. * Returns a pointer to an array describing the rpc methods
  40. * supported by this object, terminated by either
  41. * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).
  42. *
  43. * Example
  44. * > class Example : public RPC {
  45. * > int foo(int a, int b) { return a + b; }
  46. * > virtual const struct rpc_method *get_rpc_methods() {
  47. * > static const rpc_method rpc_methods[] = {
  48. * > { "foo", generic_caller<int, Example, int, int, &Example::foo> },
  49. * > RPC_METHOD_SUPER(RPC)
  50. * > };
  51. * > return rpc_methods;
  52. * > }
  53. * > };
  54. */
  55. virtual const struct rpc_method *get_rpc_methods();
  56. static bool call(const char *buf, char *result);
  57. /* Function lookup
  58. * Lookup and return the object that has the given name.
  59. *
  60. * Variables
  61. * name - the name to lookup.
  62. */
  63. static RPC *lookup(const char *name);
  64. protected:
  65. static RPC *_head;
  66. RPC *_next;
  67. char *_name;
  68. bool _from_construct;
  69. private:
  70. static rpc_class *_classes;
  71. static const rpc_function _RPC_funcs[];
  72. static rpc_class _RPC_class;
  73. void delete_self();
  74. static void list_objs(Arguments *args, Reply *result);
  75. static void clear(Arguments *args, Reply *result);
  76. public:
  77. /* Function add_rpc_class
  78. * Add the class to the list of classes which can have static
  79. * methods called via rpc (the static methods which can be called
  80. * are defined by that class' get_rpc_class() static method).
  81. */
  82. template<class C>
  83. static void add_rpc_class() {
  84. rpc_class *c = C::get_rpc_class();
  85. c->next = _classes;
  86. _classes = c;
  87. }
  88. template<class C>
  89. static const char *construct() {
  90. RPC *p = new C();
  91. p->_from_construct = true;
  92. return p->_name;
  93. }
  94. template<class C, typename A1>
  95. static const char *construct(A1 arg1) {
  96. RPC *p = new C(arg1);
  97. p->_from_construct = true;
  98. return p->_name;
  99. }
  100. template<class C, typename A1, typename A2>
  101. static const char *construct(A1 arg1, A2 arg2) {
  102. RPC *p = new C(arg1, arg2);
  103. p->_from_construct = true;
  104. return p->_name;
  105. }
  106. template<class C, typename A1, typename A2, typename A3>
  107. static const char *construct(A1 arg1, A2 arg2, A3 arg3) {
  108. RPC *p = new C(arg1, arg2, arg3);
  109. p->_from_construct = true;
  110. return p->_name;
  111. }
  112. template<class C, typename A1, typename A2, typename A3, typename A4>
  113. static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
  114. RPC *p = new C(arg1, arg2, arg3, arg4);
  115. p->_from_construct = true;
  116. return p->_name;
  117. }
  118. };
  119. /* Macro MBED_OBJECT_NAME_MAX
  120. * The maximum size of object name (including terminating null byte)
  121. * that will be recognised when using fopen to open a FileLike
  122. * object, or when using the rpc function.
  123. */
  124. #define MBED_OBJECT_NAME_MAX 32
  125. /* Macro MBED_METHOD_NAME_MAX
  126. * The maximum size of rpc method name (including terminating null
  127. * byte) that will be recognised by the rpc function (in rpc.h).
  128. */
  129. #define MBED_METHOD_NAME_MAX 32
  130. /* Function rpc_method_caller
  131. */
  132. template<class T, void(T::*member)(const char *, char *)>
  133. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  134. (static_cast<T*>(this_ptr)->*member)(arguments, result);
  135. }
  136. /* Function rpc_method_caller
  137. */
  138. template<class T, void(T::*member)()>
  139. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  140. (static_cast<T*>(this_ptr)->*member)();
  141. }
  142. /* Function rpc_method_caller
  143. */
  144. template<class T, typename A1, void(T::*member)(A1)>
  145. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  146. A1 arg1 = arguments->getArg<A1>();
  147. (static_cast<T*>(this_ptr)->*member)(arg1);
  148. }
  149. /* Function rpc_method_caller
  150. */
  151. template<class T, typename A1, typename A2, void(T::*member)(A1, A2)>
  152. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  153. A1 arg1 = arguments->getArg<A1>();
  154. A2 arg2 = arguments->getArg<A2>();
  155. (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
  156. }
  157. /* Function rpc_method_caller
  158. */
  159. template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)>
  160. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  161. A1 arg1 = arguments->getArg<A1>();
  162. A2 arg2 = arguments->getArg<A2>();
  163. A3 arg3 = arguments->getArg<A3>();
  164. (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
  165. }
  166. /* Function rpc_method_caller
  167. */
  168. template<typename R, class T, R(T::*member)()>
  169. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  170. R res = (static_cast<T*>(this_ptr)->*member)();
  171. result->putData<R>(res);
  172. }
  173. /* Function rpc_method_caller
  174. */
  175. template<typename R, class T, typename A1, R(T::*member)(A1)>
  176. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  177. A1 arg1 = arguments->getArg<A1>();
  178. R res = (static_cast<T*>(this_ptr)->*member)(arg1);
  179. result->putData<R>(res);
  180. }
  181. /* Function rpc_method_caller
  182. */
  183. template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)>
  184. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  185. A1 arg1 = arguments->getArg<A1>();
  186. A2 arg2 = arguments->getArg<A2>();
  187. R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
  188. result->putData<R>(res);
  189. }
  190. /* Function rpc_method_caller
  191. */
  192. template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)>
  193. void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
  194. A1 arg1 = arguments->getArg<A1>();
  195. A2 arg2 = arguments->getArg<A2>();
  196. A3 arg3 = arguments->getArg<A3>();
  197. R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
  198. result->putData<R>(res);
  199. }
  200. /* Function rpc_function caller
  201. */
  202. template<typename R, R(*func)()>
  203. void rpc_function_caller(Arguments *arguments, Reply *result) {
  204. R res = (*func)();
  205. result->putData<R>(res);
  206. }
  207. /* Function rpc_function caller
  208. */
  209. template<typename R, typename A1, R(*func)(A1)>
  210. void rpc_function_caller(Arguments *arguments, Reply *result) {
  211. A1 arg1 = arguments->getArg<A1>();
  212. R res = (*func)(arg1);
  213. result->putData<R>(res);
  214. }
  215. /* Function rpc_function caller
  216. */
  217. template<typename R, typename A1, typename A2, R(*func)(A1, A2)>
  218. void rpc_function_caller(Arguments *arguments, Reply *result) {
  219. A1 arg1 = arguments->getArg<A1>();
  220. A2 arg2 = arguments->getArg<A2>();
  221. R res = (*func)(arg1, arg2);
  222. result->putData<R>(res);
  223. }
  224. /* Function rpc_function caller
  225. */
  226. template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)>
  227. void rpc_function_caller(Arguments *arguments, Reply *result) {
  228. A1 arg1 = arguments->getArg<A1>();
  229. A2 arg2 = arguments->getArg<A2>();
  230. A3 arg3 = arguments->getArg<A3>();
  231. R res = (*func)(arg1, arg2, arg3);
  232. result->putData<R>(res);
  233. }
  234. /* Function rpc_function caller
  235. */
  236. template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)>
  237. void rpc_function_caller(Arguments *arguments, Reply *result) {
  238. A1 arg1 = arguments->getArg<A1>();
  239. A2 arg2 = arguments->getArg<A2>();
  240. A3 arg3 = arguments->getArg<A3>();
  241. A4 arg4 = arguments->getArg<A4>();
  242. R res = (*func)(arg1, arg2, arg3, arg4);
  243. result->putData<R>(res);
  244. }
  245. struct rpc_method {
  246. const char *name;
  247. typedef void (*method_caller_t)(RPC*, Arguments*, Reply*);
  248. typedef const struct rpc_method *(*super_t)(RPC*);
  249. union {
  250. method_caller_t method_caller;
  251. super_t super;
  252. };
  253. };
  254. template<class C>
  255. const struct rpc_method *rpc_super(RPC *this_ptr) {
  256. return static_cast<C*>(this_ptr)->C::get_rpc_methods();
  257. }
  258. #define RPC_METHOD_END { NULL, NULL }
  259. #define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> }
  260. } // namespace mbed
  261. #endif