I don't know why this is happening.
Fork of BLE_API by
services/DFUService.h@714:a6130aaa0fd9, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 714:a6130aaa0fd9
Synchronized with git rev 7e8977d8
Author: Rohit Grover
Release 0.3.8
=============
This is a minor set of enhancements before we yotta-ize BLE_API.
Enhancements
~~~~~~~~~~~~
* Minor rework for class UUID; added a default and copy constructor; and a != operator.
* Added copy constructor and accessors for GapAdvertisingParams.
* GapScanningParams:: remove unnecessary checks for SCAN_TIMEOUT_MAX.
* Add a comment header block to explain why BLEDevice::init() may not be safe
to call from global static context.
* Introduce GattAttribute::INVALID_HANDLE.
* Replace some deprecated uses of Gap::address_t with Gap::Address_t.
Bugfixes
~~~~~~~~
* None.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 714:a6130aaa0fd9 | 1 | /* mbed Microcontroller Library |
rgrover1 | 714:a6130aaa0fd9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 714:a6130aaa0fd9 | 3 | * |
rgrover1 | 714:a6130aaa0fd9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 714:a6130aaa0fd9 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 714:a6130aaa0fd9 | 6 | * You may obtain a copy of the License at |
rgrover1 | 714:a6130aaa0fd9 | 7 | * |
rgrover1 | 714:a6130aaa0fd9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 714:a6130aaa0fd9 | 9 | * |
rgrover1 | 714:a6130aaa0fd9 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 714:a6130aaa0fd9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 714:a6130aaa0fd9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 714:a6130aaa0fd9 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 714:a6130aaa0fd9 | 14 | * limitations under the License. |
rgrover1 | 714:a6130aaa0fd9 | 15 | */ |
rgrover1 | 714:a6130aaa0fd9 | 16 | |
rgrover1 | 714:a6130aaa0fd9 | 17 | #ifndef __BLE_DFU_SERVICE_H__ |
rgrover1 | 714:a6130aaa0fd9 | 18 | #define __BLE_DFU_SERVICE_H__ |
rgrover1 | 714:a6130aaa0fd9 | 19 | |
rgrover1 | 714:a6130aaa0fd9 | 20 | #include "BLEDevice.h" |
rgrover1 | 714:a6130aaa0fd9 | 21 | #include "UUID.h" |
rgrover1 | 714:a6130aaa0fd9 | 22 | |
rgrover1 | 714:a6130aaa0fd9 | 23 | extern "C" void bootloader_start(void); |
rgrover1 | 714:a6130aaa0fd9 | 24 | |
rgrover1 | 714:a6130aaa0fd9 | 25 | extern const uint8_t DFUServiceBaseUUID[]; |
rgrover1 | 714:a6130aaa0fd9 | 26 | extern const uint16_t DFUServiceShortUUID; |
rgrover1 | 714:a6130aaa0fd9 | 27 | extern const uint16_t DFUServiceControlCharacteristicShortUUID; |
rgrover1 | 714:a6130aaa0fd9 | 28 | |
rgrover1 | 714:a6130aaa0fd9 | 29 | extern const uint8_t DFUServiceUUID[]; |
rgrover1 | 714:a6130aaa0fd9 | 30 | extern const uint8_t DFUServiceControlCharacteristicUUID[]; |
rgrover1 | 714:a6130aaa0fd9 | 31 | extern const uint8_t DFUServicePacketCharacteristicUUID[]; |
rgrover1 | 714:a6130aaa0fd9 | 32 | |
rgrover1 | 714:a6130aaa0fd9 | 33 | /** |
rgrover1 | 714:a6130aaa0fd9 | 34 | * @class DFUService |
rgrover1 | 714:a6130aaa0fd9 | 35 | * @brief Device Firmware Update Service. |
rgrover1 | 714:a6130aaa0fd9 | 36 | */ |
rgrover1 | 714:a6130aaa0fd9 | 37 | class DFUService { |
rgrover1 | 714:a6130aaa0fd9 | 38 | public: |
rgrover1 | 714:a6130aaa0fd9 | 39 | /** |
rgrover1 | 714:a6130aaa0fd9 | 40 | * @brief Signature for the handover callback. The application may provide such a |
rgrover1 | 714:a6130aaa0fd9 | 41 | * callback when setting up the DFU service, in which case it will be |
rgrover1 | 714:a6130aaa0fd9 | 42 | * invoked before handing control over to the bootloader. |
rgrover1 | 714:a6130aaa0fd9 | 43 | */ |
rgrover1 | 714:a6130aaa0fd9 | 44 | typedef void (*ResetPrepare_t)(void); |
rgrover1 | 714:a6130aaa0fd9 | 45 | |
rgrover1 | 714:a6130aaa0fd9 | 46 | public: |
rgrover1 | 714:a6130aaa0fd9 | 47 | /** |
rgrover1 | 714:a6130aaa0fd9 | 48 | * @brief Adds Device Firmware Update service to an existing ble object. |
rgrover1 | 714:a6130aaa0fd9 | 49 | * |
rgrover1 | 714:a6130aaa0fd9 | 50 | * @param[ref] _ble |
rgrover1 | 714:a6130aaa0fd9 | 51 | * BLEDevice object for the underlying controller. |
rgrover1 | 714:a6130aaa0fd9 | 52 | * @param[in] _handoverCallback |
rgrover1 | 714:a6130aaa0fd9 | 53 | * Application specific handover callback. |
rgrover1 | 714:a6130aaa0fd9 | 54 | */ |
rgrover1 | 714:a6130aaa0fd9 | 55 | DFUService(BLEDevice &_ble, ResetPrepare_t _handoverCallback = NULL) : |
rgrover1 | 714:a6130aaa0fd9 | 56 | ble(_ble), |
rgrover1 | 714:a6130aaa0fd9 | 57 | controlBytes(), |
rgrover1 | 714:a6130aaa0fd9 | 58 | packetBytes(), |
rgrover1 | 714:a6130aaa0fd9 | 59 | controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
rgrover1 | 714:a6130aaa0fd9 | 60 | packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES, |
rgrover1 | 714:a6130aaa0fd9 | 61 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) { |
rgrover1 | 714:a6130aaa0fd9 | 62 | static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */ |
rgrover1 | 714:a6130aaa0fd9 | 63 | if (serviceAdded) { |
rgrover1 | 714:a6130aaa0fd9 | 64 | return; |
rgrover1 | 714:a6130aaa0fd9 | 65 | } |
rgrover1 | 714:a6130aaa0fd9 | 66 | |
rgrover1 | 714:a6130aaa0fd9 | 67 | /* Set an initial value for control bytes so that the application's DFUService can |
rgrover1 | 714:a6130aaa0fd9 | 68 | * be distinguished from the real DFU service provided by the bootloader. */ |
rgrover1 | 714:a6130aaa0fd9 | 69 | controlBytes[0] = 0xFF; |
rgrover1 | 714:a6130aaa0fd9 | 70 | controlBytes[1] = 0xFF; |
rgrover1 | 714:a6130aaa0fd9 | 71 | |
rgrover1 | 714:a6130aaa0fd9 | 72 | GattCharacteristic *dfuChars[] = {&controlPoint, &packet}; |
rgrover1 | 714:a6130aaa0fd9 | 73 | GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *)); |
rgrover1 | 714:a6130aaa0fd9 | 74 | |
rgrover1 | 714:a6130aaa0fd9 | 75 | ble.addService(dfuService); |
rgrover1 | 714:a6130aaa0fd9 | 76 | handoverCallback = _handoverCallback; |
rgrover1 | 714:a6130aaa0fd9 | 77 | serviceAdded = true; |
rgrover1 | 714:a6130aaa0fd9 | 78 | |
rgrover1 | 714:a6130aaa0fd9 | 79 | ble.onDataWritten(this, &DFUService::onDataWritten); |
rgrover1 | 714:a6130aaa0fd9 | 80 | } |
rgrover1 | 714:a6130aaa0fd9 | 81 | |
rgrover1 | 714:a6130aaa0fd9 | 82 | /** |
rgrover1 | 714:a6130aaa0fd9 | 83 | * @brief get the handle for the value attribute of the control characteristic. |
rgrover1 | 714:a6130aaa0fd9 | 84 | */ |
rgrover1 | 714:a6130aaa0fd9 | 85 | uint16_t getControlHandle(void) const { |
rgrover1 | 714:a6130aaa0fd9 | 86 | return controlPoint.getValueHandle(); |
rgrover1 | 714:a6130aaa0fd9 | 87 | } |
rgrover1 | 714:a6130aaa0fd9 | 88 | |
rgrover1 | 714:a6130aaa0fd9 | 89 | /** |
rgrover1 | 714:a6130aaa0fd9 | 90 | * @brief This callback allows the DFU service to receive the initial trigger to |
rgrover1 | 714:a6130aaa0fd9 | 91 | * handover control to the bootloader; but first the application is given a |
rgrover1 | 714:a6130aaa0fd9 | 92 | * chance to clean up. |
rgrover1 | 714:a6130aaa0fd9 | 93 | * |
rgrover1 | 714:a6130aaa0fd9 | 94 | * @param[in] params |
rgrover1 | 714:a6130aaa0fd9 | 95 | * Information about the characterisitc being updated. |
rgrover1 | 714:a6130aaa0fd9 | 96 | */ |
rgrover1 | 714:a6130aaa0fd9 | 97 | virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { |
rgrover1 | 714:a6130aaa0fd9 | 98 | if (params->charHandle == controlPoint.getValueHandle()) { |
rgrover1 | 714:a6130aaa0fd9 | 99 | /* At present, writing anything will do the trick--this needs to be improved. */ |
rgrover1 | 714:a6130aaa0fd9 | 100 | if (handoverCallback) { |
rgrover1 | 714:a6130aaa0fd9 | 101 | handoverCallback(); |
rgrover1 | 714:a6130aaa0fd9 | 102 | } |
rgrover1 | 714:a6130aaa0fd9 | 103 | |
rgrover1 | 714:a6130aaa0fd9 | 104 | bootloader_start(); |
rgrover1 | 714:a6130aaa0fd9 | 105 | } |
rgrover1 | 714:a6130aaa0fd9 | 106 | } |
rgrover1 | 714:a6130aaa0fd9 | 107 | |
rgrover1 | 714:a6130aaa0fd9 | 108 | private: |
rgrover1 | 714:a6130aaa0fd9 | 109 | static const unsigned SIZEOF_CONTROL_BYTES = 2; |
rgrover1 | 714:a6130aaa0fd9 | 110 | static const unsigned SIZEOF_PACKET_BYTES = 20; |
rgrover1 | 714:a6130aaa0fd9 | 111 | |
rgrover1 | 714:a6130aaa0fd9 | 112 | static ResetPrepare_t handoverCallback; /**< application specific handover callback. */ |
rgrover1 | 714:a6130aaa0fd9 | 113 | |
rgrover1 | 714:a6130aaa0fd9 | 114 | private: |
rgrover1 | 714:a6130aaa0fd9 | 115 | BLEDevice &ble; |
rgrover1 | 714:a6130aaa0fd9 | 116 | uint8_t controlBytes[SIZEOF_CONTROL_BYTES]; |
rgrover1 | 714:a6130aaa0fd9 | 117 | uint8_t packetBytes[SIZEOF_PACKET_BYTES]; |
rgrover1 | 714:a6130aaa0fd9 | 118 | |
rgrover1 | 714:a6130aaa0fd9 | 119 | /**< Writing to the control characteristic triggers the handover to dfu- |
rgrover1 | 714:a6130aaa0fd9 | 120 | * bootloader. At present, writing anything will do the trick--this needs |
rgrover1 | 714:a6130aaa0fd9 | 121 | * to be improved. */ |
rgrover1 | 714:a6130aaa0fd9 | 122 | WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint; |
rgrover1 | 714:a6130aaa0fd9 | 123 | |
rgrover1 | 714:a6130aaa0fd9 | 124 | /**< The packet characteristic in this service doesn't do anything meaningful, but |
rgrover1 | 714:a6130aaa0fd9 | 125 | * is only a placeholder to mimic the corresponding characteristic in the |
rgrover1 | 714:a6130aaa0fd9 | 126 | * actual DFU service implemented by the bootloader. Without this, some |
rgrover1 | 714:a6130aaa0fd9 | 127 | * FOTA clients might get confused as service definitions change after |
rgrover1 | 714:a6130aaa0fd9 | 128 | * handing control over to the bootloader. */ |
rgrover1 | 714:a6130aaa0fd9 | 129 | GattCharacteristic packet; |
rgrover1 | 714:a6130aaa0fd9 | 130 | }; |
rgrover1 | 714:a6130aaa0fd9 | 131 | |
rgrover1 | 714:a6130aaa0fd9 | 132 | #endif /* #ifndef __BLE_DFU_SERVICE_H__*/ |