Ejemplo n.º 1
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
    bdaddr_t sba, dba;

    if (err) {
        printf("connect error: %s\n", err->message);
        set_state(STATE_DISCONNECTED);
        return;
    }

    if (!io) {
        printf("connect error io NULL\n");
        set_state(STATE_DISCONNECTED);
        return;
    }

    iochannel = io;
    attrib = g_attrib_new(iochannel);
    g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
                      attrib, NULL);
    g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
                      attrib, NULL);

    /* LE connections share Client and Server paths */
    if (!opt_psm) {
        str2ba(opt_dst, &dba);
        str2ba(opt_src, &sba);
        attrib_server_attach(attrib, &sba, &dba, ATT_DEFAULT_LE_MTU);
    }

    set_state(STATE_CONNECTED);
}
static void setup_context(struct context *cxt, gconstpointer data)
{
	int err, sv[2];

	cxt->main_loop = g_main_loop_new(NULL, FALSE);
	g_assert(cxt->main_loop != NULL);

	err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
	g_assert(err == 0);

	cxt->att_io = g_io_channel_unix_new(sv[0]);
	g_assert(cxt->att_io != NULL);

	g_io_channel_set_close_on_unref(cxt->att_io, TRUE);

	cxt->server_io = g_io_channel_unix_new(sv[1]);
	g_assert(cxt->server_io != NULL);

	g_io_channel_set_close_on_unref(cxt->server_io, TRUE);
	g_io_channel_set_encoding(cxt->server_io, NULL, NULL);
	g_io_channel_set_buffered(cxt->server_io, FALSE);

	cxt->att = g_attrib_new(cxt->att_io, DEFAULT_MTU);
	g_assert(cxt->att != NULL);
}
Ejemplo n.º 3
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
	uint16_t mtu;
	uint16_t cid;

	if (err) {
		set_state(STATE_DISCONNECTED);
		error("%s\n", err->message);
		return;
	}

	bt_io_get(io, &err, BT_IO_OPT_IMTU, &mtu,
				BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);

	if (err) {
		g_printerr("Can't detect MTU, using default: %s", err->message);
		g_error_free(err);
		mtu = ATT_DEFAULT_LE_MTU;
	}

	if (cid == ATT_CID)
		mtu = ATT_DEFAULT_LE_MTU;

	attrib = g_attrib_new(iochannel, mtu, false);
	g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
						events_handler, attrib, NULL);
	g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
						events_handler, attrib, NULL);
	set_state(STATE_CONNECTED);
	rl_printf("Connection successful\n");
}
Ejemplo n.º 4
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
    uint16_t mtu;
    uint16_t cid;
    GError *gerr = NULL;
    uint8_t *value;

    if (err) {
        printf("# Connect error: %s\n", err->message);
        g_main_loop_quit(event_loop);
        return;
    }

    bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
                BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);

    if (gerr) {
        printf("# Can't detect MTU, using default");
        g_error_free(gerr);
        mtu = ATT_DEFAULT_LE_MTU;
    }
    else if (cid == ATT_CID)
        mtu = ATT_DEFAULT_LE_MTU;

    attrib = g_attrib_new(iochannel, mtu, false);

    g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
                        events_handler, attrib, NULL);
    g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
                        events_handler, attrib, NULL);
  printf("Connected.\n");
  //Enabling notifying
  gatt_attr_data_from_string("0100", &value);
  gatt_write_cmd(attrib, 0x002f, value, 2, NULL, NULL);
}
Ejemplo n.º 5
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
    uint16_t mtu;
    uint16_t cid;
    GError *gerr = NULL;

    DBG("io = %p, err = %p", io, err);
    if (err) {
        DBG("err = %s", err->message);
        set_state(STATE_DISCONNECTED);
        resp_error(err_CONN_FAIL);
        printf("# Connect error: %s\n", err->message);
        return;
    }

    bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
                BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);

    if (gerr) {
        printf("# Can't detect MTU, using default");
        g_error_free(gerr);
        mtu = ATT_DEFAULT_LE_MTU;
    }
    else if (cid == ATT_CID)
        mtu = ATT_DEFAULT_LE_MTU;

    attrib = g_attrib_new(iochannel, mtu);

    g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
                        events_handler, attrib, NULL);
    g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
                        events_handler, attrib, NULL);
    g_attrib_register(attrib, ATT_OP_FIND_INFO_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_FIND_BY_TYPE_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_READ_BY_TYPE_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_READ_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_READ_BLOB_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_READ_MULTI_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_READ_BY_GROUP_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_WRITE_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_WRITE_CMD, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_SIGNED_WRITE_CMD, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_PREP_WRITE_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);
    g_attrib_register(attrib, ATT_OP_EXEC_WRITE_REQ, GATTRIB_ALL_HANDLES,
                        req_gatts, NULL, NULL);

    set_state(STATE_CONNECTED);
}
Ejemplo n.º 6
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
	if (err) {
		std::cout <<  err->message << std::endl;
		return;
	}
	user_data_t* ud = static_cast<user_data_t*>(user_data);
	GAttrib* attrib = g_attrib_new(io);
	if (ud->mtu < ATT_DEFAULT_LE_MTU) {
		std::cout << "Invalid value. Minimum MTU size is " << ATT_DEFAULT_LE_MTU;
		g_main_loop_quit(event_loop);
		return;
	}
	ud->attrib = attrib;
	gatt_exchange_mtu(attrib, ud->mtu, exchange_mtu_cb, ud);
}
Ejemplo n.º 7
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
  if (err) {
    set_error(ERR_CONNECT_FAILED, err->message, user_data);
    set_state(STATE_DISCONNECTED);

  } else {
    /* attrib is declared as global for now */
    attrib = g_attrib_new(iochannel);
    g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES, events_handler, attrib, NULL);
    g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES, events_handler, attrib, NULL);
    set_state(STATE_CONNECTED);
  }
  g_main_loop_quit(event_loop);
}
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
	if (err) {
		printf("connect error: %s\n", err->message);
		set_state(STATE_DISCONNECTED);
		return;
	}

	attrib = g_attrib_new(iochannel);
	g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
							attrib, NULL);
	g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
							attrib, NULL);
	set_state(STATE_CONNECTED);
}
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
    if (err) {
        set_state(STATE_DISCONNECTED);
        error("%s\n", err->message);
        return;
    }

    attrib = g_attrib_new(iochannel);
    g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
                      events_handler, attrib, NULL);
    g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
                      events_handler, attrib, NULL);
    set_state(STATE_CONNECTED);
    rl_printf("Connection successful\n");
}
Ejemplo n.º 10
0
static struct context *create_context(gconstpointer data)
{
	struct context *context;
	GIOChannel *channel, *att_io;
	int err, sv[2], fd;
	char name[] = "bluez-hog";
	uint16_t vendor = 0x0002;
	uint16_t product = 0x0001;
	uint16_t version = 0x0001;

