Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleEventQueue.h Source File

SimpleEventQueue.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017-2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef BLE_PAL_SIMPLE_EVENT_QUEUE_H_
00018 #define BLE_PAL_SIMPLE_EVENT_QUEUE_H_
00019 
00020 #include <new>
00021 #include "EventQueue.h"
00022 #include "ble/BLEInstanceBase.h "
00023 #include "ble/BLE.h"
00024 
00025 namespace ble {
00026 namespace pal {
00027 
00028 /**
00029  * Simple implementation of the pal::EventQueue.
00030  */
00031 struct SimpleEventQueue : EventQueue {
00032 
00033     typedef mbed::Callback<void()> event_t;
00034 
00035     /**
00036      * Construct an empty event queue.
00037      *
00038      * @attention a call to initialize is mandatory before any other call.
00039      *
00040      * @param ble_instance_id The id of the ble instance associated with that
00041      * event queue.
00042      */
00043     SimpleEventQueue() :
00044         _ble_base(NULL), _ble_instance_id(0), _events(NULL) { }
00045 
00046     /**
00047      * Initialize the event queue with a BLEInstanceBase and a ble id.
00048      *
00049      * @param ble_base the instance which will be used to signal the presence
00050      * of new events.
00051      *
00052      * @param ble_id Id of the BLE instance using that event queue.
00053      */
00054     void initialize(BLEInstanceBase* ble_base, BLE::InstanceID_t ble_id)
00055     {
00056         _ble_base = ble_base;
00057         _ble_instance_id = ble_id;
00058     }
00059 
00060     /**
00061      * @see ble::pal::EventQueue
00062      */
00063     ~SimpleEventQueue ()
00064     {
00065         clear();
00066     }
00067 
00068     /**
00069      * @see ble::pal::post
00070      */
00071     virtual bool post (const mbed::Callback<void()>& event)
00072     {
00073         if (_ble_base == NULL) {
00074             return false;
00075         }
00076 
00077         EventNode* next = new (std::nothrow) EventNode(event);
00078         if (next == NULL) {
00079             return false;
00080         }
00081 
00082         if (_events == NULL) {
00083             _events = next;
00084         } else {
00085             EventNode* previous = _events;
00086             while (previous->next) {
00087                 previous = previous->next;
00088             }
00089 
00090             previous->next = next;
00091         }
00092 
00093         signal_event();
00094 
00095         return true;
00096     }
00097 
00098     /**
00099      * Clear the event queue from all its events.
00100      */
00101     void clear()
00102     {
00103         while (_events) {
00104             EventNode* next = _events->next;
00105             delete _events;
00106             _events = next;
00107         }
00108     }
00109 
00110     /**
00111      * Process the event queue.
00112      */
00113     void process()
00114     {
00115         while (_events) {
00116             EventNode* next = _events->next;
00117             _events->event();
00118             delete _events;
00119             _events = next;
00120         }
00121     }
00122 
00123 private:
00124     struct EventNode {
00125         EventNode(const event_t event) : event(event), next(NULL) { }
00126         event_t event;
00127         EventNode* next;
00128     };
00129 
00130     void signal_event()
00131     {
00132         _ble_base->signalEventsToProcess(_ble_instance_id);
00133     }
00134 
00135     BLEInstanceBase* _ble_base;
00136     BLE::InstanceID_t _ble_instance_id;
00137     EventNode* _events;
00138 };
00139 
00140 } // namespace pal
00141 } // namespace ble
00142 
00143 #endif /* BLE_PAL_SIMPLE_EVENT_QUEUE_H_ */