Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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