Fix device name setting for mode=off when bluetoothd starts

This patch fixes the way that the device name gets set when bluetoothd
starts and the previously stored mode is off.
diff --git a/plugins/hciops.c b/plugins/hciops.c
index ebcaa94..2b43737 100644
--- a/plugins/hciops.c
+++ b/plugins/hciops.c
@@ -103,18 +103,6 @@
 		return;
 	}
 
-	/* Set device name */
-	if ((main_opts.flags & (1 << HCID_SET_NAME)) && main_opts.name) {
-		change_local_name_cp cp;
-
-		memset(cp.name, 0, sizeof(cp.name));
-		expand_name((char *) cp.name, sizeof(cp.name),
-						main_opts.name, index);
-
-		hci_send_cmd(dd, OGF_HOST_CTL, OCF_CHANGE_LOCAL_NAME,
-					CHANGE_LOCAL_NAME_CP_SIZE, &cp);
-	}
-
 	/* Set device class */
 	if ((main_opts.flags & (1 << HCID_SET_CLASS))) {
 		write_class_of_dev_cp cp;
diff --git a/src/adapter.c b/src/adapter.c
index 49ef35d..15fc613 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1748,7 +1748,7 @@
 	return 0;
 }
 
-static int adapter_setup(struct btd_adapter *adapter)
+static int adapter_setup(struct btd_adapter *adapter, const char *mode)
 {
 	struct hci_dev *dev = &adapter->dev;
 	uint8_t events[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00 };
@@ -1797,9 +1797,6 @@
 						sizeof(events), events);
 	}
 
-	if (read_local_name(&adapter->bdaddr, name) == 0)
-		adapter_ops->set_name(adapter->dev_id, name);
-
 	inqmode = get_inquiry_mode(dev);
 	if (inqmode < 1)
 		goto done;
@@ -1812,6 +1809,14 @@
 		return err;
 	}
 
+	if (read_local_name(&adapter->bdaddr, name) < 0)
+		expand_name(name, MAX_NAME_LENGTH, main_opts.name,
+							adapter->dev_id);
+
+	adapter_ops->set_name(adapter->dev_id, name);
+	if (g_str_equal(mode, "off"))
+		strncpy((char *) adapter->dev.name, name, MAX_NAME_LENGTH);
+
 done:
 	hci_close_dev(dd);
 	return 0;
@@ -1965,9 +1970,9 @@
 	return main_opts.pairto;
 }
 
-static int adapter_up(struct btd_adapter *adapter)
+static int adapter_up(struct btd_adapter *adapter, const char *mode)
 {
-	char mode[14], srcaddr[18];
+	char srcaddr[18];
 	uint8_t scan_mode;
 	gboolean powered, dev_down = FALSE;
 	int err;
@@ -1988,19 +1993,9 @@
 	if (read_device_pairable(&adapter->bdaddr, &adapter->pairable) < 0)
 		adapter->pairable = TRUE;
 
-	if (!adapter->initialized && !main_opts.remember_powered) {
-		if (main_opts.mode == MODE_OFF)
-			strcpy(mode, "off");
-		else
-			strcpy(mode, "connectable");
-	} else if (read_device_mode(srcaddr, mode, sizeof(mode)) < 0) {
-		if (!adapter->initialized && main_opts.mode == MODE_OFF)
-			strcpy(mode, "off");
-		else
-			goto proceed;
-	}
-
 	if (g_str_equal(mode, "off")) {
+		char onmode[14];
+
 		powered = FALSE;
 
 		if (!adapter->initialized) {
@@ -2008,13 +2003,13 @@
 			goto proceed;
 		}
 
-		if (read_on_mode(srcaddr, mode, sizeof(mode)) < 0 ||
-						g_str_equal(mode, "off"))
-			write_device_mode(&adapter->bdaddr, "connectable");
-		else
-			write_device_mode(&adapter->bdaddr, mode);
+		if (read_on_mode(srcaddr, onmode, sizeof(onmode)) < 0 ||
+						g_str_equal(onmode, "off"))
+			strcpy(onmode, "connectable");
 
-		return adapter_up(adapter);
+		write_device_mode(&adapter->bdaddr, onmode);
+
+		return adapter_up(adapter, onmode);
 	} else if (!g_str_equal(mode, "connectable") &&
 			adapter->discov_timeout == 0) {
 		/* Set discoverable only if timeout is 0 */
@@ -2065,6 +2060,7 @@
 	struct hci_version ver;
 	uint8_t features[8];
 	int dd, err;
+	char mode[14], address[18];
 
 	if (hci_devinfo(adapter->dev_id, &di) < 0)
 		return -errno;
@@ -2086,6 +2082,15 @@
 
 	bacpy(&adapter->bdaddr, &di.bdaddr);
 	memcpy(dev->features, di.features, 8);
+	ba2str(&adapter->bdaddr, address);
+
+	if (!main_opts.remember_powered ||
+			read_device_mode(address, mode, sizeof(mode)) < 0) {
+		if (!adapter->initialized && main_opts.mode == MODE_OFF)
+			strcpy(mode, "off");
+		else
+			strcpy(mode, "connectable");
+	}
 
 	dd = hci_open_dev(adapter->dev_id);
 	if (dd < 0) {
@@ -2148,14 +2153,14 @@
 								0, NULL);
 	hci_close_dev(dd);
 
-	adapter_setup(adapter);
+	adapter_setup(adapter, mode);
 
 	if (!adapter->initialized && adapter->already_up) {
 		debug("Stopping Inquiry at adapter startup");
 		adapter_ops->stop_discovery(adapter->dev_id);
 	}
 
-	err = adapter_up(adapter);
+	err = adapter_up(adapter, mode);
 
 	info("Adapter %s has been enabled", adapter->path);