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.
Dependents: Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal
Fork of BLE_API by
BLE.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "ble/BLE.h" 00018 00019 #if defined(TARGET_OTA_ENABLED) 00020 #include "ble/services/DFUService.h" 00021 #endif 00022 00023 ble_error_t 00024 BLE::init() 00025 { 00026 ble_error_t err = transport->init(); 00027 if (err != BLE_ERROR_NONE) { 00028 return err; 00029 } 00030 00031 /* Platforms enabled for DFU should introduce the DFU Service into 00032 * applications automatically. */ 00033 #if defined(TARGET_OTA_ENABLED) 00034 static DFUService dfu(*this); // defined static so that the object remains alive 00035 #endif // TARGET_OTA_ENABLED 00036 00037 return BLE_ERROR_NONE; 00038 } 00039 00040 /** 00041 * BLE::Instance() and BLE constructor rely upon a static array of initializers 00042 * to create actual BLE transport instances. A description of these instances 00043 * and initializers is supposed to be put in some .json file contributing to 00044 * yotta's configuration (typically in the target definition described by 00045 * target.json). Here's a sample: 00046 * 00047 * "config": { 00048 * ... 00049 * "ble_instances": { 00050 * "count": 1, 00051 * "0" : { 00052 * "initializer" : "createBLEInstance" 00053 * } 00054 * } 00055 * ... 00056 * } 00057 * 00058 * The following macros result in translating the above config into a static 00059 * array: instanceConstructors. 00060 */ 00061 #ifdef YOTTA_CFG_BLE_INSTANCES_COUNT 00062 #define CONCATENATE(A, B) A ## B 00063 #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). */ 00064 00065 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1 YOTTA_CFG_BLE_INSTANCES_0_INITIALIZER 00066 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1, YOTTA_CFG_BLE_INSTANCES_1_INITIALIZER 00067 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2, YOTTA_CFG_BLE_INSTANCES_2_INITIALIZER 00068 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3, YOTTA_CFG_BLE_INSTANCES_3_INITIALIZER 00069 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_5 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4, YOTTA_CFG_BLE_INSTANCES_4_INITIALIZER 00070 /* ... add more of the above if ever needed */ 00071 00072 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(N) EXPAND(CONCATENATE(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_, N)) 00073 #elif !defined(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS) 00074 /* 00075 * The following applies when building without yotta. By default BLE_API provides 00076 * a trivial initializer list containing a single constructor: createBLEInstance. 00077 * This may be overridden. 00078 */ 00079 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS createBLEInstance 00080 #endif /* YOTTA_CFG_BLE_INSTANCES_COUNT */ 00081 00082 typedef BLEInstanceBase *(*InstanceConstructor_t)(void); 00083 static const InstanceConstructor_t instanceConstructors[BLE::NUM_INSTANCES] = { 00084 #ifndef YOTTA_CFG_BLE_INSTANCES_COUNT 00085 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS 00086 #else 00087 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(YOTTA_CFG_BLE_INSTANCES_COUNT) 00088 #endif 00089 }; 00090 00091 BLE & 00092 BLE::Instance(InstanceID_t id) 00093 { 00094 static BLE *singletons[NUM_INSTANCES]; 00095 if (id < NUM_INSTANCES) { 00096 if (singletons[id] == NULL) { 00097 singletons[id] = new BLE(id); /* This object will never be freed. */ 00098 } 00099 00100 return *singletons[id]; 00101 } 00102 00103 /* we come here only in the case of a bad interfaceID. */ 00104 static BLE badSingleton(NUM_INSTANCES /* this is a bad index; and will result in a NULL transport. */); 00105 return badSingleton; 00106 } 00107 00108 BLE::BLE(InstanceID_t instanceID) : transport() 00109 { 00110 static BLEInstanceBase *transportInstances[NUM_INSTANCES]; 00111 00112 if (instanceID < NUM_INSTANCES) { 00113 if (!transportInstances[instanceID]) { 00114 transportInstances[instanceID] = instanceConstructors[instanceID](); /* Call the stack's initializer for the transport object. */ 00115 } 00116 transport = transportInstances[instanceID]; 00117 } else { 00118 transport = NULL; 00119 } 00120 }
Generated on Mon Jul 18 2022 21:56:46 by