	context = g_new0(struct context, 1);
	err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
	g_assert(err == 0);

	att_io = g_io_channel_unix_new(sv[0]);

	g_io_channel_set_close_on_unref(att_io, TRUE);

	context->attrib = g_attrib_new(att_io, 23, false);
	g_assert(context->attrib);

	g_io_channel_unref(att_io);

	fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
	g_assert(fd > 0);

	context->hog = bt_hog_new(fd, name, vendor, product, version, NULL);
	g_assert(context->hog);

	channel = g_io_channel_unix_new(sv[1]);

	g_io_channel_set_close_on_unref(channel, TRUE);
	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);

	context->source = g_io_add_watch(channel,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				test_handler, context);
	g_assert(context->source > 0);

	g_io_channel_unref(channel);

	context->fd = sv[1];
	context->data = data;

	return context;
}
Ejemplo n.º 11
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
	GAttrib *attrib;

	if (err) {
		g_printerr("%s\n", err->message);
		got_error = TRUE;
		g_main_loop_quit(event_loop);
	}

	attrib = g_attrib_new(io);

	if (opt_listen)
		g_idle_add(listen_start, attrib);

	operation(attrib);
}
Ejemplo n.º 12
0
/*
 * Callback functions
 */
void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
  printf_dbg("[CB] IN connect_cb\n");
  if (err) {
    set_conn_state(STATE_DISCONNECTED);
    cb_ret_val = BL_REQUEST_FAIL_ERROR;
    sprintf(cb_ret_msg, "%s", err->message);
    goto error;
  }
  attrib = g_attrib_new(iochannel);
  set_conn_state(STATE_CONNECTED);
  strcpy(cb_ret_msg, "Connection successful\n");
  cb_ret_val = BL_NO_ERROR;

 error:
  g_mutex_unlock(pending_callback);
  printf_dbg("[CB] OUT connect_cb\n");
}
Ejemplo n.º 13
0
//callback function when we get connected
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
	if (err) {
		printf("%s\n", err->message);
		return;
	}

	if (connect_err) {
		printf("%s\n", connect_err->message);
		return;
	}

	attrib = g_attrib_new(iochannel);
	if(attrib == NULL){
		printf("Connect Error\n");
	}
	ble_connected = 1;
}
Ejemplo n.º 14
0
/*
 * Callback functions
 */
void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
    cb_ctx_t *cb_ctx = user_data;

    printf_dbg("IN connect_cb\n");
    if (err) {
        set_conn_state(cb_ctx->dev_ctx, STATE_DISCONNECTED);
        cb_ctx->cb_ret_val = BL_REQUEST_FAIL_ERROR;
        sprintf(cb_ctx->cb_ret_msg, "%s", err->message);
        goto error;
    }
    cb_ctx->dev_ctx->attrib = g_attrib_new(cb_ctx->dev_ctx->iochannel);
    set_conn_state(cb_ctx->dev_ctx, STATE_CONNECTED);
    strcpy(cb_ctx->cb_ret_msg, "Connection successful\n");
    cb_ctx->cb_ret_val = BL_NO_ERROR;

