Bluetooth: Store device name

This uses bt_dev to store the name and allow changing it at runtime, in
addtion to that if CONFIG_BT_SETTINGS is defined make the name
persistent.

Fixes #8357

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
diff --git a/subsys/bluetooth/host/settings.c b/subsys/bluetooth/host/settings.c
index 225de6c..815c381 100644
--- a/subsys/bluetooth/host/settings.c
+++ b/subsys/bluetooth/host/settings.c
@@ -121,6 +121,19 @@
 		return 0;
 	}
 
+	if (!strcmp(argv[0], "name")) {
+		if (strlen(val) >= sizeof(bt_dev.name)) {
+			BT_ERR("Invalid length for device name in storage: name"
+			       " will be truncated");
+		}
+
+		strncpy(bt_dev.name, val, sizeof(bt_dev.name) - 1);
+		bt_dev.name[sizeof(bt_dev.name) - 1] = '\0';
+
+		BT_DBG("Name set to %s", bt_dev.name);
+		return 0;
+	}
+
 #if defined(CONFIG_BT_PRIVACY)
 	if (!strcmp(argv[0], "irk")) {
 		len = sizeof(bt_dev.irk);
@@ -203,6 +216,12 @@
 		generate_static_addr();
 	}
 
+#if CONFIG_BT_DEVICE_NAME_MAX > 0
+	if (bt_dev.name[0] == '\0') {
+		bt_set_name(CONFIG_BT_DEVICE_NAME);
+	}
+#endif
+
 #if defined(CONFIG_BT_PRIVACY)
 	{
 		u8_t zero[16] = { 0 };