Improve readability with getHandle inline
Fork of BLE_API by
public/Gap.h@257:6be2b4b0cd71, 2015-01-12 (annotated)
- Committer:
- schilit
- Date:
- Mon Jan 12 14:49:53 2015 -0800
- Revision:
- 257:6be2b4b0cd71
- Parent:
- 246:aa639ef2f290
Merged the BLE_API from upstream
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
106:a20be740075d | 1 | /* mbed Microcontroller Library |
Rohit Grover |
106:a20be740075d | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
106:a20be740075d | 3 | * |
Rohit Grover |
106:a20be740075d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
106:a20be740075d | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
106:a20be740075d | 6 | * You may obtain a copy of the License at |
Rohit Grover |
106:a20be740075d | 7 | * |
Rohit Grover |
106:a20be740075d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
106:a20be740075d | 9 | * |
Rohit Grover |
106:a20be740075d | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
106:a20be740075d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
106:a20be740075d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
106:a20be740075d | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
106:a20be740075d | 14 | * limitations under the License. |
Rohit Grover |
106:a20be740075d | 15 | */ |
Rohit Grover |
106:a20be740075d | 16 | |
Rohit Grover |
106:a20be740075d | 17 | #ifndef __GAP_H__ |
Rohit Grover |
106:a20be740075d | 18 | #define __GAP_H__ |
Rohit Grover |
106:a20be740075d | 19 | |
Rohit Grover |
106:a20be740075d | 20 | #include "GapAdvertisingData.h" |
Rohit Grover |
106:a20be740075d | 21 | #include "GapAdvertisingParams.h" |
Rohit Grover |
106:a20be740075d | 22 | #include "GapEvents.h" |
rgrover1 | 246:aa639ef2f290 | 23 | #include "CallChain.h" |
rgrover1 | 246:aa639ef2f290 | 24 | |
rgrover1 | 246:aa639ef2f290 | 25 | using namespace mbed; |
Rohit Grover |
106:a20be740075d | 26 | |
rgrover1 | 143:8bdf577b1598 | 27 | class Gap { |
Rohit Grover |
106:a20be740075d | 28 | public: |
Rohit Grover |
106:a20be740075d | 29 | typedef enum addr_type_e { |
Rohit Grover |
106:a20be740075d | 30 | ADDR_TYPE_PUBLIC = 0, |
Rohit Grover |
106:a20be740075d | 31 | ADDR_TYPE_RANDOM_STATIC, |
Rohit Grover |
106:a20be740075d | 32 | ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, |
Rohit Grover |
106:a20be740075d | 33 | ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE |
Rohit Grover |
106:a20be740075d | 34 | } addr_type_t; |
Rohit Grover |
106:a20be740075d | 35 | |
Rohit Grover |
123:fede41ce9407 | 36 | static const unsigned ADDR_LEN = 6; |
rgrover1 | 140:407d134c179d | 37 | typedef uint8_t address_t[ADDR_LEN]; |
Rohit Grover |
123:fede41ce9407 | 38 | |
Rohit Grover |
117:0fb20195102b | 39 | /** |
rgrover1 | 143:8bdf577b1598 | 40 | * Enumeration for disconnection reasons. The values for these reasons are |
Rohit Grover |
117:0fb20195102b | 41 | * derived from Nordic's implementation; but the reasons are meant to be |
Rohit Grover |
117:0fb20195102b | 42 | * independent of the transport. If you are returned a reason which is not |
Rohit Grover |
117:0fb20195102b | 43 | * covered by this enumeration, then please refer to the underlying |
Rohit Grover |
117:0fb20195102b | 44 | * transport library. |
Rohit Grover |
117:0fb20195102b | 45 | */ |
Rohit Grover |
116:ca826083980e | 46 | enum DisconnectionReason_t { |
Rohit Grover |
117:0fb20195102b | 47 | REMOTE_USER_TERMINATED_CONNECTION = 0x13, |
Rohit Grover |
117:0fb20195102b | 48 | LOCAL_HOST_TERMINATED_CONNECTION = 0x16, |
Rohit Grover |
117:0fb20195102b | 49 | CONN_INTERVAL_UNACCEPTABLE = 0x3B, |
Rohit Grover |
116:ca826083980e | 50 | }; |
Rohit Grover |
116:ca826083980e | 51 | |
Rohit Grover |
106:a20be740075d | 52 | /* Describes the current state of the device (more than one bit can be set) */ |
Rohit Grover |
106:a20be740075d | 53 | typedef struct GapState_s { |
Rohit Grover |
106:a20be740075d | 54 | unsigned advertising : 1; /**< peripheral is currently advertising */ |
Rohit Grover |
106:a20be740075d | 55 | unsigned connected : 1; /**< peripheral is connected to a central */ |
Rohit Grover |
106:a20be740075d | 56 | } GapState_t; |
Rohit Grover |
106:a20be740075d | 57 | |
Rohit Grover |
106:a20be740075d | 58 | typedef uint16_t Handle_t; |
Rohit Grover |
106:a20be740075d | 59 | |
Rohit Grover |
106:a20be740075d | 60 | typedef struct { |
rgrover1 | 140:407d134c179d | 61 | uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
rgrover1 | 140:407d134c179d | 62 | uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
rgrover1 | 140:407d134c179d | 63 | uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ |
rgrover1 | 140:407d134c179d | 64 | uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
Rohit Grover |
106:a20be740075d | 65 | } ConnectionParams_t; |
Rohit Grover |
106:a20be740075d | 66 | |
rgrover1 | 146:543877bb1f52 | 67 | static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */ |
rgrover1 | 146:543877bb1f52 | 68 | static const uint16_t UNIT_0_625_MS = 650; /**< Number of microseconds in 0.625 milliseconds. */ |
rgrover1 | 140:407d134c179d | 69 | static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) { |
rgrover1 | 140:407d134c179d | 70 | return (durationInMillis * 1000) / UNIT_1_25_MS; |
rgrover1 | 140:407d134c179d | 71 | } |
rgrover1 | 146:543877bb1f52 | 72 | static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) { |
rgrover1 | 146:543877bb1f52 | 73 | return (durationInMillis * 1000) / UNIT_0_625_MS; |
rgrover1 | 146:543877bb1f52 | 74 | } |
rgrover1 | 140:407d134c179d | 75 | |
rgrover1 | 142:42f038a81ea7 | 76 | typedef void (*EventCallback_t)(void); |
rgrover1 | 142:42f038a81ea7 | 77 | typedef void (*ConnectionEventCallback_t)(Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *); |
rgrover1 | 142:42f038a81ea7 | 78 | typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t); |
rgrover1 | 142:42f038a81ea7 | 79 | |
rgrover1 | 142:42f038a81ea7 | 80 | friend class BLEDevice; |
rgrover1 | 142:42f038a81ea7 | 81 | private: |
Rohit Grover |
106:a20be740075d | 82 | /* These functions must be defined in the sub-class */ |
rgrover1 | 140:407d134c179d | 83 | virtual ble_error_t setAddress(addr_type_t type, const address_t address) = 0; |
rgrover1 | 140:407d134c179d | 84 | virtual ble_error_t getAddress(addr_type_t *typeP, address_t address) = 0; |
Rohit Grover |
106:a20be740075d | 85 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; |
rgrover1 | 140:407d134c179d | 86 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; |
rgrover1 | 140:407d134c179d | 87 | virtual ble_error_t stopAdvertising(void) = 0; |
rgrover1 | 140:407d134c179d | 88 | virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0; |
rgrover1 | 140:407d134c179d | 89 | virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; |
rgrover1 | 140:407d134c179d | 90 | virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0; |
rgrover1 | 140:407d134c179d | 91 | virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0; |
Rohit Grover |
106:a20be740075d | 92 | |
rgrover1 | 140:407d134c179d | 93 | virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0; |
Rohit Grover |
116:ca826083980e | 94 | virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0; |
rgrover1 | 140:407d134c179d | 95 | virtual ble_error_t setAppearance(uint16_t appearance) = 0; |
rgrover1 | 140:407d134c179d | 96 | virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0; |
Rohit Grover |
116:ca826083980e | 97 | |
rgrover1 | 142:42f038a81ea7 | 98 | private: |
rgrover1 | 142:42f038a81ea7 | 99 | /* Event callback handlers */ |
rgrover1 | 142:42f038a81ea7 | 100 | void setOnTimeout(EventCallback_t callback) {onTimeout = callback;} |
rgrover1 | 142:42f038a81ea7 | 101 | void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;} |
rgrover1 | 246:aa639ef2f290 | 102 | |
rgrover1 | 246:aa639ef2f290 | 103 | /** |
rgrover1 | 246:aa639ef2f290 | 104 | * Set the application callback for disconnection events. |
rgrover1 | 246:aa639ef2f290 | 105 | * @param callback |
rgrover1 | 246:aa639ef2f290 | 106 | * Pointer to the unique callback. |
rgrover1 | 246:aa639ef2f290 | 107 | */ |
rgrover1 | 142:42f038a81ea7 | 108 | void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;} |
Rohit Grover |
106:a20be740075d | 109 | |
rgrover1 | 246:aa639ef2f290 | 110 | /** |
rgrover1 | 246:aa639ef2f290 | 111 | * Append to a chain of callbacks to be invoked upon disconnection; these |
rgrover1 | 246:aa639ef2f290 | 112 | * callbacks receive no context and are therefore different from the |
rgrover1 | 246:aa639ef2f290 | 113 | * onDisconnection callback. |
rgrover1 | 246:aa639ef2f290 | 114 | * @param callback |
rgrover1 | 246:aa639ef2f290 | 115 | * function pointer to be invoked upon disconnection; receives no context. |
rgrover1 | 246:aa639ef2f290 | 116 | * |
rgrover1 | 246:aa639ef2f290 | 117 | * @note the disconnection CallChain should have been merged with |
rgrover1 | 246:aa639ef2f290 | 118 | * onDisconnctionCallback; but this was not possible because |
rgrover1 | 246:aa639ef2f290 | 119 | * FunctionPointer (which is a building block for CallChain) doesn't |
rgrover1 | 246:aa639ef2f290 | 120 | * accept variadic templates. |
rgrover1 | 246:aa639ef2f290 | 121 | */ |
rgrover1 | 246:aa639ef2f290 | 122 | template<typename T> |
rgrover1 | 246:aa639ef2f290 | 123 | void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);} |
rgrover1 | 246:aa639ef2f290 | 124 | |
rgrover1 | 142:42f038a81ea7 | 125 | GapState_t getState(void) const { |
rgrover1 | 142:42f038a81ea7 | 126 | return state; |
Rohit Grover |
106:a20be740075d | 127 | } |
rgrover1 | 140:407d134c179d | 128 | |
rgrover1 | 142:42f038a81ea7 | 129 | protected: |
rgrover1 | 246:aa639ef2f290 | 130 | /* Default constructor. */ |
rgrover1 | 246:aa639ef2f290 | 131 | Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL), disconnectionCallChain() { |
rgrover1 | 142:42f038a81ea7 | 132 | /* empty */ |
Rohit Grover |
106:a20be740075d | 133 | } |
rgrover1 | 140:407d134c179d | 134 | |
rgrover1 | 142:42f038a81ea7 | 135 | public: |
rgrover1 | 140:407d134c179d | 136 | void processConnectionEvent(Handle_t handle, addr_type_t type, const address_t addr, const ConnectionParams_t *params) { |
Rohit Grover |
116:ca826083980e | 137 | state.connected = 1; |
Rohit Grover |
116:ca826083980e | 138 | if (onConnection) { |
rgrover1 | 140:407d134c179d | 139 | onConnection(handle, type, addr, params); |
Rohit Grover |
116:ca826083980e | 140 | } |
Rohit Grover |
116:ca826083980e | 141 | } |
Rohit Grover |
116:ca826083980e | 142 | |
Rohit Grover |
116:ca826083980e | 143 | void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) { |
Rohit Grover |
116:ca826083980e | 144 | state.connected = 0; |
Rohit Grover |
116:ca826083980e | 145 | if (onDisconnection) { |
Rohit Grover |
116:ca826083980e | 146 | onDisconnection(handle, reason); |
Rohit Grover |
106:a20be740075d | 147 | } |
rgrover1 | 246:aa639ef2f290 | 148 | disconnectionCallChain.call(); |
Rohit Grover |
106:a20be740075d | 149 | } |
Rohit Grover |
106:a20be740075d | 150 | |
Rohit Grover |
106:a20be740075d | 151 | void processEvent(GapEvents::gapEvent_e type) { |
Rohit Grover |
106:a20be740075d | 152 | switch (type) { |
Rohit Grover |
106:a20be740075d | 153 | case GapEvents::GAP_EVENT_TIMEOUT: |
Rohit Grover |
106:a20be740075d | 154 | state.advertising = 0; |
Rohit Grover |
106:a20be740075d | 155 | if (onTimeout) { |
Rohit Grover |
106:a20be740075d | 156 | onTimeout(); |
Rohit Grover |
106:a20be740075d | 157 | } |
Rohit Grover |
106:a20be740075d | 158 | break; |
Rohit Grover |
106:a20be740075d | 159 | } |
Rohit Grover |
106:a20be740075d | 160 | } |
Rohit Grover |
106:a20be740075d | 161 | |
Rohit Grover |
106:a20be740075d | 162 | protected: |
rgrover1 | 140:407d134c179d | 163 | GapState_t state; |
Rohit Grover |
106:a20be740075d | 164 | |
Rohit Grover |
106:a20be740075d | 165 | private: |
Rohit Grover |
116:ca826083980e | 166 | EventCallback_t onTimeout; |
Rohit Grover |
116:ca826083980e | 167 | ConnectionEventCallback_t onConnection; |
Rohit Grover |
116:ca826083980e | 168 | DisconnectionEventCallback_t onDisconnection; |
rgrover1 | 246:aa639ef2f290 | 169 | CallChain disconnectionCallChain; |
rgrover1 | 142:42f038a81ea7 | 170 | |
rgrover1 | 142:42f038a81ea7 | 171 | private: |
rgrover1 | 143:8bdf577b1598 | 172 | /* disallow copy and assignment */ |
rgrover1 | 142:42f038a81ea7 | 173 | Gap(const Gap &); |
rgrover1 | 142:42f038a81ea7 | 174 | Gap& operator=(const Gap &); |
Rohit Grover |
106:a20be740075d | 175 | }; |
Rohit Grover |
106:a20be740075d | 176 | |
rgrover1 | 126:fdebe4d5d62f | 177 | #endif // ifndef __GAP_H__ |