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