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@63:653378e782ea, 2014-06-04 (annotated)
- Committer:
- Rohit Grover
- Date:
- Wed Jun 04 09:36:31 2014 +0100
- Revision:
- 63:653378e782ea
- Parent:
- 40:d405c9b1419d
- Child:
- 73:eeb1ac3545e9
add Gap Callbacks
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 |
40:d405c9b1419d | 45 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, |
| Rohit Grover |
40:d405c9b1419d | 46 | const GapAdvertisingData &) = 0; |
| Rohit Grover |
40:d405c9b1419d | 47 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; |
| Rohit Grover |
34:da2ea8cd6216 | 48 | virtual ble_error_t stopAdvertising(void) = 0; |
| Rohit Grover |
34:da2ea8cd6216 | 49 | virtual ble_error_t disconnect(void) = 0; |
| bogdanm | 31:2c94f0501807 | 50 | |
| Rohit Grover |
34:da2ea8cd6216 | 51 | /* Describes the current state of the device (more than one bit can be |
| Rohit Grover |
34:da2ea8cd6216 | 52 | *set) */ |
| Rohit Grover |
63:653378e782ea | 53 | typedef struct GapState_s { |
| Rohit Grover |
63:653378e782ea | 54 | unsigned advertising : 1; /**< peripheral is currently advertising */ |
| Rohit Grover |
63:653378e782ea | 55 | unsigned connected : 1; /**< peripheral is connected to a central */ |
| Rohit Grover |
34:da2ea8cd6216 | 56 | } GapState_t; |
| Rohit Grover |
34:da2ea8cd6216 | 57 | |
| Rohit Grover |
34:da2ea8cd6216 | 58 | /* Event callback handlers */ |
| Rohit Grover |
63:653378e782ea | 59 | typedef void (*EventCallback_t)(void); |
| Rohit Grover |
63:653378e782ea | 60 | void setOnTimeout(EventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 61 | onTimeout = callback; |
| Rohit Grover |
63:653378e782ea | 62 | } |
| Rohit Grover |
63:653378e782ea | 63 | void setOnConnection(EventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 64 | onConnection = callback; |
| Rohit Grover |
63:653378e782ea | 65 | } |
| Rohit Grover |
63:653378e782ea | 66 | void setOnDisconnection(EventCallback_t callback) { |
| Rohit Grover |
63:653378e782ea | 67 | onDisconnection = callback; |
| Rohit Grover |
34:da2ea8cd6216 | 68 | } |
| ktownsend | 29:011e95ce78b8 | 69 | |
| Rohit Grover |
34:da2ea8cd6216 | 70 | void handleEvent(GapEvents::gapEvent_e type) { |
| Rohit Grover |
34:da2ea8cd6216 | 71 | switch (type) { |
| Rohit Grover |
34:da2ea8cd6216 | 72 | case GapEvents::GAP_EVENT_TIMEOUT: |
| Rohit Grover |
34:da2ea8cd6216 | 73 | state.advertising = 0; |
| Rohit Grover |
63:653378e782ea | 74 | if (onTimeout) { |
| Rohit Grover |
63:653378e782ea | 75 | onTimeout(); |
| Rohit Grover |
63:653378e782ea | 76 | } |
| Rohit Grover |
34:da2ea8cd6216 | 77 | break; |
| Rohit Grover |
34:da2ea8cd6216 | 78 | case GapEvents::GAP_EVENT_CONNECTED: |
| Rohit Grover |
34:da2ea8cd6216 | 79 | state.connected = 1; |
| Rohit Grover |
63:653378e782ea | 80 | if (onConnection) { |
| Rohit Grover |
63:653378e782ea | 81 | onConnection(); |
| Rohit Grover |
63:653378e782ea | 82 | } |
| Rohit Grover |
34:da2ea8cd6216 | 83 | break; |
| Rohit Grover |
34:da2ea8cd6216 | 84 | case GapEvents::GAP_EVENT_DISCONNECTED: |
| Rohit Grover |
34:da2ea8cd6216 | 85 | state.connected = 0; |
| Rohit Grover |
63:653378e782ea | 86 | if (onDisconnection) { |
| Rohit Grover |
63:653378e782ea | 87 | onDisconnection(); |
| Rohit Grover |
63:653378e782ea | 88 | } |
| Rohit Grover |
34:da2ea8cd6216 | 89 | break; |
| Rohit Grover |
34:da2ea8cd6216 | 90 | } |
| Rohit Grover |
34:da2ea8cd6216 | 91 | } |
| Rohit Grover |
34:da2ea8cd6216 | 92 | |
| Rohit Grover |
63:653378e782ea | 93 | protected: |
| Rohit Grover |
63:653378e782ea | 94 | Gap() : |
| Rohit Grover |
63:653378e782ea | 95 | state(), |
| Rohit Grover |
63:653378e782ea | 96 | onTimeout(NULL), |
| Rohit Grover |
63:653378e782ea | 97 | onConnection(NULL), |
| Rohit Grover |
63:653378e782ea | 98 | onDisconnection(NULL) { |
| Rohit Grover |
63:653378e782ea | 99 | /* empty */ |
| Rohit Grover |
63:653378e782ea | 100 | } |
| Rohit Grover |
63:653378e782ea | 101 | |
| Rohit Grover |
63:653378e782ea | 102 | protected: |
| Rohit Grover |
34:da2ea8cd6216 | 103 | GapState_t state; |
| Rohit Grover |
63:653378e782ea | 104 | |
| Rohit Grover |
63:653378e782ea | 105 | private: |
| Rohit Grover |
63:653378e782ea | 106 | EventCallback_t onTimeout; |
| Rohit Grover |
63:653378e782ea | 107 | EventCallback_t onConnection; |
| Rohit Grover |
63:653378e782ea | 108 | EventCallback_t onDisconnection; |
| ktownsend | 29:011e95ce78b8 | 109 | }; |
| ktownsend | 29:011e95ce78b8 | 110 | |
| Rohit Grover |
34:da2ea8cd6216 | 111 | #endif // ifndef __GAP_H__ |
