High level Bluetooth Low Energy API and radio abstraction layer

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Nov 26 12:52:00 2015 +0000
Revision:
890:f3144091ae19
Parent:
886:0a45c67b2301
Child:
906:8ddab3271c2f
Synchronized with git rev 216f73bc
Author: Irit Arkin
New version

Based on the main repo's master version, with edits to the API.

Who changed what in which revision?

UserRevisionLine numberNew 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 890:f3144091ae19 24 BLE::init()
rgrover1 712:b04b5db36865 25 {
rgrover1 890:f3144091ae19 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 822:a0f080d1e836 38 }
rgrover1 822:a0f080d1e836 39
rgrover1 822:a0f080d1e836 40 /**
rgrover1 822:a0f080d1e836 41 * BLE::Instance() and BLE constructor rely upon a static array of initializers
rgrover1 822:a0f080d1e836 42 * to create actual BLE transport instances. A description of these instances
rgrover1 822:a0f080d1e836 43 * and initializers is supposed to be put in some .json file contributing to
rgrover1 828:d22ab1419e92 44 * yotta's configuration (typically in the target definition described by
rgrover1 828:d22ab1419e92 45 * target.json). Here's a sample:
rgrover1 822:a0f080d1e836 46 *
rgrover1 822:a0f080d1e836 47 * "config": {
rgrover1 822:a0f080d1e836 48 * ...
rgrover1 822:a0f080d1e836 49 * "ble_instances": {
rgrover1 822:a0f080d1e836 50 * "count": 1,
rgrover1 822:a0f080d1e836 51 * "0" : {
rgrover1 822:a0f080d1e836 52 * "initializer" : "createBLEInstance"
rgrover1 822:a0f080d1e836 53 * }
rgrover1 822:a0f080d1e836 54 * }
rgrover1 828:d22ab1419e92 55 * ...
rgrover1 822:a0f080d1e836 56 * }
rgrover1 822:a0f080d1e836 57 *
rgrover1 822:a0f080d1e836 58 * The following macros result in translating the above config into a static
rgrover1 822:a0f080d1e836 59 * array: instanceConstructors.
rgrover1 822:a0f080d1e836 60 */
rgrover1 822:a0f080d1e836 61 #ifdef YOTTA_CFG_BLE_INSTANCES_COUNT
rgrover1 822:a0f080d1e836 62 #define CONCATENATE(A, B) A ## B
rgrover1 822:a0f080d1e836 63 #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 822:a0f080d1e836 64
rgrover1 822:a0f080d1e836 65 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1 YOTTA_CFG_BLE_INSTANCES_0_INITIALIZER
rgrover1 822:a0f080d1e836 66 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1, YOTTA_CFG_BLE_INSTANCES_1_INITIALIZER
rgrover1 822:a0f080d1e836 67 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2, YOTTA_CFG_BLE_INSTANCES_2_INITIALIZER
rgrover1 822:a0f080d1e836 68 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3, YOTTA_CFG_BLE_INSTANCES_3_INITIALIZER
rgrover1 822:a0f080d1e836 69 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_5 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4, YOTTA_CFG_BLE_INSTANCES_4_INITIALIZER
rgrover1 822:a0f080d1e836 70 /* ... add more of the above if ever needed */
rgrover1 822:a0f080d1e836 71
rgrover1 822:a0f080d1e836 72 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(N) EXPAND(CONCATENATE(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_, N))
rgrover1 827:a63b24d78132 73 #elif !defined(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS)
rgrover1 827:a63b24d78132 74 /*
rgrover1 827:a63b24d78132 75 * The following applies when building without yotta. By default BLE_API provides
rgrover1 827:a63b24d78132 76 * a trivial initializer list containing a single constructor: createBLEInstance.
rgrover1 827:a63b24d78132 77 * This may be overridden.
rgrover1 827:a63b24d78132 78 */
rgrover1 827:a63b24d78132 79 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS createBLEInstance
rgrover1 822:a0f080d1e836 80 #endif /* YOTTA_CFG_BLE_INSTANCES_COUNT */
rgrover1 822:a0f080d1e836 81
rgrover1 822:a0f080d1e836 82 typedef BLEInstanceBase *(*InstanceConstructor_t)(void);
rgrover1 822:a0f080d1e836 83 static const InstanceConstructor_t instanceConstructors[BLE::NUM_INSTANCES] = {
rgrover1 822:a0f080d1e836 84 #ifndef YOTTA_CFG_BLE_INSTANCES_COUNT
rgrover1 827:a63b24d78132 85 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS
rgrover1 822:a0f080d1e836 86 #else
rgrover1 822:a0f080d1e836 87 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(YOTTA_CFG_BLE_INSTANCES_COUNT)
rgrover1 822:a0f080d1e836 88 #endif
rgrover1 822:a0f080d1e836 89 };
rgrover1 822:a0f080d1e836 90
rgrover1 825:b65c6a222525 91 BLE &
rgrover1 822:a0f080d1e836 92 BLE::Instance(InstanceID_t id)
rgrover1 822:a0f080d1e836 93 {
rgrover1 822:a0f080d1e836 94 static BLE *singletons[NUM_INSTANCES];
rgrover1 822:a0f080d1e836 95 if (id < NUM_INSTANCES) {
rgrover1 822:a0f080d1e836 96 if (singletons[id] == NULL) {
rgrover1 822:a0f080d1e836 97 singletons[id] = new BLE(id); /* This object will never be freed. */
rgrover1 822:a0f080d1e836 98 }
rgrover1 822:a0f080d1e836 99
rgrover1 822:a0f080d1e836 100 return *singletons[id];
rgrover1 822:a0f080d1e836 101 }
rgrover1 822:a0f080d1e836 102
rgrover1 822:a0f080d1e836 103 /* we come here only in the case of a bad interfaceID. */
rgrover1 822:a0f080d1e836 104 static BLE badSingleton(NUM_INSTANCES /* this is a bad index; and will result in a NULL transport. */);
rgrover1 822:a0f080d1e836 105 return badSingleton;
rgrover1 822:a0f080d1e836 106 }
rgrover1 822:a0f080d1e836 107
rgrover1 890:f3144091ae19 108 BLE::BLE(InstanceID_t instanceID) : transport()
rgrover1 822:a0f080d1e836 109 {
rgrover1 822:a0f080d1e836 110 static BLEInstanceBase *transportInstances[NUM_INSTANCES];
rgrover1 822:a0f080d1e836 111
rgrover1 822:a0f080d1e836 112 if (instanceID < NUM_INSTANCES) {
rgrover1 822:a0f080d1e836 113 if (!transportInstances[instanceID]) {
rgrover1 822:a0f080d1e836 114 transportInstances[instanceID] = instanceConstructors[instanceID](); /* Call the stack's initializer for the transport object. */
rgrover1 822:a0f080d1e836 115 }
rgrover1 822:a0f080d1e836 116 transport = transportInstances[instanceID];
rgrover1 822:a0f080d1e836 117 } else {
rgrover1 822:a0f080d1e836 118 transport = NULL;
rgrover1 822:a0f080d1e836 119 }
rgrover1 712:b04b5db36865 120 }