mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 2 | // 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 10 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 12 | #include "shill/service.h" |
| 13 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 14 | using std::map; |
| 15 | using std::string; |
Paul Stewart | bcb2c96 | 2012-10-31 10:52:10 -0700 | [diff] [blame] | 16 | using std::vector; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 17 | |
| 18 | namespace shill { |
| 19 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 20 | // static |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 21 | const char ServiceDBusAdaptor::kPath[] = "/service/"; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 22 | |
Chris Masone | ec6b18b | 2011-06-08 14:09:10 -0700 | [diff] [blame] | 23 | ServiceDBusAdaptor::ServiceDBusAdaptor(DBus::Connection* conn, Service *service) |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 24 | : DBusAdaptor(conn, kPath + service->unique_name()), |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 25 | service_(service) {} |
Chris Masone | ec6b18b | 2011-06-08 14:09:10 -0700 | [diff] [blame] | 26 | |
| 27 | ServiceDBusAdaptor::~ServiceDBusAdaptor() { |
| 28 | service_ = NULL; |
| 29 | } |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 30 | |
| 31 | void ServiceDBusAdaptor::UpdateConnected() {} |
| 32 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 33 | void ServiceDBusAdaptor::EmitBoolChanged(const string &name, bool value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 34 | SLOG(DBus, 2) << __func__ << ": " << name; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 35 | PropertyChanged(name, DBusAdaptor::BoolToVariant(value)); |
| 36 | } |
| 37 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 38 | void ServiceDBusAdaptor::EmitUint8Changed(const string &name, uint8 value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 39 | SLOG(DBus, 2) << __func__ << ": " << name; |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 40 | PropertyChanged(name, DBusAdaptor::ByteToVariant(value)); |
| 41 | } |
| 42 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 43 | void ServiceDBusAdaptor::EmitUint16Changed(const string &name, uint16 value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 44 | SLOG(DBus, 2) << __func__ << ": " << name; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 45 | PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value)); |
| 46 | } |
| 47 | |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 48 | void ServiceDBusAdaptor::EmitUint16sChanged(const string &name, |
| 49 | const Uint16s &value) { |
| 50 | SLOG(DBus, 2) << __func__ << ": " << name; |
| 51 | PropertyChanged(name, DBusAdaptor::Uint16sToVariant(value)); |
| 52 | } |
| 53 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 54 | void ServiceDBusAdaptor::EmitUintChanged(const string &name, uint32 value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 55 | SLOG(DBus, 2) << __func__ << ": " << name; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 56 | PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 59 | void ServiceDBusAdaptor::EmitIntChanged(const string &name, int value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 60 | SLOG(DBus, 2) << __func__ << ": " << name; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 61 | PropertyChanged(name, DBusAdaptor::Int32ToVariant(value)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Paul Stewart | 1e3bc496 | 2012-09-14 12:20:22 -0700 | [diff] [blame] | 64 | void ServiceDBusAdaptor::EmitRpcIdentifierChanged(const string &name, |
| 65 | const string &value) { |
| 66 | SLOG(DBus, 2) << __func__ << ": " << name; |
| 67 | PropertyChanged(name, DBusAdaptor::PathToVariant(value)); |
| 68 | } |
| 69 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 70 | void ServiceDBusAdaptor::EmitStringChanged(const string &name, |
| 71 | const string &value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 72 | SLOG(DBus, 2) << __func__ << ": " << name; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 73 | PropertyChanged(name, DBusAdaptor::StringToVariant(value)); |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 74 | } |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 75 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 76 | void ServiceDBusAdaptor::EmitStringmapChanged(const string &name, |
| 77 | const Stringmap &value) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 78 | SLOG(DBus, 2) << __func__ << ": " << name; |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 79 | PropertyChanged(name, DBusAdaptor::StringmapToVariant(value)); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 82 | map<string, ::DBus::Variant> ServiceDBusAdaptor::GetProperties( |
| 83 | ::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 84 | SLOG(DBus, 2) << __func__; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 85 | map<string, ::DBus::Variant> properties; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 86 | DBusAdaptor::GetProperties(service_->store(), &properties, &error); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 87 | return properties; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 90 | void ServiceDBusAdaptor::SetProperty(const string &name, |
| 91 | const ::DBus::Variant &value, |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 92 | ::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 93 | SLOG(DBus, 2) << __func__ << ": " << name; |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 94 | DBusAdaptor::SetProperty(service_->mutable_store(), name, value, &error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Paul Stewart | ad0e598 | 2013-07-02 08:47:47 -0700 | [diff] [blame] | 97 | void 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 agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 112 | void ServiceDBusAdaptor::ClearProperty(const string &name, |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 113 | ::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 114 | SLOG(DBus, 2) << __func__ << ": " << name; |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 115 | DBusAdaptor::ClearProperty(service_->mutable_store(), name, &error); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 116 | if (!error.is_set()) { |
| 117 | service_->OnPropertyChanged(name); |
| 118 | } |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Paul Stewart | bcb2c96 | 2012-10-31 10:52:10 -0700 | [diff] [blame] | 121 | vector<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 Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 134 | void ServiceDBusAdaptor::Connect(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 135 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 136 | Error e; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 137 | service_->Connect(&e, "D-Bus RPC"); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 138 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 139 | } |
| 140 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 141 | void ServiceDBusAdaptor::Disconnect(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 142 | SLOG(DBus, 2) << __func__; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 143 | Error e; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 144 | service_->UserInitiatedDisconnect(&e); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 145 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 148 | void ServiceDBusAdaptor::Remove(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 149 | SLOG(DBus, 2) << __func__; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 150 | Error e; |
| 151 | service_->Remove(&e); |
| 152 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void ServiceDBusAdaptor::MoveBefore(const ::DBus::Path& , |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 156 | ::DBus::Error &/*error*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 157 | SLOG(DBus, 2) << __func__; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ServiceDBusAdaptor::MoveAfter(const ::DBus::Path& , |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 161 | ::DBus::Error &/*error*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 162 | SLOG(DBus, 2) << __func__; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 165 | void ServiceDBusAdaptor::ActivateCellularModem(const string &carrier, |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 166 | ::DBus::Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 167 | SLOG(DBus, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 168 | Error e(Error::kOperationInitiated); |
| 169 | DBus::Tag *tag = new DBus::Tag(); |
| 170 | service_->ActivateCellularModem(carrier, &e, GetMethodReplyCallback(tag)); |
| 171 | ReturnResultOrDefer(tag, e, &error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ben Chan | 5d92454 | 2013-02-14 17:49:08 -0800 | [diff] [blame] | 174 | void ServiceDBusAdaptor::CompleteCellularActivation(::DBus::Error &error) { |
| 175 | SLOG(DBus, 2) << __func__; |
| 176 | Error e; |
| 177 | service_->CompleteCellularActivation(&e); |
| 178 | e.ToDBusError(&error); |
| 179 | } |
| 180 | |
Paul Stewart | 967eaeb | 2013-04-25 19:53:07 -0700 | [diff] [blame] | 181 | map<::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 Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 193 | } // namespace shill |