Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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