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

📄 dbus.c

📁 bluetooth 开发应用bluez-utils-2.23
💻 C
📖 第 1 页 / 共 5 页
字号:
		syslog(LOG_ERR, "Couldn't allocate D-BUS message");		goto failed;	}	req = malloc(sizeof(*req));	req->dev = dev;	bacpy(&req->bda, &ci->bdaddr);	dbus_message_append_args(message, DBUS_TYPE_BOOLEAN, &out,			DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,			&addr, sizeof(bdaddr_t), DBUS_TYPE_INVALID);	if (dbus_connection_send_with_reply(connection, message,						&pending, TIMEOUT) == FALSE) {		syslog(LOG_ERR, "D-BUS send failed");		goto failed;	}	dbus_pending_call_set_notify(pending, reply_handler_function,							req, free_pin_req);	dbus_connection_flush(connection);	dbus_message_unref(message);	return;failed:	dbus_message_unref(message);	hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY, 6, &ci->bdaddr);}void hcid_dbus_inquiry_start(bdaddr_t *local){	DBusMessage *message = NULL;	char path[MAX_PATH_LENGTH];	char *local_addr;	bdaddr_t tmp;	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_INQ_START);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS inquiry start message");		goto failed;	}	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS inquiry start message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);}void hcid_dbus_inquiry_complete(bdaddr_t *local){	DBusMessage *message = NULL;	char path[MAX_PATH_LENGTH];	char *local_addr;	bdaddr_t tmp;	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_INQ_COMPLETE);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS inquiry complete message");		goto failed;	}	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS inquiry complete message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);}void hcid_dbus_inquiry_result(bdaddr_t *local, bdaddr_t *peer, uint32_t class, int8_t rssi){	DBusMessage *message = NULL;	char path[MAX_PATH_LENGTH];	char *local_addr, *peer_addr;	dbus_uint32_t tmp_class = class;	dbus_int32_t tmp_rssi = rssi;	bdaddr_t tmp;	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	baswap(&tmp, peer); peer_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_INQ_RESULT);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS inquiry result message");		goto failed;	}	dbus_message_append_args(message,					DBUS_TYPE_STRING, &peer_addr,					DBUS_TYPE_UINT32, &tmp_class,					DBUS_TYPE_INT32, &tmp_rssi,					DBUS_TYPE_INVALID);	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS inquiry result message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);	bt_free(peer_addr);}void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name){	DBusMessage *message = NULL;	char path[MAX_PATH_LENGTH];	char *local_addr, *peer_addr;	bdaddr_t tmp;	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	baswap(&tmp, peer); peer_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_REMOTE_NAME);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS remote name message");		goto failed;	}	dbus_message_append_args(message,					DBUS_TYPE_STRING, &peer_addr,					DBUS_TYPE_STRING, &name,					DBUS_TYPE_INVALID);	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS remote name message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);	bt_free(peer_addr);}void hcid_dbus_remote_name_failed(bdaddr_t *local, bdaddr_t *peer, uint8_t status){	DBusMessage *message = NULL;	char path[MAX_PATH_LENGTH];	char *local_addr, *peer_addr;	bdaddr_t tmp;	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	baswap(&tmp, peer); peer_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_REMOTE_NAME_FAILED);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS remote name message");		goto failed;	}	dbus_message_append_args(message,					DBUS_TYPE_STRING, &peer_addr,					DBUS_TYPE_BYTE, &status,					DBUS_TYPE_INVALID);	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS remote name message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);	bt_free(peer_addr);}void hcid_dbus_conn_complete(bdaddr_t *local, bdaddr_t *peer){}void hcid_dbus_disconn_complete(bdaddr_t *local, bdaddr_t *peer, uint8_t reason){}void hcid_dbus_auth_complete(bdaddr_t *local, bdaddr_t *peer, const uint8_t status){	DBusMessage *message = NULL;	char *local_addr, *peer_addr;	bdaddr_t tmp;	char path[MAX_PATH_LENGTH];	int id;	baswap(&tmp, local); local_addr = batostr(&tmp);	baswap(&tmp, peer); peer_addr = batostr(&tmp);	id = hci_devid(local_addr);	if (id < 0) {		syslog(LOG_ERR, "No matching device id for %s", local_addr);		goto failed;	}	snprintf(path, sizeof(path), "%s/hci%d/%s", DEVICE_PATH, id, BLUEZ_HCI);	message = dbus_message_new_signal(path, DEV_HCI_INTERFACE,						BLUEZ_HCI_AUTH_COMPLETE);	if (message == NULL) {		syslog(LOG_ERR, "Can't allocate D-BUS remote name message");		goto failed;	}	dbus_message_append_args(message,					DBUS_TYPE_STRING, &peer_addr,					DBUS_TYPE_BYTE, &status,					DBUS_TYPE_INVALID);	if (dbus_connection_send(connection, message, NULL) == FALSE) {		syslog(LOG_ERR, "Can't send D-BUS remote name message");		goto failed;	}	dbus_connection_flush(connection);failed:	dbus_message_unref(message);	bt_free(local_addr);	bt_free(peer_addr);}gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data){	DBusWatch *watch = (DBusWatch *) data;	int flags = 0;	if (cond & G_IO_IN)  flags |= DBUS_WATCH_READABLE;	if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;	if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;	if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;	dbus_watch_handle(watch, flags);	dbus_connection_ref(connection);	/* Dispatch messages */	while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS);	dbus_connection_unref(connection);	return TRUE;}dbus_bool_t add_watch(DBusWatch *watch, void *data){	GIOCondition cond = G_IO_HUP | G_IO_ERR;	GIOChannel *io;	guint *id;	int fd, flags;	if (!dbus_watch_get_enabled(watch))		return TRUE;	id = malloc(sizeof(guint));	if (id == NULL)		return FALSE;	fd = dbus_watch_get_fd(watch);	io = g_io_channel_unix_new(fd);	flags = dbus_watch_get_flags(watch);	if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;	if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;	*id = g_io_add_watch(io, cond, watch_func, watch);	dbus_watch_set_data(watch, id, NULL);	return TRUE;}static void remove_watch(DBusWatch *watch, void *data){	guint *id = dbus_watch_get_data(watch);	dbus_watch_set_data(watch, NULL, NULL);	if (id) {		g_io_remove_watch(*id);		free(id);	}}static void watch_toggled(DBusWatch *watch, void *data){	/* Because we just exit on OOM, enable/disable is	 * no different from add/remove */	if (dbus_watch_get_enabled(watch))		add_watch(watch, data);	else		remove_watch(watch, data);}static gboolean unregister_device_path(const char *path){	char **children = NULL;	if (!dbus_connection_list_registered(connection, path, &children))		goto done;	for (; *children; children++) {		char child_path[MAX_PATH_LENGTH];		snprintf(child_path, sizeof(child_path), "%s/%s", path, *children);		unregister_dbus_path(child_path);	}	if (*children)		dbus_free_string_array(children);done:	return unregister_dbus_path(path);}gboolean hcid_dbus_init(void){	DBusError error;	dbus_error_init(&error);	connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);	if (dbus_error_is_set(&error)) {		syslog(LOG_ERR, "Can't open system message bus connection: %s",								error.message);		dbus_error_free(&error);		return FALSE;	}	dbus_connection_set_exit_on_disconnect(connection, FALSE);	dbus_bus_request_name(connection, BASE_INTERFACE,				DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &error);	if (dbus_error_is_set(&error)) {		syslog(LOG_ERR, "Can't get system message bus name: %s",								error.message);		dbus_error_free(&error);		return FALSE;	}	if (!register_dbus_path(DEVICE_PATH, DEVICE_ROOT_ID, INVALID_DEV_ID,				&obj_dev_vtable, TRUE))		return FALSE;	if (!register_dbus_path(MANAGER_PATH, MANAGER_ROOT_ID, INVALID_DEV_ID,				&obj_mgr_vtable, FALSE))		return FALSE;	if (!dbus_connection_add_filter(connection, hci_signal_filter, NULL, NULL)) {		syslog(LOG_ERR, "Can't add new HCI filter");		return FALSE;	}	dbus_connection_set_watch_functions(connection,			add_watch, remove_watch, watch_toggled, NULL, NULL);	return TRUE;}void hcid_dbus_exit(void){	char **children = NULL;	if (!connection)		return;	/* Unregister all paths in Device path hierarchy */	if (dbus_connection_list_registered(connection, DEVICE_PATH, &children)) {		for (; *children; children++) {			char dev_path[MAX_PATH_LENGTH];			snprintf(dev_path, sizeof(dev_path), "%s/%s", DEVICE_PATH, *children);			unregister_device_path(dev_path);		}		if (*children)			dbus_free_string_array(children);	}	unregister_dbus_path(DEVICE_PATH);	unregister_dbus_path(MANAGER_PATH);	dbus_connection_close(connection);}gboolean hcid_dbus_register_device(uint16_t id) {

⌨️ 快捷键说明

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