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 BLE_API by
UUID.cpp@46:33d87f468a0d, 2014-05-23 (annotated)
- Committer:
- Rohit Grover
- Date:
- Fri May 23 15:21:47 2014 +0100
- Revision:
- 46:33d87f468a0d
- Parent:
- 43:ac154ac74a30
- Child:
- 50:9078969e80e4
simplification of the API used to setup GAP advertising
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ktownsend | 27:4a83843f04b0 | 1 | /* mbed Microcontroller Library |
| ktownsend | 27:4a83843f04b0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| ktownsend | 27:4a83843f04b0 | 3 | * |
| ktownsend | 27:4a83843f04b0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| ktownsend | 27:4a83843f04b0 | 5 | * you may not use this file except in compliance with the License. |
| ktownsend | 27:4a83843f04b0 | 6 | * You may obtain a copy of the License at |
| ktownsend | 27:4a83843f04b0 | 7 | * |
| ktownsend | 27:4a83843f04b0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| ktownsend | 27:4a83843f04b0 | 9 | * |
| ktownsend | 27:4a83843f04b0 | 10 | * Unless required by applicable law or agreed to in writing, software |
| ktownsend | 27:4a83843f04b0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| ktownsend | 27:4a83843f04b0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| ktownsend | 27:4a83843f04b0 | 13 | * See the License for the specific language governing permissions and |
| ktownsend | 27:4a83843f04b0 | 14 | * limitations under the License. |
| ktownsend | 27:4a83843f04b0 | 15 | */ |
| Rohit Grover |
34:da2ea8cd6216 | 16 | |
| ktownsend | 27:4a83843f04b0 | 17 | |
| ktownsend | 0:ace2e8d3ce79 | 18 | #include <stdio.h> |
| ktownsend | 0:ace2e8d3ce79 | 19 | #include <string.h> |
| ktownsend | 0:ace2e8d3ce79 | 20 | |
| ktownsend | 27:4a83843f04b0 | 21 | #include "UUID.h" |
| ktownsend | 0:ace2e8d3ce79 | 22 | |
| ktownsend | 0:ace2e8d3ce79 | 23 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 24 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 25 | @brief Creates an empty 128-bit UUID |
| Rohit Grover |
34:da2ea8cd6216 | 26 | |
| ktownsend | 0:ace2e8d3ce79 | 27 | @note This UUID must be assigned a valid value via the 'update' |
| ktownsend | 0:ace2e8d3ce79 | 28 | function before it can be safely used! |
| ktownsend | 0:ace2e8d3ce79 | 29 | */ |
| ktownsend | 0:ace2e8d3ce79 | 30 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 31 | UUID::UUID(void) |
| ktownsend | 0:ace2e8d3ce79 | 32 | { |
| ktownsend | 0:ace2e8d3ce79 | 33 | memset(base, 0, 16); |
| ktownsend | 0:ace2e8d3ce79 | 34 | value = 0; |
| ktownsend | 0:ace2e8d3ce79 | 35 | type = UUID_TYPE_SHORT; |
| ktownsend | 0:ace2e8d3ce79 | 36 | } |
| ktownsend | 0:ace2e8d3ce79 | 37 | |
| ktownsend | 0:ace2e8d3ce79 | 38 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 39 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 40 | @brief Creates a new 128-bit UUID |
| Rohit Grover |
34:da2ea8cd6216 | 41 | |
| ktownsend | 0:ace2e8d3ce79 | 42 | @note The UUID is a unique 128-bit (16 byte) ID used to identify |
| ktownsend | 0:ace2e8d3ce79 | 43 | different service or characteristics on the BLE device. |
| Rohit Grover |
34:da2ea8cd6216 | 44 | |
| ktownsend | 0:ace2e8d3ce79 | 45 | @note When creating a UUID, the constructor will check if all bytes |
| ktownsend | 0:ace2e8d3ce79 | 46 | except bytes 2/3 are equal to 0. If only bytes 2/3 have a |
| ktownsend | 0:ace2e8d3ce79 | 47 | value, the UUID will be treated as a short/BLE UUID, and the |
| ktownsend | 0:ace2e8d3ce79 | 48 | .type field will be set to UUID::UUID_TYPE_SHORT. If any |
| ktownsend | 0:ace2e8d3ce79 | 49 | of the bytes outside byte 2/3 have a non-zero value, the UUID |
| Rohit Grover |
34:da2ea8cd6216 | 50 | will be considered a 128-bit ID, and .type will be assigned |
| Rohit Grover |
34:da2ea8cd6216 | 51 | as UUID::UUID_TYPE_LONG. |
| ktownsend | 0:ace2e8d3ce79 | 52 | |
| ktownsend | 0:ace2e8d3ce79 | 53 | @param[in] uuid_base |
| ktownsend | 0:ace2e8d3ce79 | 54 | The 128-bit (16-byte) UUID value. For 128-bit values, |
| ktownsend | 0:ace2e8d3ce79 | 55 | assign all 16 bytes. For 16-bit values, assign the |
| ktownsend | 0:ace2e8d3ce79 | 56 | 16-bits to byte 2 and 3, and leave the rest of the bytes |
| ktownsend | 0:ace2e8d3ce79 | 57 | as 0. |
| ktownsend | 0:ace2e8d3ce79 | 58 | |
| ktownsend | 0:ace2e8d3ce79 | 59 | @section EXAMPLE |
| ktownsend | 0:ace2e8d3ce79 | 60 | |
| ktownsend | 0:ace2e8d3ce79 | 61 | @code |
| ktownsend | 0:ace2e8d3ce79 | 62 | |
| ktownsend | 0:ace2e8d3ce79 | 63 | // Create a short UUID (0x180F) |
| ktownsend | 0:ace2e8d3ce79 | 64 | uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| ktownsend | 0:ace2e8d3ce79 | 65 | UUID ble_uuid = UUID(shortID); |
| ktownsend | 0:ace2e8d3ce79 | 66 | // ble_uuid.type = UUID_TYPE_SHORT |
| ktownsend | 0:ace2e8d3ce79 | 67 | // ble_uuid.value = 0x180F |
| Rohit Grover |
34:da2ea8cd6216 | 68 | |
| ktownsend | 0:ace2e8d3ce79 | 69 | // Creeate a long UUID |
| ktownsend | 0:ace2e8d3ce79 | 70 | uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33, |
| Rohit Grover |
34:da2ea8cd6216 | 71 | 0x44, 0x55, 0x66, 0x77, |
| Rohit Grover |
34:da2ea8cd6216 | 72 | 0x88, 0x99, 0xAA, 0xBB, |
| ktownsend | 0:ace2e8d3ce79 | 73 | 0xCC, 0xDD, 0xEE, 0xFF }; |
| ktownsend | 0:ace2e8d3ce79 | 74 | UUID custom_uuid = UUID(longID); |
| ktownsend | 0:ace2e8d3ce79 | 75 | // custom_uuid.type = UUID_TYPE_LONG |
| ktownsend | 0:ace2e8d3ce79 | 76 | // custom_uuid.value = 0x3322 |
| ktownsend | 0:ace2e8d3ce79 | 77 | // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
| Rohit Grover |
34:da2ea8cd6216 | 78 | |
| ktownsend | 0:ace2e8d3ce79 | 79 | @endcode |
| ktownsend | 0:ace2e8d3ce79 | 80 | */ |
| ktownsend | 0:ace2e8d3ce79 | 81 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 82 | UUID::UUID(uint8_t const uuid_base[16]) |
| ktownsend | 0:ace2e8d3ce79 | 83 | { |
| ktownsend | 0:ace2e8d3ce79 | 84 | memcpy(base, uuid_base, 16); |
| ktownsend | 0:ace2e8d3ce79 | 85 | value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); |
| ktownsend | 0:ace2e8d3ce79 | 86 | |
| ktownsend | 0:ace2e8d3ce79 | 87 | /* Check if this is a short of a long UUID */ |
| Rohit Grover |
34:da2ea8cd6216 | 88 | if (uuid_base[0] + uuid_base[1] + |
| Rohit Grover |
34:da2ea8cd6216 | 89 | uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + |
| Rohit Grover |
34:da2ea8cd6216 | 90 | uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + |
| Rohit Grover |
34:da2ea8cd6216 | 91 | uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) { |
| Rohit Grover |
34:da2ea8cd6216 | 92 | type = UUID_TYPE_SHORT; |
| Rohit Grover |
43:ac154ac74a30 | 93 | } else { |
| ktownsend | 0:ace2e8d3ce79 | 94 | type = UUID_TYPE_LONG; |
| ktownsend | 0:ace2e8d3ce79 | 95 | } |
| ktownsend | 0:ace2e8d3ce79 | 96 | } |
| ktownsend | 0:ace2e8d3ce79 | 97 | |
| ktownsend | 0:ace2e8d3ce79 | 98 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 99 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 100 | @brief Creates a short (16-bit) UUID |
| ktownsend | 0:ace2e8d3ce79 | 101 | |
| ktownsend | 0:ace2e8d3ce79 | 102 | @param[in] ble_uuid |
| ktownsend | 0:ace2e8d3ce79 | 103 | The 16-bit BLE UUID value. |
| ktownsend | 0:ace2e8d3ce79 | 104 | */ |
| ktownsend | 0:ace2e8d3ce79 | 105 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 106 | UUID::UUID(uint16_t const ble_uuid) |
| ktownsend | 0:ace2e8d3ce79 | 107 | { |
| ktownsend | 0:ace2e8d3ce79 | 108 | memset(base, 0, 16); |
| Rohit Grover |
34:da2ea8cd6216 | 109 | memcpy(base + 2, (uint8_t *)&ble_uuid, 2); |
| ktownsend | 0:ace2e8d3ce79 | 110 | value = ble_uuid; |
| Rohit Grover |
34:da2ea8cd6216 | 111 | type = UUID_TYPE_SHORT; |
| ktownsend | 0:ace2e8d3ce79 | 112 | } |
| ktownsend | 0:ace2e8d3ce79 | 113 | |
| ktownsend | 0:ace2e8d3ce79 | 114 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 115 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 116 | @brief UUID destructor |
| ktownsend | 0:ace2e8d3ce79 | 117 | */ |
| ktownsend | 0:ace2e8d3ce79 | 118 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 119 | UUID::~UUID(void) |
| ktownsend | 0:ace2e8d3ce79 | 120 | { |
| ktownsend | 0:ace2e8d3ce79 | 121 | } |
| ktownsend | 0:ace2e8d3ce79 | 122 | |
| ktownsend | 0:ace2e8d3ce79 | 123 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 124 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 125 | @brief Updates the value of the UUID |
| Rohit Grover |
34:da2ea8cd6216 | 126 | |
| ktownsend | 0:ace2e8d3ce79 | 127 | @args[in] uuid_base |
| ktownsend | 0:ace2e8d3ce79 | 128 | The 128-bit value to use when updating the UUID. For |
| ktownsend | 0:ace2e8d3ce79 | 129 | 16-bit IDs, insert the ID in bytes 2/3 in LSB format. |
| Rohit Grover |
34:da2ea8cd6216 | 130 | |
| ktownsend | 0:ace2e8d3ce79 | 131 | @returns BLE_ERROR_NONE (0) if everything executed correctly, or an |
| ktownsend | 0:ace2e8d3ce79 | 132 | error code if there was a problem |
| ktownsend | 0:ace2e8d3ce79 | 133 | @retval BLE_ERROR_NONE |
| ktownsend | 0:ace2e8d3ce79 | 134 | Everything executed correctly |
| ktownsend | 0:ace2e8d3ce79 | 135 | |
| ktownsend | 0:ace2e8d3ce79 | 136 | @section EXAMPLE |
| ktownsend | 0:ace2e8d3ce79 | 137 | |
| ktownsend | 0:ace2e8d3ce79 | 138 | @code |
| Rohit Grover |
34:da2ea8cd6216 | 139 | |
| Rohit Grover |
34:da2ea8cd6216 | 140 | @endcode |
| ktownsend | 0:ace2e8d3ce79 | 141 | */ |
| ktownsend | 0:ace2e8d3ce79 | 142 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 143 | ble_error_t UUID::update(uint8_t const uuid_base[16]) |
| ktownsend | 0:ace2e8d3ce79 | 144 | { |
| ktownsend | 0:ace2e8d3ce79 | 145 | memcpy(base, uuid_base, 16); |
| ktownsend | 0:ace2e8d3ce79 | 146 | value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); |
| ktownsend | 0:ace2e8d3ce79 | 147 | |
| ktownsend | 0:ace2e8d3ce79 | 148 | /* Check if this is a short of a long UUID */ |
| Rohit Grover |
34:da2ea8cd6216 | 149 | if (uuid_base[0] + uuid_base[1] + |
| Rohit Grover |
34:da2ea8cd6216 | 150 | uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + |
| Rohit Grover |
34:da2ea8cd6216 | 151 | uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + |
| Rohit Grover |
34:da2ea8cd6216 | 152 | uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) { |
| Rohit Grover |
34:da2ea8cd6216 | 153 | type = UUID_TYPE_SHORT; |
| Rohit Grover |
43:ac154ac74a30 | 154 | } else { |
| ktownsend | 0:ace2e8d3ce79 | 155 | type = UUID_TYPE_LONG; |
| ktownsend | 0:ace2e8d3ce79 | 156 | } |
| Rohit Grover |
34:da2ea8cd6216 | 157 | |
| ktownsend | 0:ace2e8d3ce79 | 158 | return BLE_ERROR_NONE; |
| ktownsend | 0:ace2e8d3ce79 | 159 | } |
| ktownsend | 0:ace2e8d3ce79 | 160 | |
| ktownsend | 0:ace2e8d3ce79 | 161 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 162 | /*! |
| ktownsend | 0:ace2e8d3ce79 | 163 | @brief Updates the value of the UUID |
| Rohit Grover |
34:da2ea8cd6216 | 164 | |
| ktownsend | 0:ace2e8d3ce79 | 165 | @args[in] ble_uuid |
| ktownsend | 0:ace2e8d3ce79 | 166 | The 16-bit value to use when updating the UUID. |
| Rohit Grover |
34:da2ea8cd6216 | 167 | |
| ktownsend | 0:ace2e8d3ce79 | 168 | @returns BLE_ERROR_NONE (0) if everything executed correctly, or an |
| ktownsend | 0:ace2e8d3ce79 | 169 | error code if there was a problem |
| ktownsend | 0:ace2e8d3ce79 | 170 | @retval BLE_ERROR_NONE |
| ktownsend | 0:ace2e8d3ce79 | 171 | Everything executed correctly |
| ktownsend | 0:ace2e8d3ce79 | 172 | |
| ktownsend | 0:ace2e8d3ce79 | 173 | @section EXAMPLE |
| ktownsend | 0:ace2e8d3ce79 | 174 | |
| ktownsend | 0:ace2e8d3ce79 | 175 | @code |
| Rohit Grover |
34:da2ea8cd6216 | 176 | |
| Rohit Grover |
34:da2ea8cd6216 | 177 | @endcode |
| ktownsend | 0:ace2e8d3ce79 | 178 | */ |
| ktownsend | 0:ace2e8d3ce79 | 179 | /**************************************************************************/ |
| ktownsend | 0:ace2e8d3ce79 | 180 | ble_error_t UUID::update(uint16_t const ble_uuid) |
| ktownsend | 0:ace2e8d3ce79 | 181 | { |
| ktownsend | 0:ace2e8d3ce79 | 182 | memset(base, 0, 16); |
| Rohit Grover |
34:da2ea8cd6216 | 183 | memcpy(base + 2, (uint8_t *)&ble_uuid, 2); |
| ktownsend | 0:ace2e8d3ce79 | 184 | value = ble_uuid; |
| Rohit Grover |
34:da2ea8cd6216 | 185 | type = UUID_TYPE_SHORT; |
| Rohit Grover |
34:da2ea8cd6216 | 186 | |
| ktownsend | 0:ace2e8d3ce79 | 187 | return BLE_ERROR_NONE; |
| ktownsend | 0:ace2e8d3ce79 | 188 | } |
