blob: 7eeecf85aa2f9041e2cb018cac7f1d93b227718e [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 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 <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07006#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07007
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070010
Chris Masoneee929b72011-05-10 10:02:18 -070011#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070012#include <base/memory/ref_counted.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070014
Paul Stewart75897df2011-04-27 09:05:53 -070015#include "shill/control_interface.h"
16#include "shill/device.h"
Chris Masoned7732e42011-05-20 11:08:56 -070017#include "shill/device_dbus_adaptor.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070018#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070019#include "shill/error.h"
20#include "shill/manager.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070021#include "shill/rtnl_handler.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070022#include "shill/shill_event.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023
Chris Masone8fe2c7e2011-06-09 15:51:19 -070024using std::string;
25using std::vector;
26
Paul Stewart75897df2011-04-27 09:05:53 -070027namespace shill {
28Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070029 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070030 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070031 const string &link_name,
Paul Stewartb50f0b92011-05-16 16:31:42 -070032 int interface_index)
Darin Petkovafa6fc42011-06-21 16:21:08 -070033 : manager_(manager),
34 link_name_(link_name),
Paul Stewartf1ce5d22011-05-19 13:10:20 -070035 adaptor_(control_interface->CreateDeviceAdaptor(this)),
36 interface_index_(interface_index),
37 running_(false) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070038 known_properties_.push_back(flimflam::kNameProperty);
39 known_properties_.push_back(flimflam::kTypeProperty);
40 known_properties_.push_back(flimflam::kPoweredProperty);
41 known_properties_.push_back(flimflam::kScanningProperty);
42 // known_properties_.push_back(flimflam::kReconnectProperty);
43 known_properties_.push_back(flimflam::kScanIntervalProperty);
44 known_properties_.push_back(flimflam::kBgscanMethodProperty);
45 known_properties_.push_back(flimflam::kBgscanShortIntervalProperty);
46 known_properties_.push_back(flimflam::kBgscanSignalThresholdProperty);
47 known_properties_.push_back(flimflam::kNetworksProperty);
48 known_properties_.push_back(flimflam::kIPConfigsProperty);
49 known_properties_.push_back(flimflam::kCellularAllowRoamingProperty);
50 known_properties_.push_back(flimflam::kCarrierProperty);
51 known_properties_.push_back(flimflam::kMeidProperty);
52 known_properties_.push_back(flimflam::kImeiProperty);
53 known_properties_.push_back(flimflam::kImsiProperty);
54 known_properties_.push_back(flimflam::kEsnProperty);
55 known_properties_.push_back(flimflam::kMdnProperty);
56 known_properties_.push_back(flimflam::kModelIDProperty);
57 known_properties_.push_back(flimflam::kManufacturerProperty);
58 known_properties_.push_back(flimflam::kFirmwareRevisionProperty);
59 known_properties_.push_back(flimflam::kHardwareRevisionProperty);
60 known_properties_.push_back(flimflam::kPRLVersionProperty);
61 known_properties_.push_back(flimflam::kSIMLockStatusProperty);
62 known_properties_.push_back(flimflam::kFoundNetworksProperty);
63 known_properties_.push_back(flimflam::kDBusConnectionProperty);
64 known_properties_.push_back(flimflam::kDBusObjectProperty);
Chris Masoneee929b72011-05-10 10:02:18 -070065 // Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -070066 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -070067}
68
69Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -070070 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -070071}
72
73void Device::Start() {
74 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070075 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -070076 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -070077}
78
79void Device::Stop() {
80 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070081 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -070082}
83
Darin Petkovafa6fc42011-06-21 16:21:08 -070084bool Device::TechnologyIs(const Technology type) {
85 return false;
86}
87
Paul Stewartf1ce5d22011-05-19 13:10:20 -070088void Device::LinkEvent(unsigned flags, unsigned change) {
89 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
90 << change;
91}
92
93void Device::Scan() {
94 VLOG(2) << "Device " << link_name_ << " scan requested.";
95}
96
Chris Masone3bd3c8c2011-06-13 08:20:26 -070097bool Device::Contains(const std::string &property) {
98 vector<string>::iterator it;
99 for (it = known_properties_.begin(); it != known_properties_.end(); ++it) {
100 if (property == *it)
101 return true;
102 }
103 return false;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700104}
105
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700106bool Device::SetBoolProperty(const string& name, bool value, Error *error) {
107 VLOG(2) << "Setting " << name << " as a bool.";
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700108 // TODO(cmasone): Set actual properties.
109 return true;
110}
111
112bool Device::SetInt32Property(const std::string& name,
113 int32 value,
114 Error *error) {
115 VLOG(2) << "Setting " << name << " as an int32.";
116 // TODO(cmasone): Set actual properties.
117 return true;
118}
119
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700120bool Device::SetUint16Property(const std::string& name,
121 uint16 value,
122 Error *error) {
123 VLOG(2) << "Setting " << name << " as a uint16.";
124 // TODO(cmasone): Set actual properties.
125 return true;
126}
127
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700128const string& Device::UniqueName() const {
129 // TODO(pstew): link_name is only run-time unique and won't persist
Darin Petkovafa6fc42011-06-21 16:21:08 -0700130 return link_name();
131}
132
133void Device::DestroyIPConfig() {
134 if (ipconfig_.get()) {
Paul Stewartc39f1132011-06-22 12:02:28 -0700135 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
136 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700137 ipconfig_->ReleaseIP();
138 ipconfig_ = NULL;
139 }
140}
141
142bool Device::AcquireDHCPConfig() {
143 DestroyIPConfig();
144 ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name());
145 ipconfig_->RegisterUpdateCallback(
146 NewCallback(this, &Device::IPConfigUpdatedCallback));
147 return ipconfig_->RequestIP();
148}
149
150void Device::IPConfigUpdatedCallback(IPConfigRefPtr ipconfig, bool success) {
151 // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through
152 // ConfigIP? Also, maybe allow forwarding the callback to interested listeners
153 // (e.g., the Manager).
Paul Stewartc39f1132011-06-22 12:02:28 -0700154 if (success) {
155 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
156 *ipconfig);
157 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700158}
159
Paul Stewart75897df2011-04-27 09:05:53 -0700160} // namespace shill