| 1 | /* mbed Microcontroller Library - Base |
|---|
| 2 | * Copyright (c) 2006-2008 ARM Limited. All rights reserved. |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #ifndef MBED_BASE_H |
|---|
| 6 | #define MBED_BASE_H |
|---|
| 7 | |
|---|
| 8 | #include "platform.h" |
|---|
| 9 | #include "PinNames.h" |
|---|
| 10 | #include "PeripheralNames.h" |
|---|
| 11 | #include <cstdlib> |
|---|
| 12 | #include "DirHandle.h" |
|---|
| 13 | |
|---|
| 14 | namespace mbed { |
|---|
| 15 | |
|---|
| 16 | #ifdef MBED_RPC |
|---|
| 17 | struct rpc_function { |
|---|
| 18 | const char *name; |
|---|
| 19 | void (*caller)(const char*, char*); |
|---|
| 20 | }; |
|---|
| 21 | |
|---|
| 22 | struct rpc_class { |
|---|
| 23 | const char *name; |
|---|
| 24 | const rpc_function *static_functions; |
|---|
| 25 | struct rpc_class *next; |
|---|
| 26 | }; |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | /* Class Base |
|---|
| 30 | * The base class for most things |
|---|
| 31 | */ |
|---|
| 32 | class Base { |
|---|
| 33 | |
|---|
| 34 | public: |
|---|
| 35 | |
|---|
| 36 | Base(const char *name = NULL); |
|---|
| 37 | |
|---|
| 38 | virtual ~Base(); |
|---|
| 39 | |
|---|
| 40 | /* Function register_object |
|---|
| 41 | * Registers this object with the given name, so that it can be |
|---|
| 42 | * looked up with lookup. If this object has already been |
|---|
| 43 | * registered, then this just changes the name. |
|---|
| 44 | * |
|---|
| 45 | * Variables |
|---|
| 46 | * name - The name to give the object. If NULL we do nothing. |
|---|
| 47 | */ |
|---|
| 48 | void register_object(const char *name); |
|---|
| 49 | |
|---|
| 50 | /* Function name |
|---|
| 51 | * Returns the name of the object, or NULL if it has no name. |
|---|
| 52 | */ |
|---|
| 53 | const char *name(); |
|---|
| 54 | |
|---|
| 55 | #ifdef MBED_RPC |
|---|
| 56 | |
|---|
| 57 | /* Function rpc |
|---|
| 58 | * Call the given method with the given arguments, and write the |
|---|
| 59 | * result into the string pointed to by result. The default |
|---|
| 60 | * implementation calls rpc_methods to determine the supported |
|---|
| 61 | * methods. |
|---|
| 62 | * |
|---|
| 63 | * Variables |
|---|
| 64 | * method - The name of the method to call. |
|---|
| 65 | * arguments - A list of arguments separated by spaces. |
|---|
| 66 | * result - A pointer to a string to write the result into. May |
|---|
| 67 | * be NULL, in which case nothing is written. |
|---|
| 68 | * |
|---|
| 69 | * Returns |
|---|
| 70 | * true if method corresponds to a valid rpc method, or |
|---|
| 71 | * false otherwise. |
|---|
| 72 | */ |
|---|
| 73 | virtual bool rpc(const char *method, const char *arguments, char *result); |
|---|
| 74 | |
|---|
| 75 | /* Function get_rpc_methods |
|---|
| 76 | * Returns a pointer to an array describing the rpc methods |
|---|
| 77 | * supported by this object, terminated by either |
|---|
| 78 | * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass). |
|---|
| 79 | * |
|---|
| 80 | * Example |
|---|
| 81 | * > class Example : public Base { |
|---|
| 82 | * > int foo(int a, int b) { return a + b; } |
|---|
| 83 | * > virtual const struct rpc_method *get_rpc_methods() { |
|---|
| 84 | * > static const rpc_method rpc_methods[] = { |
|---|
| 85 | * > { "foo", generic_caller<int, Example, int, int, &Example::foo> }, |
|---|
| 86 | * > RPC_METHOD_SUPER(Base) |
|---|
| 87 | * > }; |
|---|
| 88 | * > return rpc_methods; |
|---|
| 89 | * > } |
|---|
| 90 | * > }; |
|---|
| 91 | */ |
|---|
| 92 | virtual const struct rpc_method *get_rpc_methods(); |
|---|
| 93 | |
|---|
| 94 | /* Function rpc |
|---|
| 95 | * Use the lookup function to lookup an object and, if |
|---|
| 96 | * successful, call its rpc method |
|---|
| 97 | * |
|---|
| 98 | * Variables |
|---|
| 99 | * returns - false if name does not correspond to an object, |
|---|
| 100 | * otherwise the return value of the call to the object's rpc |
|---|
| 101 | * method. |
|---|
| 102 | */ |
|---|
| 103 | static bool rpc(const char *name, const char *method, const char *arguments, char *result); |
|---|
| 104 | |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | /* Function lookup |
|---|
| 108 | * Lookup and return the object that has the given name. |
|---|
| 109 | * |
|---|
| 110 | * Variables |
|---|
| 111 | * name - the name to lookup. |
|---|
| 112 | * len - the length of name. |
|---|
| 113 | */ |
|---|
| 114 | static Base *lookup(const char *name, unsigned int len); |
|---|
| 115 | |
|---|
| 116 | static DirHandle *opendir(); |
|---|
| 117 | friend class BaseDirHandle; |
|---|
| 118 | |
|---|
| 119 | protected: |
|---|
| 120 | |
|---|
| 121 | static Base *_head; |
|---|
| 122 | Base *_next; |
|---|
| 123 | const char *_name; |
|---|
| 124 | bool _from_construct; |
|---|
| 125 | |
|---|
| 126 | private: |
|---|
| 127 | |
|---|
| 128 | #ifdef MBED_RPC |
|---|
| 129 | static rpc_class *_classes; |
|---|
| 130 | |
|---|
| 131 | static const rpc_function _base_funcs[]; |
|---|
| 132 | static rpc_class _base_class; |
|---|
| 133 | #endif |
|---|
| 134 | |
|---|
| 135 | void delete_self(); |
|---|
| 136 | static void list_objs(const char *arguments, char *result); |
|---|
| 137 | static void clear(const char*,char*); |
|---|
| 138 | |
|---|
| 139 | static char *new_name(Base *p); |
|---|
| 140 | |
|---|
| 141 | public: |
|---|
| 142 | |
|---|
| 143 | #ifdef MBED_RPC |
|---|
| 144 | /* Function add_rpc_class |
|---|
| 145 | * Add the class to the list of classes which can have static |
|---|
| 146 | * methods called via rpc (the static methods which can be called |
|---|
| 147 | * are defined by that class' get_rpc_class() static method). |
|---|
| 148 | */ |
|---|
| 149 | template<class C> |
|---|
| 150 | static void add_rpc_class() { |
|---|
| 151 | rpc_class *c = C::get_rpc_class(); |
|---|
| 152 | c->next = _classes; |
|---|
| 153 | _classes = c; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | template<class C> |
|---|
| 157 | static const char *construct() { |
|---|
| 158 | Base *p = new C(); |
|---|
| 159 | p->_from_construct = true; |
|---|
| 160 | if(p->_name==NULL) { |
|---|
| 161 | p->register_object(new_name(p)); |
|---|
| 162 | } |
|---|
| 163 | return p->_name; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | template<class C, typename A1> |
|---|
| 167 | static const char *construct(A1 arg1) { |
|---|
| 168 | Base *p = new C(arg1); |
|---|
| 169 | p->_from_construct = true; |
|---|
| 170 | if(p->_name==NULL) { |
|---|
| 171 | p->register_object(new_name(p)); |
|---|
| 172 | } |
|---|
| 173 | return p->_name; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | template<class C, typename A1, typename A2> |
|---|
| 177 | static const char *construct(A1 arg1, A2 arg2) { |
|---|
| 178 | Base *p = new C(arg1,arg2); |
|---|
| 179 | p->_from_construct = true; |
|---|
| 180 | if(p->_name==NULL) { |
|---|
| 181 | p->register_object(new_name(p)); |
|---|
| 182 | } |
|---|
| 183 | return p->_name; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | template<class C, typename A1, typename A2, typename A3> |
|---|
| 187 | static const char *construct(A1 arg1, A2 arg2, A3 arg3) { |
|---|
| 188 | Base *p = new C(arg1,arg2,arg3); |
|---|
| 189 | p->_from_construct = true; |
|---|
| 190 | if(p->_name==NULL) { |
|---|
| 191 | p->register_object(new_name(p)); |
|---|
| 192 | } |
|---|
| 193 | return p->_name; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | template<class C, typename A1, typename A2, typename A3, typename A4> |
|---|
| 197 | static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { |
|---|
| 198 | Base *p = new C(arg1,arg2,arg3,arg4); |
|---|
| 199 | p->_from_construct = true; |
|---|
| 200 | if(p->_name==NULL) { |
|---|
| 201 | p->register_object(new_name(p)); |
|---|
| 202 | } |
|---|
| 203 | return p->_name; |
|---|
| 204 | } |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | /* Macro MBED_OBJECT_NAME_MAX |
|---|
| 210 | * The maximum size of object name (including terminating null byte) |
|---|
| 211 | * that will be recognised when using fopen to open a FileLike |
|---|
| 212 | * object, or when using the rpc function. |
|---|
| 213 | */ |
|---|
| 214 | #define MBED_OBJECT_NAME_MAX 32 |
|---|
| 215 | |
|---|
| 216 | /* Macro MBED_METHOD_NAME_MAX |
|---|
| 217 | * The maximum size of rpc method name (including terminating null |
|---|
| 218 | * byte) that will be recognised by the rpc function (in rpc.h). |
|---|
| 219 | */ |
|---|
| 220 | #define MBED_METHOD_NAME_MAX 32 |
|---|
| 221 | |
|---|
| 222 | } // namespace mbed |
|---|
| 223 | |
|---|
| 224 | #endif |
|---|
| 225 | |
|---|