David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/modem.h" |
| 6 | |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame^] | 7 | #include <base/file_util.h> |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 8 | #include <mm/ModemManager-enums.h> |
| 9 | #include <mm/ModemManager-names.h> |
| 10 | |
| 11 | #include "shill/cellular.h" |
| 12 | |
| 13 | using std::string; |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | Modem1::Modem1(const string &owner, |
| 18 | const string &path, |
| 19 | ControlInterface *control_interface, |
| 20 | EventDispatcher *dispatcher, |
| 21 | Metrics *metrics, |
| 22 | Manager *manager, |
| 23 | mobile_provider_db *provider_db) |
| 24 | : Modem(owner, path, control_interface, dispatcher, metrics, manager, |
| 25 | provider_db) { |
| 26 | } |
| 27 | |
| 28 | Modem1::~Modem1() {} |
| 29 | |
| 30 | Cellular::ModemState Modem1::ConvertMmToCellularModemState(uint32 input) const { |
| 31 | switch (input) { |
| 32 | case MM_MODEM_STATE_UNKNOWN: return Cellular::kModemStateUnknown; |
| 33 | case MM_MODEM_STATE_INITIALIZING: return Cellular::kModemStateInitializing; |
| 34 | case MM_MODEM_STATE_LOCKED: return Cellular::kModemStateLocked; |
| 35 | case MM_MODEM_STATE_DISABLED: return Cellular::kModemStateDisabled; |
| 36 | case MM_MODEM_STATE_DISABLING: return Cellular::kModemStateDisabling; |
| 37 | case MM_MODEM_STATE_ENABLING: return Cellular::kModemStateEnabling; |
| 38 | case MM_MODEM_STATE_ENABLED: return Cellular::kModemStateEnabled; |
| 39 | case MM_MODEM_STATE_SEARCHING: return Cellular::kModemStateSearching; |
| 40 | case MM_MODEM_STATE_REGISTERED: return Cellular::kModemStateRegistered; |
| 41 | case MM_MODEM_STATE_DISCONNECTING: |
| 42 | return Cellular::kModemStateDisconnecting; |
| 43 | case MM_MODEM_STATE_CONNECTING: return Cellular::kModemStateConnecting; |
| 44 | case MM_MODEM_STATE_CONNECTED: return Cellular::kModemStateConnected; |
| 45 | default: |
| 46 | DCHECK(false) << "Unknown cellular state: " << input; |
| 47 | return Cellular::kModemStateUnknown; |
| 48 | } |
| 49 | } |
| 50 | |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame^] | 51 | bool Modem1::GetLinkName(const DBusPropertiesMap &modem_props, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 52 | string *name) const { |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame^] | 53 | string device_prop; |
| 54 | if (!DBusProperties::GetString(modem_props, |
| 55 | Modem::kPropertyLinkName, |
| 56 | &device_prop)) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // |device_prop| will be a sysfs path such as: |
| 61 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2 |
| 62 | FilePath device_path(device_prop); |
| 63 | |
| 64 | // Each entry in /sys/class/net has the name of a network interface |
| 65 | // and is a symlink into the actual device structure: |
| 66 | // eth0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:01:00.0/net/eth0 |
| 67 | // Iterate over all of these and see if any of them point into |
| 68 | // subdirectories of the sysfs path from the Device property. |
| 69 | FilePath netfiles_path("/sys/class/net"); |
| 70 | |
| 71 | // FileEnumerator warns that it is a blocking interface; that |
| 72 | // shouldn't be a problem here. |
| 73 | file_util::FileEnumerator netfiles(netfiles_path, |
| 74 | false, // don't recurse |
| 75 | file_util::FileEnumerator::DIRECTORIES); |
| 76 | for (FilePath link = netfiles.Next(); !link.empty(); link = netfiles.Next()) { |
| 77 | FilePath target; |
| 78 | if (!file_util::ReadSymbolicLink(link, &target)) |
| 79 | continue; |
| 80 | if (!target.IsAbsolute()) |
| 81 | target = netfiles_path.Append(target); |
| 82 | if (file_util::ContainsPath(device_path, target)) { |
| 83 | *name = link.BaseName().value(); |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return false; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void Modem1::CreateDeviceMM1(const DBusInterfaceToProperties &i_to_p) { |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 91 | Init(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 92 | DBusInterfaceToProperties::const_iterator modem_properties = |
| 93 | i_to_p.find(MM_DBUS_INTERFACE_MODEM); |
| 94 | if (modem_properties == i_to_p.end()) { |
| 95 | LOG(ERROR) << "Cellular device with no modem properties"; |
| 96 | return; |
| 97 | } |
| 98 | set_type(Cellular::kTypeUniversal); |
| 99 | |
| 100 | // We cannot check the IP method to make sure it's not PPP. The IP |
| 101 | // method will be checked later when the bearer object is fetched. |
| 102 | CreateDeviceFromModemProperties(modem_properties->second); |
| 103 | } |
| 104 | |
| 105 | } // namespace shill |