blob: 71003d618c27060f7650902803a8621decbb84c0 [file] [log] [blame]
mukesh agrawal4d0401c2012-01-06 16:05:31 -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/service_dbus_adaptor.h"
6
7#include <map>
8#include <string>
9
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070011#include "shill/logging.h"
Chris Masonea82b7112011-05-25 15:16:29 -070012#include "shill/service.h"
13
Chris Masoned7732e42011-05-20 11:08:56 -070014using std::map;
15using std::string;
Paul Stewartbcb2c962012-10-31 10:52:10 -070016using std::vector;
Chris Masoned7732e42011-05-20 11:08:56 -070017
18namespace shill {
19
Chris Masoned7732e42011-05-20 11:08:56 -070020// static
Chris Masonea82b7112011-05-25 15:16:29 -070021const char ServiceDBusAdaptor::kPath[] = "/service/";
Chris Masoned7732e42011-05-20 11:08:56 -070022
Chris Masoneec6b18b2011-06-08 14:09:10 -070023ServiceDBusAdaptor::ServiceDBusAdaptor(DBus::Connection* conn, Service *service)
Darin Petkov457728b2013-01-09 09:49:08 +010024 : DBusAdaptor(conn, kPath + service->unique_name()),
Chris Masoned7732e42011-05-20 11:08:56 -070025 service_(service) {}
Chris Masoneec6b18b2011-06-08 14:09:10 -070026
27ServiceDBusAdaptor::~ServiceDBusAdaptor() {
28 service_ = NULL;
29}
Chris Masoned7732e42011-05-20 11:08:56 -070030
31void ServiceDBusAdaptor::UpdateConnected() {}
32
Darin Petkovd78ee7e2012-01-12 11:21:10 +010033void ServiceDBusAdaptor::EmitBoolChanged(const string &name, bool value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070034 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070035 PropertyChanged(name, DBusAdaptor::BoolToVariant(value));
36}
37
Darin Petkovd78ee7e2012-01-12 11:21:10 +010038void ServiceDBusAdaptor::EmitUint8Changed(const string &name, uint8 value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070039 SLOG(DBus, 2) << __func__ << ": " << name;
Darin Petkovd78ee7e2012-01-12 11:21:10 +010040 PropertyChanged(name, DBusAdaptor::ByteToVariant(value));
41}
42
mukesh agrawale1d90e92012-02-15 17:36:08 -080043void ServiceDBusAdaptor::EmitUint16Changed(const string &name, uint16 value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070044 SLOG(DBus, 2) << __func__ << ": " << name;
mukesh agrawale1d90e92012-02-15 17:36:08 -080045 PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value));
46}
47
mukesh agrawale7c7e652013-06-18 17:19:39 -070048void ServiceDBusAdaptor::EmitUint16sChanged(const string &name,
49 const Uint16s &value) {
50 SLOG(DBus, 2) << __func__ << ": " << name;
51 PropertyChanged(name, DBusAdaptor::Uint16sToVariant(value));
52}
53
Darin Petkovd78ee7e2012-01-12 11:21:10 +010054void ServiceDBusAdaptor::EmitUintChanged(const string &name, uint32 value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070055 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070057}
58
Darin Petkovd78ee7e2012-01-12 11:21:10 +010059void ServiceDBusAdaptor::EmitIntChanged(const string &name, int value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070060 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070061 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070062}
63
Paul Stewart1e3bc4962012-09-14 12:20:22 -070064void ServiceDBusAdaptor::EmitRpcIdentifierChanged(const string &name,
65 const string &value) {
66 SLOG(DBus, 2) << __func__ << ": " << name;
67 PropertyChanged(name, DBusAdaptor::PathToVariant(value));
68}
69
Darin Petkovd78ee7e2012-01-12 11:21:10 +010070void ServiceDBusAdaptor::EmitStringChanged(const string &name,
71 const string &value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070072 SLOG(DBus, 2) << __func__ << ": " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070073 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
Darin Petkov9cb02682012-01-28 00:17:38 +010074}
Chris Masoned0ceb8c2011-06-02 10:05:39 -070075
Darin Petkov9cb02682012-01-28 00:17:38 +010076void ServiceDBusAdaptor::EmitStringmapChanged(const string &name,
77 const Stringmap &value) {
mukesh agrawal06175d72012-04-23 16:46:01 -070078 SLOG(DBus, 2) << __func__ << ": " << name;
Darin Petkov9cb02682012-01-28 00:17:38 +010079 PropertyChanged(name, DBusAdaptor::StringmapToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070080}
81
Chris Masoned7732e42011-05-20 11:08:56 -070082map<string, ::DBus::Variant> ServiceDBusAdaptor::GetProperties(
83 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -070084 SLOG(DBus, 2) << __func__;
Chris Masonea8a2c252011-06-27 22:16:30 -070085 map<string, ::DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -070086 DBusAdaptor::GetProperties(service_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -070087 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -070088}
89
mukesh agrawal4d0401c2012-01-06 16:05:31 -080090void ServiceDBusAdaptor::SetProperty(const string &name,
91 const ::DBus::Variant &value,
Chris Masoned7732e42011-05-20 11:08:56 -070092 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -070093 SLOG(DBus, 2) << __func__ << ": " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080094 DBusAdaptor::SetProperty(service_->mutable_store(), name, value, &error);
Chris Masoned7732e42011-05-20 11:08:56 -070095}
96
Paul Stewartad0e5982013-07-02 08:47:47 -070097void ServiceDBusAdaptor::SetProperties(
98 const map<string, ::DBus::Variant> &args,
99 ::DBus::Error &error) {
100 SLOG(DBus, 2) << __func__;
101 KeyValueStore args_store;
102 Error key_value_store_error;
103 DBusAdaptor::ArgsToKeyValueStore(args, &args_store, &key_value_store_error);
104 if (key_value_store_error.ToDBusError(&error)) {
105 return;
106 }
107 Error configure_error;
108 service_->Configure(args_store, &configure_error);
109 configure_error.ToDBusError(&error);
110}
111
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800112void ServiceDBusAdaptor::ClearProperty(const string &name,
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800113 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700114 SLOG(DBus, 2) << __func__ << ": " << name;
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800115 DBusAdaptor::ClearProperty(service_->mutable_store(), name, &error);
Paul Stewartd215af62012-04-24 23:25:50 -0700116 if (!error.is_set()) {
117 service_->OnPropertyChanged(name);
118 }
Chris Masoned7732e42011-05-20 11:08:56 -0700119}
120
Paul Stewartbcb2c962012-10-31 10:52:10 -0700121vector<bool> ServiceDBusAdaptor::ClearProperties(const vector<string> &names,
122 ::DBus::Error &/*error*/) {
123 SLOG(DBus, 2) << __func__;
124 vector<bool> results;
125 vector<string>::const_iterator it;
126 for (it = names.begin(); it != names.end(); ++it) {
127 ::DBus::Error error;
128 ClearProperty(*it, error);
129 results.push_back(!error.is_set());
130 }
131 return results;
132}
133
Chris Masoned7732e42011-05-20 11:08:56 -0700134void ServiceDBusAdaptor::Connect(::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700135 SLOG(DBus, 2) << __func__;
Darin Petkov4d6d9412011-08-24 13:19:54 -0700136 Error e;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700137 service_->Connect(&e, "D-Bus RPC");
Darin Petkov4d6d9412011-08-24 13:19:54 -0700138 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700139}
140
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000141void ServiceDBusAdaptor::Disconnect(::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700142 SLOG(DBus, 2) << __func__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000143 Error e;
Christopher Wileyabd3b502012-09-26 13:08:52 -0700144 service_->UserInitiatedDisconnect(&e);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000145 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700146}
147
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800148void ServiceDBusAdaptor::Remove(::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700149 SLOG(DBus, 2) << __func__;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800150 Error e;
151 service_->Remove(&e);
152 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700153}
154
155void ServiceDBusAdaptor::MoveBefore(const ::DBus::Path& ,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700156 ::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700157 SLOG(DBus, 2) << __func__;
Chris Masoned7732e42011-05-20 11:08:56 -0700158}
159
160void ServiceDBusAdaptor::MoveAfter(const ::DBus::Path& ,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700161 ::DBus::Error &/*error*/) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700162 SLOG(DBus, 2) << __func__;
Chris Masoned7732e42011-05-20 11:08:56 -0700163}
164
Darin Petkovc408e692011-08-17 13:47:15 -0700165void ServiceDBusAdaptor::ActivateCellularModem(const string &carrier,
Chris Masoned7732e42011-05-20 11:08:56 -0700166 ::DBus::Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700167 SLOG(DBus, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500168 Error e(Error::kOperationInitiated);
169 DBus::Tag *tag = new DBus::Tag();
170 service_->ActivateCellularModem(carrier, &e, GetMethodReplyCallback(tag));
171 ReturnResultOrDefer(tag, e, &error);
Chris Masoned7732e42011-05-20 11:08:56 -0700172}
173
Ben Chan5d924542013-02-14 17:49:08 -0800174void ServiceDBusAdaptor::CompleteCellularActivation(::DBus::Error &error) {
175 SLOG(DBus, 2) << __func__;
176 Error e;
177 service_->CompleteCellularActivation(&e);
178 e.ToDBusError(&error);
179}
180
Paul Stewart967eaeb2013-04-25 19:53:07 -0700181map<::DBus::Path, string> ServiceDBusAdaptor::GetLoadableProfileEntries(
182 ::DBus::Error &error) {
183 SLOG(DBus, 2) << __func__;
184 map<string, string> profile_entry_strings =
185 service_->GetLoadableProfileEntries();
186 map<::DBus::Path, string> profile_entries;
187 for (const auto &entry : profile_entry_strings) {
188 profile_entries[::DBus::Path(entry.first)] = entry.second;
189 }
190 return profile_entries;
191}
192
Chris Masoned7732e42011-05-20 11:08:56 -0700193} // namespace shill