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