/* 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; }
/* Poll Bluetooth and print result */ void BTD::HCI_task() { switch(hci_state) { case HCI_INIT_STATE: hci_counter++; if(hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events hci_reset(); hci_state = HCI_RESET_STATE; hci_counter = 0; } break; case HCI_RESET_STATE: hci_counter++; if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { hci_counter = 0; #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nHCI Reset complete"), 0x80); #endif hci_state = HCI_CLASS_STATE; hci_write_class_of_device(); } else if(hci_counter > hci_num_reset_loops) { hci_num_reset_loops *= 10; if(hci_num_reset_loops > 2000) hci_num_reset_loops = 2000; #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nNo response to HCI Reset"), 0x80); #endif hci_state = HCI_INIT_STATE; hci_counter = 0; } break; case HCI_CLASS_STATE: if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nWrite class of device"), 0x80); #endif hci_state = HCI_BDADDR_STATE; hci_read_bdaddr(); } break; case HCI_BDADDR_STATE: if(hci_check_flag(HCI_FLAG_READ_BDADDR)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80); for(int8_t i = 5; i > 0; i--) { D_PrintHex<uint8_t > (my_bdaddr[i], 0x80); Notify(PSTR(":"), 0x80); } D_PrintHex<uint8_t > (my_bdaddr[0], 0x80); #endif hci_read_local_version_information(); hci_state = HCI_LOCAL_VERSION_STATE; } break; case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class if(hci_check_flag(HCI_FLAG_READ_VERSION)) { if(btdName != NULL) { hci_set_local_name(btdName); hci_state = HCI_SET_NAME_STATE; } else hci_state = HCI_CHECK_DEVICE_SERVICE; } break; case HCI_SET_NAME_STATE: if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nThe name is set to: "), 0x80); NotifyStr(btdName, 0x80); #endif hci_state = HCI_CHECK_DEVICE_SERVICE; } break; case HCI_CHECK_DEVICE_SERVICE: if(pairWithHIDDevice || pairWithWii) { // Check if it should try to connect to a Wiimote #ifdef DEBUG_USB_HOST if(pairWithWii) Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller"), 0x80); else Notify(PSTR("\r\nPlease enable discovery of your device"), 0x80); #endif hci_inquiry(); hci_state = HCI_INQUIRY_STATE; } else hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote break; case HCI_INQUIRY_STATE: if(hci_check_flag(HCI_FLAG_DEVICE_FOUND)) { hci_inquiry_cancel(); // Stop inquiry #ifdef DEBUG_USB_HOST if(pairWithWii) Notify(PSTR("\r\nWiimote found"), 0x80); else Notify(PSTR("\r\nHID device found"), 0x80); Notify(PSTR("\r\nNow just create the instance like so:"), 0x80); if(pairWithWii) Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80); else Notify(PSTR("\r\nBTHID hid(&Btd);"), 0x80); Notify(PSTR("\r\nAnd then press any button on the "), 0x80); if(pairWithWii) Notify(PSTR("Wiimote"), 0x80); else Notify(PSTR("device"), 0x80); #endif if(motionPlusInside) { hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller hci_state = HCI_REMOTE_NAME_STATE; } else hci_state = HCI_CONNECT_DEVICE_STATE; } break; case HCI_CONNECT_DEVICE_STATE: if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { #ifdef DEBUG_USB_HOST if(pairWithWii) Notify(PSTR("\r\nConnecting to Wiimote"), 0x80); else Notify(PSTR("\r\nConnecting to HID device"), 0x80); #endif hci_connect(); hci_state = HCI_CONNECTED_DEVICE_STATE; } break; case HCI_CONNECTED_DEVICE_STATE: if(hci_check_flag(HCI_FLAG_CONNECT_EVENT)) { if(hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) { #ifdef DEBUG_USB_HOST if(pairWithWii) Notify(PSTR("\r\nConnected to Wiimote"), 0x80); else Notify(PSTR("\r\nConnected to HID device"), 0x80); #endif hci_authentication_request(); // This will start the pairing with the Wiimote hci_state = HCI_SCANNING_STATE; } else { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nTrying to connect one more time..."), 0x80); #endif hci_connect(); // Try to connect one more time } } break; case HCI_SCANNING_STATE: if(!connectToWii && !pairWithWii && !connectToHIDDevice && !pairWithHIDDevice) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80); #endif hci_write_scan_enable(); watingForConnection = true; hci_state = HCI_CONNECT_IN_STATE; } break; case HCI_CONNECT_IN_STATE: if(hci_check_flag(HCI_FLAG_INCOMING_REQUEST)) { watingForConnection = false; #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nIncoming Connection Request"), 0x80); #endif hci_remote_name(); hci_state = HCI_REMOTE_NAME_STATE; } else if(hci_check_flag(HCI_FLAG_DISCONNECT_COMPLETE)) hci_state = HCI_DISCONNECT_STATE; break; case HCI_REMOTE_NAME_STATE: if(hci_check_flag(HCI_FLAG_REMOTE_NAME_COMPLETE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nRemote Name: "), 0x80); for(uint8_t i = 0; i < strlen(remote_name); i++) Notifyc(remote_name[i], 0x80); #endif if(strncmp((const char*)remote_name, "Nintendo", 8) == 0) { incomingWii = true; #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nWiimote is connecting"), 0x80); #endif if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) { #ifdef DEBUG_USB_HOST Notify(PSTR(" with Motion Plus Inside"), 0x80); #endif motionPlusInside = true; } else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) { #ifdef DEBUG_USB_HOST Notify(PSTR(" - Wii U Pro Controller"), 0x80); #endif motionPlusInside = true; wiiUProController = true; } else { motionPlusInside = false; wiiUProController = false; } } if(classOfDevice[2] == 0 && classOfDevice[1] == 0x25 && classOfDevice[0] == 0x08 && strncmp((const char*)remote_name, "Wireless Controller", 19) == 0) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nPS4 controller is connecting"), 0x80); #endif incomingPS4 = true; } if(pairWithWii && motionPlusInside) hci_state = HCI_CONNECT_DEVICE_STATE; else { hci_accept_connection(); hci_state = HCI_CONNECTED_STATE; } } break; case HCI_CONNECTED_STATE: if(hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nConnected to Device: "), 0x80); for(int8_t i = 5; i > 0; i--) { D_PrintHex<uint8_t > (disc_bdaddr[i], 0x80); Notify(PSTR(":"), 0x80); } D_PrintHex<uint8_t > (disc_bdaddr[0], 0x80); #endif if(incomingPS4) connectToHIDDevice = true; // We should always connect to the PS4 controller // Clear these flags for a new connection l2capConnectionClaimed = false; sdpConnectionClaimed = false; rfcommConnectionClaimed = false; hci_event_flag = 0; hci_state = HCI_DONE_STATE; } break; case HCI_DONE_STATE: hci_counter++; if(hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started hci_counter = 0; hci_state = HCI_SCANNING_STATE; } break; case HCI_DISCONNECT_STATE: if(hci_check_flag(HCI_FLAG_DISCONNECT_COMPLETE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80); #endif hci_event_flag = 0; // Clear all flags // Reset all buffers memset(hcibuf, 0, BULK_MAXPKTSIZE); memset(l2capinbuf, 0, BULK_MAXPKTSIZE); connectToWii = incomingWii = pairWithWii = false; connectToHIDDevice = incomingHIDDevice = pairWithHIDDevice = false; incomingPS4 = false; hci_state = HCI_SCANNING_STATE; } break; default: break; } }
/* * Class: com_example_pmxota_OtaLoadJNI * Method: btDeviceScan * Signature: ()[Ljava/lang/String; */ JNIEXPORT jobjectArray JNICALL Java_com_example_pmxota_OtaLoadJNI_btDeviceScan(JNIEnv *env, jobject obj) { inquiry_info *ii = NULL; int max_rsp = 255; // length of device scan * 1.25 sec int len = 8; int num_rsp; int dev_id, flags, i; char addr[19] = {0}; char name[248] = {0}; int bHasError = 0; jstring errMsg; jobjectArray btDevices; dev_id = hci_get_route(NULL); if (dev_id < 0) { bHasError = 1; errMsg = (*env)->NewStringUTF(env, "Error obtaining device ID"); } flags = IREQ_CACHE_FLUSH; num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if (num_rsp < 0) perror("hci_inquiry failed"); btDevices = (*env)->NewObjectArray(env, num_rsp, (*env)->FindClass(env, "java/lang/String"), (*env)->NewStringUTF(env, "")); for (i=0; i<num_rsp; i++) { ba2str(&(ii+i)->bdaddr, addr); (*env)->SetObjectArrayElement(env, btDevices, i, (*env)->NewStringUTF(env, addr)); } return btDevices; }
void Bluetooth::scan() { int len = 4; int maxRsp = 128; inquiry_info *info = (inquiry_info*)malloc(maxRsp * sizeof(inquiry_info)); int count = hci_inquiry(_devId, len, maxRsp, NULL, &info, IREQ_CACHE_FLUSH); if (count < 0) { printf("ERROR hci_inquiry\n"); free(info); return; } for (int i=0; i<count; i++) { char name[20] = { 0 }; int stat = hci_read_remote_name(_sock, &(info[i].bdaddr), sizeof(name), name, 0); if (stat < 0) { strcpy(name, "???"); } printf("found: %s\n", name); } free(info); }
/** * @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 wiic_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; } WIIC_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); WIIC_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; }
static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) { struct sock *sk = sock->sk; int err; BT_DBG("cmd %x arg %lx", cmd, arg); switch (cmd) { case HCIGETDEVLIST: return hci_get_dev_list(arg); case HCIGETDEVINFO: return hci_get_dev_info(arg); case HCIGETCONNLIST: return hci_get_conn_list(arg); case HCIDEVUP: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_open(arg); case HCIDEVDOWN: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_close(arg); case HCIDEVRESET: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_reset(arg); case HCIDEVRESTAT: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_reset_stat(arg); case HCISETSCAN: case HCISETAUTH: case HCISETENCRYPT: case HCISETPTYPE: case HCISETLINKPOL: case HCISETLINKMODE: case HCISETACLMTU: case HCISETSCOMTU: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_cmd(cmd, arg); case HCIINQUIRY: return hci_inquiry(arg); default: lock_sock(sk); err = hci_sock_bound_ioctl(sk, cmd, arg); release_sock(sk); return err; }; }
/* Search and connect * Returns: * -1 - critical error (exit persist mode) * 1 - non critical error * 0 - success */ static int do_connect(void) { inquiry_info *ii; int reconnect = 0; int i, n, r = 0; do { if (reconnect) sleep(persist); reconnect = 1; if (cache.valid > 0) { /* Use cached bdaddr */ r = create_connection(cache.dst, &cache.bdaddr); if (r < 0) { terminate = 1; break; } continue; } syslog(LOG_INFO, "Inquiring"); /* FIXME: Should we use non general LAP here ? */ ii = NULL; n = hci_inquiry(src_dev, search_duration, 10, NULL, &ii, 0); if (n < 0) { syslog(LOG_ERR, "Inquiry failed. %s(%d)", strerror(errno), errno); continue; } for (i = 0; i < n; i++) { char dst[40]; ba2str(&ii[i].bdaddr, dst); if (use_sdp) { syslog(LOG_INFO, "Searching for %s on %s", bnep_svc2str(service), dst); if (bnep_sdp_search(&src_addr, &ii[i].bdaddr, service) <= 0) continue; } r = create_connection(dst, &ii[i].bdaddr); if (r < 0) { terminate = 1; break; } } free(ii); } while (!terminate && persist); return r; }
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; }
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; }
static int scan(int dev_id, int s) { inquiry_info *info = NULL; int max, len, flags; char addr[18], name[256]; int i, sock; len = 4; max = 100; flags = IREQ_CACHE_FLUSH; max = hci_inquiry(dev_id, len, max, NULL, &info, flags); if (max == -1) { perror("hci_inquiry"); return -1; } for (i = 0; i < max; i++) { if (bt_cache_find(&info[i].bdaddr)) continue; sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM); if (sock == -1) { perror("socket"); continue; } if (bt_bind(sock, dev_id, &info[i].bdaddr)) continue; if (hci_read_remote_name(s, &info[i].bdaddr, sizeof(name), name, 2)) name[0] = '\0'; ba2str(&info[i].bdaddr, addr); printf("<phone btaddr=\"%s\" name=\"%s\">\n", addr, name); fflush(stdout); bt_rfcomm(dev_id); bt_release(sock, dev_id); close(sock); puts("</phone>"); fflush(stdout); bt_cache_add(&info[i].bdaddr); } free(info); return 0; }
/* * inquiry_complete(): * * Called by HCI when a inquiry complete event was received. * Connects to the first device in the list. * Initializes a search for other devices if the inquiry failed. */ err_t inquiry_complete(void *arg, struct hci_pcb *pcb, struct hci_inq_res *ires, u16_t result) { struct l2cap_pcb *l2cappcb; if(result == HCI_SUCCESS) { LWIP_DEBUGF(BT_SPP_DEBUG, ("successful Inquiry\n")); if(ires != NULL) { LWIP_DEBUGF(BT_SPP_DEBUG, ("Initiate L2CAP connection\n")); LWIP_DEBUGF(BT_SPP_DEBUG, ("ires->psrm %d\n ires->psm %d\n ires->co %d\n", ires->psrm, ires->psm, ires->co)); LWIP_DEBUGF(BT_SPP_DEBUG, ("ires->bdaddr %02x:%02x:%02x:%02x:%02x:%02x\n", ires->bdaddr.addr[5], ires->bdaddr.addr[4], ires->bdaddr.addr[3], ires->bdaddr.addr[2], ires->bdaddr.addr[1], ires->bdaddr.addr[0])); /*if((ires->cod[1] & 0x1F) == 0x03) { bt_spp_state.profile = LAP_PROFILE; } else { bt_spp_state.profile = DUN_PROFILE; }*/ if((l2cappcb = l2cap_new()) == NULL) { LWIP_DEBUGF(BT_SPP_DEBUG, ("inquiry_complete: Could not alloc L2CAP pcb\n")); return ERR_MEM; } //if(bt_spp_state.profile == DUN_PROFILE) { // l2ca_connect_req(l2cappcb, &(ires->bdaddr), SDP_PSM, 0, l2cap_connected); //} else { l2ca_connect_req(l2cappcb, &(ires->bdaddr), SDP_PSM, HCI_ALLOW_ROLE_SWITCH, l2cap_connected); //} } else { hci_inquiry(0x009E8B33, 0x04, 0x01, inquiry_complete); } } else { LWIP_DEBUGF(BT_SPP_DEBUG, ("Unsuccessful Inquiry.\n")); hci_inquiry(0x009E8B33, 0x04, 0x01, inquiry_complete); } return ERR_OK; }
int bt_scan_devices(int dev_id, int s_sock, bt_device_t *devices[MAX_BT_DEVICES]) { int flags = IREQ_CACHE_FLUSH;/*Flush cache of previously discovered BlueTooth devices*/ int inq_res;/*hci_inquiry results (Number of Bluetooth devices found*/ int i; char addr[BT_ADDR_LEN] = { 0 }; char name[BT_NAME_LEN] = { 0 }; inquiry_info *dev_in_range = NULL; // if(devices != NULL) /*To prevent a segmentation fault if devices == NULL*/ //memset(devices, 0, MAX_BT_DEVICES * sizeof(bt_device_t)); dev_in_range = (inquiry_info *) malloc(sizeof(inquiry_info) * MAX_BT_DEVICES); inq_res = hci_inquiry( dev_id, MAX_INQUIRY_LEN, MAX_BT_DEVICES, NULL, &dev_in_range, flags); if( inq_res < 0 ) { perror("hci_inquiry error\n"); return -1; } /*TODO: error when inq_res == 0*/ printf("BT_Server: # Devices found: %d\n",inq_res); for(i=0; i<inq_res; ++i) { ba2str(&dev_in_range[i].bdaddr,addr); memset(name,0,BT_NAME_LEN); int r = hci_read_remote_name(s_sock, &dev_in_range[i].bdaddr, BT_NAME_LEN, name, 0); if( r < 0) { strcpy(name,"[unknown]\0"); } devices[i] = (bt_device_t *) malloc(sizeof(bt_device_t)); strcpy(devices[i]->name, name); memcpy(&devices[i]->device_addr,&dev_in_range[i].bdaddr,sizeof(bdaddr_t)); printf("BT_Server: Device #: %d ; Device ID: %s ; Device Name: %s ;\n",i, addr,devices[i]->name); } free(dev_in_range); close(s_sock); return inq_res; }
int main(int argc, char** argv) { inquiry_info *ii = NULL; int max_rsp, num_rsp; int dev_id, sock, len, flags; int i; char addr[19] = { 0 }; char name[248] = { 0 }; dev_id = hci_get_route(NULL); sock = hci_open_dev(dev_id); if(dev_id < 0 || sock < 0) { perror("opening socket"); exit(1); } len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if(num_rsp < 0) { perror("hci_inquiry"); } for(i = 0; i < num_rsp; ++i) { ba2str(&(ii+i)->bdaddr, addr); memset(name, 0, sizeof(name)); if(hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { strcpy(name, "[unknown]"); } printf("%s %s\n", addr, name); } free( ii ); close( sock ); return 0; }
void bluez_detect(void) { inquiry_info *ii = NULL; int max_rsp, num_rsp; int dev_id, sock, len, flags; int i; char addr[19] = { 0 }; char name[248] = { 0 }; dev_id = hci_get_route(NULL); sock = hci_open_dev(dev_id); if (dev_id < 0 || sock < 0) { perror("opening socket"); exit(1); } len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info *) malloc(max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if (num_rsp < 0) perror("hci_inquiry"); for (i = 0; i < num_rsp; i++) { ba2str(&(ii + i)->bdaddr, addr); memset(name, 0, sizeof(name)); if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0) < 0) strcpy(name, "[unknown]"); if (debug) printf("; %s %s\n", addr, name); if (find_list(addr, at_prefixes)) { print_config(addr, name, "blueat"); } if (find_list(addr, nokia_prefixes)) { print_config(addr, name, "bluephonet"); } } free(ii); close(sock); }
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; }
int main(int argc, char **argv) { inquiry_info *devices = NULL; int max_rsp, num_rsp; int adapter_id, sock, len, flags; int i; char addr[19] = { 0 }; char name[248] = { 0 }; adapter_id = hci_get_route(NULL); sock = hci_open_dev( adapter_id ); if (adapter_id < 0 || sock < 0) { perror("Opening socket.."); exit(EXIT_FAILURE); } len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; devices = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info)); 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) { ba2str(&(devices+i)->bdaddr, addr); memset(name, 0, sizeof(name)); if (0 != hci_read_remote_name(sock, &(devices+i)->bdaddr, sizeof(name), name, 0)) strcpy(name, "[unknown]"); printf("%s | %s\n", addr, name); } free( devices ); close( sock ); return EXIT_SUCCESS; }
int getDevices(char dev_addr[MAX_RISP][19], int max_rsp){ inquiry_info *ii; int i, dev_id, sock, len, flags, num_rsp; char addr[ADDR_LEN] = { 0 }; char name[248] = { 0 }; dev_id = hci_get_route(NULL); sock = hci_open_dev( dev_id ); if (dev_id < 0 || sock < 0) { perror("opening socket"); return -1; } len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); printf("Scanning ...\n"); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if( num_rsp < 0 ) perror("hci_inquiry"); printf("Select a device: \n"); for (i = 0; i < num_rsp; i++) { ba2str(&(ii+i)->bdaddr, dev_addr[i]); memset(name, 0, sizeof(name)); if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(addr), name, 0) < 0) strcpy(name, "[unknown]"); printf("\t%d %s\t%s\n", i+1, dev_addr[i], name); } free(ii); close( sock ); return num_rsp; }
int scanBt(){ inquiry_info* info = NULL; int max_rsp = 255; int flags = IREQ_CACHE_FLUSH; int num_rsp = 0; bdaddr_t addr; int i, sock; int DEVIDHCI=0; info = malloc(max_rsp * sizeof(inquiry_info)); memset(info, 0, max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(DEVIDHCI, 8, num_rsp, NULL, &info, flags); sock = hci_open_dev(DEVIDHCI); if (num_rsp < 0) { perror("hci_inquiry"); } for (i = 0; i < num_rsp; i++) { baswap(&addr, &(info + i)->bdaddr); printf("Addr : %s\n", batostr(&addr)); } close(sock); }
JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_runDeviceInquiryImpl (JNIEnv *env, jobject peer, jobject inquiryRunnable, jobject startedNotify, jint deviceID, jint deviceDescriptor, jint accessCode, jint inquiryLength, jint maxResponses, jobject listener) { struct DeviceInquiryCallback callback; DeviceInquiryCallback_Init(&callback); if (!DeviceInquiryCallback_builDeviceInquiryCallbacks(env, &callback, inquiryRunnable, startedNotify)) { return INQUIRY_ERROR; } if (!DeviceInquiryCallback_callDeviceInquiryStartedCallback(env, &callback)) { return INQUIRY_ERROR; } int max_rsp = maxResponses; inquiry_info *ii = NULL; int num_rsp = hci_inquiry(deviceID, inquiryLength, max_rsp, NULL, &ii, accessCode); int rc = INQUIRY_COMPLETED; if (num_rsp < 0) { rc = INQUIRY_ERROR; } else { int i; for(i = 0; i < num_rsp; i++) { bdaddr_t* address = &(ii+i)->bdaddr; jlong addressLong = deviceAddrToLong(address); uint8_t *dev_class = (ii+i)->dev_class; int deviceClass = deviceClassBytesToInt(dev_class); jboolean paired = false; // TODO jstring name = NULL; // Names are stored in RemoteDeviceHelper and can be reused. if (!DeviceInquiryCallback_callDeviceDiscovered(env, &callback, listener, addressLong, deviceClass, name, paired)) { rc = INQUIRY_ERROR; break; } } } free(ii); return rc; }
void bluetooth::queryDevices(std::vector<BluetoothAddress> &devices, int max_rsp) { inquiry_info *ii = NULL; int num_rsp; int dev_id, sock, len, flags; int i; char addr[19] = {0}; char name[1024] = {0}; dev_id = hci_get_route(NULL); if (dev_id == -1) { throw std::runtime_error("cannot hci_get_route"); } sock = hci_open_dev(dev_id); if (sock == -1) { throw std::runtime_error("cannot hci_open_dev"); } len = 8; flags = IREQ_CACHE_FLUSH; ii = new inquiry_info[max_rsp]; num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if (num_rsp == -1) { delete []ii; close(sock); throw std::runtime_error("cannot call hci_inquiry"); } for (int i = 0; i < num_rsp; i++) { const unsigned char * addr = (const unsigned char *) &(ii + i)->bdaddr.b; const unsigned char * devClass = (const unsigned char *) &(ii + i)->dev_class; std::string strName = ""; memset(name, 0, sizeof(name)); if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0) == 0) { strName = name; } else strName = "[unknown]"; devices.push_back(BluetoothAddress(addr, devClass, strName)); } }
// Scan for available bluetooth devices int scan(char * log_file, char * filename) { inquiry_info *ii = NULL; int max_rsp, num_rsp; int dev_id, sock, len, flags; int i; int t_count; char addr[19] = { 0 }; dev_id = hci_get_route(NULL); sock = hci_open_dev( dev_id ); // Error with opening socket if (dev_id < 0 || sock < 0) { perror("error: opening socket\n"); return 1; } len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); // Check for any devices found if( num_rsp < 0 ) { return 1; } // Cycle through all the addresses found for (i = 0; i < num_rsp; i++) { char name[248] = { 0 }; ba2str(&(ii+i)->bdaddr, addr); memset(name, 0, sizeof(name)); // Check for device name if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) strcpy(name, "[unknown]"); // while (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { // // Requesting device name // } int num = 0; int matchfound = 0; // Check to see if a new device has been discovered while (num < numaddrs) { if (strcmp (addr, addrs[num]) == 0) { matchfound = 1; } num++; } // Run commands for newly found device address if (matchfound == 0) { time_stamp(addr, name, log_file); run_config(addr, log_file, filename); // Add address to list of discovered devices addrs[numaddrs] = malloc(sizeof addr + 1); strcpy(addrs[numaddrs], addr); numaddrs++; } } free( ii ); close( sock ); return 0; }
static void hciDiscovery(int dev_id) { int length, flags, dd, i; bdaddr_t bdaddr; char name[248]; inquiry_info *info = NULL; int num_rsp = 100; dd = hci_open_dev(dev_id); if (dd < 0) { fprintf(stderr, "HCI device open failed"); free(info); exit(1); } int ctl; if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { perror("Can't open HCI socket."); } struct hci_dev_req dr; dr.dev_id = dev_id; dr.dev_opt = SCAN_DISABLED; // Stop the dongles from discovering each other! ioctl(ctl, HCISETSCAN, (unsigned long) &dr); /* Reset HCI device with a stop[ and start*/ printf("resetting hci device. \n"); ioctl(ctl, HCIDEVDOWN, dev_id); ioctl(ctl, HCIDEVUP, dev_id); close(ctl); // Need to field test small values. length = 4; /* multiply by 1.2 seconds */ flags = IREQ_CACHE_FLUSH; printf("sending inquiry\n"); num_rsp = hci_inquiry(dev_id, length, num_rsp, NULL, &info, flags); if(num_rsp > 0) { printf("hci%d saw %d responses\n", dev_id, num_rsp); } // we need to come up with a device class lookup function // this is just incase we drive or walk by too quick to get a devname... this should be logged somewhere. char addr[18]; for (i = 0; i < num_rsp; i++) { ba2str(&(info+i)->bdaddr, addr); printf("%s\tclass: 0x%2.2x%2.2x%2.2x\n",addr, (info+i)->dev_class[2], (info+i)->dev_class[1], (info+i)->dev_class[0]); } // this may need to be done differently. for(i = 0; i < num_rsp; i++) { memset(name, 0, sizeof(name)); if (hci_read_remote_name(dd, &(info+i)->bdaddr, sizeof(name), name, opts.btout) == 0) { baswap(&bdaddr, &(info+i)->bdaddr); printf("hci%d Discovered: %s [%s] - %s\n", dev_id, name, batostr(&bdaddr), findManf(batostr(&bdaddr))); sdptool(name,findManf(batostr(&bdaddr)),batostr(&bdaddr),dev_id); } } // where does the sdptool code go? hci_close_dev(dd); free(info); }
int main(int argc, char *argv[]) { inquiry_info *ii = NULL; int i, opt, dev_id, dev_handle, len, flags, max_rsp, num_rsp, lap, timeout = 20; uint8_t uap, extended = 0; uint8_t scan = 0; char ubertooth_device = -1; char *bt_dev = "hci0"; char addr[19] = { 0 }; ubertooth_t* ut = NULL; btbb_piconet* pn; bdaddr_t bdaddr; while ((opt=getopt(argc,argv,"hU:t:e:xsb:")) != EOF) { switch(opt) { case 'U': ubertooth_device = atoi(optarg); break; case 'b': bt_dev = optarg; if (bt_dev == NULL) { perror(optarg); return 1; } break; case 't': timeout = atoi(optarg); break; case 'e': max_ac_errors = atoi(optarg); break; case 'x': extended = 1; break; case 's': scan = 1; break; case 'h': default: usage(); return 1; } } dev_id = hci_devid(bt_dev); if (dev_id < 0) { printf("error: Unable to find %s (%d)\n", bt_dev, dev_id); return 1; } dev_handle = hci_open_dev( dev_id ); if (dev_handle < 0) { perror("HCI device open failed"); return 1; } ut = ubertooth_start(ubertooth_device); if (ut == NULL) { usage(); return 1; } /* Set sweep mode - otherwise AFH map is useless */ cmd_set_channel(ut->devh, 9999); if (scan) { /* Equivalent to "hcitool scan" */ printf("HCI scan\n"); len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if( num_rsp < 0 ) perror("hci_inquiry"); for (i = 0; i < num_rsp; i++) { ba2str(&(ii+i)->bdaddr, addr); print_name_and_class(dev_handle, dev_id, &(ii+i)->bdaddr, addr, extended); } free(ii); } /* Now find hidden piconets with Ubertooth */ printf("\nUbertooth scan\n"); btbb_init_survey(); rx_live(ut, NULL, timeout); ubertooth_stop(ut); while((pn=btbb_next_survey_result()) != NULL) { lap = btbb_piconet_get_lap(pn); if (btbb_piconet_get_flag(pn, BTBB_UAP_VALID)) { uap = btbb_piconet_get_uap(pn); sprintf(addr, "00:00:%02X:%02X:%02X:%02X", uap, (lap >> 16) & 0xFF, (lap >> 8) & 0xFF, lap & 0xFF); str2ba(addr, &bdaddr); /* Printable version showing that the NAP is unknown */ sprintf(addr, "??:??:%02X:%02X:%02X:%02X", uap, (lap >> 16) & 0xFF, (lap >> 8) & 0xFF, lap & 0xFF); print_name_and_class(dev_handle, dev_id, &bdaddr, addr, extended); } else printf("??:??:??:%02X:%02X:%02X\n", (lap >> 16) & 0xFF, (lap >> 8) & 0xFF, lap & 0xFF); btbb_print_afh_map(pn); }
/* Poll Bluetooth and print result */ void BTD::HCI_task() { switch (hci_state){ case HCI_INIT_STATE: hci_counter++; if (hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events hci_reset(); hci_state = HCI_RESET_STATE; hci_counter = 0; } break; case HCI_RESET_STATE: hci_counter++; if (hci_cmd_complete) { hci_counter = 0; #ifdef DEBUG Notify(PSTR("\r\nHCI Reset complete")); #endif hci_state = HCI_BDADDR_STATE; hci_read_bdaddr(); } else if (hci_counter > hci_num_reset_loops) { hci_num_reset_loops *= 10; if(hci_num_reset_loops > 2000) hci_num_reset_loops = 2000; #ifdef DEBUG Notify(PSTR("\r\nNo response to HCI Reset")); #endif hci_state = HCI_INIT_STATE; hci_counter = 0; } break; case HCI_BDADDR_STATE: if (hci_read_bdaddr_complete) { #ifdef DEBUG Notify(PSTR("\r\nLocal Bluetooth Address: ")); for(int8_t i = 5; i > 0;i--) { PrintHex<uint8_t>(my_bdaddr[i]); Notify(PSTR(":")); } PrintHex<uint8_t>(my_bdaddr[0]); #endif hci_read_local_version_information(); hci_state = HCI_LOCAL_VERSION_STATE; } break; case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class if (hci_read_version_complete) { if(btdName != NULL) { hci_set_local_name(btdName); hci_state = HCI_SET_NAME_STATE; } else hci_state = HCI_CHECK_WII_SERVICE; } break; case HCI_SET_NAME_STATE: if (hci_cmd_complete) { #ifdef DEBUG Notify(PSTR("\r\nThe name is set to: ")); Serial.print(btdName); #endif hci_state = HCI_CHECK_WII_SERVICE; } break; case HCI_CHECK_WII_SERVICE: if(pairWithWii) { // Check if it should try to connect to a wiimote #ifdef DEBUG Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller")); #endif hci_inquiry(); hci_state = HCI_INQUIRY_STATE; } else hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote break; case HCI_INQUIRY_STATE: if(hci_wii_found) { hci_inquiry_cancel(); // Stop inquiry #ifdef DEBUG Notify(PSTR("\r\nWiimote found")); Notify(PSTR("\r\nNow just create the instance like so:")); Notify(PSTR("\r\nWII Wii(&Btd);")); Notify(PSTR("\r\nAnd then press any button on the Wiimote")); #endif if(motionPlusInside) { hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller hci_state = HCI_REMOTE_NAME_STATE; } else hci_state = HCI_CONNECT_WII_STATE; } break; case HCI_CONNECT_WII_STATE: if(hci_cmd_complete) { #ifdef DEBUG Notify(PSTR("\r\nConnecting to Wiimote")); #endif hci_connect(); hci_state = HCI_CONNECTED_WII_STATE; } break; case HCI_CONNECTED_WII_STATE: if(hci_connect_event) { if(hci_connect_complete) { #ifdef DEBUG Notify(PSTR("\r\nConnected to Wiimote")); #endif hci_authentication_request(); // This will start the pairing with the wiimote hci_state = HCI_SCANNING_STATE; } else { #ifdef DEBUG Notify(PSTR("\r\nTrying to connect one more time...")); #endif hci_connect(); // Try to connect one more time } } break; case HCI_SCANNING_STATE: if(!connectToWii && !pairWithWii) { #ifdef DEBUG Notify(PSTR("\r\nWait For Incoming Connection Request")); #endif hci_write_scan_enable(); watingForConnection = true; hci_state = HCI_CONNECT_IN_STATE; } break; case HCI_CONNECT_IN_STATE: if(hci_incoming_connect_request) { watingForConnection = false; #ifdef DEBUG Notify(PSTR("\r\nIncoming Connection Request")); #endif hci_remote_name(); hci_state = HCI_REMOTE_NAME_STATE; } else if (hci_disconnect_complete) hci_state = HCI_DISCONNECT_STATE; break; case HCI_REMOTE_NAME_STATE: if(hci_remote_name_complete) { #ifdef DEBUG Notify(PSTR("\r\nRemote Name: ")); for (uint8_t i = 0; i < 30; i++) { if(remote_name[i] == NULL) break; Serial.write(remote_name[i]); } #endif if(strncmp((const char*)remote_name, "Nintendo", 8) == 0) { #ifdef DEBUG Notify(PSTR("\r\nWiimote is connecting")); #endif if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) { #ifdef DEBUG Notify(PSTR(" with Motion Plus Inside")); #endif motionPlusInside = true; } else if(strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) { #ifdef DEBUG Notify(PSTR(" - Wii U Pro Controller")); #endif motionPlusInside = true; wiiUProController = true; } else { motionPlusInside = false; wiiUProController = false; } incomingWii = true; } if(pairWithWii && motionPlusInside) hci_state = HCI_CONNECT_WII_STATE; else { hci_accept_connection(); hci_state = HCI_CONNECTED_STATE; } } break; case HCI_CONNECTED_STATE: if (hci_connect_complete) { #ifdef DEBUG Notify(PSTR("\r\nConnected to Device: ")); for(int8_t i = 5; i>0;i--) { PrintHex<uint8_t>(disc_bdaddr[i]); Notify(PSTR(":")); } PrintHex<uint8_t>(disc_bdaddr[0]); #endif // Clear these flags for a new connection l2capConnectionClaimed = false; sdpConnectionClaimed = false; rfcommConnectionClaimed = false; hci_event_flag = 0; hci_state = HCI_DONE_STATE; } break; case HCI_DONE_STATE: hci_counter++; if (hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started hci_counter = 0; hci_state = HCI_SCANNING_STATE; } break; case HCI_DISCONNECT_STATE: if (hci_disconnect_complete) { #ifdef DEBUG Notify(PSTR("\r\nHCI Disconnected from Device")); #endif hci_event_flag = 0; // Clear all flags // Reset all buffers for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) hcibuf[i] = 0; for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) l2capinbuf[i] = 0; hci_state = HCI_SCANNING_STATE; } break; default: break; } }
void WiimoteScanner::FindWiimotes(std::vector<Wiimote*>& found_wiimotes, Wiimote*& found_board) { // supposedly 1.28 seconds int const wait_len = 1; int const max_infos = 255; inquiry_info scan_infos[max_infos] = {}; auto* scan_infos_ptr = scan_infos; found_board = nullptr; // Use Limited Dedicated Inquiry Access Code (LIAC) to query, since third-party Wiimotes // cannot be discovered without it. const u8 lap[3] = {0x00, 0x8b, 0x9e}; // Scan for Bluetooth devices int const found_devices = hci_inquiry(device_id, wait_len, max_infos, lap, &scan_infos_ptr, IREQ_CACHE_FLUSH); if (found_devices < 0) { ERROR_LOG(WIIMOTE, "Error searching for Bluetooth devices."); return; } DEBUG_LOG(WIIMOTE, "Found %i Bluetooth device(s).", found_devices); // Display discovered devices for (int i = 0; i < found_devices; ++i) { ERROR_LOG(WIIMOTE, "found a device..."); // BT names are a maximum of 248 bytes apparently char name[255] = {}; if (hci_read_remote_name(device_sock, &scan_infos[i].bdaddr, sizeof(name), name, 1000) < 0) { ERROR_LOG(WIIMOTE, "name request failed"); continue; } ERROR_LOG(WIIMOTE, "device name %s", name); if (IsValidBluetoothName(name)) { bool new_wiimote = true; // Determine if this Wiimote has already been found. for (int j = 0; j < MAX_BBMOTES && new_wiimote; ++j) { // compare this address with the stored addresses in our global array // static_cast is OK here, since we're only ever going to have this subclass in g_wiimotes // on Linux (and likewise, only WiimoteWindows on Windows, etc) auto connected_wiimote = static_cast<WiimoteLinux*>(g_wiimotes[j]); if (connected_wiimote && bacmp(&scan_infos[i].bdaddr, &connected_wiimote->Address()) == 0) new_wiimote = false; } if (new_wiimote) { // Found a new device char bdaddr_str[18] = {}; ba2str(&scan_infos[i].bdaddr, bdaddr_str); Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr); if (IsBalanceBoardName(name)) { found_board = wm; NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str); } else { found_wiimotes.push_back(wm); NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str); } } } } }
void WiimoteScannerLinux::FindWiimotes(std::vector<Wiimote*>& found_wiimotes, Wiimote*& found_board) { // supposedly 1.28 seconds int const wait_len = 1; int const max_infos = 255; inquiry_info scan_infos[max_infos] = {}; auto* scan_infos_ptr = scan_infos; found_board = nullptr; // Use Limited Dedicated Inquiry Access Code (LIAC) to query, since third-party Wiimotes // cannot be discovered without it. const u8 lap[3] = {0x00, 0x8b, 0x9e}; // Scan for Bluetooth devices int const found_devices = hci_inquiry(m_device_id, wait_len, max_infos, lap, &scan_infos_ptr, IREQ_CACHE_FLUSH); if (found_devices < 0) { ERROR_LOG(WIIMOTE, "Error searching for Bluetooth devices."); return; } DEBUG_LOG(WIIMOTE, "Found %i Bluetooth device(s).", found_devices); // Display discovered devices for (int i = 0; i < found_devices; ++i) { NOTICE_LOG(WIIMOTE, "found a device..."); // BT names are a maximum of 248 bytes apparently char name[255] = {}; if (hci_read_remote_name(m_device_sock, &scan_infos[i].bdaddr, sizeof(name), name, 1000) < 0) { ERROR_LOG(WIIMOTE, "name request failed"); continue; } NOTICE_LOG(WIIMOTE, "device name %s", name); if (!IsValidDeviceName(name)) continue; char bdaddr_str[18] = {}; ba2str(&scan_infos[i].bdaddr, bdaddr_str); if (!IsNewWiimote(bdaddr_str)) continue; // Found a new device Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr); if (IsBalanceBoardName(name)) { found_board = wm; NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str); } else { found_wiimotes.push_back(wm); NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str); } } }
static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) { struct sock *sk = sock->sk; struct hci_dev *hdev = hci_pi(sk)->hdev; __u32 mode; DBG("cmd %x arg %lx", cmd, arg); switch (cmd) { case HCIGETINFO: return hci_dev_info(arg); case HCIGETDEVLIST: return hci_dev_list(arg); case HCIDEVUP: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_open(arg); case HCIDEVDOWN: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_close(arg); case HCIDEVRESET: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_reset(arg); case HCIRESETSTAT: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_reset_stat(arg); case HCISETSCAN: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_setscan(arg); case HCISETAUTH: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_setauth(arg); case HCISETRAW: if (!capable(CAP_NET_ADMIN)) return -EACCES; if (!hdev) return -EBADFD; if (arg) mode = HCI_RAW; else mode = HCI_NORMAL; return hci_dev_setmode(hdev, mode); case HCISETPTYPE: if (!capable(CAP_NET_ADMIN)) return -EACCES; return hci_dev_setptype(arg); case HCIINQUIRY: return hci_inquiry(arg); case HCIGETCONNLIST: return hci_conn_list(arg); default: return -EINVAL; }; }
int main(int argc, char *argv[]) { inquiry_info *ii = NULL; int i, opt, dev_id, sock, len, flags, max_rsp, num_rsp, lap, timeout = 20; uint8_t extended = 0; uint8_t scan = 0; char ubertooth_device = -1; char *bt_dev = "hci0"; char addr[19] = { 0 }; char name[248] = { 0 }; struct libusb_device_handle *devh = NULL; btbb_piconet *pn; bdaddr_t bdaddr; while ((opt=getopt(argc,argv,"ht:xsb:")) != EOF) { switch(opt) { case 'b': bt_dev = optarg; if (bt_dev == NULL) { perror(optarg); return 1; } break; case 't': timeout = atoi(optarg); break; case 'x': extended = 1; break; case 's': scan = 1; break; case 'h': default: usage(); return 1; } } dev_id = hci_devid(bt_dev); sock = hci_open_dev( dev_id ); if (dev_id < 0 || sock < 0) { perror("opening socket"); return 1; } devh = ubertooth_start(ubertooth_device); if (devh == NULL) { usage(); return 1; } /* Set sweep mode - otherwise AFH map is useless */ cmd_set_channel(devh, 9999); if (scan) { len = 8; max_rsp = 255; flags = IREQ_CACHE_FLUSH; ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if( num_rsp < 0 ) perror("hci_inquiry"); /* Equivalent to "hcitool scan" */ printf("HCI scan\n"); for (i = 0; i < num_rsp; i++) { ba2str(&(ii+i)->bdaddr, addr); memset(name, 0, sizeof(name)); if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) strcpy(name, "[unknown]"); printf("%s %s\n", addr, name); } free(ii); } /* Now find hidden piconets with Ubertooth */ printf("\nUbertooth scan\n"); btbb_init_survey(); rx_live(devh, NULL, timeout); ubertooth_stop(devh); while((pn=btbb_next_survey_result()) != NULL) { lap = btbb_piconet_get_lap(pn); if (btbb_piconet_get_flag(pn, BTBB_UAP_VALID)) { lap = btbb_piconet_get_lap(pn); sprintf(addr, "00:00:%02X:%02X:%02X:%02X", btbb_piconet_get_uap(pn), (lap >> 16) & 0xFF, (lap >> 8) & 0xFF, lap & 0xFF); str2ba(addr, &bdaddr); memset(name, 0, sizeof(name)); if (hci_read_remote_name(sock, &bdaddr, sizeof(name), name, 0) < 0) strcpy(name, "[unknown]"); printf("%s %s\n", addr, name); if (extended) extra_info(sock, dev_id, &bdaddr); } else printf("00:00:00:%02X:%02X:%02X\n", (lap >> 16) & 0xFF, (lap >> 8) & 0xFF, lap & 0xFF); btbb_print_afh_map(pn); }
void* BtDeviceController::run(void* param){ HLoggerPtr logger = LoggerUtil::getLogger("br.ufscar.lince.ginga.hones.idservice.btdevicecontroller"); LoggerUtil_info(logger, "Entrando no metodo BtDeviceController::run(param)"); #ifdef HAVE_BLUETOOTH BtDeviceController* devControl = (BtDeviceController*) param; int max_rsp = 255; int len = 6; char addr[19] = {0}; char name[248] = {0}; LoggerUtil_debug(logger, "Inicializando o Bluetooth..."); int dev_id = hci_get_route(NULL); int sock = hci_open_dev(dev_id); if ((dev_id < 0) || (sock < 0)){ LoggerUtil_debug(logger, "Bluetooth indisponível!"); return NULL; } LoggerUtil_debug(logger, "Bluetooth ligado!"); inquiry_info* ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info)); BtDeviceList* currentList = devControl->getBtDeviceList(); BtDeviceList newList; bool removeAction = false; while (devControl->isRunning() == true) { pthread_testcancel(); int num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, IREQ_CACHE_FLUSH); for (int i=0; i < num_rsp; i++) { ba2str(&(ii + i)->bdaddr, addr); memset(name, 0, sizeof (name)); if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof (name), name, 0) < 0) strcpy(name, "[desconhecido]"); if (newList.getBtDeviceByUDN(addr) == NULL){ BtDevice* device = new BtDevice(); device->setFriendlyName(name); device->setUDN(addr); newList.add(device); } } int listSize = newList.size(); for(int i = 0; i < listSize; i++){ BtDevice* device = newList.getBtDevice(i); if (currentList->getBtDeviceByUDN(device->getUDN()) == NULL){ devControl->addBtDevice(new BtDevice(device)); } } if (removeAction == true) { listSize = currentList->size(); int index = 0; while (index < listSize) { BtDevice* device = currentList->getBtDevice(index); if (newList.getBtDeviceByUDN(device->getUDN()) == NULL) { devControl->removeBtDevice(device); delete device; listSize = currentList->size(); index = -1; } index++; } newList.clear(); removeAction = false; } else { removeAction = true; } } newList.clear(); free(ii); close(sock); #endif return NULL; }
bt_device_table_t hci_scan_devices(hci_socket_t *hci_socket, hci_controller_t *hci_controller, uint8_t duration, uint16_t max_rsp, long flags) { bt_device_table_t res; res.device = NULL; res.length = 0; if (!hci_controller) { print_trace(TRACE_ERROR, "hci_scan_devices : invalid controller reference.\n"); return res; } if (hci_controller->state != HCI_STATE_OPEN) { print_trace(TRACE_ERROR, "hci_scan_devices : busy or closed controller.\n"); return res; } char new_socket = 0; char socket_err = 0; check_hci_socket_ptr(&hci_socket, hci_controller, &new_socket, &socket_err); if (socket_err) { return res; } // Creation of the inquiry_info table : inquiry_info *ii = calloc(max_rsp, sizeof(inquiry_info)); // Starting the inquiry : print_trace(TRACE_INFO, "Starting the scanning inquiry..."); hci_change_state(hci_controller, HCI_STATE_SCANNING); int16_t num_rsp = hci_inquiry(hci_socket->dev_id, duration, max_rsp, NULL, &ii, flags); if(num_rsp <= 0) { print_trace(TRACE_STDOUT, " No device found.\n"); goto end; } print_trace(TRACE_INFO, " [DONE]\n"); hci_change_state(hci_controller, HCI_STATE_OPEN); bt_device_t *device_table = calloc(num_rsp, sizeof(bt_device_t)); for (uint16_t i = 0; i < num_rsp; i++) { memset(&(device_table[i]), 0, sizeof(bt_device_t)); device_table[i].mac = ii[i].bdaddr; hci_compute_device_name(hci_socket, hci_controller, &(device_table[i])); device_table[i].add_type = UNKNOWN_ADDRESS_TYPE; strcpy(device_table[i].custom_name, "UNKNOWN"); if (!bt_already_registered_device(device_table[i].mac)) { bt_register_device(device_table[i]); } } res.device = device_table; res.length = num_rsp; end : hci_change_state(hci_controller, HCI_STATE_OPEN); free(ii); if (new_socket) { close_hci_socket(hci_socket); free(hci_socket); } return res; }