IOTIO
Fork of Nucleo_BLE_API by
public/Gap.h@2:584d5bedac75, 2016-10-29 (annotated)
- Committer:
- 16038618
- Date:
- Sat Oct 29 14:16:12 2016 +0000
- Revision:
- 2:584d5bedac75
- Parent:
- 0:289fd2dae405
IOTIO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:289fd2dae405 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:289fd2dae405 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:289fd2dae405 | 3 | * |
sjallouli | 0:289fd2dae405 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:289fd2dae405 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:289fd2dae405 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:289fd2dae405 | 7 | * |
sjallouli | 0:289fd2dae405 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:289fd2dae405 | 9 | * |
sjallouli | 0:289fd2dae405 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:289fd2dae405 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:289fd2dae405 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:289fd2dae405 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:289fd2dae405 | 14 | * limitations under the License. |
sjallouli | 0:289fd2dae405 | 15 | */ |
sjallouli | 0:289fd2dae405 | 16 | |
sjallouli | 0:289fd2dae405 | 17 | #ifndef __GAP_H__ |
sjallouli | 0:289fd2dae405 | 18 | #define __GAP_H__ |
sjallouli | 0:289fd2dae405 | 19 | |
sjallouli | 0:289fd2dae405 | 20 | #include "mbed.h" |
sjallouli | 0:289fd2dae405 | 21 | #include "blecommon.h" |
sjallouli | 0:289fd2dae405 | 22 | #include "GapAdvertisingData.h" |
sjallouli | 0:289fd2dae405 | 23 | #include "GapAdvertisingParams.h" |
sjallouli | 0:289fd2dae405 | 24 | #include "GapEvents.h" |
sjallouli | 0:289fd2dae405 | 25 | |
sjallouli | 0:289fd2dae405 | 26 | /**************************************************************************/ |
sjallouli | 0:289fd2dae405 | 27 | /*! |
sjallouli | 0:289fd2dae405 | 28 | \brief |
sjallouli | 0:289fd2dae405 | 29 | The base class used to abstract GAP functionality to a specific radio |
sjallouli | 0:289fd2dae405 | 30 | transceiver, SOC or BLE Stack. |
sjallouli | 0:289fd2dae405 | 31 | */ |
sjallouli | 0:289fd2dae405 | 32 | /**************************************************************************/ |
sjallouli | 0:289fd2dae405 | 33 | class Gap |
sjallouli | 0:289fd2dae405 | 34 | { |
sjallouli | 0:289fd2dae405 | 35 | public: |
sjallouli | 0:289fd2dae405 | 36 | typedef enum addr_type_e { |
sjallouli | 0:289fd2dae405 | 37 | ADDR_TYPE_PUBLIC = 0, |
sjallouli | 0:289fd2dae405 | 38 | ADDR_TYPE_RANDOM_STATIC, |
sjallouli | 0:289fd2dae405 | 39 | ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, |
sjallouli | 0:289fd2dae405 | 40 | ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE |
sjallouli | 0:289fd2dae405 | 41 | } addr_type_t; |
sjallouli | 0:289fd2dae405 | 42 | |
sjallouli | 0:289fd2dae405 | 43 | /** |
sjallouli | 0:289fd2dae405 | 44 | * enumeration for disconnection reasons. The values for these reasons are |
sjallouli | 0:289fd2dae405 | 45 | * derived from Nordic's implementation; but the reasons are meant to be |
sjallouli | 0:289fd2dae405 | 46 | * independent of the transport. If you are returned a reason which is not |
sjallouli | 0:289fd2dae405 | 47 | * covered by this enumeration, then please refer to the underlying |
sjallouli | 0:289fd2dae405 | 48 | * transport library. |
sjallouli | 0:289fd2dae405 | 49 | */ |
sjallouli | 0:289fd2dae405 | 50 | enum DisconnectionReason_t { |
sjallouli | 0:289fd2dae405 | 51 | REMOTE_USER_TERMINATED_CONNECTION = 0x13, |
sjallouli | 0:289fd2dae405 | 52 | LOCAL_HOST_TERMINATED_CONNECTION = 0x16, |
sjallouli | 0:289fd2dae405 | 53 | CONN_INTERVAL_UNACCEPTABLE = 0x3B, |
sjallouli | 0:289fd2dae405 | 54 | }; |
sjallouli | 0:289fd2dae405 | 55 | |
sjallouli | 0:289fd2dae405 | 56 | /* Describes the current state of the device (more than one bit can be set) */ |
sjallouli | 0:289fd2dae405 | 57 | typedef struct GapState_s { |
sjallouli | 0:289fd2dae405 | 58 | unsigned advertising : 1; /**< peripheral is currently advertising */ |
sjallouli | 0:289fd2dae405 | 59 | unsigned connected : 1; /**< peripheral is connected to a central */ |
sjallouli | 0:289fd2dae405 | 60 | } GapState_t; |
sjallouli | 0:289fd2dae405 | 61 | |
sjallouli | 0:289fd2dae405 | 62 | typedef uint16_t Handle_t; |
sjallouli | 0:289fd2dae405 | 63 | |
sjallouli | 0:289fd2dae405 | 64 | typedef struct { |
sjallouli | 0:289fd2dae405 | 65 | uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
sjallouli | 0:289fd2dae405 | 66 | uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
sjallouli | 0:289fd2dae405 | 67 | uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ |
sjallouli | 0:289fd2dae405 | 68 | uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
sjallouli | 0:289fd2dae405 | 69 | } ConnectionParams_t; |
sjallouli | 0:289fd2dae405 | 70 | |
sjallouli | 0:289fd2dae405 | 71 | public: |
sjallouli | 0:289fd2dae405 | 72 | /* These functions must be defined in the sub-class */ |
sjallouli | 0:289fd2dae405 | 73 | virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0; |
sjallouli | 0:289fd2dae405 | 74 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; |
sjallouli | 0:289fd2dae405 | 75 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; |
sjallouli | 0:289fd2dae405 | 76 | virtual ble_error_t stopAdvertising(void) = 0; |
sjallouli | 0:289fd2dae405 | 77 | virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0; |
sjallouli | 0:289fd2dae405 | 78 | virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; |
sjallouli | 0:289fd2dae405 | 79 | virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0; |
sjallouli | 0:289fd2dae405 | 80 | virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0; |
sjallouli | 0:289fd2dae405 | 81 | |
sjallouli | 0:289fd2dae405 | 82 | virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0; |
sjallouli | 0:289fd2dae405 | 83 | virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0; |
sjallouli | 0:289fd2dae405 | 84 | virtual ble_error_t setAppearance(uint16_t appearance) = 0; |
sjallouli | 0:289fd2dae405 | 85 | virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0; |
sjallouli | 0:289fd2dae405 | 86 | |
sjallouli | 0:289fd2dae405 | 87 | typedef void (*EventCallback_t)(void); |
sjallouli | 0:289fd2dae405 | 88 | typedef void (*ConnectionEventCallback_t)(Handle_t, const ConnectionParams_t *); |
sjallouli | 0:289fd2dae405 | 89 | typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t); |
sjallouli | 0:289fd2dae405 | 90 | |
sjallouli | 0:289fd2dae405 | 91 | /* Event callback handlers */ |
sjallouli | 0:289fd2dae405 | 92 | void setOnTimeout(EventCallback_t callback) { |
sjallouli | 0:289fd2dae405 | 93 | onTimeout = callback; |
sjallouli | 0:289fd2dae405 | 94 | } |
sjallouli | 0:289fd2dae405 | 95 | void setOnConnection(ConnectionEventCallback_t callback) { |
sjallouli | 0:289fd2dae405 | 96 | onConnection = callback; |
sjallouli | 0:289fd2dae405 | 97 | } |
sjallouli | 0:289fd2dae405 | 98 | void setOnDisconnection(DisconnectionEventCallback_t callback) { |
sjallouli | 0:289fd2dae405 | 99 | onDisconnection = callback; |
sjallouli | 0:289fd2dae405 | 100 | } |
sjallouli | 0:289fd2dae405 | 101 | |
sjallouli | 0:289fd2dae405 | 102 | void processConnectionEvent(Handle_t handle, const ConnectionParams_t *params) { |
sjallouli | 0:289fd2dae405 | 103 | state.connected = 1; |
sjallouli | 0:289fd2dae405 | 104 | if (onConnection) { |
sjallouli | 0:289fd2dae405 | 105 | onConnection(handle, params); |
sjallouli | 0:289fd2dae405 | 106 | } |
sjallouli | 0:289fd2dae405 | 107 | } |
sjallouli | 0:289fd2dae405 | 108 | |
sjallouli | 0:289fd2dae405 | 109 | void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) { |
sjallouli | 0:289fd2dae405 | 110 | state.connected = 0; |
sjallouli | 0:289fd2dae405 | 111 | if (onDisconnection) { |
sjallouli | 0:289fd2dae405 | 112 | onDisconnection(handle, reason); |
sjallouli | 0:289fd2dae405 | 113 | } |
sjallouli | 0:289fd2dae405 | 114 | } |
sjallouli | 0:289fd2dae405 | 115 | |
sjallouli | 0:289fd2dae405 | 116 | void processEvent(GapEvents::gapEvent_e type) { |
sjallouli | 0:289fd2dae405 | 117 | switch (type) { |
sjallouli | 0:289fd2dae405 | 118 | case GapEvents::GAP_EVENT_TIMEOUT: |
sjallouli | 0:289fd2dae405 | 119 | state.advertising = 0; |
sjallouli | 0:289fd2dae405 | 120 | if (onTimeout) { |
sjallouli | 0:289fd2dae405 | 121 | onTimeout(); |
sjallouli | 0:289fd2dae405 | 122 | } |
sjallouli | 0:289fd2dae405 | 123 | break; |
sjallouli | 0:289fd2dae405 | 124 | } |
sjallouli | 0:289fd2dae405 | 125 | } |
sjallouli | 0:289fd2dae405 | 126 | |
sjallouli | 0:289fd2dae405 | 127 | GapState_t getState(void) const { |
sjallouli | 0:289fd2dae405 | 128 | return state; |
sjallouli | 0:289fd2dae405 | 129 | } |
sjallouli | 0:289fd2dae405 | 130 | |
sjallouli | 0:289fd2dae405 | 131 | protected: |
sjallouli | 0:289fd2dae405 | 132 | Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) { |
sjallouli | 0:289fd2dae405 | 133 | /* empty */ |
sjallouli | 0:289fd2dae405 | 134 | } |
sjallouli | 0:289fd2dae405 | 135 | |
sjallouli | 0:289fd2dae405 | 136 | protected: |
sjallouli | 0:289fd2dae405 | 137 | GapState_t state; |
sjallouli | 0:289fd2dae405 | 138 | |
sjallouli | 0:289fd2dae405 | 139 | private: |
sjallouli | 0:289fd2dae405 | 140 | EventCallback_t onTimeout; |
sjallouli | 0:289fd2dae405 | 141 | ConnectionEventCallback_t onConnection; |
sjallouli | 0:289fd2dae405 | 142 | DisconnectionEventCallback_t onDisconnection; |
sjallouli | 0:289fd2dae405 | 143 | }; |
sjallouli | 0:289fd2dae405 | 144 | |
sjallouli | 0:289fd2dae405 | 145 | #endif // ifndef __GAP_H__ |