device_state.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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
12namespace chromeos {
13
14DeviceState::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
23DeviceState::~DeviceState() {
24}
25
26bool DeviceState::PropertyChanged(const std::string& key,
27                                  const base::Value& value) {
28  // All property values get stored in |properties_|.
29  properties_.Set(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::kSIMLockStatusProperty) {
69    const base::DictionaryValue* dict = NULL;
70    if (!value.GetAsDictionary(&dict))
71      return false;
72
73    // Return true if at least one of the property values changed.
74    bool property_changed = false;
75    const base::Value* out_value = NULL;
76    if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockRetriesLeftProperty,
77                                       &out_value))
78      return false;
79    if (GetUInt32Value(flimflam::kSIMLockRetriesLeftProperty,
80                       *out_value, &sim_retries_left_))
81      property_changed = true;
82
83    if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockTypeProperty,
84                                       &out_value))
85      return false;
86    if (GetStringValue(flimflam::kSIMLockTypeProperty,
87                       *out_value, &sim_lock_type_))
88      property_changed = true;
89
90    if (!dict->GetWithoutPathExpansion(flimflam::kSIMLockEnabledProperty,
91                                       &out_value))
92      return false;
93    if (GetBooleanValue(flimflam::kSIMLockEnabledProperty,
94                        *out_value, &sim_lock_enabled_))
95      property_changed = true;
96
97    return property_changed;
98  } else if (key == flimflam::kMeidProperty) {
99    return GetStringValue(key, value, &meid_);
100  } else if (key == flimflam::kImeiProperty) {
101    return GetStringValue(key, value, &imei_);
102  } else if (key == flimflam::kIccidProperty) {
103    return GetStringValue(key, value, &iccid_);
104  } else if (key == flimflam::kMdnProperty) {
105    return GetStringValue(key, value, &mdn_);
106  } else if (key == shill::kSIMPresentProperty) {
107    return GetBooleanValue(key, value, &sim_present_);
108  }
109  return false;
110}
111
112bool DeviceState::IsSimAbsent() const {
113  return technology_family_ == flimflam::kTechnologyFamilyGsm && !sim_present_;
114}
115
116}  // namespace chromeos
117