High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Jun 19 15:52:49 2015 +0100
Revision:
569:a5a6a1681fcf
Parent:
567:e4b38e43de7c
Child:
575:76fe165fc0b1
Synchronized with git rev 8d3e0929
Author: Rohit Grover
add a missing include for GapScanningParams.h

Who changed what in which revision?

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