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