blob: 51615b9f5c2878e1d5d4d95be22ad5acaad7946d [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),
Chris Masone7df0c672011-07-15 10:24:54 -070054 manager_(manager),
mukesh agrawalf60e4062011-05-27 13:13:41 -070055 adaptor_(control_interface->CreateDeviceAdaptor(this)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070056
Chris Masone27c4aa52011-07-02 13:10:14 -070057 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070058
59 // flimflam::kBgscanMethodProperty: Registered in WiFi
60 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
61 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
62
63 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
64 // flimflam::kCarrierProperty: Registered in Cellular
65 // flimflam::kEsnProperty: Registered in Cellular
66 // flimflam::kImeiProperty: Registered in Cellular
67 // flimflam::kImsiProperty: Registered in Cellular
68 // flimflam::kManufacturerProperty: Registered in Cellular
69 // flimflam::kMdnProperty: Registered in Cellular
70 // flimflam::kMeidProperty: Registered in Cellular
71 // flimflam::kMinProperty: Registered in Cellular
72 // flimflam::kModelIDProperty: Registered in Cellular
73 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
74 // flimflam::kHardwareRevisionProperty: Registered in Cellular
75 // flimflam::kPRLVersionProperty: Registered in Cellular
76 // flimflam::kSIMLockStatusProperty: Registered in Cellular
77 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070078 // flimflam::kDBusConnectionProperty: Registered in Cellular
79 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070080
Chris Masoneb925cc82011-06-22 15:39:57 -070081 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070082 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
83 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
84 &Device::AvailableIPConfigs,
85 NULL);
86 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
87 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070088 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070089 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070090
Chris Masone4e851612011-07-01 10:46:53 -070091 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070092 // known_properties_.push_back(flimflam::kNetworksProperty);
93
Chris Masone4d42df82011-07-02 17:09:39 -070094 // flimflam::kScanningProperty: Registered in WiFi, Cellular
95 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
96
97 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -070098 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -070099}
100
101Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700102 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700103}
104
105void Device::Start() {
106 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700107 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700108 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700109}
110
111void Device::Stop() {
112 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700113 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700114}
115
Darin Petkovafa6fc42011-06-21 16:21:08 -0700116bool Device::TechnologyIs(const Technology type) {
117 return false;
118}
119
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700120void Device::LinkEvent(unsigned flags, unsigned change) {
121 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
122 << change;
123}
124
125void Device::Scan() {
126 VLOG(2) << "Device " << link_name_ << " scan requested.";
127}
128
Chris Masone27c4aa52011-07-02 13:10:14 -0700129string Device::GetRpcIdentifier() {
130 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700131}
132
Chris Masone5dec5f42011-07-22 14:07:55 -0700133string Device::GetStorageIdentifier() {
134 string id = GetRpcIdentifier();
135 ControlInterface::RpcIdToStorageId(&id);
136 return id;
137}
138
Chris Masone19e30402011-07-19 15:48:47 -0700139const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700140 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700141}
142
Chris Masone19e30402011-07-19 15:48:47 -0700143const string& Device::UniqueName() const {
144 return unique_id_;
145}
146
Chris Masone5dec5f42011-07-22 14:07:55 -0700147bool Device::Load(StoreInterface *storage) {
148 const string id = GetStorageIdentifier();
149 if (!storage->ContainsGroup(id)) {
150 LOG(WARNING) << "Device is not available in the persistent store: " << id;
151 return false;
152 }
153 storage->GetBool(id, kStoragePowered, &powered_);
154 // TODO(cmasone): What does it mean to load an IPConfig identifier??
155 return true;
156}
157
158bool Device::Save(StoreInterface *storage) {
159 const string id = GetStorageIdentifier();
160 storage->SetBool(id, kStoragePowered, powered_);
161 if (ipconfig_.get())
162 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage());
163 return true;
164}
165
Darin Petkovafa6fc42011-06-21 16:21:08 -0700166void Device::DestroyIPConfig() {
167 if (ipconfig_.get()) {
Paul Stewartc39f1132011-06-22 12:02:28 -0700168 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
169 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700170 ipconfig_->ReleaseIP();
171 ipconfig_ = NULL;
172 }
173}
174
175bool Device::AcquireDHCPConfig() {
176 DestroyIPConfig();
Chris Masone7df0c672011-07-15 10:24:54 -0700177 ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700178 ipconfig_->RegisterUpdateCallback(
179 NewCallback(this, &Device::IPConfigUpdatedCallback));
180 return ipconfig_->RequestIP();
181}
182
Chris Masone27c4aa52011-07-02 13:10:14 -0700183void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700184 Strings(Device::*get)(void),
185 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700186 store_.RegisterDerivedStrings(
187 name,
188 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700189}
190
Chris Masone2b105542011-06-22 10:58:09 -0700191void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
192 bool success) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700193 // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through
194 // ConfigIP? Also, maybe allow forwarding the callback to interested listeners
195 // (e.g., the Manager).
Paul Stewartc39f1132011-06-22 12:02:28 -0700196 if (success) {
197 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
198 *ipconfig);
199 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700200}
201
Chris Masone5dec5f42011-07-22 14:07:55 -0700202string Device::SerializeIPConfigsForStorage() {
203 return StringPrintf("%s:%s",
204 ipconfig_->GetStorageIdentifier().c_str(),
205 ipconfig_->type().c_str());
206}
207
Chris Masone4e851612011-07-01 10:46:53 -0700208vector<string> Device::AvailableIPConfigs() {
209 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
210 return vector<string>(1, id);
211}
212
213string Device::GetRpcConnectionIdentifier() {
214 return adaptor_->GetRpcConnectionIdentifier();
215}
216
Paul Stewart75897df2011-04-27 09:05:53 -0700217} // namespace shill