3 * Copyright 2004,2008 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>
33 #include <omnithread.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
47 #ifdef MSDD_DEBUG_TRUE
48 #define MSDD_DEBUG(x) std::cout << x << std::endl;
54 #ifdef MSDD_DEBUG2_TRUE
55 #define MSDD_DEBUG2(x) std::cout << x << std::endl;
57 #define MSDD_DEBUG2(x)
62 const int OUTPUT_MAX((1 << 15)*8);
63 const double PGA_MAX(75);
64 const double PGA_MIN(10);
65 const double PGA_STEP(.5);
66 const double DEFAULT_RX_FREQ(2.417e6);
67 const double DEFAULT_GAIN(32);
68 const msdd_source_base::msdd_fft_mode_t DEFAULT_FFT_MODE(msdd_source_base::MODE_MAG);
69 const msdd_source_base::msdd_fft_points_t DEFAULT_FFT_POINTS(msdd_source_base::S8192);
70 const msdd_source_base::msdd_decimation_t DEFAULT_DECIMATION_RATE(msdd_source_base::D2);
73 class msdd_source_base::Impl {
78 d_deci_rate (DEFAULT_DECIMATION_RATE),
79 d_rx_freq ((unsigned long) DEFAULT_RX_FREQ),
83 d_msdd_command_type((msdd_command_type_t) opp_mode),
84 d_msdd_fft_mode(DEFAULT_FFT_MODE),
85 d_desired_sample_size(2^15),
86 d_fft_points (DEFAULT_FFT_POINTS)
92 msdd_decimation_t d_deci_rate;
93 unsigned long d_rx_freq;
97 msdd_command_type_t d_msdd_command_type;
98 msdd_fft_mode_t d_msdd_fft_mode;
99 unsigned long d_desired_sample_size;
101 int d_socket; // handle to socket
102 int d_socket_rcv; // handle to socket retuned in the accept call
103 struct in_addr d_ip_src; // store the source IP address to use
104 unsigned short d_port_src; // the port number to open for connections to this service
105 sockaddr_in d_sockaddr_src; // store the source sockaddr data (formatted IP address and port number)
106 std::auto_ptr<unsigned char> d_temp_buff; // hold buffer between calls
109 msdd_fft_points_t d_fft_points;
111 struct msdd_request_fft_packet {
112 msdd_command_type_t command_type;
114 unsigned int center_freq_mhz;
117 msdd_fft_window_type_t window_type;
118 msdd_fft_points_t fft_points;
119 msdd_decimation_t decimation;
120 msdd_fft_mode_t fft_mode;
122 } __attribute__((__packed__));
124 struct msdd_request_iq_packet {
125 msdd_command_type_t command_type;
127 unsigned int center_freq_mhz;
131 msdd_decimation_t decimation;
133 } __attribute__((__packed__));
135 void make_request_fft_packet(msdd_request_fft_packet& packet);
137 void make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples);
139 msdd_request_fft_packet d_fft_request_packet; // fft request packet
140 msdd_request_iq_packet d_iq_request_packet; // fft request packet
144 msdd_source_base::msdd_source_base (const std::string &name,
145 gr_io_signature_sptr output_signature,
149 unsigned short port_src
150 ) throw (std::runtime_error)
151 : gr_sync_block (name,
152 gr_make_io_signature (0, 0, 0),
154 pimpl( new Impl(opp_mode))
159 // Set up the address stucture for the source address and port numbers
160 // Get the source IP address from the host name
161 struct hostent *hsrc (gethostbyname(src));
163 if(hsrc) { // if the source was provided as a host namex
164 pimpl->d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];
166 else { // assume it was specified as an IP address
167 if((ret=inet_aton(src, &pimpl->d_ip_src)) == 0) { // format IP address
168 perror("Not a valid source IP address or host name");
169 throw std::runtime_error("can't initialize source socket");
173 pimpl->d_port_src = htons(port_src); // format port number
175 pimpl->d_sockaddr_src.sin_family = AF_INET;
176 pimpl->d_sockaddr_src.sin_addr = pimpl->d_ip_src;
177 pimpl->d_sockaddr_src.sin_port = pimpl->d_port_src;
179 pimpl->d_temp_buff.reset(new unsigned char[OUTPUT_MAX +
180 std::max(sizeof(Impl::msdd_request_iq_packet),
181 sizeof(Impl::msdd_request_fft_packet))]); // allow it to hold up to payload_size bytes
183 set_output_multiple (OUTPUT_MAX / output_signature->sizeof_stream_item (0));
188 msdd_source_base::open()
190 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
192 MSDD_DEBUG2("MSDD: Before socket ")
193 pimpl->d_socket = socket(PF_INET, SOCK_STREAM, 0);
194 if(pimpl->d_socket == -1) {
195 perror("socket open");
196 throw std::runtime_error("can't open socket");
199 // Turn on reuse address
201 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&opt_val, sizeof(int)) == -1) {
202 perror("SO_REUSEADDR");
203 throw std::runtime_error("can't set socket option SO_REUSEADDR");
206 // Don't wait when shutting down
210 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_LINGER, (void*)&lngr, sizeof(linger)) == -1) {
212 throw std::runtime_error("can't set socket option SO_LINGER");
215 // Set a timeout on the receive function to not block indefinitely
216 // This value can (and probably should) be changed
218 // timeout.tv_sec = 1;
219 // timeout.tv_usec = 0;
220 // if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof(timeout)) == -1) {
221 // perror("SO_RCVTIMEO");
222 // throw std::runtime_error("can't set socket option SO_RCVTIMEO");
225 // bind socket to an address and port number to listen on
226 MSDD_DEBUG2("MSDD: Before socket bind to " << pimpl->d_sockaddr_src.sin_port)
227 if(::connect(pimpl->d_socket, (struct sockaddr*)&pimpl->d_sockaddr_src, sizeof(pimpl->d_sockaddr_src)) == -1) {
228 perror("socket bind");
229 throw std::runtime_error("can't bind socket");
232 MSDD_DEBUG2("MSDD: Socket open")
233 pimpl->d_updated = true;
234 return pimpl->d_socket != 0;
237 /* read n bytes from a socket descriptor */
239 msdd_source_base::readsock(int sockfd, unsigned char* buf, int nbytes) {
244 MSDD_DEBUG2("MSDD: Before socket read: " << nleft)
245 if ((nread = ::read(sockfd, buf, nleft)) < 0) {
246 return(nread); /* error, nread < 0 */
247 } else if (nread == 0) {
254 return(nbytes - nleft);
258 msdd_source_base::close()
260 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
262 if (pimpl->d_socket){
263 shutdown(pimpl->d_socket, SHUT_RDWR);
266 pimpl->d_updated = true;
271 msdd_source_base::~msdd_source_base ()
273 msdd_source_base::close();
277 msdd_source_base::sizeof_basic_sample() const
279 switch (pimpl->d_msdd_command_type) {
280 case SAMPLES_REALTIME:
283 switch (pimpl->d_msdd_fft_mode) {
290 assert (false); // bad mode
293 assert (false); // bad mode
298 msdd_source_base::start()
300 return msdd_source_base::open();
304 msdd_source_base::stop()
306 return msdd_source_base::close();
310 msdd_source_base::make_request_packet(unsigned int& size, unsigned int number_samples) {
311 switch (pimpl->d_msdd_command_type) {
312 case SAMPLES_REALTIME:
313 pimpl->make_request_iq_packet(pimpl->d_iq_request_packet, number_samples);
314 size = sizeof (pimpl->d_iq_request_packet);
315 return &pimpl->d_iq_request_packet;
317 pimpl->make_request_fft_packet(pimpl->d_fft_request_packet);
318 size = sizeof (pimpl->d_fft_request_packet);
319 return &pimpl->d_fft_request_packet;
321 assert (false); // bad mode
326 msdd_source_base::Impl::make_request_fft_packet(msdd_request_fft_packet& packet)
328 packet.command_type = SAMPLES_FFT; // FFT samples Command
329 packet.foo_x20 = 0x20;
330 packet.center_freq_mhz = d_rx_freq;
331 packet.offset_freq_hz = 0;
332 packet.gain = (int) d_gain; // gain
333 packet.window_type = WINDOW_HANNING; // magic number
334 packet.fft_points = d_fft_points;
335 packet.decimation = d_deci_rate;
336 packet.fft_mode = MODE_MAGDB;
337 packet.number_sets = 1;
341 msdd_source_base::Impl::make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples)
343 packet.command_type = SAMPLES_REALTIME; // FFT samples Command
344 packet.foo0x18 = 0x18; // magic number
345 packet.center_freq_mhz = d_rx_freq;
346 packet.offset_freq_hz = 0;
347 packet.gain = (int) d_gain; // gain
348 packet.number = number_samples * 4;
349 packet.decimation = d_deci_rate;
350 packet.number_sets = 1;
354 msdd_source_base::work (int noutput_items,
355 gr_vector_const_void_star &input_items,
356 gr_vector_void_star &output_items)
358 int output_index (0);
359 int output_items_produced;
362 unsigned int packet_size;
364 MSDD_DEBUG("MSDD: requested items: " << noutput_items)
365 int noutput_items_desired = std::min (noutput_items, (int) pimpl->d_desired_sample_size);
366 MSDD_DEBUG("MSDD: desired items: " << noutput_items_desired)
368 while (output_index < noutput_items_desired){
370 int nbytes = (pimpl->d_msdd_command_type == SAMPLES_REALTIME) ?
371 ninput_bytes_reqd_for_noutput_items (noutput_items_desired - output_index) :
372 ninput_bytes_reqd_for_noutput_items (msdd_source_base::fft_points());
374 void* request_packet = msdd_source_base::make_request_packet(packet_size, noutput_items_desired);
376 nbytes = std::min (nbytes, OUTPUT_MAX);
377 MSDD_DEBUG2("MSDD: payload sizes: nbytes1: " << nbytes )
380 int result_nbytes = ::write(pimpl->d_socket, request_packet, packet_size);
381 //assert (result_nbytes == sizeof(msdd_request_packet));
384 result_nbytes = ::read (pimpl->d_socket, (unsigned char*) request_packet, packet_size);
385 MSDD_DEBUG2("MSDD: response: " << result_nbytes)
386 //assert (result_nbytes == sizeof(msdd_request_packet));
389 result_nbytes = msdd_source_base::readsock (pimpl->d_socket, pimpl->d_temp_buff.get(), nbytes);
390 MSDD_DEBUG("MSDD: reading bytes: " << nbytes << " received: " << result_nbytes)
391 if (result_nbytes > (int) nbytes){
392 // fprintf (stderr, "msdd_source: overrun\n");
393 fputs ("uO", stderr);
394 pimpl->d_noverruns++;
395 result_nbytes = nbytes; // truncate
398 if (result_nbytes < 0) // We've got a problem. Usually board unplugged or powered down.
399 return -1; // Indicate we're done.
401 if (result_nbytes != nbytes){ // not really an error, but unexpected
402 fprintf (stderr, "msdd_source: short read. Expected %d, got %d\n",
403 nbytes, result_nbytes);
406 copy_from_msdd_buffer (output_items,
408 noutput_items_desired - output_index, // output_items_available
409 output_items_produced, // [out]
410 pimpl->d_temp_buff.get(), // usrp_buffer
412 bytes_read); // [out]
414 output_index += output_items_produced;
416 if (pimpl->d_msdd_command_type == SAMPLES_FFT) break;
419 MSDD_DEBUG("MSDD: items produced: " << output_items_produced << " index: " << output_index)
427 msdd_source_base::set_decim_rate (unsigned int rate)
432 pimpl->d_deci_rate = D0;
435 pimpl->d_deci_rate = D1;
438 pimpl->d_deci_rate = D2;
441 pimpl->d_deci_rate = D3;
444 pimpl->d_deci_rate = D4;
447 pimpl->d_deci_rate = D5;
450 pimpl->d_deci_rate = D6;
453 pimpl->d_deci_rate = D7;
456 pimpl->d_deci_rate = D8;
466 //msdd_source_base::set_nchannels (int nchan)
468 // // return d_usrp->set_nchannels (nchan);
473 //msdd_source_base::set_mux (int mux)
475 // return d_usrp->set_mux (mux);
479 msdd_source_base::set_rx_freq (int channel, double freq)
481 assert (channel == 0);
484 if (freq >= 30e6 && freq <= 6e9) {
485 pimpl->d_rx_freq = (unsigned long) freq / 1000000;
494 msdd_source_base::set_fft_size (int channel, unsigned long fft_size)
496 assert (channel == 1);
500 pimpl->d_fft_points = S256;
503 pimpl->d_fft_points = S512;
506 pimpl->d_fft_points = S1024;
509 pimpl->d_fft_points = S2048;
512 pimpl->d_fft_points = S4096;
515 pimpl->d_fft_points = S8192;
518 pimpl->d_fft_points = S16384;
521 pimpl->d_fft_points = S32768;
525 return msdd_source_base::fft_points();
530 //msdd_source_base::fpga_master_clock_freq() const
532 // return d_usrp->fpga_master_clock_freq();
536 //msdd_source_base::converter_rate() const
538 // // return d_usrp->converter_rate();
543 msdd_source_base::decim_rate () const
545 return 1 << pimpl->d_deci_rate;
549 //msdd_source_base::nchannels () const
551 // return d_usrp->nchannels ();
555 //msdd_source_base::mux () const
557 // return d_usrp->mux ();
561 msdd_source_base::rx_freq (int channel) const
563 assert (channel == 0);
565 return pimpl->d_rx_freq;
569 msdd_source_base::fft_points() const
571 return (1 << pimpl->d_fft_points);
575 msdd_source_base::noverruns () const
577 return pimpl->d_noverruns;
581 //msdd_source_base::set_fpga_mode (int mode)
583 // return d_usrp->set_fpga_mode (mode);
587 //msdd_source_base::set_ddc_phase (int channel, int phase)
589 // return d_usrp->set_ddc_phase(channel, phase);
593 //msdd_source_base::set_dc_offset_cl_enable(int bits, int mask)
595 // return d_usrp->set_dc_offset_cl_enable(bits, mask);
599 msdd_source_base::set_verbose (bool verbose)
601 pimpl->d_verbose = verbose;
605 //msdd_source_base::write_aux_dac (int which_dboard, int which_dac, int value)
607 // return d_usrp->write_aux_dac (which_dboard, which_dac, value);
611 //msdd_source_base::read_aux_adc (int which_dboard, int which_adc)
613 // return d_usrp->read_aux_adc (which_dboard, which_adc);
617 //msdd_source_base::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
619 // return d_usrp->write_eeprom (i2c_addr, eeprom_offset, buf);
623 //msdd_source_base::read_eeprom (int i2c_addr, int eeprom_offset, int len)
625 // return d_usrp->read_eeprom (i2c_addr, eeprom_offset, len);
629 //msdd_source_base::write_i2c (int i2c_addr, const std::string buf)
631 // return d_usrp->write_i2c (i2c_addr, buf);
635 //msdd_source_base::read_i2c (int i2c_addr, int len)
637 // return d_usrp->read_i2c (i2c_addr, len);
641 msdd_source_base::set_pga (int which, double gain)
643 if (gain >= PGA_MIN & gain <= PGA_MAX) {
644 pimpl->d_gain = gain;
651 msdd_source_base::pga (int which) const
653 return pimpl->d_gain;
657 msdd_source_base::pga_min () const
663 msdd_source_base::pga_max () const
669 msdd_source_base::pga_db_per_step () const
675 //msdd_source_base::daughterboard_id (int which) const
677 // return d_usrp->daughterboard_id (which);
682 //msdd_source_base::set_adc_offset (int which, int offset)
684 // return d_usrp->set_adc_offset (which, offset);
688 //msdd_source_base::set_dac_offset (int which, int offset, int offset_pin)
690 // return d_usrp->set_dac_offset (which, offset, offset_pin);
694 //msdd_source_base::set_adc_buffer_bypass (int which, bool bypass)
696 // return d_usrp->set_adc_buffer_bypass (which, bypass);
700 msdd_source_base::serial_number()
702 return "SoftTronics MSDD 6000";
706 //msdd_source_base::_write_oe (int which_dboard, int value, int mask)
708 // return d_usrp->_write_oe (which_dboard, value, mask);
712 //msdd_source_base::write_io (int which_dboard, int value, int mask)
714 // return d_usrp->write_io (which_dboard, value, mask);
718 //msdd_source_base::read_io (int which_dboard)
720 // return d_usrp->read_io (which_dboard);
726 //// internal routines...
729 //msdd_source_base::_write_fpga_reg (int regno, int value)
731 // return d_usrp->_write_fpga_reg (regno, value);
735 //msdd_source_base::_write_fpga_reg_masked (int regno, int value, int mask)
737 // return d_usrp->_write_fpga_reg_masked (regno, value, mask);
741 //msdd_source_base::_read_fpga_reg (int regno)
743 // return d_usrp->_read_fpga_reg (regno);
747 //msdd_source_base::_write_9862 (int which_codec, int regno, unsigned char value)
749 // return d_usrp->_write_9862 (which_codec, regno, value);
753 //msdd_source_base::_read_9862 (int which_codec, int regno) const
755 // return d_usrp->_read_9862 (which_codec, regno);
759 //msdd_source_base::_write_spi (int optional_header, int enables,
760 // int format, std::string buf)
762 // return d_usrp->_write_spi (optional_header, enables, format, buf);
766 //msdd_source_base::_read_spi (int optional_header, int enables, int format, int len)
768 // return d_usrp->_read_spi (optional_header, enables, format, len);
772 //msdd_source_base::set_format(unsigned int format)
774 // return d_usrp->set_format(format);
778 //msdd_source_base::format() const
780 // return d_usrp->format();
784 //msdd_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
786 // return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
790 //msdd_source_base::format_width(unsigned int format)
792 // return usrp_standard_rx::format_width(format);
796 //msdd_source_base::format_shift(unsigned int format)
798 // return usrp_standard_rx::format_shift(format);
802 //msdd_source_base::format_want_q(unsigned int format)
804 // return usrp_standard_rx::format_want_q(format);
808 //msdd_source_base::format_bypass_halfband(unsigned int format)
810 // return usrp_standard_rx::format_bypass_halfband(format);
813 bool msdd_source_base::set_desired_packet_size (int which, unsigned long packet_size) {
816 if (pimpl->d_desired_sample_size < 2^32) { // FIXME: find maximum sample request for MSDD check if greater than
817 pimpl->d_desired_sample_size = packet_size;
822 unsigned long msdd_source_base::desired_packet_size (int which) const {
823 return pimpl->d_desired_sample_size;