fork microbit-dal

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit microbit

Fork of microbit-dal by Wendy Warne

Committer:
bluetooth_mdw
Date:
Wed Feb 08 07:49:17 2017 +0000
Revision:
74:a8f5674a0079
Child:
75:739b6a1c1b50
Eddystone URL test using modified DAL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bluetooth_mdw 74:a8f5674a0079 1 /*
bluetooth_mdw 74:a8f5674a0079 2 The MIT License (MIT)
bluetooth_mdw 74:a8f5674a0079 3
bluetooth_mdw 74:a8f5674a0079 4 Copyright (c) 2016 British Broadcasting Corporation.
bluetooth_mdw 74:a8f5674a0079 5 This software is provided by Lancaster University by arrangement with the BBC.
bluetooth_mdw 74:a8f5674a0079 6
bluetooth_mdw 74:a8f5674a0079 7 Permission is hereby granted, free of charge, to any person obtaining a
bluetooth_mdw 74:a8f5674a0079 8 copy of this software and associated documentation files (the "Software"),
bluetooth_mdw 74:a8f5674a0079 9 to deal in the Software without restriction, including without limitation
bluetooth_mdw 74:a8f5674a0079 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
bluetooth_mdw 74:a8f5674a0079 11 and/or sell copies of the Software, and to permit persons to whom the
bluetooth_mdw 74:a8f5674a0079 12 Software is furnished to do so, subject to the following conditions:
bluetooth_mdw 74:a8f5674a0079 13
bluetooth_mdw 74:a8f5674a0079 14 The above copyright notice and this permission notice shall be included in
bluetooth_mdw 74:a8f5674a0079 15 all copies or substantial portions of the Software.
bluetooth_mdw 74:a8f5674a0079 16
bluetooth_mdw 74:a8f5674a0079 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bluetooth_mdw 74:a8f5674a0079 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bluetooth_mdw 74:a8f5674a0079 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
bluetooth_mdw 74:a8f5674a0079 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bluetooth_mdw 74:a8f5674a0079 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
bluetooth_mdw 74:a8f5674a0079 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
bluetooth_mdw 74:a8f5674a0079 23 DEALINGS IN THE SOFTWARE.
bluetooth_mdw 74:a8f5674a0079 24 */
bluetooth_mdw 74:a8f5674a0079 25
bluetooth_mdw 74:a8f5674a0079 26 #include "MicroBitConfig.h"
bluetooth_mdw 74:a8f5674a0079 27 #include "MicroBitEddystone.h"
bluetooth_mdw 74:a8f5674a0079 28
bluetooth_mdw 74:a8f5674a0079 29 MicroBitEddystone *MicroBitEddystone::_instance = NULL;
bluetooth_mdw 74:a8f5674a0079 30
bluetooth_mdw 74:a8f5674a0079 31 /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ.
bluetooth_mdw 74:a8f5674a0079 32 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
bluetooth_mdw 74:a8f5674a0079 33 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
bluetooth_mdw 74:a8f5674a0079 34 * as a compatability option, but does not support the options used...
bluetooth_mdw 74:a8f5674a0079 35 */
bluetooth_mdw 74:a8f5674a0079 36 #if !defined(__arm)
bluetooth_mdw 74:a8f5674a0079 37 #pragma GCC diagnostic ignored "-Wunused-function"
bluetooth_mdw 74:a8f5674a0079 38 #pragma GCC diagnostic push
bluetooth_mdw 74:a8f5674a0079 39 #pragma GCC diagnostic ignored "-Wunused-parameter"
bluetooth_mdw 74:a8f5674a0079 40 #endif
bluetooth_mdw 74:a8f5674a0079 41
bluetooth_mdw 74:a8f5674a0079 42 /*
bluetooth_mdw 74:a8f5674a0079 43 * Return to our predefined compiler settings.
bluetooth_mdw 74:a8f5674a0079 44 */
bluetooth_mdw 74:a8f5674a0079 45 #if !defined(__arm)
bluetooth_mdw 74:a8f5674a0079 46 #pragma GCC diagnostic pop
bluetooth_mdw 74:a8f5674a0079 47 #endif
bluetooth_mdw 74:a8f5674a0079 48
bluetooth_mdw 74:a8f5674a0079 49 const uint8_t EDDYSTONE_UUID[] = {0xAA, 0xFE};
bluetooth_mdw 74:a8f5674a0079 50
bluetooth_mdw 74:a8f5674a0079 51 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
bluetooth_mdw 74:a8f5674a0079 52 const char *EDDYSTONE_URL_PREFIXES[] = {"http://www.", "https://www.", "http://", "https://"};
bluetooth_mdw 74:a8f5674a0079 53 const size_t EDDYSTONE_URL_PREFIXES_LENGTH = sizeof(EDDYSTONE_URL_PREFIXES) / sizeof(char *);
bluetooth_mdw 74:a8f5674a0079 54 const char *EDDYSTONE_URL_SUFFIXES[] = {".com/", ".org/", ".edu/", ".net/", ".info/", ".biz/", ".gov/", ".com", ".org", ".edu", ".net", ".info", ".biz", ".gov"};
bluetooth_mdw 74:a8f5674a0079 55 const size_t EDDYSTONE_URL_SUFFIXES_LENGTH = sizeof(EDDYSTONE_URL_SUFFIXES) / sizeof(char *);
bluetooth_mdw 74:a8f5674a0079 56 const int EDDYSTONE_URL_MAX_LENGTH = 18;
bluetooth_mdw 74:a8f5674a0079 57 const uint8_t EDDYSTONE_URL_FRAME_TYPE = 0x10;
bluetooth_mdw 74:a8f5674a0079 58 #endif
bluetooth_mdw 74:a8f5674a0079 59
bluetooth_mdw 74:a8f5674a0079 60 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID)
bluetooth_mdw 74:a8f5674a0079 61 const int EDDYSTONE_UID_NAMESPACE_MAX_LENGTH = 10;
bluetooth_mdw 74:a8f5674a0079 62 const int EDDYSTONE_UID_INSTANCE_MAX_LENGTH = 6;
bluetooth_mdw 74:a8f5674a0079 63 const uint8_t EDDYSTONE_UID_FRAME_TYPE = 0x00;
bluetooth_mdw 74:a8f5674a0079 64 #endif
bluetooth_mdw 74:a8f5674a0079 65
bluetooth_mdw 74:a8f5674a0079 66 /**
bluetooth_mdw 74:a8f5674a0079 67 * Constructor.
bluetooth_mdw 74:a8f5674a0079 68 *
bluetooth_mdw 74:a8f5674a0079 69 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
bluetooth_mdw 74:a8f5674a0079 70 *
bluetooth_mdw 74:a8f5674a0079 71 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
bluetooth_mdw 74:a8f5674a0079 72 *
bluetooth_mdw 74:a8f5674a0079 73 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
bluetooth_mdw 74:a8f5674a0079 74 * Hence, the init() member function should be used to initialise the BLE stack.
bluetooth_mdw 74:a8f5674a0079 75 */
bluetooth_mdw 74:a8f5674a0079 76 MicroBitEddystone::MicroBitEddystone()
bluetooth_mdw 74:a8f5674a0079 77 {
bluetooth_mdw 74:a8f5674a0079 78 }
bluetooth_mdw 74:a8f5674a0079 79
bluetooth_mdw 74:a8f5674a0079 80 MicroBitEddystone* MicroBitEddystone::getInstance()
bluetooth_mdw 74:a8f5674a0079 81 {
bluetooth_mdw 74:a8f5674a0079 82 if (_instance == 0)
bluetooth_mdw 74:a8f5674a0079 83 _instance = new MicroBitEddystone;
bluetooth_mdw 74:a8f5674a0079 84
bluetooth_mdw 74:a8f5674a0079 85 return _instance;
bluetooth_mdw 74:a8f5674a0079 86 }
bluetooth_mdw 74:a8f5674a0079 87
bluetooth_mdw 74:a8f5674a0079 88 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
bluetooth_mdw 74:a8f5674a0079 89
bluetooth_mdw 74:a8f5674a0079 90 /**
bluetooth_mdw 74:a8f5674a0079 91 * Set the content of Eddystone URL frames
bluetooth_mdw 74:a8f5674a0079 92 *
bluetooth_mdw 74:a8f5674a0079 93 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 94 *
bluetooth_mdw 74:a8f5674a0079 95 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 96 *
bluetooth_mdw 74:a8f5674a0079 97 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 98 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 99 */
bluetooth_mdw 74:a8f5674a0079 100 int MicroBitEddystone::setURL(BLEDevice* ble, const char* url, int8_t calibratedPower)
bluetooth_mdw 74:a8f5674a0079 101 {
bluetooth_mdw 74:a8f5674a0079 102 int urlDataLength = 0;
bluetooth_mdw 74:a8f5674a0079 103 char urlData[EDDYSTONE_URL_MAX_LENGTH];
bluetooth_mdw 74:a8f5674a0079 104 memset(urlData, 0, EDDYSTONE_URL_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 105
bluetooth_mdw 74:a8f5674a0079 106 if (url == NULL || strlen(url) == 0)
bluetooth_mdw 74:a8f5674a0079 107 return MICROBIT_INVALID_PARAMETER;
bluetooth_mdw 74:a8f5674a0079 108
bluetooth_mdw 74:a8f5674a0079 109 // Prefix
bluetooth_mdw 74:a8f5674a0079 110 for (size_t i = 0; i < EDDYSTONE_URL_PREFIXES_LENGTH; i++)
bluetooth_mdw 74:a8f5674a0079 111 {
bluetooth_mdw 74:a8f5674a0079 112 size_t prefixLen = strlen(EDDYSTONE_URL_PREFIXES[i]);
bluetooth_mdw 74:a8f5674a0079 113 if (strncmp(url, EDDYSTONE_URL_PREFIXES[i], prefixLen) == 0)
bluetooth_mdw 74:a8f5674a0079 114 {
bluetooth_mdw 74:a8f5674a0079 115 urlData[urlDataLength++] = i;
bluetooth_mdw 74:a8f5674a0079 116 url += prefixLen;
bluetooth_mdw 74:a8f5674a0079 117 break;
bluetooth_mdw 74:a8f5674a0079 118 }
bluetooth_mdw 74:a8f5674a0079 119 }
bluetooth_mdw 74:a8f5674a0079 120
bluetooth_mdw 74:a8f5674a0079 121 // Suffix
bluetooth_mdw 74:a8f5674a0079 122 while (*url && (urlDataLength < EDDYSTONE_URL_MAX_LENGTH))
bluetooth_mdw 74:a8f5674a0079 123 {
bluetooth_mdw 74:a8f5674a0079 124 size_t i;
bluetooth_mdw 74:a8f5674a0079 125 for (i = 0; i < EDDYSTONE_URL_SUFFIXES_LENGTH; i++)
bluetooth_mdw 74:a8f5674a0079 126 {
bluetooth_mdw 74:a8f5674a0079 127 size_t suffixLen = strlen(EDDYSTONE_URL_SUFFIXES[i]);
bluetooth_mdw 74:a8f5674a0079 128 if (strncmp(url, EDDYSTONE_URL_SUFFIXES[i], suffixLen) == 0)
bluetooth_mdw 74:a8f5674a0079 129 {
bluetooth_mdw 74:a8f5674a0079 130 urlData[urlDataLength++] = i;
bluetooth_mdw 74:a8f5674a0079 131 url += suffixLen;
bluetooth_mdw 74:a8f5674a0079 132 break;
bluetooth_mdw 74:a8f5674a0079 133 }
bluetooth_mdw 74:a8f5674a0079 134 }
bluetooth_mdw 74:a8f5674a0079 135
bluetooth_mdw 74:a8f5674a0079 136 // Catch the default case where the suffix doesn't match a preset ones
bluetooth_mdw 74:a8f5674a0079 137 if (i == EDDYSTONE_URL_SUFFIXES_LENGTH)
bluetooth_mdw 74:a8f5674a0079 138 {
bluetooth_mdw 74:a8f5674a0079 139 urlData[urlDataLength++] = *url;
bluetooth_mdw 74:a8f5674a0079 140 ++url;
bluetooth_mdw 74:a8f5674a0079 141 }
bluetooth_mdw 74:a8f5674a0079 142 }
bluetooth_mdw 74:a8f5674a0079 143
bluetooth_mdw 74:a8f5674a0079 144 uint8_t rawFrame[EDDYSTONE_URL_MAX_LENGTH + 4];
bluetooth_mdw 74:a8f5674a0079 145 size_t index = 0;
bluetooth_mdw 74:a8f5674a0079 146 rawFrame[index++] = EDDYSTONE_UUID[0];
bluetooth_mdw 74:a8f5674a0079 147 rawFrame[index++] = EDDYSTONE_UUID[1];
bluetooth_mdw 74:a8f5674a0079 148 rawFrame[index++] = EDDYSTONE_URL_FRAME_TYPE;
bluetooth_mdw 74:a8f5674a0079 149 rawFrame[index++] = calibratedPower;
bluetooth_mdw 74:a8f5674a0079 150 memcpy(rawFrame + index, urlData, urlDataLength);
bluetooth_mdw 74:a8f5674a0079 151
bluetooth_mdw 74:a8f5674a0079 152 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, EDDYSTONE_UUID, sizeof(EDDYSTONE_UUID));
bluetooth_mdw 74:a8f5674a0079 153 ble->accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, rawFrame, index + urlDataLength);
bluetooth_mdw 74:a8f5674a0079 154
bluetooth_mdw 74:a8f5674a0079 155 return MICROBIT_OK;
bluetooth_mdw 74:a8f5674a0079 156 }
bluetooth_mdw 74:a8f5674a0079 157
bluetooth_mdw 74:a8f5674a0079 158 /**
bluetooth_mdw 74:a8f5674a0079 159 * Set the content of Eddystone URL frames, but accepts a ManagedString as a url.
bluetooth_mdw 74:a8f5674a0079 160 *
bluetooth_mdw 74:a8f5674a0079 161 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 162 *
bluetooth_mdw 74:a8f5674a0079 163 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 164 *
bluetooth_mdw 74:a8f5674a0079 165 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 166 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 167 */
bluetooth_mdw 74:a8f5674a0079 168 int MicroBitEddystone::setURL(BLEDevice* ble, ManagedString url, int8_t calibratedPower)
bluetooth_mdw 74:a8f5674a0079 169 {
bluetooth_mdw 74:a8f5674a0079 170 return setURL(ble, (char *)url.toCharArray(), calibratedPower);
bluetooth_mdw 74:a8f5674a0079 171 }
bluetooth_mdw 74:a8f5674a0079 172 #endif
bluetooth_mdw 74:a8f5674a0079 173
bluetooth_mdw 74:a8f5674a0079 174 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID)
bluetooth_mdw 74:a8f5674a0079 175
bluetooth_mdw 74:a8f5674a0079 176 /**
bluetooth_mdw 74:a8f5674a0079 177 * Set the content of Eddystone UID frames
bluetooth_mdw 74:a8f5674a0079 178 *
bluetooth_mdw 74:a8f5674a0079 179 * @param uid_namespace the uid namespace. Must 10 bytes long.
bluetooth_mdw 74:a8f5674a0079 180 *
bluetooth_mdw 74:a8f5674a0079 181 * @param uid_instance the uid instance value. Must 6 bytes long.
bluetooth_mdw 74:a8f5674a0079 182 *
bluetooth_mdw 74:a8f5674a0079 183 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 184 *
bluetooth_mdw 74:a8f5674a0079 185 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 186 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 187 */
bluetooth_mdw 74:a8f5674a0079 188 int MicroBitEddystone::setUID(BLEDevice* ble, const char* uid_namespace, const char* uid_instance, int8_t calibratedPower)
bluetooth_mdw 74:a8f5674a0079 189 {
bluetooth_mdw 74:a8f5674a0079 190 if (uid_namespace == NULL || uid_instance == NULL)
bluetooth_mdw 74:a8f5674a0079 191 return MICROBIT_INVALID_PARAMETER;
bluetooth_mdw 74:a8f5674a0079 192
bluetooth_mdw 74:a8f5674a0079 193 char uidData[EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH];
bluetooth_mdw 74:a8f5674a0079 194
bluetooth_mdw 74:a8f5674a0079 195 // UID namespace
bluetooth_mdw 74:a8f5674a0079 196 memcpy(uidData, uid_namespace, EDDYSTONE_UID_NAMESPACE_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 197
bluetooth_mdw 74:a8f5674a0079 198 // UID instance
bluetooth_mdw 74:a8f5674a0079 199 memcpy(uidData + EDDYSTONE_UID_NAMESPACE_MAX_LENGTH, uid_instance, EDDYSTONE_UID_INSTANCE_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 200
bluetooth_mdw 74:a8f5674a0079 201 uint8_t rawFrame[EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH + 4];
bluetooth_mdw 74:a8f5674a0079 202 size_t index = 0;
bluetooth_mdw 74:a8f5674a0079 203 rawFrame[index++] = EDDYSTONE_UUID[0];
bluetooth_mdw 74:a8f5674a0079 204 rawFrame[index++] = EDDYSTONE_UUID[1];
bluetooth_mdw 74:a8f5674a0079 205 rawFrame[index++] = EDDYSTONE_UID_FRAME_TYPE;
bluetooth_mdw 74:a8f5674a0079 206 rawFrame[index++] = calibratedPower;
bluetooth_mdw 74:a8f5674a0079 207 memcpy(rawFrame + index, uidData, EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 208
bluetooth_mdw 74:a8f5674a0079 209 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, EDDYSTONE_UUID, sizeof(EDDYSTONE_UUID));
bluetooth_mdw 74:a8f5674a0079 210 ble->accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, rawFrame, index + EDDYSTONE_UID_NAMESPACE_MAX_LENGTH + EDDYSTONE_UID_INSTANCE_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 211
bluetooth_mdw 74:a8f5674a0079 212 return MICROBIT_OK;
bluetooth_mdw 74:a8f5674a0079 213 }
bluetooth_mdw 74:a8f5674a0079 214
bluetooth_mdw 74:a8f5674a0079 215 #endif