blob: c6b6fb4aec8ebb159baabadcbf0ad2554542c9bf [file] [log] [blame]
Ben Chan99c8a4d2012-05-01 08:11:53 -07001// 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 Petkov9893d9c2012-05-17 15:27:31 -07007#include <algorithm>
8
Darin Petkovd1cd7972012-05-22 15:26:15 +02009#include <base/string_number_conversions.h>
Darin Petkove4b27022012-05-16 13:28:50 +020010#include <base/string_util.h>
Darin Petkov9893d9c2012-05-17 15:27:31 -070011#include <base/stringprintf.h>
Darin Petkove4b27022012-05-16 13:28:50 +020012#include <chromeos/dbus/service_constants.h>
13
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070014#include "shill/dbus_adaptor.h"
Paul Stewartc43cbbe2013-04-11 06:29:30 -070015#include "shill/eap_credentials.h"
Darin Petkov25665aa2012-05-21 14:08:12 +020016#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070017#include "shill/logging.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020018#include "shill/manager.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020019#include "shill/store_interface.h"
Ben Chan99c8a4d2012-05-01 08:11:53 -070020#include "shill/technology.h"
21#include "shill/wimax.h"
22
Darin Petkov9893d9c2012-05-17 15:27:31 -070023using std::replace_if;
Ben Chanc07362b2012-05-12 10:54:11 -070024using std::string;
25
Ben Chan99c8a4d2012-05-01 08:11:53 -070026namespace shill {
27
Darin Petkovd1cd7972012-05-22 15:26:15 +020028const char WiMaxService::kStorageNetworkId[] = "NetworkId";
29const char WiMaxService::kNetworkIdProperty[] = "NetworkId";
Darin Petkov6b9b2e12012-07-10 15:51:42 +020030
Ben Chan99c8a4d2012-05-01 08:11:53 -070031WiMaxService::WiMaxService(ControlInterface *control,
32 EventDispatcher *dispatcher,
33 Metrics *metrics,
Darin Petkovc63dcf02012-05-24 11:51:43 +020034 Manager *manager)
Ben Chan99c8a4d2012-05-01 08:11:53 -070035 : Service(control, dispatcher, metrics, manager, Technology::kWiMax),
Darin Petkovc1e52732012-05-25 15:23:45 +020036 need_passphrase_(true),
37 is_default_(false) {
Ben Chan4e5c1312012-05-18 18:45:38 -070038 PropertyStore *store = this->mutable_store();
39 // TODO(benchan): Support networks that require no user credentials or
40 // implicitly defined credentials.
Ben Chan4276cca2013-09-20 10:07:04 -070041 store->RegisterBool(kPassphraseRequiredProperty, &need_passphrase_);
Darin Petkovd1cd7972012-05-22 15:26:15 +020042 store->RegisterConstString(kNetworkIdProperty, &network_id_);
43
Paul Stewartc43cbbe2013-04-11 06:29:30 -070044 SetEapCredentials(new EapCredentials());
45
Darin Petkovd1cd7972012-05-22 15:26:15 +020046 IgnoreParameterForConfigure(kNetworkIdProperty);
Darin Petkovc1e52732012-05-25 15:23:45 +020047
48 // Initialize a default storage identifier based on the service's unique
49 // name. The identifier most likely needs to be reinitialized by the caller
50 // when its components have been set.
51 InitStorageIdentifier();
Ben Chan4e5c1312012-05-18 18:45:38 -070052}
Ben Chan99c8a4d2012-05-01 08:11:53 -070053
54WiMaxService::~WiMaxService() {}
55
Darin Petkov25665aa2012-05-21 14:08:12 +020056void WiMaxService::GetConnectParameters(KeyValueStore *parameters) const {
Ben Chan4e5c1312012-05-18 18:45:38 -070057 CHECK(parameters);
Paul Stewartc43cbbe2013-04-11 06:29:30 -070058 eap()->PopulateWiMaxProperties(parameters);
Ben Chan4e5c1312012-05-18 18:45:38 -070059}
60
Darin Petkov1e52a1b2012-05-21 10:35:56 +020061RpcIdentifier WiMaxService::GetNetworkObjectPath() const {
Ben Chan4e5c1312012-05-18 18:45:38 -070062 CHECK(proxy_.get());
Darin Petkov1e52a1b2012-05-21 10:35:56 +020063 return proxy_->path();
Ben Chan4e5c1312012-05-18 18:45:38 -070064}
65
Darin Petkovd1cd7972012-05-22 15:26:15 +020066void WiMaxService::Stop() {
Darin Petkovc63dcf02012-05-24 11:51:43 +020067 if (!IsStarted()) {
68 return;
69 }
70 LOG(INFO) << "Stopping WiMAX service: " << GetStorageIdentifier();
Darin Petkovd1cd7972012-05-22 15:26:15 +020071 proxy_.reset();
72 SetStrength(0);
Darin Petkovc63dcf02012-05-24 11:51:43 +020073 if (device_) {
74 device_->OnServiceStopped(this);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070075 SetDevice(NULL);
Darin Petkovc63dcf02012-05-24 11:51:43 +020076 }
Darin Petkovc1e52732012-05-25 15:23:45 +020077 UpdateConnectable();
Darin Petkovd1cd7972012-05-22 15:26:15 +020078}
79
Darin Petkov9893d9c2012-05-17 15:27:31 -070080bool WiMaxService::Start(WiMaxNetworkProxyInterface *proxy) {
81 SLOG(WiMax, 2) << __func__;
Darin Petkov1e52a1b2012-05-21 10:35:56 +020082 CHECK(proxy);
Darin Petkovd1cd7972012-05-22 15:26:15 +020083 scoped_ptr<WiMaxNetworkProxyInterface> local_proxy(proxy);
84 if (IsStarted()) {
85 return true;
86 }
87 if (friendly_name().empty()) {
88 LOG(ERROR) << "Empty service name.";
89 return false;
90 }
Darin Petkov9893d9c2012-05-17 15:27:31 -070091 Error error;
Darin Petkovd1cd7972012-05-22 15:26:15 +020092 network_name_ = proxy->Name(&error);
93 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -070094 return false;
95 }
Darin Petkovd1cd7972012-05-22 15:26:15 +020096 uint32 identifier = proxy->Identifier(&error);
97 if (error.IsFailure()) {
Darin Petkov9893d9c2012-05-17 15:27:31 -070098 return false;
99 }
Darin Petkovd1cd7972012-05-22 15:26:15 +0200100 WiMaxNetworkId id = ConvertIdentifierToNetworkId(identifier);
101 if (id != network_id_) {
102 LOG(ERROR) << "Network identifiers don't match: "
103 << id << " != " << network_id_;
104 return false;
105 }
106 int signal_strength = proxy->SignalStrength(&error);
107 if (error.IsFailure()) {
Ben Chanac6e8362012-05-20 00:39:58 -0700108 return false;
109 }
110 SetStrength(signal_strength);
Darin Petkovd1cd7972012-05-22 15:26:15 +0200111 proxy->set_signal_strength_changed_callback(
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200112 Bind(&WiMaxService::OnSignalStrengthChanged, Unretained(this)));
Darin Petkovd1cd7972012-05-22 15:26:15 +0200113 proxy_.reset(local_proxy.release());
Darin Petkova3f9f772012-05-31 12:11:28 +0200114 UpdateConnectable();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200115 LOG(INFO) << "WiMAX service started: " << GetStorageIdentifier();
Darin Petkov9893d9c2012-05-17 15:27:31 -0700116 return true;
117}
118
Darin Petkovd1cd7972012-05-22 15:26:15 +0200119bool WiMaxService::IsStarted() const {
120 return proxy_.get();
121}
122
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700123void WiMaxService::Connect(Error *error, const char *reason) {
Ben Chane494e312013-08-23 12:56:05 -0700124 SLOG(WiMax, 2) << __func__;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200125 if (device_) {
Ben Chanb5310a12012-06-15 13:53:29 -0700126 // TODO(benchan): Populate error again after changing the way that
127 // Chrome handles Error::kAlreadyConnected situation.
128 LOG(WARNING) << "Service " << GetStorageIdentifier()
129 << " is already being connected or already connected.";
Darin Petkovc63dcf02012-05-24 11:51:43 +0200130 return;
131 }
Darin Petkova3f9f772012-05-31 12:11:28 +0200132 if (!connectable()) {
133 LOG(ERROR) << "Can't connect. Service " << GetStorageIdentifier()
134 << " is not connectable.";
135 Error::PopulateAndLog(error, Error::kOperationFailed,
136 Error::GetDefaultMessage(Error::kOperationFailed));
137 return;
138 }
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200139 WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this);
140 if (!carrier) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200141 Error::PopulateAndLog(
142 error, Error::kNoCarrier, "No suitable WiMAX device available.");
143 return;
144 }
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700145 Service::Connect(error, reason);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200146 carrier->ConnectTo(this, error);
147 if (error->IsSuccess()) {
148 // Associate with the carrier device if the connection process has been
149 // initiated successfully.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700150 SetDevice(carrier);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200151 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700152}
153
154void WiMaxService::Disconnect(Error *error) {
Ben Chane494e312013-08-23 12:56:05 -0700155 SLOG(WiMax, 2) << __func__;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200156 if (!device_) {
157 Error::PopulateAndLog(
158 error, Error::kNotConnected, "Not connected.");
159 return;
160 }
Ben Chan99c8a4d2012-05-01 08:11:53 -0700161 Service::Disconnect(error);
Darin Petkovc63dcf02012-05-24 11:51:43 +0200162 device_->DisconnectFrom(this, error);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700163 SetDevice(NULL);
Ben Chan99c8a4d2012-05-01 08:11:53 -0700164}
165
Ben Chanc07362b2012-05-12 10:54:11 -0700166string WiMaxService::GetStorageIdentifier() const {
Darin Petkove4b27022012-05-16 13:28:50 +0200167 return storage_id_;
Ben Chanc07362b2012-05-12 10:54:11 -0700168}
169
Paul Stewart1cf7eb82013-12-03 19:34:36 -0800170string WiMaxService::GetDeviceRpcId(Error *error) const {
mukesh agrawald4dc0832013-03-25 14:38:26 -0700171 if (!device_) {
172 error->Populate(Error::kNotFound, "Not associated with a device");
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700173 return DBusAdaptor::kNullPath;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200174 }
mukesh agrawald4dc0832013-03-25 14:38:26 -0700175 return device_->GetRpcIdentifier();
Ben Chanc07362b2012-05-12 10:54:11 -0700176}
177
Darin Petkov6b9b2e12012-07-10 15:51:42 +0200178bool WiMaxService::IsAutoConnectable(const char **reason) const {
179 if (!Service::IsAutoConnectable(reason)) {
180 return false;
181 }
182 WiMaxRefPtr device = manager()->wimax_provider()->SelectCarrier(this);
183 DCHECK(device);
184 if (!device->IsIdle()) {
185 *reason = kAutoConnBusy;
186 return false;
187 }
188 return true;
189}
190
Darin Petkov8021e7f2012-05-21 12:15:00 +0200191bool WiMaxService::Is8021x() const {
192 return true;
193}
194
Ben Chanf4647d42013-05-02 22:01:01 -0700195bool WiMaxService::IsVisible() const {
196 // WiMAX services should be displayed only if they are in range (i.e.
197 // a corresponding network is exposed by WiMAX manager).
198 return IsStarted();
199}
200
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700201void WiMaxService::OnEapCredentialsChanged() {
202 need_passphrase_ = !eap()->IsConnectableUsingPassphrase();
Darin Petkov8021e7f2012-05-21 12:15:00 +0200203 UpdateConnectable();
204}
205
206void WiMaxService::UpdateConnectable() {
Ben Chane494e312013-08-23 12:56:05 -0700207 SLOG(WiMax, 2) << __func__ << "(started: " << IsStarted()
208 << ", need passphrase: " << need_passphrase_ << ")";
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700209 SetConnectableFull(IsStarted() && !need_passphrase_);
Darin Petkov8021e7f2012-05-21 12:15:00 +0200210}
211
Darin Petkov1e52a1b2012-05-21 10:35:56 +0200212void WiMaxService::OnSignalStrengthChanged(int strength) {
213 SLOG(WiMax, 2) << __func__ << "(" << strength << ")";
214 SetStrength(strength);
215}
216
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700217void WiMaxService::SetDevice(WiMaxRefPtr new_device) {
218 if (device_ == new_device)
219 return;
220 if (new_device) {
Ben Chan4276cca2013-09-20 10:07:04 -0700221 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700222 new_device->GetRpcIdentifier());
223 } else {
Ben Chan4276cca2013-09-20 10:07:04 -0700224 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700225 DBusAdaptor::kNullPath);
226 }
227 device_ = new_device;
228}
229
Darin Petkovd1cd7972012-05-22 15:26:15 +0200230bool WiMaxService::Save(StoreInterface *storage) {
231 SLOG(WiMax, 2) << __func__;
232 if (!Service::Save(storage)) {
233 return false;
234 }
235 const string id = GetStorageIdentifier();
236 storage->SetString(id, kStorageNetworkId, network_id_);
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700237
Darin Petkovd1cd7972012-05-22 15:26:15 +0200238 return true;
239}
240
Darin Petkovc1e52732012-05-25 15:23:45 +0200241bool WiMaxService::Unload() {
Ben Chane494e312013-08-23 12:56:05 -0700242 SLOG(WiMax, 2) << __func__;
Darin Petkovc1e52732012-05-25 15:23:45 +0200243 // The base method also disconnects the service.
244 Service::Unload();
Darin Petkova3f9f772012-05-31 12:11:28 +0200245 ClearPassphrase();
Darin Petkovc1e52732012-05-25 15:23:45 +0200246 // Notify the WiMAX provider that this service has been unloaded. If the
247 // provider releases ownership of this service, it needs to be deregistered.
248 return manager()->wimax_provider()->OnServiceUnloaded(this);
249}
250
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200251void WiMaxService::SetState(ConnectState state) {
252 Service::SetState(state);
253 if (!IsConnecting() && !IsConnected()) {
254 // Disassociate from any carrier device if it's not connected anymore.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700255 SetDevice(NULL);
Darin Petkov8ea0eaf2012-05-29 11:21:33 +0200256 }
257}
258
Darin Petkovd1cd7972012-05-22 15:26:15 +0200259// static
260WiMaxNetworkId WiMaxService::ConvertIdentifierToNetworkId(uint32 identifier) {
261 return base::StringPrintf("%08x", identifier);
262}
263
264void WiMaxService::InitStorageIdentifier() {
265 storage_id_ = CreateStorageIdentifier(network_id_, friendly_name());
266}
267
268// static
269string WiMaxService::CreateStorageIdentifier(const WiMaxNetworkId &id,
270 const string &name) {
271 string storage_id =
272 base::StringPrintf("%s_%s_%s",
Ben Chan4276cca2013-09-20 10:07:04 -0700273 kTypeWimax, name.c_str(), id.c_str());
Darin Petkovd1cd7972012-05-22 15:26:15 +0200274 StringToLowerASCII(&storage_id);
275 replace_if(storage_id.begin(), storage_id.end(), &Service::IllegalChar, '_');
276 return storage_id;
277}
278
Darin Petkova3f9f772012-05-31 12:11:28 +0200279void WiMaxService::ClearPassphrase() {
Ben Chane494e312013-08-23 12:56:05 -0700280 SLOG(WiMax, 2) << __func__;
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700281 mutable_eap()->set_password("");
282 OnEapCredentialsChanged();
Darin Petkova3f9f772012-05-31 12:11:28 +0200283}
284
Ben Chan99c8a4d2012-05-01 08:11:53 -0700285} // namespace shill