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