hackrf_common.h
Go to the documentation of this file.
/* -*- c++ -*- */
/*
* Copyright 2020 Clayton Smith <argilo@gmail.com>
*
* gr-osmosdr is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* gr-osmosdr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gr-osmosdr; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDED_HACKRF_COMMON_H
#define INCLUDED_HACKRF_COMMON_H
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <boost/format.hpp>
#include <osmosdr/ranges.h>
#include <libhackrf/hackrf.h>
#define BUF_LEN (16 * 32 * 512) /* must be multiple of 512 */
#define BUF_NUM 15
#define HACKRF_OC_PORT_COUNT 8
#define HACKRF_OC_PORTS_PER_SIDE 4
#define HACKRF_OC_PORT_INVALID -1
#define BYTES_PER_SAMPLE 2 /* HackRF device produces/consumes 8 bit signed IQ data */
#define HACKRF_FORMAT_ERROR(ret, msg) \
boost::str( boost::format(msg " (%1%) %2%") \
% ret % hackrf_error_name((enum hackrf_error)ret) )
#define HACKRF_THROW_ON_ERROR(ret, msg) \
if ( ret != HACKRF_SUCCESS ) \
{ \
throw std::runtime_error( HACKRF_FORMAT_ERROR(ret, msg) ); \
}
#define HACKRF_FUNC_STR(func, arg) \
boost::str(boost::format(func "(%1%)") % arg) + " has failed"
typedef std::shared_ptr<hackrf_device> hackrf_sptr;
class hackrf_common
{
public:
hackrf_common(const std::string &args);
protected:
static std::vector< std::string > get_devices();
osmosdr::meta_range_t get_sample_rates( void );
double set_sample_rate( double rate );
double get_sample_rate( void );
osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
double set_center_freq( double freq, size_t chan = 0 );
double get_center_freq( size_t chan = 0 );
double set_freq_corr( double ppm, size_t chan = 0 );
double get_freq_corr( size_t chan = 0 );
bool set_gain_mode( bool automatic, size_t chan = 0 );
bool get_gain_mode( size_t chan = 0 );
double set_gain( double gain, size_t chan = 0 );
double get_gain( size_t chan = 0 );
std::vector< std::string > get_antennas( size_t chan = 0 );
std::string set_antenna( const std::string & antenna, size_t chan = 0 );
std::string get_antenna( size_t chan = 0 );
double set_bandwidth( double bandwidth, size_t chan = 0 );
double get_bandwidth( size_t chan = 0 );
osmosdr::freq_range_t get_bandwidth_range( size_t chan = 0 );
bool set_bias( bool bias );
bool get_bias();
void start();
void stop();
hackrf_sptr _dev;
private:
static void close(void *dev);
#ifdef HACKRF_OPERACAKE_SUPPORT
uint8_t get_operacake_address();
std::string port2str(int port);
int str2port(std::string const &str);
#endif
static int _usage;
static std::mutex _usage_mutex;
static std::map<std::string, std::weak_ptr<hackrf_device>> _devs;
static std::mutex _devs_mutex;
double _sample_rate;
double _center_freq;
double _freq_corr;
bool _auto_gain;
double _amp_gain;
double _requested_bandwidth;
double _bandwidth;
bool _bias;
bool _started;
int _selected_port = HACKRF_OC_PORT_INVALID;
};
#endif /* INCLUDED_HACKRF_COMMON_H */
hackrf_common::hackrf_common
hackrf_common(const std::string &args)
hackrf_common::set_antenna
std::string set_antenna(const std::string &antenna, size_t chan=0)
hackrf_common::get_antennas
std::vector< std::string > get_antennas(size_t chan=0)
hackrf_common::stop
void stop()
hackrf_common::set_sample_rate
double set_sample_rate(double rate)
hackrf_common::get_center_freq
double get_center_freq(size_t chan=0)
hackrf_common::set_bias
bool set_bias(bool bias)
hackrf_common::get_sample_rates
osmosdr::meta_range_t get_sample_rates(void)
hackrf_common::get_freq_corr
double get_freq_corr(size_t chan=0)
hackrf_common::get_bandwidth
double get_bandwidth(size_t chan=0)
hackrf_common::set_freq_corr
double set_freq_corr(double ppm, size_t chan=0)
hackrf_common::set_bandwidth
double set_bandwidth(double bandwidth, size_t chan=0)
hackrf_common::set_center_freq
double set_center_freq(double freq, size_t chan=0)
hackrf_common::get_antenna
std::string get_antenna(size_t chan=0)
hackrf_common::get_devices
static std::vector< std::string > get_devices()
hackrf_common::set_gain_mode
bool set_gain_mode(bool automatic, size_t chan=0)
hackrf_common::get_gain
double get_gain(size_t chan=0)
hackrf_common::get_sample_rate
double get_sample_rate(void)
hackrf_common::get_gain_mode
bool get_gain_mode(size_t chan=0)
hackrf_common::set_gain
double set_gain(double gain, size_t chan=0)
hackrf_common::start
void start()
hackrf_common::_dev
hackrf_sptr _dev
Definition hackrf_common.h:95
hackrf_common::get_freq_range
osmosdr::freq_range_t get_freq_range(size_t chan=0)
hackrf_common::get_bandwidth_range
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
hackrf_common::get_bias
bool get_bias()
HACKRF_OC_PORT_INVALID
#define HACKRF_OC_PORT_INVALID
Definition hackrf_common.h:39
osmosdr::freq_range_t
meta_range_t freq_range_t
Definition ranges.h:125
ranges.h
osmosdr::meta_range_t
Definition ranges.h:75