/******************************************************************************* ** ** Function gatt_act_connect ** ** Description GATT connection initiation. ** ** Returns void. ** *******************************************************************************/ BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr) { BOOLEAN ret = FALSE; tGATT_TCB *p_tcb; UINT8 st; GATT_TRACE_DEBUG0("gatt_act_connect"); if ((p_tcb = gatt_find_tcb_by_addr(bd_addr)) != NULL) { ret = TRUE; st = gatt_get_ch_state(p_tcb); /* before link down, another app try to open a GATT connection */ if(st == GATT_CH_OPEN && gatt_num_apps_hold_link(p_tcb) == 0 && /* only connection on fix channel when the l2cap channel is already open */ p_tcb->att_lcid == L2CAP_ATT_CID ) { if (!gatt_connect(bd_addr, p_tcb)) ret = FALSE; } else if(st == GATT_CH_CLOSING) { /* need to complete the closing first */ ret = FALSE; } } else { if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr)) != NULL) { if (!gatt_connect(bd_addr, p_tcb)) { GATT_TRACE_ERROR0("gatt_connect failed"); memset(p_tcb, 0, sizeof(tGATT_TCB)); } else ret = TRUE; } else { ret = 0; GATT_TRACE_ERROR1("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if); } } if (ret) { gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE); } return ret; }
/******************************************************************************* ** ** Function gatt_act_connect ** ** Description GATT connection initiation. ** ** Returns void. ** *******************************************************************************/ BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr, tBT_TRANSPORT transport) { BOOLEAN ret = FALSE; tGATT_TCB *p_tcb; UINT8 st; if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL) { ret = TRUE; st = gatt_get_ch_state(p_tcb); /* before link down, another app try to open a GATT connection */ if(st == GATT_CH_OPEN && gatt_num_apps_hold_link(p_tcb) == 0 && transport == BT_TRANSPORT_LE ) { if (!gatt_connect(bd_addr, p_tcb, transport)) ret = FALSE; } else if(st == GATT_CH_CLOSING) { /* need to complete the closing first */ ret = FALSE; } } else { if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport)) != NULL) { if (!gatt_connect(bd_addr, p_tcb, transport)) { GATT_TRACE_ERROR("gatt_connect failed"); memset(p_tcb, 0, sizeof(tGATT_TCB)); } else ret = TRUE; } else { ret = 0; GATT_TRACE_ERROR("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if); } } if (ret) { gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE); } return ret; }
static void cmd_connect(int argcp, char **argvp) { GError *gerr=NULL; if (conn_state != STATE_DISCONNECTED) return; if (argcp > 1) { g_free(opt_dst); opt_dst = g_strdup(argvp[1]); g_free(opt_dst_type); if (argcp > 2) opt_dst_type = g_strdup(argvp[2]); else opt_dst_type = g_strdup("public"); } if (opt_dst == NULL) { resp_error(err_BAD_PARAM); return; } set_state(STATE_CONNECTING); iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb,&gerr); if (iochannel == NULL) set_state(STATE_DISCONNECTED); else g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL); }
static void cmd_connect(int argcp, char **argvp) { GError *gerr = NULL; if (conn_state != STATE_DISCONNECTED) return; if (argcp > 1) { g_free(opt_dst); opt_dst = g_strdup(argvp[1]); g_free(opt_dst_type); if (argcp > 2) opt_dst_type = g_strdup(argvp[2]); else opt_dst_type = g_strdup("public"); } if (opt_dst == NULL) { error("Remote Bluetooth address required\n"); return; } rl_printf("Attempting to connect to %s\n", opt_dst); set_state(STATE_CONNECTING); iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr); if (iochannel == NULL) { set_state(STATE_DISCONNECTED); error("%s\n", gerr->message); g_error_free(gerr); } else g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL); }
static void cmd_connect(int argcp, char **argvp) { if (conn_state != STATE_DISCONNECTED) goto done; if (argcp > 1) { g_free(opt_dst); opt_dst = g_strdup(argvp[1]); } if (opt_dst == NULL) { printf("Remote Bluetooth address required\n"); goto done; } set_state(STATE_CONNECTING); iochannel = gatt_connect(opt_src, opt_dst, opt_sec_level, opt_psm, opt_mtu, connect_cb); if (iochannel == NULL) set_state(STATE_DISCONNECTED); else return; done: printf("\r%s", get_prompt()); fflush(stdout); }
void static myconnect() { GError *gerr = NULL; opt_dst_type = g_strdup("random"); rl_printf("Attempting to connect to %s\n", opt_dst); set_state(STATE_CONNECTING); iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr); if (iochannel == NULL) { set_state(STATE_DISCONNECTED); error("%s\n", gerr->message); g_error_free(gerr); } else g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL); }
//function to actually make the connection void *new_ble_connection(void *opt_dst_void){ loop = g_main_loop_new (NULL, FALSE); //Connect to address char *opt_src = NULL; char *opt_dst = (char*)opt_dst_void; char *opt_dst_type = "public"; char *opt_sec_level = "low"; int opt_psm = 0; int opt_mtu = 0; iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb, &connect_err); if(iochannel == NULL){ printf("iochannel error\n"); } g_main_loop_run(loop); printf("done main loop\n"); return NULL; }
int main(int argc, char *argv[]) { GIOChannel *pchan; gint events; opt_sec_level = g_strdup("low"); opt_src = NULL; //opt_dst = NULL; opt_dst_type = g_strdup("public"); event_loop = g_main_loop_new(NULL, FALSE); pchan = g_io_channel_unix_new(fileno(stdin)); g_io_channel_set_close_on_unref(pchan, TRUE); events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL; g_io_add_watch(pchan, events, prompt_read, NULL); GError *gerr = NULL; printf("Connecting to.. %s\n", argv[1]); iochannel = gatt_connect(opt_src, argv[1], opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr); if (iochannel == NULL) { g_error_free(gerr); } else g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL); g_main_loop_run(event_loop); fflush(stdout); g_io_channel_unref(pchan); g_main_loop_unref(event_loop); g_free(opt_src); //g_free(opt_dst); g_free(opt_sec_level); mgmt_unregister_index(mgmt_master, mgmt_ind); mgmt_cancel_index(mgmt_master, mgmt_ind); mgmt_unref(mgmt_master); mgmt_master = NULL; return EXIT_SUCCESS; }
/******************************************************************************* ** ** Function gatt_act_connect ** ** Description GATT connection initiation. ** ** Returns void. ** *******************************************************************************/ BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr) { BOOLEAN ret = FALSE; tGATT_TCB *p_tcb; GATT_TRACE_DEBUG0("gatt_act_connect"); if ((p_tcb = gatt_find_tcb_by_addr(bd_addr)) != NULL) { ret = TRUE; if(gatt_get_ch_state(p_tcb) == GATT_CH_CLOSING ) { /* need to complete the closing first */ ret = FALSE; } } else { if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr)) != NULL) { if (!gatt_connect(bd_addr, p_tcb)) { GATT_TRACE_ERROR0("gatt_connect failed"); memset(p_tcb, 0, sizeof(tGATT_TCB)); } else ret = TRUE; } else { ret = 0; GATT_TRACE_ERROR1("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if); } } if (ret) { gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE); } return ret; }
static void cmd_connect(int argcp, char **argvp) { if (conn_state != STATE_DISCONNECTED) return; if (argcp > 1) { g_free(opt_dst); opt_dst = g_strdup(argvp[1]); } if (opt_dst == NULL) { printf("Remote Bluetooth address required\n"); return; } set_state(STATE_CONNECTING); iochannel = gatt_connect(opt_src, opt_dst, opt_sec_level, opt_psm, opt_mtu, connect_cb); if (iochannel == NULL) set_state(STATE_DISCONNECTED); }
int main(int argc, char *argv[]) { GOptionContext *context; GOptionGroup *gatt_group, *params_group, *char_rw_group; GError *gerr = NULL; GIOChannel *chan; opt_dst_type = g_strdup("public"); 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_dst_type, opt_psm); goto done; } if (opt_primary) operation = primary; else if (opt_characteristics) operation = characteristics; else if (opt_char_read) operation = characteristics_read; else if (opt_char_write) operation = characteristics_write; else if (opt_char_write_req) operation = characteristics_write_req; else if (opt_char_desc) operation = 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_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb); if (chan == NULL) { got_error = TRUE; goto done; } event_loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(event_loop); g_main_loop_unref(event_loop); 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); }