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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/network/cellular_connection_handler.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chromeos/ash/components/dbus/hermes/hermes_euicc_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_profile_client.h"
#include "chromeos/ash/components/network/cellular_esim_profile_handler.h"
#include "chromeos/ash/components/network/cellular_inhibitor.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/hermes_metrics_util.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "third_party/cros_system_api/dbus/hermes/dbus-constants.h"
namespace ash {
namespace {
constexpr base::TimeDelta kWaitingForConnectableTimeout = base::Seconds(30);
bool CanInitiateShillConnection(const NetworkState* network) {
// The network must be part of a Shill profile (i.e., it cannot be a "stub"
// network which is not exposed by Shill).
if (network->IsNonProfileType())
return false;
// The Connectable property must be set to true, indicating that the network
// is the active SIM profile in its slot.
return network->connectable();
}
std::optional<dbus::ObjectPath> GetEuiccPath(const std::string& eid) {
const std::vector<dbus::ObjectPath>& euicc_paths =
HermesManagerClient::Get()->GetAvailableEuiccs();
for (const auto& euicc_path : euicc_paths) {
HermesEuiccClient::Properties* euicc_properties =
HermesEuiccClient::Get()->GetProperties(euicc_path);
if (euicc_properties && euicc_properties->eid().value() == eid)
return euicc_path;
}
return std::nullopt;
}
std::optional<dbus::ObjectPath> GetProfilePath(const std::string& eid,
const std::string& iccid) {
std::optional<dbus::ObjectPath> euicc_path = GetEuiccPath(eid);
if (!euicc_path)
return std::nullopt;
HermesEuiccClient::Properties* euicc_properties =
HermesEuiccClient::Get()->GetProperties(*euicc_path);
if (!euicc_properties)
return std::nullopt;
const std::vector<dbus::ObjectPath>& profile_paths =
euicc_properties->profiles().value();
for (const auto& profile_path : profile_paths) {
HermesProfileClient::Properties* profile_properties =
HermesProfileClient::Get()->GetProperties(profile_path);
if (profile_properties && profile_properties->iccid().value() == iccid) {
return profile_path;
}
}
return std::nullopt;
}
} // namespace
// static
const base::TimeDelta CellularConnectionHandler::kWaitingForAutoConnectTimeout =
base::Minutes(2);
std::optional<std::string> CellularConnectionHandler::ResultToErrorString(
PrepareCellularConnectionResult result) {
switch (result) {
case PrepareCellularConnectionResult::kSuccess:
return std::nullopt;
case PrepareCellularConnectionResult::kCouldNotFindNetworkWithIccid:
return NetworkConnectionHandler::kErrorNotFound;
case PrepareCellularConnectionResult::kInhibitFailed:
return NetworkConnectionHandler::kErrorCellularInhibitFailure;
case PrepareCellularConnectionResult::kSimLocked:
return NetworkConnectionHandler::kErrorSimPinPukLocked;
case PrepareCellularConnectionResult::kCouldNotFindRelevantEuicc:
[[fallthrough]];
case PrepareCellularConnectionResult::kRefreshProfilesFailed:
[[fallthrough]];
case PrepareCellularConnectionResult::kCouldNotFindRelevantESimProfile:
[[fallthrough]];
case PrepareCellularConnectionResult::kEnableProfileFailed:
return NetworkConnectionHandler::kErrorESimProfileIssue;
case PrepareCellularConnectionResult::kTimeoutWaitingForConnectable:
return NetworkConnectionHandler::kConnectableCellularTimeout;
}
}
CellularConnectionHandler::ConnectionRequestMetadata::ConnectionRequestMetadata(
const std::string& iccid,
SuccessCallback success_callback,
ErrorCallback error_callback)
: iccid(iccid),
success_callback(std::move(success_callback)),
error_callback(std::move(error_callback)) {}
CellularConnectionHandler::ConnectionRequestMetadata::ConnectionRequestMetadata(
const dbus::ObjectPath& euicc_path,
const dbus::ObjectPath& profile_path,
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock,
SuccessCallback success_callback,
ErrorCallback error_callback)
: euicc_path(euicc_path),
profile_path(profile_path),
inhibit_lock(std::move(inhibit_lock)),
success_callback(std::move(success_callback)),
error_callback(std::move(error_callback)) {}
CellularConnectionHandler::ConnectionRequestMetadata::
~ConnectionRequestMetadata() = default;
CellularConnectionHandler::CellularConnectionHandler() = default;
CellularConnectionHandler::~CellularConnectionHandler() = default;
void CellularConnectionHandler::Init(
NetworkStateHandler* network_state_handler,
CellularInhibitor* cellular_inhibitor,
CellularESimProfileHandler* cellular_esim_profile_handler) {
network_state_handler_ = network_state_handler;
cellular_inhibitor_ = cellular_inhibitor;
cellular_esim_profile_handler_ = cellular_esim_profile_handler;
network_state_handler_observer_.Observe(network_state_handler_.get());
}
void CellularConnectionHandler::PrepareExistingCellularNetworkForConnection(
const std::string& iccid,
SuccessCallback success_callback,
ErrorCallback error_callback) {
request_queue_.push(std::make_unique<ConnectionRequestMetadata>(
iccid, std::move(success_callback), std::move(error_callback)));
ProcessRequestQueue();
}
void CellularConnectionHandler::
PrepareNewlyInstalledCellularNetworkForConnection(
const dbus::ObjectPath& euicc_path,
const dbus::ObjectPath& profile_path,
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock,
SuccessCallback success_callback,
ErrorCallback error_callback) {
request_queue_.push(std::make_unique<ConnectionRequestMetadata>(
euicc_path, profile_path, std::move(inhibit_lock),
std::move(success_callback), std::move(error_callback)));
ProcessRequestQueue();
}
void CellularConnectionHandler::NetworkListChanged() {
HandleNetworkPropertiesUpdate();
}
void CellularConnectionHandler::NetworkPropertiesUpdated(
const NetworkState* network) {
HandleNetworkPropertiesUpdate();
}
void CellularConnectionHandler::NetworkIdentifierTransitioned(
const std::string& old_service_path,
const std::string& new_service_path,
const std::string& old_guid,
const std::string& new_guid) {
HandleNetworkPropertiesUpdate();
}
void CellularConnectionHandler::ProcessRequestQueue() {
// No requests to process.
if (request_queue_.empty())
return;
// A request is already being processed; wait until that one is finished
// before processing any additional requests.
if (state_ != ConnectionState::kIdle)
return;
const ConnectionRequestMetadata* current_request =
request_queue_.front().get();
// If the request has an ICCID, it is an existing network. Start by checking
// the service status.
if (current_request->iccid) {
TransitionToConnectionState(ConnectionState::kCheckingServiceStatus);
CheckServiceStatus();
return;
}
// Otherwise, this is a newly-installed profile, so we can skip straight to
// enabling it.
DCHECK(current_request->inhibit_lock);
DCHECK(current_request->euicc_path);
DCHECK(current_request->profile_path);
TransitionToConnectionState(ConnectionState::kEnablingProfile);
EnableProfile();
}
void CellularConnectionHandler::TransitionToConnectionState(
ConnectionState state) {
NET_LOG(EVENT) << "CellularConnectionHandler state: " << state_ << " => "
<< state;
state_ = state;
}
void CellularConnectionHandler::CompleteConnectionAttempt(
PrepareCellularConnectionResult result,
bool auto_connected) {
DCHECK(state_ != ConnectionState::kIdle);
DCHECK(!request_queue_.empty());
base::UmaHistogramEnumeration(
"Network.Cellular.PrepareCellularConnection.OperationResult", result);
if (timer_.IsRunning())
timer_.Stop();
std::string service_path;
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
if (network_state)
service_path = network_state->path();
TransitionToConnectionState(ConnectionState::kIdle);
std::unique_ptr<ConnectionRequestMetadata> metadata =
std::move(request_queue_.front());
request_queue_.pop();
const std::optional<std::string> error_name = ResultToErrorString(result);
if (error_name) {
std::move(metadata->error_callback).Run(service_path, *error_name);
} else if (service_path.empty()) {
std::move(metadata->error_callback)
.Run(service_path, NetworkConnectionHandler::kErrorNotFound);
} else {
std::move(metadata->success_callback).Run(service_path, auto_connected);
}
ProcessRequestQueue();
// In case of errors, metadata will be destroyed at this point along with
// it's inhibit_lock and the cellular device will uninhibit automatically.
}
const NetworkState*
CellularConnectionHandler::GetNetworkStateForCurrentOperation() const {
if (request_queue_.empty())
return nullptr;
std::string iccid;
const ConnectionRequestMetadata* current_request =
request_queue_.front().get();
if (current_request->iccid) {
iccid = *current_request->iccid;
} else {
iccid = HermesProfileClient::Get()
->GetProperties(*current_request->profile_path)
->iccid()
.value();
}
DCHECK(!iccid.empty());
NetworkStateHandler::NetworkStateList network_list;
network_state_handler_->GetVisibleNetworkListByType(
NetworkTypePattern::Cellular(), &network_list);
for (const NetworkState* network : network_list) {
if (network->iccid() == iccid)
return network;
}
return nullptr;
}
std::optional<dbus::ObjectPath>
CellularConnectionHandler::GetEuiccPathForCurrentOperation() const {
const ConnectionRequestMetadata* current_request =
request_queue_.front().get();
if (current_request->euicc_path) {
return current_request->euicc_path;
}
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
if (!network_state)
return std::nullopt;
return GetEuiccPath(network_state->eid());
}
std::optional<dbus::ObjectPath>
CellularConnectionHandler::GetProfilePathForCurrentOperation() const {
const ConnectionRequestMetadata* current_request =
request_queue_.front().get();
if (current_request->profile_path) {
return current_request->profile_path;
}
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
if (!network_state)
return std::nullopt;
return GetProfilePath(network_state->eid(), network_state->iccid());
}
void CellularConnectionHandler::CheckServiceStatus() {
DCHECK_EQ(state_, ConnectionState::kCheckingServiceStatus);
const std::string& iccid = *request_queue_.front()->iccid;
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
if (!network_state) {
NET_LOG(ERROR) << "Could not find network for ICCID "
<< *request_queue_.front()->iccid;
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kCouldNotFindNetworkWithIccid,
/*auto_connected=*/false);
return;
}
if (CanInitiateShillConnection(network_state)) {
NET_LOG(USER) << "Cellular service with ICCID " << iccid
<< " is connectable";
CompleteConnectionAttempt(PrepareCellularConnectionResult::kSuccess,
/*auto_connected=*/false);
return;
}
NET_LOG(USER) << "Starting cellular connection flow. ICCID: " << iccid
<< ", Service path: " << network_state->path()
<< ", EID: " << network_state->eid();
// If this is a pSIM network, we expect that Shill will eventually expose a
// connectable Service corresponding to this network. Invoking
// CheckForConnectable() starts a timeout for this process in case this never
// ends up occurring.
if (network_state->eid().empty()) {
NET_LOG(EVENT) << "Waiting for connectable pSIM network";
TransitionToConnectionState(ConnectionState::kWaitingForConnectable);
CheckForConnectable();
return;
}
DCHECK(!request_queue_.front()->inhibit_lock);
TransitionToConnectionState(ConnectionState::kInhibitingScans);
cellular_inhibitor_->InhibitCellularScanning(
CellularInhibitor::InhibitReason::kConnectingToProfile,
base::BindOnce(&CellularConnectionHandler::OnInhibitScanResult,
weak_ptr_factory_.GetWeakPtr()));
}
void CellularConnectionHandler::OnInhibitScanResult(
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock) {
DCHECK_EQ(state_, ConnectionState::kInhibitingScans);
if (!inhibit_lock) {
NET_LOG(ERROR) << "eSIM connection flow failed to inhibit scan";
CompleteConnectionAttempt(PrepareCellularConnectionResult::kInhibitFailed,
/*auto_connected=*/false);
return;
}
request_queue_.front()->inhibit_lock = std::move(inhibit_lock);
TransitionToConnectionState(
ConnectionState::kRequestingProfilesBeforeEnabling);
RequestInstalledProfiles();
}
void CellularConnectionHandler::RequestInstalledProfiles() {
std::optional<dbus::ObjectPath> euicc_path =
GetEuiccPathForCurrentOperation();
if (!euicc_path) {
NET_LOG(ERROR) << "eSIM connection flow could not find relevant EUICC";
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kCouldNotFindRelevantEuicc,
/*auto_connected=*/false);
return;
}
cellular_esim_profile_handler_->RefreshProfileList(
*euicc_path,
base::BindOnce(&CellularConnectionHandler::OnRefreshProfileListResult,
weak_ptr_factory_.GetWeakPtr()),
std::move(request_queue_.front()->inhibit_lock));
}
void CellularConnectionHandler::OnRefreshProfileListResult(
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock) {
DCHECK_EQ(ConnectionState::kRequestingProfilesBeforeEnabling, state_);
if (!inhibit_lock) {
NET_LOG(ERROR) << "eSIM connection flow failed to request profiles";
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kRefreshProfilesFailed,
/*auto_connected=*/false);
return;
}
request_queue_.front()->inhibit_lock = std::move(inhibit_lock);
TransitionToConnectionState(ConnectionState::kEnablingProfile);
EnableProfile();
}
void CellularConnectionHandler::EnableProfile() {
std::optional<dbus::ObjectPath> profile_path =
GetProfilePathForCurrentOperation();
if (!profile_path) {
NET_LOG(ERROR) << "eSIM connection flow could not find profile";
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kCouldNotFindRelevantESimProfile,
/*auto_connected=*/false);
return;
}
HermesProfileClient::Get()->EnableCarrierProfile(
*profile_path,
base::BindOnce(&CellularConnectionHandler::OnEnableCarrierProfileResult,
weak_ptr_factory_.GetWeakPtr()));
}
void CellularConnectionHandler::OnEnableCarrierProfileResult(
HermesResponseStatus status) {
DCHECK_EQ(state_, ConnectionState::kEnablingProfile);
hermes_metrics::LogEnableProfileResult(status);
// If we try to enable and "fail" with an already-enabled error, count this as
// a success.
bool success = status == HermesResponseStatus::kSuccess ||
status == HermesResponseStatus::kErrorAlreadyEnabled;
if (!success) {
NET_LOG(ERROR) << "eSIM connection flow failed to enable profile";
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kEnableProfileFailed,
/*auto_connected=*/false);
return;
}
// kErrorAlreadyEnabled implies that the SIM profile was already enabled.
request_queue_.front()->did_connection_require_enabling_profile =
status == HermesResponseStatus::kSuccess;
// Reset the inhibit_lock so that the device will be uninhibited
// automatically.
request_queue_.front()->inhibit_lock.reset();
TransitionToConnectionState(ConnectionState::kWaitingForConnectable);
CheckForConnectable();
}
void CellularConnectionHandler::HandleNetworkPropertiesUpdate() {
if (state_ == ConnectionState::kWaitingForConnectable) {
CheckForConnectable();
}
}
void CellularConnectionHandler::NetworkConnectionStateChanged(
const NetworkState* network) {
if (state_ == ConnectionState::kWaitingForShillAutoConnect) {
CheckForAutoConnected();
}
}
void CellularConnectionHandler::DevicePropertiesUpdated(
const DeviceState* device) {
if (device->type() != shill::kTypeCellular ||
state_ != ConnectionState::kWaitingForConnectable) {
return;
}
CheckForConnectable();
}
void CellularConnectionHandler::CheckForConnectable() {
DCHECK_EQ(state_, ConnectionState::kWaitingForConnectable);
// If the cellular device is locked, exit early, there's no need to wait for
// the network to be connectable.
const DeviceState* cellular_device =
network_state_handler_->GetDeviceStateByType(
NetworkTypePattern::Cellular());
DCHECK(cellular_device);
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
// We can check the cellular device lock status since the current SIM is the
// active SIM.
if (cellular_device->IsSimLocked() && network_state &&
cellular_device->iccid() == network_state->iccid()) {
CompleteConnectionAttempt(PrepareCellularConnectionResult::kSimLocked,
/*auto_connected=*/false);
return;
}
if (network_state && CanInitiateShillConnection(network_state)) {
if (!request_queue_.front()->did_connection_require_enabling_profile) {
CompleteConnectionAttempt(PrepareCellularConnectionResult::kSuccess,
/*auto_connected=*/false);
} else {
StartWaitingForShillAutoConnect();
}
return;
}
// If network is not connectable or if network state is not available for a
// newly installed profile, start a timer and wait for the network to become
// available and connectable.
if (!timer_.IsRunning()) {
timer_.Start(
FROM_HERE, kWaitingForConnectableTimeout,
base::BindOnce(&CellularConnectionHandler::OnWaitForConnectableTimeout,
weak_ptr_factory_.GetWeakPtr()));
}
}
void CellularConnectionHandler::OnWaitForConnectableTimeout() {
DCHECK_EQ(state_, ConnectionState::kWaitingForConnectable);
NET_LOG(ERROR) << "Cellular connection timed out waiting for network to "
"become connectable";
CompleteConnectionAttempt(
PrepareCellularConnectionResult::kTimeoutWaitingForConnectable,
/*auto_connected=*/false);
}
void CellularConnectionHandler::StartWaitingForShillAutoConnect() {
// Stop the timer that wait for the network to become connectable.
if (timer_.IsRunning()) {
timer_.Stop();
}
TransitionToConnectionState(ConnectionState::kWaitingForShillAutoConnect);
CheckForAutoConnected();
}
void CellularConnectionHandler::CheckForAutoConnected() {
CHECK_EQ(state_, ConnectionState::kWaitingForShillAutoConnect);
const NetworkState* network_state = GetNetworkStateForCurrentOperation();
if (network_state && network_state->IsConnectedState()) {
CompleteConnectionAttempt(PrepareCellularConnectionResult::kSuccess,
/*auto_connected=*/true);
return;
}
// If network hasn't autoconnected by Shill yet, start a timer and wait for
// the network to become connected.
if (!timer_.IsRunning()) {
timer_.Start(
FROM_HERE, kWaitingForAutoConnectTimeout,
base::BindOnce(&CellularConnectionHandler::OnWaitForAutoConnectTimeout,
weak_ptr_factory_.GetWeakPtr()));
}
}
void CellularConnectionHandler::OnWaitForAutoConnectTimeout() {
DCHECK_EQ(state_, ConnectionState::kWaitingForShillAutoConnect);
NET_LOG(ERROR) << "Cellular connection timed out waiting for network to "
"become auto connected";
CompleteConnectionAttempt(PrepareCellularConnectionResult::kSuccess,
/*auto_connected=*/false);
}
std::ostream& operator<<(
std::ostream& stream,
const CellularConnectionHandler::ConnectionState& state) {
switch (state) {
case CellularConnectionHandler::ConnectionState::kIdle:
stream << "[Idle]";
break;
case CellularConnectionHandler::ConnectionState::kCheckingServiceStatus:
stream << "[Checking service status]";
break;
case CellularConnectionHandler::ConnectionState::kInhibitingScans:
stream << "[Inhibiting scans]";
break;
case CellularConnectionHandler::ConnectionState::
kRequestingProfilesBeforeEnabling:
stream << "[Requesting profiles before enabling]";
break;
case CellularConnectionHandler::ConnectionState::kEnablingProfile:
stream << "[Enabling profile]";
break;
case CellularConnectionHandler::ConnectionState::kWaitingForConnectable:
stream << "[Waiting for network to become connectable]";
break;
case CellularConnectionHandler::ConnectionState::
kWaitingForShillAutoConnect:
stream << "[Waiting for network to become auto-connected]";
break;
}
return stream;
}
} // namespace ash
|