void HackRFSource::get_device_names(std::vector<std::string>& devices) { hackrf_device *hackrf_ptr; read_partid_serialno_t read_partid_serialno; hackrf_error rc; int i; rc = (hackrf_error) hackrf_init(); if (rc != HACKRF_SUCCESS) { std::cerr << "HackRFSource::get_device_names: Failed to open HackRF library: " << rc << ": " << hackrf_error_name(rc) << std::endl; return; } hackrf_device_list_t *hackrf_devices = hackrf_device_list(); devices.clear(); for (i=0; i < hackrf_devices->devicecount; i++) { rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, i, &hackrf_ptr); if (rc == HACKRF_SUCCESS) { std::cerr << "HackRFSource::get_device_names: try to get device " << i << " serial number" << std::endl; rc = (hackrf_error) hackrf_board_partid_serialno_read(hackrf_ptr, &read_partid_serialno); if (rc != HACKRF_SUCCESS) { std::cerr << "HackRFSource::get_device_names: failed to get device " << i << " serial number: " << rc << ": " << hackrf_error_name(rc) << std::endl; hackrf_close(hackrf_ptr); continue; } else { std::cerr << "HackRFSource::get_device_names: device " << i << " OK" << std::endl; hackrf_close(hackrf_ptr); } uint32_t serial_msb = read_partid_serialno.serial_no[2]; uint32_t serial_lsb = read_partid_serialno.serial_no[3]; std::ostringstream devname_ostr; devname_ostr << "Serial " << std::hex << std::setw(8) << std::setfill('0') << serial_msb << serial_lsb; devices.push_back(devname_ostr.str()); } else { std::cerr << "HackRFSource::get_device_names: failed to open device " << i << std::endl; } } hackrf_device_list_free(hackrf_devices); rc = (hackrf_error) hackrf_exit(); std::cerr << "HackRFSource::get_device_names: HackRF library exit: " << rc << ": " << hackrf_error_name(rc) << std::endl; }
hackrf_device_list_t* ADDCALL hackrf_device_list() { ssize_t i; libusb_device_handle* usb_device = NULL; hackrf_device_list_t* list = calloc(1, sizeof(*list)); if ( list == NULL ) return NULL; list->usb_devicecount = libusb_get_device_list(g_libusb_context, (libusb_device ***)&list->usb_devices); list->serial_numbers = calloc(list->usb_devicecount, sizeof(void *)); list->usb_board_ids = calloc(list->usb_devicecount, sizeof(enum hackrf_usb_board_id)); list->usb_device_index = calloc(list->usb_devicecount, sizeof(int)); if ( list->serial_numbers == NULL || list->usb_board_ids == NULL || list->usb_device_index == NULL) { hackrf_device_list_free(list); return NULL; } for (i=0; i<list->usb_devicecount; i++) { struct libusb_device_descriptor device_descriptor; libusb_get_device_descriptor(list->usb_devices[i], &device_descriptor); if( device_descriptor.idVendor == hackrf_usb_vid ) { if((device_descriptor.idProduct == hackrf_one_usb_pid) || (device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) || (device_descriptor.idProduct == rad1o_usb_pid)) { int idx = list->devicecount++; list->usb_board_ids[idx] = device_descriptor.idProduct; list->usb_device_index[idx] = i; const uint_fast8_t serial_descriptor_index = device_descriptor.iSerialNumber; if( serial_descriptor_index > 0 ) { if( libusb_open(list->usb_devices[i], &usb_device) != 0 ) { usb_device = NULL; continue; } char serial_number[64]; const int serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number)); if( serial_number_length == 32 ) { serial_number[32] = 0; list->serial_numbers[idx] = strdup(serial_number); } libusb_close(usb_device); usb_device = NULL; } } } } return list; }
/* Entry point to C/C++ */ void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { int result =HACKRF_SUCCESS,i; struct hackrf_device *_device [10]; hackrf_device_list_t *list; uint8_t board_id = BOARD_ID_INVALID; char version[255 + 1]; read_partid_serialno_t read_partid_serialno; result = hackrf_init(); if (result != HACKRF_SUCCESS) { mexPrintf("hackrf_init() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } list = hackrf_device_list(); if (list->devicecount < 1 ) { mexPrintf("No HackRF boards found.\n"); } for (i = 0; i < list->devicecount; i++){ mexPrintf("Found HackRF board %d:\n", i); if (list->serial_numbers[i]) mexPrintf("USB descriptor string: %s\n", list->serial_numbers[i]); result = hackrf_device_list_open(list, i, &_device [i]); if (result != HACKRF_SUCCESS) { mexPrintf("hackrf_open() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } result = hackrf_board_id_read(_device[i], &board_id); if (result != HACKRF_SUCCESS) { mexPrintf("hackrf_board_id_read() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } mexPrintf("Board ID Number: %d (%s)\n", board_id,hackrf_board_id_name((hackrf_board_id)board_id)); result = hackrf_version_string_read(_device[i], &version[0], 255); if (result != HACKRF_SUCCESS) { mexPrintf("hackrf_version_string_read() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } printf("Firmware Version: %s\n", version); result = hackrf_board_partid_serialno_read(_device[i], &read_partid_serialno); if (result != HACKRF_SUCCESS) { mexPrintf( "hackrf_board_partid_serialno_read() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } printf("Part ID Number: 0x%08x 0x%08x\n", read_partid_serialno.part_id[0], read_partid_serialno.part_id[1]); printf("Serial Number: 0x%08x 0x%08x 0x%08x 0x%08x\n", read_partid_serialno.serial_no[0], read_partid_serialno.serial_no[1], read_partid_serialno.serial_no[2], read_partid_serialno.serial_no[3]); result = hackrf_close(_device[i]); if (result != HACKRF_SUCCESS) { mexPrintf("hackrf_close() failed: %s (%d)\n",hackrf_error_name((hackrf_error)result), result); } } hackrf_device_list_free(list); hackrf_exit(); }