blob: 5d57a84161c7e99837854cf8ffc4d178e684b779 [file] [log] [blame]
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoned7732e42011-05-20 11:08:56 -07002// 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/manager_dbus_adaptor.h"
6
7#include <map>
8#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <vector>
Chris Masoned7732e42011-05-20 11:08:56 -070010
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <dbus-c++/dbus.h>
12
mukesh agrawal32399322011-09-01 10:53:43 -070013#include "shill/device.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070014#include "shill/error.h"
Gaurav Shahb790aa22012-10-23 12:51:12 -070015#include "shill/geolocation_info.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070016#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070017#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070018#include "shill/manager.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070019#include "shill/wifi_service.h"
Chris Masone7ccc8192011-05-24 14:54:49 -070020
Chris Masoned7732e42011-05-20 11:08:56 -070021using std::map;
22using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023using std::vector;
Chris Masoned7732e42011-05-20 11:08:56 -070024
25namespace shill {
26
Chris Masoned7732e42011-05-20 11:08:56 -070027// static
Chris Masone19e30402011-07-19 15:48:47 -070028const char ManagerDBusAdaptor::kPath[] = "/";
Chris Masoned7732e42011-05-20 11:08:56 -070029
Chris Masoned0ceb8c2011-06-02 10:05:39 -070030ManagerDBusAdaptor::ManagerDBusAdaptor(DBus::Connection* conn, Manager *manager)
Chris Masoned7732e42011-05-20 11:08:56 -070031 : DBusAdaptor(conn, kPath),
32 manager_(manager) {
33}
Chris Masoneec6b18b2011-06-08 14:09:10 -070034
35ManagerDBusAdaptor::~ManagerDBusAdaptor() {
36 manager_ = NULL;
37}
Chris Masoned7732e42011-05-20 11:08:56 -070038
39void ManagerDBusAdaptor::UpdateRunning() {}
40
Chris Masone8fe2c7e2011-06-09 15:51:19 -070041void ManagerDBusAdaptor::EmitBoolChanged(const string &name, bool value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070042 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070043 PropertyChanged(name, DBusAdaptor::BoolToVariant(value));
44}
45
Chris Masone8fe2c7e2011-06-09 15:51:19 -070046void ManagerDBusAdaptor::EmitUintChanged(const string &name,
Chris Masoned0ceb8c2011-06-02 10:05:39 -070047 uint32 value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070048 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070049 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070050}
51
Chris Masone8fe2c7e2011-06-09 15:51:19 -070052void ManagerDBusAdaptor::EmitIntChanged(const string &name, int value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070053 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070054 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070055}
56
Chris Masone8fe2c7e2011-06-09 15:51:19 -070057void ManagerDBusAdaptor::EmitStringChanged(const string &name,
58 const string &value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070059 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070060 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
61}
62
Gaurav Shah435de2c2011-11-17 19:01:07 -080063void ManagerDBusAdaptor::EmitStringsChanged(const string &name,
64 const vector<string> &value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070065 SLOG(DBus, 2) << __func__ << ": " << name;
Gaurav Shah435de2c2011-11-17 19:01:07 -080066 PropertyChanged(name, DBusAdaptor::StringsToVariant(value));
67}
68
Paul Stewart49739c02012-08-08 17:24:03 -070069void ManagerDBusAdaptor::EmitRpcIdentifierChanged(
70 const string &name,
71 const string &value) {
72 SLOG(DBus, 2) << __func__ << ": " << name;
73 PropertyChanged(name, DBusAdaptor::PathToVariant(value));
74}
75
mukesh agrawal32399322011-09-01 10:53:43 -070076void ManagerDBusAdaptor::EmitRpcIdentifierArrayChanged(
77 const string &name,
78 const vector<string> &value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070079 SLOG(DBus, 2) << __func__ << ": " << name;
mukesh agrawal32399322011-09-01 10:53:43 -070080 vector< ::DBus::Path> paths;
81 vector<string>::const_iterator it;
82 for (it = value.begin(); it != value.end(); ++it) {
83 paths.push_back(*it);
84 }
85
mukesh agrawal2366eed2012-03-20 18:21:50 -070086 PropertyChanged(name, DBusAdaptor::PathsToVariant(paths));
mukesh agrawal32399322011-09-01 10:53:43 -070087}
88
Chris Masone8fe2c7e2011-06-09 15:51:19 -070089void ManagerDBusAdaptor::EmitStateChanged(const string &new_state) {
mukesh agrawal06175d72012-04-23 16:46:01 -070090 SLOG(DBus, 2) << __func__;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070091 StateChanged(new_state);
92}
93
Chris Masoned7732e42011-05-20 11:08:56 -070094map<string, ::DBus::Variant> ManagerDBusAdaptor::GetProperties(
95 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -070096 SLOG(DBus, 2) << __func__;
Chris Masonea8a2c252011-06-27 22:16:30 -070097 map<string, ::DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -070098 DBusAdaptor::GetProperties(manager_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -070099 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -0700100}
101
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700102void ManagerDBusAdaptor::SetProperty(const string &name,
103 const ::DBus::Variant &value,
Chris Masoned7732e42011-05-20 11:08:56 -0700104 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700105 SLOG(DBus, 2) << __func__ << ": " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800106 if (DBusAdaptor::SetProperty(manager_->mutable_store(),
107 name,
108 value,
109 &error)) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110 PropertyChanged(name, value);
111 }
Chris Masoned7732e42011-05-20 11:08:56 -0700112}
113
mukesh agrawal1830fa12011-09-26 14:31:40 -0700114string ManagerDBusAdaptor::GetState(::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700115 SLOG(DBus, 2) << __func__;
Paul Stewart38fd24c2012-04-25 14:06:29 -0700116 return manager_->CalculateState(NULL);
Chris Masoned7732e42011-05-20 11:08:56 -0700117}
118
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700119::DBus::Path ManagerDBusAdaptor::CreateProfile(const string &name,
Paul Stewart19c871d2011-12-15 16:10:13 -0800120 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700121 SLOG(DBus, 2) << __func__ << ": " << name;
Paul Stewart19c871d2011-12-15 16:10:13 -0800122 Error e;
123 string path;
124 manager_->CreateProfile(name, &path, &e);
125 e.ToDBusError(&error);
126 return ::DBus::Path(path);
Chris Masoned7732e42011-05-20 11:08:56 -0700127}
128
Paul Stewarte73d05c2012-03-29 16:26:05 -0700129void ManagerDBusAdaptor::RemoveProfile(const string &name,
130 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700131 SLOG(DBus, 2) << __func__ << ": " << name;
Paul Stewarte73d05c2012-03-29 16:26:05 -0700132 Error e;
133 manager_->RemoveProfile(name, &e);
134 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700135}
136
mukesh agrawale5929bf2011-08-29 13:47:23 -0700137::DBus::Path ManagerDBusAdaptor::PushProfile(const std::string &name,
Paul Stewart19c871d2011-12-15 16:10:13 -0800138 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700139 SLOG(DBus, 2) << __func__ << ": " << name;
Paul Stewart19c871d2011-12-15 16:10:13 -0800140 Error e;
141 string path;
142 manager_->PushProfile(name, &path, &e);
143 e.ToDBusError(&error);
144 return ::DBus::Path(path);
Chris Masoneccc88812011-06-08 18:00:10 -0700145}
146
Paul Stewart19c871d2011-12-15 16:10:13 -0800147void ManagerDBusAdaptor::PopProfile(const std::string &name,
148 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700149 SLOG(DBus, 2) << __func__ << ": " << name;
Paul Stewart19c871d2011-12-15 16:10:13 -0800150 Error e;
151 manager_->PopProfile(name, &e);
152 e.ToDBusError(&error);
Chris Masoneccc88812011-06-08 18:00:10 -0700153}
154
Paul Stewart19c871d2011-12-15 16:10:13 -0800155void ManagerDBusAdaptor::PopAnyProfile(::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700156 SLOG(DBus, 2) << __func__;
Paul Stewart19c871d2011-12-15 16:10:13 -0800157 Error e;
158 manager_->PopAnyProfile(&e);
159 e.ToDBusError(&error);
Chris Masoneccc88812011-06-08 18:00:10 -0700160}
161
Paul Stewartc681fa02012-03-02 19:40:04 -0800162void ManagerDBusAdaptor::RecheckPortal(::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700163 SLOG(DBus, 2) << __func__;
Paul Stewartc681fa02012-03-02 19:40:04 -0800164 Error e;
165 manager_->RecheckPortal(&e);
166 e.ToDBusError(&error);
167}
168
mukesh agrawal32399322011-09-01 10:53:43 -0700169void ManagerDBusAdaptor::RequestScan(const string &technology,
Chris Masoned7732e42011-05-20 11:08:56 -0700170 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700171 SLOG(DBus, 2) << __func__ << ": " << technology;
mukesh agrawal32399322011-09-01 10:53:43 -0700172 Error e;
173 manager_->RequestScan(technology, &e);
174 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700175}
176
Eric Shienbrood9a245532012-03-07 14:20:39 -0500177void ManagerDBusAdaptor::EnableTechnology(const string &technology_name,
178 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700179 SLOG(DBus, 2) << __func__ << ": " << technology_name;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500180 Error e(Error::kOperationInitiated);
181 DBus::Tag *tag = new DBus::Tag();
182 manager_->EnableTechnology(technology_name, &e, GetMethodReplyCallback(tag));
183 ReturnResultOrDefer(tag, e, &error);
184 // TODO(ers): A reply will be sent to the client as soon as the first
185 // device of a given technology has finished being enabled. It would
186 // seem arguably more correct to wait until all the devices were enabled.
Chris Masoned7732e42011-05-20 11:08:56 -0700187}
188
Eric Shienbrood9a245532012-03-07 14:20:39 -0500189void ManagerDBusAdaptor::DisableTechnology(const string &technology_name,
190 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700191 SLOG(DBus, 2) << __func__ << ": " << technology_name;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 Error e(Error::kOperationInitiated);
193 DBus::Tag *tag = new DBus::Tag();
194 manager_->DisableTechnology(technology_name, &e, GetMethodReplyCallback(tag));
195 ReturnResultOrDefer(tag, e, &error);
196 // TODO(ers): A reply will be sent to the client as soon as the first
197 // device of a given technology has finished being disabled. It would
198 // seem arguably more correct to wait until all the devices were enabled.
Chris Masoned7732e42011-05-20 11:08:56 -0700199}
200
Darin Petkovb65c2452012-02-23 15:17:06 +0100201// Called, e.g., to get WiFiService handle for a hidden SSID.
Chris Masoned7732e42011-05-20 11:08:56 -0700202::DBus::Path ManagerDBusAdaptor::GetService(
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700203 const map<string, ::DBus::Variant> &args,
Chris Masoned7732e42011-05-20 11:08:56 -0700204 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700205 SLOG(DBus, 2) << __func__;
Darin Petkovb65c2452012-02-23 15:17:06 +0100206 ServiceRefPtr service;
207 KeyValueStore args_store;
208 Error e;
209 DBusAdaptor::ArgsToKeyValueStore(args, &args_store, &e);
210 if (e.IsSuccess()) {
211 service = manager_->GetService(args_store, &e);
212 }
213 if (e.ToDBusError(&error)) {
214 return "/"; // ensure return is syntactically valid
215 }
216 return service->GetRpcIdentifier();
Chris Masoned7732e42011-05-20 11:08:56 -0700217}
218
Darin Petkovb65c2452012-02-23 15:17:06 +0100219// Obsolete, use GetService instead.
220::DBus::Path ManagerDBusAdaptor::GetVPNService(
221 const map<string, ::DBus::Variant> &args,
222 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700223 SLOG(DBus, 2) << __func__;
Darin Petkovb65c2452012-02-23 15:17:06 +0100224 return GetService(args, error);
225}
226
227// Obsolete, use GetService instead.
Chris Masoned7732e42011-05-20 11:08:56 -0700228::DBus::Path ManagerDBusAdaptor::GetWifiService(
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700229 const map<string, ::DBus::Variant> &args,
Chris Masoned7732e42011-05-20 11:08:56 -0700230 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700231 SLOG(DBus, 2) << __func__;
Darin Petkovb65c2452012-02-23 15:17:06 +0100232 return GetService(args, error);
Chris Masoned7732e42011-05-20 11:08:56 -0700233}
234
Paul Stewart7f61e522012-03-22 11:13:45 -0700235
236void ManagerDBusAdaptor::ConfigureService(
237 const map<string, ::DBus::Variant> &args,
238 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700239 SLOG(DBus, 2) << __func__;
Paul Stewart7f61e522012-03-22 11:13:45 -0700240 KeyValueStore args_store;
241 Error e;
242 DBusAdaptor::ArgsToKeyValueStore(args, &args_store, &e);
243 if (e.IsSuccess()) {
244 manager_->ConfigureService(args_store, &e);
245 }
246 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700247}
248
mukesh agrawal1830fa12011-09-26 14:31:40 -0700249int32_t ManagerDBusAdaptor::GetDebugLevel(::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700250 SLOG(DBus, 2) << __func__;
Chris Masone7ccc8192011-05-24 14:54:49 -0700251 return logging::GetMinLogLevel();
Chris Masoned7732e42011-05-20 11:08:56 -0700252}
253
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700254void ManagerDBusAdaptor::SetDebugLevel(const int32_t &level,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700255 ::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700256 SLOG(DBus, 2) << __func__ << ": " << level;
Ben Chanbc49ac72012-04-10 19:59:45 -0700257 if (level < logging::LOG_NUM_SEVERITIES) {
Chris Masone7ccc8192011-05-24 14:54:49 -0700258 logging::SetMinLogLevel(level);
Ben Chanbc49ac72012-04-10 19:59:45 -0700259 // Like VLOG, SLOG uses negative verbose level.
260 ScopeLogger::GetInstance()->set_verbose_level(-level);
261 } else {
Chris Masone7ccc8192011-05-24 14:54:49 -0700262 LOG(WARNING) << "Ignoring attempt to set log level to " << level;
Ben Chanbc49ac72012-04-10 19:59:45 -0700263 }
Chris Masoned7732e42011-05-20 11:08:56 -0700264}
265
mukesh agrawal1830fa12011-09-26 14:31:40 -0700266string ManagerDBusAdaptor::GetServiceOrder(::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700267 SLOG(DBus, 2) << __func__;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700268 return manager_->GetTechnologyOrder();
Chris Masoned7732e42011-05-20 11:08:56 -0700269}
270
Paul Stewart22aa71b2011-09-16 12:15:11 -0700271void ManagerDBusAdaptor::SetServiceOrder(const string &order,
Chris Masoned7732e42011-05-20 11:08:56 -0700272 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700273 SLOG(DBus, 2) << __func__ << ": " << order;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700274 Error e;
275 manager_->SetTechnologyOrder(order, &e);
276 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700277}
278
Elly Jones16227d32012-02-09 14:17:25 -0500279std::string ManagerDBusAdaptor::GetDebugTags(::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700280 SLOG(DBus, 2) << __func__;
Ben Chanbc49ac72012-04-10 19:59:45 -0700281 return ScopeLogger::GetInstance()->GetEnabledScopeNames();
Elly Jones16227d32012-02-09 14:17:25 -0500282}
283
Ben Chanbc49ac72012-04-10 19:59:45 -0700284void ManagerDBusAdaptor::SetDebugTags(const std::string &tags,
Elly Jones16227d32012-02-09 14:17:25 -0500285 ::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700286 SLOG(DBus, 2) << __func__ << ": " << tags;
Ben Chanbc49ac72012-04-10 19:59:45 -0700287 ScopeLogger::GetInstance()->EnableScopesByName(tags);
Elly Jones16227d32012-02-09 14:17:25 -0500288}
289
290std::string ManagerDBusAdaptor::ListDebugTags(::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700291 SLOG(DBus, 2) << __func__;
Ben Chanbc49ac72012-04-10 19:59:45 -0700292 return ScopeLogger::GetInstance()->GetAllScopeNames();
Elly Jones16227d32012-02-09 14:17:25 -0500293}
294
Gaurav Shah645bff82012-10-08 14:06:04 -0700295map<string, ::DBus::Variant> ManagerDBusAdaptor::GetNetworksForGeolocation(
296 ::DBus::Error &/*error*/) {
297 SLOG(DBus, 2) << __func__;
298 map<string, ::DBus::Variant> networks;
Gaurav Shahb790aa22012-10-23 12:51:12 -0700299 map<string, GeolocationInfos> geoinfo_map =
300 manager_->GetNetworksForGeolocation();
301 for (map<string, GeolocationInfos>::iterator it = geoinfo_map.begin();
302 it != geoinfo_map.end(); ++it) {
303 Stringmaps value;
304 // Convert GeolocationInfos to their Stringmaps equivalent.
305 for(GeolocationInfos::const_iterator geoinfo_it = it->second.begin();
306 geoinfo_it != it->second.end(); ++geoinfo_it) {
307 value.push_back(geoinfo_it->properties());
308 }
309 networks[it->first] = StringmapsToVariant(value);
310 }
Gaurav Shah645bff82012-10-08 14:06:04 -0700311 return networks;
312}
313
Chris Masoned7732e42011-05-20 11:08:56 -0700314} // namespace shill