High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/UUID.h@1023:a072b59caddb, 2015-12-10 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Dec 10 09:15:02 2015 +0000
- Revision:
- 1023:a072b59caddb
- Parent:
- 1022:306c409f6c09
- Child:
- 1035:d8ba3cb5a39a
Synchronized with git rev b9af3222
Author: Irit Arkin
Edit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 710:b2e1a2660ec2 | 1 | /* mbed Microcontroller Library |
rgrover1 | 710:b2e1a2660ec2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 710:b2e1a2660ec2 | 3 | * |
rgrover1 | 710:b2e1a2660ec2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 710:b2e1a2660ec2 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 710:b2e1a2660ec2 | 6 | * You may obtain a copy of the License at |
rgrover1 | 710:b2e1a2660ec2 | 7 | * |
rgrover1 | 710:b2e1a2660ec2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 710:b2e1a2660ec2 | 9 | * |
rgrover1 | 710:b2e1a2660ec2 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 710:b2e1a2660ec2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 710:b2e1a2660ec2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 710:b2e1a2660ec2 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 710:b2e1a2660ec2 | 14 | * limitations under the License. |
rgrover1 | 710:b2e1a2660ec2 | 15 | */ |
rgrover1 | 710:b2e1a2660ec2 | 16 | |
rgrover1 | 710:b2e1a2660ec2 | 17 | #ifndef __UUID_H__ |
rgrover1 | 710:b2e1a2660ec2 | 18 | #define __UUID_H__ |
rgrover1 | 710:b2e1a2660ec2 | 19 | |
rgrover1 | 710:b2e1a2660ec2 | 20 | #include <stdint.h> |
rgrover1 | 710:b2e1a2660ec2 | 21 | #include <string.h> |
rgrover1 | 710:b2e1a2660ec2 | 22 | |
rgrover1 | 710:b2e1a2660ec2 | 23 | #include "blecommon.h" |
rgrover1 | 710:b2e1a2660ec2 | 24 | |
rgrover1 | 710:b2e1a2660ec2 | 25 | class UUID { |
rgrover1 | 710:b2e1a2660ec2 | 26 | public: |
rgrover1 | 710:b2e1a2660ec2 | 27 | enum UUID_Type_t { |
rgrover1 | 993:4d62b7967c11 | 28 | UUID_TYPE_SHORT = 0, // Short BLE UUID. |
rgrover1 | 993:4d62b7967c11 | 29 | UUID_TYPE_LONG = 1 // Full 128-bit UUID. |
rgrover1 | 710:b2e1a2660ec2 | 30 | }; |
rgrover1 | 710:b2e1a2660ec2 | 31 | |
rgrover1 | 710:b2e1a2660ec2 | 32 | typedef uint16_t ShortUUIDBytes_t; |
rgrover1 | 710:b2e1a2660ec2 | 33 | |
rgrover1 | 710:b2e1a2660ec2 | 34 | static const unsigned LENGTH_OF_LONG_UUID = 16; |
rgrover1 | 710:b2e1a2660ec2 | 35 | typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; |
rgrover1 | 710:b2e1a2660ec2 | 36 | |
rgrover1 | 710:b2e1a2660ec2 | 37 | public: |
rgrover1 | 710:b2e1a2660ec2 | 38 | /** |
rgrover1 | 993:4d62b7967c11 | 39 | * Creates a new 128-bit UUID. |
rgrover1 | 710:b2e1a2660ec2 | 40 | * |
rgrover1 | 710:b2e1a2660ec2 | 41 | * @note The UUID is a unique 128-bit (16 byte) ID used to identify |
rgrover1 | 710:b2e1a2660ec2 | 42 | * different service or characteristics on the BLE device. |
rgrover1 | 710:b2e1a2660ec2 | 43 | * |
rgrover1 | 710:b2e1a2660ec2 | 44 | * @param longUUID |
rgrover1 | 1023:a072b59caddb | 45 | * The 128-bit (16-byte) UUID value, MSB first (big-endian). |
rgrover1 | 710:b2e1a2660ec2 | 46 | */ |
rgrover1 | 710:b2e1a2660ec2 | 47 | UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) { |
rgrover1 | 710:b2e1a2660ec2 | 48 | setupLong(longUUID); |
rgrover1 | 710:b2e1a2660ec2 | 49 | } |
rgrover1 | 710:b2e1a2660ec2 | 50 | |
rgrover1 | 710:b2e1a2660ec2 | 51 | /** |
rgrover1 | 993:4d62b7967c11 | 52 | * Creates a new 16-bit UUID. |
rgrover1 | 710:b2e1a2660ec2 | 53 | * |
rgrover1 | 710:b2e1a2660ec2 | 54 | * @note The UUID is a unique 16-bit (2 byte) ID used to identify |
rgrover1 | 710:b2e1a2660ec2 | 55 | * different service or characteristics on the BLE device. |
rgrover1 | 710:b2e1a2660ec2 | 56 | * |
rgrover1 | 710:b2e1a2660ec2 | 57 | * For efficiency, and because 16 bytes would take a large chunk of the |
rgrover1 | 710:b2e1a2660ec2 | 58 | * 27-byte data payload length of the Link Layer, the BLE specification adds |
rgrover1 | 710:b2e1a2660ec2 | 59 | * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened |
rgrover1 | 710:b2e1a2660ec2 | 60 | * formats can be used only with UUIDs that are defined in the Bluetooth |
rgrover1 | 993:4d62b7967c11 | 61 | * specification (listed by the Bluetooth SIG as standard |
rgrover1 | 710:b2e1a2660ec2 | 62 | * Bluetooth UUIDs). |
rgrover1 | 710:b2e1a2660ec2 | 63 | * |
rgrover1 | 710:b2e1a2660ec2 | 64 | * To reconstruct the full 128-bit UUID from the shortened version, insert |
rgrover1 | 710:b2e1a2660ec2 | 65 | * the 16-bit short value (indicated by xxxx, including leading zeros) into |
rgrover1 | 710:b2e1a2660ec2 | 66 | * the Bluetooth Base UUID: |
rgrover1 | 710:b2e1a2660ec2 | 67 | * |
rgrover1 | 710:b2e1a2660ec2 | 68 | * 0000xxxx-0000-1000-8000-00805F9B34FB |
rgrover1 | 710:b2e1a2660ec2 | 69 | * |
rgrover1 | 710:b2e1a2660ec2 | 70 | * @note Shortening is not available for UUIDs that are not derived from the |
rgrover1 | 710:b2e1a2660ec2 | 71 | * Bluetooth Base UUID. Such non-standard UUIDs are commonly called |
rgrover1 | 710:b2e1a2660ec2 | 72 | * vendor-specific UUIDs. In these cases, you’ll need to use the full |
rgrover1 | 710:b2e1a2660ec2 | 73 | * 128-bit UUID value at all times. |
rgrover1 | 710:b2e1a2660ec2 | 74 | * |
rgrover1 | 993:4d62b7967c11 | 75 | * @note We don't yet support 32-bit shortened UUIDs. |
rgrover1 | 710:b2e1a2660ec2 | 76 | */ |
rgrover1 | 911:885ee648c6ab | 77 | UUID(ShortUUIDBytes_t _shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(_shortUUID) { |
rgrover1 | 993:4d62b7967c11 | 78 | /* Empty */ |
rgrover1 | 710:b2e1a2660ec2 | 79 | } |
rgrover1 | 710:b2e1a2660ec2 | 80 | |
rgrover1 | 710:b2e1a2660ec2 | 81 | UUID(const UUID &source) { |
rgrover1 | 710:b2e1a2660ec2 | 82 | type = source.type; |
rgrover1 | 710:b2e1a2660ec2 | 83 | shortUUID = source.shortUUID; |
rgrover1 | 710:b2e1a2660ec2 | 84 | memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID); |
rgrover1 | 710:b2e1a2660ec2 | 85 | } |
rgrover1 | 710:b2e1a2660ec2 | 86 | |
rgrover1 | 710:b2e1a2660ec2 | 87 | UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) { |
rgrover1 | 710:b2e1a2660ec2 | 88 | /* empty */ |
rgrover1 | 710:b2e1a2660ec2 | 89 | } |
rgrover1 | 710:b2e1a2660ec2 | 90 | |
rgrover1 | 710:b2e1a2660ec2 | 91 | /** |
rgrover1 | 993:4d62b7967c11 | 92 | * Fill in a 128-bit UUID; this is useful when the UUID isn't known at the time of the object construction. |
rgrover1 | 710:b2e1a2660ec2 | 93 | */ |
rgrover1 | 710:b2e1a2660ec2 | 94 | void setupLong(const LongUUIDBytes_t longUUID) { |
rgrover1 | 710:b2e1a2660ec2 | 95 | type = UUID_TYPE_LONG; |
rgrover1 | 710:b2e1a2660ec2 | 96 | memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID); |
rgrover1 | 1023:a072b59caddb | 97 | shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3])); |
rgrover1 | 710:b2e1a2660ec2 | 98 | } |
rgrover1 | 710:b2e1a2660ec2 | 99 | |
rgrover1 | 710:b2e1a2660ec2 | 100 | public: |
rgrover1 | 710:b2e1a2660ec2 | 101 | UUID_Type_t shortOrLong(void) const {return type; } |
rgrover1 | 710:b2e1a2660ec2 | 102 | const uint8_t *getBaseUUID(void) const { |
rgrover1 | 710:b2e1a2660ec2 | 103 | if (type == UUID_TYPE_SHORT) { |
rgrover1 | 710:b2e1a2660ec2 | 104 | return (const uint8_t*)&shortUUID; |
rgrover1 | 710:b2e1a2660ec2 | 105 | } else { |
rgrover1 | 710:b2e1a2660ec2 | 106 | return baseUUID; |
rgrover1 | 710:b2e1a2660ec2 | 107 | } |
rgrover1 | 710:b2e1a2660ec2 | 108 | } |
rgrover1 | 710:b2e1a2660ec2 | 109 | |
rgrover1 | 710:b2e1a2660ec2 | 110 | ShortUUIDBytes_t getShortUUID(void) const {return shortUUID;} |
rgrover1 | 710:b2e1a2660ec2 | 111 | uint8_t getLen(void) const { |
rgrover1 | 710:b2e1a2660ec2 | 112 | return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID); |
rgrover1 | 710:b2e1a2660ec2 | 113 | } |
rgrover1 | 710:b2e1a2660ec2 | 114 | |
rgrover1 | 710:b2e1a2660ec2 | 115 | bool operator== (const UUID &other) const { |
rgrover1 | 710:b2e1a2660ec2 | 116 | if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) && |
rgrover1 | 710:b2e1a2660ec2 | 117 | (this->shortUUID == other.shortUUID)) { |
rgrover1 | 710:b2e1a2660ec2 | 118 | return true; |
rgrover1 | 710:b2e1a2660ec2 | 119 | } |
rgrover1 | 710:b2e1a2660ec2 | 120 | |
rgrover1 | 710:b2e1a2660ec2 | 121 | if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) && |
rgrover1 | 710:b2e1a2660ec2 | 122 | (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) { |
rgrover1 | 710:b2e1a2660ec2 | 123 | return true; |
rgrover1 | 710:b2e1a2660ec2 | 124 | } |
rgrover1 | 710:b2e1a2660ec2 | 125 | |
rgrover1 | 710:b2e1a2660ec2 | 126 | return false; |
rgrover1 | 710:b2e1a2660ec2 | 127 | } |
rgrover1 | 710:b2e1a2660ec2 | 128 | |
rgrover1 | 710:b2e1a2660ec2 | 129 | bool operator!= (const UUID &other) const { |
rgrover1 | 710:b2e1a2660ec2 | 130 | return !(*this == other); |
rgrover1 | 710:b2e1a2660ec2 | 131 | } |
rgrover1 | 710:b2e1a2660ec2 | 132 | |
rgrover1 | 710:b2e1a2660ec2 | 133 | private: |
rgrover1 | 710:b2e1a2660ec2 | 134 | UUID_Type_t type; // UUID_TYPE_SHORT or UUID_TYPE_LONG |
rgrover1 | 993:4d62b7967c11 | 135 | LongUUIDBytes_t baseUUID; /* The base of the long UUID (if |
rgrover1 | 710:b2e1a2660ec2 | 136 | * used). Note: bytes 12 and 13 (counting from LSB) |
rgrover1 | 710:b2e1a2660ec2 | 137 | * are zeroed out to allow comparison with other long |
rgrover1 | 993:4d62b7967c11 | 138 | * UUIDs, which differ only in the 16-bit relative |
rgrover1 | 710:b2e1a2660ec2 | 139 | * part.*/ |
rgrover1 | 993:4d62b7967c11 | 140 | ShortUUIDBytes_t shortUUID; // 16 bit UUID (byte 2-3 using with base). |
rgrover1 | 710:b2e1a2660ec2 | 141 | }; |
rgrover1 | 710:b2e1a2660ec2 | 142 | |
rgrover1 | 710:b2e1a2660ec2 | 143 | #endif // ifndef __UUID_H__ |