Example for Bluetooth low energy interface
Dependencies: mbed HC_SR04_Ultrasonic_Library
BLE_API/public/Gap.h@3:829f081fde15, 2015-05-19 (annotated)
- Committer:
- julientiron
- Date:
- Tue May 19 20:42:25 2015 +0000
- Revision:
- 3:829f081fde15
- Parent:
- 0:3d641e170a74
hihi;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
julientiron | 0:3d641e170a74 | 1 | /* mbed Microcontroller Library |
julientiron | 0:3d641e170a74 | 2 | * Copyright (c) 2006-2013 ARM Limited |
julientiron | 0:3d641e170a74 | 3 | * |
julientiron | 0:3d641e170a74 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
julientiron | 0:3d641e170a74 | 5 | * you may not use this file except in compliance with the License. |
julientiron | 0:3d641e170a74 | 6 | * You may obtain a copy of the License at |
julientiron | 0:3d641e170a74 | 7 | * |
julientiron | 0:3d641e170a74 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
julientiron | 0:3d641e170a74 | 9 | * |
julientiron | 0:3d641e170a74 | 10 | * Unless required by applicable law or agreed to in writing, software |
julientiron | 0:3d641e170a74 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
julientiron | 0:3d641e170a74 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
julientiron | 0:3d641e170a74 | 13 | * See the License for the specific language governing permissions and |
julientiron | 0:3d641e170a74 | 14 | * limitations under the License. |
julientiron | 0:3d641e170a74 | 15 | */ |
julientiron | 0:3d641e170a74 | 16 | |
julientiron | 0:3d641e170a74 | 17 | #ifndef __GAP_H__ |
julientiron | 0:3d641e170a74 | 18 | #define __GAP_H__ |
julientiron | 0:3d641e170a74 | 19 | |
julientiron | 0:3d641e170a74 | 20 | #include "mbed.h" |
julientiron | 0:3d641e170a74 | 21 | #include "blecommon.h" |
julientiron | 0:3d641e170a74 | 22 | #include "GapAdvertisingData.h" |
julientiron | 0:3d641e170a74 | 23 | #include "GapAdvertisingParams.h" |
julientiron | 0:3d641e170a74 | 24 | #include "GapEvents.h" |
julientiron | 0:3d641e170a74 | 25 | |
julientiron | 0:3d641e170a74 | 26 | /**************************************************************************/ |
julientiron | 0:3d641e170a74 | 27 | /*! |
julientiron | 0:3d641e170a74 | 28 | \brief |
julientiron | 0:3d641e170a74 | 29 | The base class used to abstract GAP functionality to a specific radio |
julientiron | 0:3d641e170a74 | 30 | transceiver, SOC or BLE Stack. |
julientiron | 0:3d641e170a74 | 31 | */ |
julientiron | 0:3d641e170a74 | 32 | /**************************************************************************/ |
julientiron | 0:3d641e170a74 | 33 | class Gap |
julientiron | 0:3d641e170a74 | 34 | { |
julientiron | 0:3d641e170a74 | 35 | public: |
julientiron | 0:3d641e170a74 | 36 | typedef enum addr_type_e { |
julientiron | 0:3d641e170a74 | 37 | ADDR_TYPE_PUBLIC = 0, |
julientiron | 0:3d641e170a74 | 38 | ADDR_TYPE_RANDOM_STATIC, |
julientiron | 0:3d641e170a74 | 39 | ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, |
julientiron | 0:3d641e170a74 | 40 | ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE |
julientiron | 0:3d641e170a74 | 41 | } addr_type_t; |
julientiron | 0:3d641e170a74 | 42 | |
julientiron | 0:3d641e170a74 | 43 | /* Describes the current state of the device (more than one bit can be set) */ |
julientiron | 0:3d641e170a74 | 44 | typedef struct GapState_s { |
julientiron | 0:3d641e170a74 | 45 | unsigned advertising : 1; /**< peripheral is currently advertising */ |
julientiron | 0:3d641e170a74 | 46 | unsigned connected : 1; /**< peripheral is connected to a central */ |
julientiron | 0:3d641e170a74 | 47 | } GapState_t; |
julientiron | 0:3d641e170a74 | 48 | |
julientiron | 0:3d641e170a74 | 49 | typedef uint16_t Handle_t; |
julientiron | 0:3d641e170a74 | 50 | |
julientiron | 0:3d641e170a74 | 51 | typedef struct { |
julientiron | 0:3d641e170a74 | 52 | uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
julientiron | 0:3d641e170a74 | 53 | uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
julientiron | 0:3d641e170a74 | 54 | uint16_t slaveLatency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ |
julientiron | 0:3d641e170a74 | 55 | uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ |
julientiron | 0:3d641e170a74 | 56 | } ConnectionParams_t; |
julientiron | 0:3d641e170a74 | 57 | |
julientiron | 0:3d641e170a74 | 58 | public: |
julientiron | 0:3d641e170a74 | 59 | /* These functions must be defined in the sub-class */ |
julientiron | 0:3d641e170a74 | 60 | virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0; |
julientiron | 0:3d641e170a74 | 61 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; |
julientiron | 0:3d641e170a74 | 62 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; |
julientiron | 0:3d641e170a74 | 63 | virtual ble_error_t stopAdvertising(void) = 0; |
julientiron | 0:3d641e170a74 | 64 | virtual ble_error_t disconnect(void) = 0; |
julientiron | 0:3d641e170a74 | 65 | virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; |
julientiron | 0:3d641e170a74 | 66 | virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0; |
julientiron | 0:3d641e170a74 | 67 | virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) = 0; |
julientiron | 0:3d641e170a74 | 68 | |
julientiron | 0:3d641e170a74 | 69 | typedef void (*EventCallback_t)(void); |
julientiron | 0:3d641e170a74 | 70 | typedef void (*HandleSpecificEventCallback_t)(Handle_t); |
julientiron | 0:3d641e170a74 | 71 | |
julientiron | 0:3d641e170a74 | 72 | /* Event callback handlers */ |
julientiron | 0:3d641e170a74 | 73 | void setOnTimeout(EventCallback_t callback) { |
julientiron | 0:3d641e170a74 | 74 | onTimeout = callback; |
julientiron | 0:3d641e170a74 | 75 | } |
julientiron | 0:3d641e170a74 | 76 | void setOnConnection(HandleSpecificEventCallback_t callback) { |
julientiron | 0:3d641e170a74 | 77 | onConnection = callback; |
julientiron | 0:3d641e170a74 | 78 | } |
julientiron | 0:3d641e170a74 | 79 | void setOnDisconnection(HandleSpecificEventCallback_t callback) { |
julientiron | 0:3d641e170a74 | 80 | onDisconnection = callback; |
julientiron | 0:3d641e170a74 | 81 | } |
julientiron | 0:3d641e170a74 | 82 | |
julientiron | 0:3d641e170a74 | 83 | void processHandleSpecificEvent(GapEvents::gapEvent_e type, Handle_t handle) { |
julientiron | 0:3d641e170a74 | 84 | switch (type) { |
julientiron | 0:3d641e170a74 | 85 | case GapEvents::GAP_EVENT_CONNECTED: |
julientiron | 0:3d641e170a74 | 86 | state.connected = 1; |
julientiron | 0:3d641e170a74 | 87 | if (onConnection) { |
julientiron | 0:3d641e170a74 | 88 | onConnection(handle); |
julientiron | 0:3d641e170a74 | 89 | } |
julientiron | 0:3d641e170a74 | 90 | break; |
julientiron | 0:3d641e170a74 | 91 | case GapEvents::GAP_EVENT_DISCONNECTED: |
julientiron | 0:3d641e170a74 | 92 | state.connected = 0; |
julientiron | 0:3d641e170a74 | 93 | if (onDisconnection) { |
julientiron | 0:3d641e170a74 | 94 | onDisconnection(handle); |
julientiron | 0:3d641e170a74 | 95 | } |
julientiron | 0:3d641e170a74 | 96 | break; |
julientiron | 0:3d641e170a74 | 97 | } |
julientiron | 0:3d641e170a74 | 98 | } |
julientiron | 0:3d641e170a74 | 99 | |
julientiron | 0:3d641e170a74 | 100 | void processEvent(GapEvents::gapEvent_e type) { |
julientiron | 0:3d641e170a74 | 101 | switch (type) { |
julientiron | 0:3d641e170a74 | 102 | case GapEvents::GAP_EVENT_TIMEOUT: |
julientiron | 0:3d641e170a74 | 103 | state.advertising = 0; |
julientiron | 0:3d641e170a74 | 104 | if (onTimeout) { |
julientiron | 0:3d641e170a74 | 105 | onTimeout(); |
julientiron | 0:3d641e170a74 | 106 | } |
julientiron | 0:3d641e170a74 | 107 | break; |
julientiron | 0:3d641e170a74 | 108 | } |
julientiron | 0:3d641e170a74 | 109 | } |
julientiron | 0:3d641e170a74 | 110 | |
julientiron | 0:3d641e170a74 | 111 | GapState_t getState(void) const { |
julientiron | 0:3d641e170a74 | 112 | return state; |
julientiron | 0:3d641e170a74 | 113 | } |
julientiron | 0:3d641e170a74 | 114 | |
julientiron | 0:3d641e170a74 | 115 | protected: |
julientiron | 0:3d641e170a74 | 116 | Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) { |
julientiron | 0:3d641e170a74 | 117 | /* empty */ |
julientiron | 0:3d641e170a74 | 118 | } |
julientiron | 0:3d641e170a74 | 119 | |
julientiron | 0:3d641e170a74 | 120 | protected: |
julientiron | 0:3d641e170a74 | 121 | GapState_t state; |
julientiron | 0:3d641e170a74 | 122 | |
julientiron | 0:3d641e170a74 | 123 | private: |
julientiron | 0:3d641e170a74 | 124 | EventCallback_t onTimeout; |
julientiron | 0:3d641e170a74 | 125 | HandleSpecificEventCallback_t onConnection; |
julientiron | 0:3d641e170a74 | 126 | HandleSpecificEventCallback_t onDisconnection; |
julientiron | 0:3d641e170a74 | 127 | }; |
julientiron | 0:3d641e170a74 | 128 | |
julientiron | 0:3d641e170a74 | 129 | #endif // ifndef __GAP_H__ |