Exemple #1
0
// Connect two rooms. Returns true on success or false on failure.
// Failure may be that the rooms are already connected or the room's maximum
// number of connections has been reached.
bool connect(unsigned int room1, unsigned int room2,
                struct room rooms_list[NUM_ROOMS]) {
        struct room *r1 = &rooms_list[room1];
        struct room *r2 = &rooms_list[room2];
        // If the number of connections is maxed out, we're done
        if (r1->num_conns == MAX_CONN) {
                return true;
        }
        // if the rooms are already connected, don't connect them again
        if (already_connected(room1, room2)) {
                return false;
        }
        // if either of the rooms maximum number of connections has been
        // reached don't connect them.
        if (r1->num_conns >= MAX_CONN || r2->num_conns >= MAX_CONN) {
                return false;
        }
        assert(r1 != NULL);
        assert(r2 != NULL);
        // Connect the rooms
        r1->connections[r1->num_conns] = r2;
        r2->connections[r2->num_conns] = r1;
        // update the number of connections
        r1->num_conns++;
        r2->num_conns++;
        assert(r1->connections[r1->num_conns-1] != NULL);
        assert(r2->connections[r2->num_conns-1] != NULL);
        return true;
}
static DBusMessage *input_device_connect(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	struct input_device *idev = data;
	struct input_conn *iconn;
	struct fake_input *fake;
	DBusMessage *reply;
	GError *err = NULL;

	iconn = find_connection(idev->connections, "HID");
	if (!iconn)
		return not_supported(msg);

	if (iconn->pending_connect)
		return in_progress(msg);

	if (is_connected(iconn))
		return already_connected(msg);

	iconn->pending_connect = dbus_message_ref(msg);
	fake = iconn->fake;

	if (fake) {
		/* Fake input device */
		if (fake->connect(iconn, &err))
			fake->flags |= FI_FLAG_CONNECTED;
	} else {
		/* HID devices */
		GIOChannel *io;

		io = bt_io_connect(BT_IO_L2CAP, control_connect_cb, iconn,
					NULL, &err,
					BT_IO_OPT_SOURCE_BDADDR, &idev->src,
					BT_IO_OPT_DEST_BDADDR, &idev->dst,
					BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
					BT_IO_OPT_INVALID);
		iconn->ctrl_io = io;
	}

	if (err == NULL)
		return NULL;

	error("%s", err->message);
	dbus_message_unref(iconn->pending_connect);
	iconn->pending_connect = NULL;
	reply = connection_attempt_failed(msg, err->message);
	g_error_free(err);
	return reply;
}