|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file |
| 20 | + * Provides an implementation of Device Firmware Upgrade using SMP protocol |
| 21 | + * over Bluetooth LE functionality for Telink platform. |
| 22 | + */ |
| 23 | + |
| 24 | +#pragma once |
| 25 | + |
| 26 | +#include <lib/core/OTAImageHeader.h> |
| 27 | +#include <zephyr/drivers/flash.h> |
| 28 | +#include <zephyr/storage/flash_map.h> |
| 29 | + |
| 30 | +enum VerificationFailReason : unsigned char |
| 31 | +{ |
| 32 | + NO_FAIL = 0, |
| 33 | + WRONG_PRODUCT_ID = 1 << 1, |
| 34 | + WRONG_VENDOR_ID = 1 << 2, |
| 35 | + WRONG_VERSION = 1 << 3, |
| 36 | +}; |
| 37 | + |
| 38 | +using verificationFailCallback = void (*)(VerificationFailReason); |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief DFU over SMP helper class |
| 42 | + * |
| 43 | + * The purpose of this class is to enable Device Firmware Upgrade mechanism |
| 44 | + * using Simple Management Protocol (SMP) over Bluetooth LE. Besides |
| 45 | + * facilitating initialization of the SMP server, it is capable of requesting |
| 46 | + * BLE advertising in a way that is compatible with other application components |
| 47 | + * that use BLE, such as Matter BLE layer. |
| 48 | + */ |
| 49 | +class DFUOverSMP |
| 50 | +{ |
| 51 | +public: |
| 52 | + /** |
| 53 | + * @brief Initialize DFU over SMP utility |
| 54 | + * |
| 55 | + * Initialize internal structures and register necessary commands in the SMP |
| 56 | + * server. |
| 57 | + */ |
| 58 | + void Init(); |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Set callback function for verification failing event |
| 62 | + * |
| 63 | + * Set a callback function that will be called if the image footer verification fails. |
| 64 | + * Callback function have to receive argument that will describe |
| 65 | + * what was the reason of the failure. |
| 66 | + */ |
| 67 | + void SetFailCallback(verificationFailCallback cb); |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Start processing the footer of the image in slot 1. |
| 71 | + * |
| 72 | + * Starts footer check of the newly received image in the slot 1. |
| 73 | + */ |
| 74 | + CHIP_ERROR ProcessImageFooter(); |
| 75 | + |
| 76 | +private: |
| 77 | + static constexpr uint16_t VERSION_STRING_MAX_LENGTH = 64; |
| 78 | + |
| 79 | + verificationFailCallback failCallback; |
| 80 | + |
| 81 | + friend DFUOverSMP & GetDFUOverSMP(); |
| 82 | + static DFUOverSMP sDFUOverSMP; |
| 83 | + |
| 84 | + CHIP_ERROR GetDFUImageFooter(chip::OTAImageHeader & footer, const struct flash_area * fa); |
| 85 | + CHIP_ERROR GetDFUImageFooterOffset(unsigned int & footer_offset, const struct flash_area * fa); |
| 86 | + CHIP_ERROR CheckDFUImageFooter(chip::OTAImageHeader * imageHeader); |
| 87 | +}; |
| 88 | + |
| 89 | +inline DFUOverSMP & GetDFUOverSMP() |
| 90 | +{ |
| 91 | + return DFUOverSMP::sDFUOverSMP; |
| 92 | +} |
0 commit comments