mirror of
https://github.com/gnss-sdr/gnss-sdr
synced 2024-12-15 04:30:33 +00:00
Initialize all members in the constructor
Replace C-style casts by C++ casts Fixes spelling errors Other minor cleaning for consistency
This commit is contained in:
parent
9a020457da
commit
a05952c966
@ -33,10 +33,8 @@
|
||||
#include "gr_complex_ip_packet_source.h"
|
||||
#include <gnuradio/io_signature.h>
|
||||
|
||||
//#include <cstdlib>
|
||||
|
||||
|
||||
#define FIFO_SIZE 1472000
|
||||
const int FIFO_SIZE = 1472000;
|
||||
|
||||
|
||||
/* 4 bytes IP address */
|
||||
@ -48,6 +46,7 @@ typedef struct gr_ip_address
|
||||
u_char byte4;
|
||||
} gr_ip_address;
|
||||
|
||||
|
||||
/* IPv4 header */
|
||||
typedef struct gr_ip_header
|
||||
{
|
||||
@ -64,6 +63,7 @@ typedef struct gr_ip_header
|
||||
u_int op_pad; // Option + Padding
|
||||
} gr_ip_header;
|
||||
|
||||
|
||||
/* UDP header*/
|
||||
typedef struct gr_udp_header
|
||||
{
|
||||
@ -73,6 +73,7 @@ typedef struct gr_udp_header
|
||||
u_short crc; // Checksum
|
||||
} gr_udp_header;
|
||||
|
||||
|
||||
gr_complex_ip_packet_source::sptr
|
||||
gr_complex_ip_packet_source::make(std::string src_device,
|
||||
std::string origin_address,
|
||||
@ -93,6 +94,7 @@ gr_complex_ip_packet_source::make(std::string src_device,
|
||||
IQ_swap_));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* The private constructor
|
||||
*/
|
||||
@ -108,7 +110,6 @@ gr_complex_ip_packet_source::gr_complex_ip_packet_source(std::string src_device,
|
||||
gr::io_signature::make(0, 0, 0),
|
||||
gr::io_signature::make(1, 4, item_size)) // 1 to 4 baseband complex channels
|
||||
{
|
||||
// constructor code here
|
||||
std::cout << "Start Ethernet packet capture\n";
|
||||
|
||||
d_n_baseband_channels = n_baseband_channels;
|
||||
@ -143,6 +144,8 @@ gr_complex_ip_packet_source::gr_complex_ip_packet_source(std::string src_device,
|
||||
d_sock_raw = 0;
|
||||
d_pcap_thread = NULL;
|
||||
descr = NULL;
|
||||
|
||||
memset(reinterpret_cast<char *>(&si_me), 0, sizeof(si_me));
|
||||
}
|
||||
|
||||
|
||||
@ -163,6 +166,7 @@ bool gr_complex_ip_packet_source::start()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Called by gnuradio to disable drivers, etc for i/o devices.
|
||||
bool gr_complex_ip_packet_source::stop()
|
||||
{
|
||||
@ -176,42 +180,44 @@ bool gr_complex_ip_packet_source::stop()
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool gr_complex_ip_packet_source::open()
|
||||
{
|
||||
char errbuf[PCAP_ERRBUF_SIZE];
|
||||
boost::mutex::scoped_lock lock(d_mutex); // hold mutex for duration of this function
|
||||
/* open device for reading */
|
||||
// open device for reading
|
||||
descr = pcap_open_live(d_src_device.c_str(), 1500, 1, 1000, errbuf);
|
||||
if (descr == NULL)
|
||||
{
|
||||
std::cout << "Error openning Ethernet device " << d_src_device << std::endl;
|
||||
printf("Fatal Error in pcap_open_live(): %s\n", errbuf);
|
||||
std::cout << "Error opening Ethernet device " << d_src_device << std::endl;
|
||||
std::cout << "Fatal Error in pcap_open_live(): " << std::string(errbuf) << std::endl;
|
||||
return false;
|
||||
}
|
||||
//bind UDP port to avoid automatic reply with ICMP port ureacheable packets from kernel
|
||||
// bind UDP port to avoid automatic reply with ICMP port unreachable packets from kernel
|
||||
d_sock_raw = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (d_sock_raw == -1)
|
||||
{
|
||||
std::cout << "Error openning UDP socket" << std::endl;
|
||||
std::cout << "Error opening UDP socket" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// zero out the structure
|
||||
memset((char *)&si_me, 0, sizeof(si_me));
|
||||
memset(reinterpret_cast<char *>(&si_me), 0, sizeof(si_me));
|
||||
|
||||
si_me.sin_family = AF_INET;
|
||||
si_me.sin_port = htons(d_udp_port);
|
||||
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
// bind socket to port
|
||||
if (bind(d_sock_raw, (struct sockaddr *)&si_me, sizeof(si_me)) == -1)
|
||||
if (bind(d_sock_raw, reinterpret_cast<struct sockaddr *>(&si_me), sizeof(si_me)) == -1)
|
||||
{
|
||||
std::cout << "Error openning UDP socket" << std::endl;
|
||||
std::cout << "Error opening UDP socket" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
gr_complex_ip_packet_source::~gr_complex_ip_packet_source()
|
||||
{
|
||||
if (d_pcap_thread != NULL)
|
||||
@ -222,35 +228,36 @@ gr_complex_ip_packet_source::~gr_complex_ip_packet_source()
|
||||
std::cout << "Stop Ethernet packet capture\n";
|
||||
}
|
||||
|
||||
|
||||
void gr_complex_ip_packet_source::static_pcap_callback(u_char *args, const struct pcap_pkthdr *pkthdr,
|
||||
const u_char *packet)
|
||||
{
|
||||
gr_complex_ip_packet_source *bridge = (gr_complex_ip_packet_source *)args;
|
||||
gr_complex_ip_packet_source *bridge = reinterpret_cast<gr_complex_ip_packet_source *>(args);
|
||||
bridge->pcap_callback(args, pkthdr, packet);
|
||||
}
|
||||
|
||||
|
||||
void gr_complex_ip_packet_source::pcap_callback(__attribute__((unused)) u_char *args, __attribute__((unused)) const struct pcap_pkthdr *pkthdr,
|
||||
const u_char *packet)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(d_mutex); // hold mutex for duration of this function
|
||||
|
||||
gr_ip_header *ih;
|
||||
gr_udp_header *uh;
|
||||
const gr_ip_header *ih;
|
||||
const gr_udp_header *uh;
|
||||
|
||||
// eth frame parameters
|
||||
// **** UDP RAW PACKET DECODER ****
|
||||
if ((packet[12] == 0x08) & (packet[13] == 0x00)) // IP FRAME
|
||||
{
|
||||
/* retireve the position of the ip header */
|
||||
ih = (gr_ip_header *)(packet +
|
||||
14); //length of ethernet header
|
||||
// retrieve the position of the ip header
|
||||
ih = reinterpret_cast<const gr_ip_header *>(packet + 14); // length of ethernet header
|
||||
|
||||
/* retireve the position of the udp header */
|
||||
// retrieve the position of the udp header
|
||||
u_int ip_len;
|
||||
ip_len = (ih->ver_ihl & 0xf) * 4;
|
||||
uh = (gr_udp_header *)((u_char *)ih + ip_len);
|
||||
uh = reinterpret_cast<const gr_udp_header *>(reinterpret_cast<const u_char *>(ih) + ip_len);
|
||||
|
||||
/* convert from network byte order to host byte order */
|
||||
// convert from network byte order to host byte order
|
||||
//u_short sport;
|
||||
u_short dport;
|
||||
dport = ntohs(uh->dport);
|
||||
@ -271,27 +278,27 @@ void gr_complex_ip_packet_source::pcap_callback(__attribute__((unused)) u_char *
|
||||
// dport);
|
||||
// std::cout<<"uh->len:"<<ntohs(uh->len)<<std::endl;
|
||||
|
||||
int payload_lenght_bytes = ntohs(uh->len) - 8; //total udp packet lenght minus the header lenght
|
||||
int payload_length_bytes = ntohs(uh->len) - 8; // total udp packet length minus the header length
|
||||
// read the payload bytes and insert them into the shared circular buffer
|
||||
u_char *udp_payload = ((u_char *)uh + sizeof(gr_udp_header));
|
||||
if (fifo_items <= (FIFO_SIZE - payload_lenght_bytes))
|
||||
const u_char *udp_payload = (reinterpret_cast<const u_char *>(uh) + sizeof(gr_udp_header));
|
||||
if (fifo_items <= (FIFO_SIZE - payload_length_bytes))
|
||||
{
|
||||
int aligned_write_items = FIFO_SIZE - fifo_write_ptr;
|
||||
if (aligned_write_items >= payload_lenght_bytes)
|
||||
if (aligned_write_items >= payload_length_bytes)
|
||||
{
|
||||
// write all in a single memcpy
|
||||
memcpy(&fifo_buff[fifo_write_ptr], &udp_payload[0], payload_lenght_bytes); //size in bytes
|
||||
fifo_write_ptr += payload_lenght_bytes;
|
||||
memcpy(&fifo_buff[fifo_write_ptr], &udp_payload[0], payload_length_bytes); // size in bytes
|
||||
fifo_write_ptr += payload_length_bytes;
|
||||
if (fifo_write_ptr == FIFO_SIZE) fifo_write_ptr = 0;
|
||||
fifo_items += payload_lenght_bytes;
|
||||
fifo_items += payload_length_bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
// two step wrap write
|
||||
memcpy(&fifo_buff[fifo_write_ptr], &udp_payload[0], aligned_write_items); // size in bytes
|
||||
fifo_write_ptr = payload_lenght_bytes - aligned_write_items;
|
||||
fifo_write_ptr = payload_length_bytes - aligned_write_items;
|
||||
memcpy(&fifo_buff[0], &udp_payload[aligned_write_items], fifo_write_ptr); // size in bytes
|
||||
fifo_items += payload_lenght_bytes;
|
||||
fifo_items += payload_length_bytes;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -303,12 +310,13 @@ void gr_complex_ip_packet_source::pcap_callback(__attribute__((unused)) u_char *
|
||||
}
|
||||
}
|
||||
|
||||
void gr_complex_ip_packet_source::my_pcap_loop_thread(pcap_t *pcap_handle)
|
||||
|
||||
void gr_complex_ip_packet_source::my_pcap_loop_thread(pcap_t *pcap_handle)
|
||||
{
|
||||
pcap_loop(pcap_handle, -1, gr_complex_ip_packet_source::static_pcap_callback, (u_char *)this);
|
||||
pcap_loop(pcap_handle, -1, gr_complex_ip_packet_source::static_pcap_callback, reinterpret_cast<u_char *>(this));
|
||||
}
|
||||
|
||||
|
||||
void gr_complex_ip_packet_source::demux_samples(gr_vector_void_star output_items, int num_samples_readed)
|
||||
{
|
||||
int8_t real;
|
||||
@ -325,15 +333,15 @@ void gr_complex_ip_packet_source::demux_samples(gr_vector_void_star output_items
|
||||
imag = fifo_buff[fifo_read_ptr++];
|
||||
if (d_IQ_swap)
|
||||
{
|
||||
(static_cast<gr_complex *>(output_items[i]))[n] = gr_complex(real, imag);
|
||||
static_cast<gr_complex *>(output_items[i])[n] = gr_complex(real, imag);
|
||||
}
|
||||
else
|
||||
{
|
||||
(static_cast<gr_complex *>(output_items[i]))[n] = gr_complex(imag, real);
|
||||
static_cast<gr_complex *>(output_items[i])[n] = gr_complex(imag, real);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2: // 4bits samples
|
||||
case 2: // 4-bit samples
|
||||
for (long unsigned int i = 0; i < output_items.size(); i++)
|
||||
{
|
||||
tmp_char2 = fifo_buff[fifo_read_ptr] & 0x0F;
|
||||
@ -357,11 +365,11 @@ void gr_complex_ip_packet_source::demux_samples(gr_vector_void_star output_items
|
||||
}
|
||||
if (d_IQ_swap)
|
||||
{
|
||||
(static_cast<gr_complex *>(output_items[i]))[n] = gr_complex(imag, real);
|
||||
static_cast<gr_complex *>(output_items[i])[n] = gr_complex(imag, real);
|
||||
}
|
||||
else
|
||||
{
|
||||
(static_cast<gr_complex *>(output_items[i]))[n] = gr_complex(real, imag);
|
||||
static_cast<gr_complex *>(output_items[i])[n] = gr_complex(real, imag);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -373,6 +381,7 @@ void gr_complex_ip_packet_source::demux_samples(gr_vector_void_star output_items
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int gr_complex_ip_packet_source::work(int noutput_items,
|
||||
__attribute__((unused)) gr_vector_const_void_star &input_items,
|
||||
gr_vector_void_star &output_items)
|
||||
|
Loading…
Reference in New Issue
Block a user