static int adapter_register(struct btd_adapter *adapter)
{
	struct agent *agent;
	struct gatt_db *db;

	if (powering_down)
		return -EBUSY;

	adapter->path = g_strdup_printf("/org/bluez/hci%d", adapter->dev_id);

	if (!g_dbus_register_interface(dbus_conn,
					adapter->path, ADAPTER_INTERFACE,
					adapter_methods, NULL,
					adapter_properties, adapter,
					adapter_free)) {
		btd_error(adapter->dev_id,
				"Adapter interface init failed on path %s",
							adapter->path);
		g_free(adapter->path);
		adapter->path = NULL;
		return -EINVAL;
	}

	if (adapters == NULL)
		adapter->is_default = true;

	adapters = g_slist_append(adapters, adapter);

	agent = agent_get(NULL);
	if (agent) {
		uint8_t io_cap = agent_get_io_capability(agent);
		adapter_set_io_capability(adapter, io_cap);
		agent_unref(agent);
	}

	/* Don't start GATT database and advertising managers on
	 * non-LE controllers.
	 */
	if (!(adapter->supported_settings & MGMT_SETTING_LE) ||
					btd_opts.mode == BT_MODE_BREDR)
		goto load;

	adapter->database = btd_gatt_database_new(adapter);
	if (!adapter->database) {
		btd_error(adapter->dev_id,
				"Failed to create GATT database for adapter");
		adapters = g_slist_remove(adapters, adapter);
		return -EINVAL;
	}

	adapter->adv_manager = btd_adv_manager_new(adapter, adapter->mgmt);

	if (g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) {
		if (adapter->supported_settings & MGMT_SETTING_LE) {
			adapter->adv_monitor_manager =
				btd_adv_monitor_manager_create(adapter,
								adapter->mgmt);
			if (!adapter->adv_monitor_manager) {
				btd_error(adapter->dev_id,
						"Failed to create Adv Monitor "
						"Manager for adapter");
				return -EINVAL;
			}
		} else {
			btd_info(adapter->dev_id, "Adv Monitor Manager "
					"skipped, LE unavailable");
		}
	}

	if (g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) {
		adapter->battery_provider_manager =
			btd_battery_provider_manager_create(adapter);
	}

	db = btd_gatt_database_get_db(adapter->database);
	adapter->db_id = gatt_db_register(db, services_modified,
							services_modified,
							adapter, NULL);

load:
	mgmt_register(adapter->mgmt, MGMT_EV_DEVICE_FLAGS_CHANGED,
						adapter->dev_id,
						device_flags_changed_callback,
						adapter, NULL);

	load_config(adapter);
	fix_storage(adapter);
	load_drivers(adapter);
	btd_profile_foreach(probe_profile, adapter);
	clear_blocked(adapter);
	load_defaults(adapter);
	load_devices(adapter);

	/* restore Service Changed CCC value for bonded devices */
	btd_gatt_database_restore_svc_chng_ccc(adapter->database);

	/* retrieve the active connections: address the scenario where
	 * the are active connections before the daemon've started */
	if (btd_adapter_get_powered(adapter))
		load_connections(adapter);

	adapter->initialized = TRUE;

	if (btd_opts.did_source) {
		/* DeviceID record is added by sdpd-server before any other
		 * record is registered. */
		adapter_service_insert(adapter, sdp_record_find(0x10000));
		set_did(adapter, btd_opts.did_vendor, btd_opts.did_product,
				btd_opts.did_version, btd_opts.did_source);
	}

	DBG("Adapter %s registered", adapter->path);

	return 0;
}
static int adapter_register(struct btd_adapter *adapter)
{
	struct agent *agent;
	struct gatt_db *db;

	if (powering_down)
		return -EBUSY;

	adapter->path = g_strdup_printf("/org/bluez/hci%d", adapter->dev_id);

	if (!g_dbus_register_interface(dbus_conn,
					adapter->path, ADAPTER_INTERFACE,
					adapter_methods, NULL,
					adapter_properties, adapter,
					adapter_free)) {
		btd_error(adapter->dev_id,
				"Adapter interface init failed on path %s",
							adapter->path);
		g_free(adapter->path);
		adapter->path = NULL;
		return -EINVAL;
	}

	if (adapters == NULL)
		adapter->is_default = true;

	adapters = g_slist_append(adapters, adapter);

	agent = agent_get(NULL);
	if (agent) {
		uint8_t io_cap = agent_get_io_capability(agent);
		adapter_set_io_capability(adapter, io_cap);
		agent_unref(agent);
	}

	 
	if (!(adapter->supported_settings & MGMT_SETTING_LE) ||
					btd_opts.mode == BT_MODE_BREDR)
		goto load;

	adapter->database = btd_gatt_database_new(adapter);
	if (!adapter->database) {
		btd_error(adapter->dev_id,
				"Failed to create GATT database for adapter");
		adapters = g_slist_remove(adapters, adapter);
		return -EINVAL;
	}

	adapter->adv_manager = btd_adv_manager_new(adapter, adapter->mgmt);

	if (g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) {
		if (adapter->supported_settings & MGMT_SETTING_LE) {
			adapter->adv_monitor_manager =
				btd_adv_monitor_manager_create(adapter,
								adapter->mgmt);
			if (!adapter->adv_monitor_manager) {
				btd_error(adapter->dev_id,
						"Failed to create Adv Monitor "
						"Manager for adapter");
				return -EINVAL;
			}
		} else {
			btd_info(adapter->dev_id, "Adv Monitor Manager "
					"skipped, LE unavailable");
		}
	}

	if (g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) {
		adapter->battery_provider_manager =
			btd_battery_provider_manager_create(adapter);
	}

	db = btd_gatt_database_get_db(adapter->database);
	adapter->db_id = gatt_db_register(db, services_modified,
							services_modified,
							adapter, NULL);

load:
	mgmt_register(adapter->mgmt, MGMT_EV_DEVICE_FLAGS_CHANGED,
						adapter->dev_id,
						device_flags_changed_callback,
						adapter, NULL);

	load_config(adapter);
	fix_storage(adapter);
	load_drivers(adapter);
	btd_profile_foreach(probe_profile, adapter);
	clear_blocked(adapter);
	load_defaults(adapter);
	load_devices(adapter);

	 
	btd_gatt_database_restore_svc_chng_ccc(adapter->database);

	 
	if (btd_adapter_get_powered(adapter))
		load_connections(adapter);

	adapter->initialized = TRUE;

	if (btd_opts.did_source) {
		 
		adapter_service_insert(adapter, sdp_record_find(0x10000));
		set_did(adapter, btd_opts.did_vendor, btd_opts.did_product,
				btd_opts.did_version, btd_opts.did_source);
	}

	DBG("Adapter %s registered", adapter->path);

	return 0;
}