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