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" |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 12 | #include "shill/device_info.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 13 | |
| 14 | using std::string; |
| 15 | |
| 16 | namespace shill { |
| 17 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 18 | namespace { |
| 19 | // The default place where the system keeps symbolic links for network device |
| 20 | const char kDefaultNetfilesPath[] = "/sys/class/net"; |
| 21 | } // namespace {} |
| 22 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 23 | Modem1::Modem1(const string &owner, |
| 24 | const string &path, |
| 25 | ControlInterface *control_interface, |
| 26 | EventDispatcher *dispatcher, |
| 27 | Metrics *metrics, |
| 28 | Manager *manager, |
| 29 | mobile_provider_db *provider_db) |
| 30 | : Modem(owner, path, control_interface, dispatcher, metrics, manager, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 31 | provider_db), |
| 32 | netfiles_path_(kDefaultNetfilesPath) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Modem1::~Modem1() {} |
| 36 | |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 37 | bool Modem1::GetLinkName(const DBusPropertiesMap &modem_props, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 38 | string *name) const { |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 39 | string device_prop; |
| 40 | if (!DBusProperties::GetString(modem_props, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 41 | MM_MODEM_PROPERTY_DEVICE, |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 42 | &device_prop)) { |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 43 | LOG(ERROR) << "Device missing property: " << MM_MODEM_PROPERTY_DEVICE; |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 47 | if (device_prop.find(DeviceInfo::kModemPseudoDeviceNamePrefix) == 0) { |
| 48 | *name = device_prop; |
| 49 | return true; |
| 50 | } |
| 51 | |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 52 | // |device_prop| will be a sysfs path such as: |
| 53 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2 |
| 54 | FilePath device_path(device_prop); |
| 55 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 56 | // Each entry in |netfiles_path_" (typically /sys/class/net) |
| 57 | // has the name of a network interface and is a symlink into the |
| 58 | // actual device structure: |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 59 | // eth0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:01:00.0/net/eth0 |
| 60 | // Iterate over all of these and see if any of them point into |
| 61 | // subdirectories of the sysfs path from the Device property. |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 62 | // FileEnumerator warns that it is a blocking interface; that |
| 63 | // shouldn't be a problem here. |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 64 | file_util::FileEnumerator netfiles(netfiles_path_, |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 65 | false, // don't recurse |
| 66 | file_util::FileEnumerator::DIRECTORIES); |
| 67 | for (FilePath link = netfiles.Next(); !link.empty(); link = netfiles.Next()) { |
| 68 | FilePath target; |
| 69 | if (!file_util::ReadSymbolicLink(link, &target)) |
| 70 | continue; |
| 71 | if (!target.IsAbsolute()) |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 72 | target = netfiles_path_.Append(target); |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 73 | if (file_util::ContainsPath(device_path, target)) { |
| 74 | *name = link.BaseName().value(); |
| 75 | return true; |
| 76 | } |
| 77 | } |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 78 | LOG(ERROR) << "No link name found for: " << device_prop; |
Nathan Williams | fd10ec2 | 2012-04-11 20:29:16 -0400 | [diff] [blame] | 79 | return false; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Modem1::CreateDeviceMM1(const DBusInterfaceToProperties &i_to_p) { |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 83 | Init(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 84 | DBusInterfaceToProperties::const_iterator modem_properties = |
| 85 | i_to_p.find(MM_DBUS_INTERFACE_MODEM); |
| 86 | if (modem_properties == i_to_p.end()) { |
| 87 | LOG(ERROR) << "Cellular device with no modem properties"; |
| 88 | return; |
| 89 | } |
| 90 | set_type(Cellular::kTypeUniversal); |
| 91 | |
| 92 | // We cannot check the IP method to make sure it's not PPP. The IP |
| 93 | // method will be checked later when the bearer object is fetched. |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 94 | // TODO(jglasgow): We should pass i_to_p because there are lots of |
| 95 | // properties we might want |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 96 | CreateDeviceFromModemProperties(modem_properties->second); |
| 97 | } |
| 98 | |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 99 | string Modem1::GetModemInterface(void) const { |
| 100 | return string(MM_DBUS_INTERFACE_MODEM); |
| 101 | } |
| 102 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 103 | } // namespace shill |