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