Lab 1 Program C
Fork of mbed by
Base.h@4:5d1359a283bc, 2008-11-27 (annotated)
- Committer:
- simon.ford@mbed.co.uk
- Date:
- Thu Nov 27 16:23:24 2008 +0000
- Revision:
- 4:5d1359a283bc
- Parent:
- 1:6b7f447ca868
- Child:
- 5:62573be585e9
New version of framework: vectors, environment, platform, base and file system
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 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 9 | #include <cstdlib> |
simon.ford@mbed.co.uk | 0:82220227f4fa | 10 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 11 | namespace mbed { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 12 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 13 | /* Class Base |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 14 | * The base class for most things |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 15 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 16 | class Base { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 17 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 18 | public: |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 19 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 20 | Base(const char *name = NULL); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 21 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 22 | virtual ~Base(); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 23 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 24 | /* Function register_object |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 25 | * Registers this object with the given name, so that it can be |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 26 | * looked up with lookup. If this object has already been |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 27 | * registered, then this just changes the name. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 28 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 29 | * Variables |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 30 | * name - The name to give the object. If NULL we do nothing. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 31 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 32 | void register_object(const char *name); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 33 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 34 | /* Function name |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 35 | * Returns the name of the object, or NULL if it has no name. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 36 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 37 | const char *name(); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 38 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 39 | /* Function rpc |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 40 | * Call the given method with the given arguments, and write the |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 41 | * result into the string pointed to by result. The default |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 42 | * implementation calls rpc_methods to determine the supported |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 43 | * methods. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 44 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 45 | * Variables |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 46 | * method - The name of the method to call. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 47 | * arguments - A list of arguments separated by spaces. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 48 | * result - A pointer to a string to write the result into. May |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 49 | * be NULL, in which case nothing is written. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 50 | * returns - true if method corresponds to a valid rpc method, or |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 51 | * false otherwise. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 52 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 53 | virtual bool rpc(const char *method, const char *arguments, char *result); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 54 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 55 | /* Function rpc_method |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 56 | * Returns a pointer to an array describing the rpc methods |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 57 | * supported by this object, terminated by RPC_METHOD_END. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 58 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 59 | * Example |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 60 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 61 | * > class Example : public Base { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 62 | * > int foo(int a, int b) { return a + b; } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 63 | * > virtual const struct rpc_method *rpc_methods() { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 64 | * > static const rpc_method rpc_methods[] = { |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 65 | * > { "foo", generic_caller<int, Example, int, int, &Example::foo> }, |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 66 | * > RPC_METHOD_END |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 67 | * > }; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 68 | * > return rpc_methods; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 69 | * > } |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 70 | * > }; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 71 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 72 | virtual const struct rpc_method *rpc_methods(); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 73 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 74 | /* Function rpc |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 75 | * Use the lookup function to lookup an object and, if |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 76 | * successful, call its rpc method |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 77 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 78 | * Variables |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 79 | * returns - false if name does not correspond to an object, |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 80 | * otherwise the return value of the call to the object's rpc |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 81 | * method. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 82 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 83 | static bool rpc(const char *name, const char *method, const char *arguments, char *result); |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 84 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 85 | /* Function lookup |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 86 | * Lookup and return the object that has the given name. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 87 | * |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 88 | * Variables |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 89 | * name - the name to lookup. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 90 | * len - the length of name. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 91 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 92 | static Base *lookup(const char *name, unsigned int len); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 93 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 94 | protected: |
simon.ford@mbed.co.uk | 0:82220227f4fa | 95 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 96 | static Base *_head; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 97 | Base *_next; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 98 | const char *_name; |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 99 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 100 | }; |
simon.ford@mbed.co.uk | 0:82220227f4fa | 101 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 102 | /* Macro MBED_OBJECT_NAME_MAX |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 103 | * The maximum size of object name (including terminating null byte) |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 104 | * that will be recognised when using fopen to open a FileLike |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 105 | * object, or when using the rpc function. |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 106 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 107 | #define MBED_OBJECT_NAME_MAX 32 |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 108 | |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 109 | /* Macro MBED_METHOD_NAME_MAX |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 110 | * The maximum size of rpc method name (including terminating null |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 111 | * byte) that will be recognised by the rpc function (in rpc.h). |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 112 | */ |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 113 | #define MBED_METHOD_NAME_MAX 32 |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 114 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 115 | } // namespace mbed |
simon.ford@mbed.co.uk | 0:82220227f4fa | 116 | |
simon.ford@mbed.co.uk | 1:6b7f447ca868 | 117 | #endif |
simon.ford@mbed.co.uk | 1:6b7f447ca868 | 118 |