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