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:
Thu Aug 21 14:09:18 2014 +0000
Revision:
1:d623a5792ce5
Parent:
0:4eaf82806f06
Child:
3:d58f3a5bd66c
minor cleanup.

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 0:4eaf82806f06 33 void disconnectionCallback(Gap::Handle_t handle)
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 0:4eaf82806f06 40 void onConnectionCallback(Gap::Handle_t handle)
rgrover1 0:4eaf82806f06 41 {
rgrover1 0:4eaf82806f06 42 DEBUG("____[ Connected ]______________________________________\r\n");
rgrover1 0:4eaf82806f06 43
rgrover1 0:4eaf82806f06 44 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rgrover1 0:4eaf82806f06 45 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rgrover1 0:4eaf82806f06 46 connectionParams.slaveLatency = Config::slaveLatency;
rgrover1 0:4eaf82806f06 47 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rgrover1 0:4eaf82806f06 48 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
rgrover1 0:4eaf82806f06 49 DEBUG("failed to update connection paramter\r\n");
rgrover1 0:4eaf82806f06 50 }
rgrover1 0:4eaf82806f06 51
rgrover1 0:4eaf82806f06 52 Transfer::reset();
rgrover1 0:4eaf82806f06 53 }
rgrover1 0:4eaf82806f06 54
rgrover1 0:4eaf82806f06 55 void onUpdatesEnabled(Gap::Handle_t handle)
rgrover1 0:4eaf82806f06 56 {
rgrover1 0:4eaf82806f06 57 DEBUG("Notifications enabled for %d\r\n", handle);
rgrover1 0:4eaf82806f06 58 }
rgrover1 0:4eaf82806f06 59
rgrover1 0:4eaf82806f06 60 void onDataWritten(Gap::Handle_t handle)
rgrover1 0:4eaf82806f06 61 {
rgrover1 0:4eaf82806f06 62 // bubble up to services, they will emit callbacks if handle matches
rgrover1 0:4eaf82806f06 63 Transfer::handleDataWritten(handle);
rgrover1 0:4eaf82806f06 64 }
rgrover1 0:4eaf82806f06 65
rgrover1 0:4eaf82806f06 66 void bluetoothInit()
rgrover1 0:4eaf82806f06 67 {
rgrover1 0:4eaf82806f06 68 DEBUG("Bluetooth initialising...\r\n");
rgrover1 0:4eaf82806f06 69 ble.init();
rgrover1 0:4eaf82806f06 70 ble.setDeviceName(Config::deviceName);
rgrover1 0:4eaf82806f06 71 ble.onDisconnection(disconnectionCallback);
rgrover1 0:4eaf82806f06 72 ble.onConnection(onConnectionCallback);
rgrover1 0:4eaf82806f06 73 ble.onDataWritten(onDataWritten);
rgrover1 0:4eaf82806f06 74 ble.onUpdatesEnabled(onUpdatesEnabled);
rgrover1 0:4eaf82806f06 75
rgrover1 0:4eaf82806f06 76 // Make sure we use our preferred conn. parameters
rgrover1 0:4eaf82806f06 77 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rgrover1 0:4eaf82806f06 78 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rgrover1 0:4eaf82806f06 79 connectionParams.slaveLatency = Config::slaveLatency;
rgrover1 0:4eaf82806f06 80 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rgrover1 0:4eaf82806f06 81 ble.setPreferredConnectionParams(&connectionParams);
rgrover1 0:4eaf82806f06 82 ble.getPreferredConnectionParams(&connectionParams);
rgrover1 0:4eaf82806f06 83 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rgrover1 0:4eaf82806f06 84 connectionParams.minConnectionInterval,
rgrover1 0:4eaf82806f06 85 connectionParams.maxConnectionInterval,
rgrover1 0:4eaf82806f06 86 connectionParams.slaveLatency,
rgrover1 0:4eaf82806f06 87 connectionParams.connectionSupervisionTimeout);
rgrover1 0:4eaf82806f06 88
rgrover1 0:4eaf82806f06 89 // Initialise transfer service
rgrover1 0:4eaf82806f06 90 Transfer::init(ble);
rgrover1 0:4eaf82806f06 91
rgrover1 0:4eaf82806f06 92 /* setup advertising */
rgrover1 0:4eaf82806f06 93 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:4eaf82806f06 94 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16);
rgrover1 0:4eaf82806f06 95 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:4eaf82806f06 96 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 0:4eaf82806f06 97 ble.setAdvertisingInterval(Config::advertisingInterval);
rgrover1 0:4eaf82806f06 98 ble.startAdvertising();
rgrover1 0:4eaf82806f06 99 DEBUG("Ready. Advertising.\r\n");
rgrover1 0:4eaf82806f06 100 }
rgrover1 0:4eaf82806f06 101
rgrover1 0:4eaf82806f06 102 int main(void)
rgrover1 0:4eaf82806f06 103 {
rgrover1 0:4eaf82806f06 104 DEBUG("Initialising TRANSFER PRO | Built %s %s\n\r", __DATE__, __TIME__);
rgrover1 0:4eaf82806f06 105
rgrover1 0:4eaf82806f06 106 for (int i = 0; i < 16; i++) {
rgrover1 0:4eaf82806f06 107 uuidlistrev[15 - i] = uuidlist[i];
rgrover1 0:4eaf82806f06 108 }
rgrover1 0:4eaf82806f06 109
rgrover1 0:4eaf82806f06 110 bluetoothInit();
rgrover1 0:4eaf82806f06 111
rgrover1 0:4eaf82806f06 112 while (true) {
rgrover1 0:4eaf82806f06 113 ble.waitForEvent();
rgrover1 0:4eaf82806f06 114 }
rgrover1 0:4eaf82806f06 115 }