2017-10-12 12:58:41 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2017-2017 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBED_BLE_GENERIC_GAP
|
|
|
|
#define MBED_BLE_GENERIC_GAP
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "ble/BLE.h"
|
|
|
|
#include "ble/BLEProtocol.h"
|
|
|
|
#include "ble/Gap.h"
|
|
|
|
#include "ble/pal/PalGap.h"
|
2018-05-14 12:36:02 +00:00
|
|
|
#include "ble/pal/PalSecurityManager.h"
|
2017-10-12 12:58:41 +00:00
|
|
|
#include "ble/pal/GapEvents.h"
|
|
|
|
#include "ble/pal/GapTypes.h"
|
2018-02-06 22:59:21 +00:00
|
|
|
#include "ble/BLETypes.h"
|
2017-10-12 12:58:41 +00:00
|
|
|
#include "ble/pal/GenericAccessService.h"
|
|
|
|
#include "ble/pal/EventQueue.h"
|
2018-02-19 17:41:18 +00:00
|
|
|
#include "ble/pal/ConnectionEventMonitor.h"
|
2017-10-12 12:58:41 +00:00
|
|
|
|
|
|
|
#include "drivers/Timeout.h"
|
|
|
|
|
|
|
|
namespace ble {
|
|
|
|
namespace generic {
|
|
|
|
/**
|
|
|
|
* Generic implementation of the Gap class.
|
|
|
|
* It requires a pal::Gap and a pal::GenericAccessService injected at
|
|
|
|
* construction site.
|
|
|
|
*
|
2017-12-13 16:14:16 +00:00
|
|
|
* @attention: Not part of the public interface of BLE API.
|
2017-10-12 12:58:41 +00:00
|
|
|
*/
|
2018-02-19 15:32:07 +00:00
|
|
|
class GenericGap : public ::Gap,
|
2018-07-16 13:03:12 +00:00
|
|
|
public pal::ConnectionEventMonitor,
|
|
|
|
public pal::Gap::EventHandler {
|
2017-10-12 12:58:41 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a GenericGap instance for a given BLE instance ID.
|
|
|
|
*
|
|
|
|
* @param ble_instance_id Id of the BLE instance using this instance.
|
|
|
|
*
|
|
|
|
* @param pal_gap GAP Platform abstraction instance containing the base GAP
|
|
|
|
* primitives.
|
|
|
|
*
|
|
|
|
* @param generic_access_service Platform abstraction instance managing
|
|
|
|
* the GATT generic access service.
|
2018-05-14 12:36:02 +00:00
|
|
|
*
|
|
|
|
* @param pal_sm Security Manager Platform abstraction instance containing the base
|
|
|
|
* Security Manager primitives.
|
2017-10-12 12:58:41 +00:00
|
|
|
*/
|
|
|
|
GenericGap(
|
|
|
|
pal::EventQueue &event_queue,
|
|
|
|
pal::Gap &pal_gap,
|
2018-05-14 12:36:02 +00:00
|
|
|
pal::GenericAccessService &generic_access_service,
|
|
|
|
pal::SecurityManager &pal_sm
|
2017-10-12 12:58:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::~Gap
|
|
|
|
*/
|
|
|
|
virtual ~GenericGap();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setAddress
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setAddress(
|
|
|
|
BLEProtocol::AddressType_t type,
|
|
|
|
const BLEProtocol::AddressBytes_t address
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getAddress
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getAddress(
|
|
|
|
BLEProtocol::AddressType_t *type,
|
|
|
|
BLEProtocol::AddressBytes_t address
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getMinAdvertisingInterval
|
|
|
|
*/
|
|
|
|
virtual uint16_t getMinAdvertisingInterval() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getMinNonConnectableAdvertisingInterval
|
|
|
|
*/
|
|
|
|
virtual uint16_t getMinNonConnectableAdvertisingInterval() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getMaxAdvertisingInterval
|
|
|
|
*/
|
|
|
|
virtual uint16_t getMaxAdvertisingInterval() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::stopAdvertising
|
|
|
|
*/
|
|
|
|
virtual ble_error_t stopAdvertising();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::stopScan
|
|
|
|
*/
|
|
|
|
virtual ble_error_t stopScan();
|
|
|
|
|
2018-05-21 12:29:06 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::connect
|
|
|
|
*/
|
|
|
|
virtual ble_error_t connect(
|
|
|
|
const BLEProtocol::AddressBytes_t peerAddr,
|
|
|
|
PeerAddressType_t peerAddrType,
|
|
|
|
const ConnectionParams_t *connectionParams,
|
|
|
|
const GapScanningParams *scanParams
|
|
|
|
);
|
|
|
|
|
2017-10-12 12:58:41 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::connect
|
|
|
|
*/
|
|
|
|
virtual ble_error_t connect(
|
|
|
|
const BLEProtocol::AddressBytes_t peerAddr,
|
|
|
|
BLEProtocol::AddressType_t peerAddrType,
|
|
|
|
const ConnectionParams_t *connectionParams,
|
|
|
|
const GapScanningParams *scanParams
|
|
|
|
);
|
|
|
|
|
2018-07-16 13:03:12 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::readPhy
|
|
|
|
*/
|
|
|
|
virtual ble_error_t readPhy(Handle_t connection);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setPreferedPhys
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setPreferedPhys(
|
|
|
|
const phy_set_t* txPhys,
|
|
|
|
const phy_set_t* rxPhys
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setPhy
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setPhy(
|
|
|
|
Handle_t connection,
|
|
|
|
const phy_set_t* txPhys,
|
|
|
|
const phy_set_t* rxPhys,
|
|
|
|
CodedSymbolPerBit_t codedSymbol
|
|
|
|
);
|
|
|
|
|
2017-10-12 12:58:41 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::disconnect
|
|
|
|
*/
|
|
|
|
virtual ble_error_t disconnect(
|
|
|
|
Handle_t connectionHandle,
|
|
|
|
DisconnectionReason_t reason
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::updateConnectionParams
|
|
|
|
*/
|
|
|
|
virtual ble_error_t updateConnectionParams(
|
|
|
|
Handle_t handle,
|
|
|
|
const ConnectionParams_t *params
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getPreferredConnectionParams
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getPreferredConnectionParams(
|
|
|
|
ConnectionParams_t *params
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setPreferredConnectionParams
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setPreferredConnectionParams(
|
|
|
|
const ConnectionParams_t *params
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setDeviceName
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setDeviceName(const uint8_t *deviceName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getDeviceName
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setAppearance
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getAppearance
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setTxPower
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setTxPower(int8_t txPower);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getPermittedTxPowerValues
|
|
|
|
*/
|
|
|
|
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getMaxWhitelistSize
|
|
|
|
*/
|
|
|
|
virtual uint8_t getMaxWhitelistSize(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getWhitelist
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getWhitelist(Whitelist_t &whitelist) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setWhitelist
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setWhitelist(const Whitelist_t &whitelist);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setAdvertisingPolicyMode
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setScanningPolicyMode
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setInitiatorPolicyMode
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getAdvertisingPolicyMode
|
|
|
|
*/
|
|
|
|
virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getScanningPolicyMode
|
|
|
|
*/
|
|
|
|
virtual ScanningPolicyMode_t getScanningPolicyMode(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getInitiatorPolicyMode
|
|
|
|
*/
|
|
|
|
virtual InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::startRadioScan
|
|
|
|
*/
|
|
|
|
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::initRadioNotification
|
|
|
|
*/
|
|
|
|
virtual ble_error_t initRadioNotification(void);
|
|
|
|
|
2018-05-13 16:57:27 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::enablePrivacy
|
|
|
|
*/
|
|
|
|
virtual ble_error_t enablePrivacy(bool enable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setPeripheralPrivacyConfiguration
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setPeripheralPrivacyConfiguration(
|
|
|
|
const PeripheralPrivacyConfiguration_t *configuration
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getPeripheralPrivacyConfiguration
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getPeripheralPrivacyConfiguration(
|
|
|
|
PeripheralPrivacyConfiguration_t *configuration
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::setCentralPrivacyConfiguration
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setCentralPrivacyConfiguration(
|
|
|
|
const CentralPrivacyConfiguration_t *configuration
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::getCentralPrivacyConfiguration
|
|
|
|
*/
|
|
|
|
virtual ble_error_t getCentralPrivacyConfiguration(
|
|
|
|
CentralPrivacyConfiguration_t *configuration
|
|
|
|
);
|
|
|
|
|
2017-10-12 12:58:41 +00:00
|
|
|
/**
|
|
|
|
* @see Gap::setAdvertisingData
|
|
|
|
*/
|
|
|
|
virtual ble_error_t setAdvertisingData(
|
|
|
|
const GapAdvertisingData &advData,
|
|
|
|
const GapAdvertisingData &scanResponse
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::startAdvertising
|
|
|
|
*/
|
|
|
|
virtual ble_error_t startAdvertising(const GapAdvertisingParams ¶ms);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Gap::reset
|
|
|
|
*/
|
|
|
|
virtual ble_error_t reset(void);
|
|
|
|
|
2018-02-19 15:32:07 +00:00
|
|
|
/**
|
|
|
|
* @copydoc ::Gap::processConnectionEvent
|
|
|
|
*/
|
2018-02-27 17:22:13 +00:00
|
|
|
void processConnectionEvent(
|
2018-02-19 15:32:07 +00:00
|
|
|
Handle_t handle,
|
|
|
|
Role_t role,
|
2018-05-21 12:29:06 +00:00
|
|
|
peer_address_type_t peerAddrType,
|
2018-02-19 15:32:07 +00:00
|
|
|
const BLEProtocol::AddressBytes_t peerAddr,
|
|
|
|
BLEProtocol::AddressType_t ownAddrType,
|
|
|
|
const BLEProtocol::AddressBytes_t ownAddr,
|
2018-05-21 12:29:06 +00:00
|
|
|
const ConnectionParams_t *connectionParams,
|
|
|
|
const uint8_t *peerResolvableAddr,
|
|
|
|
const uint8_t *localResolvableAddr
|
2018-02-19 15:32:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc ::Gap::processDisconnectionEvent
|
|
|
|
*/
|
2018-02-27 17:22:13 +00:00
|
|
|
void processDisconnectionEvent(
|
2018-02-19 15:32:07 +00:00
|
|
|
Handle_t handle,
|
|
|
|
DisconnectionReason_t reason
|
|
|
|
);
|
|
|
|
|
2018-02-27 17:22:13 +00:00
|
|
|
private:
|
2018-02-19 16:28:01 +00:00
|
|
|
/** @note Implements ConnectionEventMonitor.
|
|
|
|
* @copydoc ConnectionEventMonitor::set_connection_event_handler
|
|
|
|
*/
|
2018-03-01 20:31:51 +00:00
|
|
|
void set_connection_event_handler(pal::ConnectionEventMonitor::EventHandler *_connection_event_handler);
|
2018-02-19 16:28:01 +00:00
|
|
|
|
2017-10-12 12:58:41 +00:00
|
|
|
void on_scan_timeout();
|
|
|
|
|
|
|
|
void process_scan_timeout();
|
|
|
|
|
|
|
|
void on_advertising_timeout();
|
|
|
|
|
|
|
|
void process_advertising_timeout();
|
|
|
|
|
|
|
|
void on_gap_event_received(const pal::GapEvent &e);
|
|
|
|
|
|
|
|
void on_advertising_report(const pal::GapAdvertisingReportEvent &e);
|
|
|
|
|
|
|
|
void on_connection_complete(const pal::GapConnectionCompleteEvent &e);
|
|
|
|
|
|
|
|
void on_disconnection_complete(const pal::GapDisconnectionCompleteEvent &e);
|
|
|
|
|
|
|
|
void on_connection_parameter_request(
|
|
|
|
const pal::GapRemoteConnectionParameterRequestEvent &e
|
|
|
|
);
|
|
|
|
|
|
|
|
void on_connection_update(const pal::GapConnectionUpdateEvent &e);
|
|
|
|
|
|
|
|
void on_unexpected_error(const pal::GapUnexpectedErrorEvent &e);
|
|
|
|
|
2018-05-15 17:37:50 +00:00
|
|
|
enum AddressUseType_t {
|
|
|
|
CENTRAL_CONNECTION,
|
|
|
|
CENTRAL_SCAN,
|
|
|
|
PERIPHERAL_CONNECTABLE,
|
|
|
|
PERIPHERAL_NON_CONNECTABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
pal::own_address_type_t get_own_address_type(AddressUseType_t address_use_type);
|
2017-10-12 12:58:41 +00:00
|
|
|
|
|
|
|
bool initialize_whitelist() const;
|
|
|
|
|
2018-05-13 18:14:56 +00:00
|
|
|
ble_error_t update_address_resolution_setting();
|
|
|
|
|
2018-05-14 12:26:39 +00:00
|
|
|
void set_random_address_rotation(bool enable);
|
|
|
|
|
|
|
|
void update_random_address();
|
|
|
|
|
|
|
|
void on_address_rotation_timeout();
|
|
|
|
|
2018-07-16 13:03:12 +00:00
|
|
|
/* implements pal::Gap::EventHandler */
|
|
|
|
private:
|
|
|
|
virtual void on_read_phy(
|
|
|
|
pal::hci_error_code_t hci_status,
|
|
|
|
Handle_t connection_handle,
|
|
|
|
ble::phy_t tx_phy,
|
|
|
|
ble::phy_t rx_phy
|
|
|
|
);
|
|
|
|
|
|
|
|
virtual void on_phy_update_complete(
|
|
|
|
pal::hci_error_code_t hci_status,
|
|
|
|
Handle_t connection_handle,
|
|
|
|
ble::phy_t tx_phy,
|
|
|
|
ble::phy_t rx_phy
|
|
|
|
);
|
|
|
|
|
|
|
|
private:
|
2017-10-12 12:58:41 +00:00
|
|
|
pal::EventQueue& _event_queue;
|
|
|
|
pal::Gap &_pal_gap;
|
|
|
|
pal::GenericAccessService &_gap_service;
|
2018-05-14 12:36:02 +00:00
|
|
|
pal::SecurityManager &_pal_sm;
|
2017-10-12 12:58:41 +00:00
|
|
|
BLEProtocol::AddressType_t _address_type;
|
2018-02-06 22:59:21 +00:00
|
|
|
ble::address_t _address;
|
2017-10-12 12:58:41 +00:00
|
|
|
pal::initiator_policy_t _initiator_policy_mode;
|
|
|
|
pal::scanning_filter_policy_t _scanning_filter_policy;
|
|
|
|
pal::advertising_filter_policy_t _advertising_filter_policy;
|
|
|
|
mutable Whitelist_t _whitelist;
|
2018-05-13 17:52:54 +00:00
|
|
|
|
|
|
|
bool _privacy_enabled;
|
|
|
|
PeripheralPrivacyConfiguration_t _peripheral_privacy_configuration;
|
|
|
|
CentralPrivacyConfiguration_t _central_privacy_configuration;
|
2018-05-14 12:26:39 +00:00
|
|
|
ble::address_t _random_static_identity_address;
|
|
|
|
bool _random_address_rotating;
|
2018-05-13 17:52:54 +00:00
|
|
|
|
2017-10-12 12:58:41 +00:00
|
|
|
mbed::Timeout _advertising_timeout;
|
|
|
|
mbed::Timeout _scan_timeout;
|
2018-05-14 12:26:39 +00:00
|
|
|
mbed::Ticker _address_rotation_ticker;
|
2018-03-01 20:31:51 +00:00
|
|
|
pal::ConnectionEventMonitor::EventHandler *_connection_event_handler;
|
2017-10-12 12:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBED_BLE_GENERIC_GAP */
|