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; }
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); }
static void cmd_gatts(int argcp, char **argvp) { if (argcp == 2) { GAttribResultFunc cb_func = NULL; uint8_t *opdu = NULL; size_t olen = gatt_attr_data_from_string(argvp[1], &opdu); DBG("GATTS %p %d %zd", opdu, opt_mtu, olen); /* Check if it is a value gatt server request */ if (opdu && (opt_mtu == 0 || olen <= opt_mtu) && !(opdu[0] == BT_ATT_OP_MTU_REQ || opdu[0] == BT_ATT_OP_FIND_INFO_REQ || opdu[0] == BT_ATT_OP_FIND_BY_TYPE_VAL_REQ || opdu[0] == BT_ATT_OP_READ_BY_TYPE_REQ || opdu[0] == BT_ATT_OP_READ_REQ || opdu[0] == BT_ATT_OP_READ_BLOB_REQ || opdu[0] == BT_ATT_OP_READ_MULT_REQ || opdu[0] == BT_ATT_OP_READ_BY_GRP_TYPE_REQ || opdu[0] == BT_ATT_OP_WRITE_REQ || opdu[0] == BT_ATT_OP_PREP_WRITE_REQ || opdu[0] == BT_ATT_OP_EXEC_WRITE_REQ)) { if (opdu[0] == BT_ATT_OP_HANDLE_VAL_IND) { cb_func = on_indicate_complete; } g_attrib_send(attrib, 0, opdu, olen, cb_func, NULL, NULL); } else { resp_error(err_BAD_PARAM);; } g_free(opdu); } else { resp_error(err_BAD_PARAM);; } }
static gboolean characteristics_write_req(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_char(attrib, opt_handle, value, len, char_write_req_cb, NULL); return FALSE; error: g_main_loop_quit(event_loop); return FALSE; }
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); }
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); }
static void cmd_char_write(int argcp, char **argvp) { uint8_t *value; size_t plen; int handle; if (conn_state != STATE_CONNECTED) { printf("Command failed: disconnected\n"); goto done; } if (argcp < 3) { printf("Usage: %s <handle> <new value>\n", argvp[0]); goto done; } handle = strtoll(argvp[1], NULL, 16); if (errno != 0 || handle <= 0) { printf("A valid handle is required\n"); goto done; } plen = gatt_attr_data_from_string(argvp[2], &value); if (plen == 0) { g_printerr("Invalid value\n"); goto done; } if (g_strcmp0("char-write-req", argvp[0]) == 0) gatt_write_char(attrib, handle, value, plen, char_write_req_cb, NULL); else gatt_write_char(attrib, handle, value, plen, NULL, NULL); g_free(value); return; done: printf("\r%s", get_prompt()); fflush(stdout); }
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_char(attrib, handle, value, plen, NULL, NULL); resp_begin(rsp_WRITE); resp_end(); } g_free(value); }
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); }
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; }
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); }