shill: cellular: Report first available retry count.

If no count is available for the current SIM lock type but some count is
available, report it, as it is better than reporting 999.

BUG=chromium:253988
TEST=Build and run unit tests.

Change-Id: I5f02b37cc3550561b44bc1313b535883d32cbc7e
Reviewed-on: https://gerrit.chromium.org/gerrit/59930
Reviewed-by: Thieu Le <thieule@chromium.org>
Tested-by: Arman Uguray <armansito@chromium.org>
Commit-Queue: Arman Uguray <armansito@chromium.org>
diff --git a/cellular_capability_universal.cc b/cellular_capability_universal.cc
index aa1dc77..2c674a8 100644
--- a/cellular_capability_universal.cc
+++ b/cellular_capability_universal.cc
@@ -1526,18 +1526,25 @@
 
   // Unlock required and SimLock
   uint32 unlock_required;  // This is really of type MMModemLock
+  bool lock_status_changed = false;
   if (DBusProperties::GetUint32(properties,
                                 MM_MODEM_PROPERTY_UNLOCKREQUIRED,
-                                &unlock_required))
+                                &unlock_required)) {
     OnLockTypeChanged(static_cast<MMModemLock>(unlock_required));
+    lock_status_changed = true;
+  }
 
   // Unlock retries
   it = properties.find(MM_MODEM_PROPERTY_UNLOCKRETRIES);
   if (it != properties.end()) {
     LockRetryData lock_retries = it->second.operator LockRetryData();
     OnLockRetriesChanged(lock_retries);
+    lock_status_changed = true;
   }
 
+  if (lock_status_changed)
+    OnSimLockStatusChanged();
+
   if (DBusProperties::GetUint32(properties,
                                 MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES,
                                 &uint_value))
@@ -1753,20 +1760,18 @@
     const LockRetryData &lock_retries) {
   SLOG(Cellular, 2) << __func__;
 
-  // Look for the retries left for the current lock. If the SIM
-  // is not locked, then pick the first available value.
-  LockRetryData::const_iterator it;
-  if (sim_lock_status_.lock_type != MM_MODEM_LOCK_NONE &&
-      sim_lock_status_.lock_type != MM_MODEM_LOCK_UNKNOWN)
-      it = lock_retries.find(sim_lock_status_.lock_type);
-  else
+  // Look for the retries left for the current lock. Try the obtain the count
+  // that matches the current count. If no count for the current lock is
+  // available, report the first one in the dictionary.
+  LockRetryData::const_iterator it =
+      lock_retries.find(sim_lock_status_.lock_type);
+  if (it == lock_retries.end())
       it = lock_retries.begin();
   if (it != lock_retries.end())
     sim_lock_status_.retries_left = it->second;
   else
     // Unknown, use 999
     sim_lock_status_.retries_left = 999;
-  OnSimLockStatusChanged();
 }
 
 void CellularCapabilityUniversal::OnLockTypeChanged(
@@ -1782,7 +1787,6 @@
       lock_type != MM_MODEM_LOCK_UNKNOWN &&
       !sim_lock_status_.enabled)
     sim_lock_status_.enabled = true;
-  OnSimLockStatusChanged();
 }
 
 void CellularCapabilityUniversal::OnSimLockStatusChanged() {
diff --git a/cellular_capability_universal_unittest.cc b/cellular_capability_universal_unittest.cc
index 825a3b3..d4fb59a 100644
--- a/cellular_capability_universal_unittest.cc
+++ b/cellular_capability_universal_unittest.cc
@@ -2115,12 +2115,13 @@
   EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
 
   data[MM_MODEM_LOCK_SIM_PIN] = 3;
+  data[MM_MODEM_LOCK_SIM_PUK] = 10;
   capability_->OnLockRetriesChanged(data);
   EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
 
-  capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN2;
+  capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK;
   capability_->OnLockRetriesChanged(data);
-  EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
+  EXPECT_EQ(10, capability_->sim_lock_status_.retries_left);
 
   capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
   capability_->OnLockRetriesChanged(data);
@@ -2193,7 +2194,7 @@
   EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
 
   // Unlock retries changed with a value that doesn't match the current
-  // lock type. Default to 999.
+  // lock type. Default to whatever count is available.
   retry_data.clear();
   retry_data[MM_MODEM_LOCK_SIM_PIN2] = 2;
   variant.clear();
@@ -2202,7 +2203,7 @@
   changed[MM_MODEM_PROPERTY_UNLOCKRETRIES] = variant;
   capability_->OnModemPropertiesChanged(changed, invalidated);
   EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
-  EXPECT_EQ(999, capability_->sim_lock_status_.retries_left);
+  EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
 }
 
 }  // namespace shill