Ejemplo n.º 1
0
gboolean static writehnd()
{
	//rl_printf("test\n");
	int chs = getChannelstatus();

	//rl_printf("status=%s\n",data);
	if (conn_state == STATE_DISCONNECTED && chs==1)
	{


		myconnect();

	}
	if(chs==0)
	{
		disconnect_io();
		return TRUE;
	}
	if(conn_state == STATE_CONNECTING)
		return TRUE;


	if(attrib ==NULL)
	return TRUE;
	http();
	rl_printf("data = %s\n",data);

	if( pre_state != 31 && atoi(data) ==1)
	{
		size_t plen;

		uint8_t *value;
		plen = gatt_attr_data_from_string("31", &value);
		gatt_write_cmd(attrib, 14, value, plen, NULL, NULL);
		g_free(value);

		pre_state = 31;
	}
	else if(pre_state != 30 && atoi(data)==0)
	{
		size_t plen;

		uint8_t *value;
		plen = gatt_attr_data_from_string("30", &value);
		gatt_write_cmd(attrib, 14, value, plen, NULL, NULL);
		g_free(value);
		pre_state = 30;
	}
	//http();



	return TRUE;
}
Ejemplo n.º 2
0
static DBusMessage *set_value(DBusConnection *conn, DBusMessage *msg,
			DBusMessageIter *iter, struct characteristic *chr)
{
	struct gatt_service *gatt = chr->gatt;
	DBusMessageIter sub;
	uint8_t *value;
	int len;

	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
			dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE)
		return btd_error_invalid_args(msg);

	dbus_message_iter_recurse(iter, &sub);

	dbus_message_iter_get_fixed_array(&sub, &value, &len);

	characteristic_set_value(chr, value, len);

	if (gatt->attioid == 0)
		gatt->attioid = btd_device_add_attio_callback(gatt->dev,
							attio_connected,
							attio_disconnected,
							gatt);

	if (gatt->attrib)
		gatt_write_cmd(gatt->attrib, chr->handle, value, len,
								NULL, NULL);
	else
		gatt->offline_chars = g_slist_append(gatt->offline_chars, chr);

	return dbus_message_new_method_return(msg);
}
Ejemplo n.º 3
0
static void proto_mode_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
							gpointer user_data)
{
	struct hog_device *hogdev = user_data;
	uint8_t value;
	ssize_t vlen;

	if (status != 0) {
		error("Protocol Mode characteristic read failed: %s",
							att_ecode2str(status));
		return;
	}

	vlen = dec_read_resp(pdu, plen, &value, sizeof(value));
	if (vlen < 0) {
		error("ATT protocol error");
		return;
	}

	if (value == HOG_PROTO_MODE_BOOT) {
		uint8_t nval = HOG_PROTO_MODE_REPORT;

		DBG("HoG device 0x%04X is operating in Boot Procotol Mode",
								hogdev->id);

		gatt_write_cmd(hogdev->attrib, hogdev->proto_mode_handle, &nval,
						sizeof(nval), NULL, NULL);
	} else if (value == HOG_PROTO_MODE_REPORT)
		DBG("HoG device 0x%04X is operating in Report Protocol Mode",
								hogdev->id);
}
Ejemplo n.º 4
0
static void forward_report(struct uhid_event *ev, void *user_data)
{
	struct hog_device *hogdev = user_data;
	struct report *report;
	void *data;
	int size;

	report = find_report(hogdev, ev->u.output.rtype, ev->u.output.data[0]);
	if (!report)
		return;

	data = ev->u.output.data;
	size = ev->u.output.size;
	if (hogdev->has_report_id && size > 0) {
		data++;
		--size;
	}

	DBG("Sending report type %d ID %d to handle 0x%X", report->type,
				report->id, report->decl->value_handle);

	if (hogdev->attrib == NULL)
		return;

	if (report->decl->properties & GATT_CHR_PROP_WRITE)
		gatt_write_char(hogdev->attrib, report->decl->value_handle,
				data, size, output_written_cb, hogdev);
	else if (report->decl->properties & GATT_CHR_PROP_WRITE_WITHOUT_RESP)
		gatt_write_cmd(hogdev->attrib, report->decl->value_handle,
						data, size, NULL, NULL);
}
Ejemplo n.º 5
0
static gboolean immediate_timeout(gpointer user_data)
{
	struct monitor *monitor = user_data;
	const char *path = device_get_path(monitor->device);

	monitor->immediateto = 0;

	if (g_strcmp0(monitor->immediatelevel, "none") == 0)
		return FALSE;

	if (monitor->attrib) {
		uint8_t value = ALERT_NONE;
		gatt_write_cmd(monitor->attrib, monitor->immediatehandle,
				&value, 1, NULL, NULL);
	}

	g_free(monitor->immediatelevel);
	monitor->immediatelevel = g_strdup("none");


	g_dbus_emit_property_changed(btd_get_dbus_connection(), path,
				PROXIMITY_INTERFACE, "ImmediateAlertLevel");

	return FALSE;
}
Ejemplo n.º 6
0
static gboolean characteristics_write(gpointer user_data)
{
	GAttrib *attrib = user_data;
	uint8_t *value;
	size_t len;

	if (opt_handle <= 0) {
		g_printerr("A valid handle is required\n");
		goto error;
	}

	if (opt_value == NULL || opt_value[0] == '\0') {
		g_printerr("A value is required\n");
		goto error;
	}

	len = gatt_attr_data_from_string(opt_value, &value);
	if (len == 0) {
		g_printerr("Invalid value\n");
		goto error;
	}

	gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);

	return FALSE;

