Holla back

Fork of BLE_API by Bluetooth Low Energy

Committer:
jakerosenthal@gmail.com
Date:
Fri Oct 10 17:32:22 2014 -0700
Branch:
2chains
Revision:
123:d2cdf4ebe524
Parent:
117:0fb20195102b
first attempt at chaining onconnect and ondisconnect

Who changed what in which revision?

UserRevisionLine numberNew 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 "mbed.h"
Rohit Grover 106:a20be740075d 21 #include "blecommon.h"
Rohit Grover 106:a20be740075d 22 #include "GapAdvertisingData.h"
Rohit Grover 106:a20be740075d 23 #include "GapAdvertisingParams.h"
Rohit Grover 106:a20be740075d 24 #include "GapEvents.h"
jakerosenthal@gmail.com 123:d2cdf4ebe524 25 #include "CallChainOfFunctionPointersWithContext.h"
jakerosenthal@gmail.com 123:d2cdf4ebe524 26
Rohit Grover 106:a20be740075d 27
Rohit Grover 106:a20be740075d 28 /**************************************************************************/
Rohit Grover 106:a20be740075d 29 /*!
Rohit Grover 106:a20be740075d 30 \brief
Rohit Grover 106:a20be740075d 31 The base class used to abstract GAP functionality to a specific radio
Rohit Grover 106:a20be740075d 32 transceiver, SOC or BLE Stack.
Rohit Grover 106:a20be740075d 33 */
Rohit Grover 106:a20be740075d 34 /**************************************************************************/
Rohit Grover 106:a20be740075d 35 class Gap
Rohit Grover 106:a20be740075d 36 {
Rohit Grover 106:a20be740075d 37 public:
Rohit Grover 106:a20be740075d 38 typedef enum addr_type_e {
Rohit Grover 106:a20be740075d 39 ADDR_TYPE_PUBLIC = 0,
Rohit Grover 106:a20be740075d 40 ADDR_TYPE_RANDOM_STATIC,
Rohit Grover 106:a20be740075d 41 ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
Rohit Grover 106:a20be740075d 42 ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE
Rohit Grover 106:a20be740075d 43 } addr_type_t;
Rohit Grover 106:a20be740075d 44
Rohit Grover 117:0fb20195102b 45 /**
Rohit Grover 117:0fb20195102b 46 * enumeration for disconnection reasons. The values for these reasons are
Rohit Grover 117:0fb20195102b 47 * derived from Nordic's implementation; but the reasons are meant to be
Rohit Grover 117:0fb20195102b 48 * independent of the transport. If you are returned a reason which is not
Rohit Grover 117:0fb20195102b 49 * covered by this enumeration, then please refer to the underlying
Rohit Grover 117:0fb20195102b 50 * transport library.
Rohit Grover 117:0fb20195102b 51 */
Rohit Grover 116:ca826083980e 52 enum DisconnectionReason_t {
Rohit Grover 117:0fb20195102b 53 REMOTE_USER_TERMINATED_CONNECTION = 0x13,
Rohit Grover 117:0fb20195102b 54 LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
Rohit Grover 117:0fb20195102b 55 CONN_INTERVAL_UNACCEPTABLE = 0x3B,
Rohit Grover 116:ca826083980e 56 };
Rohit Grover 116:ca826083980e 57
Rohit Grover 106:a20be740075d 58 /* Describes the current state of the device (more than one bit can be set) */
Rohit Grover 106:a20be740075d 59 typedef struct GapState_s {
Rohit Grover 106:a20be740075d 60 unsigned advertising : 1; /**< peripheral is currently advertising */
Rohit Grover 106:a20be740075d 61 unsigned connected : 1; /**< peripheral is connected to a central */
Rohit Grover 106:a20be740075d 62 } GapState_t;
Rohit Grover 106:a20be740075d 63
Rohit Grover 106:a20be740075d 64 typedef uint16_t Handle_t;
Rohit Grover 106:a20be740075d 65
Rohit Grover 106:a20be740075d 66 typedef struct {
Rohit Grover 106:a20be740075d 67 uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
Rohit Grover 106:a20be740075d 68 uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
Rohit Grover 106:a20be740075d 69 uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
Rohit Grover 106:a20be740075d 70 uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
Rohit Grover 106:a20be740075d 71 } ConnectionParams_t;
Rohit Grover 106:a20be740075d 72
Rohit Grover 106:a20be740075d 73 public:
Rohit Grover 106:a20be740075d 74 /* These functions must be defined in the sub-class */
Rohit Grover 106:a20be740075d 75 virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0;
Rohit Grover 106:a20be740075d 76 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0;
Rohit Grover 106:a20be740075d 77 virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0;
Rohit Grover 106:a20be740075d 78 virtual ble_error_t stopAdvertising(void) = 0;
Rohit Grover 116:ca826083980e 79 virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0;
Rohit Grover 106:a20be740075d 80 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0;
Rohit Grover 106:a20be740075d 81 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
Rohit Grover 106:a20be740075d 82 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0;
Rohit Grover 106:a20be740075d 83
Rohit Grover 116:ca826083980e 84 virtual ble_error_t setDeviceName(const uint8_t *deviceName) = 0;
Rohit Grover 116:ca826083980e 85 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) = 0;
Rohit Grover 116:ca826083980e 86 virtual ble_error_t setAppearance(uint16_t appearance) = 0;
Rohit Grover 116:ca826083980e 87 virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0;
Rohit Grover 116:ca826083980e 88
Rohit Grover 106:a20be740075d 89 typedef void (*EventCallback_t)(void);
Rohit Grover 106:a20be740075d 90
Rohit Grover 106:a20be740075d 91 /* Event callback handlers */
Rohit Grover 106:a20be740075d 92 void setOnTimeout(EventCallback_t callback) {
Rohit Grover 106:a20be740075d 93 onTimeout = callback;
Rohit Grover 106:a20be740075d 94 }
jakerosenthal@gmail.com 123:d2cdf4ebe524 95 void setOnConnection(void (*callback)(Handle_t handle, const ConnectionParams_t *eventDataP)) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 96 onConnection.add(callback);
jakerosenthal@gmail.com 123:d2cdf4ebe524 97 }
jakerosenthal@gmail.com 123:d2cdf4ebe524 98 template <typename T>
jakerosenthal@gmail.com 123:d2cdf4ebe524 99 void setOnConnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, const ConnectionParams_t *context)) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 100 onConnection.add(objPtr, memberPtr);
Rohit Grover 106:a20be740075d 101 }
jakerosenthal@gmail.com 123:d2cdf4ebe524 102
jakerosenthal@gmail.com 123:d2cdf4ebe524 103 void setOnDisconnection(void (*callback)(Handle_t handle, DisconnectionReason_t reason)) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 104 onDisconnection.add(callback);
jakerosenthal@gmail.com 123:d2cdf4ebe524 105 }
jakerosenthal@gmail.com 123:d2cdf4ebe524 106 template <typename T>
jakerosenthal@gmail.com 123:d2cdf4ebe524 107 void setOnDisconnection(T *objPtr, void (T::*memberPtr)(Handle_t handle, DisconnectionReason_t reason)) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 108 onDisconnection.add(objPtr, memberPtr);
Rohit Grover 106:a20be740075d 109 }
Rohit Grover 106:a20be740075d 110
Rohit Grover 116:ca826083980e 111 void processConnectionEvent(Handle_t handle, const ConnectionParams_t *params) {
Rohit Grover 116:ca826083980e 112 state.connected = 1;
jakerosenthal@gmail.com 123:d2cdf4ebe524 113 if (onConnection.hasCallbacksAttached()) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 114 onConnection.call(handle, params);
Rohit Grover 116:ca826083980e 115 }
Rohit Grover 116:ca826083980e 116 }
Rohit Grover 116:ca826083980e 117
Rohit Grover 116:ca826083980e 118 void processDisconnectionEvent(Handle_t handle, DisconnectionReason_t reason) {
Rohit Grover 116:ca826083980e 119 state.connected = 0;
jakerosenthal@gmail.com 123:d2cdf4ebe524 120 if (onDisconnection.hasCallbacksAttached()) {
jakerosenthal@gmail.com 123:d2cdf4ebe524 121 onDisconnection.call(handle, reason);
Rohit Grover 106:a20be740075d 122 }
Rohit Grover 106:a20be740075d 123 }
Rohit Grover 106:a20be740075d 124
Rohit Grover 106:a20be740075d 125 void processEvent(GapEvents::gapEvent_e type) {
Rohit Grover 106:a20be740075d 126 switch (type) {
Rohit Grover 106:a20be740075d 127 case GapEvents::GAP_EVENT_TIMEOUT:
Rohit Grover 106:a20be740075d 128 state.advertising = 0;
Rohit Grover 106:a20be740075d 129 if (onTimeout) {
Rohit Grover 106:a20be740075d 130 onTimeout();
Rohit Grover 106:a20be740075d 131 }
Rohit Grover 106:a20be740075d 132 break;
Rohit Grover 106:a20be740075d 133 }
Rohit Grover 106:a20be740075d 134 }
Rohit Grover 106:a20be740075d 135
Rohit Grover 106:a20be740075d 136 GapState_t getState(void) const {
Rohit Grover 106:a20be740075d 137 return state;
Rohit Grover 106:a20be740075d 138 }
Rohit Grover 106:a20be740075d 139
Rohit Grover 106:a20be740075d 140 protected:
jakerosenthal@gmail.com 123:d2cdf4ebe524 141 Gap() : state(), onTimeout(NULL), onConnection(), onDisconnection() {
Rohit Grover 106:a20be740075d 142 /* empty */
Rohit Grover 106:a20be740075d 143 }
Rohit Grover 106:a20be740075d 144
Rohit Grover 106:a20be740075d 145 protected:
Rohit Grover 106:a20be740075d 146 GapState_t state;
Rohit Grover 106:a20be740075d 147
Rohit Grover 106:a20be740075d 148 private:
Rohit Grover 116:ca826083980e 149 EventCallback_t onTimeout;
jakerosenthal@gmail.com 123:d2cdf4ebe524 150 CallChainOfFunctionPointersWithContext<Handle_t, const ConnectionParams_t *> onConnection;
jakerosenthal@gmail.com 123:d2cdf4ebe524 151 CallChainOfFunctionPointersWithContext<Handle_t, DisconnectionReason_t> onDisconnection;
Rohit Grover 106:a20be740075d 152 };
Rohit Grover 106:a20be740075d 153
Rohit Grover 106:a20be740075d 154 #endif // ifndef __GAP_H__