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