shill: wimax: Listen to WiMaxManager.Device.StatusChanged signal.

Use the status signal to update the connection state of the WiMax
service appropriately. Also, make sure that WiMaxService is
disassociated from the carrier device if a connection can't be
initiated or if it's dropped.

BUG=chrome-os-partner:10010
TEST=unit tests
CQ-DEPEND=I29dcfe4915e6f2559d022c60353aa12358ef5966
CQ-DEPEND=I223eaf61894f74905c591e38590e5e0620d07be0

Change-Id: I5fe48f0cc84c066eb6a63dc5d347ac5f265b86b1
Reviewed-on: https://gerrit.chromium.org/gerrit/23879
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Ready: Ben Chan <benchan@chromium.org>
diff --git a/wimax_service.cc b/wimax_service.cc
index 3bb7096..06463f3 100644
--- a/wimax_service.cc
+++ b/wimax_service.cc
@@ -135,14 +135,19 @@
         error, Error::kAlreadyConnected, "Already connected.");
     return;
   }
-  device_ = manager()->wimax_provider()->SelectCarrier(this);
-  if (!device_) {
+  WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this);
+  if (!carrier) {
     Error::PopulateAndLog(
         error, Error::kNoCarrier, "No suitable WiMAX device available.");
     return;
   }
   Service::Connect(error);
-  device_->ConnectTo(this, error);
+  carrier->ConnectTo(this, error);
+  if (error->IsSuccess()) {
+    // Associate with the carrier device if the connection process has been
+    // initiated successfully.
+    device_ = carrier;
+  }
 }
 
 void WiMaxService::Disconnect(Error *error) {
@@ -212,6 +217,14 @@
   return manager()->wimax_provider()->OnServiceUnloaded(this);
 }
 
+void WiMaxService::SetState(ConnectState state) {
+  Service::SetState(state);
+  if (!IsConnecting() && !IsConnected()) {
+    // Disassociate from any carrier device if it's not connected anymore.
+    device_ = NULL;
+  }
+}
+
 // static
 WiMaxNetworkId WiMaxService::ConvertIdentifierToNetworkId(uint32 identifier) {
   return base::StringPrintf("%08x", identifier);