BLE temperature profile using digital DS1820 or analog LM35 sensors

Dependencies:   DS1820

Committer:
gkroussos
Date:
Sat Mar 07 16:23:41 2015 +0000
Revision:
0:637031152314
Working version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gkroussos 0:637031152314 1 /* mbed Microcontroller Library
gkroussos 0:637031152314 2 * Copyright (c) 2006-2013 ARM Limited
gkroussos 0:637031152314 3 *
gkroussos 0:637031152314 4 * Licensed under the Apache License, Version 2.0 (the "License");
gkroussos 0:637031152314 5 * you may not use this file except in compliance with the License.
gkroussos 0:637031152314 6 * You may obtain a copy of the License at
gkroussos 0:637031152314 7 *
gkroussos 0:637031152314 8 * http://www.apache.org/licenses/LICENSE-2.0
gkroussos 0:637031152314 9 *
gkroussos 0:637031152314 10 * Unless required by applicable law or agreed to in writing, software
gkroussos 0:637031152314 11 * distributed under the License is distributed on an "AS IS" BASIS,
gkroussos 0:637031152314 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
gkroussos 0:637031152314 13 * See the License for the specific language governing permissions and
gkroussos 0:637031152314 14 * limitations under the License.
gkroussos 0:637031152314 15 */
gkroussos 0:637031152314 16
gkroussos 0:637031152314 17 #include "nRF51Gap.h"
gkroussos 0:637031152314 18 #include "mbed.h"
gkroussos 0:637031152314 19
gkroussos 0:637031152314 20 #include "common/common.h"
gkroussos 0:637031152314 21 #include "ble_advdata.h"
gkroussos 0:637031152314 22
gkroussos 0:637031152314 23 /**************************************************************************/
gkroussos 0:637031152314 24 /*!
gkroussos 0:637031152314 25 @brief Sets the advertising parameters and payload for the device
gkroussos 0:637031152314 26
gkroussos 0:637031152314 27 @param[in] params
gkroussos 0:637031152314 28 Basic advertising details, including the advertising
gkroussos 0:637031152314 29 delay, timeout and how the device should be advertised
gkroussos 0:637031152314 30 @params[in] advData
gkroussos 0:637031152314 31 The primary advertising data payload
gkroussos 0:637031152314 32 @params[in] scanResponse
gkroussos 0:637031152314 33 The optional Scan Response payload if the advertising
gkroussos 0:637031152314 34 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
gkroussos 0:637031152314 35 in \ref GapAdveritinngParams
gkroussos 0:637031152314 36
gkroussos 0:637031152314 37 @returns \ref ble_error_t
gkroussos 0:637031152314 38
gkroussos 0:637031152314 39 @retval BLE_ERROR_NONE
gkroussos 0:637031152314 40 Everything executed properly
gkroussos 0:637031152314 41
gkroussos 0:637031152314 42 @retval BLE_ERROR_BUFFER_OVERFLOW
gkroussos 0:637031152314 43 The proposed action would cause a buffer overflow. All
gkroussos 0:637031152314 44 advertising payloads must be <= 31 bytes, for example.
gkroussos 0:637031152314 45
gkroussos 0:637031152314 46 @retval BLE_ERROR_NOT_IMPLEMENTED
gkroussos 0:637031152314 47 A feature was requested that is not yet supported in the
gkroussos 0:637031152314 48 nRF51 firmware or hardware.
gkroussos 0:637031152314 49
gkroussos 0:637031152314 50 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
gkroussos 0:637031152314 51 One of the proposed values is outside the valid range.
gkroussos 0:637031152314 52
gkroussos 0:637031152314 53 @section EXAMPLE
gkroussos 0:637031152314 54
gkroussos 0:637031152314 55 @code
gkroussos 0:637031152314 56
gkroussos 0:637031152314 57 @endcode
gkroussos 0:637031152314 58 */
gkroussos 0:637031152314 59 /**************************************************************************/
gkroussos 0:637031152314 60 ble_error_t nRF51Gap::setAdvertisingData(GapAdvertisingData & advData, GapAdvertisingData & scanResponse)
gkroussos 0:637031152314 61 {
gkroussos 0:637031152314 62 /* Make sure we don't exceed the advertising payload length */
gkroussos 0:637031152314 63 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
gkroussos 0:637031152314 64 {
gkroussos 0:637031152314 65 return BLE_ERROR_BUFFER_OVERFLOW;
gkroussos 0:637031152314 66 }
gkroussos 0:637031152314 67
gkroussos 0:637031152314 68 /* Make sure we have a payload! */
gkroussos 0:637031152314 69 if (advData.getPayloadLen() == 0)
gkroussos 0:637031152314 70 {
gkroussos 0:637031152314 71 return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 72 }
gkroussos 0:637031152314 73
gkroussos 0:637031152314 74 /* Check the scan response payload limits */
gkroussos 0:637031152314 75 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
gkroussos 0:637031152314 76 //{
gkroussos 0:637031152314 77 // /* Check if we're within the upper limit */
gkroussos 0:637031152314 78 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
gkroussos 0:637031152314 79 // {
gkroussos 0:637031152314 80 // return BLE_ERROR_BUFFER_OVERFLOW;
gkroussos 0:637031152314 81 // }
gkroussos 0:637031152314 82 // /* Make sure we have a payload! */
gkroussos 0:637031152314 83 // if (advData.getPayloadLen() == 0)
gkroussos 0:637031152314 84 // {
gkroussos 0:637031152314 85 // return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 86 // }
gkroussos 0:637031152314 87 //}
gkroussos 0:637031152314 88
gkroussos 0:637031152314 89 /* Send advertising data! */
gkroussos 0:637031152314 90 ASSERT( ERROR_NONE == sd_ble_gap_adv_data_set(advData.getPayload(), advData.getPayloadLen(),
gkroussos 0:637031152314 91 scanResponse.getPayload(), scanResponse.getPayloadLen()), BLE_ERROR_PARAM_OUT_OF_RANGE);
gkroussos 0:637031152314 92
gkroussos 0:637031152314 93 /* Make sure the GAP Service appearance value is aligned with the appearance from GapAdvertisingData */
gkroussos 0:637031152314 94 ASSERT( ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), BLE_ERROR_PARAM_OUT_OF_RANGE);
gkroussos 0:637031152314 95
gkroussos 0:637031152314 96 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
gkroussos 0:637031152314 97 /* contains a flags AD type, etc. */
gkroussos 0:637031152314 98
gkroussos 0:637031152314 99 return BLE_ERROR_NONE;
gkroussos 0:637031152314 100 }
gkroussos 0:637031152314 101
gkroussos 0:637031152314 102 /**************************************************************************/
gkroussos 0:637031152314 103 /*!
gkroussos 0:637031152314 104 @brief Starts the BLE HW, initialising any services that were
gkroussos 0:637031152314 105 added before this function was called.
gkroussos 0:637031152314 106
gkroussos 0:637031152314 107 @note All services must be added before calling this function!
gkroussos 0:637031152314 108
gkroussos 0:637031152314 109 @returns ble_error_t
gkroussos 0:637031152314 110
gkroussos 0:637031152314 111 @retval BLE_ERROR_NONE
gkroussos 0:637031152314 112 Everything executed properly
gkroussos 0:637031152314 113
gkroussos 0:637031152314 114 @section EXAMPLE
gkroussos 0:637031152314 115
gkroussos 0:637031152314 116 @code
gkroussos 0:637031152314 117
gkroussos 0:637031152314 118 @endcode
gkroussos 0:637031152314 119 */
gkroussos 0:637031152314 120 /**************************************************************************/
gkroussos 0:637031152314 121 ble_error_t nRF51Gap::startAdvertising(GapAdvertisingParams & params)
gkroussos 0:637031152314 122 {
gkroussos 0:637031152314 123 /* Make sure we support the advertising type */
gkroussos 0:637031152314 124 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
gkroussos 0:637031152314 125 {
gkroussos 0:637031152314 126 /* ToDo: This requires a propery security implementation, etc. */
gkroussos 0:637031152314 127 return BLE_ERROR_NOT_IMPLEMENTED;
gkroussos 0:637031152314 128 }
gkroussos 0:637031152314 129
gkroussos 0:637031152314 130 /* Check interval range */
gkroussos 0:637031152314 131 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED)
gkroussos 0:637031152314 132 {
gkroussos 0:637031152314 133 /* Min delay is slightly longer for unconnectable devices */
gkroussos 0:637031152314 134 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
gkroussos 0:637031152314 135 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
gkroussos 0:637031152314 136 {
gkroussos 0:637031152314 137 return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 138 }
gkroussos 0:637031152314 139 }
gkroussos 0:637031152314 140 else
gkroussos 0:637031152314 141 {
gkroussos 0:637031152314 142 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
gkroussos 0:637031152314 143 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
gkroussos 0:637031152314 144 {
gkroussos 0:637031152314 145 return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 146 }
gkroussos 0:637031152314 147 }
gkroussos 0:637031152314 148
gkroussos 0:637031152314 149 /* Check timeout is zero for Connectable Directed */
gkroussos 0:637031152314 150 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
gkroussos 0:637031152314 151 (params.getTimeout() != 0))
gkroussos 0:637031152314 152 {
gkroussos 0:637031152314 153 /* Timeout must be 0 with this type, although we'll never get here */
gkroussos 0:637031152314 154 /* since this isn't implemented yet anyway */
gkroussos 0:637031152314 155 return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 156 }
gkroussos 0:637031152314 157
gkroussos 0:637031152314 158 /* Check timeout for other advertising types */
gkroussos 0:637031152314 159 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
gkroussos 0:637031152314 160 (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
gkroussos 0:637031152314 161 {
gkroussos 0:637031152314 162 return BLE_ERROR_PARAM_OUT_OF_RANGE;
gkroussos 0:637031152314 163 }
gkroussos 0:637031152314 164
gkroussos 0:637031152314 165 /* ToDo: Start Advertising */
gkroussos 0:637031152314 166 ble_gap_adv_params_t adv_para = { 0 };
gkroussos 0:637031152314 167
gkroussos 0:637031152314 168 adv_para.type = params.getAdvertisingType() ;
gkroussos 0:637031152314 169 adv_para.p_peer_addr = NULL ; // Undirected advertisement
gkroussos 0:637031152314 170 adv_para.fp = BLE_GAP_ADV_FP_ANY ;
gkroussos 0:637031152314 171 adv_para.p_whitelist = NULL ;
gkroussos 0:637031152314 172 adv_para.interval = params.getInterval() ; // advertising interval (in units of 0.625 ms)
gkroussos 0:637031152314 173 adv_para.timeout = params.getTimeout() ;
gkroussos 0:637031152314 174
gkroussos 0:637031152314 175 ASSERT( ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
gkroussos 0:637031152314 176
gkroussos 0:637031152314 177 state.advertising = 1;
gkroussos 0:637031152314 178
gkroussos 0:637031152314 179 return BLE_ERROR_NONE;
gkroussos 0:637031152314 180 }
gkroussos 0:637031152314 181
gkroussos 0:637031152314 182 /**************************************************************************/
gkroussos 0:637031152314 183 /*!
gkroussos 0:637031152314 184 @brief Stops the BLE HW and disconnects from any devices
gkroussos 0:637031152314 185
gkroussos 0:637031152314 186 @returns ble_error_t
gkroussos 0:637031152314 187
gkroussos 0:637031152314 188 @retval BLE_ERROR_NONE
gkroussos 0:637031152314 189 Everything executed properly
gkroussos 0:637031152314 190
gkroussos 0:637031152314 191 @section EXAMPLE
gkroussos 0:637031152314 192
gkroussos 0:637031152314 193 @code
gkroussos 0:637031152314 194
gkroussos 0:637031152314 195 @endcode
gkroussos 0:637031152314 196 */
gkroussos 0:637031152314 197 /**************************************************************************/
gkroussos 0:637031152314 198 ble_error_t nRF51Gap::stopAdvertising(void)
gkroussos 0:637031152314 199 {
gkroussos 0:637031152314 200 /* ToDo: Stop Advertising */
gkroussos 0:637031152314 201
gkroussos 0:637031152314 202 /* ToDo: Check response */
gkroussos 0:637031152314 203 wait(0.1);
gkroussos 0:637031152314 204
gkroussos 0:637031152314 205 state.advertising = 0;
gkroussos 0:637031152314 206
gkroussos 0:637031152314 207 return BLE_ERROR_NONE;
gkroussos 0:637031152314 208 }
gkroussos 0:637031152314 209
gkroussos 0:637031152314 210 /**************************************************************************/
gkroussos 0:637031152314 211 /*!
gkroussos 0:637031152314 212 @brief Disconnects if we are connected to a central device
gkroussos 0:637031152314 213
gkroussos 0:637031152314 214 @returns ble_error_t
gkroussos 0:637031152314 215
gkroussos 0:637031152314 216 @retval BLE_ERROR_NONE
gkroussos 0:637031152314 217 Everything executed properly
gkroussos 0:637031152314 218
gkroussos 0:637031152314 219 @section EXAMPLE
gkroussos 0:637031152314 220
gkroussos 0:637031152314 221 @code
gkroussos 0:637031152314 222
gkroussos 0:637031152314 223 @endcode
gkroussos 0:637031152314 224 */
gkroussos 0:637031152314 225 /**************************************************************************/
gkroussos 0:637031152314 226 ble_error_t nRF51Gap::disconnect(void)
gkroussos 0:637031152314 227 {
gkroussos 0:637031152314 228 /* ToDo: Disconnect if we are connected to a central device */
gkroussos 0:637031152314 229
gkroussos 0:637031152314 230 state.advertising = 0;
gkroussos 0:637031152314 231 state.connected = 0;
gkroussos 0:637031152314 232
gkroussos 0:637031152314 233 return BLE_ERROR_NONE;
gkroussos 0:637031152314 234 }