Exemplo n.º 1
1
/*
Scans for nearby bluetooth devices. It looks for a maximum of max_devices to be nearby.
The timeout for the discovery is set to (1.28*inquiry_length) seconds. Finally,
a reference to a discovered_dev_t (see scanner.h) array is passed, used to store the 
results of the scanning.

@param max_devices The number (maximum) of devices that can be discovered.
@param inquiry_length Time to wait for nearby devices to respond.
@param nearby Datastructure to hold information about the nearby/discovered devices.
@return nr_nearby The actual number of devices found nearby.
*/
int scan_nearby(int max_devices, int inquiry_length, discovered_dev_t* nearby){

	int adapter_id = hci_get_route(NULL);
	if(adapter_id < 0){
		log_event("<scan_nearby>", "Cannot access bluetooth device (maybe off?)", WARN);
		return -1;
	}

	int socket = hci_open_dev(adapter_id);
	
	if(socket < 0){
		log_event("<scan_nearby>", "Error opening socket", ERRO);
		exit(EXIT_ERR_OPEN_SOCKET);
	}

	// Setting parameters for discovery.
	int status; 					// -1 if any error, else is set by HCI_INQUIRY to number of devices discovered
	long flags = IREQ_CACHE_FLUSH;
	inquiry_info* inq_inf = NULL; 	// will store data for discovered devices.
	
	inq_inf = (inquiry_info*)malloc(max_devices*sizeof(inquiry_info));
	if (inq_inf == NULL){
		log_event("<scan_nearby>", "Error allocating resources for inquiry_info", ERRO);
		exit(EXIT_ERR_ALLOC_MEM);
	}

	status = hci_inquiry(adapter_id, inquiry_length, max_devices, NULL, &inq_inf,flags );
	if(status < 0){
		log_event("<scan_nearby>", "Error hci_inquiry", ERRO);
		exit(EXIT_ERR_HQI_INQUIRY);
	}
	
	int nr_nearby = status;
	int i;
	char name[20];

	for(i = 0; i < nr_nearby; i++){
		discovered_dev_t nearby_dev;
		memset(name,0,sizeof(name));
		bdaddr_t mac_addr = inq_inf[i].bdaddr;
		ba2str(&mac_addr,nearby_dev.addr);
		
		// try to get a name for the device ...
		status = hci_read_remote_name(socket, &mac_addr, sizeof(name),name,0);
		if(status < 0){
			strcpy(nearby_dev.name,"Unknown");
		}else{
			strcpy(nearby_dev.name,name);
		}

		nearby[i] = nearby_dev;

	}

	close(socket);
	free(inq_inf);
	return nr_nearby;

}
Exemplo n.º 2
0
static void cmd_revision(int ctl, int hdev, char *opt)
{
  struct hci_version ver;
  int dd;

  dd = hci_open_dev(hdev);
  if (dd < 0) {
    fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
            hdev, strerror(errno), errno);
    return;
  }

  if (hci_read_local_version(dd, &ver, 1000) < 0) {
    fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
            hdev, strerror(errno), errno);
    return;
  }

  switch (ver.manufacturer) {
  case 10:
    print_rev_csr(dd, ver.hci_rev);
    break;
  default:
    break;
  }
  return;
}
struct hci_state open_default_hci_device()
{
  struct hci_state current_hci_state = {0};

  current_hci_state.device_id = hci_get_route(NULL);

  if((current_hci_state.device_handle = hci_open_dev(current_hci_state.device_id)) < 0) 
  {
    current_hci_state.has_error = TRUE;
    snprintf(current_hci_state.error_message, sizeof(current_hci_state.error_message), "Could not open device: %s", strerror(errno));
    return current_hci_state;
  }

  // Set fd non-blocking
  int on = 1;
  if(ioctl(current_hci_state.device_handle, FIONBIO, (char *)&on) < 0)
  {
    current_hci_state.has_error = TRUE;
    snprintf(current_hci_state.error_message, sizeof(current_hci_state.error_message), "Could set device to non-blocking: %s", strerror(errno));
    return current_hci_state;
  }

  current_hci_state.state = HCI_STATE_OPEN;

