shill: wimax: Avoid creating duplicate WiMax devices.

BUG=chrome-os-partner:9735
TEST=unit tests

Change-Id: I97b24b0c2582a6018eeacaf1236d7e6d69b8ac2a
Reviewed-on: https://gerrit.chromium.org/gerrit/22917
Commit-Ready: Darin Petkov <petkov@chromium.org>
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
diff --git a/wimax_provider.cc b/wimax_provider.cc
index 86d5eb7..10ca5bb 100644
--- a/wimax_provider.cc
+++ b/wimax_provider.cc
@@ -106,6 +106,11 @@
 void WiMaxProvider::CreateDevice(const string &link_name,
                                  const RpcIdentifier &path) {
   SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")";
+  if (ContainsKey(devices_, link_name)) {
+    SLOG(WiMax, 2) << "Device already exists.";
+    CHECK_EQ(path, devices_[link_name]->path());
+    return;
+  }
   pending_devices_.erase(link_name);
   DeviceInfo *device_info = manager_->device_info();
   if (device_info->IsDeviceBlackListed(link_name)) {
@@ -123,14 +128,14 @@
   }
   ByteString address_bytes;
   if (!device_info->GetMACAddress(index, &address_bytes)) {
-    LOG(ERROR) << "Unable to create a WiMax device with not MAC address: "
+    LOG(ERROR) << "Unable to create a WiMax device with no MAC address: "
                << link_name;
     return;
   }
   string address = address_bytes.HexEncode();
   WiMaxRefPtr device(new WiMax(control_, dispatcher_, metrics_, manager_,
                                link_name, address, index, path));
-  devices_.push_back(device);
+  devices_[link_name] = device;
   device_info->RegisterDevice(device);
 }
 
@@ -146,13 +151,13 @@
       ++it;
     }
   }
-  for (vector<WiMaxRefPtr>::iterator it = devices_.begin();
+  for (map<string, WiMaxRefPtr>::iterator it = devices_.begin();
        it != devices_.end(); ) {
-    if (find(live_devices.begin(), live_devices.end(), (*it)->path()) ==
+    if (find(live_devices.begin(), live_devices.end(), it->second->path()) ==
         live_devices.end()) {
-      SLOG(WiMax, 2) << "Destroying device: " << (*it)->link_name();
-      manager_->device_info()->DeregisterDevice(*it);
-      it = devices_.erase(it);
+      SLOG(WiMax, 2) << "Destroying device: " << it->first;
+      manager_->device_info()->DeregisterDevice(it->second);
+      devices_.erase(it++);
     } else {
       ++it;
     }