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