blob: 1eea4385449259fff7bb3b9948395fd6a6216926 [file] [log] [blame]
David Rochbergfa1d31d2012-03-20 10:38:07 -04001// 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 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
14using std::string;
15
16namespace shill {
17
Jason Glasgow82f9ab32012-04-04 14:27:19 -040018namespace {
Ben Chan62028b22012-11-05 11:20:02 -080019
Jason Glasgow82f9ab32012-04-04 14:27:19 -040020// The default place where the system keeps symbolic links for network device
21const char kDefaultNetfilesPath[] = "/sys/class/net";
Ben Chan62028b22012-11-05 11:20:02 -080022
23} // namespace
Jason Glasgow82f9ab32012-04-04 14:27:19 -040024
David Rochbergfa1d31d2012-03-20 10:38:07 -040025Modem1::Modem1(const string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040026 const string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -040027 const string &path,
28 ControlInterface *control_interface,
29 EventDispatcher *dispatcher,
30 Metrics *metrics,
31 Manager *manager,
Ben Chan62028b22012-11-05 11:20:02 -080032 CellularOperatorInfo *cellular_operator_info,
David Rochbergfa1d31d2012-03-20 10:38:07 -040033 mobile_provider_db *provider_db)
Jason Glasgowa585fc32012-06-06 11:04:09 -040034 : Modem(owner, service, path, control_interface, dispatcher, metrics,
Ben Chan62028b22012-11-05 11:20:02 -080035 manager, cellular_operator_info, provider_db),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040036 netfiles_path_(kDefaultNetfilesPath) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040037}
38
39Modem1::~Modem1() {}
40
Nathan Williamsfd10ec22012-04-11 20:29:16 -040041bool Modem1::GetLinkName(const DBusPropertiesMap &modem_props,
David Rochbergfa1d31d2012-03-20 10:38:07 -040042 string *name) const {
Nathan Williamsfd10ec22012-04-11 20:29:16 -040043 string device_prop;
44 if (!DBusProperties::GetString(modem_props,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040045 MM_MODEM_PROPERTY_DEVICE,
Nathan Williamsfd10ec22012-04-11 20:29:16 -040046 &device_prop)) {
Jason Glasgowabc54032012-04-20 16:08:32 -040047 LOG(ERROR) << "Device missing property: " << MM_MODEM_PROPERTY_DEVICE;
Nathan Williamsfd10ec22012-04-11 20:29:16 -040048 return false;
49 }
50
Jason Glasgowabc54032012-04-20 16:08:32 -040051 if (device_prop.find(DeviceInfo::kModemPseudoDeviceNamePrefix) == 0) {
52 *name = device_prop;
53 return true;
54 }
55
Nathan Williamsfd10ec22012-04-11 20:29:16 -040056 // |device_prop| will be a sysfs path such as:
57 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
58 FilePath device_path(device_prop);
59
Jason Glasgow82f9ab32012-04-04 14:27:19 -040060 // Each entry in |netfiles_path_" (typically /sys/class/net)
61 // has the name of a network interface and is a symlink into the
62 // actual device structure:
Nathan Williamsfd10ec22012-04-11 20:29:16 -040063 // eth0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:01:00.0/net/eth0
64 // Iterate over all of these and see if any of them point into
65 // subdirectories of the sysfs path from the Device property.
Nathan Williamsfd10ec22012-04-11 20:29:16 -040066 // FileEnumerator warns that it is a blocking interface; that
67 // shouldn't be a problem here.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040068 file_util::FileEnumerator netfiles(netfiles_path_,
Ben Chan62028b22012-11-05 11:20:02 -080069 false, // don't recurse
Nathan Williamsfd10ec22012-04-11 20:29:16 -040070 file_util::FileEnumerator::DIRECTORIES);
71 for (FilePath link = netfiles.Next(); !link.empty(); link = netfiles.Next()) {
72 FilePath target;
73 if (!file_util::ReadSymbolicLink(link, &target))
74 continue;
75 if (!target.IsAbsolute())
Jason Glasgow82f9ab32012-04-04 14:27:19 -040076 target = netfiles_path_.Append(target);
Nathan Williamsfd10ec22012-04-11 20:29:16 -040077 if (file_util::ContainsPath(device_path, target)) {
78 *name = link.BaseName().value();
79 return true;
80 }
81 }
Jason Glasgowabc54032012-04-20 16:08:32 -040082 LOG(ERROR) << "No link name found for: " << device_prop;
Nathan Williamsfd10ec22012-04-11 20:29:16 -040083 return false;
David Rochbergfa1d31d2012-03-20 10:38:07 -040084}
85
Ben Chan876efd32012-09-28 15:25:13 -070086void Modem1::CreateDeviceMM1(const DBusInterfaceToProperties &properties) {
Eric Shienbrood11567d02012-04-10 18:08:49 -040087 Init();
Ben Chan876efd32012-09-28 15:25:13 -070088 if (!ContainsKey(properties, MM_DBUS_INTERFACE_MODEM)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040089 LOG(ERROR) << "Cellular device with no modem properties";
90 return;
91 }
92 set_type(Cellular::kTypeUniversal);
93
94 // We cannot check the IP method to make sure it's not PPP. The IP
95 // method will be checked later when the bearer object is fetched.
Ben Chan876efd32012-09-28 15:25:13 -070096 CreateDeviceFromModemProperties(properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -040097}
98
Jason Glasgow4c0724a2012-04-17 15:47:40 -040099string Modem1::GetModemInterface(void) const {
100 return string(MM_DBUS_INTERFACE_MODEM);
101}
102
David Rochbergfa1d31d2012-03-20 10:38:07 -0400103} // namespace shill