<commit_before>/*
 *
 *    Copyright (c) 2021 Project CHIP Authors
 *    All rights reserved.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#include "BDXDownloader.h"

#include <app/data-model/Nullable.h>
#include <lib/core/CHIPError.h>
#include <lib/support/BufferReader.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/bdx/BdxMessages.h>
#include <system/SystemClock.h> /* TODO:(#12520) remove */
#include <system/SystemPacketBuffer.h>
#include <transport/raw/MessageHeader.h>

using chip::OTADownloader;
using chip::app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum;
using chip::app::DataModel::Nullable;
using chip::bdx::TransferSession;

namespace chip {

void TransferTimeoutCheckHandler(System::Layer * systemLayer, void * appState)
{
    VerifyOrReturn(appState != nullptr);
    BDXDownloader * bdxDownloader = static_cast<BDXDownloader *>(appState);

    if (bdxDownloader->HasTransferTimedOut())
    {
        // End download if transfer timeout has been detected
        bdxDownloader->OnDownloadTimeout();
    }
    else
    {
        // Else restart the timer
        systemLayer->StartTimer(bdxDownloader->GetTimeout(), TransferTimeoutCheckHandler, appState);
    }
}

System::Clock::Timeout BDXDownloader::GetTimeout()
{
    return mTimeout;
}

void BDXDownloader::Reset()
{
    mPrevBlockCounter = 0;
    DeviceLayer::SystemLayer().StartTimer(mTimeout, TransferTimeoutCheckHandler, this);
}

bool BDXDownloader::HasTransferTimedOut()
{
    uint32_t curBlockCounter = mBdxTransfer.GetNextBlockNum();

    if (curBlockCounter > mPrevBlockCounter)
    {
        mPrevBlockCounter = curBlockCounter;
        return false;
    }
    else
    {
        ChipLogError(BDX, "BDX transfer timeout");
        return true;
    }
}

void BDXDownloader::OnMessageReceived(const chip::PayloadHeader & payloadHeader, chip::System::PacketBufferHandle msg)
{
    VerifyOrReturn(mState == State::kInProgress, ChipLogError(BDX, "Can't accept messages, no transfer in progress"));<commit_msg>Issue #15935 [ota-requestor] Cancel the TransferTimeout timer when QueryImage download succeeds or aborts (#15936)<commit_after>/*
 *
 *    Copyright (c) 2021 Project CHIP Authors
 *    All rights reserved.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#include "BDXDownloader.h"

#include <app/data-model/Nullable.h>
#include <lib/core/CHIPError.h>
#include <lib/support/BufferReader.h>
#include <lib/support/BytesToHex.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/bdx/BdxMessages.h>
#include <system/SystemClock.h> /* TODO:(#12520) remove */
#include <system/SystemPacketBuffer.h>
#include <transport/raw/MessageHeader.h>

using chip::OTADownloader;
using chip::app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum;
using chip::app::DataModel::Nullable;
using chip::bdx::TransferSession;

namespace chip {

void TransferTimeoutCheckHandler(System::Layer * systemLayer, void * appState)
{
    VerifyOrReturn(appState != nullptr);
    BDXDownloader * bdxDownloader = static_cast<BDXDownloader *>(appState);

    if (bdxDownloader->HasTransferTimedOut())
    {
        // End download if transfer timeout has been detected
        bdxDownloader->OnDownloadTimeout();
    }
    else
    {
        // Else restart the timer
        systemLayer->StartTimer(bdxDownloader->GetTimeout(), TransferTimeoutCheckHandler, appState);
    }
}

System::Clock::Timeout BDXDownloader::GetTimeout()
{
    return mTimeout;
}

void BDXDownloader::Reset()
{
    mPrevBlockCounter = 0;
    DeviceLayer::SystemLayer().CancelTimer(TransferTimeoutCheckHandler, this);
}

bool BDXDownloader::HasTransferTimedOut()
{
    uint32_t curBlockCounter = mBdxTransfer.GetNextBlockNum();

    if (curBlockCounter > mPrevBlockCounter)
    {
        mPrevBlockCounter = curBlockCounter;
        return false;
    }
    else
    {
        ChipLogError(BDX, "BDX transfer timeout");
        return true;
    }
}

void BDXDownloader::OnMessageReceived(const chip::PayloadHeader & payloadHeader, chip::System::PacketBufferHandle msg)
{
    VerifyOrReturn(mState == State::kInProgress, ChipLogError(BDX, "Can't accept messages, no transfer in progress"));
{'fraction_non_alphanumeric': 0.10624766180321736, 'fraction_numerical': 0.007856341189674524, 'mean_word_length': 4.211500974658869, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 29, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519562', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1557, 'n_words': 407}