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.
UUID.cpp
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 00018 #include <stdio.h> 00019 #include <string.h> 00020 00021 #include "UUID.h" 00022 00023 /**************************************************************************/ 00024 /*! 00025 @brief Creates an empty 128-bit UUID 00026 00027 @note This UUID must be assigned a valid value via the 'update' 00028 function before it can be safely used! 00029 */ 00030 /**************************************************************************/ 00031 UUID::UUID(void) 00032 { 00033 memset(base, 0, 16); 00034 value = 0; 00035 type = UUID_TYPE_SHORT; 00036 } 00037 00038 /**************************************************************************/ 00039 /*! 00040 @brief Creates a new 128-bit UUID 00041 00042 @note The UUID is a unique 128-bit (16 byte) ID used to identify 00043 different service or characteristics on the BLE device. 00044 00045 @note When creating a UUID, the constructor will check if all bytes 00046 except bytes 2/3 are equal to 0. If only bytes 2/3 have a 00047 value, the UUID will be treated as a short/BLE UUID, and the 00048 .type field will be set to UUID::UUID_TYPE_SHORT. If any 00049 of the bytes outside byte 2/3 have a non-zero value, the UUID 00050 will be considered a 128-bit ID, and .type will be assigned 00051 as UUID::UUID_TYPE_LONG. 00052 00053 @param[in] uuid_base 00054 The 128-bit (16-byte) UUID value. For 128-bit values, 00055 assign all 16 bytes. For 16-bit values, assign the 00056 16-bits to byte 2 and 3, and leave the rest of the bytes 00057 as 0. 00058 00059 @section EXAMPLE 00060 00061 @code 00062 00063 // Create a short UUID (0x180F) 00064 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 00065 UUID ble_uuid = UUID(shortID); 00066 // ble_uuid.type = UUID_TYPE_SHORT 00067 // ble_uuid.value = 0x180F 00068 00069 // Creeate a long UUID 00070 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33, 00071 0x44, 0x55, 0x66, 0x77, 00072 0x88, 0x99, 0xAA, 0xBB, 00073 0xCC, 0xDD, 0xEE, 0xFF }; 00074 UUID custom_uuid = UUID(longID); 00075 // custom_uuid.type = UUID_TYPE_LONG 00076 // custom_uuid.value = 0x3322 00077 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00078 00079 @endcode 00080 */ 00081 /**************************************************************************/ 00082 UUID::UUID(uint8_t const uuid_base[16]) 00083 { 00084 memcpy(base, uuid_base, 16); 00085 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); 00086 00087 /* Check if this is a short of a long UUID */ 00088 if (uuid_base[0] + uuid_base[1] + 00089 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + 00090 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + 00091 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) 00092 { 00093 type = UUID_TYPE_SHORT; 00094 } 00095 else 00096 { 00097 type = UUID_TYPE_LONG; 00098 } 00099 } 00100 00101 /**************************************************************************/ 00102 /*! 00103 @brief Creates a short (16-bit) UUID 00104 00105 @param[in] ble_uuid 00106 The 16-bit BLE UUID value. 00107 */ 00108 /**************************************************************************/ 00109 UUID::UUID(uint16_t const ble_uuid) 00110 { 00111 memset(base, 0, 16); 00112 memcpy(base+2, (uint8_t *)&ble_uuid, 2); 00113 value = ble_uuid; 00114 type = UUID_TYPE_SHORT; 00115 } 00116 00117 /**************************************************************************/ 00118 /*! 00119 @brief UUID destructor 00120 */ 00121 /**************************************************************************/ 00122 UUID::~UUID(void) 00123 { 00124 } 00125 00126 /**************************************************************************/ 00127 /*! 00128 @brief Updates the value of the UUID 00129 00130 @args[in] uuid_base 00131 The 128-bit value to use when updating the UUID. For 00132 16-bit IDs, insert the ID in bytes 2/3 in LSB format. 00133 00134 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an 00135 error code if there was a problem 00136 @retval BLE_ERROR_NONE 00137 Everything executed correctly 00138 00139 @section EXAMPLE 00140 00141 @code 00142 00143 @endcode 00144 */ 00145 /**************************************************************************/ 00146 ble_error_t UUID::update(uint8_t const uuid_base[16]) 00147 { 00148 memcpy(base, uuid_base, 16); 00149 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); 00150 00151 /* Check if this is a short of a long UUID */ 00152 if (uuid_base[0] + uuid_base[1] + 00153 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + 00154 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + 00155 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) 00156 { 00157 type = UUID_TYPE_SHORT; 00158 } 00159 else 00160 { 00161 type = UUID_TYPE_LONG; 00162 } 00163 00164 return BLE_ERROR_NONE; 00165 } 00166 00167 /**************************************************************************/ 00168 /*! 00169 @brief Updates the value of the UUID 00170 00171 @args[in] ble_uuid 00172 The 16-bit value to use when updating the UUID. 00173 00174 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an 00175 error code if there was a problem 00176 @retval BLE_ERROR_NONE 00177 Everything executed correctly 00178 00179 @section EXAMPLE 00180 00181 @code 00182 00183 @endcode 00184 */ 00185 /**************************************************************************/ 00186 ble_error_t UUID::update(uint16_t const ble_uuid) 00187 { 00188 memset(base, 0, 16); 00189 memcpy(base+2, (uint8_t *)&ble_uuid, 2); 00190 value = ble_uuid; 00191 type = UUID_TYPE_SHORT; 00192 00193 return BLE_ERROR_NONE; 00194 }
Generated on Tue Jul 12 2022 12:14:19 by
