Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
Gap.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 __GAP_H__ 00018 #define __GAP_H__ 00019 00020 #include "GapAdvertisingData.h" 00021 #include "GapAdvertisingParams.h" 00022 #include "GapEvents.h" 00023 #include "CallChain.h" 00024 00025 using namespace mbed; 00026 00027 class Gap { 00028 public: 00029 typedef enum addr_type_e { 00030 ADDR_TYPE_PUBLIC = 0, 00031 ADDR_TYPE_RANDOM_STATIC, 00032 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, 00033 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE 00034 } addr_type_t; 00035 00036 static const unsigned ADDR_LEN = 6; 00037 typedef uint8_t address_t[ADDR_LEN]; 00038 00039 /** 00040 * Enumeration for disconnection reasons. The values for these reasons are 00041 * derived from Nordic's implementation; but the reasons are meant to be 00042 * independent of the transport. If you are returned a reason which is not 00043 * covered by this enumeration, then please refer to the underlying 00044 * transport library. 00045 */ 00046 enum DisconnectionReason_t { 00047 REMOTE_USER_TERMINATED_CONNECTION = 0x13, 00048 LOCAL_HOST_TERMINATED_CONNECTION = 0x16, 00049 CONN_INTERVAL_UNACCEPTABLE = 0x3B, 00050 }; 00051 00052 /* Describes the current state of the device (more than one bit can be set) */ 00053 typedef struct GapState_s { 00054 unsigned advertising : 1; /**< peripheral is currently advertising */ 00055 unsigned connected : 1; /**< peripheral is connected to a central */ 00056 } GapState_t; 00057 00058 typedef uint16_t Handle_t; 00059 00060 typedef struct { 00061 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00062 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00063 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ 00064 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00065 } ConnectionParams_t; 00066 00067 static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */ 00068 static const uint16_t UNIT_0_625_MS = 650; /**< Number of microseconds in 0.625 milliseconds. */ 00069 static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) { 00070 return (durationInMillis * 1000) / UNIT_1_25_MS; 00071 } 00072 static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) { 00073 return (durationInMillis * 1000) / UNIT_0_625_MS; 00074 } 00075 00076 typedef void (*EventCallback_t)(void); 00077 typedef void (*ConnectionEventCallback_t)(Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *); 00078 typedef void (*DisconnectionEventCallback_t)(Handle_t, DisconnectionReason_t); 00079 00080 friend class BLEDevice; 00081 private: 00082 /* These functions must be defined in the sub-class */ 00083 virtual ble_error_t setAddress(addr_type_t type, const address_t address) = 0; 00084 virtual ble_error_t getAddress(addr_type_t *typeP, address_t address) = 0; 00085 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; 00086 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; 00087 virtual ble_error_t stopAdvertising(void) = 0; 00088 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0; 00089 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; 00090 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0; 00091 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0; 00092 00093 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0; 00094 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0; 00095 virtual ble_error_t setAppearance(uint16_t appearance) = 0; 00096 virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0; 00097 00098 private: 00099 /* Event callback handlers */ 00100 void setOnTimeout(EventCallback_t callback) {onTimeout = callback;} 00101 void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;} 00102 00103 /** 00104 * Set the application callback for disconnection events. 00105 * @param callback 00106 * Pointer to the unique callback. 00107 */ 00108 void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;} 00109 00110 /** 00111 * Append to a chain of callbacks to be invoked upon disconnection; these 00112 * callbacks receive no context and are therefore different from the 00113 * onDisconnection callback. 00114 * @param callback 00115 * function pointer to be invoked upon disconnection; receives no context. 00116 * 00117 * @note the disconnection CallChain should have been merged with 00118 * onDisconnctionCallback; but this was not possible because 00119 * FunctionPointer (which is a building block for CallChain) doesn't 00120 * accept variadic templates. 00121 */ 00122 template<typename T> 00123 void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);} 00124 00125 GapState_t getState(void) const { 00126 return state; 00127 } 00128 00129 protected: 00130 /* Default constructor. */ 00131 Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL), disconnectionCallChain() { 00132 /* empty */ 00133 } 00134 00135 public: 00136 void processConnectionEvent(Handle_t handle, addr_type_t type, const address_t addr, const ConnectionParams_t *params) { 00137 state.connected = 1; 00138 if (onConnection) { 00139 onConnection(handle, type, addr, params); 00140 } 00141 } 00142 00143 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) { 00144 state.connected = 0; 00145 if (onDisconnection) { 00146 onDisconnection(handle, reason); 00147 } 00148 disconnectionCallChain.call(); 00149 } 00150 00151 void processEvent(GapEvents::gapEvent_e type) { 00152 switch (type) { 00153 case GapEvents::GAP_EVENT_TIMEOUT: 00154 state.advertising = 0; 00155 if (onTimeout) { 00156 onTimeout(); 00157 } 00158 break; 00159 } 00160 } 00161 00162 protected: 00163 GapState_t state; 00164 00165 private: 00166 EventCallback_t onTimeout; 00167 ConnectionEventCallback_t onConnection; 00168 DisconnectionEventCallback_t onDisconnection; 00169 CallChain disconnectionCallChain; 00170 00171 private: 00172 /* disallow copy and assignment */ 00173 Gap(const Gap &); 00174 Gap& operator=(const Gap &); 00175 }; 00176 00177 #endif // ifndef __GAP_H__
Generated on Tue Jul 12 2022 21:47:54 by
1.7.2
