1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chromeos/network/device_state.h" 6 7 #include "base/logging.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/values.h" 10 #include "third_party/cros_system_api/dbus/service_constants.h" 11 12 namespace chromeos { 13 14 DeviceState::DeviceState(const std::string& path) 15 : ManagedState(MANAGED_TYPE_DEVICE, path), 16 provider_requires_roaming_(false), 17 support_network_scan_(false), 18 scanning_(false), 19 sim_lock_enabled_(false), 20 sim_present_(true) { 21 } 22 23 DeviceState::~DeviceState() { 24 } 25 26 bool DeviceState::PropertyChanged(const std::string& key, 27 const base::Value& value) { 28 // All property values get stored in |properties_|. 29 properties_.SetWithoutPathExpansion(key, value.DeepCopy()); 30 31 if (ManagedStatePropertyChanged(key, value)) 32 return true; 33 if (key == flimflam::kAddressProperty) { 34 return GetStringValue(key, value, &mac_address_); 35 } else if (key == flimflam::kScanningProperty) { 36 return GetBooleanValue(key, value, &scanning_); 37 } else if (key == flimflam::kSupportNetworkScanProperty) { 38 return GetBooleanValue(key, value, &support_network_scan_); 39 } else if (key == shill::kProviderRequiresRoamingProperty) { 40 return GetBooleanValue(key, value, &provider_requires_roaming_); 41 } else if (key == flimflam::kHomeProviderProperty) { 42 const base::DictionaryValue* dict = NULL; 43 if (!value.GetAsDictionary(&dict)) 44 return false; 45 std::string home_provider_country; 46 std::string home_provider_name; 47 dict->GetStringWithoutPathExpansion(flimflam::kOperatorCountryKey, 48 &home_provider_country); 49 dict->GetStringWithoutPathExpansion(flimflam::kOperatorNameKey, 50 &home_provider_name); 51 // Set home_provider_id_ 52 if (!home_provider_name.empty() && !home_provider_country.empty()) { 53 home_provider_id_ = base::StringPrintf( 54 "%s (%s)", 55 home_provider_name.c_str(), 56 home_provider_country.c_str()); 57 } else { 58 dict->GetStringWithoutPathExpansion(flimflam::kOperatorCodeKey, 59 &home_provider_id_); 60 LOG(WARNING) << "Carrier ID not defined, using code instead: " 61 << home_provider_id_; 62 } 63 return true; 64 } else if (key == flimflam::kTechnologyFamilyProperty) { 65 return GetStringValue(key, value, &technology_family_); 66 } else if (key == flimflam::kCarrierProperty) { 67 return GetStringValue(key, value, &carrier_); 68 } else if (key == flimflam::kFoundNetworksProperty) { 69 const base::ListValue* list = NULL; 70 if (!value.GetAsList(&list)) 71 return false; 72 CellularScanResults parsed_results; 73 if (!network_util::ParseCellularScanResults(*list, &parsed_results)) 74 return false; 75 scan_results_.swap(parsed_results); 76 return true; 77 } else if (key == flimflam::kSIMLockStatusProperty) { 78 const base::DictionaryValue* dict = NULL; 79 if (!value.GetAsDictionary(&dict)) 80 return false; 81 82 // Return true if at least one of the property values changed. 83 bool property_changed = false; 84 const base::Value* out_value = NULL; 85 if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockRetriesLeftProperty, 86 &out_value)) 87 return false; 88 if (GetUInt32Value(flimflam::kSIMLockRetriesLeftProperty, 89 *out_value, &sim_retries_left_)) 90 property_changed = true; 91 92 if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockTypeProperty, 93 &out_value)) 94 return false; 95 if (GetStringValue(flimflam::kSIMLockTypeProperty, 96 *out_value, &sim_lock_type_)) 97 property_changed = true; 98 99 if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockEnabledProperty, 100 &out_value)) 101 return false; 102 if (GetBooleanValue(flimflam::kSIMLockEnabledProperty, 103 *out_value, &sim_lock_enabled_)) 104 property_changed = true; 105 106 return property_changed; 107 } else if (key == flimflam::kMeidProperty) { 108 return GetStringValue(key, value, &meid_); 109 } else if (key == flimflam::kImeiProperty) { 110 return GetStringValue(key, value, &imei_); 111 } else if (key == flimflam::kIccidProperty) { 112 return GetStringValue(key, value, &iccid_); 113 } else if (key == flimflam::kMdnProperty) { 114 return GetStringValue(key, value, &mdn_); 115 } else if (key == shill::kSIMPresentProperty) { 116 return GetBooleanValue(key, value, &sim_present_); 117 } 118 return false; 119 } 120 121 bool DeviceState::IsSimAbsent() const { 122 return technology_family_ == flimflam::kTechnologyFamilyGsm && !sim_present_; 123 } 124 125 } // namespace chromeos 126