|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017-2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef BLE_PAL_SIMPLE_EVENT_QUEUE_H_ |
| 18 | +#define BLE_PAL_SIMPLE_EVENT_QUEUE_H_ |
| 19 | + |
| 20 | +#include <new> |
| 21 | +#include "EventQueue.h" |
| 22 | +#include "ble/BLEInstanceBase.h" |
| 23 | +#include "BLE/ble.h" |
| 24 | + |
| 25 | +namespace ble { |
| 26 | +namespace pal { |
| 27 | + |
| 28 | +/** |
| 29 | + * Simple implementation of the pal::EventQueue. |
| 30 | + */ |
| 31 | +struct SimpleEventQueue : EventQueue { |
| 32 | + |
| 33 | + typedef mbed::Callback<void()> event_t; |
| 34 | + |
| 35 | + /** |
| 36 | + * Construct an empty event queue. |
| 37 | + * |
| 38 | + * @important a call to initialize is mandatory before any other call. |
| 39 | + * |
| 40 | + * @param ble_instance_id The id of the ble instance associated with that |
| 41 | + * event queue. |
| 42 | + */ |
| 43 | + SimpleEventQueue() : |
| 44 | + _ble_base(NULL), _ble_instance_id(0), _events(NULL) { } |
| 45 | + |
| 46 | + /** |
| 47 | + * Initialize the event queue with a BLEInstanceBase and a ble id. |
| 48 | + * |
| 49 | + * @param ble_base the instance which will be used to signal the presence |
| 50 | + * of new events. |
| 51 | + * |
| 52 | + * @param ble_id Id of the BLE instance using that event queue. |
| 53 | + */ |
| 54 | + void initialize(BLEInstanceBase* ble_base, BLE::InstanceID_t ble_id) |
| 55 | + { |
| 56 | + _ble_base = ble_base; |
| 57 | + _ble_instance_id = ble_id; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @see ble::pal::EventQueue |
| 62 | + */ |
| 63 | + ~SimpleEventQueue() |
| 64 | + { |
| 65 | + clear(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @see ble::pal::post |
| 70 | + */ |
| 71 | + virtual bool post(const mbed::Callback<void()>& event) |
| 72 | + { |
| 73 | + if (_ble_base == NULL) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + EventNode* next = new (std::nothrow) EventNode(event); |
| 78 | + if (next == NULL) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if (_events == NULL) { |
| 83 | + _events = next; |
| 84 | + } else { |
| 85 | + EventNode* previous = _events; |
| 86 | + while (previous->next) { |
| 87 | + previous = previous->next; |
| 88 | + } |
| 89 | + |
| 90 | + previous->next = next; |
| 91 | + } |
| 92 | + |
| 93 | + signal_event(); |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Clear the event queue from all its events. |
| 100 | + */ |
| 101 | + void clear() |
| 102 | + { |
| 103 | + while (_events) { |
| 104 | + EventNode* next = _events->next; |
| 105 | + delete _events; |
| 106 | + _events = next; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Process the event queue. |
| 112 | + */ |
| 113 | + void process() |
| 114 | + { |
| 115 | + while (_events) { |
| 116 | + EventNode* next = _events->next; |
| 117 | + _events->event(); |
| 118 | + delete _events; |
| 119 | + _events = next; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +private: |
| 124 | + struct EventNode { |
| 125 | + EventNode(const event_t event) : event(event), next(NULL) { } |
| 126 | + event_t event; |
| 127 | + EventNode* next; |
| 128 | + }; |
| 129 | + |
| 130 | + void signal_event() |
| 131 | + { |
| 132 | + _ble_base->signalEventsToProcess(_ble_instance_id); |
| 133 | + } |
| 134 | + |
| 135 | + BLEInstanceBase* _ble_base; |
| 136 | + BLE::InstanceID_t _ble_instance_id; |
| 137 | + EventNode* _events; |
| 138 | +}; |
| 139 | + |
| 140 | +} // namespace pal |
| 141 | +} // namespace ble |
| 142 | + |
| 143 | +#endif /* BLE_PAL_SIMPLE_EVENT_QUEUE_H_ */ |
0 commit comments