  return current_hci_state;
}
Exemplo n.º 4
0
int bt_disconnect(char bdaddr[18])
{
  int err = 0, dd;
  struct hci_conn_info_req *cr = 0;

  // find the connection handle to the specified bluetooth device
  cr = (struct hci_conn_info_req*) malloc(
      sizeof(struct hci_conn_info_req) + sizeof(struct hci_conn_info));
  str2ba(bdaddr, &cr->bdaddr);
  cr->type = ACL_LINK;
  dd = hci_open_dev(hci_get_route(&cr->bdaddr));
  if (dd < 0)
  {
    err = dd;
    goto cleanup;
  }
  err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr);
  if (err)
    goto cleanup;

  hci_disconnect(dd, cr->conn_info->handle, HCI_OE_USER_ENDED_CONNECTION, HCI_REQ_TIMEOUT);

  cleanup: free(cr);
  if (dd >= 0)
    close(dd);

  return err;
}
Exemplo n.º 5
0
/**
* @brief Start low energy scan
*
* @param device_id
*
* @return 
*/
static int start_lescan(int device_id)
{
  int device_handle = 0;
  if((device_handle = hci_open_dev(device_id)) < 0)
  {
    perror("Could not open device");
    return -1;
  }
  uint8_t filter_policy = 0x00;
  uint16_t interval = htobs(0x0010);
  uint16_t window = htobs(0x0010);

  int err = hci_le_set_scan_parameters(device_handle, 0x01, interval, window, 0x00, filter_policy, 1000);
  if (err < 0) {
  	perror("Set scan parameters failed");
	return -1;
  }

  err = hci_le_set_scan_enable(device_handle, 0x01, 0, 1000);
  if (err < 0) {
 	perror("Enable scan failed");
	return -1;
  }
  return device_handle;
}
Exemplo n.º 6
0
/**
 *	@brief Find a wiimote or wiimotes.
 *
 *	@param wm			An array of wiimote_t structures.
 *	@param max_wiimotes	The number of wiimote structures in \a wm.
 *	@param timeout		The number of seconds before the search times out.
 *
 *	@return The number of wiimotes found.
 *
 *	@see wiimote_connect()
 *
 *	This function will only look for wiimote devices.						\n
 *	When a device is found the address in the structures will be set.		\n
 *	You can then call wiimote_connect() to connect to the found				\n
 *	devices.
 */
