-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
LinuxOTAImageProcessor.cpp
204 lines (171 loc) · 5.71 KB
/
LinuxOTAImageProcessor.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
/*
*
* 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 <app/clusters/ota-requestor/OTADownloader.h>
#include "LinuxOTAImageProcessor.h"
namespace chip {
// TODO: Dummy function to be removed once BDX downloader is implemented and can return a real instance
OTADownloader * GetDownloaderInstance()
{
return nullptr;
}
CHIP_ERROR LinuxOTAImageProcessor::PrepareDownload()
{
if (mParams.imageFile.empty())
{
ChipLogError(SoftwareUpdate, "Invalid output image file supplied");
return CHIP_ERROR_INTERNAL;
}
DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR LinuxOTAImageProcessor::Finalize()
{
DeviceLayer::PlatformMgr().ScheduleWork(HandleFinalize, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR LinuxOTAImageProcessor::Abort()
{
if (mParams.imageFile.empty())
{
ChipLogError(SoftwareUpdate, "Invalid output image file supplied");
return CHIP_ERROR_INTERNAL;
}
DeviceLayer::PlatformMgr().ScheduleWork(HandleAbort, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
CHIP_ERROR LinuxOTAImageProcessor::ProcessBlock(ByteSpan & block)
{
if (!mOfs.is_open() || !mOfs.good())
{
return CHIP_ERROR_INTERNAL;
}
if ((block.data() == nullptr) || block.empty())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
// Store block data for HandleProcessBlock to access
CHIP_ERROR err = SetBlock(block);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format());
}
DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast<intptr_t>(this));
return CHIP_NO_ERROR;
}
void LinuxOTAImageProcessor::HandlePrepareDownload(intptr_t context)
{
OTADownloader * downloader = GetDownloaderInstance();
if (downloader == nullptr)
{
ChipLogError(SoftwareUpdate, "No known OTA downloader");
return;
}
auto * imageProcessor = reinterpret_cast<LinuxOTAImageProcessor *>(context);
if (imageProcessor == nullptr)
{
downloader->OnPreparedForDownload(CHIP_ERROR_INVALID_ARGUMENT);
return;
}
imageProcessor->mOfs.open(imageProcessor->mParams.imageFile.data(),
std::ofstream::out | std::ofstream::ate | std::ofstream::app);
if (!imageProcessor->mOfs.good())
{
downloader->OnPreparedForDownload(CHIP_ERROR_OPEN_FAILED);
return;
}
downloader->OnPreparedForDownload(CHIP_NO_ERROR);
}
void LinuxOTAImageProcessor::HandleFinalize(intptr_t context)
{
auto * imageProcessor = reinterpret_cast<LinuxOTAImageProcessor *>(context);
if (imageProcessor == nullptr)
{
return;
}
imageProcessor->mOfs.close();
imageProcessor->ReleaseBlock();
}
void LinuxOTAImageProcessor::HandleAbort(intptr_t context)
{
auto * imageProcessor = reinterpret_cast<LinuxOTAImageProcessor *>(context);
if (imageProcessor == nullptr)
{
return;
}
imageProcessor->mOfs.close();
remove(imageProcessor->mParams.imageFile.data());
imageProcessor->ReleaseBlock();
}
void LinuxOTAImageProcessor::HandleProcessBlock(intptr_t context)
{
OTADownloader * downloader = GetDownloaderInstance();
if (downloader == nullptr)
{
ChipLogError(SoftwareUpdate, "No known OTA downloader");
return;
}
auto * imageProcessor = reinterpret_cast<LinuxOTAImageProcessor *>(context);
if (imageProcessor == nullptr)
{
downloader->OnBlockProcessed(CHIP_ERROR_INVALID_ARGUMENT, OTADownloader::kEnd);
return;
}
// TODO: Process block header if any
if (!imageProcessor->mOfs.write(reinterpret_cast<const char *>(imageProcessor->mBlock.data()),
static_cast<std::streamsize>(imageProcessor->mBlock.size())))
{
downloader->OnBlockProcessed(CHIP_ERROR_WRITE_FAILED, OTADownloader::kEnd);
return;
}
imageProcessor->mParams.downloadedBytes += imageProcessor->mBlock.size();
downloader->OnBlockProcessed(CHIP_NO_ERROR, OTADownloader::kGetNext);
}
CHIP_ERROR LinuxOTAImageProcessor::SetBlock(ByteSpan & block)
{
if ((block.data() == nullptr) || block.empty())
{
return CHIP_NO_ERROR;
}
// Allocate memory for block data if it has not been done yet
if (mBlock.empty())
{
mBlock = MutableByteSpan(static_cast<uint8_t *>(chip::Platform::MemoryAlloc(block.size())), block.size());
if (mBlock.data() == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
}
}
// Store the actual block data
CHIP_ERROR err = CopySpanToMutableSpan(block, mBlock);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot copy block data: %" CHIP_ERROR_FORMAT, err.Format());
return err;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR LinuxOTAImageProcessor::ReleaseBlock()
{
if (mBlock.data() != nullptr)
{
chip::Platform::MemoryFree(mBlock.data());
}
mBlock = MutableByteSpan();
return CHIP_NO_ERROR;
}
} // namespace chip