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 "ble/BLE.h" 00021 #include "ble/UUID.h" 00022 00023 extern "C" { 00024 #include "dfu_app_handler.h" 00025 } 00026 00027 extern const uint8_t DFUServiceBaseUUID[]; 00028 extern const uint16_t DFUServiceShortUUID; 00029 extern const uint16_t DFUServiceControlCharacteristicShortUUID; 00030 00031 extern const uint8_t DFUServiceUUID[]; 00032 extern const uint8_t DFUServiceControlCharacteristicUUID[]; 00033 extern const uint8_t DFUServicePacketCharacteristicUUID[]; 00034 00035 /** 00036 * @class DFUService 00037 * @brief Device Firmware Update Service. 00038 */ 00039 class DFUService { 00040 public: 00041 /** 00042 * @brief Signature for the handover callback. The application may provide this 00043 * callback when setting up the DFU service. The callback is then 00044 * invoked before handing control over to the bootloader. 00045 */ 00046 typedef void (*ResetPrepare_t)(void); 00047 00048 public: 00049 /** 00050 * @brief Adds Device Firmware Update Service to an existing BLE object. 00051 * 00052 * @param[ref] _ble 00053 * BLE object for the underlying controller. 00054 * @param[in] _handoverCallback 00055 * Application-specific handover callback. 00056 */ 00057 DFUService(BLE &_ble, ResetPrepare_t _handoverCallback = NULL) : 00058 ble(_ble), 00059 controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00060 packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES, 00061 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), 00062 controlBytes(), 00063 packetBytes() { 00064 static bool serviceAdded = false; /* We only add the DFU service once. */ 00065 if (serviceAdded) { 00066 return; 00067 } 00068 00069 /* Set an initial value for control bytes, so that the application's DFU service can 00070 * be distinguished from the real DFU service provided by the bootloader. */ 00071 controlBytes[0] = 0xFF; 00072 controlBytes[1] = 0xFF; 00073 00074 GattCharacteristic *dfuChars[] = {&controlPoint, &packet}; 00075 GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *)); 00076 00077 ble.addService(dfuService); 00078 handoverCallback = _handoverCallback; 00079 serviceAdded = true; 00080 00081 ble.onDataWritten(this, &DFUService::onDataWritten); 00082 } 00083 00084 /** 00085 * @brief Get the handle for the value attribute of the control characteristic. 00086 */ 00087 uint16_t getControlHandle(void) const { 00088 return controlPoint.getValueHandle(); 00089 } 00090 00091 /** 00092 * @brief This callback allows the DFU service to receive the initial trigger to 00093 * hand control over to the bootloader. First, the application is given a 00094 * chance to clean up. 00095 * 00096 * @param[in] params 00097 * Information about the characterisitc being updated. 00098 */ 00099 virtual void onDataWritten(const GattWriteCallbackParams *params) { 00100 if (params->handle == controlPoint.getValueHandle()) { 00101 /* At present, writing anything will do the trick - this needs to be improved. */ 00102 if (handoverCallback) { 00103 handoverCallback(); 00104 } 00105 00106 // Call bootloader_start implicitly trough a event handler call 00107 // it is a work around for bootloader_start not being public in sdk 8.1 00108 ble_dfu_t p_dfu; 00109 ble_dfu_evt_t p_evt; 00110 00111 p_dfu.conn_handle = params->connHandle; 00112 p_evt.ble_dfu_evt_type = BLE_DFU_START; 00113 00114 dfu_app_on_dfu_evt(&p_dfu, &p_evt); 00115 } 00116 } 00117 00118 protected: 00119 static const unsigned SIZEOF_CONTROL_BYTES = 2; 00120 static const unsigned SIZEOF_PACKET_BYTES = 20; 00121 00122 protected: 00123 BLE &ble; 00124 00125 /**< Writing to the control characteristic triggers the handover to DFU 00126 * bootloader. At present, writing anything will do the trick - this needs 00127 * to be improved. */ 00128 WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint; 00129 00130 /**< The packet characteristic in this service doesn't do anything meaningful; 00131 * it is only a placeholder to mimic the corresponding characteristic in the 00132 * actual DFU service implemented by the bootloader. Without this, some 00133 * FOTA clients might get confused, because service definitions change after 00134 * handing control over to the bootloader. */ 00135 GattCharacteristic packet; 00136 00137 uint8_t controlBytes[SIZEOF_CONTROL_BYTES]; 00138 uint8_t packetBytes[SIZEOF_PACKET_BYTES]; 00139 00140 static ResetPrepare_t handoverCallback; /**< Application-specific handover callback. */ 00141 }; 00142 00143 #endif /* #ifndef __BLE_DFU_SERVICE_H__*/
Generated on Tue Jul 12 2022 21:38:40 by
1.7.2
