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; }
static void mdlStart(SimStruct *S) /* ======================================================================== */ { int ret = HACKRF_SUCCESS; hackrf_device_list_t *list; struct hackrf_device *device; const uint32_t device_index = (uint32_t)mxGetScalar(ssGetSFcnParam(S, DEVICE_INDEX)); const uint64_t frequency = (uint64_t)mxGetScalar(ssGetSFcnParam(S, FREQUENCY)); const double sample_rate = mxGetScalar(ssGetSFcnParam(S, SAMPLE_RATE)); const uint32_t txvga = (uint32_t)mxGetScalar(ssGetSFcnParam(S, TXVGA)); const uint32_t amp = (uint32_t)mxGetScalar(ssGetSFcnParam(S, AMP)); const uint32_t bandwidth = (uint32_t)mxGetScalar(ssGetSFcnParam(S, BANDWIDTH)); /* Set options of this Block */ ssSetOptions(S, ssGetOptions(S) | SS_OPTION_CALL_TERMINATE_ON_EXIT); /* give handle to PWork vector */ ssSetPWorkValue(S, DEVICE, NULL); /* init HackRF device */ ret = hackrf_init(); if (ret < 0) { ssSetErrorStatusf(S, "Failed to init HackRF device #%d", device_index); return; } list = hackrf_device_list(); if (list->devicecount < 1) { ssSetErrorStatusf(S, "No HackRF boards found.\n"); return; } /* open HackRF device */ ret = hackrf_device_list_open(list, device_index, &device); if (ret < 0) { ssSetErrorStatusf(S, "Failed to open HackRF device #%d", device_index); return; } /* give handle to PWork vector */ ssSetPWorkValue(S, DEVICE, (struct hackrf_device *)device); ret = hackrf_set_sample_rate(device, sample_rate); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF Sample Rate #%d", device_index); } ret = hackrf_set_freq(device, frequency); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF frequency #%d", device_index); } ret = hackrf_set_baseband_filter_bandwidth(device, bandwidth); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF bandwidth #%d", device_index); } ret = hackrf_set_txvga_gain(device, txvga); ret |= hackrf_set_amp_enable(device, amp); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Set HackRF gain #%d", device_index); } /* create mutex for sample thread */ pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); //*mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_init(mutex, NULL); ssSetPWorkValue(S, MUTEX, mutex); /* create condition variable for sample thread */ pthread_cond_t *cond_var = (pthread_cond_t *)malloc(sizeof(pthread_cond_t)); pthread_cond_init(cond_var, NULL); ssSetPWorkValue(S, COND_VAR, cond_var); /* allocate memory for sample buffer */ SampleBuffer *sbuf = (SampleBuffer*)malloc(sizeof(SampleBuffer)); sbuf->num = BUF_NUM; sbuf->head = sbuf->offset = sbuf->count = sbuf->tail = 0; sbuf->samp_avail = BUF_SIZE / BYTES_PER_SAMPLE; sbuf->underrun = sbuf->underrun_before = false; sbuf->buf = (unsigned char **)malloc(sbuf->num * sizeof(unsigned char *)); if (sbuf->buf) { for (unsigned int i = 0; i < sbuf->num; ++i) sbuf->buf[i] = (unsigned char *)malloc(BUF_SIZE * sizeof(unsigned char)); } ssSetPWorkValue(S, SBUF, sbuf); ret = hackrf_start_tx(device, tx_callback, (void*)S); if (ret < 0) { ssSetErrorStatusf(S, "Failed to Start HackRF #%d", device_index); } }
// Open HackRF device. HackRFSource::HackRFSource(int dev_index) : m_dev(0), m_sampleRate(5000000), m_frequency(100000000), m_ppm(0), m_lnaGain(16), m_vgaGain(22), m_bandwidth(2500000), m_extAmp(false), m_biasAnt(false), m_running(false), m_thread(0) { hackrf_error rc = (hackrf_error) hackrf_init(); if (rc != HACKRF_SUCCESS) { std::ostringstream err_ostr; err_ostr << "Failed to open HackRF library (" << rc << ": " << hackrf_error_name(rc) << ")"; m_error = err_ostr.str(); m_dev = 0; } else { hackrf_device_list_t *hackrf_devices = hackrf_device_list(); rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, dev_index, &m_dev); if (rc != HACKRF_SUCCESS) { std::ostringstream err_ostr; err_ostr << "Failed to open HackRF device " << dev_index << " (" << rc << ": " << hackrf_error_name(rc) << ")"; m_error = err_ostr.str(); m_dev = 0; } } std::ostringstream lgains_ostr; for (int g: m_lgains) { lgains_ostr << g << " "; } m_lgainsStr = lgains_ostr.str(); std::ostringstream vgains_ostr; for (int g: m_vgains) { vgains_ostr << g << " "; } m_vgainsStr = vgains_ostr.str(); std::ostringstream bwfilt_ostr; bwfilt_ostr << std::fixed << std::setprecision(2); for (int b: m_bwfilt) { bwfilt_ostr << b * 1e-6 << " "; } m_bwfiltStr = bwfilt_ostr.str(); m_this = this; }
/* 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(); }