3 * Copyright 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>
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)
86 msdd_decimation_t d_deci_rate;
87 unsigned long d_rx_freq;
91 msdd_command_type_t d_msdd_command_type;
92 msdd_fft_mode_t d_msdd_fft_mode;
93 unsigned long d_desired_sample_size;
95 int d_socket; // handle to socket
96 int d_socket_rcv; // handle to socket retuned in the accept call
97 struct in_addr d_ip_src; // store the source IP address to use
98 unsigned short d_port_src; // the port number to open for connections to this service
99 sockaddr_in d_sockaddr_src; // store the source sockaddr data (formatted IP address and port number)
100 std::auto_ptr<unsigned char> d_temp_buff; // hold buffer between calls
103 msdd_fft_points_t d_fft_points;
105 struct msdd_request_fft_packet {
106 msdd_command_type_t command_type;
108 unsigned int center_freq_mhz;
111 msdd_fft_window_type_t window_type;
112 msdd_fft_points_t fft_points;
113 msdd_decimation_t decimation;
114 msdd_fft_mode_t fft_mode;
116 } __attribute__((__packed__));
118 struct msdd_request_iq_packet {
119 msdd_command_type_t command_type;
121 unsigned int center_freq_mhz;
125 msdd_decimation_t decimation;
127 } __attribute__((__packed__));
129 void make_request_fft_packet(msdd_request_fft_packet& packet);
131 void make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples);
133 msdd_request_fft_packet d_fft_request_packet; // fft request packet
134 msdd_request_iq_packet d_iq_request_packet; // fft request packet
138 msdd_source_base::msdd_source_base (const std::string &name,
139 gr_io_signature_sptr output_signature,
143 unsigned short port_src
144 ) throw (std::runtime_error)
145 : gr_sync_block (name,
146 gr_make_io_signature (0, 0, 0),
148 pimpl(new Impl(opp_mode))
152 // Set up the address stucture for the source address and port numbers
153 // Get the source IP address from the host name
154 struct hostent *hsrc (gethostbyname(src));
156 if(hsrc) { // if the source was provided as a host namex
157 pimpl->d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];
159 else { // assume it was specified as an IP address
160 if((ret=inet_aton(src, &pimpl->d_ip_src)) == 0) { // format IP address
161 perror("Not a valid source IP address or host name");
162 throw std::runtime_error("can't initialize source socket");
166 pimpl->d_port_src = htons(port_src); // format port number
168 pimpl->d_sockaddr_src.sin_family = AF_INET;
169 pimpl->d_sockaddr_src.sin_addr = pimpl->d_ip_src;
170 pimpl->d_sockaddr_src.sin_port = pimpl->d_port_src;
172 pimpl->d_temp_buff.reset(new unsigned char[OUTPUT_MAX +
173 std::max(sizeof(Impl::msdd_request_iq_packet),
174 sizeof(Impl::msdd_request_fft_packet))]); // allow it to hold up to payload_size bytes
176 set_output_multiple (OUTPUT_MAX / output_signature->sizeof_stream_item (0));
181 msdd_source_base::open()
183 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
185 MSDD_DEBUG2("MSDD: Before socket ")
186 pimpl->d_socket = socket(PF_INET, SOCK_DGRAM, 0);
187 if(pimpl->d_socket == -1) {
188 perror("socket open");
189 throw std::runtime_error("can't open socket");
192 // Turn on reuse address
194 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&opt_val, sizeof(int)) == -1) {
195 perror("SO_REUSEADDR");
196 throw std::runtime_error("can't set socket option SO_REUSEADDR");
199 // Don't wait when shutting down
203 if(setsockopt(pimpl->d_socket, SOL_SOCKET, SO_LINGER, (void*)&lngr, sizeof(linger)) == -1) {
205 throw std::runtime_error("can't set socket option SO_LINGER");
208 // Set a timeout on the receive function to not block indefinitely
209 // This value can (and probably should) be changed
211 // timeout.tv_sec = 1;
212 // timeout.tv_usec = 0;
213 // if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&timeout, sizeof(timeout)) == -1) {
214 // perror("SO_RCVTIMEO");
215 // throw std::runtime_error("can't set socket option SO_RCVTIMEO");
218 // bind socket to an address and port number to listen on
219 MSDD_DEBUG2("MSDD: Before socket bind to " << pimpl->d_sockaddr_src.sin_port)
220 if(::connect(pimpl->d_socket, (struct sockaddr*)&pimpl->d_sockaddr_src, sizeof(pimpl->d_sockaddr_src)) == -1) {
221 perror("socket bind");
222 throw std::runtime_error("can't bind socket");
224 MSDD_DEBUG2("MSDD: Socket open")
226 // Turn streaming service on
227 write_request_packet(pimpl->d_desired_sample_size);
229 pimpl->d_updated = true;
230 return pimpl->d_socket != 0;
233 /* read n bytes from a socket descriptor */
235 msdd_source_base::readsock(int sockfd, unsigned char* buf, int nbytes) {
240 MSDD_DEBUG2("MSDD: Before socket read: " << nleft)
241 if ((nread = ::read(sockfd, buf, nleft)) < 0) {
242 return(nread); /* error, nread < 0 */
243 } else if (nread == 0) {
250 return(nbytes - nleft);
254 msdd_source_base::close()
256 omni_mutex_lock l(pimpl->d_mutex); // hold mutex for duration of this function
258 //This should turn off UDP streaming but does not yet work
259 //write_request_packet(0);
261 if (pimpl->d_socket){
262 shutdown(pimpl->d_socket, SHUT_RDWR);
265 pimpl->d_updated = true;
270 msdd_source_base::~msdd_source_base ()
272 msdd_source_base::close();
276 msdd_source_base::sizeof_basic_sample() const
278 switch (pimpl->d_msdd_command_type) {
279 case SAMPLES_REALTIME:
282 switch (pimpl->d_msdd_fft_mode) {
289 assert (false); // bad mode
292 assert (false); // bad mode
297 msdd_source_base::start()
299 return msdd_source_base::open();
303 msdd_source_base::stop()
305 return msdd_source_base::close();
309 msdd_source_base::write_request_packet(unsigned int number_samples)
311 unsigned int packet_size;
312 void* request_packet = msdd_source_base::make_request_packet(packet_size, number_samples);
313 int result_nbytes = ::write(pimpl->d_socket, request_packet, packet_size);
314 MSDD_DEBUG2("MSDD: wrote control command: " << result_nbytes)
317 result_nbytes = ::read (pimpl->d_socket, (unsigned char*) request_packet, packet_size);
318 MSDD_DEBUG2("MSDD: response: " << result_nbytes)
323 msdd_source_base::make_request_packet(unsigned int& size, unsigned int number_samples) {
324 switch (pimpl->d_msdd_command_type) {
325 case SAMPLES_REALTIME:
326 pimpl->make_request_iq_packet(pimpl->d_iq_request_packet, number_samples);
327 size = sizeof (pimpl->d_iq_request_packet);
328 return &pimpl->d_iq_request_packet;
330 pimpl->make_request_fft_packet(pimpl->d_fft_request_packet);
331 size = sizeof (pimpl->d_fft_request_packet);
332 return &pimpl->d_fft_request_packet;
334 assert (false); // bad mode
339 msdd_source_base::Impl::make_request_fft_packet(msdd_request_fft_packet& packet)
341 packet.command_type = SAMPLES_FFT; // FFT samples Command
342 packet.foo_x20 = 0x20;
343 packet.center_freq_mhz = d_rx_freq;
344 packet.offset_freq_hz = 0;
345 packet.gain = (int) d_gain; // gain
346 packet.window_type = WINDOW_HANNING; // magic number
347 packet.fft_points = d_fft_points;
348 packet.decimation = d_deci_rate;
349 packet.fft_mode = MODE_MAGDB;
350 packet.number_sets = 1;
354 msdd_source_base::Impl::make_request_iq_packet(msdd_request_iq_packet& packet, unsigned int number_samples)
356 packet.command_type = SAMPLES_REALTIME; // IQ samples Command
357 packet.foo0x18 = 0x18; // magic number
358 packet.center_freq_mhz = d_rx_freq;
359 packet.offset_freq_hz = 0;
360 packet.gain = (int) d_gain; // gain
361 packet.number = number_samples * 4;
362 packet.decimation = d_deci_rate;
363 packet.number_sets = 1;
367 msdd_source_base::work (int noutput_items,
368 gr_vector_const_void_star &input_items,
369 gr_vector_void_star &output_items)
371 int output_index (0);
372 int output_items_produced;
375 MSDD_DEBUG("MSDD: requested items: " << noutput_items)
376 int noutput_items_desired = std::min (noutput_items, (int) pimpl->d_desired_sample_size);
377 MSDD_DEBUG("MSDD: desired items: " << noutput_items_desired)
379 while (output_index < noutput_items_desired){
381 int nbytes = (pimpl->d_msdd_command_type == SAMPLES_REALTIME) ?
382 ninput_bytes_reqd_for_noutput_items (noutput_items_desired - output_index) :
383 ninput_bytes_reqd_for_noutput_items (msdd_source_base::fft_points());
385 nbytes = std::min (nbytes, OUTPUT_MAX);
386 MSDD_DEBUG2("MSDD: payload sizes: nbytes1: " << nbytes )
389 int 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;
462 // Resubmit the control to update the decimation rate
463 write_request_packet(pimpl->d_desired_sample_size);
469 //msdd_source_base::set_nchannels (int nchan)
471 // // return d_usrp->set_nchannels (nchan);
476 //msdd_source_base::set_mux (int mux)
478 // return d_usrp->set_mux (mux);
482 msdd_source_base::set_rx_freq (int channel, double freq)
484 assert (channel == 0);
487 if (freq >= 30e6 && freq <= 6e9) {
488 pimpl->d_rx_freq = (unsigned long) freq / 1000000;
492 // Resubmit the control to update the RX frequency
493 write_request_packet(pimpl->d_desired_sample_size);
500 msdd_source_base::set_fft_size (int channel, unsigned long fft_size)
502 assert (channel == 1);
506 pimpl->d_fft_points = S256;
509 pimpl->d_fft_points = S512;
512 pimpl->d_fft_points = S1024;
515 pimpl->d_fft_points = S2048;
518 pimpl->d_fft_points = S4096;
521 pimpl->d_fft_points = S8192;
524 pimpl->d_fft_points = S16384;
527 pimpl->d_fft_points = S32768;
531 // Resubmit the control to update the FFT size
532 write_request_packet(pimpl->d_desired_sample_size);
534 return msdd_source_base::fft_points();
539 //msdd_source_base::fpga_master_clock_freq() const
541 // return d_usrp->fpga_master_clock_freq();
545 //msdd_source_base::converter_rate() const
547 // // return d_usrp->converter_rate();
552 msdd_source_base::decim_rate () const
554 return 1 << pimpl->d_deci_rate;
558 //msdd_source_base::nchannels () const
560 // return d_usrp->nchannels ();
564 //msdd_source_base::mux () const
566 // return d_usrp->mux ();
570 msdd_source_base::rx_freq (int channel) const
572 assert (channel == 0);
574 return pimpl->d_rx_freq;
578 msdd_source_base::fft_points() const
580 return (1 << pimpl->d_fft_points);
584 msdd_source_base::noverruns () const
586 return pimpl->d_noverruns;
590 //msdd_source_base::set_fpga_mode (int mode)
592 // return d_usrp->set_fpga_mode (mode);
596 //msdd_source_base::set_ddc_phase (int channel, int phase)
598 // return d_usrp->set_ddc_phase(channel, phase);
602 //msdd_source_base::set_dc_offset_cl_enable(int bits, int mask)
604 // return d_usrp->set_dc_offset_cl_enable(bits, mask);
608 msdd_source_base::set_verbose (bool verbose)
610 pimpl->d_verbose = verbose;
614 //msdd_source_base::write_aux_dac (int which_dboard, int which_dac, int value)
616 // return d_usrp->write_aux_dac (which_dboard, which_dac, value);
620 //msdd_source_base::read_aux_adc (int which_dboard, int which_adc)
622 // return d_usrp->read_aux_adc (which_dboard, which_adc);
626 //msdd_source_base::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
628 // return d_usrp->write_eeprom (i2c_addr, eeprom_offset, buf);
632 //msdd_source_base::read_eeprom (int i2c_addr, int eeprom_offset, int len)
634 // return d_usrp->read_eeprom (i2c_addr, eeprom_offset, len);
638 //msdd_source_base::write_i2c (int i2c_addr, const std::string buf)
640 // return d_usrp->write_i2c (i2c_addr, buf);
644 //msdd_source_base::read_i2c (int i2c_addr, int len)
646 // return d_usrp->read_i2c (i2c_addr, len);
650 msdd_source_base::set_pga (int which, double gain)
652 if (gain >= PGA_MIN & gain <= PGA_MAX) {
653 pimpl->d_gain = gain;
655 // Resubmit the control to update the PGA gain
656 write_request_packet(pimpl->d_desired_sample_size);
664 msdd_source_base::pga (int which) const
666 return pimpl->d_gain;
670 msdd_source_base::pga_min () const
676 msdd_source_base::pga_max () const
682 msdd_source_base::pga_db_per_step () const
688 //msdd_source_base::daughterboard_id (int which) const
690 // return d_usrp->daughterboard_id (which);
695 //msdd_source_base::set_adc_offset (int which, int offset)
697 // return d_usrp->set_adc_offset (which, offset);
701 //msdd_source_base::set_dac_offset (int which, int offset, int offset_pin)
703 // return d_usrp->set_dac_offset (which, offset, offset_pin);
707 //msdd_source_base::set_adc_buffer_bypass (int which, bool bypass)
709 // return d_usrp->set_adc_buffer_bypass (which, bypass);
713 msdd_source_base::serial_number()
715 return "SoftTronics MSDD 6000";
719 //msdd_source_base::_write_oe (int which_dboard, int value, int mask)
721 // return d_usrp->_write_oe (which_dboard, value, mask);
725 //msdd_source_base::write_io (int which_dboard, int value, int mask)
727 // return d_usrp->write_io (which_dboard, value, mask);
731 //msdd_source_base::read_io (int which_dboard)
733 // return d_usrp->read_io (which_dboard);
739 //// internal routines...
742 //msdd_source_base::_write_fpga_reg (int regno, int value)
744 // return d_usrp->_write_fpga_reg (regno, value);
748 //msdd_source_base::_write_fpga_reg_masked (int regno, int value, int mask)
750 // return d_usrp->_write_fpga_reg_masked (regno, value, mask);
754 //msdd_source_base::_read_fpga_reg (int regno)
756 // return d_usrp->_read_fpga_reg (regno);
760 //msdd_source_base::_write_9862 (int which_codec, int regno, unsigned char value)
762 // return d_usrp->_write_9862 (which_codec, regno, value);
766 //msdd_source_base::_read_9862 (int which_codec, int regno) const
768 // return d_usrp->_read_9862 (which_codec, regno);
772 //msdd_source_base::_write_spi (int optional_header, int enables,
773 // int format, std::string buf)
775 // return d_usrp->_write_spi (optional_header, enables, format, buf);
779 //msdd_source_base::_read_spi (int optional_header, int enables, int format, int len)
781 // return d_usrp->_read_spi (optional_header, enables, format, len);
785 //msdd_source_base::set_format(unsigned int format)
787 // return d_usrp->set_format(format);
791 //msdd_source_base::format() const
793 // return d_usrp->format();
797 //msdd_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
799 // return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
803 //msdd_source_base::format_width(unsigned int format)
805 // return usrp_standard_rx::format_width(format);
809 //msdd_source_base::format_shift(unsigned int format)
811 // return usrp_standard_rx::format_shift(format);
815 //msdd_source_base::format_want_q(unsigned int format)
817 // return usrp_standard_rx::format_want_q(format);
821 //msdd_source_base::format_bypass_halfband(unsigned int format)
823 // return usrp_standard_rx::format_bypass_halfband(format);
826 bool msdd_source_base::set_desired_packet_size (int which, unsigned long packet_size) {
829 if (pimpl->d_desired_sample_size < 2^32) { // FIXME: find maximum sample request for MSDD check if greater than
830 pimpl->d_desired_sample_size = packet_size;
833 // Resubmit the control to update the packet size
834 write_request_packet(pimpl->d_desired_sample_size);
839 unsigned long msdd_source_base::desired_packet_size (int which) const {
840 return pimpl->d_desired_sample_size;