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

📄 dbus-test.c

📁 Linux的蓝牙操作工具。配合bluez-lib使用
💻 C
📖 第 1 页 / 共 2 页
字号:
	}	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);		goto failed;	}	if (ret != 0) {		error("l2raw_connect failed: %s (%d)", strerror(ret), ret);		goto failed;	}	debug("AuditRemoteDevice: connected");	/* Send L2CAP info request */	memset(buf, 0, sizeof(buf));	cmd->code  = L2CAP_INFO_REQ;	cmd->ident = 42;	cmd->len   = htobs(2);	req->type  = htobs(0x0001);	if (send(sk, buf, L2CAP_CMD_HDR_SIZE + L2CAP_INFO_REQ_SIZE, 0) < 0) {		error("Can't send info request: %s (%d)", strerror(errno), errno);		goto failed;	}	audit->timeout = g_timeout_add(L2INFO_TIMEOUT, (GSourceFunc)			l2raw_input_timer, audit);	audit->io_id = g_io_add_watch(audit->io,					G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,					(GIOFunc) l2raw_data_callback, audit);	return FALSE;failed:	send_audit_status(audit, "AuditRemoteDeviceFailed");	g_io_channel_close(io);	g_io_channel_unref(io);	audits = g_slist_remove(audits, audit);	name_listener_remove(audit->conn, audit->requestor,				(name_cb_t) audit_requestor_exited, audit);	audit_free(audit);	return FALSE;}static DBusHandlerResult audit_remote_device(DBusConnection *conn,						DBusMessage *msg, void *data){	DBusMessage *reply;	DBusError err;	bdaddr_t peer, local;	const char *address;	struct audit *audit;	struct adapter *adapter = data;	gboolean queue;	dbus_error_init(&err);	dbus_message_get_args(msg, &err,				DBUS_TYPE_STRING, &address,				DBUS_TYPE_INVALID);	if (dbus_error_is_set(&err)) {		error("Can't extract message arguments:%s", err.message);		dbus_error_free(&err);		return error_invalid_arguments(conn, msg, NULL);	}	if (check_address(address) < 0)		return error_invalid_arguments(conn, msg, NULL);	str2ba(address, &peer);	str2ba(adapter->address, &local);	pending_remote_name_cancel(adapter);	if (adapter->bonding)		return error_bonding_in_progress(conn, msg);	if (g_slist_find_custom(adapter->pin_reqs, &peer, pin_req_cmp))		return error_bonding_in_progress(conn, msg);	if (!read_l2cap_info(&local, &peer, NULL, NULL, NULL, NULL))		return error_audit_already_exists(conn, msg);	reply = dbus_message_new_method_return(msg);	if (!reply)		return DBUS_HANDLER_RESULT_NEED_MEMORY;	/* Just return if an audit for the same device is already queued */	if (g_slist_find_custom(audits, &peer, audit_addr_cmp))		return send_message_and_unref(conn, reply);	if (adapter->discov_active || (adapter->pdiscov_active && !adapter->pinq_idle))		queue = TRUE;	else		queue = audit_in_progress();	audit = audit_new(conn, msg, &peer, &local);	if (!audit) {		dbus_message_unref(reply);		return DBUS_HANDLER_RESULT_NEED_MEMORY;	}	if (!queue) {		int sk;		sk = l2raw_connect(adapter->address, &peer);		if (sk < 0) {			audit_free(audit);			dbus_message_unref(reply);			return error_connection_attempt_failed(conn, msg, 0);		}		bacpy(&adapter->agents_disabled, &peer);		audit->io = g_io_channel_unix_new(sk);		audit->io_id = g_io_add_watch(audit->io,						G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR,						(GIOFunc) l2raw_connect_complete, audit);	}	name_listener_add(conn, dbus_message_get_sender(msg),				(name_cb_t) audit_requestor_exited, audit);	audits = g_slist_append(audits, audit);	return send_message_and_unref(conn, reply);}static DBusHandlerResult cancel_audit_remote_device(DBusConnection *conn,						DBusMessage *msg, void *data){	struct adapter *adapter = data;	DBusMessage *reply;	DBusError err;	const char *address;	bdaddr_t peer, local;	GSList *l;	struct audit *audit;	dbus_error_init(&err);	dbus_message_get_args(msg, &err,				DBUS_TYPE_STRING, &address,				DBUS_TYPE_INVALID);	if (dbus_error_is_set(&err)) {		error("Can't extract message arguments:%s", err.message);		dbus_error_free(&err);		return error_invalid_arguments(conn, msg, NULL);	}	if (check_address(address) < 0)		return error_invalid_arguments(conn, msg, NULL);	str2ba(address, &peer);	str2ba(adapter->address, &local);	l = g_slist_find_custom(audits, &peer, audit_addr_cmp);	if (!l)		return error_not_in_progress(conn, msg, "Audit not in progress");	audit = l->data;	/* Check that the audit wasn't for another adapter */	if (bacmp(&audit->local, &local))		return error_not_in_progress(conn, msg, "Audit not in progress");	if (strcmp(audit->requestor, dbus_message_get_sender(msg)))		return error_not_authorized(conn, msg);	if (audit->io) {		send_audit_status(audit, "AuditRemoteDeviceComplete");		bacpy(&adapter->agents_disabled, BDADDR_ANY);		g_io_channel_close(audit->io);	}	if (audit->timeout)		g_source_remove(audit->timeout);	audits = g_slist_remove(audits, audit);	name_listener_remove(audit->conn, audit->requestor,				(name_cb_t) audit_requestor_exited, audit);	audit_free(audit);	reply = dbus_message_new_method_return(msg);	if (!reply)		return DBUS_HANDLER_RESULT_NEED_MEMORY;	return send_message_and_unref(conn, reply);}static DBusHandlerResult get_l2cap_feature_mask(DBusConnection *conn,						DBusMessage *msg, void *data){	struct adapter *adapter = data;	DBusMessage *reply;	DBusError err;	const char *address;	bdaddr_t peer, local;	uint32_t mask;	uint16_t result;	dbus_error_init(&err);	dbus_message_get_args(msg, &err,				DBUS_TYPE_STRING, &address,				DBUS_TYPE_INVALID);	if (dbus_error_is_set(&err)) {		error("Can't extract message arguments:%s", err.message);		dbus_error_free(&err);		return error_invalid_arguments(conn, msg, NULL);	}	if (check_address(address) < 0)		return error_invalid_arguments(conn, msg, NULL);	str2ba(address, &peer);	str2ba(adapter->address, &local);	if (read_l2cap_info(&local, &peer, NULL, NULL, &result, &mask) < 0)		return error_not_available(conn, msg);	if (result)		return error_not_supported(conn, msg);	reply = dbus_message_new_method_return(msg);	if (!reply)		return DBUS_HANDLER_RESULT_NEED_MEMORY;	dbus_message_append_args(reply, DBUS_TYPE_UINT32, &mask,					DBUS_TYPE_INVALID);	return send_message_and_unref(conn, reply);}static DBusHandlerResult get_l2cap_mtu_size(DBusConnection *conn,						DBusMessage *msg, void *data){	struct adapter *adapter = data;	DBusMessage *reply;	DBusError err;	const char *address;	bdaddr_t peer, local;	uint16_t result, mtu;	dbus_error_init(&err);	dbus_message_get_args(msg, &err,				DBUS_TYPE_STRING, &address,				DBUS_TYPE_INVALID);	if (dbus_error_is_set(&err)) {		error("Can't extract message arguments:%s", err.message);		dbus_error_free(&err);		return error_invalid_arguments(conn, msg, NULL);	}	if (check_address(address) < 0)		return error_invalid_arguments(conn, msg, NULL);	str2ba(address, &peer);	str2ba(adapter->address, &local);	if (read_l2cap_info(&local, &peer, &result, &mtu, NULL, NULL) < 0)		return error_not_available(conn, msg);	if (result)		return error_not_supported(conn, msg);	reply = dbus_message_new_method_return(msg);	if (!reply)		return DBUS_HANDLER_RESULT_NEED_MEMORY;	dbus_message_append_args(reply, DBUS_TYPE_UINT16, &mtu,					DBUS_TYPE_INVALID);	return send_message_and_unref(conn, reply);}static DBusMethodVTable test_methods[] = {	{ "AuditRemoteDevice",		audit_remote_device,		"s",	""	},	{ "CancelAuditRemoteDevice",	cancel_audit_remote_device,		"s",	""	},	{ "GetL2capFeatureMask",	get_l2cap_feature_mask,		"s",	"u"	},	{ "GetL2capMtuSize",		get_l2cap_mtu_size,		"s",	"q"	},	{ NULL, NULL, NULL, NULL }};dbus_bool_t test_init(DBusConnection *conn, const char *path){	if (!hcid_dbus_use_experimental())		return TRUE;	return dbus_connection_register_interface(conn, path, TEST_INTERFACE,							test_methods,							NULL, NULL);}void process_audits_list(const char *adapter_path){	GSList *l, *next;	for (l = audits; l != NULL; l = next) {		struct adapter *adapter;		struct audit *audit;		int sk;		audit = l->data;		next = l->next;		if (strcmp(adapter_path, audit->adapter_path))			continue;		if (audit->io)			return;		adapter = NULL;		dbus_connection_get_object_user_data(audit->conn,							audit->adapter_path,							(void *) &adapter);		if (!adapter) {			audits = g_slist_remove(audits, audit);			name_listener_remove(audit->conn, audit->requestor,					(name_cb_t) audit_requestor_exited, audit);			audit_free(audit);			continue;		}		if (adapter->discov_active || (adapter->pdiscov_active					&& !adapter->pinq_idle))			continue;		sk = l2raw_connect(adapter->address, &audit->peer);		if (sk < 0) {			send_audit_status(audit, "AuditRemoteDeviceFailed");			audits = g_slist_remove(audits, audit);			name_listener_remove(audit->conn, audit->requestor,					(name_cb_t) audit_requestor_exited, audit);			audit_free(audit);			continue;		}		bacpy(&adapter->agents_disabled, &audit->peer);		audit->io = g_io_channel_unix_new(sk);		audit->io_id = g_io_add_watch(audit->io,						G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR,						(GIOFunc) l2raw_connect_complete, audit);		return;	}}

⌨️ 快捷键说明

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