int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) {
	int device_id;
	int device_sock;
	int found_devices;
	int found_wiimotes;

	/* reset all wiimote bluetooth device addresses */
	for (found_wiimotes = 0; found_wiimotes < max_wiimotes; ++found_wiimotes)
		wm[found_wiimotes]->bdaddr = *BDADDR_ANY;
	found_wiimotes = 0;

	/* get the id of the first bluetooth device. */
	device_id = hci_get_route(NULL);
	if (device_id < 0) {
		perror("hci_get_route");
		return 0;
	}

	/* create a socket to the device */
	device_sock = hci_open_dev(device_id);
	if (device_sock < 0) {
		perror("hci_open_dev");
		return 0;
	}

	inquiry_info scan_info_arr[128];
	inquiry_info* scan_info = scan_info_arr;
	memset(&scan_info_arr, 0, sizeof(scan_info_arr));

	/* scan for bluetooth devices for 'timeout' seconds */
	found_devices = hci_inquiry(device_id, timeout, 128, NULL, &scan_info, IREQ_CACHE_FLUSH);
	if (found_devices < 0) {
		perror("hci_inquiry");
		return 0;
	}

	WIIUSE_INFO("Found %i bluetooth device(s).", found_devices);

	int i = 0;

	/* display discovered devices */
	for (; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
		if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) &&
			(scan_info[i].dev_class[1] == WM_DEV_CLASS_1) &&
			(scan_info[i].dev_class[2] == WM_DEV_CLASS_2))
		{
			/* found a device */
			ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str);

			WIIUSE_INFO("Found wiimote (%s) [id %i].", wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid);

			wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr;
			WIIMOTE_ENABLE_STATE(wm[found_wiimotes], WIIMOTE_STATE_DEV_FOUND);
			++found_wiimotes;
		}
	}

	close(device_sock);
	return found_wiimotes;
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
int devname(char *pAddress, char *pName, int sz ) 
{
  bdaddr_t bdaddr;
  //char name[248]
  int dd;

  str2ba( pAddress, &bdaddr );

  int dev_id;
  dev_id = hci_get_route( &bdaddr );

  if ( dev_id < 0 ) {
    printf("Device not available\n");
    return -1;
  }
  
  dd = hci_open_dev(dev_id);
  if (dd < 0) {
    printf("HCI device open failed\n");
    return -2;
  }

  if ( hci_read_remote_name( dd, 
			     &bdaddr, 
			     sz, 
			     pName,
			     25000) != 0) {
    close(dd);
    printf("Could not find device %s\n", pAddress);
    return -3;
  }

  close(dd);
  return 0;
}
Exemplo n.º 9
0
int main(int argc,char **argv)
{
/*
    start_le_adv(0,-1);
    printf("le_adv started successful\n");
    printf("now try to stop adv\n");
    stop_le_adv(0,-1);
*/
    int dev_id = hci_get_route(NULL);
    if(dev_id < 0)
    {
        printf("hci_get_route faild\n");
        return 1;
    }

    int dd = hci_open_dev(dev_id);
//    stop_le_adv(0,-1);

    uint8_t cmd_data[32]= {0x1F};
    MYDATA mydata;
    memset(&mydata,0,sizeof(MYDATA));
    mydata.device_id = htobe64(123456789);
    mydata.length = 0x1E;
    mydata.magic_number = MAGIC_NUMBER_PERIPHERAL_TO_CENTRAL;

    sha1_hmac(KEY,strlen(KEY),(unsigned char *)&mydata.device_id,8,(unsigned char *)&mydata.checksum);

    memcpy(cmd_data+1,&mydata,sizeof(MYDATA));
    set_adv_data(dev_id,cmd_data,sizeof(cmd_data));

    start_le_adv(0,-1);
    lescan(dev_id,PERIPHERAL);
    return 0;
}
Exemplo n.º 10
0
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, ti, NULL);

	hci_close_dev(dd);

	return ret;
}
Exemplo n.º 11
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;
}
Exemplo n.º 12
0
HCI::HCI(const char *bdaddr) : device(bdaddr, 0) {
  _dev_id = hci_get_route(&this->bdaddr);

  _hci_sock = hci_open_dev(_dev_id);
  if (_dev_id < 0 || _hci_sock < 0) {
    err::code = err::LIB_SYS;
  }
}
Exemplo n.º 13
0
HCI::HCI() : device() {
  _dev_id = hci_get_route(nullptr);

  _hci_sock = hci_open_dev(_dev_id);
  if (_dev_id < 0 || _hci_sock < 0) {
    err::code = err::LIB_SYS;
  }

  hci_devba(_dev_id, &this->bdaddr);
}
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;
}
Exemplo n.º 15
0
int main(int argc, char *argv[]) {
	int dev_id;
	int ret;

	if (argc == 1) {
		dev_id = hci_get_route(NULL);
	} else if (argc == 2) {
		dev_id = hci_devid(argv[1]);
	} else {
		fprintf(stderr, "%s [<bluetooth-adapter>]\n", argv[0]);
		return 1;
	}
	if (dev_id < 0) {
		fprintf(stderr, "ERROR: Invalid device.\n");
		return 1;
	}

	LIST_INIT(&g_ble_connections);

	device_desc = hci_open_dev(dev_id);
	if (device_desc < 0) {
		fprintf(stderr, "ERROR: Could not open device.\n");
		return 1;
	}

	ret = ble_scan_enable(device_desc);
	if (ret != 0) {
		fprintf(stderr, "ERROR: Scanning fail.\n");
		return 1;
	}

	pthread_mutex_lock(&g_mutex);
	ret = ble_scan(device_desc, ble_discovered_device, BLE_SCAN_TIMEOUT);
	if (ret != 0) {
		fprintf(stderr, "ERROR: Advertisement fail.\n");
		return 1;
	}

	ble_scan_disable(device_desc);

	puts("Scan completed");
	pthread_mutex_unlock(&g_mutex);

	// Wait for the thread to complete
	while (g_ble_connections.lh_first != NULL) {
		struct connection_t* connection = g_ble_connections.lh_first;
		pthread_join(connection->thread, NULL);
		LIST_REMOVE(g_ble_connections.lh_first, entries);
		free(connection->addr);
		free(connection);
	}

	return 0;
}
Exemplo n.º 16
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);
}
Exemplo n.º 17
0
Arquivo: main.c Projeto: tazjel/QtSixA
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);
}
Exemplo n.º 18
0
/*
 * Open a connection to the HCI device.
 *
 * Returns:
 *     0 on success or a negative error code (defined in error.h) on failure.
 */
