fork

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
Jonathan Austin
Date:
Thu Apr 07 01:33:22 2016 +0100
Revision:
1:8aa5cdb4ab67
Synchronized with git rev 55cb9199

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
Jonathan Austin 1:8aa5cdb4ab67 26 /**
Jonathan Austin 1:8aa5cdb4ab67 27 * Class definition for a MicroBit Device Firmware Update loader.
Jonathan Austin 1:8aa5cdb4ab67 28 *
Jonathan Austin 1:8aa5cdb4ab67 29 * This is actually just a frontend to a memory resident nordic DFU loader.
Jonathan Austin 1:8aa5cdb4ab67 30 *
Jonathan Austin 1:8aa5cdb4ab67 31 * We rely on the BLE standard pairing processes to provide encryption and authentication.
Jonathan Austin 1:8aa5cdb4ab67 32 * We assume any device that is paied with the micro:bit is authorized to reprogram the device.
Jonathan Austin 1:8aa5cdb4ab67 33 *
Jonathan Austin 1:8aa5cdb4ab67 34 */
Jonathan Austin 1:8aa5cdb4ab67 35 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 36 #include "MicroBitDFUService.h"
Jonathan Austin 1:8aa5cdb4ab67 37 #include "ble/UUID.h"
Jonathan Austin 1:8aa5cdb4ab67 38 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 39
Jonathan Austin 1:8aa5cdb4ab67 40 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 41 #pragma GCC diagnostic ignored "-Wunused-function"
Jonathan Austin 1:8aa5cdb4ab67 42 #pragma GCC diagnostic push
Jonathan Austin 1:8aa5cdb4ab67 43 #pragma GCC diagnostic ignored "-Wunused-parameter"
Jonathan Austin 1:8aa5cdb4ab67 44 #endif
Jonathan Austin 1:8aa5cdb4ab67 45
Jonathan Austin 1:8aa5cdb4ab67 46 /*
Jonathan Austin 1:8aa5cdb4ab67 47 * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
Jonathan Austin 1:8aa5cdb4ab67 48 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
Jonathan Austin 1:8aa5cdb4ab67 49 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
Jonathan Austin 1:8aa5cdb4ab67 50 * as a compatability option, but does not support the options used...
Jonathan Austin 1:8aa5cdb4ab67 51 */
Jonathan Austin 1:8aa5cdb4ab67 52 extern "C" {
Jonathan Austin 1:8aa5cdb4ab67 53 #include "dfu_app_handler.h"
Jonathan Austin 1:8aa5cdb4ab67 54 }
Jonathan Austin 1:8aa5cdb4ab67 55
Jonathan Austin 1:8aa5cdb4ab67 56 /*
Jonathan Austin 1:8aa5cdb4ab67 57 * Return to our predefined compiler settings.
Jonathan Austin 1:8aa5cdb4ab67 58 */
Jonathan Austin 1:8aa5cdb4ab67 59 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 60 #pragma GCC diagnostic pop
Jonathan Austin 1:8aa5cdb4ab67 61 #endif
Jonathan Austin 1:8aa5cdb4ab67 62
Jonathan Austin 1:8aa5cdb4ab67 63
Jonathan Austin 1:8aa5cdb4ab67 64 /**
Jonathan Austin 1:8aa5cdb4ab67 65 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 66 * Initialise the Device Firmware Update service.
Jonathan Austin 1:8aa5cdb4ab67 67 * @param _ble The instance of a BLE device that we're running on.
Jonathan Austin 1:8aa5cdb4ab67 68 */
Jonathan Austin 1:8aa5cdb4ab67 69 MicroBitDFUService::MicroBitDFUService(BLEDevice &_ble) :
Jonathan Austin 1:8aa5cdb4ab67 70 ble(_ble)
Jonathan Austin 1:8aa5cdb4ab67 71 {
Jonathan Austin 1:8aa5cdb4ab67 72 // Opcodes can be issued here to control the MicroBitDFU Service, as defined above.
Jonathan Austin 1:8aa5cdb4ab67 73 GattCharacteristic microBitDFUServiceControlCharacteristic(MicroBitDFUServiceControlCharacteristicUUID, &controlByte, 0, sizeof(uint8_t),
Jonathan Austin 1:8aa5cdb4ab67 74 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Jonathan Austin 1:8aa5cdb4ab67 75
Jonathan Austin 1:8aa5cdb4ab67 76 controlByte = 0x00;
Jonathan Austin 1:8aa5cdb4ab67 77
Jonathan Austin 1:8aa5cdb4ab67 78 // Set default security requirements
Jonathan Austin 1:8aa5cdb4ab67 79 microBitDFUServiceControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
Jonathan Austin 1:8aa5cdb4ab67 80
Jonathan Austin 1:8aa5cdb4ab67 81 GattCharacteristic *characteristics[] = {&microBitDFUServiceControlCharacteristic};
Jonathan Austin 1:8aa5cdb4ab67 82 GattService service(MicroBitDFUServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
Jonathan Austin 1:8aa5cdb4ab67 83
Jonathan Austin 1:8aa5cdb4ab67 84 ble.addService(service);
Jonathan Austin 1:8aa5cdb4ab67 85
Jonathan Austin 1:8aa5cdb4ab67 86 microBitDFUServiceControlCharacteristicHandle = microBitDFUServiceControlCharacteristic.getValueHandle();
Jonathan Austin 1:8aa5cdb4ab67 87
Jonathan Austin 1:8aa5cdb4ab67 88 ble.gattServer().write(microBitDFUServiceControlCharacteristicHandle, &controlByte, sizeof(uint8_t));
Jonathan Austin 1:8aa5cdb4ab67 89 ble.gattServer().onDataWritten(this, &MicroBitDFUService::onDataWritten);
Jonathan Austin 1:8aa5cdb4ab67 90 }
Jonathan Austin 1:8aa5cdb4ab67 91
Jonathan Austin 1:8aa5cdb4ab67 92 /**
Jonathan Austin 1:8aa5cdb4ab67 93 * Callback. Invoked when any of our attributes are written via BLE.
Jonathan Austin 1:8aa5cdb4ab67 94 */
Jonathan Austin 1:8aa5cdb4ab67 95 void MicroBitDFUService::onDataWritten(const GattWriteCallbackParams *params)
Jonathan Austin 1:8aa5cdb4ab67 96 {
Jonathan Austin 1:8aa5cdb4ab67 97 if (params->handle == microBitDFUServiceControlCharacteristicHandle)
Jonathan Austin 1:8aa5cdb4ab67 98 {
Jonathan Austin 1:8aa5cdb4ab67 99 if(params->len > 0 && params->data[0] == MICROBIT_DFU_OPCODE_START_DFU)
Jonathan Austin 1:8aa5cdb4ab67 100 {
Jonathan Austin 1:8aa5cdb4ab67 101 // TODO: Raise a SYSTEM event here.
Jonathan Austin 1:8aa5cdb4ab67 102 //uBit.display.stopAnimation();
Jonathan Austin 1:8aa5cdb4ab67 103 //uBit.display.clear();
Jonathan Austin 1:8aa5cdb4ab67 104
Jonathan Austin 1:8aa5cdb4ab67 105 #if CONFIG_ENABLED(MICROBIT_DBG)
Jonathan Austin 1:8aa5cdb4ab67 106 printf(" ACTIVATING BOOTLOADER.\n");
Jonathan Austin 1:8aa5cdb4ab67 107 #endif
Jonathan Austin 1:8aa5cdb4ab67 108
Jonathan Austin 1:8aa5cdb4ab67 109 // Perform an explicit disconnection to assist our peer to reconnect to the DFU service
Jonathan Austin 1:8aa5cdb4ab67 110 ble.disconnect(Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF);
Jonathan Austin 1:8aa5cdb4ab67 111
Jonathan Austin 1:8aa5cdb4ab67 112 wait_ms(1000);
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 // Call bootloader_start implicitly trough a event handler call
Jonathan Austin 1:8aa5cdb4ab67 115 // it is a work around for bootloader_start not being public in sdk 8.1
Jonathan Austin 1:8aa5cdb4ab67 116 ble_dfu_t p_dfu;
Jonathan Austin 1:8aa5cdb4ab67 117 ble_dfu_evt_t p_evt;
Jonathan Austin 1:8aa5cdb4ab67 118
Jonathan Austin 1:8aa5cdb4ab67 119 p_dfu.conn_handle = params->connHandle;
Jonathan Austin 1:8aa5cdb4ab67 120 p_evt.ble_dfu_evt_type = BLE_DFU_START;
Jonathan Austin 1:8aa5cdb4ab67 121
Jonathan Austin 1:8aa5cdb4ab67 122 dfu_app_on_dfu_evt(&p_dfu, &p_evt);
Jonathan Austin 1:8aa5cdb4ab67 123 }
Jonathan Austin 1:8aa5cdb4ab67 124 }
Jonathan Austin 1:8aa5cdb4ab67 125 }
Jonathan Austin 1:8aa5cdb4ab67 126
Jonathan Austin 1:8aa5cdb4ab67 127
Jonathan Austin 1:8aa5cdb4ab67 128 /**
Jonathan Austin 1:8aa5cdb4ab67 129 * UUID definitions for BLE Services and Characteristics.
Jonathan Austin 1:8aa5cdb4ab67 130 */
Jonathan Austin 1:8aa5cdb4ab67 131 const uint8_t MicroBitDFUServiceUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 132 0xe9,0x5d,0x93,0xb0,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 133 };
Jonathan Austin 1:8aa5cdb4ab67 134
Jonathan Austin 1:8aa5cdb4ab67 135 const uint8_t MicroBitDFUServiceControlCharacteristicUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 136 0xe9,0x5d,0x93,0xb1,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 137 };