error:
	g_main_loop_quit(event_loop);
	return FALSE;
}
Ejemplo n.º 7
0
static void cmd_anki_vehicle_write(int argcp, char **argvp)
{
        uint8_t *value;
        size_t plen;
        int handle;

        if (conn_state != STATE_CONNECTED) {
                failed("Disconnected\n");
                return;
        }

        if (argcp < 2) {
                rl_printf("Usage: %s <handle> <new value>\n", argvp[0]);
                return;
        }

        handle = vehicle.write_char.value_handle;

        plen = gatt_attr_data_from_string(argvp[1], &value);
        if (plen == 0) {
                error("Invalid value\n");
                return;
        }

        if (g_strcmp0("send-data-req", argvp[0]) == 0)
                gatt_write_char(attrib, handle, value, plen,
                                        char_write_req_cb, NULL);
        else
                gatt_write_cmd(attrib, handle, value, plen, NULL, NULL);

        g_free(value);
}
Ejemplo n.º 8
0
static void write_immediate_alert(struct monitor *monitor)
{
	uint8_t value = str2level(monitor->immediatelevel);

	gatt_write_cmd(monitor->attrib, monitor->immediatehandle, &value, 1,
						immediate_written, monitor);
}
Ejemplo n.º 9
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.º 10
0
static void offline_char_write(gpointer data, gpointer user_data)
{
	struct characteristic *chr = data;
	GAttrib *attrib = user_data;

	gatt_write_cmd(attrib, chr->handle, chr->value, chr->vlen,
						offline_char_written, chr);
}
Ejemplo n.º 11
0
static void write_scan_params(GAttrib *attrib, uint16_t handle)
{
	uint8_t value[4];

	put_le16(SCAN_INTERVAL, &value[0]);
	put_le16(SCAN_WINDOW, &value[2]);

	gatt_write_cmd(attrib, handle, value, sizeof(value), NULL, NULL);
}
Ejemplo n.º 12
0
static void write_scan_params(GAttrib *attrib, uint16_t handle,
					uint16_t interval, uint16_t window)
{
	uint8_t value[4];

	put_le16(interval, &value[0]);
	put_le16(window, &value[2]);

	gatt_write_cmd(attrib, handle, value, sizeof(value), NULL, NULL);
}
Ejemplo n.º 13
0
Archivo: hog.c Proyecto: aguedes/bluez
int bt_hog_set_control_point(struct bt_hog *hog, bool suspend)
{
	uint8_t value = suspend ? 0x00 : 0x01;

	if (hog->attrib == NULL)
		return -ENOTCONN;

	if (hog->ctrlpt_handle == 0)
		return -ENOTSUP;

	gatt_write_cmd(hog->attrib, hog->ctrlpt_handle, &value,
					sizeof(value), NULL, NULL);

	return 0;
}
Ejemplo n.º 14
0
static void parse_line(char *line_read)
{
    int i, c, str_len, chunks;
    size_t plen;
    uint8_t *value;
    line_read = g_strstrip(line_read);
	str_len = strlen(line_read);
    chunks = (str_len / 10);
    char  outword[20];
    for(c = 0; c<chunks; c++){
        for(i = 0; i<10; i++){
            sprintf(outword+i*2, "%02X", line_read[10*c+i]);
        }
        plen = gatt_attr_data_from_string(outword, &value);
        gatt_write_cmd(attrib, 0x0031, value, plen, NULL, NULL);
    }
// remainder
    for(i = 0; i<str_len%10; i++){
        sprintf(outword+i*2, "%02X", line_read[10*c+i]);
    }       
        sprintf(outword+i*2, "%02X", 13); // CR
        plen = gatt_attr_data_from_string(outword, &value);
        gatt_write_cmd(attrib, 0x0031, value, plen, NULL, NULL);
}
Ejemplo n.º 15
0
Archivo: hog.c Proyecto: aguedes/bluez
static void forward_report(struct uhid_event *ev, void *user_data)
{
	struct bt_hog *hog = user_data;
	struct report *report;
	GSList *l;
	void *data;
	int size;
	guint type;

	if (hog->has_report_id) {
		data = ev->u.output.data + 1;
		size = ev->u.output.size - 1;
	} else {
		data = ev->u.output.data;
		size = ev->u.output.size;
	}

	switch (ev->type) {
	case UHID_OUTPUT:
		type = HOG_REPORT_TYPE_OUTPUT;
		break;
	case UHID_FEATURE:
		type = HOG_REPORT_TYPE_FEATURE;
		break;
	default:
		return;
	}

	l = g_slist_find_custom(hog->reports, GUINT_TO_POINTER(type),
							report_type_cmp);
	if (!l)
		return;

	report = l->data;

	DBG("Sending report type %d to handle 0x%X", type,
						report->decl->value_handle);

	if (hog->attrib == NULL)
		return;

	if (report->decl->properties & GATT_CHR_PROP_WRITE)
		gatt_write_char(hog->attrib, report->decl->value_handle,
				data, size, output_written_cb, hog);
	else if (report->decl->properties & GATT_CHR_PROP_WRITE_WITHOUT_RESP)
		gatt_write_cmd(hog->attrib, report->decl->value_handle,
						data, size, NULL, NULL);
}
Ejemplo n.º 16
0
static DBusMessage *set_broadcasting(DBusConnection *conn, DBusMessage *msg,
			DBusMessageIter *iter, struct characteristic *chr)
{
	struct gatt_service *gatt = chr->gatt;
	DBusMessageIter sub;
	dbus_bool_t broadcasting;
	uint8_t value[2];
	int len = 2;
	struct write_val_op *op;

	INFO("set_broadcasting");

	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN)
		return btd_error_invalid_args(msg);

	dbus_message_iter_get_basic(iter, &broadcasting);

	if (chr->scc_handle == 0) /* Error if char does not have a ccc */
		return btd_error_not_supported(msg);

	if ((chr->perm & GATT_CHAR_PROPS_BROADCAST) == 0) /* Error if char permissions do not allow broadcasting */
		return btd_error_not_supported(msg);

	if (broadcasting) {
		chr->server_config = chr->server_config | GATT_CHAR_SERVERCONF_BROADCAST;
	} else {
		chr->server_config = chr->server_config & ~GATT_CHAR_SERVERCONF_BROADCAST;
	}

	value[0] = chr->server_config & 0xFF;
	value[1] = 0;

	op = g_new0(struct write_val_op, 1);
	op->msg = 0;
	op->conn = conn;
	op->chr = chr;
	INFO("value[0]=%02X",chr->server_config);


	gatt_write_cmd(chr->gatt->attrib, chr->scc_handle, (uint8_t*)value, len, set_broadcasting_cb, op);

	return dbus_message_new_method_return(msg);
}
Ejemplo n.º 17
0
static DBusMessage *set_indicating(DBusConnection *conn, DBusMessage *msg,
			DBusMessageIter *iter, struct characteristic *chr)
{
	struct gatt_service *gatt = chr->gatt;
	DBusMessageIter sub;
	dbus_bool_t indicating;
	uint8_t value[2];
	int len = 2;
	struct write_val_op *op;

	INFO("set_indicating");

	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BOOLEAN)
		return btd_error_invalid_args(msg);

	dbus_message_iter_get_basic(iter, &indicating);

	if (chr->ccc_handle == 0) /* Error if char does not have a ccc */
		return btd_error_not_supported(msg);

	if ((chr->perm & GATT_CHAR_PROPS_INDICATE) == 0) /* Error if char permissions do not allow indicating */
		return btd_error_not_supported(msg);

	if (indicating) { /* we do not allow notification + indicating at the same time */
		chr->client_config = chr->client_config | GATT_CHAR_CLIENTCONF_INDICATE;
		chr->client_config = chr->client_config & ~GATT_CHAR_CLIENTCONF_NOTIFY;
	} else {
		chr->client_config = chr->client_config & ~GATT_CHAR_CLIENTCONF_INDICATE;
	}

	value[0] = chr->client_config & 0xFF;
	value[1] = 0;
	INFO("value[0]=%02X",chr->client_config);

	op = g_new0(struct write_val_op, 1);
	op->msg = 0;
	op->conn = conn;
	op->chr = chr;

	gatt_write_cmd(chr->gatt->attrib, chr->ccc_handle, (uint8_t*)value, len, set_indicating_cb, op);

	return dbus_message_new_method_return(msg);
}
Ejemplo n.º 18
0
static DBusMessage *write_val(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	DBusMessage *reply;
	struct characteristic *chr = data;
	struct gatt_service *gatt = chr->gatt;
	char* value;
	char* write_method;
	uint16_t len = 0;
	uint16_t offset = 0;
	uint16_t timeout = 0;
	guint retvalue = 0;
	struct write_val_op *op;

	if (!dbus_message_get_args(msg, NULL,
			DBUS_TYPE_ARRAY,DBUS_TYPE_BYTE, &value, &len,
			DBUS_TYPE_STRING, &write_method,
			DBUS_TYPE_UINT16, &offset,
			DBUS_TYPE_UINT16, &timeout,
			DBUS_TYPE_INVALID))
		return btd_error_invalid_args(msg);

	DBG("WriteValue");// chr=%s value[0]=%d len=%d write_method=%s offset=%d timeout=%d",chr->path, value[0], len, write_method, offset, timeout);

	op = g_new0(struct write_val_op, 1);
	op->msg = dbus_message_ref(msg);
	op->conn = conn;
	op->chr = chr;

	if (g_str_equal("WriteWithResponse", write_method)) {
		INFO("write with response");
		retvalue = gatt_write_char(chr->gatt->attrib, chr->handle, (uint8_t*)value, len, write_req_cb, op);
	} else if (g_str_equal("WriteWithoutResponse", write_method)) {
		INFO("write without response");
		retvalue = gatt_write_cmd(chr->gatt->attrib, chr->handle, (uint8_t*)value, len, write_cmd_cb, op);
	} else {
		ERROR("write_method %s is not supported at the moment.",write_method);
		return btd_error_not_supported(msg);
	}
	return NULL;
}
Ejemplo n.º 19
0
static void discover_char_cb(GSList *characteristics, guint8 status, gpointer user_data)
{
	GSList *l;

	if (status) {
		error("Discover all characteristics failed: %s\n",
							att_ecode2str(status));
		return;
	}

	for (l = characteristics; l; l = l->next) {
		struct gatt_char *chars = l->data;

                if (strncasecmp(chars->uuid, ANKI_STR_CHR_READ_UUID, strlen(ANKI_STR_CHR_READ_UUID)) == 0) {
                        memmove(&(vehicle.read_char), chars, sizeof(struct gatt_char));
                        rl_printf("Anki Read Characteristic: %s ", chars->uuid);
		        rl_printf("[handle: 0x%04x, char properties: 0x%02x, char value "
				"handle: 0x%04x]\n", chars->handle,
				chars->properties, chars->value_handle);
                }

                if (strncasecmp(chars->uuid, ANKI_STR_CHR_WRITE_UUID, strlen(ANKI_STR_CHR_WRITE_UUID)) == 0) {
                        memmove(&(vehicle.write_char), chars, sizeof(struct gatt_char));
                        rl_printf("Anki Write Characteristic: %s ", chars->uuid);
		        rl_printf("[handle: 0x%04x, char properties: 0x%02x, char value "
				"handle: 0x%04x]\n", chars->handle,
				chars->properties, chars->value_handle);
                }
	}

        if (vehicle.read_char.handle > 0 && vehicle.write_char.handle > 0) {
                // register for notifications when the vehicle sends data.
                // We do this by setting the notification bit on the
                // client configuration characteristic:
                // see:
                // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
                uint8_t notify_cmd[] = { 0x01, 0x00 };
                gatt_write_cmd(attrib, vehicle.write_char.properties, notify_cmd,  2, NULL, NULL);
        }
}
Ejemplo n.º 20
0
static void cmd_char_write_common(int argcp, char **argvp, int with_response)
{
    uint8_t *value;
    size_t plen;
    int handle;

    if (conn_state != STATE_CONNECTED) {
        resp_error(err_BAD_STATE);
        return;
    }

    if (argcp < 3) {
        resp_error(err_BAD_PARAM);;
        return;
    }

    handle = strtohandle(argvp[1]);
    if (handle <= 0) {
        resp_error(err_BAD_PARAM);;
        return;
    }

    plen = gatt_attr_data_from_string(argvp[2], &value);
    if (plen == 0) {
        resp_error(err_BAD_PARAM);;
        return;
    }

    if (with_response)
        gatt_write_char(attrib, handle, value, plen,
                    char_write_req_cb, NULL);
    else
    {
        gatt_write_cmd(attrib, handle, value, plen, NULL, NULL);
        resp_begin(rsp_WRITE);
        resp_end();
    }

    g_free(value);
}
Ejemplo n.º 21
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);
	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");


	size_t plen;
	uint8_t *value;
	plen = gatt_attr_data_from_string("0100", &value);
	gatt_write_cmd(attrib, 0xc, value, plen, NULL, NULL);
	g_free(value);
}
Ejemplo n.º 22
0
static int cmd_char_write(gpointer user_data, const char *handle, const char* data, int type)
{
  GAttrib *attrib = user_data;
  uint8_t *value;
  size_t len;
  int hdl, ret;

  /* we need to enable notifications very early, as well change mode
   * when we receive an exception to quit
   */
  if ((get_state() != STATE_DATARCVD) && (get_state() != STATE_CONNECTED)) {
    printf("Device is not connected\n");
    return -1;
  }

  hdl = strtohandle(handle);
  if (hdl <= 0) {
    printf("A valid handle is needed\n");
    return -2;
  }

  len = gatt_attr_data_from_string(data, &value);
  if (len == 0) {
    printf("Invalid value(s) passed\n");
    return -3;
  }

  if (type == WRITE_REQUEST) {
    ret = gatt_write_char(attrib, hdl, value, len, NULL, NULL);
  } else if (type == WRITE_COMMAND) {
    ret = gatt_write_cmd(attrib, hdl, value, len, NULL, NULL);
  }

  g_free(value);

  return 0;
}
Ejemplo n.º 23
0
static void cmd_char_write(int argcp, char **argvp)
{
	uint8_t *value;
	size_t plen;
	int handle;

	if (conn_state != STATE_CONNECTED) {
		failed("Disconnected\n");
		return;
	}

	if (argcp < 3) {
		rl_printf("Usage: %s <handle> <new value>\n", argvp[0]);
		return;
	}

	handle = strtohandle(argvp[1]);
	if (handle <= 0) {
		error("A valid handle is required\n");
		return;
	}

	plen = gatt_attr_data_from_string(argvp[2], &value);
	if (plen == 0) {
		error("Invalid value\n");
		return;
	}

	if (g_strcmp0("char-write-req", argvp[0]) == 0)
		gatt_write_char(attrib, handle, value, plen,
					char_write_req_cb, NULL);
	else
		gatt_write_cmd(attrib, handle, value, plen, NULL, NULL);

	g_free(value);
}