add iBeacon functionality

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Ken Ogami

Committer:
wwbluetooth
Date:
Mon Jul 17 20:49:56 2017 +0000
Revision:
75:739b6a1c1b50
Parent:
74:a8f5674a0079
sdf

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
wwbluetooth 75:739b6a1c1b50 60
bluetooth_mdw 74:a8f5674a0079 61
bluetooth_mdw 74:a8f5674a0079 62 /**
bluetooth_mdw 74:a8f5674a0079 63 * Constructor.
bluetooth_mdw 74:a8f5674a0079 64 *
bluetooth_mdw 74:a8f5674a0079 65 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
bluetooth_mdw 74:a8f5674a0079 66 *
bluetooth_mdw 74:a8f5674a0079 67 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
bluetooth_mdw 74:a8f5674a0079 68 *
bluetooth_mdw 74:a8f5674a0079 69 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
bluetooth_mdw 74:a8f5674a0079 70 * Hence, the init() member function should be used to initialise the BLE stack.
bluetooth_mdw 74:a8f5674a0079 71 */
bluetooth_mdw 74:a8f5674a0079 72 MicroBitEddystone::MicroBitEddystone()
bluetooth_mdw 74:a8f5674a0079 73 {
bluetooth_mdw 74:a8f5674a0079 74 }
bluetooth_mdw 74:a8f5674a0079 75
bluetooth_mdw 74:a8f5674a0079 76 MicroBitEddystone* MicroBitEddystone::getInstance()
bluetooth_mdw 74:a8f5674a0079 77 {
bluetooth_mdw 74:a8f5674a0079 78 if (_instance == 0)
bluetooth_mdw 74:a8f5674a0079 79 _instance = new MicroBitEddystone;
bluetooth_mdw 74:a8f5674a0079 80
bluetooth_mdw 74:a8f5674a0079 81 return _instance;
bluetooth_mdw 74:a8f5674a0079 82 }
bluetooth_mdw 74:a8f5674a0079 83
bluetooth_mdw 74:a8f5674a0079 84 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
bluetooth_mdw 74:a8f5674a0079 85
bluetooth_mdw 74:a8f5674a0079 86 /**
bluetooth_mdw 74:a8f5674a0079 87 * Set the content of Eddystone URL frames
bluetooth_mdw 74:a8f5674a0079 88 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 89 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 90 * @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 91 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 92 */
bluetooth_mdw 74:a8f5674a0079 93 int MicroBitEddystone::setURL(BLEDevice* ble, const char* url, int8_t calibratedPower)
bluetooth_mdw 74:a8f5674a0079 94 {
bluetooth_mdw 74:a8f5674a0079 95 int urlDataLength = 0;
bluetooth_mdw 74:a8f5674a0079 96 char urlData[EDDYSTONE_URL_MAX_LENGTH];
bluetooth_mdw 74:a8f5674a0079 97 memset(urlData, 0, EDDYSTONE_URL_MAX_LENGTH);
bluetooth_mdw 74:a8f5674a0079 98
bluetooth_mdw 74:a8f5674a0079 99 if (url == NULL || strlen(url) == 0)
bluetooth_mdw 74:a8f5674a0079 100 return MICROBIT_INVALID_PARAMETER;
bluetooth_mdw 74:a8f5674a0079 101
bluetooth_mdw 74:a8f5674a0079 102 // Prefix
bluetooth_mdw 74:a8f5674a0079 103 for (size_t i = 0; i < EDDYSTONE_URL_PREFIXES_LENGTH; i++)
bluetooth_mdw 74:a8f5674a0079 104 {
bluetooth_mdw 74:a8f5674a0079 105 size_t prefixLen = strlen(EDDYSTONE_URL_PREFIXES[i]);
bluetooth_mdw 74:a8f5674a0079 106 if (strncmp(url, EDDYSTONE_URL_PREFIXES[i], prefixLen) == 0)
bluetooth_mdw 74:a8f5674a0079 107 {
bluetooth_mdw 74:a8f5674a0079 108 urlData[urlDataLength++] = i;
bluetooth_mdw 74:a8f5674a0079 109 url += prefixLen;
bluetooth_mdw 74:a8f5674a0079 110 break;
bluetooth_mdw 74:a8f5674a0079 111 }
bluetooth_mdw 74:a8f5674a0079 112 }
bluetooth_mdw 74:a8f5674a0079 113
bluetooth_mdw 74:a8f5674a0079 114 // Suffix
bluetooth_mdw 74:a8f5674a0079 115 while (*url && (urlDataLength < EDDYSTONE_URL_MAX_LENGTH))
bluetooth_mdw 74:a8f5674a0079 116 {
bluetooth_mdw 74:a8f5674a0079 117 size_t i;
bluetooth_mdw 74:a8f5674a0079 118 for (i = 0; i < EDDYSTONE_URL_SUFFIXES_LENGTH; i++)
bluetooth_mdw 74:a8f5674a0079 119 {
bluetooth_mdw 74:a8f5674a0079 120 size_t suffixLen = strlen(EDDYSTONE_URL_SUFFIXES[i]);
bluetooth_mdw 74:a8f5674a0079 121 if (strncmp(url, EDDYSTONE_URL_SUFFIXES[i], suffixLen) == 0)
bluetooth_mdw 74:a8f5674a0079 122 {
bluetooth_mdw 74:a8f5674a0079 123 urlData[urlDataLength++] = i;
bluetooth_mdw 74:a8f5674a0079 124 url += suffixLen;
bluetooth_mdw 74:a8f5674a0079 125 break;
bluetooth_mdw 74:a8f5674a0079 126 }
bluetooth_mdw 74:a8f5674a0079 127 }
bluetooth_mdw 74:a8f5674a0079 128
bluetooth_mdw 74:a8f5674a0079 129 // Catch the default case where the suffix doesn't match a preset ones
bluetooth_mdw 74:a8f5674a0079 130 if (i == EDDYSTONE_URL_SUFFIXES_LENGTH)
bluetooth_mdw 74:a8f5674a0079 131 {
bluetooth_mdw 74:a8f5674a0079 132 urlData[urlDataLength++] = *url;
bluetooth_mdw 74:a8f5674a0079 133 ++url;
bluetooth_mdw 74:a8f5674a0079 134 }
bluetooth_mdw 74:a8f5674a0079 135 }
bluetooth_mdw 74:a8f5674a0079 136
bluetooth_mdw 74:a8f5674a0079 137 uint8_t rawFrame[EDDYSTONE_URL_MAX_LENGTH + 4];
bluetooth_mdw 74:a8f5674a0079 138 size_t index = 0;
bluetooth_mdw 74:a8f5674a0079 139 rawFrame[index++] = EDDYSTONE_UUID[0];
bluetooth_mdw 74:a8f5674a0079 140 rawFrame[index++] = EDDYSTONE_UUID[1];
bluetooth_mdw 74:a8f5674a0079 141 rawFrame[index++] = EDDYSTONE_URL_FRAME_TYPE;
bluetooth_mdw 74:a8f5674a0079 142 rawFrame[index++] = calibratedPower;
bluetooth_mdw 74:a8f5674a0079 143 memcpy(rawFrame + index, urlData, urlDataLength);
bluetooth_mdw 74:a8f5674a0079 144
bluetooth_mdw 74:a8f5674a0079 145 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, EDDYSTONE_UUID, sizeof(EDDYSTONE_UUID));
bluetooth_mdw 74:a8f5674a0079 146 ble->accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, rawFrame, index + urlDataLength);
bluetooth_mdw 74:a8f5674a0079 147
bluetooth_mdw 74:a8f5674a0079 148 return MICROBIT_OK;
bluetooth_mdw 74:a8f5674a0079 149 }
bluetooth_mdw 74:a8f5674a0079 150
bluetooth_mdw 74:a8f5674a0079 151 /**
bluetooth_mdw 74:a8f5674a0079 152 * Set the content of Eddystone URL frames, but accepts a ManagedString as a url.
bluetooth_mdw 74:a8f5674a0079 153 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 154 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 155 * @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 156 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 157 */
bluetooth_mdw 74:a8f5674a0079 158 int MicroBitEddystone::setURL(BLEDevice* ble, ManagedString url, int8_t calibratedPower)
bluetooth_mdw 74:a8f5674a0079 159 {
bluetooth_mdw 74:a8f5674a0079 160 return setURL(ble, (char *)url.toCharArray(), calibratedPower);
bluetooth_mdw 74:a8f5674a0079 161 }
bluetooth_mdw 74:a8f5674a0079 162 #endif
bluetooth_mdw 74:a8f5674a0079 163