BLE_API_Tiny_BLE

Dependents:   CSSE4011_BLE_IMU

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Wed Apr 15 09:05:10 2015 +0100
Revision:
332:1dc2f80b4710
Parent:
303:a0d78cb2e9ef
Child:
339:1a56af0b1d71
Synchronized with git rev 18e4b666
Author: Rohit Grover
merge UUID.cpp into UUID.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 302:8f4348c2b6b5 1 /* mbed Microcontroller Library
rgrover1 302:8f4348c2b6b5 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 302:8f4348c2b6b5 3 *
rgrover1 302:8f4348c2b6b5 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 302:8f4348c2b6b5 5 * you may not use this file except in compliance with the License.
rgrover1 302:8f4348c2b6b5 6 * You may obtain a copy of the License at
rgrover1 302:8f4348c2b6b5 7 *
rgrover1 302:8f4348c2b6b5 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 302:8f4348c2b6b5 9 *
rgrover1 302:8f4348c2b6b5 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 302:8f4348c2b6b5 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 302:8f4348c2b6b5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 302:8f4348c2b6b5 13 * See the License for the specific language governing permissions and
rgrover1 302:8f4348c2b6b5 14 * limitations under the License.
rgrover1 302:8f4348c2b6b5 15 */
rgrover1 302:8f4348c2b6b5 16
rgrover1 302:8f4348c2b6b5 17 #ifndef __UUID_H__
rgrover1 302:8f4348c2b6b5 18 #define __UUID_H__
rgrover1 302:8f4348c2b6b5 19
rgrover1 302:8f4348c2b6b5 20 #include "blecommon.h"
rgrover1 302:8f4348c2b6b5 21
rgrover1 302:8f4348c2b6b5 22 const unsigned LENGTH_OF_LONG_UUID = 16;
rgrover1 302:8f4348c2b6b5 23 typedef uint16_t ShortUUIDBytes_t;
rgrover1 302:8f4348c2b6b5 24 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
rgrover1 302:8f4348c2b6b5 25
rgrover1 302:8f4348c2b6b5 26 class UUID {
rgrover1 302:8f4348c2b6b5 27 public:
rgrover1 303:a0d78cb2e9ef 28 enum UUID_Type_t {
rgrover1 302:8f4348c2b6b5 29 UUID_TYPE_SHORT = 0, // Short BLE UUID
rgrover1 302:8f4348c2b6b5 30 UUID_TYPE_LONG = 1 // Full 128-bit UUID
rgrover1 302:8f4348c2b6b5 31 };
rgrover1 302:8f4348c2b6b5 32
rgrover1 302:8f4348c2b6b5 33 public:
rgrover1 332:1dc2f80b4710 34 /**
rgrover1 332:1dc2f80b4710 35 * Creates a new 128-bit UUID
rgrover1 332:1dc2f80b4710 36 *
rgrover1 332:1dc2f80b4710 37 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
rgrover1 332:1dc2f80b4710 38 * different service or characteristics on the BLE device.
rgrover1 332:1dc2f80b4710 39 *
rgrover1 332:1dc2f80b4710 40 * @param longUUID
rgrover1 332:1dc2f80b4710 41 * The 128-bit (16-byte) UUID value.
rgrover1 332:1dc2f80b4710 42 */
rgrover1 332:1dc2f80b4710 43 UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
rgrover1 332:1dc2f80b4710 44 memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
rgrover1 332:1dc2f80b4710 45 shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
rgrover1 332:1dc2f80b4710 46 }
rgrover1 332:1dc2f80b4710 47
rgrover1 332:1dc2f80b4710 48 /**
rgrover1 332:1dc2f80b4710 49 * Creates a new 16-bit UUID
rgrover1 332:1dc2f80b4710 50 *
rgrover1 332:1dc2f80b4710 51 * @note The UUID is a unique 16-bit (2 byte) ID used to identify
rgrover1 332:1dc2f80b4710 52 * different service or characteristics on the BLE device.
rgrover1 332:1dc2f80b4710 53 *
rgrover1 332:1dc2f80b4710 54 * For efficiency, and because 16 bytes would take a large chunk of the
rgrover1 332:1dc2f80b4710 55 * 27-byte data payload length of the Link Layer, the BLE specification adds
rgrover1 332:1dc2f80b4710 56 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened
rgrover1 332:1dc2f80b4710 57 * formats can be used only with UUIDs that are defined in the Bluetooth
rgrover1 332:1dc2f80b4710 58 * specification (i.e., that are listed by the Bluetooth SIG as standard
rgrover1 332:1dc2f80b4710 59 * Bluetooth UUIDs).
rgrover1 332:1dc2f80b4710 60 *
rgrover1 332:1dc2f80b4710 61 * To reconstruct the full 128-bit UUID from the shortened version, insert
rgrover1 332:1dc2f80b4710 62 * the 16-bit short value (indicated by xxxx, including leading zeros) into
rgrover1 332:1dc2f80b4710 63 * the Bluetooth Base UUID:
rgrover1 332:1dc2f80b4710 64 *
rgrover1 332:1dc2f80b4710 65 * 0000xxxx-0000-1000-8000-00805F9B34FB
rgrover1 332:1dc2f80b4710 66 *
rgrover1 332:1dc2f80b4710 67 * @note Shortening is not available for UUIDs that are not derived from the
rgrover1 332:1dc2f80b4710 68 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called
rgrover1 332:1dc2f80b4710 69 * vendor-specific UUIDs. In these cases, you’ll need to use the full
rgrover1 332:1dc2f80b4710 70 * 128-bit UUID value at all times.
rgrover1 332:1dc2f80b4710 71 *
rgrover1 332:1dc2f80b4710 72 * @note we don't yet support 32-bit shortened UUIDs.
rgrover1 332:1dc2f80b4710 73 */
rgrover1 332:1dc2f80b4710 74 UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
rgrover1 332:1dc2f80b4710 75 /* empty */
rgrover1 332:1dc2f80b4710 76 }
rgrover1 302:8f4348c2b6b5 77
rgrover1 302:8f4348c2b6b5 78 public:
rgrover1 303:a0d78cb2e9ef 79 UUID_Type_t shortOrLong(void) const {return type; }
rgrover1 302:8f4348c2b6b5 80 const uint8_t *getBaseUUID(void) const {
rgrover1 303:a0d78cb2e9ef 81 if (type == UUID_TYPE_SHORT) {
rgrover1 303:a0d78cb2e9ef 82 return (const uint8_t*)&shortUUID;
rgrover1 303:a0d78cb2e9ef 83 } else {
rgrover1 303:a0d78cb2e9ef 84 return baseUUID;
rgrover1 303:a0d78cb2e9ef 85 }
rgrover1 302:8f4348c2b6b5 86 }
rgrover1 332:1dc2f80b4710 87
rgrover1 302:8f4348c2b6b5 88 ShortUUIDBytes_t getShortUUID(void) const {return shortUUID;}
rgrover1 302:8f4348c2b6b5 89 uint8_t getLen(void) const {
rgrover1 302:8f4348c2b6b5 90 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
rgrover1 302:8f4348c2b6b5 91 }
rgrover1 302:8f4348c2b6b5 92
rgrover1 332:1dc2f80b4710 93 bool operator== (const UUID &other) const {
rgrover1 332:1dc2f80b4710 94 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
rgrover1 332:1dc2f80b4710 95 (this->shortUUID == other.shortUUID)) {
rgrover1 332:1dc2f80b4710 96 return true;
rgrover1 332:1dc2f80b4710 97 }
rgrover1 332:1dc2f80b4710 98
rgrover1 332:1dc2f80b4710 99 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) &&
rgrover1 332:1dc2f80b4710 100 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
rgrover1 332:1dc2f80b4710 101 return true;
rgrover1 332:1dc2f80b4710 102 }
rgrover1 332:1dc2f80b4710 103
rgrover1 332:1dc2f80b4710 104 return false;
rgrover1 332:1dc2f80b4710 105 }
rgrover1 302:8f4348c2b6b5 106
rgrover1 302:8f4348c2b6b5 107 private:
rgrover1 303:a0d78cb2e9ef 108 UUID_Type_t type; // UUID_TYPE_SHORT or UUID_TYPE_LONG
rgrover1 302:8f4348c2b6b5 109 LongUUIDBytes_t baseUUID; /* the base of the long UUID (if
rgrover1 302:8f4348c2b6b5 110 * used). Note: bytes 12 and 13 (counting from LSB)
rgrover1 302:8f4348c2b6b5 111 * are zeroed out to allow comparison with other long
rgrover1 302:8f4348c2b6b5 112 * UUIDs which differ only in the 16-bit relative
rgrover1 302:8f4348c2b6b5 113 * part.*/
rgrover1 302:8f4348c2b6b5 114 ShortUUIDBytes_t shortUUID; // 16 bit uuid (byte 2-3 using with base)
rgrover1 302:8f4348c2b6b5 115 };
rgrover1 302:8f4348c2b6b5 116
rgrover1 302:8f4348c2b6b5 117 #endif // ifndef __UUID_H__