blob: 960eaac4a30d93bedcdaab13b2421bca79948f13 [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 Masone8fe2c7e2011-06-09 15:51:19 -07005#include "shill/manager.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 Masone52cd19b2011-06-29 17:23:04 -070011#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070012
Paul Stewarte6132022011-08-16 09:11:02 -070013#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070014#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070015#include <base/memory/ref_counted.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017
Chris Masoned0ceb8c2011-06-02 10:05:39 -070018#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070020#include "shill/dbus_adaptor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070021#include "shill/default_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070022#include "shill/device.h"
23#include "shill/device_info.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/ephemeral_profile.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025#include "shill/error.h"
Chris Masone9d779932011-08-25 16:33:41 -070026#include "shill/key_file_store.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070027#include "shill/profile.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070028#include "shill/property_accessor.h"
Paul Stewarte6132022011-08-16 09:11:02 -070029#include "shill/resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070030#include "shill/shill_event.h"
31#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070032
33using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070034using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070035
36namespace shill {
Paul Stewarte6132022011-08-16 09:11:02 -070037
Paul Stewart75897df2011-04-27 09:05:53 -070038Manager::Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070039 EventDispatcher *dispatcher,
Chris Masone2ae797d2011-08-23 20:41:00 -070040 GLib *glib,
41 const string &run_directory,
42 const string &storage_directory,
43 const string &user_storage_format)
44 : run_path_(FilePath(run_directory)),
45 storage_path_(FilePath(storage_directory)),
46 user_storage_format_(user_storage_format),
Paul Stewarte6132022011-08-16 09:11:02 -070047 adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070048 device_info_(control_interface, dispatcher, this),
Darin Petkov887f2982011-07-14 16:10:17 -070049 modem_info_(control_interface, dispatcher, this, glib),
Chris Masone6791a432011-07-12 13:23:19 -070050 running_(false),
Chris Masone9d779932011-08-25 16:33:41 -070051 ephemeral_profile_(new EphemeralProfile(control_interface, this)),
Chris Masone2ae797d2011-08-23 20:41:00 -070052 control_interface_(control_interface),
53 glib_(glib) {
Chris Masone7aa5f902011-07-11 11:13:35 -070054 HelpRegisterDerivedString(flimflam::kActiveProfileProperty,
55 &Manager::GetActiveProfileName,
56 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070057 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
58 &Manager::AvailableTechnologies,
59 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070060 store_.RegisterString(flimflam::kCheckPortalListProperty,
61 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070062 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
63 &Manager::ConnectedTechnologies,
64 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070065 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070066 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
67 &Manager::DefaultTechnology,
68 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070069 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
70 &Manager::EnumerateDevices,
71 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070072 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
73 &Manager::EnabledTechnologies,
74 NULL);
75 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
76 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
77 HelpRegisterDerivedString(flimflam::kStateProperty,
78 &Manager::CalculateState,
79 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070080 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
81 &Manager::EnumerateAvailableServices,
82 NULL);
83 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
84 &Manager::EnumerateWatchedServices,
85 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070086
Chris Masone4d42df82011-07-02 17:09:39 -070087 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070088 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masone2ae797d2011-08-23 20:41:00 -070089 profiles_.push_back(new DefaultProfile(control_interface_,
Chris Masone6791a432011-07-12 13:23:19 -070090 this,
Chris Masone2ae797d2011-08-23 20:41:00 -070091 storage_path_,
Chris Masone6791a432011-07-12 13:23:19 -070092 props_));
Chris Masoneb07006b2011-05-14 16:10:04 -070093 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070094}
95
Chris Masone6791a432011-07-12 13:23:19 -070096Manager::~Manager() {
Chris Masone9d779932011-08-25 16:33:41 -070097 profiles_.clear();
Chris Masone6791a432011-07-12 13:23:19 -070098}
Paul Stewart75897df2011-04-27 09:05:53 -070099
mukesh agrawal8f317b62011-07-15 11:53:23 -0700100void Manager::AddDeviceToBlackList(const string &device_name) {
101 device_info_.AddDeviceToBlackList(device_name);
102}
103
Paul Stewart75897df2011-04-27 09:05:53 -0700104void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700105 LOG(INFO) << "Manager started.";
Paul Stewarte6132022011-08-16 09:11:02 -0700106
Chris Masone2ae797d2011-08-23 20:41:00 -0700107 CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value();
Paul Stewarte6132022011-08-16 09:11:02 -0700108 Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf"));
Chris Masone2ae797d2011-08-23 20:41:00 -0700109
110 CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value();
111
Paul Stewart75897df2011-04-27 09:05:53 -0700112 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -0700113 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700114 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -0700115 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -0700116}
117
118void Manager::Stop() {
119 running_ = false;
Chris Masone9d779932011-08-25 16:33:41 -0700120 // Persist profile, device, service information to disk.
121 vector<ProfileRefPtr>::iterator it;
122 for (it = profiles_.begin(); it != profiles_.end(); ++it) {
123 KeyFileStore storage(glib_);
124 FilePath profile_path;
125 CHECK((*it)->GetStoragePath(&profile_path));
126 storage.set_path(profile_path);
127 if (storage.Open()) {
128 (*it)->Finalize(&storage);
129 storage.Close();
130 } else {
131 LOG(ERROR) << "Could not open storage at " << profile_path.value();
132 }
133 }
134 ephemeral_profile_->Finalize(NULL);
135
Chris Masone413a3192011-05-09 17:10:05 -0700136 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -0700137 modem_info_.Stop();
138 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -0700139}
140
Chris Masone7aa5f902011-07-11 11:13:35 -0700141const ProfileRefPtr &Manager::ActiveProfile() {
142 return profiles_.back();
143}
144
Chris Masone6791a432011-07-12 13:23:19 -0700145bool Manager::MoveToActiveProfile(const ProfileRefPtr &from,
146 const ServiceRefPtr &to_move) {
147 return ActiveProfile()->AdoptService(to_move) &&
148 from->AbandonService(to_move->UniqueName());
149}
150
Chris Masone2b105542011-06-22 10:58:09 -0700151void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700152 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700153 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700154 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700155 return;
156 }
Chris Masonec1e50412011-06-07 13:04:53 -0700157 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700158
159 // TODO(pstew): Should check configuration
160 if (running_)
161 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700162}
163
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700164void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700165 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700166 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700167 if (to_forget.get() == it->get()) {
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700168 VLOG(2) << "Deregistered device: " << to_forget->UniqueName();
169 to_forget->Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700170 devices_.erase(it);
171 return;
172 }
173 }
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700174 VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700175}
176
Chris Masone2b105542011-06-22 10:58:09 -0700177void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masone6791a432011-07-12 13:23:19 -0700178 // This should look for |to_manage| in the real profiles and, if found,
179 // do...something...to merge the meaningful state, I guess.
180
181 // If not found, add it to the ephemeral profile
182 ephemeral_profile_->AdoptService(to_manage);
183
184 // Now add to OUR list.
185 // TODO(cmasone): Keep this list sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700186 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700187 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700188 if (to_manage->UniqueName() == (*it)->UniqueName())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700189 return;
190 }
Chris Masonec1e50412011-06-07 13:04:53 -0700191 services_.push_back(to_manage);
mukesh agrawal32399322011-09-01 10:53:43 -0700192
193 vector<string> service_paths;
194 for (it = services_.begin(); it != services_.end(); ++it) {
195 service_paths.push_back((*it)->GetRpcIdentifier());
196 }
197 adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty,
198 service_paths);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700199}
200
Chris Masone2b105542011-06-22 10:58:09 -0700201void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masone6791a432011-07-12 13:23:19 -0700202 // If the service is in the ephemeral profile, destroy it.
203 if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) {
204 // if it's in one of the real profiles...um...I guess mark it unconnectable?
205 }
Chris Masonec1e50412011-06-07 13:04:53 -0700206 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700207 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700208 if (to_forget->UniqueName() == (*it)->UniqueName()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700209 services_.erase(it);
210 return;
211 }
212 }
213}
214
Paul Stewart03dba0b2011-08-22 16:32:45 -0700215void Manager::UpdateService(const ServiceConstRefPtr &to_update) {
216 LOG(INFO) << "Service " << to_update->UniqueName() << " updated;"
217 << " state: " << to_update->state() << " failure: "
218 << to_update->failure();
219 // TODO(pstew): This should trigger re-sorting of services, autoconnect, etc.
220}
221
Chris Masone9be4a9d2011-05-16 15:44:09 -0700222void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700223 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700224 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700225 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700226 for (it = devices_.begin(); it != devices_.end(); ++it) {
227 if ((*it)->TechnologyIs(tech))
228 found->push_back(*it);
229 }
230}
231
Chris Masonee0dea762011-06-09 09:06:03 -0700232ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700233 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700234 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700235 if (name == (*it)->UniqueName())
Chris Masonee0dea762011-06-09 09:06:03 -0700236 return *it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700237 }
Chris Masonee0dea762011-06-09 09:06:03 -0700238 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700239}
240
Chris Masone27c4aa52011-07-02 13:10:14 -0700241void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700242 string(Manager::*get)(void),
243 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700244 store_.RegisterDerivedString(
245 name,
246 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700247}
248
Chris Masone27c4aa52011-07-02 13:10:14 -0700249void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700250 Strings(Manager::*get)(void),
251 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700252 store_.RegisterDerivedStrings(
253 name,
254 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700255}
256
257string Manager::CalculateState() {
258 return flimflam::kStateOffline;
259}
260
261vector<string> Manager::AvailableTechnologies() {
262 return vector<string>();
263}
264
265vector<string> Manager::ConnectedTechnologies() {
266 return vector<string>();
267}
268
269string Manager::DefaultTechnology() {
270 return "";
271}
272
273vector<string> Manager::EnabledTechnologies() {
274 return vector<string>();
275}
276
Chris Masone3c3f6a12011-07-01 10:01:41 -0700277vector<string> Manager::EnumerateDevices() {
278 vector<string> device_rpc_ids;
279 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
280 it != devices_.end();
281 ++it) {
282 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
283 }
284 return device_rpc_ids;
285}
286
287vector<string> Manager::EnumerateAvailableServices() {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700288 vector<string> service_rpc_ids;
289 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
290 it != services_.end();
291 ++it) {
292 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
293 }
294 return service_rpc_ids;
295}
296
297vector<string> Manager::EnumerateWatchedServices() {
Chris Masone6791a432011-07-12 13:23:19 -0700298 // TODO(cmasone): Filter this list for services in appropriate states.
Chris Masone3c3f6a12011-07-01 10:01:41 -0700299 return EnumerateAvailableServices();
300}
301
Chris Masone7aa5f902011-07-11 11:13:35 -0700302string Manager::GetActiveProfileName() {
Chris Masone7df0c672011-07-15 10:24:54 -0700303 return ActiveProfile()->GetFriendlyName();
Chris Masone7aa5f902011-07-11 11:13:35 -0700304}
305
mukesh agrawal32399322011-09-01 10:53:43 -0700306// called via RPC (e.g., from ManagerDBusAdaptor)
307void Manager::RequestScan(const std::string &technology, Error *error) {
308 if (technology == flimflam::kTypeWifi || technology == "") {
309 vector<DeviceRefPtr> wifi_devices;
310 FilterByTechnology(Device::kWifi, &wifi_devices);
311
312 for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin();
313 it != wifi_devices.end();
314 ++it) {
315 (*it)->Scan();
316 }
317 } else {
318 // TODO(quiche): support scanning for other technologies?
319 const string kMessage = "Unrecognized technology " + technology;
320 LOG(ERROR) << kMessage;
321 CHECK(error);
322 error->Populate(Error::kInvalidArguments, kMessage);
323 }
324}
325
Paul Stewart75897df2011-04-27 09:05:53 -0700326} // namespace shill