Updated

Fork of BLE_API by Bluetooth Low Energy

Committer:
vbahl2
Date:
Tue May 09 03:03:23 2017 +0000
Revision:
1210:1f56dc951637
j

Who changed what in which revision?

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