blob: 15091d522dc006aeca46fdb077e4ae85d2845ee5 [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>
David Rochbergfa1d31d2012-03-20 10:38:07 -04008#include <mm/ModemManager-enums.h>
9#include <mm/ModemManager-names.h>
10
11#include "shill/cellular.h"
12
13using std::string;
14
15namespace shill {
16
Jason Glasgow82f9ab32012-04-04 14:27:19 -040017namespace {
18// The default place where the system keeps symbolic links for network device
19const char kDefaultNetfilesPath[] = "/sys/class/net";
20} // namespace {}
21
David Rochbergfa1d31d2012-03-20 10:38:07 -040022Modem1::Modem1(const string &owner,
23 const string &path,
24 ControlInterface *control_interface,
25 EventDispatcher *dispatcher,
26 Metrics *metrics,
27 Manager *manager,
28 mobile_provider_db *provider_db)
29 : Modem(owner, path, control_interface, dispatcher, metrics, manager,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040030 provider_db),
31 netfiles_path_(kDefaultNetfilesPath) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040032}
33
34Modem1::~Modem1() {}
35
Nathan Williamsfd10ec22012-04-11 20:29:16 -040036bool Modem1::GetLinkName(const DBusPropertiesMap &modem_props,
David Rochbergfa1d31d2012-03-20 10:38:07 -040037 string *name) const {
Nathan Williamsfd10ec22012-04-11 20:29:16 -040038 string device_prop;
39 if (!DBusProperties::GetString(modem_props,
Jason Glasgow82f9ab32012-04-04 14:27:19 -040040 MM_MODEM_PROPERTY_DEVICE,
Nathan Williamsfd10ec22012-04-11 20:29:16 -040041 &device_prop)) {
42 return false;
43 }
44
45 // |device_prop| will be a sysfs path such as:
46 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
47 FilePath device_path(device_prop);
48
Jason Glasgow82f9ab32012-04-04 14:27:19 -040049 // Each entry in |netfiles_path_" (typically /sys/class/net)
50 // has the name of a network interface and is a symlink into the
51 // actual device structure:
Nathan Williamsfd10ec22012-04-11 20:29:16 -040052 // eth0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:01:00.0/net/eth0
53 // Iterate over all of these and see if any of them point into
54 // subdirectories of the sysfs path from the Device property.
Nathan Williamsfd10ec22012-04-11 20:29:16 -040055 // FileEnumerator warns that it is a blocking interface; that
56 // shouldn't be a problem here.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040057 file_util::FileEnumerator netfiles(netfiles_path_,
Nathan Williamsfd10ec22012-04-11 20:29:16 -040058 false, // don't recurse
59 file_util::FileEnumerator::DIRECTORIES);
60 for (FilePath link = netfiles.Next(); !link.empty(); link = netfiles.Next()) {
61 FilePath target;
62 if (!file_util::ReadSymbolicLink(link, &target))
63 continue;
64 if (!target.IsAbsolute())
Jason Glasgow82f9ab32012-04-04 14:27:19 -040065 target = netfiles_path_.Append(target);
Nathan Williamsfd10ec22012-04-11 20:29:16 -040066 if (file_util::ContainsPath(device_path, target)) {
67 *name = link.BaseName().value();
68 return true;
69 }
70 }
71 return false;
David Rochbergfa1d31d2012-03-20 10:38:07 -040072}
73
74void Modem1::CreateDeviceMM1(const DBusInterfaceToProperties &i_to_p) {
Eric Shienbrood11567d02012-04-10 18:08:49 -040075 Init();
David Rochbergfa1d31d2012-03-20 10:38:07 -040076 DBusInterfaceToProperties::const_iterator modem_properties =
77 i_to_p.find(MM_DBUS_INTERFACE_MODEM);
78 if (modem_properties == i_to_p.end()) {
79 LOG(ERROR) << "Cellular device with no modem properties";
80 return;
81 }
82 set_type(Cellular::kTypeUniversal);
83
84 // We cannot check the IP method to make sure it's not PPP. The IP
85 // method will be checked later when the bearer object is fetched.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040086 // TODO(jglasgow): We should pass i_to_p because there are lots of
87 // properties we might want
David Rochbergfa1d31d2012-03-20 10:38:07 -040088 CreateDeviceFromModemProperties(modem_properties->second);
89}
90
Jason Glasgow4c0724a2012-04-17 15:47:40 -040091string Modem1::GetModemInterface(void) const {
92 return string(MM_DBUS_INTERFACE_MODEM);
93}
94
David Rochbergfa1d31d2012-03-20 10:38:07 -040095} // namespace shill