Holla back
Fork of BLE_API by
Embed:
(wiki syntax)
Show/hide line numbers
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 "mbed.h" 00021 #include "blecommon.h" 00022 #include "GapAdvertisingData.h" 00023 #include "GapAdvertisingParams.h" 00024 #include "GapEvents.h" 00025 #include "CallChainOfFunctionPointersWithContext.h" 00026 00027 00028 /**************************************************************************/ 00029 /*! 00030 \brief 00031 The base class used to abstract GAP functionality to a specific radio 00032 transceiver, SOC or BLE Stack. 00033 */ 00034 /**************************************************************************/ 00035 class Gap 00036 { 00037 public: 00038 typedef enum addr_type_e { 00039 ADDR_TYPE_PUBLIC = 0, 00040 ADDR_TYPE_RANDOM_STATIC, 00041 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, 00042 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE 00043 } addr_type_t; 00044 00045 /** 00046 * enumeration for disconnection reasons. The values for these reasons are 00047 * derived from Nordic's implementation; but the reasons are meant to be 00048 * independent of the transport. If you are returned a reason which is not 00049 * covered by this enumeration, then please refer to the underlying 00050 * transport library. 00051 */ 00052 enum DisconnectionReason_t { 00053 REMOTE_USER_TERMINATED_CONNECTION = 0x13, 00054 LOCAL_HOST_TERMINATED_CONNECTION = 0x16, 00055 CONN_INTERVAL_UNACCEPTABLE = 0x3B, 00056 }; 00057 00058 /* Describes the current state of the device (more than one bit can be set) */ 00059 typedef struct GapState_s { 00060 unsigned advertising : 1; /**< peripheral is currently advertising */ 00061 unsigned connected : 1; /**< peripheral is connected to a central */ 00062 } GapState_t; 00063 00064 typedef uint16_t Handle_t; 00065 00066 typedef struct { 00067 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00068 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00069 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ 00070 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00071 } ConnectionParams_t; 00072 00073 public: 00074 /* These functions must be defined in the sub-class */ 00075 virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0; 00076 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; 00077 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; 00078 virtual ble_error_t stopAdvertising(void) = 0; 00079 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0; 00080 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; 00081 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0; 00082 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0; 00083 00084 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0; 00085 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0; 00086 virtual ble_error_t setAppearance(uint16_t appearance) = 0; 00087 virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0; 00088 00089 typedef void (*EventCallback_t)(void); 00090 00091 /* Event callback handlers */ 00092 void setOnTimeout(EventCallback_t callback) { 00093 onTimeout = callback; 00094 } 00095 void setOnConnection(void (*callback)(Handle_t handle, const ConnectionParams_t *eventDataP)) { 00096 onConnection.add(callback); 00097 } 00098 template <typename T> 00099 void setOnConnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, const ConnectionParams_t *context)) { 00100 onConnection.add(objPtr, memberPtr); 00101 } 00102 00103 void setOnDisconnection(void (*callback)(Handle_t handle, DisconnectionReason_t reason)) { 00104 onDisconnection.add(callback); 00105 } 00106 template <typename T> 00107 void setOnDisconnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, DisconnectionReason_t reason)) { 00108 onDisconnection.add(objPtr, memberPtr); 00109 } 00110 00111 void processConnectionEvent(Handle_t handle, const ConnectionParams_t *params) { 00112 state.connected = 1; 00113 if (onConnection.hasCallbacksAttached()) { 00114 onConnection.call(handle, params); 00115 } 00116 } 00117 00118 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) { 00119 state.connected = 0; 00120 if (onDisconnection.hasCallbacksAttached()) { 00121 onDisconnection.call(handle, reason); 00122 } 00123 } 00124 00125 void processEvent(GapEvents::gapEvent_e type) { 00126 switch (type) { 00127 case GapEvents::GAP_EVENT_TIMEOUT: 00128 state.advertising = 0; 00129 if (onTimeout) { 00130 onTimeout(); 00131 } 00132 break; 00133 } 00134 } 00135 00136 GapState_t getState(void) const { 00137 return state; 00138 } 00139 00140 protected: 00141 Gap() : state(), onTimeout(NULL), onConnection(), onDisconnection() { 00142 /* empty */ 00143 } 00144 00145 protected: 00146 GapState_t state; 00147 00148 private: 00149 EventCallback_t onTimeout; 00150 CallChainOfFunctionPointersWithContext<Handle_t, const ConnectionParams_t *> onConnection; 00151 CallChainOfFunctionPointersWithContext<Handle_t, DisconnectionReason_t> onDisconnection; 00152 }; 00153 00154 #endif // ifndef __GAP_H__
Generated on Tue Jul 12 2022 14:23:51 by 1.7.2