-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
BDXDownloader.cpp
218 lines (188 loc) · 7.52 KB
/
BDXDownloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
*
* 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 <lib/core/CHIPError.h>
#include <lib/support/CodeUtils.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::bdx::TransferSession;
namespace chip {
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"));
CHIP_ERROR err =
mBdxTransfer.HandleMessageReceived(payloadHeader, std::move(msg), /* TODO:(#12520) */ chip::System::Clock::Seconds16(0));
if (err != CHIP_NO_ERROR)
{
ChipLogError(BDX, "unable to handle message: %" CHIP_ERROR_FORMAT, err.Format());
}
// HandleMessageReceived() will only decode/parse the message. Need to Poll() in order to do the message handling work in
// HandleBdxEvent().
PollTransferSession();
}
CHIP_ERROR BDXDownloader::SetBDXParams(const chip::bdx::TransferSession::TransferInitData & bdxInitData)
{
VerifyOrReturnError(mState == State::kIdle, CHIP_ERROR_INCORRECT_STATE);
// Must call StartTransfer() here to store the the pointer data contained in bdxInitData in the TransferSession object.
// Otherwise it could be freed before we can use it.
ReturnErrorOnFailure(mBdxTransfer.StartTransfer(chip::bdx::TransferRole::kReceiver, bdxInitData,
/* TODO:(#12520) */ chip::System::Clock::Seconds16(30)));
return CHIP_NO_ERROR;
}
CHIP_ERROR BDXDownloader::BeginPrepareDownload()
{
VerifyOrReturnError(mState == State::kIdle, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mImageProcessor != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mImageProcessor->PrepareDownload());
mState = State::kPreparing;
return CHIP_NO_ERROR;
}
CHIP_ERROR BDXDownloader::OnPreparedForDownload(CHIP_ERROR status)
{
VerifyOrReturnError(mState == State::kPreparing, CHIP_ERROR_INCORRECT_STATE);
if (status == CHIP_NO_ERROR)
{
mState = State::kInProgress;
// Must call here because StartTransfer() should have prepared a ReceiveInit message, and now we should send it.
PollTransferSession();
}
else
{
ChipLogError(BDX, "failed to prepare download: %" CHIP_ERROR_FORMAT, status.Format());
mBdxTransfer.Reset();
mState = State::kIdle;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR BDXDownloader::FetchNextData()
{
VerifyOrReturnError(mState == State::kInProgress, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mBdxTransfer.PrepareBlockQuery());
PollTransferSession();
return CHIP_NO_ERROR;
}
void BDXDownloader::OnDownloadTimeout()
{
if (mState == State::kInProgress)
{
ChipLogDetail(BDX, "aborting due to timeout");
mBdxTransfer.Reset();
if (mImageProcessor != nullptr)
{
mImageProcessor->Abort();
}
mState = State::kIdle;
}
else
{
ChipLogError(BDX, "no download in progress");
}
}
void BDXDownloader::EndDownload(CHIP_ERROR reason)
{
if (mState == State::kInProgress)
{
// There's no method for a BDX receiving driver to cleanly end a transfer
mBdxTransfer.AbortTransfer(chip::bdx::StatusCode::kUnknown);
if (mImageProcessor != nullptr)
{
mImageProcessor->Abort();
}
mState = State::kIdle;
// Because AbortTransfer() will generate a StatusReport to send.
PollTransferSession();
}
else
{
ChipLogError(BDX, "no download in progress");
}
}
void BDXDownloader::PollTransferSession()
{
TransferSession::OutputEvent outEvent;
// WARNING: Is this dangerous? What happens if the loop encounters two messages that need to be sent? Does the ExchangeContext
// allow that?
do
{
mBdxTransfer.PollOutput(outEvent, /* TODO:(#12520) */ chip::System::Clock::Seconds16(0));
CHIP_ERROR err = HandleBdxEvent(outEvent);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(BDX, "HandleBDXEvent: %" CHIP_ERROR_FORMAT, err.Format()));
} while (outEvent.EventType != TransferSession::OutputEventType::kNone);
}
CHIP_ERROR BDXDownloader::HandleBdxEvent(const chip::bdx::TransferSession::OutputEvent & outEvent)
{
VerifyOrReturnError(mImageProcessor != nullptr, CHIP_ERROR_INCORRECT_STATE);
switch (outEvent.EventType)
{
case TransferSession::OutputEventType::kNone:
break;
case TransferSession::OutputEventType::kAcceptReceived:
ReturnErrorOnFailure(mBdxTransfer.PrepareBlockQuery());
// TODO: need to check ReceiveAccept parameters
break;
case TransferSession::OutputEventType::kMsgToSend: {
VerifyOrReturnError(mMsgDelegate != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mMsgDelegate->SendMessage(outEvent));
if (outEvent.msgTypeData.HasMessageType(chip::bdx::MessageType::BlockAckEOF))
{
// BDX transfer is not complete until BlockAckEOF has been sent
mState = State::kComplete;
// TODO: how/when to reset the BDXDownloader to be ready to handle another download
}
break;
}
case TransferSession::OutputEventType::kBlockReceived: {
chip::ByteSpan blockData(outEvent.blockdata.Data, outEvent.blockdata.Length);
ReturnErrorOnFailure(mImageProcessor->ProcessBlock(blockData));
// TODO: this will cause problems if Finalize() is not guaranteed to do its work after ProcessBlock().
if (outEvent.blockdata.IsEof)
{
mBdxTransfer.PrepareBlockAck();
ReturnErrorOnFailure(mImageProcessor->Finalize());
}
break;
}
case TransferSession::OutputEventType::kStatusReceived:
ChipLogError(BDX, "BDX StatusReport %x", static_cast<uint16_t>(outEvent.statusData.statusCode));
mBdxTransfer.Reset();
ReturnErrorOnFailure(mImageProcessor->Abort());
break;
case TransferSession::OutputEventType::kInternalError:
ChipLogError(BDX, "TransferSession error");
mBdxTransfer.Reset();
ReturnErrorOnFailure(mImageProcessor->Abort());
break;
case TransferSession::OutputEventType::kTransferTimeout:
ChipLogError(BDX, "Transfer timed out");
mBdxTransfer.Reset();
ReturnErrorOnFailure(mImageProcessor->Abort());
break;
case TransferSession::OutputEventType::kInitReceived:
case TransferSession::OutputEventType::kAckReceived:
case TransferSession::OutputEventType::kQueryReceived:
case TransferSession::OutputEventType::kAckEOFReceived:
default:
ChipLogError(BDX, "Unexpected BDX event: %u", static_cast<uint16_t>(outEvent.EventType));
break;
}
return CHIP_NO_ERROR;
}
} // namespace chip