GNU Radio's TEST Package
hackrf_common.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2020 Clayton Smith <argilo@gmail.com>
4  *
5  * gr-osmosdr is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3, or (at your option)
8  * any later version.
9  *
10  * gr-osmosdr is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with gr-osmosdr; see the file COPYING. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifndef INCLUDED_HACKRF_COMMON_H
21 #define INCLUDED_HACKRF_COMMON_H
22 
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 #include <boost/format.hpp>
30 
31 #include <osmosdr/ranges.h>
32 #include <libhackrf/hackrf.h>
33 
34 #define BUF_LEN (16 * 32 * 512) /* must be multiple of 512 */
35 #define BUF_NUM 15
36 
37 #define HACKRF_OC_PORT_COUNT 8
38 #define HACKRF_OC_PORTS_PER_SIDE 4
39 #define HACKRF_OC_PORT_INVALID -1
40 
41 #define BYTES_PER_SAMPLE 2 /* HackRF device produces/consumes 8 bit signed IQ data */
42 
43 #define HACKRF_FORMAT_ERROR(ret, msg) \
44  boost::str( boost::format(msg " (%1%) %2%") \
45  % ret % hackrf_error_name((enum hackrf_error)ret) )
46 
47 #define HACKRF_THROW_ON_ERROR(ret, msg) \
48  if ( ret != HACKRF_SUCCESS ) \
49  { \
50  throw std::runtime_error( HACKRF_FORMAT_ERROR(ret, msg) ); \
51  }
52 
53 #define HACKRF_FUNC_STR(func, arg) \
54  boost::str(boost::format(func "(%1%)") % arg) + " has failed"
55 
56 typedef std::shared_ptr<hackrf_device> hackrf_sptr;
57 
59 {
60 public:
61  hackrf_common(const std::string &args);
62 
63 protected:
64  static std::vector< std::string > get_devices();
65 
67  double set_sample_rate( double rate );
68  double get_sample_rate( void );
69 
71  double set_center_freq( double freq, size_t chan = 0 );
72  double get_center_freq( size_t chan = 0 );
73  double set_freq_corr( double ppm, size_t chan = 0 );
74  double get_freq_corr( size_t chan = 0 );
75 
76  bool set_gain_mode( bool automatic, size_t chan = 0 );
77  bool get_gain_mode( size_t chan = 0 );
78  double set_gain( double gain, size_t chan = 0 );
79  double get_gain( size_t chan = 0 );
80 
81  std::vector< std::string > get_antennas( size_t chan = 0 );
82  std::string set_antenna( const std::string & antenna, size_t chan = 0 );
83  std::string get_antenna( size_t chan = 0 );
84 
85  double set_bandwidth( double bandwidth, size_t chan = 0 );
86  double get_bandwidth( size_t chan = 0 );
88 
89  bool set_bias( bool bias );
90  bool get_bias();
91 
92  void start();
93  void stop();
94 
95  hackrf_sptr _dev;
96 
97 private:
98  static void close(void *dev);
99 
100 #ifdef HACKRF_OPERACAKE_SUPPORT
101  uint8_t get_operacake_address();
102  std::string port2str(int port);
103  int str2port(std::string const &str);
104 #endif
105 
106  static int _usage;
107  static std::mutex _usage_mutex;
108 
109  static std::map<std::string, std::weak_ptr<hackrf_device>> _devs;
110  static std::mutex _devs_mutex;
111 
112  double _sample_rate;
113  double _center_freq;
114  double _freq_corr;
115  bool _auto_gain;
116  double _amp_gain;
117  double _requested_bandwidth;
118  double _bandwidth;
119  bool _bias;
120  bool _started;
121  int _selected_port = HACKRF_OC_PORT_INVALID;
122 };
123 
124 #endif /* INCLUDED_HACKRF_COMMON_H */
Definition: hackrf_common.h:59
hackrf_common(const std::string &args)
std::string set_antenna(const std::string &antenna, size_t chan=0)
std::vector< std::string > get_antennas(size_t chan=0)
double set_sample_rate(double rate)
double get_center_freq(size_t chan=0)
bool set_bias(bool bias)
osmosdr::meta_range_t get_sample_rates(void)
double get_freq_corr(size_t chan=0)
double get_bandwidth(size_t chan=0)
double set_freq_corr(double ppm, size_t chan=0)
double set_bandwidth(double bandwidth, size_t chan=0)
double set_center_freq(double freq, size_t chan=0)
std::string get_antenna(size_t chan=0)
static std::vector< std::string > get_devices()
bool set_gain_mode(bool automatic, size_t chan=0)
double get_gain(size_t chan=0)
double get_sample_rate(void)
bool get_gain_mode(size_t chan=0)
double set_gain(double gain, size_t chan=0)
hackrf_sptr _dev
Definition: hackrf_common.h:95
osmosdr::freq_range_t get_freq_range(size_t chan=0)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
#define HACKRF_OC_PORT_INVALID
Definition: hackrf_common.h:39
Definition: ranges.h:75