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