blob: 783e09dc35e8df178ae7c93ca7db826f1f5d5b49 [file] [log] [blame]
Arman Uguray72fab6a2013-01-10 19:32:42 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
David Rochbergfa1d31d2012-03-20 10:38:07 -04002// 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 Williamsfd10ec22012-04-11 20:29:16 -04007#include <base/file_util.h>
Ben Chan876efd32012-09-28 15:25:13 -07008#include <base/stl_util.h>
Ben Chan5c853ef2012-10-05 00:05:37 -07009#include <ModemManager/ModemManager.h>
David Rochbergfa1d31d2012-03-20 10:38:07 -040010
11#include "shill/cellular.h"
Jason Glasgowabc54032012-04-20 16:08:32 -040012#include "shill/device_info.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040013
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080014using base::FilePath;
David Rochbergfa1d31d2012-03-20 10:38:07 -040015using std::string;
16
17namespace shill {
18
Jason Glasgow82f9ab32012-04-04 14:27:19 -040019namespace {
Ben Chan62028b22012-11-05 11:20:02 -080020
Jason Glasgow82f9ab32012-04-04 14:27:19 -040021// The default place where the system keeps symbolic links for network device
22const char kDefaultNetfilesPath[] = "/sys/class/net";
Ben Chan62028b22012-11-05 11:20:02 -080023
24} // namespace
Jason Glasgow82f9ab32012-04-04 14:27:19 -040025
David Rochbergfa1d31d2012-03-20 10:38:07 -040026Modem1::Modem1(const string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040027 const string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -040028 const string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070029 ModemInfo *modem_info)
30 : Modem(owner, service, path, modem_info),
31 netfiles_path_(kDefaultNetfilesPath) {}
David Rochbergfa1d31d2012-03-20 10:38:07 -040032
33Modem1::~Modem1() {}
34
Nathan Williamsfd10ec22012-04-11 20:29:16 -040035bool Modem1::GetLinkName(const DBusPropertiesMap &modem_props,
David Rochbergfa1d31d2012-03-20 10:38:07 -040036 string *name) const {
Nathan Williamsfd10ec22012-04-11 20:29:16 -040037 string device_prop;
38 if (!DBusProperties::GetString(modem_props,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040039 MM_MODEM_PROPERTY_DEVICE,
Nathan Williamsfd10ec22012-04-11 20:29:16 -040040 &device_prop)) {
Jason Glasgowabc54032012-04-20 16:08:32 -040041 LOG(ERROR) << "Device missing property: " << MM_MODEM_PROPERTY_DEVICE;
Nathan Williamsfd10ec22012-04-11 20:29:16 -040042 return false;
43 }
44
Jason Glasgowabc54032012-04-20 16:08:32 -040045 if (device_prop.find(DeviceInfo::kModemPseudoDeviceNamePrefix) == 0) {
46 *name = device_prop;
47 return true;
48 }
49
Nathan Williamsfd10ec22012-04-11 20:29:16 -040050 // |device_prop| will be a sysfs path such as:
51 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
52 FilePath device_path(device_prop);
53
Jason Glasgow82f9ab32012-04-04 14:27:19 -040054 // Each entry in |netfiles_path_" (typically /sys/class/net)
55 // has the name of a network interface and is a symlink into the
56 // actual device structure:
Nathan Williamsfd10ec22012-04-11 20:29:16 -040057 // eth0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:01:00.0/net/eth0
58 // Iterate over all of these and see if any of them point into
59 // subdirectories of the sysfs path from the Device property.
Nathan Williamsfd10ec22012-04-11 20:29:16 -040060 // FileEnumerator warns that it is a blocking interface; that
61 // shouldn't be a problem here.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040062 file_util::FileEnumerator netfiles(netfiles_path_,
Ben Chan62028b22012-11-05 11:20:02 -080063 false, // don't recurse
Nathan Williamsfd10ec22012-04-11 20:29:16 -040064 file_util::FileEnumerator::DIRECTORIES);
65 for (FilePath link = netfiles.Next(); !link.empty(); link = netfiles.Next()) {
66 FilePath target;
67 if (!file_util::ReadSymbolicLink(link, &target))
68 continue;
69 if (!target.IsAbsolute())
Jason Glasgow82f9ab32012-04-04 14:27:19 -040070 target = netfiles_path_.Append(target);
Nathan Williamsfd10ec22012-04-11 20:29:16 -040071 if (file_util::ContainsPath(device_path, target)) {
72 *name = link.BaseName().value();
73 return true;
74 }
75 }
Jason Glasgowabc54032012-04-20 16:08:32 -040076 LOG(ERROR) << "No link name found for: " << device_prop;
Nathan Williamsfd10ec22012-04-11 20:29:16 -040077 return false;
David Rochbergfa1d31d2012-03-20 10:38:07 -040078}
79
Ben Chan876efd32012-09-28 15:25:13 -070080void Modem1::CreateDeviceMM1(const DBusInterfaceToProperties &properties) {
Eric Shienbrood11567d02012-04-10 18:08:49 -040081 Init();
Arman Uguray72fab6a2013-01-10 19:32:42 -080082 uint32 capabilities = kuint32max;
83 DBusInterfaceToProperties::const_iterator it =
84 properties.find(MM_DBUS_INTERFACE_MODEM);
85 if (it == properties.end()) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040086 LOG(ERROR) << "Cellular device with no modem properties";
87 return;
88 }
Arman Uguray72fab6a2013-01-10 19:32:42 -080089 const DBusPropertiesMap &modem_props = it->second;
90 DBusProperties::GetUint32(modem_props,
91 MM_MODEM_PROPERTY_CURRENTCAPABILITIES,
92 &capabilities);
93
94 if ((capabilities & MM_MODEM_CAPABILITY_LTE) ||
95 (capabilities & MM_MODEM_CAPABILITY_GSM_UMTS)) {
96 set_type(Cellular::kTypeUniversal);
97 } else if (capabilities & MM_MODEM_CAPABILITY_CDMA_EVDO) {
98 set_type(Cellular::kTypeUniversalCDMA);
99 } else {
100 LOG(ERROR) << "Unsupported capabilities: " << capabilities;
101 return;
102 }
David Rochbergfa1d31d2012-03-20 10:38:07 -0400103
104 // We cannot check the IP method to make sure it's not PPP. The IP
105 // method will be checked later when the bearer object is fetched.
Ben Chan876efd32012-09-28 15:25:13 -0700106 CreateDeviceFromModemProperties(properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400107}
108
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400109string Modem1::GetModemInterface(void) const {
110 return string(MM_DBUS_INTERFACE_MODEM);
111}
112
David Rochbergfa1d31d2012-03-20 10:38:07 -0400113} // namespace shill