forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedOTARequestorDriver.cpp
129 lines (106 loc) · 4.55 KB
/
ExtendedOTARequestorDriver.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
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* 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 "ExtendedOTARequestorDriver.h"
#include "OTARequestorInterface.h"
namespace chip {
namespace DeviceLayer {
namespace {
constexpr System::Clock::Seconds32 kUserConsentPollInterval = System::Clock::Seconds32(30);
} // namespace
bool ExtendedOTARequestorDriver::CanConsent()
{
bool localConfigDisabled = false;
VerifyOrdo(DeviceLayer::ConfigurationMgr().GetLocalConfigDisabled(localConfigDisabled) == CHIP_NO_ERROR,
ChipLogProgress(SoftwareUpdate, "Failed to get local config disabled, using as false"));
// If local config is disabled, we can't consent.
return localConfigDisabled == false;
}
void ExtendedOTARequestorDriver::UpdateAvailable(const UpdateDescription & update, System::Clock::Seconds32 delay)
{
VerifyOrDie(mRequestor != nullptr);
if (update.userConsentNeeded == true && mUserConsentDelegate)
{
chip::ota::UserConsentSubject subject;
CHIP_ERROR err = GetUserConsentSubject(subject, update);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Failed to get user consent subject");
HandleError(UpdateFailureState::kDelayedOnUserConsent, err);
return;
}
mDelayedActionTime = delay;
HandleUserConsentState(mUserConsentDelegate->GetUserConsentState(subject));
return;
}
GenericOTARequestorDriver::UpdateAvailable(update, delay);
}
void ExtendedOTARequestorDriver::PollUserConsentState()
{
HandleUserConsentState(mUserConsentDelegate->CheckDeferredUserConsentState());
}
CHIP_ERROR ExtendedOTARequestorDriver::GetUserConsentSubject(chip::ota::UserConsentSubject & subject,
const UpdateDescription & update)
{
// mLastUsedProvider has the provider fabric index and endpoint id
subject.fabricIndex = mLastUsedProvider.fabricIndex;
subject.providerEndpointId = mLastUsedProvider.endpoint;
// TODO: As we cannot use the src/app/Server.h in here so, figure out a way to get the node id.
ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetVendorId(subject.requestorVendorId));
ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetProductId(subject.requestorProductId));
ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetSoftwareVersion(subject.requestorCurrentVersion));
subject.requestorTargetVersion = update.softwareVersion;
subject.metadata = update.metadataForRequestor;
return CHIP_NO_ERROR;
}
void ExtendedOTARequestorDriver::HandleUserConsentState(chip::ota::UserConsentState userConsentState)
{
ChipLogDetail(SoftwareUpdate, "User consent state: %s", mUserConsentDelegate->UserConsentStateToString(userConsentState));
switch (userConsentState)
{
case chip::ota::UserConsentState::kGranted:
ScheduleDelayedAction(
mDelayedActionTime,
[](System::Layer *, void * context) {
static_cast<ExtendedOTARequestorDriver *>(context)->mRequestor->DownloadUpdate();
},
this);
break;
case chip::ota::UserConsentState::kDenied:
ScheduleDelayedAction(
mDelayedActionTime,
[](System::Layer *, void * context) {
static_cast<ExtendedOTARequestorDriver *>(context)->mRequestor->CancelImageUpdate();
},
this);
break;
case chip::ota::UserConsentState::kObtaining:
SystemLayer().ScheduleWork(
[](System::Layer *, void * context) {
static_cast<ExtendedOTARequestorDriver *>(context)->mRequestor->DownloadUpdateDelayedOnUserConsent();
},
this);
ScheduleDelayedAction(
kUserConsentPollInterval,
[](System::Layer *, void * context) { static_cast<ExtendedOTARequestorDriver *>(context)->PollUserConsentState(); },
this);
break;
default:
break;
}
}
} // namespace DeviceLayer
} // namespace chip