microbit-dal
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/bluetooth/MicroBitDFUService.h@1:8aa5cdb4ab67, 2016-04-07 (annotated)
- 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?
User | Revision | Line number | New 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 | #ifndef MICROBIT_DFU_SERVICE_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_DFU_SERVICE_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "ble/BLE.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitEvent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | // MicroBit ControlPoint OpCodes |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | // Requests transfer to the Nordic DFU bootloader. |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | #define MICROBIT_DFU_OPCODE_START_DFU 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | // visual ID code constants |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | #define MICROBIT_DFU_HISTOGRAM_WIDTH 5 |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | #define MICROBIT_DFU_HISTOGRAM_HEIGHT 5 |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | // UUIDs for our service and characteristics |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | extern const uint8_t MicroBitDFUServiceUUID[]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | extern const uint8_t MicroBitDFUServiceControlCharacteristicUUID[]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | extern const uint8_t MicroBitDFUServiceFlashCodeCharacteristicUUID[]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | // Handle on the memory resident Nordic bootloader. |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | extern "C" void bootloader_start(void); |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | * Class definition for a MicroBit Device Firmware Update loader. |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | * This service allows hexes to be flashed remotely from another Bluetooth |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | * device. |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | class MicroBitDFUService |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | * Initialise the Device Firmware Update service. |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | * @param _ble The instance of a BLE device that we're running on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | MicroBitDFUService(BLEDevice &_ble); |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * Callback. Invoked when any of our attributes are written via BLE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | virtual void onDataWritten(const GattWriteCallbackParams *params); |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | private: |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | // State of paiting process. |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | bool authenticated; |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | bool flashCodeRequested; |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | // Bluetooth stack we're running on. |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | BLEDevice &ble; |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | // memory for our 8 bit control characteristic. |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | uint8_t controlByte; |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | // BLE pairing name of this device, encoded as an integer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | uint32_t flashCode; |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | GattAttribute::Handle_t microBitDFUServiceControlCharacteristicHandle; |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | GattAttribute::Handle_t microBitDFUServiceFlashCodeCharacteristicHandle; |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | // Displays the device's ID code as a histogram on the LED matrix display. |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | void showNameHistogram(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | // Displays an acknowledgement on the LED matrix display. |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | void showTick(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | // Update BLE characteristic to release our flash code. |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | void releaseFlashCode(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | // Event handlers for button clicks. |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | void onButtonA(MicroBitEvent e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | void onButtonB(MicroBitEvent e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | #endif |