Example for Bluetooth low energy interface

Dependencies:   mbed HC_SR04_Ultrasonic_Library

Committer:
julientiron
Date:
Tue May 19 20:42:25 2015 +0000
Revision:
3:829f081fde15
Parent:
0:3d641e170a74
hihi;

Who changed what in which revision?

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