Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 /* mbed Microcontroller Library
switches 0:0e018d759a2a 2 * Copyright (c) 2006-2013 ARM Limited
switches 0:0e018d759a2a 3 *
switches 0:0e018d759a2a 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 5 * you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 6 * You may obtain a copy of the License at
switches 0:0e018d759a2a 7 *
switches 0:0e018d759a2a 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 9 *
switches 0:0e018d759a2a 10 * Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 13 * See the License for the specific language governing permissions and
switches 0:0e018d759a2a 14 * limitations under the License.
switches 0:0e018d759a2a 15 */
switches 0:0e018d759a2a 16
switches 0:0e018d759a2a 17 #ifndef __UUID_H__
switches 0:0e018d759a2a 18 #define __UUID_H__
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20 #include <stdint.h>
switches 0:0e018d759a2a 21 #include <string.h>
switches 0:0e018d759a2a 22 #include <algorithm>
switches 0:0e018d759a2a 23
switches 0:0e018d759a2a 24 #include "blecommon.h"
switches 0:0e018d759a2a 25
switches 0:0e018d759a2a 26 /**
switches 0:0e018d759a2a 27 * A trivial converter for single hexadecimal character to an unsigned integer.
switches 0:0e018d759a2a 28 *
switches 0:0e018d759a2a 29 * @param c
switches 0:0e018d759a2a 30 * Hexadecimal character.
switches 0:0e018d759a2a 31 *
switches 0:0e018d759a2a 32 * @return The corresponding value as unsigned integer.
switches 0:0e018d759a2a 33 */
switches 0:0e018d759a2a 34 static uint8_t char2int(char c) {
switches 0:0e018d759a2a 35 if ((c >= '0') && (c <= '9')) {
switches 0:0e018d759a2a 36 return c - '0';
switches 0:0e018d759a2a 37 } else if ((c >= 'a') && (c <= 'f')) {
switches 0:0e018d759a2a 38 return c - 'a' + 10;
switches 0:0e018d759a2a 39 } else if ((c >= 'A') && (c <= 'F')) {
switches 0:0e018d759a2a 40 return c - 'A' + 10;
switches 0:0e018d759a2a 41 } else {
switches 0:0e018d759a2a 42 return 0;
switches 0:0e018d759a2a 43 }
switches 0:0e018d759a2a 44 }
switches 0:0e018d759a2a 45
switches 0:0e018d759a2a 46 /**
switches 0:0e018d759a2a 47 * An instance of this class represents a Universally Unique Identifier (UUID)
switches 0:0e018d759a2a 48 * in the BLE API.
switches 0:0e018d759a2a 49 */
switches 0:0e018d759a2a 50 class UUID {
switches 0:0e018d759a2a 51 public:
switches 0:0e018d759a2a 52 /**
switches 0:0e018d759a2a 53 * Enumeration of the possible types of UUIDs in BLE with regards to length.
switches 0:0e018d759a2a 54 */
switches 0:0e018d759a2a 55 enum UUID_Type_t {
switches 0:0e018d759a2a 56 UUID_TYPE_SHORT = 0, /**< Short 16-bit UUID. */
switches 0:0e018d759a2a 57 UUID_TYPE_LONG = 1 /**< Full 128-bit UUID. */
switches 0:0e018d759a2a 58 };
switches 0:0e018d759a2a 59
switches 0:0e018d759a2a 60 /**
switches 0:0e018d759a2a 61 * Enumeration to specify byte ordering of the long version of the UUID.
switches 0:0e018d759a2a 62 */
switches 0:0e018d759a2a 63 typedef enum {
switches 0:0e018d759a2a 64 MSB, /**< Most-significant byte first (at the smallest address) */
switches 0:0e018d759a2a 65 LSB /**< least-significant byte first (at the smallest address) */
switches 0:0e018d759a2a 66 } ByteOrder_t;
switches 0:0e018d759a2a 67
switches 0:0e018d759a2a 68 /**
switches 0:0e018d759a2a 69 * Type for a 16-bit UUID.
switches 0:0e018d759a2a 70 */
switches 0:0e018d759a2a 71 typedef uint16_t ShortUUIDBytes_t;
switches 0:0e018d759a2a 72
switches 0:0e018d759a2a 73 /**
switches 0:0e018d759a2a 74 * Length of a long UUID in bytes.
switches 0:0e018d759a2a 75 */
switches 0:0e018d759a2a 76 static const unsigned LENGTH_OF_LONG_UUID = 16;
switches 0:0e018d759a2a 77 /**
switches 0:0e018d759a2a 78 * Type for a 128-bit UUID.
switches 0:0e018d759a2a 79 */
switches 0:0e018d759a2a 80 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
switches 0:0e018d759a2a 81
switches 0:0e018d759a2a 82 /**
switches 0:0e018d759a2a 83 * Maximum length of a string representation of a UUID not including the
switches 0:0e018d759a2a 84 * null termination ('\0'): two characters per
switches 0:0e018d759a2a 85 * byte plus four '-' characters.
switches 0:0e018d759a2a 86 */
switches 0:0e018d759a2a 87 static const unsigned MAX_UUID_STRING_LENGTH = LENGTH_OF_LONG_UUID * 2 + 4;
switches 0:0e018d759a2a 88
switches 0:0e018d759a2a 89 public:
switches 0:0e018d759a2a 90
switches 0:0e018d759a2a 91 /**
switches 0:0e018d759a2a 92 * Creates a new 128-bit UUID.
switches 0:0e018d759a2a 93 *
switches 0:0e018d759a2a 94 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
switches 0:0e018d759a2a 95 * different service or characteristics on the BLE device.
switches 0:0e018d759a2a 96 *
switches 0:0e018d759a2a 97 * @param stringUUID
switches 0:0e018d759a2a 98 * The 128-bit (16-byte) UUID as a human readable const-string.
switches 0:0e018d759a2a 99 * Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
switches 0:0e018d759a2a 100 * Upper and lower case supported. Hyphens are optional, but only
switches 0:0e018d759a2a 101 * upto four of them. The UUID is stored internally as a 16 byte
switches 0:0e018d759a2a 102 * array, LSB (little endian), which is opposite from the string.
switches 0:0e018d759a2a 103 */
switches 0:0e018d759a2a 104 UUID(const char* stringUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
switches 0:0e018d759a2a 105 bool nibble = false;
switches 0:0e018d759a2a 106 uint8_t byte = 0;
switches 0:0e018d759a2a 107 size_t baseIndex = 0;
switches 0:0e018d759a2a 108 uint8_t tempUUID[LENGTH_OF_LONG_UUID];
switches 0:0e018d759a2a 109
switches 0:0e018d759a2a 110 /*
switches 0:0e018d759a2a 111 * Iterate through string, abort if NULL is encountered prematurely.
switches 0:0e018d759a2a 112 * Ignore upto four hyphens.
switches 0:0e018d759a2a 113 */
switches 0:0e018d759a2a 114 for (size_t index = 0; (index < MAX_UUID_STRING_LENGTH) && (baseIndex < LENGTH_OF_LONG_UUID); index++) {
switches 0:0e018d759a2a 115 if (stringUUID[index] == '\0') {
switches 0:0e018d759a2a 116 /* Error abort */
switches 0:0e018d759a2a 117 break;
switches 0:0e018d759a2a 118 } else if (stringUUID[index] == '-') {
switches 0:0e018d759a2a 119 /* Ignore hyphen */
switches 0:0e018d759a2a 120 continue;
switches 0:0e018d759a2a 121 } else if (nibble) {
switches 0:0e018d759a2a 122 /* Got second nibble */
switches 0:0e018d759a2a 123 byte |= char2int(stringUUID[index]);
switches 0:0e018d759a2a 124 nibble = false;
switches 0:0e018d759a2a 125
switches 0:0e018d759a2a 126 /* Store copy */
switches 0:0e018d759a2a 127 tempUUID[baseIndex++] = byte;
switches 0:0e018d759a2a 128 } else {
switches 0:0e018d759a2a 129 /* Got first nibble */
switches 0:0e018d759a2a 130 byte = char2int(stringUUID[index]) << 4;
switches 0:0e018d759a2a 131 nibble = true;
switches 0:0e018d759a2a 132 }
switches 0:0e018d759a2a 133 }
switches 0:0e018d759a2a 134
switches 0:0e018d759a2a 135 /* Populate internal variables if string was successfully parsed */
switches 0:0e018d759a2a 136 if (baseIndex == LENGTH_OF_LONG_UUID) {
switches 0:0e018d759a2a 137 setupLong(tempUUID, UUID::MSB);
switches 0:0e018d759a2a 138 } else {
switches 0:0e018d759a2a 139 const uint8_t sig[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
switches 0:0e018d759a2a 140 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
switches 0:0e018d759a2a 141 setupLong(sig, UUID::MSB);
switches 0:0e018d759a2a 142 }
switches 0:0e018d759a2a 143 }
switches 0:0e018d759a2a 144
switches 0:0e018d759a2a 145 /**
switches 0:0e018d759a2a 146 * Creates a new 128-bit UUID.
switches 0:0e018d759a2a 147 *
switches 0:0e018d759a2a 148 * @param[in] longUUID
switches 0:0e018d759a2a 149 * The 128-bit (16-byte) UUID value.
switches 0:0e018d759a2a 150 * @param[in] order
switches 0:0e018d759a2a 151 * The bit order of the UUID, MSB by default.
switches 0:0e018d759a2a 152 *
switches 0:0e018d759a2a 153 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
switches 0:0e018d759a2a 154 * different service or characteristics on the BLE device.
switches 0:0e018d759a2a 155 */
switches 0:0e018d759a2a 156 UUID(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
switches 0:0e018d759a2a 157 setupLong(longUUID, order);
switches 0:0e018d759a2a 158 }
switches 0:0e018d759a2a 159
switches 0:0e018d759a2a 160 /**
switches 0:0e018d759a2a 161 * Creates a new 16-bit UUID.
switches 0:0e018d759a2a 162 *
switches 0:0e018d759a2a 163 * For efficiency, and because 16 bytes would take a large chunk of the
switches 0:0e018d759a2a 164 * 27-byte data payload length of the Link Layer, the BLE specification adds
switches 0:0e018d759a2a 165 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened
switches 0:0e018d759a2a 166 * formats can be used only with UUIDs that are defined in the Bluetooth
switches 0:0e018d759a2a 167 * specification (listed by the Bluetooth SIG as standard
switches 0:0e018d759a2a 168 * Bluetooth UUIDs).
switches 0:0e018d759a2a 169 *
switches 0:0e018d759a2a 170 * To reconstruct the full 128-bit UUID from the shortened version, insert
switches 0:0e018d759a2a 171 * the 16-bit short value (indicated by xxxx, including leading zeros) into
switches 0:0e018d759a2a 172 * the Bluetooth Base UUID:
switches 0:0e018d759a2a 173 *
switches 0:0e018d759a2a 174 * 0000xxxx-0000-1000-8000-00805F9B34FB
switches 0:0e018d759a2a 175 *
switches 0:0e018d759a2a 176 * @param[in] _shortUUID
switches 0:0e018d759a2a 177 * The short UUID value.
switches 0:0e018d759a2a 178 *
switches 0:0e018d759a2a 179 * @note Shortening is not available for UUIDs that are not derived from the
switches 0:0e018d759a2a 180 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called
switches 0:0e018d759a2a 181 * vendor-specific UUIDs. In these cases, you’ll need to use the full
switches 0:0e018d759a2a 182 * 128-bit UUID value at all times.
switches 0:0e018d759a2a 183 *
switches 0:0e018d759a2a 184 * @note The UUID is a unique 16-bit (2 byte) ID used to identify
switches 0:0e018d759a2a 185 * different service or characteristics on the BLE device.
switches 0:0e018d759a2a 186 *
switches 0:0e018d759a2a 187 * @note We do not yet support 32-bit shortened UUIDs.
switches 0:0e018d759a2a 188 */
switches 0:0e018d759a2a 189 UUID(ShortUUIDBytes_t _shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(_shortUUID) {
switches 0:0e018d759a2a 190 /* Empty */
switches 0:0e018d759a2a 191 }
switches 0:0e018d759a2a 192
switches 0:0e018d759a2a 193 /**
switches 0:0e018d759a2a 194 * Copy constructor.
switches 0:0e018d759a2a 195 *
switches 0:0e018d759a2a 196 * @param[in] source
switches 0:0e018d759a2a 197 * The UUID to copy.
switches 0:0e018d759a2a 198 */
switches 0:0e018d759a2a 199 UUID(const UUID &source) {
switches 0:0e018d759a2a 200 type = source.type;
switches 0:0e018d759a2a 201 shortUUID = source.shortUUID;
switches 0:0e018d759a2a 202 memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID);
switches 0:0e018d759a2a 203 }
switches 0:0e018d759a2a 204
switches 0:0e018d759a2a 205 /**
switches 0:0e018d759a2a 206 * The empty constructor.
switches 0:0e018d759a2a 207 *
switches 0:0e018d759a2a 208 * @note The type of the resulting UUID instance is UUID_TYPE_SHORT and the
switches 0:0e018d759a2a 209 * value BLE_UUID_UNKNOWN.
switches 0:0e018d759a2a 210 */
switches 0:0e018d759a2a 211 UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) {
switches 0:0e018d759a2a 212 /* empty */
switches 0:0e018d759a2a 213 }
switches 0:0e018d759a2a 214
switches 0:0e018d759a2a 215 /**
switches 0:0e018d759a2a 216 * Fill in a 128-bit UUID; this is useful when the UUID is not known at the
switches 0:0e018d759a2a 217 * time of the object construction.
switches 0:0e018d759a2a 218 *
switches 0:0e018d759a2a 219 * @param[in] longUUID
switches 0:0e018d759a2a 220 * The UUID value to copy.
switches 0:0e018d759a2a 221 * @param[in] order
switches 0:0e018d759a2a 222 * The byte ordering of the UUID at @p longUUID.
switches 0:0e018d759a2a 223 */
switches 0:0e018d759a2a 224 void setupLong(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) {
switches 0:0e018d759a2a 225 type = UUID_TYPE_LONG;
switches 0:0e018d759a2a 226 if (order == UUID::MSB) {
switches 0:0e018d759a2a 227 /*
switches 0:0e018d759a2a 228 * Switch endian. Input is big-endian, internal representation
switches 0:0e018d759a2a 229 * is little endian.
switches 0:0e018d759a2a 230 */
switches 0:0e018d759a2a 231 std::reverse_copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
switches 0:0e018d759a2a 232 } else {
switches 0:0e018d759a2a 233 std::copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
switches 0:0e018d759a2a 234 }
switches 0:0e018d759a2a 235 shortUUID = (uint16_t)((baseUUID[13] << 8) | (baseUUID[12]));
switches 0:0e018d759a2a 236 }
switches 0:0e018d759a2a 237
switches 0:0e018d759a2a 238 public:
switches 0:0e018d759a2a 239 /**
switches 0:0e018d759a2a 240 * Check whether this UUID is short or long.
switches 0:0e018d759a2a 241 *
switches 0:0e018d759a2a 242 * @return UUID_TYPE_SHORT if the UUID is short, UUID_TYPE_LONG otherwise.
switches 0:0e018d759a2a 243 */
switches 0:0e018d759a2a 244 UUID_Type_t shortOrLong(void) const {
switches 0:0e018d759a2a 245 return type;
switches 0:0e018d759a2a 246 }
switches 0:0e018d759a2a 247
switches 0:0e018d759a2a 248 /**
switches 0:0e018d759a2a 249 * Get a pointer to the UUID value based on the current UUID type.
switches 0:0e018d759a2a 250 *
switches 0:0e018d759a2a 251 * @return A pointer to the short UUID if the type is set to
switches 0:0e018d759a2a 252 * UUID_TYPE_SHORT. Otherwise, a pointer to the long UUID if the
switches 0:0e018d759a2a 253 * type is set to UUID_TYPE_LONG.
switches 0:0e018d759a2a 254 */
switches 0:0e018d759a2a 255 const uint8_t *getBaseUUID(void) const {
switches 0:0e018d759a2a 256 if (type == UUID_TYPE_SHORT) {
switches 0:0e018d759a2a 257 return (const uint8_t*)&shortUUID;
switches 0:0e018d759a2a 258 } else {
switches 0:0e018d759a2a 259 return baseUUID;
switches 0:0e018d759a2a 260 }
switches 0:0e018d759a2a 261 }
switches 0:0e018d759a2a 262
switches 0:0e018d759a2a 263 /**
switches 0:0e018d759a2a 264 * Get the short UUID.
switches 0:0e018d759a2a 265 *
switches 0:0e018d759a2a 266 * @return The short UUID.
switches 0:0e018d759a2a 267 */
switches 0:0e018d759a2a 268 ShortUUIDBytes_t getShortUUID(void) const {
switches 0:0e018d759a2a 269 return shortUUID;
switches 0:0e018d759a2a 270 }
switches 0:0e018d759a2a 271
switches 0:0e018d759a2a 272 /**
switches 0:0e018d759a2a 273 * Get the length (in bytes) of the UUID based on its type.
switches 0:0e018d759a2a 274 *
switches 0:0e018d759a2a 275 * @retval sizeof(ShortUUIDBytes_t) if the UUID type is UUID_TYPE_SHORT.
switches 0:0e018d759a2a 276 * @retval LENGTH_OF_LONG_UUID if the UUID type is UUID_TYPE_LONG.
switches 0:0e018d759a2a 277 */
switches 0:0e018d759a2a 278 uint8_t getLen(void) const {
switches 0:0e018d759a2a 279 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
switches 0:0e018d759a2a 280 }
switches 0:0e018d759a2a 281
switches 0:0e018d759a2a 282 /**
switches 0:0e018d759a2a 283 * Overload == operator to enable UUID comparisons.
switches 0:0e018d759a2a 284 *
switches 0:0e018d759a2a 285 * @param[in] other
switches 0:0e018d759a2a 286 * The other UUID in the comparison.
switches 0:0e018d759a2a 287 *
switches 0:0e018d759a2a 288 * @return true if this == @p other, false otherwise.
switches 0:0e018d759a2a 289 */
switches 0:0e018d759a2a 290 bool operator== (const UUID &other) const {
switches 0:0e018d759a2a 291 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
switches 0:0e018d759a2a 292 (this->shortUUID == other.shortUUID)) {
switches 0:0e018d759a2a 293 return true;
switches 0:0e018d759a2a 294 }
switches 0:0e018d759a2a 295
switches 0:0e018d759a2a 296 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) &&
switches 0:0e018d759a2a 297 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
switches 0:0e018d759a2a 298 return true;
switches 0:0e018d759a2a 299 }
switches 0:0e018d759a2a 300
switches 0:0e018d759a2a 301 return false;
switches 0:0e018d759a2a 302 }
switches 0:0e018d759a2a 303
switches 0:0e018d759a2a 304 /**
switches 0:0e018d759a2a 305 * Overload != operator to enable UUID comparisons.
switches 0:0e018d759a2a 306 *
switches 0:0e018d759a2a 307 * @param[in] other
switches 0:0e018d759a2a 308 * The other UUID in the comparison.
switches 0:0e018d759a2a 309 *
switches 0:0e018d759a2a 310 * @return true if this != @p other, false otherwise.
switches 0:0e018d759a2a 311 */
switches 0:0e018d759a2a 312 bool operator!= (const UUID &other) const {
switches 0:0e018d759a2a 313 return !(*this == other);
switches 0:0e018d759a2a 314 }
switches 0:0e018d759a2a 315
switches 0:0e018d759a2a 316 private:
switches 0:0e018d759a2a 317 /**
switches 0:0e018d759a2a 318 * The UUID type. Refer to UUID_Type_t.
switches 0:0e018d759a2a 319 */
switches 0:0e018d759a2a 320 UUID_Type_t type;
switches 0:0e018d759a2a 321 /**
switches 0:0e018d759a2a 322 * The long UUID value.
switches 0:0e018d759a2a 323 */
switches 0:0e018d759a2a 324 LongUUIDBytes_t baseUUID;
switches 0:0e018d759a2a 325 /**
switches 0:0e018d759a2a 326 * The short UUID value.
switches 0:0e018d759a2a 327 */
switches 0:0e018d759a2a 328 ShortUUIDBytes_t shortUUID;
switches 0:0e018d759a2a 329 };
switches 0:0e018d759a2a 330
switches 0:0e018d759a2a 331 #endif // ifndef __UUID_H__