3 * Copyright 2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
23 //#define MSDD_DEBUG_TRUE
24 //#define MSDD_DEBUG2_TRUE
30 #include <msdd_source_base.h>
31 #include <gr_io_signature.h>
34 #include <omnithread.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
39 #ifdef MSDD_DEBUG_TRUE
41 #define MSDD_DEBUG(x) std::cout << x << std::endl;
47 #ifdef MSDD_DEBUG2_TRUE
49 #define MSDD_DEBUG2(x) std::cout << x << std::endl;
51 #define MSDD_DEBUG2(x)
57 const int OUTPUT_MAX((1 << 15)*8);
58 const double PGA_MAX(75);
59 const double PGA_MIN(10);
60 const double PGA_STEP(.5);
61 const double DEFAULT_RX_FREQ(2.417e6);
62 const double DEFAULT_GAIN(32);
63 const msdd_source_base::msdd_fft_mode_t DEFAULT_FFT_MODE(msdd_source_base::MODE_MAG);
64 const msdd_source_base::msdd_fft_points_t DEFAULT_FFT_POINTS(msdd_source_base::S8192);
65 const msdd_source_base::msdd_decimation_t DEFAULT_DECIMATION_RATE(msdd_source_base::D2);
68 class msdd_source_base::Impl {
73 d_deci_rate (DEFAULT_DECIMATION_RATE),
74 d_rx_freq ((unsigned long) DEFAULT_RX_FREQ),
78 d_msdd_command_type((msdd_command_type_t) opp_mode),
79 d_msdd_fft_mode(DEFAULT_FFT_MODE),
80 d_desired_sample_size(2^15),
81 d_fft_points (DEFAULT_FFT_POINTS)
87 msdd_decimation_t d_deci_rate;
88 unsigned long d_rx_freq;
92 msdd_command_type_t d_msdd_command_type;
93 msdd_fft_mode_t d_msdd_fft_mode;
94 unsigned long d_desired_sample_size;
96 int d_socket; // handle to socket
97 int d_socket_rcv; // handle to socket retuned in the accept call
98 struct in_addr d_ip_src; // store the source IP address to use
99 unsigned short d_port_src; // the port number to open for connections to this service
100 sockaddr_in d_sockaddr_src; // store the source sockaddr data (formatted IP address and port number)
101 std::auto_ptr<unsigned char> d_temp_buff; // hold buffer between calls
104 msdd_fft_points_t d_fft_points;
106 struct msdd_request_fft_packet {
107 msdd_command_type_t command_type;
109 unsigned int center_freq_mhz;
112 msdd_fft_window_type_t window_type;
113 msdd_fft_points_t fft_points;
114 msdd_decimation_t decimation;
115 msdd_fft_mode_t fft_mode;
117 } __attribute__((__packed__));
119 struct msdd_request_iq_packet {
120 msdd_command_type_t command_type;
122 unsigned int center_freq_mhz;
126 msdd_decimation_t decimation;
128 } __attribute__((__packed__));
130 void make_request_fft_packet(msdd_request_fft_packet& packet);
132 void make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples);
134 msdd_request_fft_packet d_fft_request_packet; // fft request packet
135 msdd_request_iq_packet d_iq_request_packet; // fft request packet
139 msdd_source_base::msdd_source_base (const std::string &name,
140 gr_io_signature_sptr output_signature,
144 unsigned short port_src
145 ) throw (std::runtime_error)
146 : gr_sync_block (name,
147 gr_make_io_signature (0, 0, 0),
149 pimpl( new Impl(opp_mode))
154 // Set up the address stucture for the source address and port numbers
155 // Get the source IP address from the host name
156 struct hostent *hsrc (gethostbyname(src));
158 if(hsrc) { // if the source was provided as a host namex
159 pimpl->d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];
161 else { // assume it was specified as an IP address
162 if((ret=inet_aton(src, &pimpl->d_ip_src)) == 0) { // format IP address
163 perror("Not a valid source IP address or host name");
164 throw std::runtime_error("can't initialize source socket");
168 pimpl->d_port_src = htons(port_src); // format port number
170 pimpl->d_sockaddr_src.sin_family = AF_INET;
171 pimpl->d_sockaddr_src.sin_addr = pimpl->d_ip_src;
172 pimpl->d_sockaddr_src.sin_port = pimpl->d_port_src;
174 pimpl->d_temp_buff.reset(new unsigned char[OUTPUT_MAX +
175 std::max(sizeof(Impl::msdd_request_iq_packet),
176 sizeof(Impl::msdd_request_fft_packet))]); // allow it to hold up to payload_size bytes
178 set_output_multiple (OUTPUT_MAX / output_signature->sizeof_stream_item (0));
183 msdd_source_base::open()
185 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
187 MSDD_DEBUG2("MSDD: Before socket ")
188 pimpl->d_socket = socket(PF_INET, SOCK_STREAM, 0);
189 if(pimpl->d_socket == -1) {
190 perror("socket open");
191 throw std::runtime_error("can't open socket");
194 // Turn on reuse address
196 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&opt_val, sizeof(int)) == -1) {
197 perror("SO_REUSEADDR");
198 throw std::runtime_error("can't set socket option SO_REUSEADDR");
201 // Don't wait when shutting down
205 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_LINGER, (void*)&lngr, sizeof(linger)) == -1) {
207 throw std::runtime_error("can't set socket option SO_LINGER");
210 // Set a timeout on the receive function to not block indefinitely
211 // This value can (and probably should) be changed
213 // timeout.tv_sec = 1;
214 // timeout.tv_usec = 0;
215 // if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof(timeout)) == -1) {
216 // perror("SO_RCVTIMEO");
217 // throw std::runtime_error("can't set socket option SO_RCVTIMEO");
220 // bind socket to an address and port number to listen on
221 MSDD_DEBUG2("MSDD: Before socket bind to " << pimpl->d_sockaddr_src.sin_port)
222 if(::connect(pimpl->d_socket, (struct sockaddr*)&pimpl->d_sockaddr_src, sizeof(pimpl->d_sockaddr_src)) == -1) {
223 perror("socket bind");
224 throw std::runtime_error("can't bind socket");
227 MSDD_DEBUG2("MSDD: Socket open")
228 pimpl->d_updated = true;
229 return pimpl->d_socket != 0;
232 /* read n bytes from a socket descriptor */
234 msdd_source_base::readsock(int sockfd, unsigned char* buf, int nbytes) {
239 MSDD_DEBUG2("MSDD: Before socket read: " << nleft)
240 if ((nread = ::read(sockfd, buf, nleft)) < 0) {
241 return(nread); /* error, nread < 0 */
242 } else if (nread == 0) {
249 return(nbytes - nleft);
253 msdd_source_base::close()
255 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
257 if (pimpl->d_socket){
258 shutdown(pimpl->d_socket, SHUT_RDWR);
261 pimpl->d_updated = true;
266 msdd_source_base::~msdd_source_base ()
268 msdd_source_base::close();
272 msdd_source_base::sizeof_basic_sample() const
274 switch (pimpl->d_msdd_command_type) {
275 case SAMPLES_REALTIME:
278 switch (pimpl->d_msdd_fft_mode) {
285 assert (false); // bad mode
288 assert (false); // bad mode
293 msdd_source_base::start()
295 return msdd_source_base::open();
299 msdd_source_base::stop()
301 return msdd_source_base::close();
305 msdd_source_base::make_request_packet(unsigned int& size, unsigned int number_samples) {
306 switch (pimpl->d_msdd_command_type) {
307 case SAMPLES_REALTIME:
308 pimpl->make_request_iq_packet(pimpl->d_iq_request_packet, number_samples);
309 size = sizeof (pimpl->d_iq_request_packet);
310 return &pimpl->d_iq_request_packet;
312 pimpl->make_request_fft_packet(pimpl->d_fft_request_packet);
313 size = sizeof (pimpl->d_fft_request_packet);
314 return &pimpl->d_fft_request_packet;
316 assert (false); // bad mode
321 msdd_source_base::Impl::make_request_fft_packet(msdd_request_fft_packet& packet)
323 packet.command_type = SAMPLES_FFT; // FFT samples Command
324 packet.foo_x20 = 0x20;
325 packet.center_freq_mhz = d_rx_freq;
326 packet.offset_freq_hz = 0;
327 packet.gain = (int) d_gain; // gain
328 packet.window_type = WINDOW_HANNING; // magic number
329 packet.fft_points = d_fft_points;
330 packet.decimation = d_deci_rate;
331 packet.fft_mode = MODE_MAGDB;
332 packet.number_sets = 1;
336 msdd_source_base::Impl::make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples)
338 packet.command_type = SAMPLES_REALTIME; // FFT samples Command
339 packet.foo0x18 = 0x18; // magic number
340 packet.center_freq_mhz = d_rx_freq;
341 packet.offset_freq_hz = 0;
342 packet.gain = (int) d_gain; // gain
343 packet.number = number_samples * 4;
344 packet.decimation = d_deci_rate;
345 packet.number_sets = 1;
349 msdd_source_base::work (int noutput_items,
350 gr_vector_const_void_star &input_items,
351 gr_vector_void_star &output_items)
353 int output_index (0);
354 int output_items_produced;
357 unsigned int packet_size;
359 MSDD_DEBUG("MSDD: requested items: " << noutput_items)
360 int noutput_items_desired = std::min (noutput_items, (int) pimpl->d_desired_sample_size);
361 MSDD_DEBUG("MSDD: desired items: " << noutput_items_desired)
363 while (output_index < noutput_items_desired){
365 int nbytes = (pimpl->d_msdd_command_type == SAMPLES_REALTIME) ?
366 ninput_bytes_reqd_for_noutput_items (noutput_items_desired - output_index) :
367 ninput_bytes_reqd_for_noutput_items (msdd_source_base::fft_points());
369 void* request_packet = msdd_source_base::make_request_packet(packet_size, noutput_items_desired);
371 nbytes = std::min (nbytes, OUTPUT_MAX);
372 MSDD_DEBUG2("MSDD: payload sizes: nbytes1: " << nbytes )
375 int result_nbytes = ::write(pimpl->d_socket, request_packet, packet_size);
376 //assert (result_nbytes == sizeof(msdd_request_packet));
379 result_nbytes = ::read (pimpl->d_socket, (unsigned char*) request_packet, packet_size);
380 MSDD_DEBUG2("MSDD: response: " << result_nbytes)
381 //assert (result_nbytes == sizeof(msdd_request_packet));
384 result_nbytes = msdd_source_base::readsock (pimpl->d_socket, pimpl->d_temp_buff.get(), nbytes);
385 MSDD_DEBUG("MSDD: reading bytes: " << nbytes << " received: " << result_nbytes)
386 if (result_nbytes > (int) nbytes){
387 // fprintf (stderr, "msdd_source: overrun\n");
388 fputs ("uO", stderr);
389 pimpl->d_noverruns++;
390 result_nbytes = nbytes; // truncate
393 if (result_nbytes < 0) // We've got a problem. Usually board unplugged or powered down.
394 return -1; // Indicate we're done.
396 if (result_nbytes != nbytes){ // not really an error, but unexpected
397 fprintf (stderr, "msdd_source: short read. Expected %d, got %d\n",
398 nbytes, result_nbytes);
401 copy_from_msdd_buffer (output_items,
403 noutput_items_desired - output_index, // output_items_available
404 output_items_produced, // [out]
405 pimpl->d_temp_buff.get(), // usrp_buffer
407 bytes_read); // [out]
409 output_index += output_items_produced;
411 if (pimpl->d_msdd_command_type == SAMPLES_FFT) break;
414 MSDD_DEBUG("MSDD: items produced: " << output_items_produced << " index: " << output_index)
422 msdd_source_base::set_decim_rate (unsigned int rate)
427 pimpl->d_deci_rate = D0;
430 pimpl->d_deci_rate = D1;
433 pimpl->d_deci_rate = D2;
436 pimpl->d_deci_rate = D3;
439 pimpl->d_deci_rate = D4;
442 pimpl->d_deci_rate = D5;
445 pimpl->d_deci_rate = D6;
448 pimpl->d_deci_rate = D7;
451 pimpl->d_deci_rate = D8;
461 //msdd_source_base::set_nchannels (int nchan)
463 // // return d_usrp->set_nchannels (nchan);
468 //msdd_source_base::set_mux (int mux)
470 // return d_usrp->set_mux (mux);
474 msdd_source_base::set_rx_freq (int channel, double freq)
476 assert (channel == 0);
479 if (freq >= 30e6 && freq <= 6e9) {
480 pimpl->d_rx_freq = (unsigned long) freq / 1000000;
489 msdd_source_base::set_fft_size (int channel, unsigned long fft_size)
491 assert (channel == 1);
495 pimpl->d_fft_points = S256;
498 pimpl->d_fft_points = S512;
501 pimpl->d_fft_points = S1024;
504 pimpl->d_fft_points = S2048;
507 pimpl->d_fft_points = S4096;
510 pimpl->d_fft_points = S8192;
513 pimpl->d_fft_points = S16384;
516 pimpl->d_fft_points = S32768;
520 return msdd_source_base::fft_points();
525 //msdd_source_base::fpga_master_clock_freq() const
527 // return d_usrp->fpga_master_clock_freq();
531 //msdd_source_base::converter_rate() const
533 // // return d_usrp->converter_rate();
538 msdd_source_base::decim_rate () const
540 return 1 << pimpl->d_deci_rate;
544 //msdd_source_base::nchannels () const
546 // return d_usrp->nchannels ();
550 //msdd_source_base::mux () const
552 // return d_usrp->mux ();
556 msdd_source_base::rx_freq (int channel) const
558 assert (channel == 0);
560 return pimpl->d_rx_freq;
564 msdd_source_base::fft_points() const
566 return (1 << pimpl->d_fft_points);
570 msdd_source_base::noverruns () const
572 return pimpl->d_noverruns;
576 //msdd_source_base::set_fpga_mode (int mode)
578 // return d_usrp->set_fpga_mode (mode);
582 //msdd_source_base::set_ddc_phase (int channel, int phase)
584 // return d_usrp->set_ddc_phase(channel, phase);
588 //msdd_source_base::set_dc_offset_cl_enable(int bits, int mask)
590 // return d_usrp->set_dc_offset_cl_enable(bits, mask);
594 msdd_source_base::set_verbose (bool verbose)
596 pimpl->d_verbose = verbose;
600 //msdd_source_base::write_aux_dac (int which_dboard, int which_dac, int value)
602 // return d_usrp->write_aux_dac (which_dboard, which_dac, value);
606 //msdd_source_base::read_aux_adc (int which_dboard, int which_adc)
608 // return d_usrp->read_aux_adc (which_dboard, which_adc);
612 //msdd_source_base::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
614 // return d_usrp->write_eeprom (i2c_addr, eeprom_offset, buf);
618 //msdd_source_base::read_eeprom (int i2c_addr, int eeprom_offset, int len)
620 // return d_usrp->read_eeprom (i2c_addr, eeprom_offset, len);
624 //msdd_source_base::write_i2c (int i2c_addr, const std::string buf)
626 // return d_usrp->write_i2c (i2c_addr, buf);
630 //msdd_source_base::read_i2c (int i2c_addr, int len)
632 // return d_usrp->read_i2c (i2c_addr, len);
636 msdd_source_base::set_pga (int which, double gain)
638 if (gain >= PGA_MIN & gain <= PGA_MAX) {
639 pimpl->d_gain = gain;
646 msdd_source_base::pga (int which) const
648 return pimpl->d_gain;
652 msdd_source_base::pga_min () const
658 msdd_source_base::pga_max () const
664 msdd_source_base::pga_db_per_step () const
670 //msdd_source_base::daughterboard_id (int which) const
672 // return d_usrp->daughterboard_id (which);
677 //msdd_source_base::set_adc_offset (int which, int offset)
679 // return d_usrp->set_adc_offset (which, offset);
683 //msdd_source_base::set_dac_offset (int which, int offset, int offset_pin)
685 // return d_usrp->set_dac_offset (which, offset, offset_pin);
689 //msdd_source_base::set_adc_buffer_bypass (int which, bool bypass)
691 // return d_usrp->set_adc_buffer_bypass (which, bypass);
695 msdd_source_base::serial_number()
697 return "SoftTronics MSDD 6000";
701 //msdd_source_base::_write_oe (int which_dboard, int value, int mask)
703 // return d_usrp->_write_oe (which_dboard, value, mask);
707 //msdd_source_base::write_io (int which_dboard, int value, int mask)
709 // return d_usrp->write_io (which_dboard, value, mask);
713 //msdd_source_base::read_io (int which_dboard)
715 // return d_usrp->read_io (which_dboard);
721 //// internal routines...
724 //msdd_source_base::_write_fpga_reg (int regno, int value)
726 // return d_usrp->_write_fpga_reg (regno, value);
730 //msdd_source_base::_write_fpga_reg_masked (int regno, int value, int mask)
732 // return d_usrp->_write_fpga_reg_masked (regno, value, mask);
736 //msdd_source_base::_read_fpga_reg (int regno)
738 // return d_usrp->_read_fpga_reg (regno);
742 //msdd_source_base::_write_9862 (int which_codec, int regno, unsigned char value)
744 // return d_usrp->_write_9862 (which_codec, regno, value);
748 //msdd_source_base::_read_9862 (int which_codec, int regno) const
750 // return d_usrp->_read_9862 (which_codec, regno);
754 //msdd_source_base::_write_spi (int optional_header, int enables,
755 // int format, std::string buf)
757 // return d_usrp->_write_spi (optional_header, enables, format, buf);
761 //msdd_source_base::_read_spi (int optional_header, int enables, int format, int len)
763 // return d_usrp->_read_spi (optional_header, enables, format, len);
767 //msdd_source_base::set_format(unsigned int format)
769 // return d_usrp->set_format(format);
773 //msdd_source_base::format() const
775 // return d_usrp->format();
779 //msdd_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
781 // return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
785 //msdd_source_base::format_width(unsigned int format)
787 // return usrp_standard_rx::format_width(format);
791 //msdd_source_base::format_shift(unsigned int format)
793 // return usrp_standard_rx::format_shift(format);
797 //msdd_source_base::format_want_q(unsigned int format)
799 // return usrp_standard_rx::format_want_q(format);
803 //msdd_source_base::format_bypass_halfband(unsigned int format)
805 // return usrp_standard_rx::format_bypass_halfband(format);
808 bool msdd_source_base::set_desired_packet_size (int which, unsigned long packet_size) {
811 if (pimpl->d_desired_sample_size < 2^32) { // FIXME: find maximum sample request for MSDD check if greater than
812 pimpl->d_desired_sample_size = packet_size;
817 unsigned long msdd_source_base::desired_packet_size (int which) const {
818 return pimpl->d_desired_sample_size;