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