Skip to content

Commit 953739c

Browse files
committed
BLE: Event Queue interface at the pal level.
To help generic code, an interface of an event queue at the PAL level has been added. Implementation can either rely on the event mechanism internal to the stack or use the SimpleEventQueue implementation provided by this patch.
1 parent 63668cb commit 953739c

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_EVENT_QUEUE_H_
18+
#define BLE_PAL_EVENT_QUEUE_H_
19+
20+
#include "platform/Callback.h"
21+
22+
namespace ble {
23+
namespace pal {
24+
25+
/**
26+
* Simple interface which allow upper layer to post an event into the event
27+
* mechanism of the abstracted stack.
28+
*
29+
* When an event is posted then it's availability shall be signaled to the upper
30+
* layer in order to inform the upper layer that events are ready to be
31+
* processed.
32+
*
33+
* Implementation can realize that operation by calling the function
34+
* signalEventsToProcess of their implementation of BLEInstanceBase.
35+
*
36+
* Events in the queue shall be processed at the next invocation of
37+
* BLEInstanceBase::processEvents.
38+
*/
39+
struct EventQueue {
40+
/**
41+
* Base constructor of an event queue.
42+
*/
43+
EventQueue() { }
44+
45+
/**
46+
* Destructor, needs to be overiden in implementation
47+
*/
48+
~EventQueue() { }
49+
50+
/**
51+
* Post an event into the event queue.
52+
*
53+
* @param event The event to store in the queue, it shall be processed at
54+
* the next invocation of BLE::processEvent.
55+
*
56+
* @return true in case of success and false otherwise
57+
*
58+
* @important Event availability shall be signaled to the upper layer and
59+
* the event queue shall be processed at the next invocation of
60+
* BLEInstanceBase::process
61+
*/
62+
virtual bool post(const mbed::Callback<void()>& event) = 0;
63+
};
64+
65+
} // namespace pal
66+
} // namespace ble
67+
68+
#endif /* BLE_PAL_EVENT_QUEUE_H_ */
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)