blob: ef751187c89fd96024e11f5aa3bd9ab16a7f985c [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
Chris Masone2b105542011-06-22 10:58:09 -07005#include "shill/device.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Paul Stewart75897df2011-04-27 09:05:53 -070010#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070012
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070014#include <base/memory/ref_counted.h>
Chris Masone5dec5f42011-07-22 14:07:55 -070015#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017
Paul Stewart75897df2011-04-27 09:05:53 -070018#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070019#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070020#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070021#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070022#include "shill/error.h"
23#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070024#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070025#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070026#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070027#include "shill/service.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070028#include "shill/shill_event.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070029#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070030
Chris Masone5dec5f42011-07-22 14:07:55 -070031using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070032using std::string;
33using std::vector;
34
Paul Stewart75897df2011-04-27 09:05:53 -070035namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070036
37// static
38const char Device::kStoragePowered[] = "Powered";
39
40// static
41const char Device::kStorageIPConfigs[] = "IPConfigs";
42
Paul Stewart75897df2011-04-27 09:05:53 -070043Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070044 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070045 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070046 const string &link_name,
Paul Stewartb50f0b92011-05-16 16:31:42 -070047 int interface_index)
Chris Masone853b81b2011-06-24 14:11:41 -070048 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070049 reconnect_(true),
mukesh agrawalf60e4062011-05-27 13:13:41 -070050 interface_index_(interface_index),
51 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070052 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070053 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070054 control_interface_(control_interface),
55 dispatcher_(dispatcher),
Chris Masone7df0c672011-07-15 10:24:54 -070056 manager_(manager),
mukesh agrawalf60e4062011-05-27 13:13:41 -070057 adaptor_(control_interface->CreateDeviceAdaptor(this)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070058
Chris Masone27c4aa52011-07-02 13:10:14 -070059 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070060
61 // flimflam::kBgscanMethodProperty: Registered in WiFi
62 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
63 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
64
65 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
66 // flimflam::kCarrierProperty: Registered in Cellular
67 // flimflam::kEsnProperty: Registered in Cellular
68 // flimflam::kImeiProperty: Registered in Cellular
69 // flimflam::kImsiProperty: Registered in Cellular
70 // flimflam::kManufacturerProperty: Registered in Cellular
71 // flimflam::kMdnProperty: Registered in Cellular
72 // flimflam::kMeidProperty: Registered in Cellular
73 // flimflam::kMinProperty: Registered in Cellular
74 // flimflam::kModelIDProperty: Registered in Cellular
75 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
76 // flimflam::kHardwareRevisionProperty: Registered in Cellular
77 // flimflam::kPRLVersionProperty: Registered in Cellular
78 // flimflam::kSIMLockStatusProperty: Registered in Cellular
79 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070080 // flimflam::kDBusConnectionProperty: Registered in Cellular
81 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070082
Chris Masoneb925cc82011-06-22 15:39:57 -070083 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070084 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
85 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
86 &Device::AvailableIPConfigs,
87 NULL);
88 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
89 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070090 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070091 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070092
Chris Masone4e851612011-07-01 10:46:53 -070093 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070094 // known_properties_.push_back(flimflam::kNetworksProperty);
95
Chris Masone4d42df82011-07-02 17:09:39 -070096 // flimflam::kScanningProperty: Registered in WiFi, Cellular
97 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
98
99 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700100 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700101}
102
103Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700104 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700105}
106
107void Device::Start() {
108 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700109 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700110 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700111}
112
113void Device::Stop() {
114 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700115 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700116}
117
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700118bool Device::TechnologyIs(const Technology type) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700119 return false;
120}
121
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700122void Device::LinkEvent(unsigned flags, unsigned change) {
123 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
124 << change;
125}
126
127void Device::Scan() {
128 VLOG(2) << "Device " << link_name_ << " scan requested.";
129}
130
Chris Masone27c4aa52011-07-02 13:10:14 -0700131string Device::GetRpcIdentifier() {
132 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700133}
134
Chris Masone5dec5f42011-07-22 14:07:55 -0700135string Device::GetStorageIdentifier() {
136 string id = GetRpcIdentifier();
137 ControlInterface::RpcIdToStorageId(&id);
138 return id;
139}
140
Chris Masone19e30402011-07-19 15:48:47 -0700141const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700142 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700143}
144
Chris Masone19e30402011-07-19 15:48:47 -0700145const string& Device::UniqueName() const {
146 return unique_id_;
147}
148
Chris Masone5dec5f42011-07-22 14:07:55 -0700149bool Device::Load(StoreInterface *storage) {
150 const string id = GetStorageIdentifier();
151 if (!storage->ContainsGroup(id)) {
152 LOG(WARNING) << "Device is not available in the persistent store: " << id;
153 return false;
154 }
155 storage->GetBool(id, kStoragePowered, &powered_);
156 // TODO(cmasone): What does it mean to load an IPConfig identifier??
157 return true;
158}
159
160bool Device::Save(StoreInterface *storage) {
161 const string id = GetStorageIdentifier();
162 storage->SetBool(id, kStoragePowered, powered_);
163 if (ipconfig_.get())
164 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage());
165 return true;
166}
167
Darin Petkovafa6fc42011-06-21 16:21:08 -0700168void Device::DestroyIPConfig() {
169 if (ipconfig_.get()) {
Paul Stewartc39f1132011-06-22 12:02:28 -0700170 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
171 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700172 ipconfig_->ReleaseIP();
173 ipconfig_ = NULL;
174 }
175}
176
177bool Device::AcquireDHCPConfig() {
178 DestroyIPConfig();
Chris Masone7df0c672011-07-15 10:24:54 -0700179 ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700180 ipconfig_->RegisterUpdateCallback(
181 NewCallback(this, &Device::IPConfigUpdatedCallback));
182 return ipconfig_->RequestIP();
183}
184
Chris Masone27c4aa52011-07-02 13:10:14 -0700185void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700186 Strings(Device::*get)(void),
187 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700188 store_.RegisterDerivedStrings(
189 name,
190 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700191}
192
Chris Masone2b105542011-06-22 10:58:09 -0700193void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
194 bool success) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700195 // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through
196 // ConfigIP? Also, maybe allow forwarding the callback to interested listeners
197 // (e.g., the Manager).
Paul Stewartc39f1132011-06-22 12:02:28 -0700198 if (success) {
199 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
200 *ipconfig);
201 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700202}
203
Chris Masone5dec5f42011-07-22 14:07:55 -0700204string Device::SerializeIPConfigsForStorage() {
205 return StringPrintf("%s:%s",
206 ipconfig_->GetStorageIdentifier().c_str(),
207 ipconfig_->type().c_str());
208}
209
Chris Masone4e851612011-07-01 10:46:53 -0700210vector<string> Device::AvailableIPConfigs() {
211 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
212 return vector<string>(1, id);
213}
214
215string Device::GetRpcConnectionIdentifier() {
216 return adaptor_->GetRpcConnectionIdentifier();
217}
218
Paul Stewart75897df2011-04-27 09:05:53 -0700219} // namespace shill