static int open_hci_dev()
{
	int errsv;  /* saved errno */
	int dd;

	if ((dd = hci_open_dev(hidc_get_app_dev_id())) < 0) {
		errsv = errno;
		log_ec(errsv, "Can't open HCI device");
		return hidc_convert_errno(errsv);
	}

	return dd;
}
Exemplo n.º 19
0
/**
 * Reads the device class. cls must be a uint8[3].
 */
int read_device_class(int hdev, uint8_t *cls) {
	int s = hci_open_dev(hdev);
	if (s < 0) {
		LOGE("Cannot open device hci%d: %s (%d)\n", hdev, strerror(errno), errno);
		return errno;
	}

	if (hci_read_class_of_dev(s, cls, 1000) < 0) {
		LOGE("Cannot read class of device hci%d: %s (%d)\n", hdev, strerror(errno), errno);
		return errno;
	}
	return 0;
}
Exemplo n.º 20
0
VALUE method_scan(int argc, VALUE *argv, VALUE klass)
{
  VALUE rb_device_id;
  int device_id;
  int device_handle;
  uint8_t scan_type = 0x01; //passive
  uint8_t own_type = 0x00; // I think this specifies not to use a random MAC
  uint8_t filter_dups = 0x00;
  uint8_t filter_policy = 0x00; // ?
  uint16_t interval = htobs(0x0005);
  uint16_t window = htobs(0x0005);

  struct hci_filter new_filter;
  socklen_t filter_size;

  // which device was specified?
  rb_scan_args(argc, argv, "01", &rb_device_id);
  if (rb_device_id == Qnil) {
    device_id = hci_get_route(NULL);
  } else {
    device_id = NUM2INT(rb_device_id);
  }
  // open the device
  if ( (device_handle = hci_open_dev(device_id)) < 0) {
    rb_raise(rb_eException, "Could not open device");
  }
  device_handles[device_id] = device_handle;
 
  // save the old filter so we can restore it later
  filter_size = sizeof(stored_filters[0]);
  if (getsockopt(device_handle, SOL_HCI, HCI_FILTER, &stored_filters[device_id], &filter_size) < 0) {
    rb_raise(rb_eException, "Could not get socket options");
  }

  // new filter to only look for event packets
  hci_filter_clear(&new_filter);
  hci_filter_set_ptype(HCI_EVENT_PKT, &new_filter);
  hci_filter_set_event(EVT_LE_META_EVENT, &new_filter);
  if (setsockopt(device_handle, SOL_HCI, HCI_FILTER, &new_filter, sizeof(new_filter)) < 0) {
    rb_raise(rb_eException, "Could not set socket options");
  }

  // set the params
  hci_le_set_scan_parameters(device_handle, scan_type, interval, window, own_type, filter_policy, 1000);
  hci_le_set_scan_enable(device_handle, 0x01, filter_dups, 1000);

  // perform the scan and make sure device gets put back into a proper state
  // even in the case of being interrupted by a ruby exception
  rb_ensure(perform_scan, INT2FIX(device_id), stop_scan, INT2FIX(device_id));
  return Qnil;
}
Exemplo n.º 21
0
int main(int argc, char* argv[])
{
	// An arr. of struct, later, contains devices data
	inquiry_info* devices = NULL;
	int max_rsp, num_rsp;
	int adapter_id, sock, len, flags;
	int i;
	// Temp variable to hold each addr and name of device
	char addr[19] = {0};
	char name[248] = {0};

	// Open bluetooth gate (to external env), and connect the socket
	adapter_id = hci_get_route(NULL);
	sock = hci_open_dev(adapter_id);
	if(adapter_id < 0 || sock < 0) {
		perror("opening socket");
		exit(1);
	}
	
	len = 8;
	max_rsp = 255;
	flags = IREQ_CACHE_FLUSH;
	devices = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
	
	// num_rsp: the number of remote device detected
	num_rsp = hci_inquiry(adapter_id, len, max_rsp, NULL, &devices, flags);
	if(num_rsp < 0) {
		perror("hci_inquiry");
	}
	
	for(i = 0; i < num_rsp; i++) {
		/*
		 * Convert struct bdaddr_t (contains only uchar[6]) into hexadec string.
		 * Other func, str2ba() do otherwise.
		 */
		ba2str(&(devices+i)->bdaddr, addr);
		memset(name, 0, sizeof(name));
		// See "not equal" (!=) comparison
		if(hci_read_remote_name(sock, &(devices+i)->bdaddr, sizeof(name), 
		                        name, 0) != 0) {
			strcpy(name, "[unknown]");
		}
		// Print device found
		printf("%s %s\n", addr, name);
	}
	
	free(devices);
	close(sock);
	
	return 0;
}
Exemplo n.º 22
0
size_t bro_bt_scan_devices (bro_bt_device_t *devices[MAX_BT_DEVICES])
{
    inquiry_info *scan_res = NULL;
    size_t num_rsp;
    int dev_id, sock, flags;
    int i;
    char addr[19] = { 0 };
    char name[248] = { 0 };
    
    flags = IREQ_CACHE_FLUSH;
    
    // Initializing BT Device for scan
    dev_id = hci_get_route(NULL);
    sock = hci_open_dev( dev_id );
    
    if (dev_id < 0 || sock < 0) {
      perror("opening socket");
      return -1;
    }

    memset(devices, 0, MAX_BT_DEVICES * sizeof(bro_bt_device_t *));
    
    // Allocate memory for all the devices found
    scan_res = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info));
    
    // Scan to find all the devices in range
    num_rsp = hci_inquiry(dev_id, BT_INQUIRY_LEN, MAX_BT_DEVICES, NULL,
                          &scan_res, flags);
    if( num_rsp < 0 ) {
        perror("hci_inquiry");
        return -1;
    }

    // For each of the found devices we retrieve its name
    for (i = 0; i < num_rsp; i++) {
      ba2str(&scan_res[i].bdaddr, addr);
      memset(name, 0, sizeof(name));
      if (hci_read_remote_name(sock, &scan_res[i].bdaddr, sizeof(name), 
          name, 0) < 0) {
        strcpy(name, "[unknown]");
      };
      //devices[i]->name = strdup(name);      // todo FREE on devices elements
      devices[i] = malloc(sizeof(bro_bt_device_t));
      strcpy(devices[i]->name, name);
      memcpy (&devices[i]->mac, &scan_res[i].bdaddr, sizeof(bdaddr_t));
    }

    free( scan_res );
    close( sock );
    return num_rsp;
}
Exemplo n.º 23
0
int bt_start_server(int *s_sock)
{
    int dev_id;

    dev_id  = hci_get_route(NULL);
    *s_sock = hci_open_dev(dev_id);

    if( dev_id < 0 || *s_sock < 0)
    {
        perror("server: error opening bt socket\n");
        return -1;
    }
    return dev_id;
}
Exemplo n.º 24
0
Arquivo: ble.c Projeto: dhn/ble
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);
}
Exemplo n.º 25
0
/**
 * Spoofs the device class. cls is a string in the format 0xffffff.
 */
