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
hw/Gap.h@104:6ef5abef5714, 2014-07-10 (annotated)
- Committer:
- Rohit Grover
- Date:
- Thu Jul 10 12:12:06 2014 +0100
- Revision:
- 104:6ef5abef5714
- Parent:
- 77:1436ecf09583
- Child:
- 105:f29ab9f74d7e
connection and disconnection callbacks need to take a connection handle
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ktownsend | 29:011e95ce78b8 | 1 | /* mbed Microcontroller Library |
| ktownsend | 29:011e95ce78b8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| ktownsend | 29:011e95ce78b8 | 3 | * |
| ktownsend | 29:011e95ce78b8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| ktownsend | 29:011e95ce78b8 | 5 | * you may not use this file except in compliance with the License. |
| ktownsend | 29:011e95ce78b8 | 6 | * You may obtain a copy of the License at |
| ktownsend | 29:011e95ce78b8 | 7 | * |
| ktownsend | 29:011e95ce78b8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| ktownsend | 29:011e95ce78b8 | 9 | * |
| ktownsend | 29:011e95ce78b8 | 10 | * Unless required by applicable law or agreed to in writing, software |
| ktownsend | 29:011e95ce78b8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| ktownsend | 29:011e95ce78b8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| ktownsend | 29:011e95ce78b8 | 13 | * See the License for the specific language governing permissions and |
| ktownsend | 29:011e95ce78b8 | 14 | * limitations under the License. |
| ktownsend | 29:011e95ce78b8 | 15 | */ |
| Rohit Grover |
34:da2ea8cd6216 | 16 | |
| ktownsend | 29:011e95ce78b8 | 17 | #ifndef __GAP_H__ |
| ktownsend | 29:011e95ce78b8 | 18 | #define __GAP_H__ |
| ktownsend | 29:011e95ce78b8 | 19 | |
| ktownsend | 29:011e95ce78b8 | 20 | #include "mbed.h" |
| ktownsend | 29:011e95ce78b8 | 21 | #include "blecommon.h" |
| ktownsend | 29:011e95ce78b8 | 22 | #include "GapAdvertisingData.h" |
| ktownsend | 29:011e95ce78b8 | 23 | #include "GapAdvertisingParams.h" |
| bogdanm | 31:2c94f0501807 | 24 | #include "GapEvents.h" |
| ktownsend | 29:011e95ce78b8 | 25 | |
| ktownsend | 29:011e95ce78b8 | 26 | /**************************************************************************/ |
| ktownsend | 29:011e95ce78b8 | 27 | /*! |
| ktownsend | 29:011e95ce78b8 | 28 | \brief |
| ktownsend | 29:011e95ce78b8 | 29 | The base class used to abstract GAP functionality to a specific radio |
| ktownsend | 29:011e95ce78b8 | 30 | transceiver, SOC or BLE Stack. |
| ktownsend | 29:011e95ce78b8 | 31 | */ |
| ktownsend | 29:011e95ce78b8 | 32 | /**************************************************************************/ |
| ktownsend | 29:011e95ce78b8 | 33 | class Gap |
| ktownsend | 29:011e95ce78b8 | 34 | { |
| Rohit Grover |
34:da2ea8cd6216 | 35 | public: |
| Rohit Grover |
34:da2ea8cd6216 | 36 | typedef enum addr_type_e { |
| Rohit Grover |
34:da2ea8cd6216 | 37 | ADDR_TYPE_PUBLIC = 0, |
| Rohit Grover |
34:da2ea8cd6216 | 38 | ADDR_TYPE_RANDOM_STATIC, |
| Rohit Grover |
34:da2ea8cd6216 | 39 | ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE, |
| Rohit Grover |
34:da2ea8cd6216 | 40 | ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE |
| Rohit Grover |
34:da2ea8cd6216 | 41 | } addr_type_t; |
| Rohit Grover |
34:da2ea8cd6216 | 42 | |
| Rohit Grover |
34:da2ea8cd6216 | 43 | /* These functions must be defined in the sub-class */ |
| Rohit Grover |
38:02cf26a2a4db | 44 | virtual ble_error_t setAddress(addr_type_t type, const uint8_t address[6]) = 0; |
| Rohit Grover |
75:d08bdef22500 | 45 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; |
| Rohit Grover |
40:d405c9b1419d | 46 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; |
| Rohit Grover |
34:da2ea8cd6216 | 47 | virtual ble_error_t stopAdvertising(void) = 0; |
| Rohit Grover |
34:da2ea8cd6216 | 48 | virtual ble_error_t disconnect(void) = 0; |
| bogdanm | 31:2c94f0501807 | 49 | |
| Rohit Grover |
34:da2ea8cd6216 | 50 | /* Describes the current state of the device (more than one bit can be |
| Rohit Grover |
34:da2ea8cd6216 | 51 | *set) */ |
| Rohit Grover |
63:653378e782ea | 52 | typedef struct GapState_s { |
| Rohit Grover |
63:653378e782ea | 53 | unsigned advertising : 1; /**< peripheral is currently advertising */ |
| Rohit Grover |
63:653378e782ea | 54 | unsigned connected : 1; /**< peripheral is connected to a central */ |
| Rohit Grover |
34:da2ea8cd6216 | 55 | } GapState_t; |
| Rohit Grover |
34:da2ea8cd6216 | 56 | |
| Rohit Grover |
104:6ef5abef5714 | 57 | typedef void (*EventCallback_t)(void); |
| Rohit Grover |
104:6ef5abef5714 | 58 | typedef uint16_t Handle_t; |
| Rohit Grover |
104:6ef5abef5714 | 59 | typedef void (*HandleSpecificEventCallback_t)(Handle_t); |
| Rohit Grover |
104:6ef5abef5714 | 60 | |
| Rohit Grover |
34:da2ea8cd6216 | 61 | /* Event callback handlers */ |
| Rohit Grover |
63:653378e782ea | 62 | void setOnTimeout(EventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 63 | onTimeout = callback; |
| Rohit Grover |
63:653378e782ea | 64 | } |
| Rohit Grover |
104:6ef5abef5714 | 65 | void setOnConnection(HandleSpecificEventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 66 | onConnection = callback; |
| Rohit Grover |
63:653378e782ea | 67 | } |
| Rohit Grover |
104:6ef5abef5714 | 68 | void setOnDisconnection(HandleSpecificEventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 69 | onDisconnection = callback; |
| Rohit Grover |
34:da2ea8cd6216 | 70 | } |
| ktownsend | 29:011e95ce78b8 | 71 | |
| Rohit Grover |
104:6ef5abef5714 | 72 | void processHandleSpecificEvent(GapEvents::gapEvent_e type, Handle_t handle) { |
| Rohit Grover |
104:6ef5abef5714 | 73 | switch (type) { |
| Rohit Grover |
104:6ef5abef5714 | 74 | case GapEvents::GAP_EVENT_CONNECTED: |
| Rohit Grover |
104:6ef5abef5714 | 75 | state.connected = 1; |
| Rohit Grover |
104:6ef5abef5714 | 76 | if (onConnection) { |
| Rohit Grover |
104:6ef5abef5714 | 77 | onConnection(handle); |
| Rohit Grover |
104:6ef5abef5714 | 78 | } |
| Rohit Grover |
104:6ef5abef5714 | 79 | break; |
| Rohit Grover |
104:6ef5abef5714 | 80 | case GapEvents::GAP_EVENT_DISCONNECTED: |
| Rohit Grover |
104:6ef5abef5714 | 81 | state.connected = 0; |
| Rohit Grover |
104:6ef5abef5714 | 82 | if (onDisconnection) { |
| Rohit Grover |
104:6ef5abef5714 | 83 | onDisconnection(handle); |
| Rohit Grover |
104:6ef5abef5714 | 84 | } |
| Rohit Grover |
104:6ef5abef5714 | 85 | break; |
| Rohit Grover |
104:6ef5abef5714 | 86 | } |
| Rohit Grover |
104:6ef5abef5714 | 87 | } |
| Rohit Grover |
104:6ef5abef5714 | 88 | |
| Rohit Grover |
104:6ef5abef5714 | 89 | void processEvent(GapEvents::gapEvent_e type) { |
| Rohit Grover |
34:da2ea8cd6216 | 90 | switch (type) { |
| Rohit Grover |
77:1436ecf09583 | 91 | case GapEvents::GAP_EVENT_TIMEOUT: |
| Rohit Grover |
77:1436ecf09583 | 92 | state.advertising = 0; |
| Rohit Grover |
77:1436ecf09583 | 93 | if (onTimeout) { |
| Rohit Grover |
77:1436ecf09583 | 94 | onTimeout(); |
| Rohit Grover |
77:1436ecf09583 | 95 | } |
| Rohit Grover |
77:1436ecf09583 | 96 | break; |
| Rohit Grover |
34:da2ea8cd6216 | 97 | } |
| Rohit Grover |
34:da2ea8cd6216 | 98 | } |
| Rohit Grover |
34:da2ea8cd6216 | 99 | |
| Rohit Grover |
73:eeb1ac3545e9 | 100 | GapState_t getState(void) const { |
| Rohit Grover |
73:eeb1ac3545e9 | 101 | return state; |
| Rohit Grover |
73:eeb1ac3545e9 | 102 | } |
| Rohit Grover |
73:eeb1ac3545e9 | 103 | |
| Rohit Grover |
63:653378e782ea | 104 | protected: |
| Rohit Grover |
75:d08bdef22500 | 105 | Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) { |
| Rohit Grover |
63:653378e782ea | 106 | /* empty */ |
| Rohit Grover |
63:653378e782ea | 107 | } |
| Rohit Grover |
63:653378e782ea | 108 | |
| Rohit Grover |
63:653378e782ea | 109 | protected: |
| Rohit Grover |
34:da2ea8cd6216 | 110 | GapState_t state; |
| Rohit Grover |
63:653378e782ea | 111 | |
| Rohit Grover |
63:653378e782ea | 112 | private: |
| Rohit Grover |
104:6ef5abef5714 | 113 | EventCallback_t onTimeout; |
| Rohit Grover |
104:6ef5abef5714 | 114 | HandleSpecificEventCallback_t onConnection; |
| Rohit Grover |
104:6ef5abef5714 | 115 | HandleSpecificEventCallback_t onDisconnection; |
| ktownsend | 29:011e95ce78b8 | 116 | }; |
| ktownsend | 29:011e95ce78b8 | 117 | |
| Rohit Grover |
34:da2ea8cd6216 | 118 | #endif // ifndef __GAP_H__ |
