Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/features/FEATURE_BLE/ble/UUID.h@0:4beb2ea291ec, 2020-03-16 (annotated)
- Committer:
- boro
- Date:
- Mon Mar 16 13:12:31 2020 +0000
- Revision:
- 0:4beb2ea291ec
a
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| boro | 0:4beb2ea291ec | 1 | /* mbed Microcontroller Library |
| boro | 0:4beb2ea291ec | 2 | * Copyright (c) 2006-2013 ARM Limited |
| boro | 0:4beb2ea291ec | 3 | * |
| boro | 0:4beb2ea291ec | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| boro | 0:4beb2ea291ec | 5 | * you may not use this file except in compliance with the License. |
| boro | 0:4beb2ea291ec | 6 | * You may obtain a copy of the License at |
| boro | 0:4beb2ea291ec | 7 | * |
| boro | 0:4beb2ea291ec | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| boro | 0:4beb2ea291ec | 9 | * |
| boro | 0:4beb2ea291ec | 10 | * Unless required by applicable law or agreed to in writing, software |
| boro | 0:4beb2ea291ec | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| boro | 0:4beb2ea291ec | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| boro | 0:4beb2ea291ec | 13 | * See the License for the specific language governing permissions and |
| boro | 0:4beb2ea291ec | 14 | * limitations under the License. |
| boro | 0:4beb2ea291ec | 15 | */ |
| boro | 0:4beb2ea291ec | 16 | |
| boro | 0:4beb2ea291ec | 17 | #ifndef MBED_UUID_H__ |
| boro | 0:4beb2ea291ec | 18 | #define MBED_UUID_H__ |
| boro | 0:4beb2ea291ec | 19 | |
| boro | 0:4beb2ea291ec | 20 | #include <stdint.h> |
| boro | 0:4beb2ea291ec | 21 | #include <string.h> |
| boro | 0:4beb2ea291ec | 22 | #include <algorithm> |
| boro | 0:4beb2ea291ec | 23 | |
| boro | 0:4beb2ea291ec | 24 | #include "blecommon.h" |
| boro | 0:4beb2ea291ec | 25 | |
| boro | 0:4beb2ea291ec | 26 | /** |
| boro | 0:4beb2ea291ec | 27 | * @file |
| boro | 0:4beb2ea291ec | 28 | * @addtogroup ble |
| boro | 0:4beb2ea291ec | 29 | * @{ |
| boro | 0:4beb2ea291ec | 30 | * @addtogroup common |
| boro | 0:4beb2ea291ec | 31 | * @{ |
| boro | 0:4beb2ea291ec | 32 | */ |
| boro | 0:4beb2ea291ec | 33 | |
| boro | 0:4beb2ea291ec | 34 | /** |
| boro | 0:4beb2ea291ec | 35 | * Convert a character containing an hexadecimal digit into an unsigned integer. |
| boro | 0:4beb2ea291ec | 36 | * |
| boro | 0:4beb2ea291ec | 37 | * @param[in] c Hexadecimal digit in a character representation. |
| boro | 0:4beb2ea291ec | 38 | * |
| boro | 0:4beb2ea291ec | 39 | * @return The corresponding value as unsigned integer. |
| boro | 0:4beb2ea291ec | 40 | */ |
| boro | 0:4beb2ea291ec | 41 | static uint8_t char2int(char c) |
| boro | 0:4beb2ea291ec | 42 | { |
| boro | 0:4beb2ea291ec | 43 | if ((c >= '0') && (c <= '9')) { |
| boro | 0:4beb2ea291ec | 44 | return c - '0'; |
| boro | 0:4beb2ea291ec | 45 | } else if ((c >= 'a') && (c <= 'f')) { |
| boro | 0:4beb2ea291ec | 46 | return c - 'a' + 10; |
| boro | 0:4beb2ea291ec | 47 | } else if ((c >= 'A') && (c <= 'F')) { |
| boro | 0:4beb2ea291ec | 48 | return c - 'A' + 10; |
| boro | 0:4beb2ea291ec | 49 | } else { |
| boro | 0:4beb2ea291ec | 50 | return 0; |
| boro | 0:4beb2ea291ec | 51 | } |
| boro | 0:4beb2ea291ec | 52 | } |
| boro | 0:4beb2ea291ec | 53 | |
| boro | 0:4beb2ea291ec | 54 | /** |
| boro | 0:4beb2ea291ec | 55 | * Representation of a Universally Unique Identifier (UUID). |
| boro | 0:4beb2ea291ec | 56 | * |
| boro | 0:4beb2ea291ec | 57 | * UUIDs are 128-bit wide numbers used to identify data type and elements in |
| boro | 0:4beb2ea291ec | 58 | * many layers of the Bluetooth specification. |
| boro | 0:4beb2ea291ec | 59 | * |
| boro | 0:4beb2ea291ec | 60 | * Two representations of UUIDS exist: |
| boro | 0:4beb2ea291ec | 61 | * - 16-bit UUIDs: Shortened representation of the 128 bit UUID |
| boro | 0:4beb2ea291ec | 62 | * 0000xxxx-0000-1000-8000-00805F9B34FB where xxxx is the 16 bit UUID. |
| boro | 0:4beb2ea291ec | 63 | * Values of those UUIDs are defined by the Bluetooth body. The short |
| boro | 0:4beb2ea291ec | 64 | * representation saves bandwidth during protocol transactions. |
| boro | 0:4beb2ea291ec | 65 | * - 128-bit UUIDs: Complete representation of a UUID. They are commonly |
| boro | 0:4beb2ea291ec | 66 | * used for user defined UUID. |
| boro | 0:4beb2ea291ec | 67 | * |
| boro | 0:4beb2ea291ec | 68 | * This class acts as an adapter over these two kinds of UUIDs to allow |
| boro | 0:4beb2ea291ec | 69 | * indiscriminate use of both forms in Mbed BLE APIs. |
| boro | 0:4beb2ea291ec | 70 | * |
| boro | 0:4beb2ea291ec | 71 | * @note 32-bit UUID representation is not supported currently. |
| boro | 0:4beb2ea291ec | 72 | */ |
| boro | 0:4beb2ea291ec | 73 | class UUID { |
| boro | 0:4beb2ea291ec | 74 | public: |
| boro | 0:4beb2ea291ec | 75 | |
| boro | 0:4beb2ea291ec | 76 | /** |
| boro | 0:4beb2ea291ec | 77 | * Enumeration of the types of UUIDs. |
| boro | 0:4beb2ea291ec | 78 | */ |
| boro | 0:4beb2ea291ec | 79 | enum UUID_Type_t { |
| boro | 0:4beb2ea291ec | 80 | /** |
| boro | 0:4beb2ea291ec | 81 | * 16-bit wide UUID representation. |
| boro | 0:4beb2ea291ec | 82 | */ |
| boro | 0:4beb2ea291ec | 83 | UUID_TYPE_SHORT = 0, |
| boro | 0:4beb2ea291ec | 84 | |
| boro | 0:4beb2ea291ec | 85 | /** |
| boro | 0:4beb2ea291ec | 86 | * 128-bit wide UUID representation. |
| boro | 0:4beb2ea291ec | 87 | */ |
| boro | 0:4beb2ea291ec | 88 | UUID_TYPE_LONG = 1 |
| boro | 0:4beb2ea291ec | 89 | }; |
| boro | 0:4beb2ea291ec | 90 | |
| boro | 0:4beb2ea291ec | 91 | /** |
| boro | 0:4beb2ea291ec | 92 | * Enumeration of byte ordering. |
| boro | 0:4beb2ea291ec | 93 | * |
| boro | 0:4beb2ea291ec | 94 | * It is used to construct 128-byte UUIDs. |
| boro | 0:4beb2ea291ec | 95 | */ |
| boro | 0:4beb2ea291ec | 96 | typedef enum { |
| boro | 0:4beb2ea291ec | 97 | /** |
| boro | 0:4beb2ea291ec | 98 | * Most significant byte first (at the smallest address). |
| boro | 0:4beb2ea291ec | 99 | */ |
| boro | 0:4beb2ea291ec | 100 | MSB, |
| boro | 0:4beb2ea291ec | 101 | |
| boro | 0:4beb2ea291ec | 102 | /** |
| boro | 0:4beb2ea291ec | 103 | * Least significant byte first (at the smallest address). |
| boro | 0:4beb2ea291ec | 104 | */ |
| boro | 0:4beb2ea291ec | 105 | LSB |
| boro | 0:4beb2ea291ec | 106 | } ByteOrder_t; |
| boro | 0:4beb2ea291ec | 107 | |
| boro | 0:4beb2ea291ec | 108 | /** |
| boro | 0:4beb2ea291ec | 109 | * Type for a 16-bit UUID. |
| boro | 0:4beb2ea291ec | 110 | */ |
| boro | 0:4beb2ea291ec | 111 | typedef uint16_t ShortUUIDBytes_t; |
| boro | 0:4beb2ea291ec | 112 | |
| boro | 0:4beb2ea291ec | 113 | /** |
| boro | 0:4beb2ea291ec | 114 | * Length in bytes of a long UUID. |
| boro | 0:4beb2ea291ec | 115 | */ |
| boro | 0:4beb2ea291ec | 116 | static const unsigned LENGTH_OF_LONG_UUID = 16; |
| boro | 0:4beb2ea291ec | 117 | |
| boro | 0:4beb2ea291ec | 118 | /** |
| boro | 0:4beb2ea291ec | 119 | * Type for a 128-bit UUID. |
| boro | 0:4beb2ea291ec | 120 | */ |
| boro | 0:4beb2ea291ec | 121 | typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; |
| boro | 0:4beb2ea291ec | 122 | |
| boro | 0:4beb2ea291ec | 123 | /** |
| boro | 0:4beb2ea291ec | 124 | * Maximum length for the string representation of a UUID excluding the null |
| boro | 0:4beb2ea291ec | 125 | * terminator. |
| boro | 0:4beb2ea291ec | 126 | * |
| boro | 0:4beb2ea291ec | 127 | * The string is composed of two characters per byte plus four '-' |
| boro | 0:4beb2ea291ec | 128 | * characters. |
| boro | 0:4beb2ea291ec | 129 | */ |
| boro | 0:4beb2ea291ec | 130 | static const unsigned MAX_UUID_STRING_LENGTH = LENGTH_OF_LONG_UUID * 2 + 4; |
| boro | 0:4beb2ea291ec | 131 | |
| boro | 0:4beb2ea291ec | 132 | public: |
| boro | 0:4beb2ea291ec | 133 | |
| boro | 0:4beb2ea291ec | 134 | /** |
| boro | 0:4beb2ea291ec | 135 | * Construct a 128-bit UUID from a string. |
| boro | 0:4beb2ea291ec | 136 | * |
| boro | 0:4beb2ea291ec | 137 | * @param[in] stringUUID Human readable representation of the UUID following |
| boro | 0:4beb2ea291ec | 138 | * the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. |
| boro | 0:4beb2ea291ec | 139 | * |
| boro | 0:4beb2ea291ec | 140 | * @note Upper and lower case are supported. |
| boro | 0:4beb2ea291ec | 141 | * @note Hyphens are optional. The string must include at most four hyphens. |
| boro | 0:4beb2ea291ec | 142 | * |
| boro | 0:4beb2ea291ec | 143 | * @note Internally, the UUID is stored in the little endian order as a |
| boro | 0:4beb2ea291ec | 144 | * 16-byte array. |
| boro | 0:4beb2ea291ec | 145 | */ |
| boro | 0:4beb2ea291ec | 146 | UUID(const char* stringUUID) : |
| boro | 0:4beb2ea291ec | 147 | type(UUID_TYPE_LONG), |
| boro | 0:4beb2ea291ec | 148 | baseUUID(), |
| boro | 0:4beb2ea291ec | 149 | shortUUID(0) |
| boro | 0:4beb2ea291ec | 150 | { |
| boro | 0:4beb2ea291ec | 151 | bool nibble = false; |
| boro | 0:4beb2ea291ec | 152 | uint8_t byte = 0; |
| boro | 0:4beb2ea291ec | 153 | size_t baseIndex = 0; |
| boro | 0:4beb2ea291ec | 154 | uint8_t tempUUID[LENGTH_OF_LONG_UUID]; |
| boro | 0:4beb2ea291ec | 155 | |
| boro | 0:4beb2ea291ec | 156 | /* |
| boro | 0:4beb2ea291ec | 157 | * Iterate through string; abort if NULL is encountered prematurely. |
| boro | 0:4beb2ea291ec | 158 | * Ignore up to four hyphens. |
| boro | 0:4beb2ea291ec | 159 | */ |
| boro | 0:4beb2ea291ec | 160 | for (size_t index = 0; (index < MAX_UUID_STRING_LENGTH) && (baseIndex < LENGTH_OF_LONG_UUID); index++) { |
| boro | 0:4beb2ea291ec | 161 | if (stringUUID[index] == '\0') { |
| boro | 0:4beb2ea291ec | 162 | /* Error abort */ |
| boro | 0:4beb2ea291ec | 163 | break; |
| boro | 0:4beb2ea291ec | 164 | } else if (stringUUID[index] == '-') { |
| boro | 0:4beb2ea291ec | 165 | /* Ignore hyphen */ |
| boro | 0:4beb2ea291ec | 166 | continue; |
| boro | 0:4beb2ea291ec | 167 | } else if (nibble) { |
| boro | 0:4beb2ea291ec | 168 | /* Got second nibble */ |
| boro | 0:4beb2ea291ec | 169 | byte |= char2int(stringUUID[index]); |
| boro | 0:4beb2ea291ec | 170 | nibble = false; |
| boro | 0:4beb2ea291ec | 171 | |
| boro | 0:4beb2ea291ec | 172 | /* Store copy */ |
| boro | 0:4beb2ea291ec | 173 | tempUUID[baseIndex++] = byte; |
| boro | 0:4beb2ea291ec | 174 | } else { |
| boro | 0:4beb2ea291ec | 175 | /* Got first nibble */ |
| boro | 0:4beb2ea291ec | 176 | byte = char2int(stringUUID[index]) << 4; |
| boro | 0:4beb2ea291ec | 177 | nibble = true; |
| boro | 0:4beb2ea291ec | 178 | } |
| boro | 0:4beb2ea291ec | 179 | } |
| boro | 0:4beb2ea291ec | 180 | |
| boro | 0:4beb2ea291ec | 181 | /* Populate internal variables if string was successfully parsed */ |
| boro | 0:4beb2ea291ec | 182 | if (baseIndex == LENGTH_OF_LONG_UUID) { |
| boro | 0:4beb2ea291ec | 183 | setupLong(tempUUID, UUID::MSB); |
| boro | 0:4beb2ea291ec | 184 | } else { |
| boro | 0:4beb2ea291ec | 185 | const uint8_t sig[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, |
| boro | 0:4beb2ea291ec | 186 | 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; |
| boro | 0:4beb2ea291ec | 187 | setupLong(sig, UUID::MSB); |
| boro | 0:4beb2ea291ec | 188 | } |
| boro | 0:4beb2ea291ec | 189 | } |
| boro | 0:4beb2ea291ec | 190 | |
| boro | 0:4beb2ea291ec | 191 | /** |
| boro | 0:4beb2ea291ec | 192 | * Construct a new UUID from a 128-bit representation. |
| boro | 0:4beb2ea291ec | 193 | * |
| boro | 0:4beb2ea291ec | 194 | * @param[in] longUUID The 128-bit (16-byte) of the UUID value. |
| boro | 0:4beb2ea291ec | 195 | * @param[in] order Bytes order of @p longUUID. |
| boro | 0:4beb2ea291ec | 196 | */ |
| boro | 0:4beb2ea291ec | 197 | UUID(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) { |
| boro | 0:4beb2ea291ec | 198 | setupLong(longUUID, order); |
| boro | 0:4beb2ea291ec | 199 | } |
| boro | 0:4beb2ea291ec | 200 | |
| boro | 0:4beb2ea291ec | 201 | /** |
| boro | 0:4beb2ea291ec | 202 | * Creates a new 16-bit UUID. |
| boro | 0:4beb2ea291ec | 203 | * |
| boro | 0:4beb2ea291ec | 204 | * The Bluetooth standard body defines 16-bit wide UUIDs. They are the |
| boro | 0:4beb2ea291ec | 205 | * shortened version of the UUID 0000xxxx-0000-1000-8000-00805F9B34FB, where |
| boro | 0:4beb2ea291ec | 206 | * xxxx is the value of the 16-bit UUID. |
| boro | 0:4beb2ea291ec | 207 | * |
| boro | 0:4beb2ea291ec | 208 | * @important 16-bit UUIDs are not used in user defined data type or |
| boro | 0:4beb2ea291ec | 209 | * user defined element ID. |
| boro | 0:4beb2ea291ec | 210 | * |
| boro | 0:4beb2ea291ec | 211 | * @param[in] _shortUUID 16-bit part of the standard UUID. |
| boro | 0:4beb2ea291ec | 212 | * The short UUID value. |
| boro | 0:4beb2ea291ec | 213 | * |
| boro | 0:4beb2ea291ec | 214 | * @note User defined UUIDs are commonly named vendor-specific UUIDs across |
| boro | 0:4beb2ea291ec | 215 | * the Bluetooth literature. |
| boro | 0:4beb2ea291ec | 216 | */ |
| boro | 0:4beb2ea291ec | 217 | UUID(ShortUUIDBytes_t _shortUUID) : |
| boro | 0:4beb2ea291ec | 218 | type(UUID_TYPE_SHORT), |
| boro | 0:4beb2ea291ec | 219 | baseUUID(), |
| boro | 0:4beb2ea291ec | 220 | shortUUID(_shortUUID) { |
| boro | 0:4beb2ea291ec | 221 | } |
| boro | 0:4beb2ea291ec | 222 | |
| boro | 0:4beb2ea291ec | 223 | /** |
| boro | 0:4beb2ea291ec | 224 | * UUID copy constructor. |
| boro | 0:4beb2ea291ec | 225 | * |
| boro | 0:4beb2ea291ec | 226 | * @param[in] source The UUID to copy. |
| boro | 0:4beb2ea291ec | 227 | */ |
| boro | 0:4beb2ea291ec | 228 | UUID(const UUID &source) |
| boro | 0:4beb2ea291ec | 229 | { |
| boro | 0:4beb2ea291ec | 230 | type = source.type; |
| boro | 0:4beb2ea291ec | 231 | shortUUID = source.shortUUID; |
| boro | 0:4beb2ea291ec | 232 | memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID); |
| boro | 0:4beb2ea291ec | 233 | } |
| boro | 0:4beb2ea291ec | 234 | |
| boro | 0:4beb2ea291ec | 235 | /** |
| boro | 0:4beb2ea291ec | 236 | * Default constructor. |
| boro | 0:4beb2ea291ec | 237 | * |
| boro | 0:4beb2ea291ec | 238 | * Construct an invalid UUID. |
| boro | 0:4beb2ea291ec | 239 | * |
| boro | 0:4beb2ea291ec | 240 | * @post shortOrLong() returns the value UUID_TYPE_SHORT. |
| boro | 0:4beb2ea291ec | 241 | * @post getShortUUID() returns the value BLE_UUID_UNKNOWN. |
| boro | 0:4beb2ea291ec | 242 | */ |
| boro | 0:4beb2ea291ec | 243 | UUID(void) : |
| boro | 0:4beb2ea291ec | 244 | type(UUID_TYPE_SHORT), |
| boro | 0:4beb2ea291ec | 245 | shortUUID(BLE_UUID_UNKNOWN) { |
| boro | 0:4beb2ea291ec | 246 | } |
| boro | 0:4beb2ea291ec | 247 | |
| boro | 0:4beb2ea291ec | 248 | /** |
| boro | 0:4beb2ea291ec | 249 | * Replace existing value with a 128-bit UUID. |
| boro | 0:4beb2ea291ec | 250 | * |
| boro | 0:4beb2ea291ec | 251 | * @param[in] longUUID New 16-byte wide UUID value. |
| boro | 0:4beb2ea291ec | 252 | * @param[in] order Byte ordering of @p longUUID. |
| boro | 0:4beb2ea291ec | 253 | */ |
| boro | 0:4beb2ea291ec | 254 | void setupLong(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) |
| boro | 0:4beb2ea291ec | 255 | { |
| boro | 0:4beb2ea291ec | 256 | type = UUID_TYPE_LONG; |
| boro | 0:4beb2ea291ec | 257 | if (order == UUID::MSB) { |
| boro | 0:4beb2ea291ec | 258 | /* |
| boro | 0:4beb2ea291ec | 259 | * Switch endian. Input is big-endian, internal representation |
| boro | 0:4beb2ea291ec | 260 | * is little endian. |
| boro | 0:4beb2ea291ec | 261 | */ |
| boro | 0:4beb2ea291ec | 262 | std::reverse_copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID); |
| boro | 0:4beb2ea291ec | 263 | } else { |
| boro | 0:4beb2ea291ec | 264 | std::copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID); |
| boro | 0:4beb2ea291ec | 265 | } |
| boro | 0:4beb2ea291ec | 266 | shortUUID = (uint16_t)((baseUUID[13] << 8) | (baseUUID[12])); |
| boro | 0:4beb2ea291ec | 267 | } |
| boro | 0:4beb2ea291ec | 268 | |
| boro | 0:4beb2ea291ec | 269 | public: |
| boro | 0:4beb2ea291ec | 270 | /** |
| boro | 0:4beb2ea291ec | 271 | * Return the internal type of the UUID. |
| boro | 0:4beb2ea291ec | 272 | * |
| boro | 0:4beb2ea291ec | 273 | * @return UUID_TYPE_SHORT if the UUID is 16-bit wide. |
| boro | 0:4beb2ea291ec | 274 | * @return UUID_TYPE_LONG if the UUID is 128-bit wide. |
| boro | 0:4beb2ea291ec | 275 | */ |
| boro | 0:4beb2ea291ec | 276 | UUID_Type_t shortOrLong(void) const |
| boro | 0:4beb2ea291ec | 277 | { |
| boro | 0:4beb2ea291ec | 278 | return type; |
| boro | 0:4beb2ea291ec | 279 | } |
| boro | 0:4beb2ea291ec | 280 | |
| boro | 0:4beb2ea291ec | 281 | /** |
| boro | 0:4beb2ea291ec | 282 | * Get a pointer to the UUID value based on the current UUID type. |
| boro | 0:4beb2ea291ec | 283 | * |
| boro | 0:4beb2ea291ec | 284 | * @return A pointer to an uint16_t object if the UUID is 16 bits long. |
| boro | 0:4beb2ea291ec | 285 | * @return A pointer to an array of 16 bytes if the UUID is 128 bits long. |
| boro | 0:4beb2ea291ec | 286 | */ |
| boro | 0:4beb2ea291ec | 287 | const uint8_t *getBaseUUID(void) const |
| boro | 0:4beb2ea291ec | 288 | { |
| boro | 0:4beb2ea291ec | 289 | if (type == UUID_TYPE_SHORT) { |
| boro | 0:4beb2ea291ec | 290 | return (const uint8_t*)&shortUUID; |
| boro | 0:4beb2ea291ec | 291 | } else { |
| boro | 0:4beb2ea291ec | 292 | return baseUUID; |
| boro | 0:4beb2ea291ec | 293 | } |
| boro | 0:4beb2ea291ec | 294 | } |
| boro | 0:4beb2ea291ec | 295 | |
| boro | 0:4beb2ea291ec | 296 | /** |
| boro | 0:4beb2ea291ec | 297 | * Get the uint16_t value of the UUID. |
| boro | 0:4beb2ea291ec | 298 | * |
| boro | 0:4beb2ea291ec | 299 | * @important This function is not used on long UUIDs. |
| boro | 0:4beb2ea291ec | 300 | * |
| boro | 0:4beb2ea291ec | 301 | * @return The value of the shortened UUID. |
| boro | 0:4beb2ea291ec | 302 | */ |
| boro | 0:4beb2ea291ec | 303 | ShortUUIDBytes_t getShortUUID(void) const |
| boro | 0:4beb2ea291ec | 304 | { |
| boro | 0:4beb2ea291ec | 305 | return shortUUID; |
| boro | 0:4beb2ea291ec | 306 | } |
| boro | 0:4beb2ea291ec | 307 | |
| boro | 0:4beb2ea291ec | 308 | /** |
| boro | 0:4beb2ea291ec | 309 | * Get the length (in bytes) of the internal UUID representation. |
| boro | 0:4beb2ea291ec | 310 | * |
| boro | 0:4beb2ea291ec | 311 | * @return sizeof(ShortUUIDBytes_t) if the UUID type is UUID_TYPE_SHORT. |
| boro | 0:4beb2ea291ec | 312 | * @return LENGTH_OF_LONG_UUID if the UUID type is UUID_TYPE_LONG. |
| boro | 0:4beb2ea291ec | 313 | */ |
| boro | 0:4beb2ea291ec | 314 | uint8_t getLen(void) const |
| boro | 0:4beb2ea291ec | 315 | { |
| boro | 0:4beb2ea291ec | 316 | return ((type == UUID_TYPE_SHORT) ? |
| boro | 0:4beb2ea291ec | 317 | sizeof(ShortUUIDBytes_t) : |
| boro | 0:4beb2ea291ec | 318 | LENGTH_OF_LONG_UUID); |
| boro | 0:4beb2ea291ec | 319 | } |
| boro | 0:4beb2ea291ec | 320 | |
| boro | 0:4beb2ea291ec | 321 | /** |
| boro | 0:4beb2ea291ec | 322 | * Equal to operator between UUIDs. |
| boro | 0:4beb2ea291ec | 323 | * |
| boro | 0:4beb2ea291ec | 324 | * @param[in] other The UUID to compare to this. |
| boro | 0:4beb2ea291ec | 325 | * |
| boro | 0:4beb2ea291ec | 326 | * @return true if both UUIDs are equal and false otherwise. |
| boro | 0:4beb2ea291ec | 327 | */ |
| boro | 0:4beb2ea291ec | 328 | bool operator== (const UUID &other) const |
| boro | 0:4beb2ea291ec | 329 | { |
| boro | 0:4beb2ea291ec | 330 | if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) && |
| boro | 0:4beb2ea291ec | 331 | (this->shortUUID == other.shortUUID)) { |
| boro | 0:4beb2ea291ec | 332 | return true; |
| boro | 0:4beb2ea291ec | 333 | } |
| boro | 0:4beb2ea291ec | 334 | |
| boro | 0:4beb2ea291ec | 335 | if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) && |
| boro | 0:4beb2ea291ec | 336 | (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) { |
| boro | 0:4beb2ea291ec | 337 | return true; |
| boro | 0:4beb2ea291ec | 338 | } |
| boro | 0:4beb2ea291ec | 339 | |
| boro | 0:4beb2ea291ec | 340 | return false; |
| boro | 0:4beb2ea291ec | 341 | } |
| boro | 0:4beb2ea291ec | 342 | |
| boro | 0:4beb2ea291ec | 343 | /** |
| boro | 0:4beb2ea291ec | 344 | * Not equal to operator. |
| boro | 0:4beb2ea291ec | 345 | * |
| boro | 0:4beb2ea291ec | 346 | * @param[in] other The UUID compared to this. |
| boro | 0:4beb2ea291ec | 347 | * |
| boro | 0:4beb2ea291ec | 348 | * @return true if both UUIDs are not equal and false otherwise. |
| boro | 0:4beb2ea291ec | 349 | */ |
| boro | 0:4beb2ea291ec | 350 | bool operator!= (const UUID &other) const |
| boro | 0:4beb2ea291ec | 351 | { |
| boro | 0:4beb2ea291ec | 352 | return !(*this == other); |
| boro | 0:4beb2ea291ec | 353 | } |
| boro | 0:4beb2ea291ec | 354 | |
| boro | 0:4beb2ea291ec | 355 | private: |
| boro | 0:4beb2ea291ec | 356 | /** |
| boro | 0:4beb2ea291ec | 357 | * Representation type of the UUID. |
| boro | 0:4beb2ea291ec | 358 | */ |
| boro | 0:4beb2ea291ec | 359 | UUID_Type_t type; |
| boro | 0:4beb2ea291ec | 360 | |
| boro | 0:4beb2ea291ec | 361 | /** |
| boro | 0:4beb2ea291ec | 362 | * Container of UUID value if the UUID type is equal to UUID_TYPE_LONG. |
| boro | 0:4beb2ea291ec | 363 | */ |
| boro | 0:4beb2ea291ec | 364 | LongUUIDBytes_t baseUUID; |
| boro | 0:4beb2ea291ec | 365 | |
| boro | 0:4beb2ea291ec | 366 | /** |
| boro | 0:4beb2ea291ec | 367 | * Container of UUID value if the UUID type is equal to UUID_TYPE_SHORT. |
| boro | 0:4beb2ea291ec | 368 | */ |
| boro | 0:4beb2ea291ec | 369 | ShortUUIDBytes_t shortUUID; |
| boro | 0:4beb2ea291ec | 370 | }; |
| boro | 0:4beb2ea291ec | 371 | |
| boro | 0:4beb2ea291ec | 372 | /** |
| boro | 0:4beb2ea291ec | 373 | * @} |
| boro | 0:4beb2ea291ec | 374 | * @} |
| boro | 0:4beb2ea291ec | 375 | */ |
| boro | 0:4beb2ea291ec | 376 | |
| boro | 0:4beb2ea291ec | 377 | #endif // ifndef MBED_UUID_H__ |