⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adapter.c

📁 BlueZ源码
💻 C
📖 第 1 页 / 共 5 页
字号:
	device = l->data;	return device;}static void adapter_update_devices(struct btd_adapter *adapter){	char **devices;	int i;	GSList *l;	/* Devices */	devices = g_new0(char *, g_slist_length(adapter->devices) + 1);	for (i = 0, l = adapter->devices; l; l = l->next, i++) {		struct btd_device *dev = l->data;		devices[i] = (char *) device_get_path(dev);	}	emit_array_property_changed(connection, adapter->path,					ADAPTER_INTERFACE, "Devices",					DBUS_TYPE_OBJECT_PATH, &devices);	g_free(devices);}struct btd_device *adapter_create_device(DBusConnection *conn,				struct btd_adapter *adapter, const char *address){	struct btd_device *device;	const char *path;	debug("adapter_create_device(%s)", address);	device = device_create(conn, adapter, address);	if (!device)		return NULL;	device_set_temporary(device, TRUE);	adapter->devices = g_slist_append(adapter->devices, device);	path = device_get_path(device);	g_dbus_emit_signal(conn, adapter->path,			ADAPTER_INTERFACE, "DeviceCreated",			DBUS_TYPE_OBJECT_PATH, &path,			DBUS_TYPE_INVALID);	adapter_update_devices(adapter);	return device;}static DBusMessage *remove_bonding(DBusConnection *conn, DBusMessage *msg,					const char *address, void *data){	struct btd_adapter *adapter = data;	struct btd_device *device;	char filename[PATH_MAX + 1];	char *str, srcaddr[18];	bdaddr_t dst;	GSList *l;	int dev;	gboolean paired;	str2ba(address, &dst);	ba2str(&adapter->bdaddr, srcaddr);	dev = hci_open_dev(adapter->dev_id);	if (dev < 0 && msg)		return no_such_adapter(msg);	create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,			"linkkeys");	/* textfile_del doesn't return an error when the key is not found */	str = textfile_caseget(filename, address);	paired = str ? TRUE : FALSE;	g_free(str);	if (!paired && msg) {		hci_close_dev(dev);		return g_dbus_create_error(msg,				ERROR_INTERFACE ".DoesNotExist",				"Bonding does not exist");	}	/* Delete the link key from storage */	if (textfile_casedel(filename, address) < 0 && msg) {		int err = errno;		hci_close_dev(dev);		return failed_strerror(msg, err);	}	/* Delete the link key from the Bluetooth chip */	hci_delete_stored_link_key(dev, &dst, 0, HCI_REQ_TIMEOUT);	/* find the connection */	l = g_slist_find_custom(adapter->active_conn, &dst,				active_conn_find_by_bdaddr);	if (l) {		struct active_conn_info *con = l->data;		/* Send the HCI disconnect command */		if ((hci_disconnect(dev, htobs(con->handle),					HCI_OE_USER_ENDED_CONNECTION,					HCI_REQ_TIMEOUT) < 0)					&& msg){			int err = errno;			error("Disconnect failed");			hci_close_dev(dev);			return failed_strerror(msg, err);		}	}	hci_close_dev(dev);	device = adapter_find_device(adapter, address);	if (!device)		goto proceed;	if (paired) {		gboolean paired = FALSE;		const gchar *dev_path = device_get_path(device);		emit_property_changed(conn, dev_path, DEVICE_INTERFACE,					"Paired", DBUS_TYPE_BOOLEAN, &paired);	}proceed:	if(!msg)		goto done;	return dbus_message_new_method_return(msg);done:	return NULL;}void adapter_remove_device(DBusConnection *conn, struct btd_adapter *adapter,				struct btd_device *device){	bdaddr_t dst;	const gchar *dev_path = device_get_path(device);	struct agent *agent;	char dstaddr[18];	device_get_address(device, &dst);	ba2str(&dst, dstaddr);	delete_entry(&adapter->bdaddr, "profiles", dstaddr);	adapter->devices = g_slist_remove(adapter->devices, device);	if (!device_is_temporary(device))		remove_bonding(conn, NULL, dstaddr, adapter);	g_dbus_emit_signal(conn, adapter->path,			ADAPTER_INTERFACE, "DeviceRemoved",			DBUS_TYPE_OBJECT_PATH, &dev_path,			DBUS_TYPE_INVALID);	adapter_update_devices(adapter);	agent = device_get_agent(device);	if (agent) {		agent_destroy(agent, FALSE);		device_set_agent(device, NULL);	}	device_remove(conn, device);}struct btd_device *adapter_get_device(DBusConnection *conn,				struct btd_adapter *adapter, const gchar *address){	struct btd_device *device;	debug("adapter_get_device(%s)", address);	if (!adapter)		return NULL;	device = adapter_find_device(adapter, address);	if (device)		return device;	return adapter_create_device(conn, adapter, address);}void remove_pending_device(struct btd_adapter *adapter){	struct btd_device *device;	char address[18];	ba2str(&adapter->bonding->bdaddr, address);	device = adapter_find_device(adapter, address);	if (!device)		return;	if (device_is_temporary(device))		adapter_remove_device(adapter->bonding->conn, adapter, device);}static gboolean create_bonding_conn_complete(GIOChannel *io, GIOCondition cond,						struct btd_adapter *adapter){	struct hci_request rq;	auth_requested_cp cp;	evt_cmd_status rp;	struct l2cap_conninfo cinfo;	socklen_t len;	int sk, dd, ret;	if (!adapter->bonding) {		/* If we come here it implies a bug somewhere */		debug("create_bonding_conn_complete: no pending bonding!");		g_io_channel_close(io);		return FALSE;	}	if (cond & G_IO_NVAL) {		DBusMessage *reply;		reply = new_authentication_return(adapter->bonding->msg, 0x09);		g_dbus_send_message(adapter->bonding->conn, reply);		goto cleanup;	}	if (cond & (G_IO_HUP | G_IO_ERR)) {		debug("Hangup or error on bonding IO channel");		if (!adapter->bonding->auth_active)			error_connection_attempt_failed(adapter->bonding->conn,							adapter->bonding->msg,							ENETDOWN);		else			reply_authentication_failure(adapter->bonding);		goto failed;	}	sk = g_io_channel_unix_get_fd(io);	len = sizeof(ret);	if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &ret, &len) < 0) {		error("Can't get socket error: %s (%d)",				strerror(errno), errno);		error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,				errno);		goto failed;	}	if (ret != 0) {		if (adapter->bonding->auth_active)			reply_authentication_failure(adapter->bonding);		else			error_connection_attempt_failed(adapter->bonding->conn,							adapter->bonding->msg,							ret);		goto failed;	}	len = sizeof(cinfo);	if (getsockopt(sk, SOL_L2CAP, L2CAP_CONNINFO, &cinfo, &len) < 0) {		error("Can't get connection info: %s (%d)",				strerror(errno), errno);		error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,				errno);		goto failed;	}	dd = hci_open_dev(adapter->dev_id);	if (dd < 0) {		DBusMessage *reply = no_such_adapter(adapter->bonding->msg);		g_dbus_send_message(adapter->bonding->conn, reply);		goto failed;	}	memset(&rp, 0, sizeof(rp));	memset(&cp, 0, sizeof(cp));	cp.handle = htobs(cinfo.hci_handle);	memset(&rq, 0, sizeof(rq));	rq.ogf    = OGF_LINK_CTL;	rq.ocf    = OCF_AUTH_REQUESTED;	rq.cparam = &cp;	rq.clen   = AUTH_REQUESTED_CP_SIZE;	rq.rparam = &rp;	rq.rlen   = EVT_CMD_STATUS_SIZE;	rq.event  = EVT_CMD_STATUS;	if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {		error("Unable to send HCI request: %s (%d)",					strerror(errno), errno);		error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,				errno);		hci_close_dev(dd);		goto failed;	}	if (rp.status) {		error("HCI_Authentication_Requested failed with status 0x%02x",				rp.status);		error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,				bt_error(rp.status));		hci_close_dev(dd);		goto failed;	}	hci_close_dev(dd);	adapter->bonding->auth_active = 1;	adapter->bonding->io_id = g_io_add_watch(io,						G_IO_NVAL | G_IO_HUP | G_IO_ERR,						(GIOFunc) create_bonding_conn_complete,						adapter);	return FALSE;failed:	g_io_channel_close(io);	remove_pending_device(adapter);cleanup:	g_dbus_remove_watch(adapter->bonding->conn,				adapter->bonding->listener_id);	bonding_request_free(adapter->bonding);	adapter->bonding = NULL;	return FALSE;}static void cancel_auth_request(struct pending_auth_info *auth, int dev_id){	int dd;	if (auth->replied)		return;	dd = hci_open_dev(dev_id);	if (dd < 0) {		error("hci_open_dev: %s (%d)", strerror(errno), errno);		return;	}	switch (auth->type) {	case AUTH_TYPE_PINCODE:		hci_send_cmd(dd, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY,				6, &auth->bdaddr);		break;	case AUTH_TYPE_CONFIRM:		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,				6, &auth->bdaddr);		break;	case AUTH_TYPE_PASSKEY:		hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_NEG_REPLY,				6, &auth->bdaddr);		break;	case AUTH_TYPE_NOTIFY:		/* User Notify doesn't require any reply */		break;	}	auth->replied = TRUE;	hci_close_dev(dd);}static void cancel_bonding(struct btd_adapter *adapter, gboolean exited){	struct pending_auth_info *auth;	struct bonding_request_info *bonding = adapter->bonding;	auth = adapter_find_auth_request(adapter, &adapter->bdaddr);	if (auth) {		cancel_auth_request(auth, adapter->dev_id);		if (auth->agent)			agent_cancel(auth->agent);		adapter_remove_auth_request(adapter, &bonding->bdaddr);	}	remove_pending_device(adapter);	if (bonding->io)		g_io_channel_close(bonding->io);	if (exited) {		if (bonding->io_id) {			g_source_remove(bonding->io_id);			bonding->io_id = 0;		}		bonding_request_free(bonding);		adapter->bonding = NULL;	} else		bonding->cancel = TRUE;}static void create_bond_req_exit(DBusConnection *conn, void *user_data){	struct btd_adapter *adapter = user_data;	debug("CreateConnection requestor exited before bonding was completed");	cancel_bonding(adapter, TRUE);}static DBusMessage *create_bonding(DBusConnection *conn, DBusMessage *msg,				const char *address, const char *agent_path,				uint8_t capability, void *data){	char filename[PATH_MAX + 1];	char *str, srcaddr[18];	struct btd_adapter *adapter = data;	struct bonding_request_info *bonding;	bdaddr_t dst;	int sk;	str2ba(address, &dst);	ba2str(&adapter->bdaddr, srcaddr);	/* check if there is a pending discover: requested by D-Bus/non clients */	if (adapter->state & STD_INQUIRY)		return in_progress(msg, "Discover in progress");	pending_remote_name_cancel(adapter);	if (adapter->bonding)		return in_progress(msg, "Bonding in progress");	if (adapter_find_auth_request(adapter, &dst))		return in_progress(msg, "Bonding in progress");	/* check if a link key already exists */	create_name(filename, PATH_MAX, STORAGEDIR, srcaddr,			"linkkeys");	str = textfile_caseget(filename, address);	if (str) {		free(str);		return g_dbus_create_error(msg,				ERROR_INTERFACE ".AlreadyExists",				"Bonding already exists");	}	sk = l2raw_connect(&adapter->bdaddr, &dst);	if (sk < 0)		return g_dbus_create_error(msg,				ERROR_INTERFACE ".ConnectionAttemptFailed",				"Connection attempt failed");	bonding = bonding_request_new(conn, msg, adapter, address, agent_path,					capability);	if (!bonding) {		close(sk);		return NULL;	}	bonding->io = g_io_channel_unix_new(sk);	bonding->io_id = g_io_add_watch(bonding->io,					G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR,					(GIOFunc) create_bonding_conn_complete,					adapter);	bonding->listener_id = g_dbus_add_disconnect_watch(conn,						dbus_message_get_sender(msg),						create_bond_req_exit, adapter,						NULL);	adapter->bonding = bonding;	return NULL;}int start_inquiry(struct btd_adapter *adapter){	inquiry_cp cp;	evt_cmd_status rp;	struct hci_request rq;	uint8_t lap[3] = { 0x33, 0x8b, 0x9e };	int dd, err;	pending_remote_name_cancel(adapter);	dd = hci_open_dev(adapter->dev_id);	if (dd < 0)		return dd;	memset(&cp, 0, sizeof(cp));	memcpy(&cp.lap, lap, 3);	cp.length = 0x08;	cp.num_rsp = 0x00;	memset(&rq, 0, sizeof(rq));	rq.ogf = OGF_LINK_CTL;	rq.ocf = OCF_INQUIRY;	rq.cparam = &cp;	rq.clen = INQUIRY_CP_SIZE;	rq.rparam = &rp;	rq.rlen = EVT_CMD_STATUS_SIZE;	rq.event = EVT_CMD_STATUS;	if (hci_send_req(dd, &rq, HCI_REQ_TIMEOUT) < 0) {		err = -errno;		error("Unable to start inquiry: %s (%d)",			strerror(err), err);		hci_close_dev(dd);		return err;	}	if (rp.status) {		error("HCI_Inquiry command failed with status 0x%02x",			rp.status);		hci_close_dev(dd);		return -bt_error(rp.status);	}	hci_close_dev(dd);	adapter->state |= RESOLVE_NAME;	return 0;}static int start_periodic_inquiry(struct btd_adapter *adapter){	periodic_inquiry_cp cp;	struct hci_request rq;	uint8_t lap[3] = { 0x33, 0x8b, 0x9e };	uint8_t status;	int dd, err;	dd = hci_open_dev(adapter->dev_id);	if (dd < 0)		return dd;	memset(&cp, 0, sizeof(cp));	memcpy(&cp.lap, lap, 3);	cp.max_period = htobs(24);	cp.min_period = htobs(16);	cp.length  = 0x08;	cp.num_rsp = 0x00;	memset(&rq, 0, sizeof(rq));	rq.ogf    = OGF_LINK_CTL;	rq.ocf    = OCF_PERIODIC_INQUIRY;	rq.cparam = &cp;	rq.clen   = PERIODIC_INQUIRY_CP_SIZE;	rq.rparam = &status;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -