int texas_post(int fd, struct termios *ti) { int dev_id, dd, ret = 0; sleep(1); dev_id = ioctl(fd, HCIUARTGETDEVICE, 0); if (dev_id < 0) { perror("cannot get device id"); return -1; } DPRINTF("\nAdded device hci%d\n", dev_id); dd = hci_open_dev(dev_id); if (dd < 0) { perror("HCI device open failed"); return -1; } if (ioctl(dd, HCIDEVUP, dev_id) < 0 && errno != EALREADY) { fprintf(stderr, "Can't init device hci%d: %s (%d)", dev_id, strerror(errno), errno); hci_close_dev(dd); return -1; } ret = brf_do_script(dd, NULL, ti, NULL); hci_close_dev(dd); return ret; }
void close_hci_device(struct hci_state current_hci_state) { if(current_hci_state.state == HCI_STATE_OPEN) { hci_close_dev(current_hci_state.device_handle); } }
int texas_post(int fd, struct termios *ti) { int dev_id, dd, ret = 0; sleep(1); dev_id = ioctl(fd, HCIUARTGETDEVICE, 0); if (dev_id < 0) { perror("cannot get device id"); return -1; } DPRINTF("\nAdded device hci%d\n", dev_id); dd = hci_open_dev(dev_id); if (dd < 0) { perror("HCI device open failed"); return -1; } ret = brf_do_script(dd, ti, NULL); hci_close_dev(dd); return ret; }
int bt_get_remote_name(char *str_bdaddr) { struct hci_conn_info_req cr; int dd, cc, handler; char name[248]; bdaddr_t bdaddr; if ((dd = hci_open_dev(device)) < 0) { fprintf(stderr, "bluesnarfer: hci_open_dev : %s\n", strerror(errno)); return -1; } str2ba(str_bdaddr, &bdaddr); memcpy(&cr.bdaddr, &bdaddr, sizeof(bdaddr_t)); cr.type = ACL_LINK; if (ioctl(dd, HCIGETCONNINFO, (unsigned long) &cr) < 0) { if ((cc = hci_create_connection(dd, &bdaddr, htobs(HCI_DM1 | HCI_DH1), 0, 0, (void *)&handler, 25000)) < 0) { fprintf(stderr, "bluesnarfer: hci_create_connection failed\n"); hci_close_dev(dd); return -1; } } if (hci_read_remote_name(dd, &bdaddr, 248, name, 25000)) { fprintf(stderr, "bluesnarfer: hci_read_remote_name failed\n"); hci_close_dev(dd); hci_disconnect(dd, handler, HCI_OE_USER_ENDED_CONNECTION, 10000); return -1; } printf("device name: %s\n", name); if (cc) hci_disconnect(dd, handler, HCI_OE_USER_ENDED_CONNECTION, 10000); hci_close_dev(dd); return 0; }
VALUE method_stop_advertising(VALUE klass, VALUE rb_device_id) { int device_id = FIX2INT(rb_device_id); int device_handle = hci_open_dev(device_id); hci_le_set_advertise_enable(device_handle, 0x00, 1000); hci_close_dev(device_handle); return Qnil; }
void btcmd_stop_scanning(int hci_dev) { setsockopt(hci_dev, SOL_HCI, HCI_FILTER, &old_filter, sizeof(old_filter)); hci_le_set_scan_enable(hci_dev, 0x00, filter_dup, 2000); hci_close_dev(hci_dev); return; }
int csr_open_hci(char *device) { struct hci_dev_info di; struct hci_version ver; int dev = 0; if (device) { dev = hci_devid(device); if (dev < 0) { fprintf(stderr, "Device not available\n"); return -1; } } dd = hci_open_dev(dev); if (dd < 0) { fprintf(stderr, "Can't open device hci%d: %s (%d)\n", dev, strerror(errno), errno); return -1; } if (hci_devinfo(dev, &di) < 0) { fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n", dev, strerror(errno), errno); hci_close_dev(dd); return -1; } if (hci_read_local_version(dd, &ver, 1000) < 0) { fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n", dev, strerror(errno), errno); hci_close_dev(dd); return -1; } if (ver.manufacturer != 10) { fprintf(stderr, "Unsupported manufacturer\n"); hci_close_dev(dd); return -1; } return 0; }
void close_hci_socket(hci_socket_t *hci_socket) { if (hci_socket->sock < 0) { print_trace(TRACE_WARNING, "close_hci_socket : already closed socket.\n"); return; } hci_close_dev(hci_socket->sock); hci_socket->sock = -1; return; }
int main(int argc, char **argv) { int dev_id; int err, opt, dd; uint8_t own_type = 0x00; uint8_t scan_type = 0x01; uint8_t filter_type = 0; uint8_t filter_policy = 0x00; uint16_t interval = htobs(0x0010); uint16_t window = htobs(0x0010); // uint8_t filter_dup = 1; uint8_t filter_dup = 0; // don't filter duplicate dev_id = hci_get_route(NULL); dd = hci_open_dev( dev_id ); if (dev_id < 0 || dd < 0) { perror("opening socket"); exit(1); } err = hci_le_set_scan_parameters(dd, scan_type, interval, window, own_type, filter_policy, 1000); if (err < 0) { perror("Set scan parameters failed"); } err = hci_le_set_scan_enable(dd, 0x01, filter_dup, 1000); if (err < 0) { perror("Enable scan failed"); exit(1); } printf("LE Scan ... \n"); err = print_advertising_devices(dd, filter_type); if (err < 0) { perror("Could not receive advertising events"); exit(1); } err = hci_le_set_scan_enable(dd, 0x00, filter_dup, 1000); if (err < 0) { perror("Disable scan failed"); exit(1); } hci_close_dev(dd); return 0; }
/** * @brief Stop low energy scan * * @param device_handle * * @return */ static int stop_lescan(int device_handle) { uint8_t filter_dup = 0; int err = hci_le_set_scan_enable(device_handle, 0x00, filter_dup, 1000); if (err < 0) { perror("Disable scan failed"); return -1; } hci_close_dev(device_handle); return 0; }
/** send_hciCmd Function * This function takes the hci commands for the BT chip configurations, creates * a hci channel to send the commadns through UART to configure BT chip * * Parameters : * @ dev_id : HCI device ID * @ command_length : Number of arguments of the command * @ command : Pointer to command list * Returns 0 upon success * , different error messages depending upon the error. */ static void send_hciCmd(int dev_id, int command_length, char **command) { unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf; struct hci_filter flt; hci_event_hdr *hdr; int i, opt, len, dd; uint16_t ocf; uint8_t ogf; if (dev_id < 0) dev_id = hci_get_route(NULL); errno = 0; ogf = strtol(command[0], NULL, 16); ocf = strtol(command[1], NULL, 16); for (i = 2, len = 0; i < command_length && len < sizeof(buf); i++, len++) *ptr++ = (uint8_t) strtol(command[i], NULL, 16); dd = hci_open_dev(dev_id); if (dd < 0) { perror("Device open failed"); return; } /* Setup filter */ hci_filter_clear(&flt); hci_filter_set_ptype(HCI_EVENT_PKT, &flt); hci_filter_all_events(&flt); if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { perror("HCI filter setup failed"); return; } /* Send the BT chip configuration commands */ if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) { perror("Send failed"); return; } /* Wait for the command completion event */ len = read(dd, buf, sizeof(buf)); if (len < 0) { perror("Read failed"); return; } hdr = (void *)(buf + 1); ptr = buf + (1 + HCI_EVENT_HDR_SIZE); len -= (1 + HCI_EVENT_HDR_SIZE); hci_close_dev(dd); }
/* * Close the connection to the HCI device. * * Parameters: * dd: A opened HCI device. * * Returns: * 0 on success or a negative error code (defined in error.h) on failure. */ static int close_hci_dev(int dd) { int errsv; /* saved errno */ if (hci_close_dev(dd) < 0) { errsv = errno; log_ec(errsv, "Can't close HCI device"); return hidc_convert_errno(errsv); } return 0; }
void update_service_classes(const bdaddr_t *bdaddr, uint8_t value) { struct hci_dev_list_req *dl; struct hci_dev_req *dr; int i, sk; sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (sk < 0) return; dl = g_malloc0(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; if (ioctl(sk, HCIGETDEVLIST, dl) < 0) { close(sk); g_free(dl); return; } dr = dl->dev_req; for (i = 0; i < dl->dev_num; i++, dr++) { struct hci_dev_info di; uint8_t cls[3]; int dd; if (hci_devinfo(dr->dev_id, &di) < 0) continue; if (hci_test_bit(HCI_RAW, &di.flags)) continue; if (get_device_class(di.dev_id, cls) < 0) continue; dd = hci_open_dev(di.dev_id); if (dd < 0) continue; set_service_classes(dd, cls, value); hci_close_dev(dd); update_adapter(di.dev_id); } g_free(dl); close(sk); }
VALUE stop_scan(VALUE device_id_in) { int device_id = FIX2INT(device_id_in); int device_handle = device_handles[device_id]; // put back the old filter setsockopt(device_handle, SOL_HCI, HCI_FILTER, &stored_filters[device_id], sizeof(stored_filters[0])); // stop scanning hci_le_set_scan_enable(device_handle, 0x00, 0x00, 1000); hci_close_dev(device_handle); return Qnil; }
void encryption(int dev_id, uint16_t handle) { if (BDADDR) str2ba(BDADDR, &bdaddr); if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); if (hci_encrypt_link(typ.dd, handle, 1, 25000) < 0) die("HCI set encryption request failed\n"); hci_close_dev(typ.dd); }
void disconnect_from_device(int dev_id, uint16_t handle) { if (dev_id < 0) dev_id = hci_get_route(NULL); if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); typ.err = hci_disconnect(typ.dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000); if (typ.err < 0) die("Could not disconnect\n"); hci_close_dev(typ.dd); }
static int hciops_powered(int index, gboolean powered) { int dd, err; uint8_t mode = SCAN_DISABLED; if (powered) return hciops_start(index); dd = hci_open_dev(index); if (dd < 0) return -EIO; err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &mode); if (err < 0) { err = -errno; hci_close_dev(dd); return err; } hci_close_dev(dd); return hciops_stop(index); }
static int hciops_connectable(int index) { int dd; uint8_t mode = SCAN_PAGE; dd = hci_open_dev(index); if (dd < 0) return -EIO; hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &mode); hci_close_dev(dd); return 0; }
static int hciops_discoverable(int index) { int dd; uint8_t mode = (SCAN_PAGE | SCAN_INQUIRY); dd = hci_open_dev(index); if (dd < 0) return -EIO; hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &mode); hci_close_dev(dd); return 0; }
int main(int argc, char** argv) { if (argc != 2) { std::cout << "argument required" << std::endl; return EXIT_FAILURE; } else { int dev_id; dev_id = hci_get_route(NULL); if (dev_id < 0) { perror("Device is not available"); exit(1); } std::cout << "connecting to: " << argv[1] << std::endl; bdaddr_t bdaddr; str2ba(argv[1], &bdaddr); int dd; dd = hci_open_dev(dev_id); if (dd < 0) { perror("hci_open_dev() failed"); exit(1); } if (hci_authenticate_link(dd, htobs(cr->conn_info->handle), 25000) < 0) { perror("HCI authentication request failed"); exit(1); } if (hci_close_dev(dd) < 0) { perror("hci_close_dev()"); exit(1); } return 0; } }
int read_rssi(int dev_id, uint16_t handle) { int8_t rssi; if (BDADDR) str2ba(BDADDR, &bdaddr); if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); if (hci_read_rssi(typ.dd, handle, &rssi, 1000) < 0) die("Read RSSI failed\n"); hci_close_dev(typ.dd); return rssi; }
static int l2cap_bluez_set_flush_timeout(int channel, int timeout_ms) { int result = 0, dd; struct hci_request rq = { 0 }; write_link_supervision_timeout_cp cmd_param; write_link_supervision_timeout_rp cmd_response; if ((dd = hci_open_dev(channels.channels[channel].devid)) < 0) { perror("hci_open_dev"); return -1; } // build a command packet to send to the bluetooth microcontroller cmd_param.handle = channels.channels[channel].handle; cmd_param.timeout = htobs(timeout_ms/BT_SLOT); rq.ogf = OGF_HOST_CTL; rq.ocf = OCF_WRITE_AUTOMATIC_FLUSH_TIMEOUT; rq.cparam = &cmd_param; rq.clen = sizeof(cmd_param); rq.rparam = &cmd_response; rq.rlen = sizeof(cmd_response); rq.event = EVT_CMD_COMPLETE; // send the command and wait for the response if( hci_send_req( dd, &rq, 0 ) < 0) { perror("hci_send_req"); result = -1; } else if( cmd_response.status ) { errno = bt_error(cmd_response.status); perror("failed to set flush timeout"); result = -1; } hci_close_dev(dd); return result; }
static int bt_sock_name2bth(int devid, const char *btname, bdaddr_t * btaddr) { int i, niinf, dd, err = -1; inquiry_info *piinf = NULL; FILE *file; char cfname[256]; if (bt_sock_cache_lookup(btname, btaddr) == 0) return 0; bt_sock_cachefile(cfname, sizeof(cfname) - 1); if ((file = fopen(cfname, "r+t")) == NULL && (file = fopen(cfname, "w+t")) == NULL) return -1; fprintf(stderr, "Resolving name '%s' ...\n", btname); if ((niinf = hci_inquiry(devid, 32, -1, NULL, &piinf, 0)) < 0) { fprintf(stderr, "hci_inquiry error\n"); fclose(file); return -1; } if ((dd = hci_open_dev(devid)) < 0) { fprintf(stderr, "Unable to open HCI device hci%d\n", devid); free(piinf); fclose(file); return -1; } for (i = 0; i < niinf; i++) { char devname[128]; if (hci_read_remote_name(dd, &piinf[i].bdaddr, sizeof(devname) - 1, devname, 100000) >= 0) { if (strcasecmp(devname, btname) == 0) { *btaddr = piinf[i].bdaddr; err = 0; } bt_sock_cache_add(file, devname, &piinf[i].bdaddr); } } hci_close_dev(dd); free(piinf); fclose(file); return err; }
static void device_devup_setup(int index) { struct hci_dev_info di; uint16_t policy; int dd, err; if (hci_devinfo(index, &di) < 0) return; if (hci_test_bit(HCI_RAW, &di.flags)) return; dd = hci_open_dev(index); if (dd < 0) { err = errno; error("Can't open device hci%d: %s (%d)", index, strerror(err), err); return; } /* Set page timeout */ if ((main_opts.flags & (1 << HCID_SET_PAGETO))) { write_page_timeout_cp cp; cp.timeout = htobs(main_opts.pageto); hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_PAGE_TIMEOUT, WRITE_PAGE_TIMEOUT_CP_SIZE, &cp); } /* Set default link policy */ policy = htobs(main_opts.link_policy); hci_send_cmd(dd, OGF_LINK_POLICY, OCF_WRITE_DEFAULT_LINK_POLICY, 2, &policy); hci_close_dev(dd); start_security_manager(index); /* Return value 1 means ioctl(DEVDOWN) was performed */ if (manager_start_adapter(index) == 1) stop_security_manager(index); }
uint16_t connect_to_device(int dev_id) { uint16_t interval, latency, max_ce_length, max_interval, min_ce_length; uint16_t min_interval, supervision_timeout, window, handle; uint8_t initiator_filter, own_bdaddr_type; initiator_filter = 0x01; /* Use white list */ if (dev_id < 0) dev_id = hci_get_route(NULL); if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); memset(&bdaddr, 0, sizeof(bdaddr_t)); if (BDADDR) str2ba(BDADDR, &bdaddr); interval = htobs(0x0004); window = htobs(0x0004); own_bdaddr_type = 0x00; min_interval = htobs(0x000F); max_interval = htobs(0x000F); latency = htobs(0x0000); supervision_timeout = htobs(0x0C80); min_ce_length = htobs(0x0001); max_ce_length = htobs(0x0001); typ.err = hci_le_create_conn(typ.dd, interval, window, initiator_filter, bdaddr_type, bdaddr, own_bdaddr_type, min_interval, max_interval, latency, supervision_timeout, min_ce_length, max_ce_length, &handle, 25000); if (typ.err < 0) die("Could not create connection\n"); hci_close_dev(typ.dd); return handle; }
static int l2cap_bluez_disconnect(int channel) { int result = 0; int dd; if ((dd = hci_open_dev(channels.channels[channel].devid)) < 0) { perror("hci_open_dev"); return -1; } if(hci_disconnect(dd, channels.channels[channel].handle, HCI_OE_USER_ENDED_CONNECTION, 5*HCI_REQ_TIMEOUT) < 0) { perror("hci_disconnect"); result = -1; } hci_close_dev(dd); return result; }
void add_to_white_list(int dev_id) { if (dev_id < 0) dev_id = hci_get_route(NULL); if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); if (BDADDR) str2ba(BDADDR, &bdaddr); typ.err = hci_le_add_white_list(typ.dd, &bdaddr, bdaddr_type, 1000); hci_close_dev(typ.dd); if (typ.err < 0) { typ.err = -errno; die("Can't add to white list!\n"); } }
static int hciops_stop(int index) { int dd; int err = 0; dd = hci_open_dev(index); if (dd < 0) return -EIO; if (ioctl(dd, HCIDEVDOWN, index) == 0) goto done; /* on success */ if (errno != EALREADY) { err = errno; error("Can't stop device hci%d: %s (%d)", index, strerror(err), err); } done: hci_close_dev(dd); return -err; }
float get_page_timeout(int hdev) { struct hci_request rq; int s; float ret; if ((s = hci_open_dev(hdev)) < 0) { ret = ERR_HCI_DEV_OPEN_FAILED; goto out; } memset(&rq, 0, sizeof(rq)); uint16_t timeout; read_page_timeout_rp rp; rq.ogf = OGF_HOST_CTL; rq.ocf = OCF_READ_PAGE_TIMEOUT; rq.rparam = &rp; rq.rlen = READ_PAGE_TIMEOUT_RP_SIZE; if (hci_send_req(s, &rq, 1000) < 0) { ret = ERR_CANT_READ_PAGE_TIMEOUT; goto out; } if (rp.status) { ret = ERR_READ_PAGE_TIMEOUT; goto out; } timeout = btohs(rp.timeout); ret = ((float)timeout * 0.625); out: if (s >= 0) hci_close_dev(s); return ret; }
VALUE method_devices() { struct hci_dev_list_req *dl; struct hci_dev_req *dr; struct hci_dev_info di; int i; int ctl; VALUE devices; if (!(dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)))) { rb_raise(rb_eException, "Can't allocate memory"); return Qnil; } dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) { rb_raise(rb_eException, "Can't get device list"); return Qnil; } devices = rb_ary_new(); for (i = 0; i< dl->dev_num; i++) { di.dev_id = (dr+i)->dev_id; if (ioctl(ctl, HCIGETDEVINFO, (void *) &di) < 0) continue; if (hci_test_bit(HCI_RAW, &di.flags) && !bacmp(&di.bdaddr, BDADDR_ANY)) { int dd = hci_open_dev(di.dev_id); hci_read_bd_addr(dd, &di.bdaddr, 1000); hci_close_dev(dd); } rb_ary_push(devices, di_to_hash(&di)); } return devices; }