Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GenericGap.h Source File

GenericGap.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 MBED_BLE_GENERIC_GAP
00018 #define MBED_BLE_GENERIC_GAP
00019 
00020 #include <algorithm>
00021 
00022 #include "ble/BLE.h"
00023 #include "ble/BLEProtocol.h"
00024 #include "ble/Gap.h"
00025 #include "ble/pal/PalGap.h"
00026 #include "ble/pal/PalSecurityManager.h"
00027 #include "ble/pal/GapEvents.h"
00028 #include "ble/pal/GapTypes.h"
00029 #include "ble/BLETypes.h"
00030 #include "ble/pal/GenericAccessService.h"
00031 #include "ble/pal/EventQueue.h"
00032 #include "ble/pal/ConnectionEventMonitor.h"
00033 
00034 #include "drivers/Timeout.h"
00035 
00036 namespace ble {
00037 namespace generic {
00038 /**
00039  * Generic implementation of the Gap class.
00040  * It requires a pal::Gap and a pal::GenericAccessService injected at
00041  * construction site.
00042  *
00043  * @attention: Not part of the public interface of BLE API.
00044  */
00045 class GenericGap : public ::Gap,
00046                    public pal::ConnectionEventMonitor {
00047 
00048 public:
00049     /**
00050      * Construct a GenericGap instance for a given BLE instance ID.
00051      *
00052      * @param ble_instance_id Id of the BLE instance using this instance.
00053      *
00054      * @param pal_gap GAP Platform abstraction instance containing the base GAP
00055      * primitives.
00056      *
00057      * @param generic_access_service Platform abstraction instance managing
00058      * the GATT generic access service.
00059      * 
00060      * @param pal_sm Security Manager Platform abstraction instance containing the base
00061      * Security Manager primitives.
00062      */
00063     GenericGap(
00064         pal::EventQueue &event_queue,
00065         pal::Gap &pal_gap,
00066         pal::GenericAccessService &generic_access_service,
00067         pal::SecurityManager &pal_sm
00068     );
00069 
00070     /**
00071      * @see Gap::~Gap
00072      */
00073     virtual ~GenericGap ();
00074 
00075     /**
00076      * @see Gap::setAddress
00077      */
00078     virtual ble_error_t setAddress (
00079         BLEProtocol::AddressType_t type,
00080         const BLEProtocol::AddressBytes_t address
00081     );
00082 
00083     /**
00084      * @see Gap::getAddress
00085      */
00086     virtual ble_error_t getAddress (
00087         BLEProtocol::AddressType_t *type,
00088         BLEProtocol::AddressBytes_t address
00089     );
00090 
00091     /**
00092      * @see Gap::getMinAdvertisingInterval
00093      */
00094     virtual uint16_t getMinAdvertisingInterval () const;
00095 
00096     /**
00097      * @see Gap::getMinNonConnectableAdvertisingInterval
00098      */
00099     virtual uint16_t getMinNonConnectableAdvertisingInterval () const;
00100 
00101     /**
00102      * @see Gap::getMaxAdvertisingInterval
00103      */
00104     virtual uint16_t getMaxAdvertisingInterval () const;
00105 
00106     /**
00107      * @see Gap::stopAdvertising
00108      */
00109     virtual ble_error_t stopAdvertising ();
00110 
00111     /**
00112      * @see Gap::stopScan
00113      */
00114     virtual ble_error_t stopScan ();
00115 
00116     /**
00117      * @see Gap::connect
00118      */
00119     virtual ble_error_t connect (
00120         const BLEProtocol::AddressBytes_t peerAddr,
00121         PeerAddressType_t peerAddrType,
00122         const ConnectionParams_t *connectionParams,
00123         const GapScanningParams *scanParams
00124     );
00125 
00126     /**
00127      * @see Gap::connect
00128      */
00129     virtual ble_error_t connect (
00130         const BLEProtocol::AddressBytes_t peerAddr,
00131         BLEProtocol::AddressType_t peerAddrType,
00132         const ConnectionParams_t *connectionParams,
00133         const GapScanningParams *scanParams
00134     );
00135 
00136     /**
00137      * @see Gap::disconnect
00138      */
00139     virtual ble_error_t disconnect (
00140         Handle_t connectionHandle,
00141         DisconnectionReason_t reason
00142     );
00143 
00144     /**
00145      * @see Gap::updateConnectionParams
00146      */
00147     virtual ble_error_t updateConnectionParams (
00148         Handle_t handle,
00149         const ConnectionParams_t *params
00150     );
00151 
00152     /**
00153      * @see Gap::getPreferredConnectionParams
00154      */
00155     virtual ble_error_t getPreferredConnectionParams (
00156         ConnectionParams_t *params
00157     );
00158 
00159     /**
00160      * @see Gap::setPreferredConnectionParams
00161      */
00162     virtual ble_error_t setPreferredConnectionParams (
00163         const ConnectionParams_t *params
00164     );
00165 
00166     /**
00167      * @see Gap::setDeviceName
00168      */
00169     virtual ble_error_t setDeviceName (const uint8_t *deviceName);
00170 
00171     /**
00172      * @see Gap::getDeviceName
00173      */
00174     virtual ble_error_t getDeviceName (uint8_t *deviceName, unsigned *lengthP);
00175 
00176     /**
00177      * @see Gap::setAppearance
00178      */
00179     virtual ble_error_t setAppearance (GapAdvertisingData::Appearance appearance);
00180 
00181     /**
00182      * @see Gap::getAppearance
00183      */
00184     virtual ble_error_t getAppearance (GapAdvertisingData::Appearance *appearanceP);
00185 
00186     /**
00187      * @see Gap::setTxPower
00188      */
00189     virtual ble_error_t setTxPower (int8_t txPower);
00190 
00191     /**
00192      * @see Gap::getPermittedTxPowerValues
00193      */
00194     virtual void getPermittedTxPowerValues (const int8_t **valueArrayPP, size_t *countP);
00195 
00196     /**
00197      * @see Gap::getMaxWhitelistSize
00198      */
00199     virtual uint8_t getMaxWhitelistSize (void) const;
00200 
00201     /**
00202      * @see Gap::getWhitelist
00203      */
00204     virtual ble_error_t getWhitelist (Whitelist_t &whitelist) const;
00205 
00206     /**
00207      * @see Gap::setWhitelist
00208      */
00209     virtual ble_error_t setWhitelist (const Whitelist_t &whitelist);
00210 
00211     /**
00212      * @see Gap::setAdvertisingPolicyMode
00213      */
00214     virtual ble_error_t setAdvertisingPolicyMode (AdvertisingPolicyMode_t mode);
00215 
00216     /**
00217      * @see Gap::setScanningPolicyMode
00218      */
00219     virtual ble_error_t setScanningPolicyMode (ScanningPolicyMode_t mode);
00220 
00221     /**
00222      * @see Gap::setInitiatorPolicyMode
00223      */
00224     virtual ble_error_t setInitiatorPolicyMode (InitiatorPolicyMode_t mode);
00225 
00226     /**
00227      * @see Gap::getAdvertisingPolicyMode
00228      */
00229     virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode (void) const;
00230 
00231     /**
00232      * @see Gap::getScanningPolicyMode
00233      */
00234     virtual ScanningPolicyMode_t getScanningPolicyMode (void) const;
00235 
00236     /**
00237      * @see Gap::getInitiatorPolicyMode
00238      */
00239     virtual InitiatorPolicyMode_t getInitiatorPolicyMode (void) const;
00240 
00241     /**
00242      * @see Gap::startRadioScan
00243      */
00244     virtual ble_error_t startRadioScan (const GapScanningParams &scanningParams);
00245 
00246     /**
00247      * @see Gap::initRadioNotification
00248      */
00249     virtual ble_error_t initRadioNotification (void);
00250 
00251     /**
00252      * @see Gap::enablePrivacy
00253      */
00254     virtual ble_error_t enablePrivacy (bool enable);
00255 
00256     /**
00257      * @see Gap::setPeripheralPrivacyConfiguration
00258      */
00259     virtual ble_error_t setPeripheralPrivacyConfiguration (
00260         const PeripheralPrivacyConfiguration_t *configuration
00261     );
00262 
00263     /**
00264      * @see Gap::getPeripheralPrivacyConfiguration
00265      */
00266     virtual ble_error_t getPeripheralPrivacyConfiguration (
00267         PeripheralPrivacyConfiguration_t *configuration
00268     );
00269 
00270     /**
00271      * @see Gap::setCentralPrivacyConfiguration
00272      */
00273     virtual ble_error_t setCentralPrivacyConfiguration (
00274         const CentralPrivacyConfiguration_t *configuration
00275     );
00276 
00277     /**
00278      * @see Gap::getCentralPrivacyConfiguration
00279      */
00280     virtual ble_error_t getCentralPrivacyConfiguration (
00281         CentralPrivacyConfiguration_t *configuration
00282     );
00283 
00284     /**
00285      * @see Gap::setAdvertisingData
00286      */
00287     virtual ble_error_t setAdvertisingData (
00288         const GapAdvertisingData &advData,
00289         const GapAdvertisingData &scanResponse
00290     );
00291 
00292     /**
00293      * @see Gap::startAdvertising
00294      */
00295     virtual ble_error_t startAdvertising(const GapAdvertisingParams &params);
00296 
00297     /**
00298      * @see Gap::reset
00299      */
00300     virtual ble_error_t reset (void);
00301 
00302     /**
00303      * @copydoc ::Gap::processConnectionEvent
00304      */
00305     void processConnectionEvent (
00306         Handle_t handle,
00307         Role_t role,
00308         peer_address_type_t peerAddrType,
00309         const BLEProtocol::AddressBytes_t peerAddr,
00310         BLEProtocol::AddressType_t ownAddrType,
00311         const BLEProtocol::AddressBytes_t ownAddr,
00312         const ConnectionParams_t *connectionParams,
00313         const uint8_t *peerResolvableAddr,
00314         const uint8_t *localResolvableAddr
00315     );
00316 
00317     /**
00318      * @copydoc ::Gap::processDisconnectionEvent
00319      */
00320     void processDisconnectionEvent (
00321         Handle_t handle,
00322         DisconnectionReason_t reason
00323     );
00324 
00325 private:
00326     /** @note Implements ConnectionEventMonitor.
00327      *  @copydoc ConnectionEventMonitor::set_connection_event_handler
00328      */
00329     void set_connection_event_handler(pal::ConnectionEventMonitor::EventHandler *_connection_event_handler);
00330 
00331     void on_scan_timeout();
00332 
00333     void process_scan_timeout();
00334 
00335     void on_advertising_timeout();
00336 
00337     void process_advertising_timeout();
00338 
00339     void on_gap_event_received(const pal::GapEvent &e);
00340 
00341     void on_advertising_report(const pal::GapAdvertisingReportEvent &e);
00342 
00343     void on_connection_complete(const pal::GapConnectionCompleteEvent &e);
00344 
00345     void on_disconnection_complete(const pal::GapDisconnectionCompleteEvent &e);
00346 
00347     void on_connection_parameter_request(
00348         const pal::GapRemoteConnectionParameterRequestEvent &e
00349     );
00350 
00351     void on_connection_update(const pal::GapConnectionUpdateEvent &e);
00352 
00353     void on_unexpected_error(const pal::GapUnexpectedErrorEvent &e);
00354 
00355     enum AddressUseType_t {
00356         CENTRAL_CONNECTION,
00357         CENTRAL_SCAN,
00358         PERIPHERAL_CONNECTABLE,
00359         PERIPHERAL_NON_CONNECTABLE
00360     };
00361 
00362     pal::own_address_type_t get_own_address_type(AddressUseType_t address_use_type);
00363 
00364     bool initialize_whitelist() const;
00365 
00366     ble_error_t update_address_resolution_setting();
00367 
00368     void set_random_address_rotation(bool enable);
00369 
00370     void update_random_address();
00371 
00372     void on_address_rotation_timeout();
00373 
00374     pal::EventQueue& _event_queue;
00375     pal::Gap &_pal_gap;
00376     pal::GenericAccessService &_gap_service;
00377     pal::SecurityManager &_pal_sm;
00378     BLEProtocol::AddressType_t _address_type;
00379     ble::address_t _address;
00380     pal::initiator_policy_t _initiator_policy_mode;
00381     pal::scanning_filter_policy_t _scanning_filter_policy;
00382     pal::advertising_filter_policy_t _advertising_filter_policy;
00383     mutable Whitelist_t _whitelist;
00384     
00385     bool _privacy_enabled;
00386     PeripheralPrivacyConfiguration_t _peripheral_privacy_configuration;
00387     CentralPrivacyConfiguration_t _central_privacy_configuration;
00388     ble::address_t _random_static_identity_address;
00389     bool _random_address_rotating;
00390     
00391     mbed::Timeout _advertising_timeout;
00392     mbed::Timeout _scan_timeout;
00393     mbed::Ticker _address_rotation_ticker;
00394     pal::ConnectionEventMonitor::EventHandler *_connection_event_handler;
00395 };
00396 
00397 }
00398 }
00399 
00400 #endif /* MBED_BLE_GENERIC_GAP */