-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
ota_activator_impl.cc
406 lines (342 loc) · 14.5 KB
/
ota_activator_impl.cc
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/services/cellular_setup/ota_activator_impl.h"
#include <sstream>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "chromeos/dbus/shill/shill_device_client.h"
#include "chromeos/network/cellular_utils.h"
#include "chromeos/network/device_state.h"
#include "chromeos/network/network_activation_handler.h"
#include "chromeos/network/network_connection_handler.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_util.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
#include "url/gurl.h"
namespace chromeos {
namespace cellular_setup {
namespace {
OtaActivatorImpl::Factory* g_test_factory = nullptr;
void OnModemResetError(const std::string& error_name,
const std::string& error_message) {
NET_LOG(ERROR) << "ShillDeviceClient::Reset() failed. " << error_name << ": "
<< error_message << ".";
}
void OnNetworkConnectionError(
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data) {
NET_LOG(ERROR) << "ConnectToNetwork() failed. Error name: " << error_name;
}
} // namespace
// static
std::unique_ptr<OtaActivator> OtaActivatorImpl::Factory::Create(
mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
base::OnceClosure on_finished_callback,
NetworkStateHandler* network_state_handler,
NetworkConnectionHandler* network_connection_handler,
NetworkActivationHandler* network_activation_handler,
scoped_refptr<base::TaskRunner> task_runner) {
if (g_test_factory) {
return g_test_factory->CreateInstance(
std::move(activation_delegate), std::move(on_finished_callback),
network_state_handler, network_connection_handler,
network_activation_handler, std::move(task_runner));
}
return base::WrapUnique(new OtaActivatorImpl(
std::move(activation_delegate), std::move(on_finished_callback),
network_state_handler, network_connection_handler,
network_activation_handler, std::move(task_runner)));
}
// static
void OtaActivatorImpl::Factory::SetFactoryForTesting(Factory* test_factory) {
g_test_factory = test_factory;
}
OtaActivatorImpl::Factory::~Factory() = default;
OtaActivatorImpl::OtaActivatorImpl(
mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
base::OnceClosure on_finished_callback,
NetworkStateHandler* network_state_handler,
NetworkConnectionHandler* network_connection_handler,
NetworkActivationHandler* network_activation_handler,
scoped_refptr<base::TaskRunner> task_runner)
: OtaActivator(std::move(on_finished_callback)),
activation_delegate_(std::move(activation_delegate)),
network_state_handler_(network_state_handler),
network_connection_handler_(network_connection_handler),
network_activation_handler_(network_activation_handler) {
task_runner->PostTask(FROM_HERE,
base::BindOnce(&OtaActivatorImpl::StartActivation,
weak_ptr_factory_.GetWeakPtr()));
}
OtaActivatorImpl::~OtaActivatorImpl() {
// If this object is being deleted but it never finished the flow, consider
// this a failure.
if (state_ != State::kFinished)
FinishActivationAttempt(mojom::ActivationResult::kFailedToActivate);
}
void OtaActivatorImpl::OnCarrierPortalStatusChange(
mojom::CarrierPortalStatus status) {
if (last_carrier_portal_status_) {
NET_LOG(USER) << "OtaActivatorImpl: Carrier portal status updated. "
<< *last_carrier_portal_status_ << " => " << status;
} else {
NET_LOG(USER) << "OtaActivatorImpl: Carrier portal status updated. "
<< "Status: " << status;
}
last_carrier_portal_status_ = status;
AttemptNextActivationStep();
}
void OtaActivatorImpl::NetworkListChanged() {
AttemptNextActivationStep();
}
void OtaActivatorImpl::DeviceListChanged() {
AttemptNextActivationStep();
}
void OtaActivatorImpl::NetworkPropertiesUpdated(const NetworkState* network) {
AttemptNextActivationStep();
}
void OtaActivatorImpl::DevicePropertiesUpdated(const DeviceState* device) {
AttemptNextActivationStep();
}
void OtaActivatorImpl::OnShuttingDown() {
// |network_state_handler_| is shutting down before activation was able to
// complete.
FinishActivationAttempt(mojom::ActivationResult::kFailedToActivate);
}
const DeviceState* OtaActivatorImpl::GetCellularDeviceState() const {
return network_state_handler_->GetDeviceStateByType(
NetworkTypePattern::Cellular());
}
const NetworkState* OtaActivatorImpl::GetCellularNetworkState() const {
NetworkStateHandler::NetworkStateList network_list;
network_state_handler_->GetVisibleNetworkListByType(
NetworkTypePattern::Cellular(), &network_list);
for (const NetworkState* network_state : network_list) {
if (network_state->iccid() == iccid_) {
return network_state;
}
}
return nullptr;
}
void OtaActivatorImpl::StartActivation() {
network_state_handler_->AddObserver(this, FROM_HERE);
// If |activation_delegate_| becomes disconnected, the activation request is
// considered canceled.
activation_delegate_.set_disconnect_handler(base::BindOnce(
&OtaActivatorImpl::FinishActivationAttempt, base::Unretained(this),
mojom::ActivationResult::kFailedToActivate));
ChangeStateAndAttemptNextStep(State::kWaitingForValidSimToBecomePresent);
}
void OtaActivatorImpl::ChangeStateAndAttemptNextStep(State state) {
DCHECK_NE(state, state_);
NET_LOG(DEBUG) << "OtaActivatorImpl: " << state_ << " => " << state << ".";
state_ = state;
AttemptNextActivationStep();
}
void OtaActivatorImpl::AttemptNextActivationStep() {
switch (state_) {
case State::kNotYetStarted:
// The flow either has not yet started; nothing to do.
break;
case State::kWaitingForValidSimToBecomePresent:
AttemptToDiscoverSim();
break;
case State::kWaitingForCellularConnection:
AttemptConnectionToCellularNetwork();
break;
case State::kWaitingForCellularPayment:
AttemptToSendMetadataToDelegate();
break;
case State::kWaitingForActivation:
AttemptToCompleteActivation();
break;
case State::kFinished:
InvokeOnFinishedCallback();
break;
}
}
void OtaActivatorImpl::FinishActivationAttempt(
mojom::ActivationResult activation_result) {
DCHECK(network_state_handler_);
network_state_handler_->RemoveObserver(this, FROM_HERE);
network_state_handler_ = nullptr;
NET_LOG(EVENT) << "Finished attempt with result " << activation_result << ".";
if (activation_delegate_)
activation_delegate_->OnActivationFinished(activation_result);
ChangeStateAndAttemptNextStep(State::kFinished);
}
void OtaActivatorImpl::AttemptToDiscoverSim() {
DCHECK(state_ == State::kWaitingForValidSimToBecomePresent);
const DeviceState* cellular_device = GetCellularDeviceState();
// If the Cellular device is not present, either this machine does not support
// cellular connections or the modem on the device is in the process of
// restarting.
if (!cellular_device)
return;
// Find status of first available pSIM slot.
// Note: We currently only support setting up devices with single physical
// SIM slots. This excludes other configurations such as multiple physical
// SIM slots and SIM cards in external dongles.
bool has_psim_slots = false;
for (const CellularSIMSlotInfo& sim_slot_info :
GetSimSlotInfosWithUpdatedEid(cellular_device)) {
if (sim_slot_info.eid.empty()) {
has_psim_slots = true;
iccid_ = sim_slot_info.iccid;
break;
}
}
if (!has_psim_slots) {
NET_LOG(ERROR) << "No PSim slots found";
FinishActivationAttempt(mojom::ActivationResult::kFailedToActivate);
return;
}
// If no SIM card is present, it may be due to the fact that some devices do
// not have hardware support for determining whether a SIM has been inserted.
// Restart the modem to see if the SIM is detected when the modem powers back
// on.
if (iccid_.empty()) {
NET_LOG(DEBUG) << "No SIM detected; restarting modem.";
ShillDeviceClient::Get()->Reset(
dbus::ObjectPath(cellular_device->path()),
base::BindOnce(&OtaActivatorImpl::AttemptNextActivationStep,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&OnModemResetError));
return;
}
ChangeStateAndAttemptNextStep(State::kWaitingForCellularConnection);
}
void OtaActivatorImpl::AttemptConnectionToCellularNetwork() {
DCHECK(state_ == State::kWaitingForCellularConnection);
const NetworkState* cellular_network = GetCellularNetworkState();
// There is no cellular network to be connected; return early and wait for
// NetworkListChanged() to be called if/when one becomes available.
if (!cellular_network)
return;
// The network is disconnected; trigger a connection and wait for
// NetworkPropertiesUpdated() to be called when the network connects.
if (!cellular_network->IsConnectingOrConnected()) {
network_connection_handler_->ConnectToNetwork(
cellular_network->path(), base::DoNothing(),
base::BindOnce(&OnNetworkConnectionError),
false /* check_error_state */, ConnectCallbackMode::ON_STARTED);
return;
}
// The network is connecting; return early and wait for
// NetworkPropertiesUpdated() to be called if/when the network connects.
if (cellular_network->IsConnectingState())
return;
// If the network is already activated, there is no need to complete the rest
// of the flow.
if (cellular_network->activation_state() ==
shill::kActivationStateActivated) {
FinishActivationAttempt(mojom::ActivationResult::kAlreadyActivated);
return;
}
const DeviceState* cellular_device = GetCellularDeviceState();
// The device must have the properties required for the actication flow;
// namely, the operator name and IMEI. Return and wait to see if
// DevicePropertiesUpdated() is invoked with valid properties.
if (cellular_device->operator_name().empty() ||
cellular_device->imei().empty()) {
NET_LOG(DEBUG) << "Insufficient activation data: "
<< "Operator name: " << cellular_device->operator_name()
<< ", IMEI: " << cellular_device->imei();
return;
}
// The network must have payment information; at minimum, a payment URL is
// required in order to contact the carrier payment portal. Return and wait to
// see if NetworkPropertiesUpdated() is invoked with valid properties.
if (cellular_network->payment_url().empty()) {
NET_LOG(DEBUG) << "Insufficient activation data: "
<< "Payment URL: " << cellular_network->payment_url() << ", "
<< "Post Data: " << cellular_network->payment_post_data();
return;
}
ChangeStateAndAttemptNextStep(State::kWaitingForCellularPayment);
}
void OtaActivatorImpl::AttemptToSendMetadataToDelegate() {
DCHECK(state_ == State::kWaitingForCellularPayment);
// Metadata should only be sent to the delegate once.
if (!has_sent_metadata_) {
has_sent_metadata_ = true;
const DeviceState* cellular_device = GetCellularDeviceState();
const NetworkState* cellular_network = GetCellularNetworkState();
NET_LOG(DEBUG) << "Sending CellularMetadata. "
<< "Payment URL: " << cellular_network->payment_url() << ", "
<< "Post data: " << cellular_network->payment_post_data()
<< ", Carrier: " << cellular_device->operator_name() << ", "
<< "MEID: " << cellular_device->meid() << ", "
<< "IMEI: " << cellular_device->imei() << ", "
<< "MDN: " << cellular_device->mdn();
activation_delegate_->OnActivationStarted(mojom::CellularMetadata::New(
GURL(cellular_network->payment_url()),
cellular_network->payment_post_data(), cellular_device->operator_name(),
cellular_device->meid(), cellular_device->imei(),
cellular_device->mdn()));
}
// The user must successfully pay via the carrier portal before continuing.
if (last_carrier_portal_status_ !=
mojom::CarrierPortalStatus::kPortalLoadedAndUserCompletedPayment) {
return;
}
ChangeStateAndAttemptNextStep(State::kWaitingForActivation);
}
void OtaActivatorImpl::AttemptToCompleteActivation() {
DCHECK(state_ == State::kWaitingForActivation);
// CompleteActivation() should only be called once.
if (has_called_complete_activation_)
return;
has_called_complete_activation_ = true;
network_activation_handler_->CompleteActivation(
GetCellularNetworkState()->path(),
base::BindOnce(&OtaActivatorImpl::FinishActivationAttempt,
weak_ptr_factory_.GetWeakPtr(),
mojom::ActivationResult::kSuccessfullyStartedActivation),
base::BindOnce(&OtaActivatorImpl::OnCompleteActivationError,
weak_ptr_factory_.GetWeakPtr()));
}
void OtaActivatorImpl::OnCompleteActivationError(
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data) {
NET_LOG(ERROR) << "CompleteActivation() failed. Error name: " << error_name;
FinishActivationAttempt(mojom::ActivationResult::kFailedToActivate);
}
void OtaActivatorImpl::FlushForTesting() {
if (activation_delegate_)
activation_delegate_.FlushForTesting();
}
std::ostream& operator<<(std::ostream& stream,
const OtaActivatorImpl::State& state) {
switch (state) {
case OtaActivatorImpl::State::kNotYetStarted:
stream << "[Not yet started]";
break;
case OtaActivatorImpl::State::kWaitingForValidSimToBecomePresent:
stream << "[Waiting for SIM to become present]";
break;
case OtaActivatorImpl::State::kWaitingForCellularConnection:
stream << "[Waiting for connected cellular network]";
break;
case OtaActivatorImpl::State::kWaitingForCellularPayment:
stream << "[Waiting cellular payment payment to complete]";
break;
case OtaActivatorImpl::State::kWaitingForActivation:
stream << "[Waiting for Shill activation to complete]";
break;
case OtaActivatorImpl::State::kFinished:
stream << "[Finished]";
break;
}
return stream;
}
} // namespace cellular_setup
} // namespace chromeos