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