Coverage Report

Created: 2025-09-02 06:46

/src/connectedhomeip/src/app/clusters/ota-requestor/BDXDownloader.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 *    Copyright (c) 2021 Project CHIP Authors
4
 *    All rights reserved.
5
 *
6
 *    Licensed under the Apache License, Version 2.0 (the "License");
7
 *    you may not use this file except in compliance with the License.
8
 *    You may obtain a copy of the License at
9
 *
10
 *        http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *    Unless required by applicable law or agreed to in writing, software
13
 *    distributed under the License is distributed on an "AS IS" BASIS,
14
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *    See the License for the specific language governing permissions and
16
 *    limitations under the License.
17
 */
18
19
/* This file contains a definition for a class that implements OTADownloader and downloads CHIP OTA images using the BDX protocol.
20
 * It should not execute any logic that is application specific.
21
 */
22
23
// TODO: unit tests
24
25
#pragma once
26
27
#include "OTADownloader.h"
28
29
#include <app-common/zap-generated/cluster-objects.h>
30
#include <lib/core/CHIPError.h>
31
#include <protocols/bdx/BdxTransferSession.h>
32
#include <system/SystemPacketBuffer.h>
33
#include <transport/raw/MessageHeader.h>
34
35
namespace chip {
36
37
class BDXDownloader : public chip::OTADownloader
38
{
39
public:
40
    // A delegate for passing messages to/from BDXDownloader and some messaging layer. This is mainly to make BDXDownloader more
41
    // easily unit-testable.
42
    class MessagingDelegate
43
    {
44
    public:
45
        virtual CHIP_ERROR SendMessage(const chip::bdx::TransferSession::OutputEvent & msgEvent) = 0;
46
0
        virtual ~MessagingDelegate() {}
47
    };
48
49
    class StateDelegate
50
    {
51
    public:
52
        // Handle download state change
53
        virtual void OnDownloadStateChanged(State state, app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum reason) = 0;
54
        // Handle update progress change
55
        virtual void OnUpdateProgressChanged(app::DataModel::Nullable<uint8_t> percent) = 0;
56
0
        virtual ~StateDelegate()                                                        = default;
57
    };
58
59
    // To be called when there is an incoming message to handle (of any protocol type)
60
    void OnMessageReceived(const chip::PayloadHeader & payloadHeader, chip::System::PacketBufferHandle msg);
61
62
0
    void SetMessageDelegate(MessagingDelegate * delegate) { mMsgDelegate = delegate; }
63
0
    void SetStateDelegate(StateDelegate * delegate) { mStateDelegate = delegate; }
64
65
    // Initialize a BDX transfer session but will not proceed until OnPreparedForDownload() is called.
66
    CHIP_ERROR SetBDXParams(const chip::bdx::TransferSession::TransferInitData & bdxInitData, System::Clock::Timeout timeout);
67
68
    // OTADownloader Overrides
69
    CHIP_ERROR BeginPrepareDownload() override;
70
    CHIP_ERROR OnPreparedForDownload(CHIP_ERROR status) override;
71
    void OnDownloadTimeout() override;
72
    // BDX does not provide a mechanism for the driver of a transfer to gracefully end the exchange, so it will abort the transfer
73
    // instead.
74
    void EndDownload(CHIP_ERROR reason = CHIP_NO_ERROR) override;
75
    CHIP_ERROR FetchNextData() override;
76
    CHIP_ERROR SkipData(uint32_t numBytes) override;
77
78
    System::Clock::Timeout GetTimeout();
79
    // If True, there's been a timeout in the transfer as measured by no download progress after 'mTimeout' seconds.
80
    // If False, there's been progress in the transfer.
81
    bool HasTransferTimedOut();
82
83
private:
84
    void PollTransferSession();
85
    void CleanupOnError(app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum reason);
86
    CHIP_ERROR HandleBdxEvent(const chip::bdx::TransferSession::OutputEvent & outEvent);
87
    void SetState(State state, app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum reason);
88
    void Reset();
89
90
    chip::bdx::TransferSession mBdxTransfer;
91
    MessagingDelegate * mMsgDelegate = nullptr;
92
    StateDelegate * mStateDelegate   = nullptr;
93
    // Timeout value in seconds to abort the download if there's no progress in the transfer session.
94
    System::Clock::Timeout mTimeout = System::Clock::kZero;
95
    // Tracks the last block counter used during the transfer session as of the previous check.
96
    uint32_t mPrevBlockCounter = 0;
97
};
98
99
} // namespace chip