High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/UUID.h@850:32ff6e392630, 2015-11-02 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Nov 02 09:09:05 2015 +0000
- Revision:
- 850:32ff6e392630
- Parent:
- 844:96aedb87cdd4
- Child:
- 851:802f445cc195
Synchronized with git rev 8aaa385e
Author: Johnny Robeson
use the github url public url in module.json
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 | 710:b2e1a2660ec2 | 28 | UUID_TYPE_SHORT = 0, // Short BLE UUID |
rgrover1 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 61 | * specification (i.e., that are 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 | 710:b2e1a2660ec2 | 75 | * @note we don't yet support 32-bit shortened UUIDs. |
rgrover1 | 710:b2e1a2660ec2 | 76 | */ |
rgrover1 | 850:32ff6e392630 | 77 | UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) { |
rgrover1 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 92 | * Fill in a 128-bit UUID; this is useful when UUID isn't known at the time of 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 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 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 | 710:b2e1a2660ec2 | 138 | * UUIDs which differ only in the 16-bit relative |
rgrover1 | 710:b2e1a2660ec2 | 139 | * part.*/ |
rgrover1 | 710:b2e1a2660ec2 | 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__ |