Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /* mbed Microcontroller Library
MrBedfordVan 0:b9164b348919 2 * Copyright (c) 2006-2013 ARM Limited
MrBedfordVan 0:b9164b348919 3 *
MrBedfordVan 0:b9164b348919 4 * Licensed under the Apache License, Version 2.0 (the "License");
MrBedfordVan 0:b9164b348919 5 * you may not use this file except in compliance with the License.
MrBedfordVan 0:b9164b348919 6 * You may obtain a copy of the License at
MrBedfordVan 0:b9164b348919 7 *
MrBedfordVan 0:b9164b348919 8 * http://www.apache.org/licenses/LICENSE-2.0
MrBedfordVan 0:b9164b348919 9 *
MrBedfordVan 0:b9164b348919 10 * Unless required by applicable law or agreed to in writing, software
MrBedfordVan 0:b9164b348919 11 * distributed under the License is distributed on an "AS IS" BASIS,
MrBedfordVan 0:b9164b348919 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MrBedfordVan 0:b9164b348919 13 * See the License for the specific language governing permissions and
MrBedfordVan 0:b9164b348919 14 * limitations under the License.
MrBedfordVan 0:b9164b348919 15 */
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 #include "ble/BLE.h"
MrBedfordVan 0:b9164b348919 18 #include "ble/BLEInstanceBase.h"
MrBedfordVan 0:b9164b348919 19
MrBedfordVan 0:b9164b348919 20 #if defined(TARGET_OTA_ENABLED)
MrBedfordVan 0:b9164b348919 21 #include "ble/services/DFUService.h"
MrBedfordVan 0:b9164b348919 22 #endif
MrBedfordVan 0:b9164b348919 23
MrBedfordVan 0:b9164b348919 24 ble_error_t
MrBedfordVan 0:b9164b348919 25 BLE::initImplementation(FunctionPointerWithContext<InitializationCompleteCallbackContext *> callback)
MrBedfordVan 0:b9164b348919 26 {
MrBedfordVan 0:b9164b348919 27 ble_error_t err = transport->init(instanceID, callback);
MrBedfordVan 0:b9164b348919 28 if (err != BLE_ERROR_NONE) {
MrBedfordVan 0:b9164b348919 29 return err;
MrBedfordVan 0:b9164b348919 30 }
MrBedfordVan 0:b9164b348919 31
MrBedfordVan 0:b9164b348919 32 /* Platforms enabled for DFU should introduce the DFU Service into
MrBedfordVan 0:b9164b348919 33 * applications automatically. */
MrBedfordVan 0:b9164b348919 34 #if defined(TARGET_OTA_ENABLED)
MrBedfordVan 0:b9164b348919 35 //static DFUService dfu(*this); // defined static so that the object remains alive
MrBedfordVan 0:b9164b348919 36 #endif // TARGET_OTA_ENABLED
MrBedfordVan 0:b9164b348919 37
MrBedfordVan 0:b9164b348919 38 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 39 }
MrBedfordVan 0:b9164b348919 40
MrBedfordVan 0:b9164b348919 41 /**
MrBedfordVan 0:b9164b348919 42 * BLE::Instance() and BLE constructor rely upon a static array of initializers
MrBedfordVan 0:b9164b348919 43 * to create actual BLE transport instances. A description of these instances
MrBedfordVan 0:b9164b348919 44 * and initializers is supposed to be put in some .json file contributing to
MrBedfordVan 0:b9164b348919 45 * yotta's configuration (typically in the target definition described by
MrBedfordVan 0:b9164b348919 46 * target.json). Here's a sample:
MrBedfordVan 0:b9164b348919 47 *
MrBedfordVan 0:b9164b348919 48 * "config": {
MrBedfordVan 0:b9164b348919 49 * ...
MrBedfordVan 0:b9164b348919 50 * "ble_instances": {
MrBedfordVan 0:b9164b348919 51 * "count": 1,
MrBedfordVan 0:b9164b348919 52 * "0" : {
MrBedfordVan 0:b9164b348919 53 * "initializer" : "createBLEInstance"
MrBedfordVan 0:b9164b348919 54 * }
MrBedfordVan 0:b9164b348919 55 * }
MrBedfordVan 0:b9164b348919 56 * ...
MrBedfordVan 0:b9164b348919 57 * }
MrBedfordVan 0:b9164b348919 58 *
MrBedfordVan 0:b9164b348919 59 * The following macros result in translating the above config into a static
MrBedfordVan 0:b9164b348919 60 * array: instanceConstructors.
MrBedfordVan 0:b9164b348919 61 */
MrBedfordVan 0:b9164b348919 62 #ifdef YOTTA_CFG_BLE_INSTANCES_COUNT
MrBedfordVan 0:b9164b348919 63 #define CONCATENATE(A, B) A ## B
MrBedfordVan 0:b9164b348919 64 #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). */
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1 YOTTA_CFG_BLE_INSTANCES_0_INITIALIZER
MrBedfordVan 0:b9164b348919 67 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1, YOTTA_CFG_BLE_INSTANCES_1_INITIALIZER
MrBedfordVan 0:b9164b348919 68 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2, YOTTA_CFG_BLE_INSTANCES_2_INITIALIZER
MrBedfordVan 0:b9164b348919 69 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3, YOTTA_CFG_BLE_INSTANCES_3_INITIALIZER
MrBedfordVan 0:b9164b348919 70 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_5 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4, YOTTA_CFG_BLE_INSTANCES_4_INITIALIZER
MrBedfordVan 0:b9164b348919 71 /* ... add more of the above if ever needed */
MrBedfordVan 0:b9164b348919 72
MrBedfordVan 0:b9164b348919 73 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(N) EXPAND(CONCATENATE(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_, N))
MrBedfordVan 0:b9164b348919 74 #elif !defined(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS)
MrBedfordVan 0:b9164b348919 75 /*
MrBedfordVan 0:b9164b348919 76 * The following applies when building without yotta. By default BLE_API provides
MrBedfordVan 0:b9164b348919 77 * a trivial initializer list containing a single constructor: createBLEInstance.
MrBedfordVan 0:b9164b348919 78 * This may be overridden.
MrBedfordVan 0:b9164b348919 79 */
MrBedfordVan 0:b9164b348919 80 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS createBLEInstance
MrBedfordVan 0:b9164b348919 81 #endif /* YOTTA_CFG_BLE_INSTANCES_COUNT */
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 typedef BLEInstanceBase *(*InstanceConstructor_t)(void);
MrBedfordVan 0:b9164b348919 84 static const InstanceConstructor_t instanceConstructors[BLE::NUM_INSTANCES] = {
MrBedfordVan 0:b9164b348919 85 #ifndef YOTTA_CFG_BLE_INSTANCES_COUNT
MrBedfordVan 0:b9164b348919 86 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS
MrBedfordVan 0:b9164b348919 87 #else
MrBedfordVan 0:b9164b348919 88 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(YOTTA_CFG_BLE_INSTANCES_COUNT)
MrBedfordVan 0:b9164b348919 89 #endif
MrBedfordVan 0:b9164b348919 90 };
MrBedfordVan 0:b9164b348919 91
MrBedfordVan 0:b9164b348919 92 BLE &
MrBedfordVan 0:b9164b348919 93 BLE::Instance(InstanceID_t id)
MrBedfordVan 0:b9164b348919 94 {
MrBedfordVan 0:b9164b348919 95 static BLE *singletons[NUM_INSTANCES];
MrBedfordVan 0:b9164b348919 96 if (id < NUM_INSTANCES) {
MrBedfordVan 0:b9164b348919 97 if (singletons[id] == NULL) {
MrBedfordVan 0:b9164b348919 98 singletons[id] = new BLE(id); /* This object will never be freed. */
MrBedfordVan 0:b9164b348919 99 }
MrBedfordVan 0:b9164b348919 100
MrBedfordVan 0:b9164b348919 101 return *singletons[id];
MrBedfordVan 0:b9164b348919 102 }
MrBedfordVan 0:b9164b348919 103
MrBedfordVan 0:b9164b348919 104 /* we come here only in the case of a bad interfaceID. */
MrBedfordVan 0:b9164b348919 105 static BLE badSingleton(NUM_INSTANCES /* this is a bad index; and will result in a NULL transport. */);
MrBedfordVan 0:b9164b348919 106 return badSingleton;
MrBedfordVan 0:b9164b348919 107 }
MrBedfordVan 0:b9164b348919 108
MrBedfordVan 0:b9164b348919 109 BLE::BLE(InstanceID_t instanceIDIn) : instanceID(instanceIDIn), transport()
MrBedfordVan 0:b9164b348919 110 {
MrBedfordVan 0:b9164b348919 111 static BLEInstanceBase *transportInstances[NUM_INSTANCES];
MrBedfordVan 0:b9164b348919 112
MrBedfordVan 0:b9164b348919 113 if (instanceID < NUM_INSTANCES) {
MrBedfordVan 0:b9164b348919 114 if (!transportInstances[instanceID]) {
MrBedfordVan 0:b9164b348919 115 transportInstances[instanceID] = instanceConstructors[instanceID](); /* Call the stack's initializer for the transport object. */
MrBedfordVan 0:b9164b348919 116 }
MrBedfordVan 0:b9164b348919 117 transport = transportInstances[instanceID];
MrBedfordVan 0:b9164b348919 118 } else {
MrBedfordVan 0:b9164b348919 119 transport = NULL;
MrBedfordVan 0:b9164b348919 120 }
MrBedfordVan 0:b9164b348919 121 }
MrBedfordVan 0:b9164b348919 122
MrBedfordVan 0:b9164b348919 123 bool BLE::hasInitialized(void) const
MrBedfordVan 0:b9164b348919 124 {
MrBedfordVan 0:b9164b348919 125 if (!transport) {
MrBedfordVan 0:b9164b348919 126 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 127 }
MrBedfordVan 0:b9164b348919 128
MrBedfordVan 0:b9164b348919 129 return transport->hasInitialized();
MrBedfordVan 0:b9164b348919 130 }
MrBedfordVan 0:b9164b348919 131
MrBedfordVan 0:b9164b348919 132 ble_error_t BLE::shutdown(void)
MrBedfordVan 0:b9164b348919 133 {
MrBedfordVan 0:b9164b348919 134 if (!transport) {
MrBedfordVan 0:b9164b348919 135 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 136 }
MrBedfordVan 0:b9164b348919 137
MrBedfordVan 0:b9164b348919 138 return transport->shutdown();
MrBedfordVan 0:b9164b348919 139 }
MrBedfordVan 0:b9164b348919 140
MrBedfordVan 0:b9164b348919 141 const char *BLE::getVersion(void)
MrBedfordVan 0:b9164b348919 142 {
MrBedfordVan 0:b9164b348919 143 if (!transport) {
MrBedfordVan 0:b9164b348919 144 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 145 }
MrBedfordVan 0:b9164b348919 146
MrBedfordVan 0:b9164b348919 147 return transport->getVersion();
MrBedfordVan 0:b9164b348919 148 }
MrBedfordVan 0:b9164b348919 149
MrBedfordVan 0:b9164b348919 150 const Gap &BLE::gap() const
MrBedfordVan 0:b9164b348919 151 {
MrBedfordVan 0:b9164b348919 152 if (!transport) {
MrBedfordVan 0:b9164b348919 153 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 154 }
MrBedfordVan 0:b9164b348919 155
MrBedfordVan 0:b9164b348919 156 return transport->getGap();
MrBedfordVan 0:b9164b348919 157 }
MrBedfordVan 0:b9164b348919 158
MrBedfordVan 0:b9164b348919 159 Gap &BLE::gap()
MrBedfordVan 0:b9164b348919 160 {
MrBedfordVan 0:b9164b348919 161 if (!transport) {
MrBedfordVan 0:b9164b348919 162 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 163 }
MrBedfordVan 0:b9164b348919 164
MrBedfordVan 0:b9164b348919 165 return transport->getGap();
MrBedfordVan 0:b9164b348919 166 }
MrBedfordVan 0:b9164b348919 167
MrBedfordVan 0:b9164b348919 168 const GattServer& BLE::gattServer() const
MrBedfordVan 0:b9164b348919 169 {
MrBedfordVan 0:b9164b348919 170 if (!transport) {
MrBedfordVan 0:b9164b348919 171 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 172 }
MrBedfordVan 0:b9164b348919 173
MrBedfordVan 0:b9164b348919 174 return transport->getGattServer();
MrBedfordVan 0:b9164b348919 175 }
MrBedfordVan 0:b9164b348919 176
MrBedfordVan 0:b9164b348919 177 GattServer& BLE::gattServer()
MrBedfordVan 0:b9164b348919 178 {
MrBedfordVan 0:b9164b348919 179 if (!transport) {
MrBedfordVan 0:b9164b348919 180 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 181 }
MrBedfordVan 0:b9164b348919 182
MrBedfordVan 0:b9164b348919 183 return transport->getGattServer();
MrBedfordVan 0:b9164b348919 184 }
MrBedfordVan 0:b9164b348919 185
MrBedfordVan 0:b9164b348919 186 const GattClient& BLE::gattClient() const
MrBedfordVan 0:b9164b348919 187 {
MrBedfordVan 0:b9164b348919 188 if (!transport) {
MrBedfordVan 0:b9164b348919 189 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 190 }
MrBedfordVan 0:b9164b348919 191
MrBedfordVan 0:b9164b348919 192 return transport->getGattClient();
MrBedfordVan 0:b9164b348919 193 }
MrBedfordVan 0:b9164b348919 194
MrBedfordVan 0:b9164b348919 195 GattClient& BLE::gattClient()
MrBedfordVan 0:b9164b348919 196 {
MrBedfordVan 0:b9164b348919 197 if (!transport) {
MrBedfordVan 0:b9164b348919 198 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 199 }
MrBedfordVan 0:b9164b348919 200
MrBedfordVan 0:b9164b348919 201 return transport->getGattClient();
MrBedfordVan 0:b9164b348919 202 }
MrBedfordVan 0:b9164b348919 203
MrBedfordVan 0:b9164b348919 204 const SecurityManager& BLE::securityManager() const
MrBedfordVan 0:b9164b348919 205 {
MrBedfordVan 0:b9164b348919 206 if (!transport) {
MrBedfordVan 0:b9164b348919 207 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 208 }
MrBedfordVan 0:b9164b348919 209
MrBedfordVan 0:b9164b348919 210 return transport->getSecurityManager();
MrBedfordVan 0:b9164b348919 211 }
MrBedfordVan 0:b9164b348919 212
MrBedfordVan 0:b9164b348919 213 SecurityManager& BLE::securityManager()
MrBedfordVan 0:b9164b348919 214 {
MrBedfordVan 0:b9164b348919 215 if (!transport) {
MrBedfordVan 0:b9164b348919 216 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 217 }
MrBedfordVan 0:b9164b348919 218
MrBedfordVan 0:b9164b348919 219 return transport->getSecurityManager();
MrBedfordVan 0:b9164b348919 220 }
MrBedfordVan 0:b9164b348919 221
MrBedfordVan 0:b9164b348919 222 void BLE::waitForEvent(void)
MrBedfordVan 0:b9164b348919 223 {
MrBedfordVan 0:b9164b348919 224 if (!transport) {
MrBedfordVan 0:b9164b348919 225 error("bad handle to underlying transport");
MrBedfordVan 0:b9164b348919 226 }
MrBedfordVan 0:b9164b348919 227
MrBedfordVan 0:b9164b348919 228 transport->waitForEvent();
MrBedfordVan 0:b9164b348919 229 }