error:
    g_mutex_unlock(&cb_ctx->pending_cb_mtx);
    printf_dbg("OUT connect_cb\n");
}
Ejemplo n.º 15
0
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
	if (err) {
		std::cout <<  err->message << std::endl;
		return;
	}
	user_data_t* ud = static_cast<user_data_t*>(user_data);
	GAttrib* attrib = g_attrib_new(io);
	g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
						notify_cb, ud, NULL);
	g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
						indicate_cb, ud, NULL);

	if (ud->mtu < ATT_DEFAULT_LE_MTU) {
		std::cout << "Invalid value. Minimum MTU size is " << ATT_DEFAULT_LE_MTU;
		disconnect(ud->attrib, ud->chan);
		g_main_loop_quit(event_loop);
		return;
	}
	ud->attrib = attrib;
	gatt_exchange_mtu(attrib, ud->mtu, exchange_mtu_cb, ud);
}
Ejemplo n.º 16
0
static void io_connect_cb(GIOChannel *io, GError *err, gpointer user_data) {
	io_connect_arg_t* io_connect_arg = user_data;

	if (err) {
		io_connect_arg->error = err;

		// Call callback if defined
		if (io_connect_arg->connect_cb) {
			io_connect_arg->connect_cb(NULL);
		}
	} else {
		io_connect_arg->conn->attrib = g_attrib_new(io);

		//
		// Register the listener callback
		//
		GSource *source = g_idle_source_new ();
		assert(source != NULL);

		g_source_set_callback(source, io_listen_cb, io_connect_arg->conn, NULL);

		// Attaches the listener to the main loop context
		guint id = g_source_attach(source, g_gattlib_thread.loop_context);
		g_source_unref (source);
		assert(id != 0);

		//
		// Call callback if defined
		//
		if (io_connect_arg->connect_cb) {
			io_connect_arg->connect_cb(io_connect_arg->conn);
		}

		io_connect_arg->connected = TRUE;
	}
	if (io_connect_arg->connect_cb) {
		free(io_connect_arg);
	}
}
Ejemplo n.º 17
0
int main(int argc, char *argv[])
{
	GOptionContext *context;
	GOptionGroup *gatt_group, *params_group, *char_rw_group;
	GError *gerr = NULL;
	GAttrib *attrib;
	GIOChannel *chan;
	GSourceFunc callback;

	opt_sec_level = g_strdup("low");

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, options, NULL);

	/* GATT commands */
	gatt_group = g_option_group_new("gatt", "GATT commands",
					"Show all GATT commands", NULL, NULL);
	g_option_context_add_group(context, gatt_group);
	g_option_group_add_entries(gatt_group, gatt_options);

	/* Primary Services and Characteristics arguments */
	params_group = g_option_group_new("params",
			"Primary Services/Characteristics arguments",
			"Show all Primary Services/Characteristics arguments",
			NULL, NULL);
	g_option_context_add_group(context, params_group);
	g_option_group_add_entries(params_group, primary_char_options);

	/* Characteristics value/descriptor read/write arguments */
	char_rw_group = g_option_group_new("char-read-write",
		"Characteristics Value/Descriptor Read/Write arguments",
		"Show all Characteristics Value/Descriptor Read/Write "
		"arguments",
		NULL, NULL);
	g_option_context_add_group(context, char_rw_group);
	g_option_group_add_entries(char_rw_group, char_rw_options);

	if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
		g_printerr("%s\n", gerr->message);
		g_error_free(gerr);
	}

	if (opt_interactive) {
		interactive(opt_src, opt_dst, opt_psm);
		goto done;
	}

	if (opt_primary)
		callback = primary;
	else if (opt_characteristics)
		callback = characteristics;
	else if (opt_char_read)
		callback = characteristics_read;
	else if (opt_char_write)
		callback = characteristics_write;
	else if (opt_char_write_req)
		callback = characteristics_write_req;
	else if (opt_char_desc)
		callback = characteristics_desc;
	else {
		gchar *help = g_option_context_get_help(context, TRUE, NULL);
		g_print("%s\n", help);
		g_free(help);
		got_error = TRUE;
		goto done;
	}

	chan = gatt_connect(opt_src, opt_dst, opt_sec_level,
					opt_psm, opt_mtu, connect_cb);
	if (chan == NULL) {
		got_error = TRUE;
		goto done;
	}

	attrib = g_attrib_new(chan);
	g_io_channel_unref(chan);

	event_loop = g_main_loop_new(NULL, FALSE);

	if (opt_listen)
		g_idle_add(listen_start, attrib);

	g_idle_add(callback, attrib);

	g_main_loop_run(event_loop);

	g_attrib_unregister_all(attrib);

	g_main_loop_unref(event_loop);

	g_attrib_unref(attrib);

done:
	g_option_context_free(context);
	g_free(opt_src);
	g_free(opt_dst);
	g_free(opt_uuid);
	g_free(opt_sec_level);

	if (got_error)
		exit(EXIT_FAILURE);
	else
		exit(EXIT_SUCCESS);
}