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 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 97 | void ServiceDBusAdaptor::ClearProperty(const string &name, |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 98 | ::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 99 | SLOG(DBus, 2) << __func__ << ": " << name; |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 100 | DBusAdaptor::ClearProperty(service_->mutable_store(), name, &error); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 101 | if (!error.is_set()) { |
| 102 | service_->OnPropertyChanged(name); |
| 103 | } |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Paul Stewart | bcb2c96 | 2012-10-31 10:52:10 -0700 | [diff] [blame] | 106 | vector<bool> ServiceDBusAdaptor::ClearProperties(const vector<string> &names, |
| 107 | ::DBus::Error &/*error*/) { |
| 108 | SLOG(DBus, 2) << __func__; |
| 109 | vector<bool> results; |
| 110 | vector<string>::const_iterator it; |
| 111 | for (it = names.begin(); it != names.end(); ++it) { |
| 112 | ::DBus::Error error; |
| 113 | ClearProperty(*it, error); |
| 114 | results.push_back(!error.is_set()); |
| 115 | } |
| 116 | return results; |
| 117 | } |
| 118 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 119 | void ServiceDBusAdaptor::Connect(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 120 | SLOG(DBus, 2) << __func__; |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 121 | Error e; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 122 | service_->Connect(&e, "D-Bus RPC"); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 123 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 124 | } |
| 125 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 126 | void ServiceDBusAdaptor::Disconnect(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 127 | SLOG(DBus, 2) << __func__; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 128 | Error e; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 129 | service_->UserInitiatedDisconnect(&e); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 130 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 133 | void ServiceDBusAdaptor::Remove(::DBus::Error &error) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 134 | SLOG(DBus, 2) << __func__; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 135 | Error e; |
| 136 | service_->Remove(&e); |
| 137 | e.ToDBusError(&error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void ServiceDBusAdaptor::MoveBefore(const ::DBus::Path& , |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 141 | ::DBus::Error &/*error*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 142 | SLOG(DBus, 2) << __func__; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void ServiceDBusAdaptor::MoveAfter(const ::DBus::Path& , |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 146 | ::DBus::Error &/*error*/) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 147 | SLOG(DBus, 2) << __func__; |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 150 | void ServiceDBusAdaptor::ActivateCellularModem(const string &carrier, |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 151 | ::DBus::Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 152 | SLOG(DBus, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 153 | Error e(Error::kOperationInitiated); |
| 154 | DBus::Tag *tag = new DBus::Tag(); |
| 155 | service_->ActivateCellularModem(carrier, &e, GetMethodReplyCallback(tag)); |
| 156 | ReturnResultOrDefer(tag, e, &error); |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ben Chan | 5d92454 | 2013-02-14 17:49:08 -0800 | [diff] [blame] | 159 | void ServiceDBusAdaptor::CompleteCellularActivation(::DBus::Error &error) { |
| 160 | SLOG(DBus, 2) << __func__; |
| 161 | Error e; |
| 162 | service_->CompleteCellularActivation(&e); |
| 163 | e.ToDBusError(&error); |
| 164 | } |
| 165 | |
Paul Stewart | 967eaeb | 2013-04-25 19:53:07 -0700 | [diff] [blame] | 166 | map<::DBus::Path, string> ServiceDBusAdaptor::GetLoadableProfileEntries( |
| 167 | ::DBus::Error &error) { |
| 168 | SLOG(DBus, 2) << __func__; |
| 169 | map<string, string> profile_entry_strings = |
| 170 | service_->GetLoadableProfileEntries(); |
| 171 | map<::DBus::Path, string> profile_entries; |
| 172 | for (const auto &entry : profile_entry_strings) { |
| 173 | profile_entries[::DBus::Path(entry.first)] = entry.second; |
| 174 | } |
| 175 | return profile_entries; |
| 176 | } |
| 177 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 178 | } // namespace shill |