Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "shill/wimax_service.h" |
| 6 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 9 | #include <base/string_number_conversions.h> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 10 | #include <base/string_util.h> |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 14 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 15 | #include "shill/logging.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 16 | #include "shill/manager.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 17 | #include "shill/store_interface.h" |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 18 | #include "shill/technology.h" |
| 19 | #include "shill/wimax.h" |
| 20 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 21 | using std::replace_if; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 22 | using std::string; |
| 23 | |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 24 | namespace shill { |
| 25 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 26 | const char WiMaxService::kStorageNetworkId[] = "NetworkId"; |
| 27 | const char WiMaxService::kNetworkIdProperty[] = "NetworkId"; |
Darin Petkov | 6b9b2e1 | 2012-07-10 15:51:42 +0200 | [diff] [blame] | 28 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 29 | |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 30 | WiMaxService::WiMaxService(ControlInterface *control, |
| 31 | EventDispatcher *dispatcher, |
| 32 | Metrics *metrics, |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 33 | Manager *manager) |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 34 | : Service(control, dispatcher, metrics, manager, Technology::kWiMax), |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 35 | need_passphrase_(true), |
| 36 | is_default_(false) { |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 37 | PropertyStore *store = this->mutable_store(); |
| 38 | // TODO(benchan): Support networks that require no user credentials or |
| 39 | // implicitly defined credentials. |
| 40 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 41 | store->RegisterConstString(kNetworkIdProperty, &network_id_); |
| 42 | |
| 43 | IgnoreParameterForConfigure(kNetworkIdProperty); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 44 | |
| 45 | // Initialize a default storage identifier based on the service's unique |
| 46 | // name. The identifier most likely needs to be reinitialized by the caller |
| 47 | // when its components have been set. |
| 48 | InitStorageIdentifier(); |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 49 | } |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 50 | |
| 51 | WiMaxService::~WiMaxService() {} |
| 52 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 53 | void WiMaxService::GetConnectParameters(KeyValueStore *parameters) const { |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 54 | CHECK(parameters); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 55 | if (!eap().anonymous_identity.empty()) { |
| 56 | parameters->SetString(wimax_manager::kEAPAnonymousIdentity, |
| 57 | eap().anonymous_identity); |
| 58 | } |
| 59 | if (!eap().identity.empty()) { |
| 60 | parameters->SetString(wimax_manager::kEAPUserIdentity, eap().identity); |
| 61 | } |
| 62 | if (!eap().password.empty()) { |
| 63 | parameters->SetString(wimax_manager::kEAPUserPassword, eap().password); |
| 64 | } |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 67 | RpcIdentifier WiMaxService::GetNetworkObjectPath() const { |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 68 | CHECK(proxy_.get()); |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 69 | return proxy_->path(); |
Ben Chan | 4e5c131 | 2012-05-18 18:45:38 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 72 | void WiMaxService::Stop() { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 73 | if (!IsStarted()) { |
| 74 | return; |
| 75 | } |
| 76 | LOG(INFO) << "Stopping WiMAX service: " << GetStorageIdentifier(); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 77 | proxy_.reset(); |
| 78 | SetStrength(0); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 79 | if (device_) { |
| 80 | device_->OnServiceStopped(this); |
| 81 | device_ = NULL; |
| 82 | } |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 83 | UpdateConnectable(); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 86 | bool WiMaxService::Start(WiMaxNetworkProxyInterface *proxy) { |
| 87 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 88 | CHECK(proxy); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 89 | scoped_ptr<WiMaxNetworkProxyInterface> local_proxy(proxy); |
| 90 | if (IsStarted()) { |
| 91 | return true; |
| 92 | } |
| 93 | if (friendly_name().empty()) { |
| 94 | LOG(ERROR) << "Empty service name."; |
| 95 | return false; |
| 96 | } |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 97 | Error error; |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 98 | network_name_ = proxy->Name(&error); |
| 99 | if (error.IsFailure()) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 102 | uint32 identifier = proxy->Identifier(&error); |
| 103 | if (error.IsFailure()) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 106 | WiMaxNetworkId id = ConvertIdentifierToNetworkId(identifier); |
| 107 | if (id != network_id_) { |
| 108 | LOG(ERROR) << "Network identifiers don't match: " |
| 109 | << id << " != " << network_id_; |
| 110 | return false; |
| 111 | } |
| 112 | int signal_strength = proxy->SignalStrength(&error); |
| 113 | if (error.IsFailure()) { |
Ben Chan | ac6e836 | 2012-05-20 00:39:58 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | SetStrength(signal_strength); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 117 | proxy->set_signal_strength_changed_callback( |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 118 | Bind(&WiMaxService::OnSignalStrengthChanged, Unretained(this))); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 119 | proxy_.reset(local_proxy.release()); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 120 | UpdateConnectable(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 121 | LOG(INFO) << "WiMAX service started: " << GetStorageIdentifier(); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 125 | bool WiMaxService::IsStarted() const { |
| 126 | return proxy_.get(); |
| 127 | } |
| 128 | |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 129 | void WiMaxService::Connect(Error *error) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 130 | if (device_) { |
Ben Chan | b5310a1 | 2012-06-15 13:53:29 -0700 | [diff] [blame] | 131 | // TODO(benchan): Populate error again after changing the way that |
| 132 | // Chrome handles Error::kAlreadyConnected situation. |
| 133 | LOG(WARNING) << "Service " << GetStorageIdentifier() |
| 134 | << " is already being connected or already connected."; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 135 | return; |
| 136 | } |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 137 | if (!connectable()) { |
| 138 | LOG(ERROR) << "Can't connect. Service " << GetStorageIdentifier() |
| 139 | << " is not connectable."; |
| 140 | Error::PopulateAndLog(error, Error::kOperationFailed, |
| 141 | Error::GetDefaultMessage(Error::kOperationFailed)); |
| 142 | return; |
| 143 | } |
Darin Petkov | 8ea0eaf | 2012-05-29 11:21:33 +0200 | [diff] [blame] | 144 | WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this); |
| 145 | if (!carrier) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 146 | Error::PopulateAndLog( |
| 147 | error, Error::kNoCarrier, "No suitable WiMAX device available."); |
| 148 | return; |
| 149 | } |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 150 | Service::Connect(error); |
Darin Petkov | 8ea0eaf | 2012-05-29 11:21:33 +0200 | [diff] [blame] | 151 | carrier->ConnectTo(this, error); |
| 152 | if (error->IsSuccess()) { |
| 153 | // Associate with the carrier device if the connection process has been |
| 154 | // initiated successfully. |
| 155 | device_ = carrier; |
| 156 | } |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void WiMaxService::Disconnect(Error *error) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 160 | if (!device_) { |
| 161 | Error::PopulateAndLog( |
| 162 | error, Error::kNotConnected, "Not connected."); |
| 163 | return; |
| 164 | } |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 165 | Service::Disconnect(error); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 166 | device_->DisconnectFrom(this, error); |
| 167 | device_ = NULL; |
Ben Chan | 1411a04 | 2012-06-05 18:56:20 -0700 | [diff] [blame] | 168 | // Set |need_passphrase_| to true so that after users explicitly disconnect |
| 169 | // from the network, the UI will prompt for credentials when they try to |
| 170 | // re-connect to the same network. This works around the fact that there is |
| 171 | // currently no mechanism for changing credentials for WiMAX connections. |
| 172 | // TODO(benchan,petkov): Find a better way to allow users to change the |
| 173 | // EAP credentials. |
| 174 | need_passphrase_ = true; |
Ben Chan | cdbfc9f | 2012-06-06 13:05:11 -0700 | [diff] [blame] | 175 | UpdateConnectable(); |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 178 | string WiMaxService::GetStorageIdentifier() const { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 179 | return storage_id_; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | string WiMaxService::GetDeviceRpcId(Error *error) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 183 | if (device_) { |
| 184 | return device_->GetRpcIdentifier(); |
| 185 | } |
| 186 | error->Populate(Error::kNotSupported); |
| 187 | return "/"; |
Ben Chan | c07362b | 2012-05-12 10:54:11 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Darin Petkov | 6b9b2e1 | 2012-07-10 15:51:42 +0200 | [diff] [blame] | 190 | bool WiMaxService::IsAutoConnectable(const char **reason) const { |
| 191 | if (!Service::IsAutoConnectable(reason)) { |
| 192 | return false; |
| 193 | } |
| 194 | WiMaxRefPtr device = manager()->wimax_provider()->SelectCarrier(this); |
| 195 | DCHECK(device); |
| 196 | if (!device->IsIdle()) { |
| 197 | *reason = kAutoConnBusy; |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 203 | bool WiMaxService::Is8021x() const { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | void WiMaxService::set_eap(const EapCredentials &eap) { |
| 208 | Service::set_eap(eap); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 209 | need_passphrase_ = eap.identity.empty() || eap.password.empty(); |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 210 | UpdateConnectable(); |
| 211 | } |
| 212 | |
| 213 | void WiMaxService::UpdateConnectable() { |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 214 | SetConnectable(IsStarted() && !need_passphrase_); |
Darin Petkov | 8021e7f | 2012-05-21 12:15:00 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Darin Petkov | 1e52a1b | 2012-05-21 10:35:56 +0200 | [diff] [blame] | 217 | void WiMaxService::OnSignalStrengthChanged(int strength) { |
| 218 | SLOG(WiMax, 2) << __func__ << "(" << strength << ")"; |
| 219 | SetStrength(strength); |
| 220 | } |
| 221 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 222 | bool WiMaxService::Save(StoreInterface *storage) { |
| 223 | SLOG(WiMax, 2) << __func__; |
| 224 | if (!Service::Save(storage)) { |
| 225 | return false; |
| 226 | } |
| 227 | const string id = GetStorageIdentifier(); |
| 228 | storage->SetString(id, kStorageNetworkId, network_id_); |
| 229 | return true; |
| 230 | } |
| 231 | |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 232 | bool WiMaxService::Unload() { |
| 233 | // The base method also disconnects the service. |
| 234 | Service::Unload(); |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 235 | ClearPassphrase(); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 236 | // Notify the WiMAX provider that this service has been unloaded. If the |
| 237 | // provider releases ownership of this service, it needs to be deregistered. |
| 238 | return manager()->wimax_provider()->OnServiceUnloaded(this); |
| 239 | } |
| 240 | |
Darin Petkov | 8ea0eaf | 2012-05-29 11:21:33 +0200 | [diff] [blame] | 241 | void WiMaxService::SetState(ConnectState state) { |
| 242 | Service::SetState(state); |
| 243 | if (!IsConnecting() && !IsConnected()) { |
| 244 | // Disassociate from any carrier device if it's not connected anymore. |
| 245 | device_ = NULL; |
| 246 | } |
| 247 | } |
| 248 | |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 249 | // static |
| 250 | WiMaxNetworkId WiMaxService::ConvertIdentifierToNetworkId(uint32 identifier) { |
| 251 | return base::StringPrintf("%08x", identifier); |
| 252 | } |
| 253 | |
| 254 | void WiMaxService::InitStorageIdentifier() { |
| 255 | storage_id_ = CreateStorageIdentifier(network_id_, friendly_name()); |
| 256 | } |
| 257 | |
| 258 | // static |
| 259 | string WiMaxService::CreateStorageIdentifier(const WiMaxNetworkId &id, |
| 260 | const string &name) { |
| 261 | string storage_id = |
| 262 | base::StringPrintf("%s_%s_%s", |
| 263 | flimflam::kTypeWimax, name.c_str(), id.c_str()); |
| 264 | StringToLowerASCII(&storage_id); |
| 265 | replace_if(storage_id.begin(), storage_id.end(), &Service::IllegalChar, '_'); |
| 266 | return storage_id; |
| 267 | } |
| 268 | |
Darin Petkov | a3f9f77 | 2012-05-31 12:11:28 +0200 | [diff] [blame] | 269 | void WiMaxService::ClearPassphrase() { |
| 270 | EapCredentials creds = eap(); |
| 271 | creds.password.clear(); |
| 272 | // Updates the service credentials and connectability status. |
| 273 | set_eap(creds); |
| 274 | } |
| 275 | |
Ben Chan | 99c8a4d | 2012-05-01 08:11:53 -0700 | [diff] [blame] | 276 | } // namespace shill |