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.
Fork of LinkNode-Test by
UUID.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __UUID_H__ 00018 #define __UUID_H__ 00019 00020 #include <stdint.h> 00021 #include <string.h> 00022 #include <algorithm> 00023 00024 #include "blecommon.h" 00025 00026 /** 00027 * A trivial converter for single hexadecimal character to unsigned-int. 00028 * @param c hexadecimal character. 00029 * @return the corresponding value as unsigned int. 00030 */ 00031 static uint8_t char2int(char c) { 00032 if ((c >= '0') && (c <= '9')) { 00033 return c - '0'; 00034 } else if ((c >= 'a') && (c <= 'f')) { 00035 return c - 'a' + 10; 00036 } else if ((c >= 'A') && (c <= 'F')) { 00037 return c - 'A' + 10; 00038 } else { 00039 return 0; 00040 } 00041 } 00042 00043 class UUID { 00044 public: 00045 enum UUID_Type_t { 00046 UUID_TYPE_SHORT = 0, // Short BLE UUID. 00047 UUID_TYPE_LONG = 1 // Full 128-bit UUID. 00048 }; 00049 00050 /** 00051 * An enumeration to specify byte ordering of the long version of the UUID. 00052 */ 00053 typedef enum { 00054 MSB, /*!< Most-significant byte first (at the smallest address) */ 00055 LSB /*!< least-significant byte first (at the smallest address) */ 00056 } ByteOrder_t; 00057 00058 typedef uint16_t ShortUUIDBytes_t; 00059 00060 static const unsigned LENGTH_OF_LONG_UUID = 16; 00061 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; 00062 00063 static const unsigned MAX_UUID_STRING_LENGTH = LENGTH_OF_LONG_UUID * 2 + 4; 00064 00065 public: 00066 00067 /** 00068 * Creates a new 128-bit UUID. 00069 * 00070 * @note The UUID is a unique 128-bit (16 byte) ID used to identify 00071 * different service or characteristics on the BLE device. 00072 * 00073 * @param stringUUID 00074 * The 128-bit (16-byte) UUID as a human readable const-string. 00075 * Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX 00076 * Upper and lower case supported. Hyphens are optional, but only 00077 * upto four of them. The UUID is stored internally as a 16 byte 00078 * array, LSB (little endian), which is opposite from the string. 00079 */ 00080 UUID(const char* stringUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) { 00081 bool nibble = false; 00082 uint8_t byte = 0; 00083 size_t baseIndex = 0; 00084 uint8_t tempUUID[LENGTH_OF_LONG_UUID]; 00085 00086 // Iterate through string, abort if NULL is encountered prematurely. 00087 // Ignore upto four hyphens. 00088 for (size_t index = 0; (index < MAX_UUID_STRING_LENGTH) && (baseIndex < LENGTH_OF_LONG_UUID); index++) { 00089 if (stringUUID[index] == '\0') { 00090 // error abort 00091 break; 00092 } else if (stringUUID[index] == '-') { 00093 // ignore hyphen 00094 continue; 00095 } else if (nibble) { 00096 // got second nibble 00097 byte |= char2int(stringUUID[index]); 00098 nibble = false; 00099 00100 // store copy 00101 tempUUID[baseIndex++] = byte; 00102 } else { 00103 // got first nibble 00104 byte = char2int(stringUUID[index]) << 4; 00105 nibble = true; 00106 } 00107 } 00108 00109 // populate internal variables if string was successfully parsed 00110 if (baseIndex == LENGTH_OF_LONG_UUID) { 00111 setupLong(tempUUID, UUID::MSB); 00112 } else { 00113 const uint8_t sig[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 00114 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; 00115 setupLong(sig, UUID::MSB); 00116 } 00117 } 00118 00119 /** 00120 * Creates a new 128-bit UUID. 00121 * 00122 * @note The UUID is a unique 128-bit (16 byte) ID used to identify 00123 * different service or characteristics on the BLE device. 00124 * 00125 * @param longUUID 00126 * The 128-bit (16-byte) UUID value. 00127 * @param order 00128 * The bit order of the UUID, MSB by default. 00129 */ 00130 UUID(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) { 00131 setupLong(longUUID, order); 00132 } 00133 00134 /** 00135 * Creates a new 16-bit UUID. 00136 * 00137 * @note The UUID is a unique 16-bit (2 byte) ID used to identify 00138 * different service or characteristics on the BLE device. 00139 * 00140 * For efficiency, and because 16 bytes would take a large chunk of the 00141 * 27-byte data payload length of the Link Layer, the BLE specification adds 00142 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened 00143 * formats can be used only with UUIDs that are defined in the Bluetooth 00144 * specification (listed by the Bluetooth SIG as standard 00145 * Bluetooth UUIDs). 00146 * 00147 * To reconstruct the full 128-bit UUID from the shortened version, insert 00148 * the 16-bit short value (indicated by xxxx, including leading zeros) into 00149 * the Bluetooth Base UUID: 00150 * 00151 * 0000xxxx-0000-1000-8000-00805F9B34FB 00152 * 00153 * @note Shortening is not available for UUIDs that are not derived from the 00154 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called 00155 * vendor-specific UUIDs. In these cases, you’ll need to use the full 00156 * 128-bit UUID value at all times. 00157 * 00158 * @note We don't yet support 32-bit shortened UUIDs. 00159 */ 00160 UUID(ShortUUIDBytes_t _shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(_shortUUID) { 00161 /* Empty */ 00162 } 00163 00164 UUID(const UUID &source) { 00165 type = source.type; 00166 shortUUID = source.shortUUID; 00167 memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID); 00168 } 00169 00170 UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) { 00171 /* empty */ 00172 } 00173 00174 /** 00175 * Fill in a 128-bit UUID; this is useful when the UUID isn't known at the time of the object construction. 00176 */ 00177 void setupLong(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) { 00178 type = UUID_TYPE_LONG; 00179 if (order == UUID::MSB) { 00180 // Switch endian. Input is big-endian, internal representation is little endian. 00181 std::reverse_copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID); 00182 } else { 00183 std::copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID); 00184 } 00185 shortUUID = (uint16_t)((baseUUID[13] << 8) | (baseUUID[12])); 00186 } 00187 00188 public: 00189 UUID_Type_t shortOrLong(void) const {return type; } 00190 const uint8_t *getBaseUUID(void) const { 00191 if (type == UUID_TYPE_SHORT) { 00192 return (const uint8_t*)&shortUUID; 00193 } else { 00194 return baseUUID; 00195 } 00196 } 00197 00198 ShortUUIDBytes_t getShortUUID(void) const {return shortUUID;} 00199 uint8_t getLen(void) const { 00200 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID); 00201 } 00202 00203 bool operator== (const UUID &other) const { 00204 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) && 00205 (this->shortUUID == other.shortUUID)) { 00206 return true; 00207 } 00208 00209 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) && 00210 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) { 00211 return true; 00212 } 00213 00214 return false; 00215 } 00216 00217 bool operator!= (const UUID &other) const { 00218 return !(*this == other); 00219 } 00220 00221 private: 00222 UUID_Type_t type; // UUID_TYPE_SHORT or UUID_TYPE_LONG 00223 LongUUIDBytes_t baseUUID; // The long UUID 00224 ShortUUIDBytes_t shortUUID; // 16 bit UUID 00225 }; 00226 00227 #endif // ifndef __UUID_H__
Generated on Tue Jul 12 2022 16:00:22 by
