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 #ifndef __UUID_H__
MrBedfordVan 0:b9164b348919 18 #define __UUID_H__
MrBedfordVan 0:b9164b348919 19
MrBedfordVan 0:b9164b348919 20 #include <stdint.h>
MrBedfordVan 0:b9164b348919 21 #include <string.h>
MrBedfordVan 0:b9164b348919 22 #include <algorithm>
MrBedfordVan 0:b9164b348919 23
MrBedfordVan 0:b9164b348919 24 #include "blecommon.h"
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * A trivial converter for single hexadecimal character to unsigned-int.
MrBedfordVan 0:b9164b348919 28 * @param c hexadecimal character.
MrBedfordVan 0:b9164b348919 29 * @return the corresponding value as unsigned int.
MrBedfordVan 0:b9164b348919 30 */
MrBedfordVan 0:b9164b348919 31 static uint8_t char2int(char c) {
MrBedfordVan 0:b9164b348919 32 if ((c >= '0') && (c <= '9')) {
MrBedfordVan 0:b9164b348919 33 return c - '0';
MrBedfordVan 0:b9164b348919 34 } else if ((c >= 'a') && (c <= 'f')) {
MrBedfordVan 0:b9164b348919 35 return c - 'a' + 10;
MrBedfordVan 0:b9164b348919 36 } else if ((c >= 'A') && (c <= 'F')) {
MrBedfordVan 0:b9164b348919 37 return c - 'A' + 10;
MrBedfordVan 0:b9164b348919 38 } else {
MrBedfordVan 0:b9164b348919 39 return 0;
MrBedfordVan 0:b9164b348919 40 }
MrBedfordVan 0:b9164b348919 41 }
MrBedfordVan 0:b9164b348919 42
MrBedfordVan 0:b9164b348919 43 class UUID {
MrBedfordVan 0:b9164b348919 44 public:
MrBedfordVan 0:b9164b348919 45 enum UUID_Type_t {
MrBedfordVan 0:b9164b348919 46 UUID_TYPE_SHORT = 0, // Short BLE UUID.
MrBedfordVan 0:b9164b348919 47 UUID_TYPE_LONG = 1 // Full 128-bit UUID.
MrBedfordVan 0:b9164b348919 48 };
MrBedfordVan 0:b9164b348919 49
MrBedfordVan 0:b9164b348919 50 /**
MrBedfordVan 0:b9164b348919 51 * An enumeration to specify byte ordering of the long version of the UUID.
MrBedfordVan 0:b9164b348919 52 */
MrBedfordVan 0:b9164b348919 53 typedef enum {
MrBedfordVan 0:b9164b348919 54 MSB, /*!< Most-significant byte first (at the smallest address) */
MrBedfordVan 0:b9164b348919 55 LSB /*!< least-significant byte first (at the smallest address) */
MrBedfordVan 0:b9164b348919 56 } ByteOrder_t;
MrBedfordVan 0:b9164b348919 57
MrBedfordVan 0:b9164b348919 58 typedef uint16_t ShortUUIDBytes_t;
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 static const unsigned LENGTH_OF_LONG_UUID = 16;
MrBedfordVan 0:b9164b348919 61 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
MrBedfordVan 0:b9164b348919 62
MrBedfordVan 0:b9164b348919 63 static const unsigned MAX_UUID_STRING_LENGTH = LENGTH_OF_LONG_UUID * 2 + 4;
MrBedfordVan 0:b9164b348919 64
MrBedfordVan 0:b9164b348919 65 public:
MrBedfordVan 0:b9164b348919 66
MrBedfordVan 0:b9164b348919 67 /**
MrBedfordVan 0:b9164b348919 68 * Creates a new 128-bit UUID.
MrBedfordVan 0:b9164b348919 69 *
MrBedfordVan 0:b9164b348919 70 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
MrBedfordVan 0:b9164b348919 71 * different service or characteristics on the BLE device.
MrBedfordVan 0:b9164b348919 72 *
MrBedfordVan 0:b9164b348919 73 * @param stringUUID
MrBedfordVan 0:b9164b348919 74 * The 128-bit (16-byte) UUID as a human readable const-string.
MrBedfordVan 0:b9164b348919 75 * Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
MrBedfordVan 0:b9164b348919 76 * Upper and lower case supported. Hyphens are optional, but only
MrBedfordVan 0:b9164b348919 77 * upto four of them. The UUID is stored internally as a 16 byte
MrBedfordVan 0:b9164b348919 78 * array, LSB (little endian), which is opposite from the string.
MrBedfordVan 0:b9164b348919 79 */
MrBedfordVan 0:b9164b348919 80 UUID(const char* stringUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
MrBedfordVan 0:b9164b348919 81 bool nibble = false;
MrBedfordVan 0:b9164b348919 82 uint8_t byte = 0;
MrBedfordVan 0:b9164b348919 83 size_t baseIndex = 0;
MrBedfordVan 0:b9164b348919 84 uint8_t tempUUID[LENGTH_OF_LONG_UUID];
MrBedfordVan 0:b9164b348919 85
MrBedfordVan 0:b9164b348919 86 // Iterate through string, abort if NULL is encountered prematurely.
MrBedfordVan 0:b9164b348919 87 // Ignore upto four hyphens.
MrBedfordVan 0:b9164b348919 88 for (size_t index = 0; (index < MAX_UUID_STRING_LENGTH) && (baseIndex < LENGTH_OF_LONG_UUID); index++) {
MrBedfordVan 0:b9164b348919 89 if (stringUUID[index] == '\0') {
MrBedfordVan 0:b9164b348919 90 // error abort
MrBedfordVan 0:b9164b348919 91 break;
MrBedfordVan 0:b9164b348919 92 } else if (stringUUID[index] == '-') {
MrBedfordVan 0:b9164b348919 93 // ignore hyphen
MrBedfordVan 0:b9164b348919 94 continue;
MrBedfordVan 0:b9164b348919 95 } else if (nibble) {
MrBedfordVan 0:b9164b348919 96 // got second nibble
MrBedfordVan 0:b9164b348919 97 byte |= char2int(stringUUID[index]);
MrBedfordVan 0:b9164b348919 98 nibble = false;
MrBedfordVan 0:b9164b348919 99
MrBedfordVan 0:b9164b348919 100 // store copy
MrBedfordVan 0:b9164b348919 101 tempUUID[baseIndex++] = byte;
MrBedfordVan 0:b9164b348919 102 } else {
MrBedfordVan 0:b9164b348919 103 // got first nibble
MrBedfordVan 0:b9164b348919 104 byte = char2int(stringUUID[index]) << 4;
MrBedfordVan 0:b9164b348919 105 nibble = true;
MrBedfordVan 0:b9164b348919 106 }
MrBedfordVan 0:b9164b348919 107 }
MrBedfordVan 0:b9164b348919 108
MrBedfordVan 0:b9164b348919 109 // populate internal variables if string was successfully parsed
MrBedfordVan 0:b9164b348919 110 if (baseIndex == LENGTH_OF_LONG_UUID) {
MrBedfordVan 0:b9164b348919 111 setupLong(tempUUID, UUID::MSB);
MrBedfordVan 0:b9164b348919 112 } else {
MrBedfordVan 0:b9164b348919 113 const uint8_t sig[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
MrBedfordVan 0:b9164b348919 114 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
MrBedfordVan 0:b9164b348919 115 setupLong(sig, UUID::MSB);
MrBedfordVan 0:b9164b348919 116 }
MrBedfordVan 0:b9164b348919 117 }
MrBedfordVan 0:b9164b348919 118
MrBedfordVan 0:b9164b348919 119 /**
MrBedfordVan 0:b9164b348919 120 * Creates a new 128-bit UUID.
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
MrBedfordVan 0:b9164b348919 123 * different service or characteristics on the BLE device.
MrBedfordVan 0:b9164b348919 124 *
MrBedfordVan 0:b9164b348919 125 * @param longUUID
MrBedfordVan 0:b9164b348919 126 * The 128-bit (16-byte) UUID value.
MrBedfordVan 0:b9164b348919 127 * @param order
MrBedfordVan 0:b9164b348919 128 * The bit order of the UUID, MSB by default.
MrBedfordVan 0:b9164b348919 129 */
MrBedfordVan 0:b9164b348919 130 UUID(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
MrBedfordVan 0:b9164b348919 131 setupLong(longUUID, order);
MrBedfordVan 0:b9164b348919 132 }
MrBedfordVan 0:b9164b348919 133
MrBedfordVan 0:b9164b348919 134 /**
MrBedfordVan 0:b9164b348919 135 * Creates a new 16-bit UUID.
MrBedfordVan 0:b9164b348919 136 *
MrBedfordVan 0:b9164b348919 137 * @note The UUID is a unique 16-bit (2 byte) ID used to identify
MrBedfordVan 0:b9164b348919 138 * different service or characteristics on the BLE device.
MrBedfordVan 0:b9164b348919 139 *
MrBedfordVan 0:b9164b348919 140 * For efficiency, and because 16 bytes would take a large chunk of the
MrBedfordVan 0:b9164b348919 141 * 27-byte data payload length of the Link Layer, the BLE specification adds
MrBedfordVan 0:b9164b348919 142 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened
MrBedfordVan 0:b9164b348919 143 * formats can be used only with UUIDs that are defined in the Bluetooth
MrBedfordVan 0:b9164b348919 144 * specification (listed by the Bluetooth SIG as standard
MrBedfordVan 0:b9164b348919 145 * Bluetooth UUIDs).
MrBedfordVan 0:b9164b348919 146 *
MrBedfordVan 0:b9164b348919 147 * To reconstruct the full 128-bit UUID from the shortened version, insert
MrBedfordVan 0:b9164b348919 148 * the 16-bit short value (indicated by xxxx, including leading zeros) into
MrBedfordVan 0:b9164b348919 149 * the Bluetooth Base UUID:
MrBedfordVan 0:b9164b348919 150 *
MrBedfordVan 0:b9164b348919 151 * 0000xxxx-0000-1000-8000-00805F9B34FB
MrBedfordVan 0:b9164b348919 152 *
MrBedfordVan 0:b9164b348919 153 * @note Shortening is not available for UUIDs that are not derived from the
MrBedfordVan 0:b9164b348919 154 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called
MrBedfordVan 0:b9164b348919 155 * vendor-specific UUIDs. In these cases, you'll need to use the full
MrBedfordVan 0:b9164b348919 156 * 128-bit UUID value at all times.
MrBedfordVan 0:b9164b348919 157 *
MrBedfordVan 0:b9164b348919 158 * @note We don't yet support 32-bit shortened UUIDs.
MrBedfordVan 0:b9164b348919 159 */
MrBedfordVan 0:b9164b348919 160 UUID(ShortUUIDBytes_t _shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(_shortUUID) {
MrBedfordVan 0:b9164b348919 161 /* Empty */
MrBedfordVan 0:b9164b348919 162 }
MrBedfordVan 0:b9164b348919 163
MrBedfordVan 0:b9164b348919 164 UUID(const UUID &source) {
MrBedfordVan 0:b9164b348919 165 type = source.type;
MrBedfordVan 0:b9164b348919 166 shortUUID = source.shortUUID;
MrBedfordVan 0:b9164b348919 167 memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID);
MrBedfordVan 0:b9164b348919 168 }
MrBedfordVan 0:b9164b348919 169
MrBedfordVan 0:b9164b348919 170 UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) {
MrBedfordVan 0:b9164b348919 171 /* empty */
MrBedfordVan 0:b9164b348919 172 }
MrBedfordVan 0:b9164b348919 173
MrBedfordVan 0:b9164b348919 174 /**
MrBedfordVan 0:b9164b348919 175 * Fill in a 128-bit UUID; this is useful when the UUID isn't known at the time of the object construction.
MrBedfordVan 0:b9164b348919 176 */
MrBedfordVan 0:b9164b348919 177 void setupLong(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) {
MrBedfordVan 0:b9164b348919 178 type = UUID_TYPE_LONG;
MrBedfordVan 0:b9164b348919 179 if (order == UUID::MSB) {
MrBedfordVan 0:b9164b348919 180 // Switch endian. Input is big-endian, internal representation is little endian.
MrBedfordVan 0:b9164b348919 181 std::reverse_copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
MrBedfordVan 0:b9164b348919 182 } else {
MrBedfordVan 0:b9164b348919 183 std::copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
MrBedfordVan 0:b9164b348919 184 }
MrBedfordVan 0:b9164b348919 185 shortUUID = (uint16_t)((baseUUID[13] << 8) | (baseUUID[12]));
MrBedfordVan 0:b9164b348919 186 }
MrBedfordVan 0:b9164b348919 187
MrBedfordVan 0:b9164b348919 188 public:
MrBedfordVan 0:b9164b348919 189 UUID_Type_t shortOrLong(void) const {return type; }
MrBedfordVan 0:b9164b348919 190 const uint8_t *getBaseUUID(void) const {
MrBedfordVan 0:b9164b348919 191 if (type == UUID_TYPE_SHORT) {
MrBedfordVan 0:b9164b348919 192 return (const uint8_t*)&shortUUID;
MrBedfordVan 0:b9164b348919 193 } else {
MrBedfordVan 0:b9164b348919 194 return baseUUID;
MrBedfordVan 0:b9164b348919 195 }
MrBedfordVan 0:b9164b348919 196 }
MrBedfordVan 0:b9164b348919 197
MrBedfordVan 0:b9164b348919 198 ShortUUIDBytes_t getShortUUID(void) const {return shortUUID;}
MrBedfordVan 0:b9164b348919 199 uint8_t getLen(void) const {
MrBedfordVan 0:b9164b348919 200 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
MrBedfordVan 0:b9164b348919 201 }
MrBedfordVan 0:b9164b348919 202
MrBedfordVan 0:b9164b348919 203 bool operator== (const UUID &other) const {
MrBedfordVan 0:b9164b348919 204 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
MrBedfordVan 0:b9164b348919 205 (this->shortUUID == other.shortUUID)) {
MrBedfordVan 0:b9164b348919 206 return true;
MrBedfordVan 0:b9164b348919 207 }
MrBedfordVan 0:b9164b348919 208
MrBedfordVan 0:b9164b348919 209 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) &&
MrBedfordVan 0:b9164b348919 210 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
MrBedfordVan 0:b9164b348919 211 return true;
MrBedfordVan 0:b9164b348919 212 }
MrBedfordVan 0:b9164b348919 213
MrBedfordVan 0:b9164b348919 214 return false;
MrBedfordVan 0:b9164b348919 215 }
MrBedfordVan 0:b9164b348919 216
MrBedfordVan 0:b9164b348919 217 bool operator!= (const UUID &other) const {
MrBedfordVan 0:b9164b348919 218 return !(*this == other);
MrBedfordVan 0:b9164b348919 219 }
MrBedfordVan 0:b9164b348919 220
MrBedfordVan 0:b9164b348919 221 private:
MrBedfordVan 0:b9164b348919 222 UUID_Type_t type; // UUID_TYPE_SHORT or UUID_TYPE_LONG
MrBedfordVan 0:b9164b348919 223 LongUUIDBytes_t baseUUID; // The long UUID
MrBedfordVan 0:b9164b348919 224 ShortUUIDBytes_t shortUUID; // 16 bit UUID
MrBedfordVan 0:b9164b348919 225 };
MrBedfordVan 0:b9164b348919 226
MrBedfordVan 0:b9164b348919 227 #endif // ifndef __UUID_H__