My fork
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw by
inc/drivers/MicroBitMessageBus.h@1:8aa5cdb4ab67, 2016-04-07 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Thu Apr 07 01:33:22 2016 +0100
- Revision:
- 1:8aa5cdb4ab67
- Child:
- 41:da05ec75cd5d
Synchronized with git rev 55cb9199
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MICROBIT_MESSAGE_BUS_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_MESSAGE_BUS_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "MicroBitComponent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitEvent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | #include "MicroBitListener.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | #include "EventModel.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | * Class definition for the MicroBitMessageBus. |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | * The MicroBitMessageBus is the common mechanism to deliver asynchronous events on the |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | * MicroBit platform. It serves a number of purposes: |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | * 1) It provides an eventing abstraction that is independent of the underlying substrate. |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | * 2) It provides a mechanism to decouple user code from trusted system code |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | * i.e. the basis of a message passing nano kernel. |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | * 3) It allows a common high level eventing abstraction across a range of hardware types.e.g. buttons, BLE... |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | * 4) It provides a mechanim for extensibility - new devices added via I/O pins can have OO based |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | * drivers and communicate via the message bus with minima impact on user level languages. |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | * 5) It allows for the possiblility of event / data aggregation, which in turn can save energy. |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | * It has the following design principles: |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | * 1) Maintain a low RAM footprint where possible |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | * 2) Make few assumptions about the underlying platform, but allow optimizations where possible. |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | class MicroBitMessageBus : public EventModel, public MicroBitComponent |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | * Default constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * Adds itself as a fiber component, and also configures itself to be the |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | * default EventModel if defaultEventBus is NULL. |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | MicroBitMessageBus(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | * Queues the given event to be sent to all registered recipients. |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | * @param evt The event to send. |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | * MicroBitMessageBus bus; |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * // Creates and sends the MicroBitEvent using bus. |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * MicrobitEvent evt(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK); |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * // Creates the MicrobitEvent, but delays the sending of that event. |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | * MicrobitEvent evt1(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, CREATE_ONLY); |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | * bus.send(evt1); |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | * // This has the same effect! |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | * evt1.fire() |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | virtual int send(MicroBitEvent evt); |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * Internal function, used to deliver the given event to all relevant recipients. |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * Normally, this is called once an event has been removed from the event queue. |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * @param evt The event to send. |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | * @param urgent The type of listeners to process (optional). If set to true, only listeners defined as urgent and non-blocking will be processed |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | * otherwise, all other (standard) listeners will be processed. Defaults to false. |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * @return 1 if all matching listeners were processed, 0 if further processing is required. |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | * @note It is recommended that all external code uses the send() function instead of this function, |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | * or the constructors provided by MicrobitEvent. |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | int process(MicroBitEvent &evt, bool urgent = false); |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | * Returns the microBitListener with the given position in our list. |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | * @param n The position in the list to return. |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | * @return the MicroBitListener at postion n in the list, or NULL if the position is invalid. |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | virtual MicroBitListener *elementAt(int n); |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | * Destructor for MicroBitMessageBus, where we deregister this instance from the array of fiber components. |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | ~MicroBitMessageBus(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | * Add the given MicroBitListener to the list of event handlers, unconditionally. |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | * @param listener The MicroBitListener to add. |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | * @return MICROBIT_OK if the listener is valid, MICROBIT_INVALID_PARAMETER otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | virtual int add(MicroBitListener *newListener); |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | * Remove the given MicroBitListener from the list of event handlers. |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | * @param listener The MicroBitListener to remove. |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | * @return MICROBIT_OK if the listener is valid, MICROBIT_INVALID_PARAMETER otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | virtual int remove(MicroBitListener *newListener); |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | private: |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 144 | MicroBitListener *listeners; // Chain of active listeners. |
Jonathan Austin |
1:8aa5cdb4ab67 | 145 | MicroBitEventQueueItem *evt_queue_head; // Head of queued events to be processed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 146 | MicroBitEventQueueItem *evt_queue_tail; // Tail of queued events to be processed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 147 | uint16_t nonce_val; // The last nonce issued. |
Jonathan Austin |
1:8aa5cdb4ab67 | 148 | uint16_t queueLength; // The number of events currently waiting to be processed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 149 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 150 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | * Cleanup any MicroBitListeners marked for deletion from the list. |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | * @return The number of listeners removed from the list. |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | int deleteMarkedListeners(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | * Queue the given event for processing at a later time. |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | * Add the given event at the tail of our queue. |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | * @param The event to queue. |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | void queueEvent(MicroBitEvent &evt); |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 165 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 166 | * Extract the next event from the front of the event queue (if present). |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 168 | * @return a pointer to the MicroBitEventQueueItem that is at the head of the list. |
Jonathan Austin |
1:8aa5cdb4ab67 | 169 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 170 | MicroBitEventQueueItem* dequeueEvent(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 171 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 172 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 173 | * Periodic callback from MicroBit. |
Jonathan Austin |
1:8aa5cdb4ab67 | 174 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 175 | * Process at least one event from the event queue, if it is not empty. |
Jonathan Austin |
1:8aa5cdb4ab67 | 176 | * We then continue processing events until something appears on the runqueue. |
Jonathan Austin |
1:8aa5cdb4ab67 | 177 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 178 | virtual void idleTick(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 180 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | * Indicates whether or not we have any background work to do. |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | * @return 1 if there are any events waitingto be processed, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | virtual int isIdleCallbackNeeded(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | #endif |