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