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:
Mon Mar 02 11:50:47 2015 +0000
Revision:
292:b5ee2ada4f33
Parent:
291:0b66dd15728e
Child:
293:6278354cfd98
Synchronized with git rev 8d1b53ed
Author: Jeremy Brodt
Added callback on characteristic reads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 292:b5ee2ada4f33 1 /* mbed Microcontroller Library
rgrover1 292:b5ee2ada4f33 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 292:b5ee2ada4f33 3 *
rgrover1 292:b5ee2ada4f33 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 292:b5ee2ada4f33 5 * you may not use this file except in compliance with the License.
rgrover1 292:b5ee2ada4f33 6 * You may obtain a copy of the License at
rgrover1 292:b5ee2ada4f33 7 *
rgrover1 292:b5ee2ada4f33 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 292:b5ee2ada4f33 9 *
rgrover1 292:b5ee2ada4f33 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 292:b5ee2ada4f33 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 292:b5ee2ada4f33 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 292:b5ee2ada4f33 13 * See the License for the specific language governing permissions and
rgrover1 292:b5ee2ada4f33 14 * limitations under the License.
rgrover1 292:b5ee2ada4f33 15 */
rgrover1 292:b5ee2ada4f33 16
rgrover1 292:b5ee2ada4f33 17 #include <string.h>
rgrover1 292:b5ee2ada4f33 18
rgrover1 292:b5ee2ada4f33 19 #include "UUID.h"
rgrover1 292:b5ee2ada4f33 20
rgrover1 292:b5ee2ada4f33 21 UUID::UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
rgrover1 292:b5ee2ada4f33 22 /* empty */
rgrover1 292:b5ee2ada4f33 23 }
rgrover1 292:b5ee2ada4f33 24
rgrover1 292:b5ee2ada4f33 25 /**************************************************************************/
rgrover1 292:b5ee2ada4f33 26 /*!
rgrover1 292:b5ee2ada4f33 27 @brief Creates a new 128-bit UUID
rgrover1 292:b5ee2ada4f33 28
rgrover1 292:b5ee2ada4f33 29 @note The UUID is a unique 128-bit (16 byte) ID used to identify
rgrover1 292:b5ee2ada4f33 30 different service or characteristics on the BLE device.
rgrover1 292:b5ee2ada4f33 31
rgrover1 292:b5ee2ada4f33 32 @note When creating a UUID, the constructor will check if all bytes
rgrover1 292:b5ee2ada4f33 33 except bytes 2/3 are equal to 0. If only bytes 2/3 have a
rgrover1 292:b5ee2ada4f33 34 value, the UUID will be treated as a short/BLE UUID, and the
rgrover1 292:b5ee2ada4f33 35 .type field will be set to UUID::UUID_TYPE_SHORT. If any
rgrover1 292:b5ee2ada4f33 36 of the bytes outside byte 2/3 have a non-zero value, the UUID
rgrover1 292:b5ee2ada4f33 37 will be considered a 128-bit ID, and .type will be assigned
rgrover1 292:b5ee2ada4f33 38 as UUID::UUID_TYPE_LONG.
rgrover1 292:b5ee2ada4f33 39
rgrover1 292:b5ee2ada4f33 40 @param[in] uuid_base
rgrover1 292:b5ee2ada4f33 41 The 128-bit (16-byte) UUID value. For 128-bit values,
rgrover1 292:b5ee2ada4f33 42 assign all 16 bytes. For 16-bit values, assign the
rgrover1 292:b5ee2ada4f33 43 16-bits to byte 2 and 3, and leave the rest of the bytes
rgrover1 292:b5ee2ada4f33 44 as 0.
rgrover1 292:b5ee2ada4f33 45
rgrover1 292:b5ee2ada4f33 46 @section EXAMPLE
rgrover1 292:b5ee2ada4f33 47
rgrover1 292:b5ee2ada4f33 48 @code
rgrover1 292:b5ee2ada4f33 49
rgrover1 292:b5ee2ada4f33 50 // Create a short UUID (0x180F)
rgrover1 292:b5ee2ada4f33 51 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
rgrover1 292:b5ee2ada4f33 52 UUID ble_uuid = UUID(shortID);
rgrover1 292:b5ee2ada4f33 53 // ble_uuid.type = UUID_TYPE_SHORT
rgrover1 292:b5ee2ada4f33 54 // ble_uuid.value = 0x180F
rgrover1 292:b5ee2ada4f33 55
rgrover1 292:b5ee2ada4f33 56 // Creeate a long UUID
rgrover1 292:b5ee2ada4f33 57 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
rgrover1 292:b5ee2ada4f33 58 0x44, 0x55, 0x66, 0x77,
rgrover1 292:b5ee2ada4f33 59 0x88, 0x99, 0xAA, 0xBB,
rgrover1 292:b5ee2ada4f33 60 0xCC, 0xDD, 0xEE, 0xFF };
rgrover1 292:b5ee2ada4f33 61 UUID custom_uuid = UUID(longID);
rgrover1 292:b5ee2ada4f33 62 // custom_uuid.type = UUID_TYPE_LONG
rgrover1 292:b5ee2ada4f33 63 // custom_uuid.value = 0x3322
rgrover1 292:b5ee2ada4f33 64 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
rgrover1 292:b5ee2ada4f33 65
rgrover1 292:b5ee2ada4f33 66 @endcode
rgrover1 292:b5ee2ada4f33 67 */
rgrover1 292:b5ee2ada4f33 68 /**************************************************************************/
rgrover1 292:b5ee2ada4f33 69 UUID::UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(0)
rgrover1 292:b5ee2ada4f33 70 {
rgrover1 292:b5ee2ada4f33 71 memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
rgrover1 292:b5ee2ada4f33 72 shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
rgrover1 292:b5ee2ada4f33 73
rgrover1 292:b5ee2ada4f33 74 /* Check if this is a short of a long UUID */
rgrover1 292:b5ee2ada4f33 75 unsigned index;
rgrover1 292:b5ee2ada4f33 76 for (index = 0; index < LENGTH_OF_LONG_UUID; index++) {
rgrover1 292:b5ee2ada4f33 77 if ((index == 2) || (index == 3)) {
rgrover1 292:b5ee2ada4f33 78 continue; /* we should not consider bytes 2 and 3 because that's
rgrover1 292:b5ee2ada4f33 79 * where the 16-bit relative UUID is placed. */
rgrover1 292:b5ee2ada4f33 80 }
rgrover1 292:b5ee2ada4f33 81
rgrover1 292:b5ee2ada4f33 82 if (baseUUID[index] != 0) {
rgrover1 292:b5ee2ada4f33 83 type = UUID_TYPE_LONG;
rgrover1 292:b5ee2ada4f33 84
rgrover1 292:b5ee2ada4f33 85 /* zero out the 16-bit part in the base; this will help equate long
rgrover1 292:b5ee2ada4f33 86 * UUIDs when they differ only in this 16-bit relative part.*/
rgrover1 292:b5ee2ada4f33 87 baseUUID[2] = 0;
rgrover1 292:b5ee2ada4f33 88 baseUUID[3] = 0;
rgrover1 292:b5ee2ada4f33 89
rgrover1 292:b5ee2ada4f33 90 return;
rgrover1 292:b5ee2ada4f33 91 }
rgrover1 292:b5ee2ada4f33 92 }
rgrover1 292:b5ee2ada4f33 93 }