extend
Fork of BLE_API by
source/BLE.cpp@811:059ed1c7b128, 2015-09-29 (annotated)
- Committer:
- rgrover1
- Date:
- Tue Sep 29 09:49:21 2015 +0100
- Revision:
- 811:059ed1c7b128
- Parent:
- 810:c91bc1f5e035
- Child:
- 818:61c62a287194
Synchronized with git rev 603dae31
Author: Rohit Grover
Release 0.4.6
=============
Enhancements
~~~~~~~~~~~~
* Add connection handle to GATT callback parameters. This paves the way for
applications requiring multiple concurrent connections: read/write/HVX
callbacks will be able to distinguish between peripherals by comparing per-
device connection handles.
* Revert to an older, working version of eddystone. This is temporary, and
will only help with demos. We will provide a mature Eddystone offering
shortly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 712:b04b5db36865 | 1 | /* mbed Microcontroller Library |
rgrover1 | 712:b04b5db36865 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 712:b04b5db36865 | 3 | * |
rgrover1 | 712:b04b5db36865 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 712:b04b5db36865 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 712:b04b5db36865 | 6 | * You may obtain a copy of the License at |
rgrover1 | 712:b04b5db36865 | 7 | * |
rgrover1 | 712:b04b5db36865 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 712:b04b5db36865 | 9 | * |
rgrover1 | 712:b04b5db36865 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 712:b04b5db36865 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 712:b04b5db36865 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 712:b04b5db36865 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 712:b04b5db36865 | 14 | * limitations under the License. |
rgrover1 | 712:b04b5db36865 | 15 | */ |
rgrover1 | 712:b04b5db36865 | 16 | |
rgrover1 | 712:b04b5db36865 | 17 | #include "ble/BLE.h" |
rgrover1 | 712:b04b5db36865 | 18 | |
rgrover1 | 712:b04b5db36865 | 19 | #if defined(TARGET_OTA_ENABLED) |
rgrover1 | 732:916f36dd93f8 | 20 | #include "ble/services/DFUService.h" |
rgrover1 | 712:b04b5db36865 | 21 | #endif |
rgrover1 | 712:b04b5db36865 | 22 | |
rgrover1 | 712:b04b5db36865 | 23 | ble_error_t |
rgrover1 | 712:b04b5db36865 | 24 | BLE::init() |
rgrover1 | 712:b04b5db36865 | 25 | { |
rgrover1 | 712:b04b5db36865 | 26 | ble_error_t err = transport->init(); |
rgrover1 | 712:b04b5db36865 | 27 | if (err != BLE_ERROR_NONE) { |
rgrover1 | 712:b04b5db36865 | 28 | return err; |
rgrover1 | 712:b04b5db36865 | 29 | } |
rgrover1 | 712:b04b5db36865 | 30 | |
rgrover1 | 712:b04b5db36865 | 31 | /* Platforms enabled for DFU should introduce the DFU Service into |
rgrover1 | 712:b04b5db36865 | 32 | * applications automatically. */ |
rgrover1 | 712:b04b5db36865 | 33 | #if defined(TARGET_OTA_ENABLED) |
rgrover1 | 712:b04b5db36865 | 34 | static DFUService dfu(*this); // defined static so that the object remains alive |
rgrover1 | 712:b04b5db36865 | 35 | #endif // TARGET_OTA_ENABLED |
rgrover1 | 712:b04b5db36865 | 36 | |
rgrover1 | 712:b04b5db36865 | 37 | return BLE_ERROR_NONE; |
rgrover1 | 811:059ed1c7b128 | 38 | } |
rgrover1 | 811:059ed1c7b128 | 39 | |
rgrover1 | 811:059ed1c7b128 | 40 | /** |
rgrover1 | 811:059ed1c7b128 | 41 | * BLE::Instance() and BLE constructor rely upon a static array of initializers |
rgrover1 | 811:059ed1c7b128 | 42 | * to create actual BLE transport instances. A description of these instances |
rgrover1 | 811:059ed1c7b128 | 43 | * and initializers is supposed to be put in some .json file contributing to |
rgrover1 | 811:059ed1c7b128 | 44 | * yotta's configuration (typically the target.json). Here's a sample: |
rgrover1 | 811:059ed1c7b128 | 45 | * |
rgrover1 | 811:059ed1c7b128 | 46 | * "config": { |
rgrover1 | 811:059ed1c7b128 | 47 | * ... |
rgrover1 | 811:059ed1c7b128 | 48 | * "ble_instances": { |
rgrover1 | 811:059ed1c7b128 | 49 | * "count": 1, |
rgrover1 | 811:059ed1c7b128 | 50 | * "0" : { |
rgrover1 | 811:059ed1c7b128 | 51 | * "initializer" : "createBLEInstance" |
rgrover1 | 811:059ed1c7b128 | 52 | * } |
rgrover1 | 811:059ed1c7b128 | 53 | * } |
rgrover1 | 811:059ed1c7b128 | 54 | * } |
rgrover1 | 811:059ed1c7b128 | 55 | * |
rgrover1 | 811:059ed1c7b128 | 56 | * The following macros result in translating the above config into a static |
rgrover1 | 811:059ed1c7b128 | 57 | * array: instanceConstructors. |
rgrover1 | 811:059ed1c7b128 | 58 | */ |
rgrover1 | 811:059ed1c7b128 | 59 | #ifdef YOTTA_CFG_BLE_INSTANCES_COUNT |
rgrover1 | 811:059ed1c7b128 | 60 | #define CONCATENATE(A, B) A ## B |
rgrover1 | 811:059ed1c7b128 | 61 | #define EXPAND(X) X /* this adds a level of indirection needed to allow macro-expansion following a token-paste operation (see use of CONCATENATE() below). */ |
rgrover1 | 811:059ed1c7b128 | 62 | |
rgrover1 | 811:059ed1c7b128 | 63 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1 YOTTA_CFG_BLE_INSTANCES_0_INITIALIZER |
rgrover1 | 811:059ed1c7b128 | 64 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1, YOTTA_CFG_BLE_INSTANCES_1_INITIALIZER |
rgrover1 | 811:059ed1c7b128 | 65 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2, YOTTA_CFG_BLE_INSTANCES_2_INITIALIZER |
rgrover1 | 811:059ed1c7b128 | 66 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3, YOTTA_CFG_BLE_INSTANCES_3_INITIALIZER |
rgrover1 | 811:059ed1c7b128 | 67 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_5 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4, YOTTA_CFG_BLE_INSTANCES_4_INITIALIZER |
rgrover1 | 811:059ed1c7b128 | 68 | /* ... add more of the above if ever needed */ |
rgrover1 | 811:059ed1c7b128 | 69 | |
rgrover1 | 811:059ed1c7b128 | 70 | #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(N) EXPAND(CONCATENATE(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_, N)) |
rgrover1 | 811:059ed1c7b128 | 71 | #endif /* YOTTA_CFG_BLE_INSTANCES_COUNT */ |
rgrover1 | 811:059ed1c7b128 | 72 | |
rgrover1 | 811:059ed1c7b128 | 73 | typedef BLEInstanceBase *(*InstanceConstructor_t)(void); |
rgrover1 | 811:059ed1c7b128 | 74 | static const InstanceConstructor_t instanceConstructors[BLE::NUM_INSTANCES] = { |
rgrover1 | 811:059ed1c7b128 | 75 | #ifndef YOTTA_CFG_BLE_INSTANCES_COUNT |
rgrover1 | 811:059ed1c7b128 | 76 | createBLEInstance |
rgrover1 | 811:059ed1c7b128 | 77 | #else |
rgrover1 | 811:059ed1c7b128 | 78 | INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(YOTTA_CFG_BLE_INSTANCES_COUNT) |
rgrover1 | 811:059ed1c7b128 | 79 | #endif |
rgrover1 | 811:059ed1c7b128 | 80 | }; |
rgrover1 | 811:059ed1c7b128 | 81 | |
rgrover1 | 811:059ed1c7b128 | 82 | BLE & |
rgrover1 | 811:059ed1c7b128 | 83 | BLE::Instance(InstanceID_t id) |
rgrover1 | 811:059ed1c7b128 | 84 | { |
rgrover1 | 811:059ed1c7b128 | 85 | static BLE *singletons[NUM_INSTANCES]; |
rgrover1 | 811:059ed1c7b128 | 86 | if (id < NUM_INSTANCES) { |
rgrover1 | 811:059ed1c7b128 | 87 | if (singletons[id] == NULL) { |
rgrover1 | 811:059ed1c7b128 | 88 | singletons[id] = new BLE(id); /* This object will never be freed. */ |
rgrover1 | 811:059ed1c7b128 | 89 | } |
rgrover1 | 811:059ed1c7b128 | 90 | |
rgrover1 | 811:059ed1c7b128 | 91 | return *singletons[id]; |
rgrover1 | 811:059ed1c7b128 | 92 | } |
rgrover1 | 811:059ed1c7b128 | 93 | |
rgrover1 | 811:059ed1c7b128 | 94 | /* we come here only in the case of a bad interfaceID. */ |
rgrover1 | 811:059ed1c7b128 | 95 | static BLE badSingleton(NUM_INSTANCES /* this is a bad index; and will result in a NULL transport. */); |
rgrover1 | 811:059ed1c7b128 | 96 | return badSingleton; |
rgrover1 | 811:059ed1c7b128 | 97 | } |
rgrover1 | 811:059ed1c7b128 | 98 | |
rgrover1 | 811:059ed1c7b128 | 99 | BLE::BLE(InstanceID_t instanceID) : transport() |
rgrover1 | 811:059ed1c7b128 | 100 | { |
rgrover1 | 811:059ed1c7b128 | 101 | static BLEInstanceBase *transportInstances[NUM_INSTANCES]; |
rgrover1 | 811:059ed1c7b128 | 102 | |
rgrover1 | 811:059ed1c7b128 | 103 | if (instanceID < NUM_INSTANCES) { |
rgrover1 | 811:059ed1c7b128 | 104 | if (!transportInstances[instanceID]) { |
rgrover1 | 811:059ed1c7b128 | 105 | transportInstances[instanceID] = instanceConstructors[instanceID](); /* Call the stack's initializer for the transport object. */ |
rgrover1 | 811:059ed1c7b128 | 106 | } |
rgrover1 | 811:059ed1c7b128 | 107 | transport = transportInstances[instanceID]; |
rgrover1 | 811:059ed1c7b128 | 108 | } else { |
rgrover1 | 811:059ed1c7b128 | 109 | transport = NULL; |
rgrover1 | 811:059ed1c7b128 | 110 | } |
rgrover1 | 712:b04b5db36865 | 111 | } |