Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate BLE_ANCS_SDAPI_IRC ... more
services/DFUService.h@671:33ec93d25695, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:53:01 2015 +0100
- Revision:
- 671:33ec93d25695
- Parent:
- 670:5e4aecd9af5b
- Child:
- 710:b2e1a2660ec2
Synchronized with git rev c89eea7a
Author: Rohit Grover
rename BLEDevice as BLE; BLEDeviceInstanceBase as BLEInstanceBase
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Rohit Grover | 118:620d28e7a1ba | 1 | /* mbed Microcontroller Library | 
| Rohit Grover | 118:620d28e7a1ba | 2 | * Copyright (c) 2006-2013 ARM Limited | 
| Rohit Grover | 118:620d28e7a1ba | 3 | * | 
| Rohit Grover | 118:620d28e7a1ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| Rohit Grover | 118:620d28e7a1ba | 5 | * you may not use this file except in compliance with the License. | 
| Rohit Grover | 118:620d28e7a1ba | 6 | * You may obtain a copy of the License at | 
| Rohit Grover | 118:620d28e7a1ba | 7 | * | 
| Rohit Grover | 118:620d28e7a1ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| Rohit Grover | 118:620d28e7a1ba | 9 | * | 
| Rohit Grover | 118:620d28e7a1ba | 10 | * Unless required by applicable law or agreed to in writing, software | 
| Rohit Grover | 118:620d28e7a1ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| Rohit Grover | 118:620d28e7a1ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| Rohit Grover | 118:620d28e7a1ba | 13 | * See the License for the specific language governing permissions and | 
| Rohit Grover | 118:620d28e7a1ba | 14 | * limitations under the License. | 
| Rohit Grover | 118:620d28e7a1ba | 15 | */ | 
| Rohit Grover | 118:620d28e7a1ba | 16 | |
| Rohit Grover | 118:620d28e7a1ba | 17 | #ifndef __BLE_DFU_SERVICE_H__ | 
| Rohit Grover | 118:620d28e7a1ba | 18 | #define __BLE_DFU_SERVICE_H__ | 
| Rohit Grover | 118:620d28e7a1ba | 19 | |
| rgrover1 | 671:33ec93d25695 | 20 | #include "BLE.h" | 
| Rohit Grover | 118:620d28e7a1ba | 21 | #include "UUID.h" | 
| Rohit Grover | 118:620d28e7a1ba | 22 | |
| Rohit Grover | 118:620d28e7a1ba | 23 | extern "C" void bootloader_start(void); | 
| Rohit Grover | 118:620d28e7a1ba | 24 | |
| Rohit Grover | 118:620d28e7a1ba | 25 | extern const uint8_t DFUServiceBaseUUID[]; | 
| Rohit Grover | 118:620d28e7a1ba | 26 | extern const uint16_t DFUServiceShortUUID; | 
| Rohit Grover | 118:620d28e7a1ba | 27 | extern const uint16_t DFUServiceControlCharacteristicShortUUID; | 
| Rohit Grover | 118:620d28e7a1ba | 28 | |
| Rohit Grover | 118:620d28e7a1ba | 29 | extern const uint8_t DFUServiceUUID[]; | 
| Rohit Grover | 118:620d28e7a1ba | 30 | extern const uint8_t DFUServiceControlCharacteristicUUID[]; | 
| Rohit Grover | 119:18684018b83e | 31 | extern const uint8_t DFUServicePacketCharacteristicUUID[]; | 
| Rohit Grover | 118:620d28e7a1ba | 32 | |
| mbedAustin | 236:a77aefd88e78 | 33 | /** | 
| mbedAustin | 236:a77aefd88e78 | 34 | * @class DFUService | 
| rgrover1 | 242:0e9201b67e2f | 35 | * @brief Device Firmware Update Service. | 
| mbedAustin | 236:a77aefd88e78 | 36 | */ | 
| Rohit Grover | 118:620d28e7a1ba | 37 | class DFUService { | 
| Rohit Grover | 118:620d28e7a1ba | 38 | public: | 
| Rohit Grover | 118:620d28e7a1ba | 39 | /** | 
| rgrover1 | 242:0e9201b67e2f | 40 | * @brief Signature for the handover callback. The application may provide such a | 
| Rohit Grover | 118:620d28e7a1ba | 41 | * callback when setting up the DFU service, in which case it will be | 
| Rohit Grover | 119:18684018b83e | 42 | * invoked before handing control over to the bootloader. | 
| Rohit Grover | 118:620d28e7a1ba | 43 | */ | 
| Rohit Grover | 118:620d28e7a1ba | 44 | typedef void (*ResetPrepare_t)(void); | 
| Rohit Grover | 118:620d28e7a1ba | 45 | |
| Rohit Grover | 118:620d28e7a1ba | 46 | public: | 
| mbedAustin | 236:a77aefd88e78 | 47 | /** | 
| rgrover1 | 242:0e9201b67e2f | 48 | * @brief Adds Device Firmware Update service to an existing ble object. | 
| mbedAustin | 236:a77aefd88e78 | 49 | * | 
| mbedAustin | 236:a77aefd88e78 | 50 | * @param[ref] _ble | 
| rgrover1 | 671:33ec93d25695 | 51 | * BLE object for the underlying controller. | 
| mbedAustin | 236:a77aefd88e78 | 52 | * @param[in] _handoverCallback | 
| mbedAustin | 236:a77aefd88e78 | 53 | * Application specific handover callback. | 
| mbedAustin | 236:a77aefd88e78 | 54 | */ | 
| rgrover1 | 671:33ec93d25695 | 55 | DFUService(BLE &_ble, ResetPrepare_t _handoverCallback = NULL) : | 
| Rohit Grover | 118:620d28e7a1ba | 56 | ble(_ble), | 
| rgrover1 | 277:1407d2f1ce3c | 57 | controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), | 
| Rohit Grover | 119:18684018b83e | 58 | packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES, | 
| rgrover1 | 669:7179b4a5aa7d | 59 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), | 
| rgrover1 | 669:7179b4a5aa7d | 60 | controlBytes(), | 
| rgrover1 | 669:7179b4a5aa7d | 61 | packetBytes() { | 
| Rohit Grover | 118:620d28e7a1ba | 62 | static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */ | 
| Rohit Grover | 118:620d28e7a1ba | 63 | if (serviceAdded) { | 
| Rohit Grover | 118:620d28e7a1ba | 64 | return; | 
| Rohit Grover | 118:620d28e7a1ba | 65 | } | 
| Rohit Grover | 118:620d28e7a1ba | 66 | |
| Rohit Grover | 120:467527c1b943 | 67 | /* Set an initial value for control bytes so that the application's DFUService can | 
| Rohit Grover | 120:467527c1b943 | 68 | * be distinguished from the real DFU service provided by the bootloader. */ | 
| Rohit Grover | 120:467527c1b943 | 69 | controlBytes[0] = 0xFF; | 
| Rohit Grover | 120:467527c1b943 | 70 | controlBytes[1] = 0xFF; | 
| Rohit Grover | 120:467527c1b943 | 71 | |
| Rohit Grover | 119:18684018b83e | 72 | GattCharacteristic *dfuChars[] = {&controlPoint, &packet}; | 
| Rohit Grover | 118:620d28e7a1ba | 73 | GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *)); | 
| Rohit Grover | 118:620d28e7a1ba | 74 | |
| Rohit Grover | 118:620d28e7a1ba | 75 | ble.addService(dfuService); | 
| Rohit Grover | 118:620d28e7a1ba | 76 | handoverCallback = _handoverCallback; | 
| rgrover1 | 242:0e9201b67e2f | 77 | serviceAdded = true; | 
| Rohit Grover | 118:620d28e7a1ba | 78 | |
| Rohit Grover | 118:620d28e7a1ba | 79 | ble.onDataWritten(this, &DFUService::onDataWritten); | 
| Rohit Grover | 118:620d28e7a1ba | 80 | } | 
| Rohit Grover | 118:620d28e7a1ba | 81 | |
| mbedAustin | 236:a77aefd88e78 | 82 | /** | 
| rgrover1 | 243:98f930d14515 | 83 | * @brief get the handle for the value attribute of the control characteristic. | 
| mbedAustin | 236:a77aefd88e78 | 84 | */ | 
| rgrover1 | 243:98f930d14515 | 85 | uint16_t getControlHandle(void) const { | 
| rgrover1 | 243:98f930d14515 | 86 | return controlPoint.getValueHandle(); | 
| Rohit Grover | 118:620d28e7a1ba | 87 | } | 
| Rohit Grover | 118:620d28e7a1ba | 88 | |
| Rohit Grover | 118:620d28e7a1ba | 89 | /** | 
| rgrover1 | 242:0e9201b67e2f | 90 | * @brief This callback allows the DFU service to receive the initial trigger to | 
| Rohit Grover | 118:620d28e7a1ba | 91 | * handover control to the bootloader; but first the application is given a | 
| Rohit Grover | 118:620d28e7a1ba | 92 | * chance to clean up. | 
| mbedAustin | 236:a77aefd88e78 | 93 | * | 
| rgrover1 | 243:98f930d14515 | 94 | * @param[in] params | 
| rgrover1 | 243:98f930d14515 | 95 | * Information about the characterisitc being updated. | 
| Rohit Grover | 118:620d28e7a1ba | 96 | */ | 
| rgrover1 | 670:5e4aecd9af5b | 97 | virtual void onDataWritten(const GattWriteCallbackParams *params) { | 
| rgrover1 | 670:5e4aecd9af5b | 98 | if (params->handle == controlPoint.getValueHandle()) { | 
| Rohit Grover | 119:18684018b83e | 99 | /* At present, writing anything will do the trick--this needs to be improved. */ | 
| Rohit Grover | 118:620d28e7a1ba | 100 | if (handoverCallback) { | 
| Rohit Grover | 118:620d28e7a1ba | 101 | handoverCallback(); | 
| Rohit Grover | 118:620d28e7a1ba | 102 | } | 
| Rohit Grover | 118:620d28e7a1ba | 103 | |
| Rohit Grover | 118:620d28e7a1ba | 104 | bootloader_start(); | 
| Rohit Grover | 118:620d28e7a1ba | 105 | } | 
| Rohit Grover | 118:620d28e7a1ba | 106 | } | 
| Rohit Grover | 118:620d28e7a1ba | 107 | |
| rgrover1 | 669:7179b4a5aa7d | 108 | protected: | 
| Rohit Grover | 118:620d28e7a1ba | 109 | static const unsigned SIZEOF_CONTROL_BYTES = 2; | 
| Rohit Grover | 119:18684018b83e | 110 | static const unsigned SIZEOF_PACKET_BYTES = 20; | 
| Rohit Grover | 118:620d28e7a1ba | 111 | |
| rgrover1 | 669:7179b4a5aa7d | 112 | protected: | 
| rgrover1 | 671:33ec93d25695 | 113 | BLE &ble; | 
| Rohit Grover | 119:18684018b83e | 114 | |
| Rohit Grover | 119:18684018b83e | 115 | /**< Writing to the control characteristic triggers the handover to dfu- | 
| Rohit Grover | 119:18684018b83e | 116 | * bootloader. At present, writing anything will do the trick--this needs | 
| Rohit Grover | 119:18684018b83e | 117 | * to be improved. */ | 
| rgrover1 | 277:1407d2f1ce3c | 118 | WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint; | 
| Rohit Grover | 119:18684018b83e | 119 | |
| Rohit Grover | 119:18684018b83e | 120 | /**< The packet characteristic in this service doesn't do anything meaningful, but | 
| Rohit Grover | 119:18684018b83e | 121 | * is only a placeholder to mimic the corresponding characteristic in the | 
| Rohit Grover | 119:18684018b83e | 122 | * actual DFU service implemented by the bootloader. Without this, some | 
| Rohit Grover | 119:18684018b83e | 123 | * FOTA clients might get confused as service definitions change after | 
| Rohit Grover | 119:18684018b83e | 124 | * handing control over to the bootloader. */ | 
| Rohit Grover | 119:18684018b83e | 125 | GattCharacteristic packet; | 
| rgrover1 | 669:7179b4a5aa7d | 126 | |
| rgrover1 | 669:7179b4a5aa7d | 127 | uint8_t controlBytes[SIZEOF_CONTROL_BYTES]; | 
| rgrover1 | 669:7179b4a5aa7d | 128 | uint8_t packetBytes[SIZEOF_PACKET_BYTES]; | 
| rgrover1 | 669:7179b4a5aa7d | 129 | |
| rgrover1 | 669:7179b4a5aa7d | 130 | static ResetPrepare_t handoverCallback; /**< application specific handover callback. */ | 
| Rohit Grover | 118:620d28e7a1ba | 131 | }; | 
| Rohit Grover | 118:620d28e7a1ba | 132 | |
| rgrover1 | 242:0e9201b67e2f | 133 | #endif /* #ifndef __BLE_DFU_SERVICE_H__*/ |