int spoof_device_class(int hdev, char *cls) {
	int s = hci_open_dev(hdev);
	if (s < 0) {
		LOGE("Cannot open device hci%d: %s (%d)\n", hdev, strerror(errno), errno);
		return errno;
	}
	uint32_t cod = strtoul(cls, NULL, 16);
	if (hci_write_class_of_dev(s, cod, 2000) < 0) {
		LOGE("Cannor write class for hci%d: %s(%d)\n", hdev, strerror(errno), errno);
		return errno;
	}
	return 0;

}
Exemplo n.º 26
0
Arquivo: ble.c Projeto: dhn/ble
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);
}
Exemplo n.º 27
0
/*
 * \brief This function writes the device class for a given device number.
 *
 * \param device_number  the device number
 * \param devclass       the device class to write
 *
 * \return 0 if successful, -1 otherwise
 */
int bt_write_device_class(int device_number, uint32_t devclass)
{
  int ret = 0;

  int s = hci_open_dev (device_number);

  if(hci_write_class_of_dev(s, devclass, HCI_REQ_TIMEOUT) < 0)
  {
    ret = -1;
  }

  close(s);

  return ret;
}
Exemplo n.º 28
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;
}
Exemplo n.º 29
0
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;
}
Exemplo n.º 30
0
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;
}