Skip to content

Commit 484b4a2

Browse files
authored
Merge pull request #5311 from pan-/ble-generic-gap
BLE: Add generic GAP implementation.
2 parents be52ba2 + 1b7a3ff commit 484b4a2

File tree

2 files changed

+1388
-0
lines changed

2 files changed

+1388
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
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 MBED_BLE_GENERIC_GAP
18+
#define MBED_BLE_GENERIC_GAP
19+
20+
#include <algorithm>
21+
22+
#include "ble/BLE.h"
23+
#include "ble/BLEProtocol.h"
24+
#include "ble/Gap.h"
25+
#include "ble/pal/PalGap.h"
26+
#include "ble/pal/GapEvents.h"
27+
#include "ble/pal/GapTypes.h"
28+
#include "ble/pal/GenericAccessService.h"
29+
#include "ble/pal/EventQueue.h"
30+
31+
#include "drivers/Timeout.h"
32+
33+
namespace ble {
34+
namespace generic {
35+
36+
/**
37+
* Generic implementation of the Gap class.
38+
* It requires a pal::Gap and a pal::GenericAccessService injected at
39+
* construction site.
40+
*
41+
* @important: Not part of the public interface of BLE API.
42+
*/
43+
class GenericGap : public ::Gap {
44+
45+
public:
46+
/**
47+
* Construct a GenericGap instance for a given BLE instance ID.
48+
*
49+
* @param ble_instance_id Id of the BLE instance using this instance.
50+
*
51+
* @param pal_gap GAP Platform abstraction instance containing the base GAP
52+
* primitives.
53+
*
54+
* @param generic_access_service Platform abstraction instance managing
55+
* the GATT generic access service.
56+
*/
57+
GenericGap(
58+
pal::EventQueue &event_queue,
59+
pal::Gap &pal_gap,
60+
pal::GenericAccessService &generic_access_service
61+
);
62+
63+
/**
64+
* @see Gap::~Gap
65+
*/
66+
virtual ~GenericGap();
67+
68+
/**
69+
* @see Gap::setAddress
70+
*/
71+
virtual ble_error_t setAddress(
72+
BLEProtocol::AddressType_t type,
73+
const BLEProtocol::AddressBytes_t address
74+
);
75+
76+
/**
77+
* @see Gap::getAddress
78+
*/
79+
virtual ble_error_t getAddress(
80+
BLEProtocol::AddressType_t *type,
81+
BLEProtocol::AddressBytes_t address
82+
);
83+
84+
/**
85+
* @see Gap::getMinAdvertisingInterval
86+
*/
87+
virtual uint16_t getMinAdvertisingInterval() const;
88+
89+
/**
90+
* @see Gap::getMinNonConnectableAdvertisingInterval
91+
*/
92+
virtual uint16_t getMinNonConnectableAdvertisingInterval() const;
93+
94+
/**
95+
* @see Gap::getMaxAdvertisingInterval
96+
*/
97+
virtual uint16_t getMaxAdvertisingInterval() const;
98+
99+
/**
100+
* @see Gap::stopAdvertising
101+
*/
102+
virtual ble_error_t stopAdvertising();
103+
104+
/**
105+
* @see Gap::stopScan
106+
*/
107+
virtual ble_error_t stopScan();
108+
109+
/**
110+
* @see Gap::connect
111+
*/
112+
virtual ble_error_t connect(
113+
const BLEProtocol::AddressBytes_t peerAddr,
114+
BLEProtocol::AddressType_t peerAddrType,
115+
const ConnectionParams_t *connectionParams,
116+
const GapScanningParams *scanParams
117+
);
118+
119+
/**
120+
* @see Gap::disconnect
121+
*/
122+
virtual ble_error_t disconnect(
123+
Handle_t connectionHandle,
124+
DisconnectionReason_t reason
125+
);
126+
127+
/**
128+
* @see Gap::updateConnectionParams
129+
*/
130+
virtual ble_error_t updateConnectionParams(
131+
Handle_t handle,
132+
const ConnectionParams_t *params
133+
);
134+
135+
/**
136+
* @see Gap::getPreferredConnectionParams
137+
*/
138+
virtual ble_error_t getPreferredConnectionParams(
139+
ConnectionParams_t *params
140+
);
141+
142+
/**
143+
* @see Gap::setPreferredConnectionParams
144+
*/
145+
virtual ble_error_t setPreferredConnectionParams(
146+
const ConnectionParams_t *params
147+
);
148+
149+
/**
150+
* @see Gap::setDeviceName
151+
*/
152+
virtual ble_error_t setDeviceName(const uint8_t *deviceName);
153+
154+
/**
155+
* @see Gap::getDeviceName
156+
*/
157+
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
158+
159+
/**
160+
* @see Gap::setAppearance
161+
*/
162+
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
163+
164+
/**
165+
* @see Gap::getAppearance
166+
*/
167+
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
168+
169+
/**
170+
* @see Gap::setTxPower
171+
*/
172+
virtual ble_error_t setTxPower(int8_t txPower);
173+
174+
/**
175+
* @see Gap::getPermittedTxPowerValues
176+
*/
177+
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
178+
179+
/**
180+
* @see Gap::getMaxWhitelistSize
181+
*/
182+
virtual uint8_t getMaxWhitelistSize(void) const;
183+
184+
/**
185+
* @see Gap::getWhitelist
186+
*/
187+
virtual ble_error_t getWhitelist(Whitelist_t &whitelist) const;
188+
189+
/**
190+
* @see Gap::setWhitelist
191+
*/
192+
virtual ble_error_t setWhitelist(const Whitelist_t &whitelist);
193+
194+
/**
195+
* @see Gap::setAdvertisingPolicyMode
196+
*/
197+
virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
198+
199+
/**
200+
* @see Gap::setScanningPolicyMode
201+
*/
202+
virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
203+
204+
/**
205+
* @see Gap::setInitiatorPolicyMode
206+
*/
207+
virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
208+
209+
/**
210+
* @see Gap::getAdvertisingPolicyMode
211+
*/
212+
virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
213+
214+
/**
215+
* @see Gap::getScanningPolicyMode
216+
*/
217+
virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
218+
219+
/**
220+
* @see Gap::getInitiatorPolicyMode
221+
*/
222+
virtual InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
223+
224+
/**
225+
* @see Gap::startRadioScan
226+
*/
227+
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
228+
229+
/**
230+
* @see Gap::initRadioNotification
231+
*/
232+
virtual ble_error_t initRadioNotification(void);
233+
234+
/**
235+
* @see Gap::setAdvertisingData
236+
*/
237+
virtual ble_error_t setAdvertisingData(
238+
const GapAdvertisingData &advData,
239+
const GapAdvertisingData &scanResponse
240+
);
241+
242+
/**
243+
* @see Gap::startAdvertising
244+
*/
245+
virtual ble_error_t startAdvertising(const GapAdvertisingParams &params);
246+
247+
/**
248+
* @see Gap::reset
249+
*/
250+
virtual ble_error_t reset(void);
251+
252+
private:
253+
void on_scan_timeout();
254+
255+
void process_scan_timeout();
256+
257+
void on_advertising_timeout();
258+
259+
void process_advertising_timeout();
260+
261+
void on_gap_event_received(const pal::GapEvent &e);
262+
263+
void on_advertising_report(const pal::GapAdvertisingReportEvent &e);
264+
265+
void on_connection_complete(const pal::GapConnectionCompleteEvent &e);
266+
267+
void on_disconnection_complete(const pal::GapDisconnectionCompleteEvent &e);
268+
269+
void on_connection_parameter_request(
270+
const pal::GapRemoteConnectionParameterRequestEvent &e
271+
);
272+
273+
void on_connection_update(const pal::GapConnectionUpdateEvent &e);
274+
275+
void on_unexpected_error(const pal::GapUnexpectedErrorEvent &e);
276+
277+
pal::own_address_type_t get_own_address_type();
278+
279+
bool initialize_whitelist() const;
280+
281+
pal::EventQueue& _event_queue;
282+
pal::Gap &_pal_gap;
283+
pal::GenericAccessService &_gap_service;
284+
BLEProtocol::AddressType_t _address_type;
285+
pal::address_t _address;
286+
pal::initiator_policy_t _initiator_policy_mode;
287+
pal::scanning_filter_policy_t _scanning_filter_policy;
288+
pal::advertising_filter_policy_t _advertising_filter_policy;
289+
mutable Whitelist_t _whitelist;
290+
mbed::Timeout _advertising_timeout;
291+
mbed::Timeout _scan_timeout;
292+
};
293+
294+
}
295+
}
296+
297+
#endif /* MBED_BLE_GENERIC_GAP */

0 commit comments

Comments
 (0)