6/22 上下往復完成版

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge_byYUTAKA3 by Maiko Matsumoto

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:76dfa9657d9d 1 /* mbed Microcontroller Library
jksoft 0:76dfa9657d9d 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 0:76dfa9657d9d 3 *
jksoft 0:76dfa9657d9d 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:76dfa9657d9d 5 * you may not use this file except in compliance with the License.
jksoft 0:76dfa9657d9d 6 * You may obtain a copy of the License at
jksoft 0:76dfa9657d9d 7 *
jksoft 0:76dfa9657d9d 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:76dfa9657d9d 9 *
jksoft 0:76dfa9657d9d 10 * Unless required by applicable law or agreed to in writing, software
jksoft 0:76dfa9657d9d 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:76dfa9657d9d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:76dfa9657d9d 13 * See the License for the specific language governing permissions and
jksoft 0:76dfa9657d9d 14 * limitations under the License.
jksoft 0:76dfa9657d9d 15 */
jksoft 0:76dfa9657d9d 16
jksoft 0:76dfa9657d9d 17 #include <stdio.h>
jksoft 0:76dfa9657d9d 18 #include <string.h>
jksoft 0:76dfa9657d9d 19
jksoft 0:76dfa9657d9d 20 #include "GapAdvertisingData.h"
jksoft 0:76dfa9657d9d 21
jksoft 0:76dfa9657d9d 22 /**************************************************************************/
jksoft 0:76dfa9657d9d 23 /*!
jksoft 0:76dfa9657d9d 24 \brief Creates a new GapAdvertisingData instance
jksoft 0:76dfa9657d9d 25
jksoft 0:76dfa9657d9d 26 \par EXAMPLE
jksoft 0:76dfa9657d9d 27
jksoft 0:76dfa9657d9d 28 \code
jksoft 0:76dfa9657d9d 29
jksoft 0:76dfa9657d9d 30 \endcode
jksoft 0:76dfa9657d9d 31 */
jksoft 0:76dfa9657d9d 32 /**************************************************************************/
jksoft 0:76dfa9657d9d 33 GapAdvertisingData::GapAdvertisingData(void) : _payload(), _payloadLen(0), _appearance(GENERIC_TAG) {
jksoft 0:76dfa9657d9d 34 /* empty */
jksoft 0:76dfa9657d9d 35 }
jksoft 0:76dfa9657d9d 36
jksoft 0:76dfa9657d9d 37 /**************************************************************************/
jksoft 0:76dfa9657d9d 38 /*!
jksoft 0:76dfa9657d9d 39 Destructor
jksoft 0:76dfa9657d9d 40 */
jksoft 0:76dfa9657d9d 41 /**************************************************************************/
jksoft 0:76dfa9657d9d 42 GapAdvertisingData::~GapAdvertisingData(void)
jksoft 0:76dfa9657d9d 43 {
jksoft 0:76dfa9657d9d 44 }
jksoft 0:76dfa9657d9d 45
jksoft 0:76dfa9657d9d 46 /**************************************************************************/
jksoft 0:76dfa9657d9d 47 /*!
jksoft 0:76dfa9657d9d 48 \brief Adds advertising data based on the specified AD type (see
jksoft 0:76dfa9657d9d 49 DataType)
jksoft 0:76dfa9657d9d 50
jksoft 0:76dfa9657d9d 51 \args[in] advDataType The Advertising 'DataType' to add
jksoft 0:76dfa9657d9d 52 \args[in] payload Pointer to the payload contents
jksoft 0:76dfa9657d9d 53 \args[in] len Size of the payload in bytes
jksoft 0:76dfa9657d9d 54
jksoft 0:76dfa9657d9d 55 \returns ble_error_t
jksoft 0:76dfa9657d9d 56
jksoft 0:76dfa9657d9d 57 \retval BLE_ERROR_NONE
jksoft 0:76dfa9657d9d 58 Everything executed properly
jksoft 0:76dfa9657d9d 59
jksoft 0:76dfa9657d9d 60 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:76dfa9657d9d 61 The specified data would cause the advertising buffer
jksoft 0:76dfa9657d9d 62 to overflow
jksoft 0:76dfa9657d9d 63
jksoft 0:76dfa9657d9d 64 \par EXAMPLE
jksoft 0:76dfa9657d9d 65
jksoft 0:76dfa9657d9d 66 \code
jksoft 0:76dfa9657d9d 67
jksoft 0:76dfa9657d9d 68 \endcode
jksoft 0:76dfa9657d9d 69 */
jksoft 0:76dfa9657d9d 70 /**************************************************************************/
jksoft 0:76dfa9657d9d 71 ble_error_t GapAdvertisingData::addData(DataType advDataType, const uint8_t *payload, uint8_t len)
jksoft 0:76dfa9657d9d 72 {
jksoft 0:76dfa9657d9d 73 /* ToDo: Check if an AD type already exists and if the existing */
jksoft 0:76dfa9657d9d 74 /* value is exclusive or not (flags, etc.) */
jksoft 0:76dfa9657d9d 75
jksoft 0:76dfa9657d9d 76 /* Make sure we don't exceed the 31 byte payload limit */
jksoft 0:76dfa9657d9d 77 if (_payloadLen + len + 2 > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
jksoft 0:76dfa9657d9d 78 return BLE_ERROR_BUFFER_OVERFLOW;
jksoft 0:76dfa9657d9d 79 }
jksoft 0:76dfa9657d9d 80
jksoft 0:76dfa9657d9d 81 /* Field length */
jksoft 0:76dfa9657d9d 82 memset(&_payload[_payloadLen], len + 1, 1);
jksoft 0:76dfa9657d9d 83 _payloadLen++;
jksoft 0:76dfa9657d9d 84
jksoft 0:76dfa9657d9d 85 /* Field ID */
jksoft 0:76dfa9657d9d 86 memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
jksoft 0:76dfa9657d9d 87 _payloadLen++;
jksoft 0:76dfa9657d9d 88
jksoft 0:76dfa9657d9d 89 /* Payload */
jksoft 0:76dfa9657d9d 90 memcpy(&_payload[_payloadLen], payload, len);
jksoft 0:76dfa9657d9d 91 _payloadLen += len;
jksoft 0:76dfa9657d9d 92
jksoft 0:76dfa9657d9d 93 return BLE_ERROR_NONE;
jksoft 0:76dfa9657d9d 94 }
jksoft 0:76dfa9657d9d 95
jksoft 0:76dfa9657d9d 96 /**************************************************************************/
jksoft 0:76dfa9657d9d 97 /*!
jksoft 0:76dfa9657d9d 98 \brief Helper function to add APPEARANCE data to the advertising
jksoft 0:76dfa9657d9d 99 payload
jksoft 0:76dfa9657d9d 100
jksoft 0:76dfa9657d9d 101 \args[in] appearance The APPEARANCE value to add
jksoft 0:76dfa9657d9d 102
jksoft 0:76dfa9657d9d 103 \returns ble_error_t
jksoft 0:76dfa9657d9d 104
jksoft 0:76dfa9657d9d 105 \retval BLE_ERROR_NONE
jksoft 0:76dfa9657d9d 106 Everything executed properly
jksoft 0:76dfa9657d9d 107
jksoft 0:76dfa9657d9d 108 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:76dfa9657d9d 109 The specified data would cause the advertising buffer
jksoft 0:76dfa9657d9d 110 to overflow
jksoft 0:76dfa9657d9d 111
jksoft 0:76dfa9657d9d 112 \par EXAMPLE
jksoft 0:76dfa9657d9d 113
jksoft 0:76dfa9657d9d 114 \code
jksoft 0:76dfa9657d9d 115
jksoft 0:76dfa9657d9d 116 \endcode
jksoft 0:76dfa9657d9d 117 */
jksoft 0:76dfa9657d9d 118 /**************************************************************************/
jksoft 0:76dfa9657d9d 119 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
jksoft 0:76dfa9657d9d 120 {
jksoft 0:76dfa9657d9d 121 _appearance = appearance;
jksoft 0:76dfa9657d9d 122 return addData(GapAdvertisingData::APPEARANCE, (uint8_t *)&appearance, 2);
jksoft 0:76dfa9657d9d 123 }
jksoft 0:76dfa9657d9d 124
jksoft 0:76dfa9657d9d 125 /**************************************************************************/
jksoft 0:76dfa9657d9d 126 /*!
jksoft 0:76dfa9657d9d 127 \brief Helper function to add FLAGS data to the advertising
jksoft 0:76dfa9657d9d 128 payload
jksoft 0:76dfa9657d9d 129
jksoft 0:76dfa9657d9d 130 \args[in] flag The FLAGS value to add
jksoft 0:76dfa9657d9d 131
jksoft 0:76dfa9657d9d 132 \par LE_LIMITED_DISCOVERABLE
jksoft 0:76dfa9657d9d 133 The peripheral is discoverable for a limited period of
jksoft 0:76dfa9657d9d 134 time
jksoft 0:76dfa9657d9d 135
jksoft 0:76dfa9657d9d 136 \par LE_GENERAL_DISCOVERABLE
jksoft 0:76dfa9657d9d 137 The peripheral is permanently discoverable
jksoft 0:76dfa9657d9d 138
jksoft 0:76dfa9657d9d 139 \par BREDR_NOT_SUPPORTED
jksoft 0:76dfa9657d9d 140 This peripheral is a Bluetooth Low Energy only device
jksoft 0:76dfa9657d9d 141 (no EDR support)
jksoft 0:76dfa9657d9d 142
jksoft 0:76dfa9657d9d 143 \returns ble_error_t
jksoft 0:76dfa9657d9d 144
jksoft 0:76dfa9657d9d 145 \retval BLE_ERROR_NONE
jksoft 0:76dfa9657d9d 146 Everything executed properly
jksoft 0:76dfa9657d9d 147
jksoft 0:76dfa9657d9d 148 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:76dfa9657d9d 149 The specified data would cause the advertising buffer
jksoft 0:76dfa9657d9d 150 to overflow
jksoft 0:76dfa9657d9d 151
jksoft 0:76dfa9657d9d 152 \par EXAMPLE
jksoft 0:76dfa9657d9d 153
jksoft 0:76dfa9657d9d 154 \code
jksoft 0:76dfa9657d9d 155
jksoft 0:76dfa9657d9d 156 \endcode
jksoft 0:76dfa9657d9d 157 */
jksoft 0:76dfa9657d9d 158 /**************************************************************************/
jksoft 0:76dfa9657d9d 159 ble_error_t GapAdvertisingData::addFlags(uint8_t flags)
jksoft 0:76dfa9657d9d 160 {
jksoft 0:76dfa9657d9d 161 return addData(GapAdvertisingData::FLAGS, &flags, 1);
jksoft 0:76dfa9657d9d 162 }
jksoft 0:76dfa9657d9d 163
jksoft 0:76dfa9657d9d 164 /**************************************************************************/
jksoft 0:76dfa9657d9d 165 /*!
jksoft 0:76dfa9657d9d 166 \brief Helper function to add TX_POWER_LEVEL data to the
jksoft 0:76dfa9657d9d 167 advertising payload
jksoft 0:76dfa9657d9d 168
jksoft 0:76dfa9657d9d 169 \args[in] flag The TX_POWER_LEVEL value to add
jksoft 0:76dfa9657d9d 170
jksoft 0:76dfa9657d9d 171 \returns ble_error_t
jksoft 0:76dfa9657d9d 172
jksoft 0:76dfa9657d9d 173 \retval BLE_ERROR_NONE
jksoft 0:76dfa9657d9d 174 Everything executed properly
jksoft 0:76dfa9657d9d 175
jksoft 0:76dfa9657d9d 176 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:76dfa9657d9d 177 The specified data would cause the advertising buffer
jksoft 0:76dfa9657d9d 178 to overflow
jksoft 0:76dfa9657d9d 179
jksoft 0:76dfa9657d9d 180 \par EXAMPLE
jksoft 0:76dfa9657d9d 181
jksoft 0:76dfa9657d9d 182 \code
jksoft 0:76dfa9657d9d 183
jksoft 0:76dfa9657d9d 184 \endcode
jksoft 0:76dfa9657d9d 185 */
jksoft 0:76dfa9657d9d 186 /**************************************************************************/
jksoft 0:76dfa9657d9d 187 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
jksoft 0:76dfa9657d9d 188 {
jksoft 0:76dfa9657d9d 189 /* ToDo: Basic error checking to make sure txPower is in range */
jksoft 0:76dfa9657d9d 190 return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t *)&txPower, 1);
jksoft 0:76dfa9657d9d 191 }
jksoft 0:76dfa9657d9d 192
jksoft 0:76dfa9657d9d 193 /**************************************************************************/
jksoft 0:76dfa9657d9d 194 /*!
jksoft 0:76dfa9657d9d 195 \brief Clears the payload and resets the payload length counter
jksoft 0:76dfa9657d9d 196 */
jksoft 0:76dfa9657d9d 197 /**************************************************************************/
jksoft 0:76dfa9657d9d 198 void GapAdvertisingData::clear(void)
jksoft 0:76dfa9657d9d 199 {
jksoft 0:76dfa9657d9d 200 memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
jksoft 0:76dfa9657d9d 201 _payloadLen = 0;
jksoft 0:76dfa9657d9d 202 }
jksoft 0:76dfa9657d9d 203
jksoft 0:76dfa9657d9d 204 /**************************************************************************/
jksoft 0:76dfa9657d9d 205 /*!
jksoft 0:76dfa9657d9d 206 \brief Returns a pointer to the the current payload
jksoft 0:76dfa9657d9d 207
jksoft 0:76dfa9657d9d 208 \returns A pointer to the payload
jksoft 0:76dfa9657d9d 209 */
jksoft 0:76dfa9657d9d 210 /**************************************************************************/
jksoft 0:76dfa9657d9d 211 const uint8_t *GapAdvertisingData::getPayload(void) const
jksoft 0:76dfa9657d9d 212 {
jksoft 0:76dfa9657d9d 213 return (_payloadLen > 0) ? _payload : NULL;
jksoft 0:76dfa9657d9d 214 }
jksoft 0:76dfa9657d9d 215
jksoft 0:76dfa9657d9d 216 /**************************************************************************/
jksoft 0:76dfa9657d9d 217 /*!
jksoft 0:76dfa9657d9d 218 \brief Returns the current payload length (0..31 bytes)
jksoft 0:76dfa9657d9d 219
jksoft 0:76dfa9657d9d 220 \returns The payload length in bytes
jksoft 0:76dfa9657d9d 221 */
jksoft 0:76dfa9657d9d 222 /**************************************************************************/
jksoft 0:76dfa9657d9d 223 uint8_t GapAdvertisingData::getPayloadLen(void) const
jksoft 0:76dfa9657d9d 224 {
jksoft 0:76dfa9657d9d 225 return _payloadLen;
jksoft 0:76dfa9657d9d 226 }
jksoft 0:76dfa9657d9d 227
jksoft 0:76dfa9657d9d 228 /**************************************************************************/
jksoft 0:76dfa9657d9d 229 /*!
jksoft 0:76dfa9657d9d 230 \brief Returns the 16-bit appearance value for this device
jksoft 0:76dfa9657d9d 231
jksoft 0:76dfa9657d9d 232 \returns The 16-bit appearance value
jksoft 0:76dfa9657d9d 233 */
jksoft 0:76dfa9657d9d 234 /**************************************************************************/
jksoft 0:76dfa9657d9d 235 uint16_t GapAdvertisingData::getAppearance(void) const
jksoft 0:76dfa9657d9d 236 {
jksoft 0:76dfa9657d9d 237 return (uint16_t)_appearance;
jksoft 0:76dfa9657d9d 238 }