| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium OS 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 "src/device.h" |
| 6 |
| 7 #include <glog/logging.h> |
| 8 |
| 9 #include "src/service.h" |
| 10 |
| 11 namespace cashew { |
| 12 |
| 13 const char* Device::kCarrierUnknown = "Unknown"; |
| 14 |
| 15 // Flimflam Device D-Bus identifiers |
| 16 static const char* kFlimflamDeviceName = "org.chromium.flimflam"; |
| 17 |
| 18 // Flimflam Device property names |
| 19 static const char* kFlimflamDeviceCarrierProperty = "Cellular.Carrier"; |
| 20 static const char* kFlimflamDeviceTypeProperty = "Type"; |
| 21 |
| 22 // Flimflam Device on-the-wire Type values |
| 23 static const char* kFlimflamDeviceTypeEthernet = "ethernet"; |
| 24 static const char* kFlimflamDeviceTypeWifi = "wifi"; |
| 25 static const char* kFlimflamDeviceTypeWimax = "wimax"; |
| 26 static const char* kFlimflamDeviceTypeBluetooth = "bluetooth"; |
| 27 static const char* kFlimflamDeviceTypeGPS = "gps"; |
| 28 static const char* kFlimflamDeviceTypeCellular = "cellular"; |
| 29 // TODO(vlaviano): what does vendor look like? |
| 30 static const char* kFlimflamDeviceTypeVendor = ""; |
| 31 |
| 32 Device::Device(DBus::Connection& connection, // NOLINT |
| 33 const DBus::Path& path) |
| 34 : DBus::ObjectProxy(connection, path, kFlimflamDeviceName), |
| 35 path_(path), type_(kTypeUnknown), carrier_(kCarrierUnknown) { |
| 36 // init our state with a GetProperties() call to our Flimflam service path |
| 37 // we'll update this state by monitoring PropertyChanged signals |
| 38 GetDeviceProperties(); |
| 39 } |
| 40 |
| 41 Device::~Device() { |
| 42 } |
| 43 |
| 44 const DBus::Path& Device::GetPath() const { |
| 45 return path_; |
| 46 } |
| 47 |
| 48 Device::Type Device::GetType() const { |
| 49 return type_; |
| 50 } |
| 51 |
| 52 const std::string& Device::GetCarrier() const { |
| 53 return carrier_; |
| 54 } |
| 55 |
| 56 // Flimflam Device D-Bus Proxy methods |
| 57 void Device::PropertyChanged(const std::string& property_name, |
| 58 const DBus::Variant& new_value) { |
| 59 DLOG(INFO) << path_ << ": PropertyChanged: property_name = " << property_name; |
| 60 if (property_name.compare(kFlimflamDeviceCarrierProperty) == 0) { |
| 61 OnCarrierUpdate(new_value.reader().get_string()); |
| 62 } else if (property_name.compare(kFlimflamDeviceTypeProperty) == 0) { |
| 63 OnTypeUpdate(new_value.reader().get_string()); |
| 64 } else { |
| 65 // we don't care about this property |
| 66 } |
| 67 } |
| 68 |
| 69 // Private methods |
| 70 |
| 71 Device::Type Device::TypeFromString(const std::string& type) const { |
| 72 if (type.compare(kFlimflamDeviceTypeEthernet) == 0) { |
| 73 return kTypeEthernet; |
| 74 } |
| 75 if (type.compare(kFlimflamDeviceTypeWifi) == 0) { |
| 76 return kTypeWifi; |
| 77 } |
| 78 if (type.compare(kFlimflamDeviceTypeWimax) == 0) { |
| 79 return kTypeWimax; |
| 80 } |
| 81 if (type.compare(kFlimflamDeviceTypeBluetooth) == 0) { |
| 82 return kTypeBluetooth; |
| 83 } |
| 84 if (type.compare(kFlimflamDeviceTypeGPS) == 0) { |
| 85 return kTypeGPS; |
| 86 } |
| 87 if (type.compare(kFlimflamDeviceTypeCellular) == 0) { |
| 88 return kTypeCellular; |
| 89 } |
| 90 if (type.compare(kFlimflamDeviceTypeVendor) == 0) { |
| 91 return kTypeVendor; |
| 92 } |
| 93 return kTypeUnknown; |
| 94 } |
| 95 |
| 96 void Device::OnCarrierUpdate(const std::string& carrier) { |
| 97 DLOG(INFO) << path_ << ": OnCarrierUpdate: carrier = " << carrier; |
| 98 if (carrier_.compare(kCarrierUnknown) && carrier_.compare(carrier)) { |
| 99 LOG(WARNING) << path_ << ": OnCarrierUpdate: carrier change from " |
| 100 << carrier_ << " to " << carrier << "!"; |
| 101 } |
| 102 // TODO(vlaviano): load backend module for this carrier if needed |
| 103 carrier_ = carrier; |
| 104 } |
| 105 |
| 106 void Device::OnTypeUpdate(const std::string& type) { |
| 107 DLOG(INFO) << path_ << ": OnTypeUpdate: type = " << type; |
| 108 type_ = TypeFromString(type); |
| 109 if (type_ != kTypeCellular) { |
| 110 LOG(WARNING) << path_ << ": OnTypeUpdate: type is not cellular!"; |
| 111 } |
| 112 } |
| 113 |
| 114 void Device::GetDeviceProperties() { |
| 115 DLOG(INFO) << path_ << ": GetDeviceProperties"; |
| 116 PropertyMap properties; |
| 117 // dbus-c++ throws exceptions |
| 118 // invoke the "Existing Non-conformant Code" clause of the style guide and |
| 119 // isolate the rest of the system from this |
| 120 try { |
| 121 // TODO(vlaviano): make this call asynchronous |
| 122 properties = GetProperties(); |
| 123 } catch (DBus::Error& error) { // NOLINT |
| 124 LOG(WARNING) << path_ |
| 125 << ": GetDeviceProperties: GetProperties() -> Exception: " |
| 126 << error.name() << ": " << error.message(); |
| 127 // TODO(vlaviano): schedule another attempt later |
| 128 return; |
| 129 } catch (...) { // NOLINT |
| 130 LOG(WARNING) << path_ |
| 131 << ": GetDeviceProperties: GetProperties() -> Exception"; |
| 132 // TODO(vlaviano): schedule another attempt later |
| 133 return; |
| 134 } |
| 135 DLOG(INFO) << "Received " << properties.size() << " properties"; |
| 136 |
| 137 // grab the properties in which we're interested |
| 138 PropertyMap::const_iterator it; |
| 139 it = properties.find(kFlimflamDeviceTypeProperty); |
| 140 if (it != properties.end()) { |
| 141 const DBus::Variant& value = static_cast<DBus::Variant>(it->second); |
| 142 OnTypeUpdate(value.reader().get_string()); |
| 143 } else { |
| 144 DLOG(WARNING) << path_ << ": GetDeviceProperties: no Type property"; |
| 145 } |
| 146 // don't expect to find Cellular.* properties if we're not a cellular device |
| 147 if (type_ != kTypeCellular) { |
| 148 return; |
| 149 } |
| 150 it = properties.find(kFlimflamDeviceCarrierProperty); |
| 151 if (it != properties.end()) { |
| 152 const DBus::Variant& value = static_cast<DBus::Variant>(it->second); |
| 153 OnCarrierUpdate(value.reader().get_string()); |
| 154 } else { |
| 155 DLOG(WARNING) << path_ << ": GetDeviceProperties: no Carrier property"; |
| 156 } |
| 157 } |
| 158 |
| 159 } // namespace cashew |
| OLD | NEW |