High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
common/UUID.cpp@293:6278354cfd98, 2015-03-02 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Mar 02 11:50:47 2015 +0000
- Revision:
- 293:6278354cfd98
- Parent:
- 292:b5ee2ada4f33
- Child:
- 294:ab928e365ea1
Synchronized with git rev eb17caed
Author: Jeremy Brodt
Corrected comparison value and cast.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 293:6278354cfd98 | 1 | /* mbed Microcontroller Library |
rgrover1 | 293:6278354cfd98 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 293:6278354cfd98 | 3 | * |
rgrover1 | 293:6278354cfd98 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 293:6278354cfd98 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 293:6278354cfd98 | 6 | * You may obtain a copy of the License at |
rgrover1 | 293:6278354cfd98 | 7 | * |
rgrover1 | 293:6278354cfd98 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 293:6278354cfd98 | 9 | * |
rgrover1 | 293:6278354cfd98 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 293:6278354cfd98 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 293:6278354cfd98 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 293:6278354cfd98 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 293:6278354cfd98 | 14 | * limitations under the License. |
rgrover1 | 293:6278354cfd98 | 15 | */ |
rgrover1 | 293:6278354cfd98 | 16 | |
rgrover1 | 293:6278354cfd98 | 17 | #include <string.h> |
rgrover1 | 293:6278354cfd98 | 18 | |
rgrover1 | 293:6278354cfd98 | 19 | #include "UUID.h" |
rgrover1 | 293:6278354cfd98 | 20 | |
rgrover1 | 293:6278354cfd98 | 21 | UUID::UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) { |
rgrover1 | 293:6278354cfd98 | 22 | /* empty */ |
rgrover1 | 293:6278354cfd98 | 23 | } |
rgrover1 | 293:6278354cfd98 | 24 | |
rgrover1 | 293:6278354cfd98 | 25 | /**************************************************************************/ |
rgrover1 | 293:6278354cfd98 | 26 | /*! |
rgrover1 | 293:6278354cfd98 | 27 | @brief Creates a new 128-bit UUID |
rgrover1 | 293:6278354cfd98 | 28 | |
rgrover1 | 293:6278354cfd98 | 29 | @note The UUID is a unique 128-bit (16 byte) ID used to identify |
rgrover1 | 293:6278354cfd98 | 30 | different service or characteristics on the BLE device. |
rgrover1 | 293:6278354cfd98 | 31 | |
rgrover1 | 293:6278354cfd98 | 32 | @note When creating a UUID, the constructor will check if all bytes |
rgrover1 | 293:6278354cfd98 | 33 | except bytes 2/3 are equal to 0. If only bytes 2/3 have a |
rgrover1 | 293:6278354cfd98 | 34 | value, the UUID will be treated as a short/BLE UUID, and the |
rgrover1 | 293:6278354cfd98 | 35 | .type field will be set to UUID::UUID_TYPE_SHORT. If any |
rgrover1 | 293:6278354cfd98 | 36 | of the bytes outside byte 2/3 have a non-zero value, the UUID |
rgrover1 | 293:6278354cfd98 | 37 | will be considered a 128-bit ID, and .type will be assigned |
rgrover1 | 293:6278354cfd98 | 38 | as UUID::UUID_TYPE_LONG. |
rgrover1 | 293:6278354cfd98 | 39 | |
rgrover1 | 293:6278354cfd98 | 40 | @param[in] uuid_base |
rgrover1 | 293:6278354cfd98 | 41 | The 128-bit (16-byte) UUID value. For 128-bit values, |
rgrover1 | 293:6278354cfd98 | 42 | assign all 16 bytes. For 16-bit values, assign the |
rgrover1 | 293:6278354cfd98 | 43 | 16-bits to byte 2 and 3, and leave the rest of the bytes |
rgrover1 | 293:6278354cfd98 | 44 | as 0. |
rgrover1 | 293:6278354cfd98 | 45 | |
rgrover1 | 293:6278354cfd98 | 46 | @section EXAMPLE |
rgrover1 | 293:6278354cfd98 | 47 | |
rgrover1 | 293:6278354cfd98 | 48 | @code |
rgrover1 | 293:6278354cfd98 | 49 | |
rgrover1 | 293:6278354cfd98 | 50 | // Create a short UUID (0x180F) |
rgrover1 | 293:6278354cfd98 | 51 | uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
rgrover1 | 293:6278354cfd98 | 52 | UUID ble_uuid = UUID(shortID); |
rgrover1 | 293:6278354cfd98 | 53 | // ble_uuid.type = UUID_TYPE_SHORT |
rgrover1 | 293:6278354cfd98 | 54 | // ble_uuid.value = 0x180F |
rgrover1 | 293:6278354cfd98 | 55 | |
rgrover1 | 293:6278354cfd98 | 56 | // Creeate a long UUID |
rgrover1 | 293:6278354cfd98 | 57 | uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33, |
rgrover1 | 293:6278354cfd98 | 58 | 0x44, 0x55, 0x66, 0x77, |
rgrover1 | 293:6278354cfd98 | 59 | 0x88, 0x99, 0xAA, 0xBB, |
rgrover1 | 293:6278354cfd98 | 60 | 0xCC, 0xDD, 0xEE, 0xFF }; |
rgrover1 | 293:6278354cfd98 | 61 | UUID custom_uuid = UUID(longID); |
rgrover1 | 293:6278354cfd98 | 62 | // custom_uuid.type = UUID_TYPE_LONG |
rgrover1 | 293:6278354cfd98 | 63 | // custom_uuid.value = 0x3322 |
rgrover1 | 293:6278354cfd98 | 64 | // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
rgrover1 | 293:6278354cfd98 | 65 | |
rgrover1 | 293:6278354cfd98 | 66 | @endcode |
rgrover1 | 293:6278354cfd98 | 67 | */ |
rgrover1 | 293:6278354cfd98 | 68 | /**************************************************************************/ |
rgrover1 | 293:6278354cfd98 | 69 | UUID::UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(0) |
rgrover1 | 293:6278354cfd98 | 70 | { |
rgrover1 | 293:6278354cfd98 | 71 | memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID); |
rgrover1 | 293:6278354cfd98 | 72 | shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3])); |
rgrover1 | 293:6278354cfd98 | 73 | |
rgrover1 | 293:6278354cfd98 | 74 | /* Check if this is a short of a long UUID */ |
rgrover1 | 293:6278354cfd98 | 75 | unsigned index; |
rgrover1 | 293:6278354cfd98 | 76 | for (index = 0; index < LENGTH_OF_LONG_UUID; index++) { |
rgrover1 | 293:6278354cfd98 | 77 | if ((index == 2) || (index == 3)) { |
rgrover1 | 293:6278354cfd98 | 78 | continue; /* we should not consider bytes 2 and 3 because that's |
rgrover1 | 293:6278354cfd98 | 79 | * where the 16-bit relative UUID is placed. */ |
rgrover1 | 293:6278354cfd98 | 80 | } |
rgrover1 | 293:6278354cfd98 | 81 | |
rgrover1 | 293:6278354cfd98 | 82 | if (baseUUID[index] != 0) { |
rgrover1 | 293:6278354cfd98 | 83 | type = UUID_TYPE_LONG; |
rgrover1 | 293:6278354cfd98 | 84 | |
rgrover1 | 293:6278354cfd98 | 85 | /* zero out the 16-bit part in the base; this will help equate long |
rgrover1 | 293:6278354cfd98 | 86 | * UUIDs when they differ only in this 16-bit relative part.*/ |
rgrover1 | 293:6278354cfd98 | 87 | baseUUID[2] = 0; |
rgrover1 | 293:6278354cfd98 | 88 | baseUUID[3] = 0; |
rgrover1 | 293:6278354cfd98 | 89 | |
rgrover1 | 293:6278354cfd98 | 90 | return; |
rgrover1 | 293:6278354cfd98 | 91 | } |
rgrover1 | 293:6278354cfd98 | 92 | } |
rgrover1 | 293:6278354cfd98 | 93 | } |
rgrover1 | 293:6278354cfd98 | 94 | |
rgrover1 | 293:6278354cfd98 | 95 | bool UUID::operator==(const UUID &other) const |
rgrover1 | 293:6278354cfd98 | 96 | { |
rgrover1 | 293:6278354cfd98 | 97 | if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) && |
rgrover1 | 293:6278354cfd98 | 98 | (this->shortUUID == other.shortUUID)) { |
rgrover1 | 293:6278354cfd98 | 99 | return true; |
rgrover1 | 293:6278354cfd98 | 100 | } |
rgrover1 | 293:6278354cfd98 | 101 | |
rgrover1 | 293:6278354cfd98 | 102 | if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) && |
rgrover1 | 293:6278354cfd98 | 103 | (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) { |
rgrover1 | 293:6278354cfd98 | 104 | return true; |
rgrover1 | 293:6278354cfd98 | 105 | } |
rgrover1 | 293:6278354cfd98 | 106 | |
rgrover1 | 293:6278354cfd98 | 107 | return false; |
rgrover1 | 293:6278354cfd98 | 108 | } |