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.
Fork of BLE_API by
DFUService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_DFU_SERVICE_H__ 00018 #define __BLE_DFU_SERVICE_H__ 00019 00020 #include "BLEDevice.h" 00021 #include "UUID.h" 00022 00023 extern "C" void bootloader_start(void); 00024 00025 extern const uint8_t DFUServiceBaseUUID[]; 00026 extern const uint16_t DFUServiceShortUUID; 00027 extern const uint16_t DFUServiceControlCharacteristicShortUUID; 00028 00029 extern const uint8_t DFUServiceUUID[]; 00030 extern const uint8_t DFUServiceControlCharacteristicUUID[]; 00031 extern const uint8_t DFUServicePacketCharacteristicUUID[]; 00032 00033 /** 00034 * @class DFUService 00035 * @brief Device Firmware Update Service. 00036 */ 00037 class DFUService { 00038 public: 00039 /** 00040 * @brief Signature for the handover callback. The application may provide such a 00041 * callback when setting up the DFU service, in which case it will be 00042 * invoked before handing control over to the bootloader. 00043 */ 00044 typedef void (*ResetPrepare_t)(void); 00045 00046 public: 00047 /** 00048 * @brief Adds Device Firmware Update service to an existing ble object. 00049 * 00050 * @param[ref] _ble 00051 * BLEDevice object for the underlying controller. 00052 * @param[in] _handoverCallback 00053 * Application specific handover callback. 00054 */ 00055 DFUService(BLEDevice &_ble, ResetPrepare_t _handoverCallback = NULL) : 00056 ble(_ble), 00057 controlBytes(), 00058 packetBytes(), 00059 controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, SIZEOF_CONTROL_BYTES, SIZEOF_CONTROL_BYTES, 00060 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00061 packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES, 00062 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) { 00063 static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */ 00064 if (serviceAdded) { 00065 return; 00066 } 00067 00068 /* Set an initial value for control bytes so that the application's DFUService can 00069 * be distinguished from the real DFU service provided by the bootloader. */ 00070 controlBytes[0] = 0xFF; 00071 controlBytes[1] = 0xFF; 00072 00073 GattCharacteristic *dfuChars[] = {&controlPoint, &packet}; 00074 GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *)); 00075 00076 ble.addService(dfuService); 00077 handoverCallback = _handoverCallback; 00078 serviceAdded = true; 00079 00080 ble.onDataWritten(this, &DFUService::onDataWritten); 00081 } 00082 00083 /** 00084 * @brief get the handle for the value attribute of the control characteristic. 00085 */ 00086 uint16_t getControlHandle(void) const { 00087 return controlPoint.getValueHandle(); 00088 } 00089 00090 /** 00091 * @brief This callback allows the DFU service to receive the initial trigger to 00092 * handover control to the bootloader; but first the application is given a 00093 * chance to clean up. 00094 * 00095 * @param[in] params 00096 * Information about the characterisitc being updated. 00097 */ 00098 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) { 00099 if (params->charHandle == controlPoint.getValueHandle()) { 00100 /* At present, writing anything will do the trick--this needs to be improved. */ 00101 if (handoverCallback) { 00102 handoverCallback(); 00103 } 00104 00105 bootloader_start(); 00106 } 00107 } 00108 00109 private: 00110 static const unsigned SIZEOF_CONTROL_BYTES = 2; 00111 static const unsigned SIZEOF_PACKET_BYTES = 20; 00112 00113 static ResetPrepare_t handoverCallback; /**< application specific handover callback. */ 00114 00115 private: 00116 BLEDevice &ble; 00117 uint8_t controlBytes[SIZEOF_CONTROL_BYTES]; 00118 uint8_t packetBytes[SIZEOF_PACKET_BYTES]; 00119 00120 /**< Writing to the control characteristic triggers the handover to dfu- 00121 * bootloader. At present, writing anything will do the trick--this needs 00122 * to be improved. */ 00123 GattCharacteristic controlPoint; 00124 00125 /**< The packet characteristic in this service doesn't do anything meaningful, but 00126 * is only a placeholder to mimic the corresponding characteristic in the 00127 * actual DFU service implemented by the bootloader. Without this, some 00128 * FOTA clients might get confused as service definitions change after 00129 * handing control over to the bootloader. */ 00130 GattCharacteristic packet; 00131 }; 00132 00133 #endif /* #ifndef __BLE_DFU_SERVICE_H__*/
Generated on Tue Jul 12 2022 21:47:54 by
1.7.2
