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