Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ghz2000
Date:
Mon Jun 16 15:06:48 2014 +0000
Revision:
20:a58a51c92075
Parent:
0:4c3097c65247
Add HeartRateMonitor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:4c3097c65247 1 /* mbed Microcontroller Library
ktownsend 0:4c3097c65247 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 0:4c3097c65247 3 *
ktownsend 0:4c3097c65247 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:4c3097c65247 5 * you may not use this file except in compliance with the License.
ktownsend 0:4c3097c65247 6 * You may obtain a copy of the License at
ktownsend 0:4c3097c65247 7 *
ktownsend 0:4c3097c65247 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:4c3097c65247 9 *
ktownsend 0:4c3097c65247 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:4c3097c65247 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:4c3097c65247 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:4c3097c65247 13 * See the License for the specific language governing permissions and
ktownsend 0:4c3097c65247 14 * limitations under the License.
ktownsend 0:4c3097c65247 15 */
ktownsend 0:4c3097c65247 16
ktownsend 0:4c3097c65247 17
ktownsend 0:4c3097c65247 18 #include <stdio.h>
ktownsend 0:4c3097c65247 19 #include <string.h>
ktownsend 0:4c3097c65247 20
ktownsend 0:4c3097c65247 21 #include "UUID.h"
ktownsend 0:4c3097c65247 22
ktownsend 0:4c3097c65247 23 /**************************************************************************/
ktownsend 0:4c3097c65247 24 /*!
ktownsend 0:4c3097c65247 25 @brief Creates an empty 128-bit UUID
ktownsend 0:4c3097c65247 26
ktownsend 0:4c3097c65247 27 @note This UUID must be assigned a valid value via the 'update'
ktownsend 0:4c3097c65247 28 function before it can be safely used!
ktownsend 0:4c3097c65247 29 */
ktownsend 0:4c3097c65247 30 /**************************************************************************/
ktownsend 0:4c3097c65247 31 UUID::UUID(void)
ktownsend 0:4c3097c65247 32 {
ktownsend 0:4c3097c65247 33 memset(base, 0, 16);
ktownsend 0:4c3097c65247 34 value = 0;
ktownsend 0:4c3097c65247 35 type = UUID_TYPE_SHORT;
ktownsend 0:4c3097c65247 36 }
ktownsend 0:4c3097c65247 37
ktownsend 0:4c3097c65247 38 /**************************************************************************/
ktownsend 0:4c3097c65247 39 /*!
ktownsend 0:4c3097c65247 40 @brief Creates a new 128-bit UUID
ktownsend 0:4c3097c65247 41
ktownsend 0:4c3097c65247 42 @note The UUID is a unique 128-bit (16 byte) ID used to identify
ktownsend 0:4c3097c65247 43 different service or characteristics on the BLE device.
ktownsend 0:4c3097c65247 44
ktownsend 0:4c3097c65247 45 @note When creating a UUID, the constructor will check if all bytes
ktownsend 0:4c3097c65247 46 except bytes 2/3 are equal to 0. If only bytes 2/3 have a
ktownsend 0:4c3097c65247 47 value, the UUID will be treated as a short/BLE UUID, and the
ktownsend 0:4c3097c65247 48 .type field will be set to UUID::UUID_TYPE_SHORT. If any
ktownsend 0:4c3097c65247 49 of the bytes outside byte 2/3 have a non-zero value, the UUID
ktownsend 0:4c3097c65247 50 will be considered a 128-bit ID, and .type will be assigned
ktownsend 0:4c3097c65247 51 as UUID::UUID_TYPE_LONG.
ktownsend 0:4c3097c65247 52
ktownsend 0:4c3097c65247 53 @param[in] uuid_base
ktownsend 0:4c3097c65247 54 The 128-bit (16-byte) UUID value. For 128-bit values,
ktownsend 0:4c3097c65247 55 assign all 16 bytes. For 16-bit values, assign the
ktownsend 0:4c3097c65247 56 16-bits to byte 2 and 3, and leave the rest of the bytes
ktownsend 0:4c3097c65247 57 as 0.
ktownsend 0:4c3097c65247 58
ktownsend 0:4c3097c65247 59 @section EXAMPLE
ktownsend 0:4c3097c65247 60
ktownsend 0:4c3097c65247 61 @code
ktownsend 0:4c3097c65247 62
ktownsend 0:4c3097c65247 63 // Create a short UUID (0x180F)
ktownsend 0:4c3097c65247 64 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ktownsend 0:4c3097c65247 65 UUID ble_uuid = UUID(shortID);
ktownsend 0:4c3097c65247 66 // ble_uuid.type = UUID_TYPE_SHORT
ktownsend 0:4c3097c65247 67 // ble_uuid.value = 0x180F
ktownsend 0:4c3097c65247 68
ktownsend 0:4c3097c65247 69 // Creeate a long UUID
ktownsend 0:4c3097c65247 70 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
ktownsend 0:4c3097c65247 71 0x44, 0x55, 0x66, 0x77,
ktownsend 0:4c3097c65247 72 0x88, 0x99, 0xAA, 0xBB,
ktownsend 0:4c3097c65247 73 0xCC, 0xDD, 0xEE, 0xFF };
ktownsend 0:4c3097c65247 74 UUID custom_uuid = UUID(longID);
ktownsend 0:4c3097c65247 75 // custom_uuid.type = UUID_TYPE_LONG
ktownsend 0:4c3097c65247 76 // custom_uuid.value = 0x3322
ktownsend 0:4c3097c65247 77 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
ktownsend 0:4c3097c65247 78
ktownsend 0:4c3097c65247 79 @endcode
ktownsend 0:4c3097c65247 80 */
ktownsend 0:4c3097c65247 81 /**************************************************************************/
ktownsend 0:4c3097c65247 82 UUID::UUID(uint8_t const uuid_base[16])
ktownsend 0:4c3097c65247 83 {
ktownsend 0:4c3097c65247 84 memcpy(base, uuid_base, 16);
ktownsend 0:4c3097c65247 85 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:4c3097c65247 86
ktownsend 0:4c3097c65247 87 /* Check if this is a short of a long UUID */
ktownsend 0:4c3097c65247 88 if (uuid_base[0] + uuid_base[1] +
ktownsend 0:4c3097c65247 89 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
ktownsend 0:4c3097c65247 90 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
ktownsend 0:4c3097c65247 91 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0)
ktownsend 0:4c3097c65247 92 {
ktownsend 0:4c3097c65247 93 type = UUID_TYPE_SHORT;
ktownsend 0:4c3097c65247 94 }
ktownsend 0:4c3097c65247 95 else
ktownsend 0:4c3097c65247 96 {
ktownsend 0:4c3097c65247 97 type = UUID_TYPE_LONG;
ktownsend 0:4c3097c65247 98 }
ktownsend 0:4c3097c65247 99 }
ktownsend 0:4c3097c65247 100
ktownsend 0:4c3097c65247 101 /**************************************************************************/
ktownsend 0:4c3097c65247 102 /*!
ktownsend 0:4c3097c65247 103 @brief Creates a short (16-bit) UUID
ktownsend 0:4c3097c65247 104
ktownsend 0:4c3097c65247 105 @param[in] ble_uuid
ktownsend 0:4c3097c65247 106 The 16-bit BLE UUID value.
ktownsend 0:4c3097c65247 107 */
ktownsend 0:4c3097c65247 108 /**************************************************************************/
ktownsend 0:4c3097c65247 109 UUID::UUID(uint16_t const ble_uuid)
ktownsend 0:4c3097c65247 110 {
ktownsend 0:4c3097c65247 111 memset(base, 0, 16);
ktownsend 0:4c3097c65247 112 memcpy(base+2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:4c3097c65247 113 value = ble_uuid;
ktownsend 0:4c3097c65247 114 type = UUID_TYPE_SHORT;
ktownsend 0:4c3097c65247 115 }
ktownsend 0:4c3097c65247 116
ktownsend 0:4c3097c65247 117 /**************************************************************************/
ktownsend 0:4c3097c65247 118 /*!
ktownsend 0:4c3097c65247 119 @brief UUID destructor
ktownsend 0:4c3097c65247 120 */
ktownsend 0:4c3097c65247 121 /**************************************************************************/
ktownsend 0:4c3097c65247 122 UUID::~UUID(void)
ktownsend 0:4c3097c65247 123 {
ktownsend 0:4c3097c65247 124 }
ktownsend 0:4c3097c65247 125
ktownsend 0:4c3097c65247 126 /**************************************************************************/
ktownsend 0:4c3097c65247 127 /*!
ktownsend 0:4c3097c65247 128 @brief Updates the value of the UUID
ktownsend 0:4c3097c65247 129
ktownsend 0:4c3097c65247 130 @args[in] uuid_base
ktownsend 0:4c3097c65247 131 The 128-bit value to use when updating the UUID. For
ktownsend 0:4c3097c65247 132 16-bit IDs, insert the ID in bytes 2/3 in LSB format.
ktownsend 0:4c3097c65247 133
ktownsend 0:4c3097c65247 134 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:4c3097c65247 135 error code if there was a problem
ktownsend 0:4c3097c65247 136 @retval BLE_ERROR_NONE
ktownsend 0:4c3097c65247 137 Everything executed correctly
ktownsend 0:4c3097c65247 138
ktownsend 0:4c3097c65247 139 @section EXAMPLE
ktownsend 0:4c3097c65247 140
ktownsend 0:4c3097c65247 141 @code
ktownsend 0:4c3097c65247 142
ktownsend 0:4c3097c65247 143 @endcode
ktownsend 0:4c3097c65247 144 */
ktownsend 0:4c3097c65247 145 /**************************************************************************/
ktownsend 0:4c3097c65247 146 ble_error_t UUID::update(uint8_t const uuid_base[16])
ktownsend 0:4c3097c65247 147 {
ktownsend 0:4c3097c65247 148 memcpy(base, uuid_base, 16);
ktownsend 0:4c3097c65247 149 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:4c3097c65247 150
ktownsend 0:4c3097c65247 151 /* Check if this is a short of a long UUID */
ktownsend 0:4c3097c65247 152 if (uuid_base[0] + uuid_base[1] +
ktownsend 0:4c3097c65247 153 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
ktownsend 0:4c3097c65247 154 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
ktownsend 0:4c3097c65247 155 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0)
ktownsend 0:4c3097c65247 156 {
ktownsend 0:4c3097c65247 157 type = UUID_TYPE_SHORT;
ktownsend 0:4c3097c65247 158 }
ktownsend 0:4c3097c65247 159 else
ktownsend 0:4c3097c65247 160 {
ktownsend 0:4c3097c65247 161 type = UUID_TYPE_LONG;
ktownsend 0:4c3097c65247 162 }
ktownsend 0:4c3097c65247 163
ktownsend 0:4c3097c65247 164 return BLE_ERROR_NONE;
ktownsend 0:4c3097c65247 165 }
ktownsend 0:4c3097c65247 166
ktownsend 0:4c3097c65247 167 /**************************************************************************/
ktownsend 0:4c3097c65247 168 /*!
ktownsend 0:4c3097c65247 169 @brief Updates the value of the UUID
ktownsend 0:4c3097c65247 170
ktownsend 0:4c3097c65247 171 @args[in] ble_uuid
ktownsend 0:4c3097c65247 172 The 16-bit value to use when updating the UUID.
ktownsend 0:4c3097c65247 173
ktownsend 0:4c3097c65247 174 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:4c3097c65247 175 error code if there was a problem
ktownsend 0:4c3097c65247 176 @retval BLE_ERROR_NONE
ktownsend 0:4c3097c65247 177 Everything executed correctly
ktownsend 0:4c3097c65247 178
ktownsend 0:4c3097c65247 179 @section EXAMPLE
ktownsend 0:4c3097c65247 180
ktownsend 0:4c3097c65247 181 @code
ktownsend 0:4c3097c65247 182
ktownsend 0:4c3097c65247 183 @endcode
ktownsend 0:4c3097c65247 184 */
ktownsend 0:4c3097c65247 185 /**************************************************************************/
ktownsend 0:4c3097c65247 186 ble_error_t UUID::update(uint16_t const ble_uuid)
ktownsend 0:4c3097c65247 187 {
ktownsend 0:4c3097c65247 188 memset(base, 0, 16);
ktownsend 0:4c3097c65247 189 memcpy(base+2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:4c3097c65247 190 value = ble_uuid;
ktownsend 0:4c3097c65247 191 type = UUID_TYPE_SHORT;
ktownsend 0:4c3097c65247 192
ktownsend 0:4c3097c65247 193 return BLE_ERROR_NONE;
ktownsend 0:4c3097c65247 194 }