Microbug / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Committer:
sam_grove
Date:
Thu Jan 09 16:41:15 2014 +0000
Revision:
28:f6022fb90701
Parent:
27:4a83843f04b0
Updated constructor to take parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 27:4a83843f04b0 1 /* mbed Microcontroller Library
ktownsend 27:4a83843f04b0 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 27:4a83843f04b0 3 *
ktownsend 27:4a83843f04b0 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 27:4a83843f04b0 5 * you may not use this file except in compliance with the License.
ktownsend 27:4a83843f04b0 6 * You may obtain a copy of the License at
ktownsend 27:4a83843f04b0 7 *
ktownsend 27:4a83843f04b0 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 27:4a83843f04b0 9 *
ktownsend 27:4a83843f04b0 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 27:4a83843f04b0 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 27:4a83843f04b0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 27:4a83843f04b0 13 * See the License for the specific language governing permissions and
ktownsend 27:4a83843f04b0 14 * limitations under the License.
ktownsend 27:4a83843f04b0 15 */
ktownsend 27:4a83843f04b0 16
ktownsend 27:4a83843f04b0 17 #include "nRF51822.h"
ktownsend 27:4a83843f04b0 18 #include "mbed.h"
ktownsend 27:4a83843f04b0 19
ktownsend 27:4a83843f04b0 20 /**************************************************************************/
ktownsend 27:4a83843f04b0 21 /*!
ktownsend 27:4a83843f04b0 22 @brief UART callback function
ktownsend 27:4a83843f04b0 23 */
ktownsend 27:4a83843f04b0 24 /**************************************************************************/
ktownsend 27:4a83843f04b0 25 void nRF51822::uartCallback(void)
ktownsend 27:4a83843f04b0 26 {
ktownsend 27:4a83843f04b0 27 /* ToDo: Check responses and set a flag for success/error/etc. */
ktownsend 27:4a83843f04b0 28
ktownsend 27:4a83843f04b0 29 /* Read serial to clear the RX interrupt */
ktownsend 27:4a83843f04b0 30 uart.getc();
ktownsend 27:4a83843f04b0 31 }
ktownsend 27:4a83843f04b0 32
ktownsend 27:4a83843f04b0 33 /**************************************************************************/
ktownsend 27:4a83843f04b0 34 /*!
ktownsend 27:4a83843f04b0 35 @brief Constructor
ktownsend 27:4a83843f04b0 36 */
ktownsend 27:4a83843f04b0 37 /**************************************************************************/
sam_grove 28:f6022fb90701 38 nRF51822::nRF51822(PinName tx, PinName rx, PinName rts, PinName cts) : uart(tx, rx)
ktownsend 27:4a83843f04b0 39 {
ktownsend 27:4a83843f04b0 40 /* Setup the nRF UART interface */
ktownsend 27:4a83843f04b0 41 uart.baud(9600);
ktownsend 27:4a83843f04b0 42
ktownsend 27:4a83843f04b0 43 /* Echo data on the debug CDC port */
ktownsend 27:4a83843f04b0 44 uart.attach(this, &nRF51822::uartCallback);
ktownsend 27:4a83843f04b0 45
ktownsend 27:4a83843f04b0 46 /* Add flow control for UART (required by the nRF51822) */
sam_grove 28:f6022fb90701 47 uart.set_flow_control(RawSerial::RTSCTS, rts, cts);
ktownsend 27:4a83843f04b0 48
ktownsend 27:4a83843f04b0 49 /* Reset the service and characteristic counters */
ktownsend 27:4a83843f04b0 50 serviceCount = 0;
ktownsend 27:4a83843f04b0 51 characteristicCount = 0;
ktownsend 27:4a83843f04b0 52 }
ktownsend 27:4a83843f04b0 53
ktownsend 27:4a83843f04b0 54 /**************************************************************************/
ktownsend 27:4a83843f04b0 55 /*!
ktownsend 27:4a83843f04b0 56 @brief Destructor
ktownsend 27:4a83843f04b0 57 */
ktownsend 27:4a83843f04b0 58 /**************************************************************************/
ktownsend 27:4a83843f04b0 59 nRF51822::~nRF51822(void)
ktownsend 27:4a83843f04b0 60 {
ktownsend 27:4a83843f04b0 61 }
ktownsend 27:4a83843f04b0 62
ktownsend 27:4a83843f04b0 63 /**************************************************************************/
ktownsend 27:4a83843f04b0 64 /*!
ktownsend 27:4a83843f04b0 65 @brief Sets the advertising parameters and payload for the device
ktownsend 27:4a83843f04b0 66
ktownsend 27:4a83843f04b0 67 @param[in] params
ktownsend 27:4a83843f04b0 68 Basic advertising details, including the advertising
ktownsend 27:4a83843f04b0 69 delay, timeout and how the device should be advertised
ktownsend 27:4a83843f04b0 70 @params[in] advData
ktownsend 27:4a83843f04b0 71 The primary advertising data payload
ktownsend 27:4a83843f04b0 72 @params[in] scanResponse
ktownsend 27:4a83843f04b0 73 The optional Scan Response payload if the advertising
ktownsend 27:4a83843f04b0 74 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
ktownsend 27:4a83843f04b0 75 in \ref GapAdveritinngParams
ktownsend 27:4a83843f04b0 76
ktownsend 27:4a83843f04b0 77 @returns \ref ble_error_t
ktownsend 27:4a83843f04b0 78
ktownsend 27:4a83843f04b0 79 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 80 Everything executed properly
ktownsend 27:4a83843f04b0 81
ktownsend 27:4a83843f04b0 82 @retval BLE_ERROR_BUFFER_OVERFLOW
ktownsend 27:4a83843f04b0 83 The proposed action would cause a buffer overflow. All
ktownsend 27:4a83843f04b0 84 advertising payloads must be <= 31 bytes, for example.
ktownsend 27:4a83843f04b0 85
ktownsend 27:4a83843f04b0 86 @retval BLE_ERROR_NOT_IMPLEMENTED
ktownsend 27:4a83843f04b0 87 A feature was requested that is not yet supported in the
ktownsend 27:4a83843f04b0 88 nRF51 firmware or hardware.
ktownsend 27:4a83843f04b0 89
ktownsend 27:4a83843f04b0 90 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
ktownsend 27:4a83843f04b0 91 One of the proposed values is outside the valid range.
ktownsend 27:4a83843f04b0 92
ktownsend 27:4a83843f04b0 93 @section EXAMPLE
ktownsend 27:4a83843f04b0 94
ktownsend 27:4a83843f04b0 95 @code
ktownsend 27:4a83843f04b0 96
ktownsend 27:4a83843f04b0 97 @endcode
ktownsend 27:4a83843f04b0 98 */
ktownsend 27:4a83843f04b0 99 /**************************************************************************/
ktownsend 27:4a83843f04b0 100 ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisingData & advData, GapAdvertisingData & scanResponse)
ktownsend 27:4a83843f04b0 101 {
ktownsend 27:4a83843f04b0 102 uint8_t len = 0;
ktownsend 27:4a83843f04b0 103 uint8_t *buffer;
ktownsend 27:4a83843f04b0 104
ktownsend 27:4a83843f04b0 105 /* Make sure we support the advertising type */
ktownsend 27:4a83843f04b0 106 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
ktownsend 27:4a83843f04b0 107 {
ktownsend 27:4a83843f04b0 108 /* ToDo: This requires a propery security implementation, etc. */
ktownsend 27:4a83843f04b0 109 return BLE_ERROR_NOT_IMPLEMENTED;
ktownsend 27:4a83843f04b0 110 }
ktownsend 27:4a83843f04b0 111
ktownsend 27:4a83843f04b0 112 /* Check interval range */
ktownsend 27:4a83843f04b0 113 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED)
ktownsend 27:4a83843f04b0 114 {
ktownsend 27:4a83843f04b0 115 /* Min delay is slightly longer for unconnectable devices */
ktownsend 27:4a83843f04b0 116 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
ktownsend 27:4a83843f04b0 117 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
ktownsend 27:4a83843f04b0 118 {
ktownsend 27:4a83843f04b0 119 return BLE_ERROR_PARAM_OUT_OF_RANGE;
ktownsend 27:4a83843f04b0 120 }
ktownsend 27:4a83843f04b0 121 }
ktownsend 27:4a83843f04b0 122 else
ktownsend 27:4a83843f04b0 123 {
ktownsend 27:4a83843f04b0 124 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
ktownsend 27:4a83843f04b0 125 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
ktownsend 27:4a83843f04b0 126 {
ktownsend 27:4a83843f04b0 127 return BLE_ERROR_PARAM_OUT_OF_RANGE;
ktownsend 27:4a83843f04b0 128 }
ktownsend 27:4a83843f04b0 129 }
ktownsend 27:4a83843f04b0 130
ktownsend 27:4a83843f04b0 131 /* Check timeout is zero for Connectable Directed */
ktownsend 27:4a83843f04b0 132 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
ktownsend 27:4a83843f04b0 133 (params.getTimeout() != 0))
ktownsend 27:4a83843f04b0 134 {
ktownsend 27:4a83843f04b0 135 /* Timeout must be 0 with this type, although we'll never get here */
ktownsend 27:4a83843f04b0 136 /* since this isn't implemented yet anyway */
ktownsend 27:4a83843f04b0 137 return BLE_ERROR_PARAM_OUT_OF_RANGE;
ktownsend 27:4a83843f04b0 138 }
ktownsend 27:4a83843f04b0 139
ktownsend 27:4a83843f04b0 140 /* Check timeout for other advertising types */
ktownsend 27:4a83843f04b0 141 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
ktownsend 27:4a83843f04b0 142 (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
ktownsend 27:4a83843f04b0 143 {
ktownsend 27:4a83843f04b0 144 return BLE_ERROR_PARAM_OUT_OF_RANGE;
ktownsend 27:4a83843f04b0 145 }
ktownsend 27:4a83843f04b0 146
ktownsend 27:4a83843f04b0 147 /* Make sure we don't exceed the advertising payload length */
ktownsend 27:4a83843f04b0 148 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
ktownsend 27:4a83843f04b0 149 {
ktownsend 27:4a83843f04b0 150 return BLE_ERROR_BUFFER_OVERFLOW;
ktownsend 27:4a83843f04b0 151 }
ktownsend 27:4a83843f04b0 152
ktownsend 27:4a83843f04b0 153 /* Check the scan response payload limits */
ktownsend 27:4a83843f04b0 154 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
ktownsend 27:4a83843f04b0 155 {
ktownsend 27:4a83843f04b0 156 /* Check if we're within the upper limit */
ktownsend 27:4a83843f04b0 157 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
ktownsend 27:4a83843f04b0 158 {
ktownsend 27:4a83843f04b0 159 return BLE_ERROR_BUFFER_OVERFLOW;
ktownsend 27:4a83843f04b0 160 }
ktownsend 27:4a83843f04b0 161 /* Make sure we have a payload! */
ktownsend 27:4a83843f04b0 162 if (advData.getPayloadLen() == 0)
ktownsend 27:4a83843f04b0 163 {
ktownsend 27:4a83843f04b0 164 return BLE_ERROR_PARAM_OUT_OF_RANGE;
ktownsend 27:4a83843f04b0 165 }
ktownsend 27:4a83843f04b0 166 }
ktownsend 27:4a83843f04b0 167
ktownsend 27:4a83843f04b0 168 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
ktownsend 27:4a83843f04b0 169 /* contains a flags AD type, etc. */
ktownsend 27:4a83843f04b0 170
ktownsend 27:4a83843f04b0 171 /* ToDo: Refactor these actions into separate private functions */
ktownsend 27:4a83843f04b0 172
ktownsend 27:4a83843f04b0 173 /* 1.) Send advertising params, Command IDs = 0x000C, 0x000D, 0x000E */
ktownsend 27:4a83843f04b0 174 /* A.) Command ID = 0x000C, Advertising Interval, uint16_t */
ktownsend 27:4a83843f04b0 175 uart.printf("10 0C 00 02 %02X %02X\r\n", (uint8_t)(params.getInterval() & 0xFF),
ktownsend 27:4a83843f04b0 176 (uint8_t)(params.getInterval() >> 8));
ktownsend 27:4a83843f04b0 177 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 178 wait(0.5);
ktownsend 27:4a83843f04b0 179
ktownsend 27:4a83843f04b0 180 /* B.) Command ID = 0x000D, Advertising Timeout, uint16_t */
ktownsend 27:4a83843f04b0 181 uart.printf("10 0D 00 02 %02X %02X\r\n", (uint8_t)(params.getTimeout() & 0xFF),
ktownsend 27:4a83843f04b0 182 (uint8_t)(params.getTimeout() >> 8));
ktownsend 27:4a83843f04b0 183 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 184 wait(0.5);
ktownsend 27:4a83843f04b0 185
ktownsend 27:4a83843f04b0 186 /* C.) Command ID = 0x000E, Advertising Type, uint8_t */
ktownsend 27:4a83843f04b0 187 uart.printf("10 0E 00 01 %02X\r\n", (uint8_t)(params.getAdvertisingType()));
ktownsend 27:4a83843f04b0 188 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 189 wait(0.5);
ktownsend 27:4a83843f04b0 190
ktownsend 27:4a83843f04b0 191 /* 2.) Send advertising data, Command ID = 0x000A */
ktownsend 27:4a83843f04b0 192 len = advData.getPayloadLen();
ktownsend 27:4a83843f04b0 193 buffer = advData.getPayload();
ktownsend 27:4a83843f04b0 194 uart.printf("10 0A 00 %02X", len);
ktownsend 27:4a83843f04b0 195 for (uint16_t i = 0; i < len; i++)
ktownsend 27:4a83843f04b0 196 {
ktownsend 27:4a83843f04b0 197 uart.printf(" %02X", buffer[i]);
ktownsend 27:4a83843f04b0 198 }
ktownsend 27:4a83843f04b0 199 uart.printf("\r\n");
ktownsend 27:4a83843f04b0 200
ktownsend 27:4a83843f04b0 201 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 202 wait(0.1);
ktownsend 27:4a83843f04b0 203
ktownsend 27:4a83843f04b0 204 /* 3.) Send scan response data, Command ID = 0x000B */
ktownsend 27:4a83843f04b0 205 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
ktownsend 27:4a83843f04b0 206 {
ktownsend 27:4a83843f04b0 207 len = scanResponse.getPayloadLen();
ktownsend 27:4a83843f04b0 208 buffer = scanResponse.getPayload();
ktownsend 27:4a83843f04b0 209 uart.printf("10 0B 00 %02X", len);
ktownsend 27:4a83843f04b0 210 for (uint16_t i = 0; i < len; i++)
ktownsend 27:4a83843f04b0 211 {
ktownsend 27:4a83843f04b0 212 uart.printf(" %02X", buffer[i]);
ktownsend 27:4a83843f04b0 213 }
ktownsend 27:4a83843f04b0 214 uart.printf("\r\n");
ktownsend 27:4a83843f04b0 215
ktownsend 27:4a83843f04b0 216 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 217 wait(0.1);
ktownsend 27:4a83843f04b0 218 }
ktownsend 27:4a83843f04b0 219
ktownsend 27:4a83843f04b0 220 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 221 }
ktownsend 27:4a83843f04b0 222
ktownsend 27:4a83843f04b0 223 /**************************************************************************/
ktownsend 27:4a83843f04b0 224 /*!
ktownsend 27:4a83843f04b0 225 @brief Adds a new service to the GATT table on the peripheral
ktownsend 27:4a83843f04b0 226
ktownsend 27:4a83843f04b0 227 @returns ble_error_t
ktownsend 27:4a83843f04b0 228
ktownsend 27:4a83843f04b0 229 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 230 Everything executed properly
ktownsend 27:4a83843f04b0 231
ktownsend 27:4a83843f04b0 232 @section EXAMPLE
ktownsend 27:4a83843f04b0 233
ktownsend 27:4a83843f04b0 234 @code
ktownsend 27:4a83843f04b0 235
ktownsend 27:4a83843f04b0 236 @endcode
ktownsend 27:4a83843f04b0 237 */
ktownsend 27:4a83843f04b0 238 /**************************************************************************/
ktownsend 27:4a83843f04b0 239 ble_error_t nRF51822::addService(GattService & service)
ktownsend 27:4a83843f04b0 240 {
ktownsend 27:4a83843f04b0 241 /* ToDo: Make sure we don't overflow the array, etc. */
ktownsend 27:4a83843f04b0 242 /* ToDo: Make sure this service UUID doesn't already exist (?) */
ktownsend 27:4a83843f04b0 243 /* ToDo: Basic validation */
ktownsend 27:4a83843f04b0 244
ktownsend 27:4a83843f04b0 245 /* Add the service to the nRF51 */
ktownsend 27:4a83843f04b0 246 if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT)
ktownsend 27:4a83843f04b0 247 {
ktownsend 27:4a83843f04b0 248 /* 16-bit BLE UUID */
ktownsend 27:4a83843f04b0 249 uart.printf("10 01 00 04 01 02 %02X %02X\r\n",
ktownsend 27:4a83843f04b0 250 service.primaryServiceID.value & 0xFF,
ktownsend 27:4a83843f04b0 251 service.primaryServiceID.value >> 8);
ktownsend 27:4a83843f04b0 252 }
ktownsend 27:4a83843f04b0 253 else
ktownsend 27:4a83843f04b0 254 {
ktownsend 27:4a83843f04b0 255 /* 128-bit Custom UUID */
ktownsend 27:4a83843f04b0 256 uart.printf("10 01 00 12 01 10 %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
ktownsend 27:4a83843f04b0 257 service.primaryServiceID.base[0],
ktownsend 27:4a83843f04b0 258 service.primaryServiceID.base[1],
ktownsend 27:4a83843f04b0 259 service.primaryServiceID.base[2],
ktownsend 27:4a83843f04b0 260 service.primaryServiceID.base[3],
ktownsend 27:4a83843f04b0 261 service.primaryServiceID.base[4],
ktownsend 27:4a83843f04b0 262 service.primaryServiceID.base[5],
ktownsend 27:4a83843f04b0 263 service.primaryServiceID.base[6],
ktownsend 27:4a83843f04b0 264 service.primaryServiceID.base[7],
ktownsend 27:4a83843f04b0 265 service.primaryServiceID.base[8],
ktownsend 27:4a83843f04b0 266 service.primaryServiceID.base[9],
ktownsend 27:4a83843f04b0 267 service.primaryServiceID.base[10],
ktownsend 27:4a83843f04b0 268 service.primaryServiceID.base[11],
ktownsend 27:4a83843f04b0 269 service.primaryServiceID.base[12],
ktownsend 27:4a83843f04b0 270 service.primaryServiceID.base[13],
ktownsend 27:4a83843f04b0 271 service.primaryServiceID.base[14],
ktownsend 27:4a83843f04b0 272 service.primaryServiceID.base[15]);
ktownsend 27:4a83843f04b0 273 }
ktownsend 27:4a83843f04b0 274
ktownsend 27:4a83843f04b0 275 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 276 wait(0.1);
ktownsend 27:4a83843f04b0 277
ktownsend 27:4a83843f04b0 278 /* Add characteristics to the service */
ktownsend 27:4a83843f04b0 279 for (uint8_t i = 0; i < service.characteristicCount; i++)
ktownsend 27:4a83843f04b0 280 {
ktownsend 27:4a83843f04b0 281 /* Command ID = 0x0002 */
ktownsend 27:4a83843f04b0 282 uart.printf("10 02 00 0F 01 02 %02X %02X 04 02 %02X %02X 05 02 %02X %02X 03 01 %02X\r\n",
ktownsend 27:4a83843f04b0 283 service.characteristics[i].uuid & 0xFF,
ktownsend 27:4a83843f04b0 284 service.characteristics[i].uuid >> 8,
ktownsend 27:4a83843f04b0 285 service.characteristics[i].lenMin & 0xFF,
ktownsend 27:4a83843f04b0 286 service.characteristics[i].lenMin >> 8,
ktownsend 27:4a83843f04b0 287 service.characteristics[i].lenMax & 0xFF,
ktownsend 27:4a83843f04b0 288 service.characteristics[i].lenMax >> 8,
ktownsend 27:4a83843f04b0 289 service.characteristics[i].properties);
ktownsend 27:4a83843f04b0 290
ktownsend 27:4a83843f04b0 291 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 292 wait(0.1);
ktownsend 27:4a83843f04b0 293
ktownsend 27:4a83843f04b0 294 /* Update the characteristic handle */
ktownsend 27:4a83843f04b0 295 service.characteristics[i].handle = characteristicCount;
ktownsend 27:4a83843f04b0 296 characteristicCount++;
ktownsend 27:4a83843f04b0 297 }
ktownsend 27:4a83843f04b0 298
ktownsend 27:4a83843f04b0 299 /* Update the service handle */
ktownsend 27:4a83843f04b0 300 service.handle = serviceCount;
ktownsend 27:4a83843f04b0 301 serviceCount++;
ktownsend 27:4a83843f04b0 302
ktownsend 27:4a83843f04b0 303 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 304 }
ktownsend 27:4a83843f04b0 305
ktownsend 27:4a83843f04b0 306 /**************************************************************************/
ktownsend 27:4a83843f04b0 307 /*!
ktownsend 27:4a83843f04b0 308 @brief Reads the value of a characteristic, based on the service
ktownsend 27:4a83843f04b0 309 and characteristic index fields
ktownsend 27:4a83843f04b0 310
ktownsend 27:4a83843f04b0 311 @param[in] charHandle
ktownsend 27:4a83843f04b0 312 The handle of the GattCharacteristic to read from
ktownsend 27:4a83843f04b0 313 @param[in] buffer
ktownsend 27:4a83843f04b0 314 Buffer to hold the the characteristic's value
ktownsend 27:4a83843f04b0 315 (raw byte array in LSB format)
ktownsend 27:4a83843f04b0 316 @param[in] len
ktownsend 27:4a83843f04b0 317 The number of bytes read into the buffer
ktownsend 27:4a83843f04b0 318
ktownsend 27:4a83843f04b0 319 @returns ble_error_t
ktownsend 27:4a83843f04b0 320
ktownsend 27:4a83843f04b0 321 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 322 Everything executed properly
ktownsend 27:4a83843f04b0 323
ktownsend 27:4a83843f04b0 324 @section EXAMPLE
ktownsend 27:4a83843f04b0 325
ktownsend 27:4a83843f04b0 326 @code
ktownsend 27:4a83843f04b0 327
ktownsend 27:4a83843f04b0 328 @endcode
ktownsend 27:4a83843f04b0 329 */
ktownsend 27:4a83843f04b0 330 /**************************************************************************/
ktownsend 27:4a83843f04b0 331 ble_error_t nRF51822::readCharacteristic(uint8_t charHandle, uint8_t buffer[], uint16_t len)
ktownsend 27:4a83843f04b0 332 {
ktownsend 27:4a83843f04b0 333 /* ToDo */
ktownsend 27:4a83843f04b0 334
ktownsend 27:4a83843f04b0 335 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 336 }
ktownsend 27:4a83843f04b0 337
ktownsend 27:4a83843f04b0 338 /**************************************************************************/
ktownsend 27:4a83843f04b0 339 /*!
ktownsend 27:4a83843f04b0 340 @brief Updates the value of a characteristic, based on the service
ktownsend 27:4a83843f04b0 341 and characteristic index fields
ktownsend 27:4a83843f04b0 342
ktownsend 27:4a83843f04b0 343 @param[in] charHandle
ktownsend 27:4a83843f04b0 344 The handle of the GattCharacteristic to write to
ktownsend 27:4a83843f04b0 345 @param[in] buffer
ktownsend 27:4a83843f04b0 346 Data to use when updating the characteristic's value
ktownsend 27:4a83843f04b0 347 (raw byte array in LSB format)
ktownsend 27:4a83843f04b0 348 @param[in] len
ktownsend 27:4a83843f04b0 349 The number of bytes in buffer
ktownsend 27:4a83843f04b0 350
ktownsend 27:4a83843f04b0 351 @returns ble_error_t
ktownsend 27:4a83843f04b0 352
ktownsend 27:4a83843f04b0 353 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 354 Everything executed properly
ktownsend 27:4a83843f04b0 355
ktownsend 27:4a83843f04b0 356 @section EXAMPLE
ktownsend 27:4a83843f04b0 357
ktownsend 27:4a83843f04b0 358 @code
ktownsend 27:4a83843f04b0 359
ktownsend 27:4a83843f04b0 360 @endcode
ktownsend 27:4a83843f04b0 361 */
ktownsend 27:4a83843f04b0 362 /**************************************************************************/
ktownsend 27:4a83843f04b0 363 ble_error_t nRF51822::writeCharacteristic(uint8_t charHandle, uint8_t buffer[], uint16_t len)
ktownsend 27:4a83843f04b0 364 {
ktownsend 27:4a83843f04b0 365 /* Command ID = 0x0006, Payload = Characteristic ID, Value */
ktownsend 27:4a83843f04b0 366 uart.printf("10 06 00 %02X %02X", len + 1, charHandle);
ktownsend 27:4a83843f04b0 367 for (uint16_t i = 0; i<len; i++)
ktownsend 27:4a83843f04b0 368 {
ktownsend 27:4a83843f04b0 369 uart.printf(" %02X", buffer[i]);
ktownsend 27:4a83843f04b0 370 }
ktownsend 27:4a83843f04b0 371 uart.printf("\r\n");
ktownsend 27:4a83843f04b0 372
ktownsend 27:4a83843f04b0 373 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 374 wait(0.1);
ktownsend 27:4a83843f04b0 375
ktownsend 27:4a83843f04b0 376 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 377 }
ktownsend 27:4a83843f04b0 378
ktownsend 27:4a83843f04b0 379 /**************************************************************************/
ktownsend 27:4a83843f04b0 380 /*!
ktownsend 27:4a83843f04b0 381 @brief Starts the BLE HW, initialising any services that were
ktownsend 27:4a83843f04b0 382 added before this function was called.
ktownsend 27:4a83843f04b0 383
ktownsend 27:4a83843f04b0 384 @note All services must be added before calling this function!
ktownsend 27:4a83843f04b0 385
ktownsend 27:4a83843f04b0 386 @returns ble_error_t
ktownsend 27:4a83843f04b0 387
ktownsend 27:4a83843f04b0 388 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 389 Everything executed properly
ktownsend 27:4a83843f04b0 390
ktownsend 27:4a83843f04b0 391 @section EXAMPLE
ktownsend 27:4a83843f04b0 392
ktownsend 27:4a83843f04b0 393 @code
ktownsend 27:4a83843f04b0 394
ktownsend 27:4a83843f04b0 395 @endcode
ktownsend 27:4a83843f04b0 396 */
ktownsend 27:4a83843f04b0 397 /**************************************************************************/
ktownsend 27:4a83843f04b0 398 ble_error_t nRF51822::start(void)
ktownsend 27:4a83843f04b0 399 {
ktownsend 27:4a83843f04b0 400 /* Command ID = 0x0003, No payload */
ktownsend 27:4a83843f04b0 401 uart.printf("10 03 00 00\r\n");
ktownsend 27:4a83843f04b0 402
ktownsend 27:4a83843f04b0 403 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 404 wait(0.5);
ktownsend 27:4a83843f04b0 405
ktownsend 27:4a83843f04b0 406 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 407 }
ktownsend 27:4a83843f04b0 408
ktownsend 27:4a83843f04b0 409 /**************************************************************************/
ktownsend 27:4a83843f04b0 410 /*!
ktownsend 27:4a83843f04b0 411 @brief Stops the BLE HW and disconnects from any devices
ktownsend 27:4a83843f04b0 412
ktownsend 27:4a83843f04b0 413 @returns ble_error_t
ktownsend 27:4a83843f04b0 414
ktownsend 27:4a83843f04b0 415 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 416 Everything executed properly
ktownsend 27:4a83843f04b0 417
ktownsend 27:4a83843f04b0 418 @section EXAMPLE
ktownsend 27:4a83843f04b0 419
ktownsend 27:4a83843f04b0 420 @code
ktownsend 27:4a83843f04b0 421
ktownsend 27:4a83843f04b0 422 @endcode
ktownsend 27:4a83843f04b0 423 */
ktownsend 27:4a83843f04b0 424 /**************************************************************************/
ktownsend 27:4a83843f04b0 425 ble_error_t nRF51822::stop(void)
ktownsend 27:4a83843f04b0 426 {
ktownsend 27:4a83843f04b0 427 /* Command ID = 0x0004, No payload */
ktownsend 27:4a83843f04b0 428 uart.printf("10 04 00 00\r\n");
ktownsend 27:4a83843f04b0 429
ktownsend 27:4a83843f04b0 430 /* ToDo: Check response */
ktownsend 27:4a83843f04b0 431 wait(0.1);
ktownsend 27:4a83843f04b0 432
ktownsend 27:4a83843f04b0 433 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 434 }
ktownsend 27:4a83843f04b0 435
ktownsend 27:4a83843f04b0 436 /**************************************************************************/
ktownsend 27:4a83843f04b0 437 /*!
ktownsend 27:4a83843f04b0 438 @brief Resets the BLE HW, removing any existing services and
ktownsend 27:4a83843f04b0 439 characteristics
ktownsend 27:4a83843f04b0 440
ktownsend 27:4a83843f04b0 441 @returns ble_error_t
ktownsend 27:4a83843f04b0 442
ktownsend 27:4a83843f04b0 443 @retval BLE_ERROR_NONE
ktownsend 27:4a83843f04b0 444 Everything executed properly
ktownsend 27:4a83843f04b0 445
ktownsend 27:4a83843f04b0 446 @section EXAMPLE
ktownsend 27:4a83843f04b0 447
ktownsend 27:4a83843f04b0 448 @code
ktownsend 27:4a83843f04b0 449
ktownsend 27:4a83843f04b0 450 @endcode
ktownsend 27:4a83843f04b0 451 */
ktownsend 27:4a83843f04b0 452 /**************************************************************************/
ktownsend 27:4a83843f04b0 453 ble_error_t nRF51822::reset(void)
ktownsend 27:4a83843f04b0 454 {
ktownsend 27:4a83843f04b0 455 /* Command ID = 0x0005, No payload */
ktownsend 27:4a83843f04b0 456 uart.printf("10 05 00 00\r\n");
ktownsend 27:4a83843f04b0 457
ktownsend 27:4a83843f04b0 458 /* Reset the service and characteristic counters */
ktownsend 27:4a83843f04b0 459 serviceCount = 0;
ktownsend 27:4a83843f04b0 460 characteristicCount = 0;
ktownsend 27:4a83843f04b0 461
ktownsend 27:4a83843f04b0 462 /* Wait for the radio to come back up */
ktownsend 27:4a83843f04b0 463 wait(1);
ktownsend 27:4a83843f04b0 464
ktownsend 27:4a83843f04b0 465 return BLE_ERROR_NONE;
ktownsend 27:4a83843f04b0 466 }