Download a stream of data to a peripheral over BLE.

Dependencies:   BLE_API mbed nRF51822

A simple demonstration of downloading a stream onto a peripheral over BLE. There's a corresponding Python script to driver the client.

Committer:
rgrover1
Date:
Tue Sep 02 16:30:18 2014 +0000
Revision:
3:d58f3a5bd66c
Parent:
1:d623a5792ce5
Child:
4:29ae814ca55e
updating underlying libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:4eaf82806f06 1 /* mbed Microcontroller Library
rgrover1 0:4eaf82806f06 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:4eaf82806f06 3 *
rgrover1 0:4eaf82806f06 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:4eaf82806f06 5 * you may not use this file except in compliance with the License.
rgrover1 0:4eaf82806f06 6 * You may obtain a copy of the License at
rgrover1 0:4eaf82806f06 7 *
rgrover1 0:4eaf82806f06 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:4eaf82806f06 9 *
rgrover1 0:4eaf82806f06 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:4eaf82806f06 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:4eaf82806f06 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:4eaf82806f06 13 * See the License for the specific language governing permissions and
rgrover1 0:4eaf82806f06 14 * limitations under the License.
rgrover1 0:4eaf82806f06 15 */
rgrover1 0:4eaf82806f06 16
rgrover1 0:4eaf82806f06 17 #include "mbed.h"
rgrover1 0:4eaf82806f06 18 #include "Logger.h"
rgrover1 0:4eaf82806f06 19 #include "Configuration.h"
rgrover1 0:4eaf82806f06 20 #include "BLEDevice.h"
rgrover1 0:4eaf82806f06 21 #include "TransferService.h"
rgrover1 0:4eaf82806f06 22
rgrover1 0:4eaf82806f06 23 BLEDevice ble;
rgrover1 0:4eaf82806f06 24
rgrover1 0:4eaf82806f06 25 static Gap::ConnectionParams_t connectionParams;
rgrover1 0:4eaf82806f06 26
rgrover1 0:4eaf82806f06 27 static const uint8_t *uuidlist = Transfer::getServiceUUIDp();
rgrover1 0:4eaf82806f06 28 static uint8_t uuidlistrev[16];
rgrover1 0:4eaf82806f06 29 static const char *DEVICE_NAME = "SD";
rgrover1 0:4eaf82806f06 30
rgrover1 0:4eaf82806f06 31 void bluetoothInit();
rgrover1 0:4eaf82806f06 32
rgrover1 3:d58f3a5bd66c 33 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:4eaf82806f06 34 {
rgrover1 0:4eaf82806f06 35 DEBUG("Disconnected\n\r");
rgrover1 0:4eaf82806f06 36 ble.startAdvertising();
rgrover1 0:4eaf82806f06 37 DEBUG("Advertising...\r\n");
rgrover1 0:4eaf82806f06 38 }
rgrover1 0:4eaf82806f06 39
rgrover1 3:d58f3a5bd66c 40 void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params)
rgrover1 0:4eaf82806f06 41 {
rgrover1 0:4eaf82806f06 42 DEBUG("____[ Connected ]______________________________________\r\n");
rgrover1 3:d58f3a5bd66c 43 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rgrover1 3:d58f3a5bd66c 44 params->minConnectionInterval, params->maxConnectionInterval, params->slaveLatency, params->connectionSupervisionTimeout);
rgrover1 0:4eaf82806f06 45
rgrover1 0:4eaf82806f06 46 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rgrover1 0:4eaf82806f06 47 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rgrover1 0:4eaf82806f06 48 connectionParams.slaveLatency = Config::slaveLatency;
rgrover1 0:4eaf82806f06 49 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rgrover1 0:4eaf82806f06 50 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
rgrover1 0:4eaf82806f06 51 DEBUG("failed to update connection paramter\r\n");
rgrover1 0:4eaf82806f06 52 }
rgrover1 0:4eaf82806f06 53
rgrover1 0:4eaf82806f06 54 Transfer::reset();
rgrover1 0:4eaf82806f06 55 }
rgrover1 0:4eaf82806f06 56
rgrover1 0:4eaf82806f06 57 void onUpdatesEnabled(Gap::Handle_t handle)
rgrover1 0:4eaf82806f06 58 {
rgrover1 0:4eaf82806f06 59 DEBUG("Notifications enabled for %d\r\n", handle);
rgrover1 0:4eaf82806f06 60 }
rgrover1 0:4eaf82806f06 61
rgrover1 3:d58f3a5bd66c 62 void onDataWritten(Gap::Handle_t handle, const GattCharacteristicWriteCBParams *params)
rgrover1 0:4eaf82806f06 63 {
rgrover1 0:4eaf82806f06 64 // bubble up to services, they will emit callbacks if handle matches
rgrover1 3:d58f3a5bd66c 65 Transfer::handleDataWritten(handle, params);
rgrover1 0:4eaf82806f06 66 }
rgrover1 0:4eaf82806f06 67
rgrover1 0:4eaf82806f06 68 void bluetoothInit()
rgrover1 0:4eaf82806f06 69 {
rgrover1 0:4eaf82806f06 70 DEBUG("Bluetooth initialising...\r\n");
rgrover1 0:4eaf82806f06 71 ble.init();
rgrover1 0:4eaf82806f06 72 ble.setDeviceName(Config::deviceName);
rgrover1 0:4eaf82806f06 73 ble.onDisconnection(disconnectionCallback);
rgrover1 0:4eaf82806f06 74 ble.onConnection(onConnectionCallback);
rgrover1 0:4eaf82806f06 75 ble.onDataWritten(onDataWritten);
rgrover1 0:4eaf82806f06 76 ble.onUpdatesEnabled(onUpdatesEnabled);
rgrover1 0:4eaf82806f06 77
rgrover1 0:4eaf82806f06 78 // Make sure we use our preferred conn. parameters
rgrover1 0:4eaf82806f06 79 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rgrover1 0:4eaf82806f06 80 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rgrover1 0:4eaf82806f06 81 connectionParams.slaveLatency = Config::slaveLatency;
rgrover1 0:4eaf82806f06 82 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rgrover1 0:4eaf82806f06 83 ble.setPreferredConnectionParams(&connectionParams);
rgrover1 0:4eaf82806f06 84 ble.getPreferredConnectionParams(&connectionParams);
rgrover1 0:4eaf82806f06 85 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rgrover1 0:4eaf82806f06 86 connectionParams.minConnectionInterval,
rgrover1 0:4eaf82806f06 87 connectionParams.maxConnectionInterval,
rgrover1 0:4eaf82806f06 88 connectionParams.slaveLatency,
rgrover1 0:4eaf82806f06 89 connectionParams.connectionSupervisionTimeout);
rgrover1 0:4eaf82806f06 90
rgrover1 0:4eaf82806f06 91 // Initialise transfer service
rgrover1 0:4eaf82806f06 92 Transfer::init(ble);
rgrover1 0:4eaf82806f06 93
rgrover1 0:4eaf82806f06 94 /* setup advertising */
rgrover1 0:4eaf82806f06 95 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:4eaf82806f06 96 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16);
rgrover1 0:4eaf82806f06 97 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:4eaf82806f06 98 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 0:4eaf82806f06 99 ble.setAdvertisingInterval(Config::advertisingInterval);
rgrover1 0:4eaf82806f06 100 ble.startAdvertising();
rgrover1 0:4eaf82806f06 101 DEBUG("Ready. Advertising.\r\n");
rgrover1 0:4eaf82806f06 102 }
rgrover1 0:4eaf82806f06 103
rgrover1 0:4eaf82806f06 104 int main(void)
rgrover1 0:4eaf82806f06 105 {
rgrover1 0:4eaf82806f06 106 DEBUG("Initialising TRANSFER PRO | Built %s %s\n\r", __DATE__, __TIME__);
rgrover1 0:4eaf82806f06 107
rgrover1 0:4eaf82806f06 108 for (int i = 0; i < 16; i++) {
rgrover1 0:4eaf82806f06 109 uuidlistrev[15 - i] = uuidlist[i];
rgrover1 0:4eaf82806f06 110 }
rgrover1 0:4eaf82806f06 111
rgrover1 0:4eaf82806f06 112 bluetoothInit();
rgrover1 0:4eaf82806f06 113
rgrover1 0:4eaf82806f06 114 while (true) {
rgrover1 0:4eaf82806f06 115 ble.waitForEvent();
rgrover1 0:4eaf82806f06 116 }
rgrover1 0:4eaf82806f06 117 }