GNSS-SDR  0.0.13
An Open Source GNSS Software Defined Receiver
gr_complex_ip_packet_source.h
Go to the documentation of this file.
1 /*!
2  * \file gr_complex_ip_packet_source.h
3  *
4  * \brief Receives ip frames containing samples in UDP frame encapsulation
5  * using a high performance packet capture library (libpcap)
6  * \author Javier Arribas jarribas (at) cttc.es
7  * -----------------------------------------------------------------------------
8  *
9  * Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
10  *
11  * GNSS-SDR is a software defined Global Navigation
12  * Satellite Systems receiver
13  *
14  * This file is part of GNSS-SDR.
15  *
16  * SPDX-License-Identifier: GPL-3.0-or-later
17  *
18  * -----------------------------------------------------------------------------
19  */
20 
21 
22 #ifndef GNSS_SDR_GR_COMPLEX_IP_PACKET_SOURCE_H
23 #define GNSS_SDR_GR_COMPLEX_IP_PACKET_SOURCE_H
24 
25 #include <boost/thread.hpp>
26 #include <gnuradio/sync_block.h>
27 #include <arpa/inet.h>
28 #include <net/ethernet.h>
29 #include <net/if.h>
30 #include <netinet/if_ether.h>
31 #include <pcap.h>
32 #include <string>
33 #include <sys/ioctl.h>
34 #if GNURADIO_USES_STD_POINTERS
35 #include <memory>
36 #else
37 #include <boost/shared_ptr.hpp>
38 #endif
39 
40 class Gr_Complex_Ip_Packet_Source : virtual public gr::sync_block
41 {
42 public:
43 #if GNURADIO_USES_STD_POINTERS
44  typedef std::shared_ptr<Gr_Complex_Ip_Packet_Source> sptr;
45 #else
46  typedef boost::shared_ptr<Gr_Complex_Ip_Packet_Source> sptr;
47 #endif
48  static sptr make(std::string src_device,
49  const std::string &origin_address,
50  int udp_port,
51  int udp_packet_size,
52  int n_baseband_channels,
53  const std::string &wire_sample_type,
54  size_t item_size,
55  bool IQ_swap_);
56  Gr_Complex_Ip_Packet_Source(std::string src_device,
57  const std::string &origin_address,
58  int udp_port,
59  int udp_packet_size,
60  int n_baseband_channels,
61  const std::string &wire_sample_type,
62  size_t item_size,
63  bool IQ_swap_);
65 
66  // Called by gnuradio to enable drivers, etc for i/o devices.
67  bool start();
68 
69  // Called by gnuradio to disable drivers, etc for i/o devices.
70  bool stop();
71 
72  // Where all the action really happens
73  int work(int noutput_items,
74  gr_vector_const_void_star &input_items,
75  gr_vector_void_star &output_items);
76 
77 private:
78  void demux_samples(const gr_vector_void_star &output_items, int num_samples_readed);
79  void my_pcap_loop_thread(pcap_t *pcap_handle);
80  void pcap_callback(u_char *args, const struct pcap_pkthdr *pkthdr, const u_char *packet);
81  static void static_pcap_callback(u_char *args, const struct pcap_pkthdr *pkthdr, const u_char *packet);
82  /*
83  * Opens the ethernet device using libpcap raw capture mode
84  * If any of these fail, the function returns the error and exits.
85  */
86  bool open();
87 
88  boost::thread *d_pcap_thread;
89  boost::mutex d_mutex;
90  struct sockaddr_in si_me
91  {
92  };
93  std::string d_src_device;
94  std::string d_origin_address;
95  pcap_t *descr; // ethernet pcap device descriptor
96  size_t d_item_size;
97  char *fifo_buff;
98  int fifo_read_ptr;
99  int fifo_write_ptr;
100  int fifo_items;
101  int d_sock_raw;
102  int d_udp_port;
103  int d_udp_payload_size;
104  int d_n_baseband_channels;
105  int d_wire_sample_type;
106  int d_bytes_per_sample;
107  bool d_IQ_swap;
108  bool d_fifo_full;
109 };
110 
111 #endif // GNSS_SDR_GR_COMPLEX_IP_PACKET_SOURCE_H