diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index cc90fda..c260c62 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -32,5 +32,6 @@ install(FILES satnogs_tcp_rigctl_msg_source.xml satnogs_frame_encoder.xml satnogs_doppler_correction_cc.xml - satnogs_upsat_fsk_frame_acquisition.xml DESTINATION share/gnuradio/grc/blocks + satnogs_upsat_fsk_frame_acquisition.xml + satnogs_upsat_fsk_frame_encoder.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/satnogs_upsat_fsk_frame_acquisition.xml b/grc/satnogs_upsat_fsk_frame_acquisition.xml index e03028f..1a87b15 100644 --- a/grc/satnogs_upsat_fsk_frame_acquisition.xml +++ b/grc/satnogs_upsat_fsk_frame_acquisition.xml @@ -1,23 +1,23 @@ - UPSAT FSK Frame Acquisition - satnogs_upsat_fsk_frame_acquisition - satnogs - import satnogs - satnogs.upsat_fsk_frame_acquisition($preamble, $sync_word, $whitening, $manchester) + UPSAT FSK Frame Acquisition + satnogs_upsat_fsk_frame_acquisition + satnogs + import satnogs + satnogs.upsat_fsk_frame_acquisition($preamble, $sync_word, $whitening, $manchester) - - Frame Preamble - preamble - raw - + + Frame Preamble + preamble + raw + + + + Synchronization Word + sync_word + raw + - - Synchronization Word - sync_word - raw - - Whitening whitening @@ -46,13 +46,13 @@ - - in - float - + + in + float + - - pdu - message - + + pdu + message + diff --git a/grc/satnogs_upsat_fsk_frame_encoder.xml b/grc/satnogs_upsat_fsk_frame_encoder.xml new file mode 100644 index 0000000..d8cddb1 --- /dev/null +++ b/grc/satnogs_upsat_fsk_frame_encoder.xml @@ -0,0 +1,72 @@ + + + UPSAT FSK Frame Encoder + satnogs_upsat_fsk_frame_encoder + satnogs + import satnogs + satnogs.upsat_fsk_frame_encoder($preamble, $sync_word, $append_crc, $whitening, $manchester) + + + Frame Preamble + preamble + raw + + + + Synchronization Word + sync_word + raw + + + + Append CRC + append_crc + enum + + + + + + Whitening + whitening + enum + + + + + + Use Manchester Coding + manchester + enum + + + + + + pdu + message + + + + out + float + + diff --git a/include/satnogs/CMakeLists.txt b/include/satnogs/CMakeLists.txt index e11ed74..32e4a5d 100644 --- a/include/satnogs/CMakeLists.txt +++ b/include/satnogs/CMakeLists.txt @@ -45,5 +45,6 @@ install(FILES doppler_correction_cc.h doppler_fit.h freq_drift.h - upsat_fsk_frame_acquisition.h DESTINATION include/satnogs + upsat_fsk_frame_acquisition.h + upsat_fsk_frame_encoder.h DESTINATION include/satnogs ) diff --git a/include/satnogs/upsat_fsk_frame_encoder.h b/include/satnogs/upsat_fsk_frame_encoder.h new file mode 100644 index 0000000..3bb9c9e --- /dev/null +++ b/include/satnogs/upsat_fsk_frame_encoder.h @@ -0,0 +1,75 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_H +#define INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_H + +#include +#include + +namespace gr +{ + namespace satnogs + { + + /*! + * \brief This block implements a FSK frame encoder for the UPSAT satellite. + * It takes as input a message containing the PDU and performs the NRZ + * encoding. The resulting float samples can be passed from a FM modulation + * block and then to the SDR device. + * + * \ingroup satnogs + * + */ + class SATNOGS_API upsat_fsk_frame_encoder : virtual public gr::sync_block + { + public: + typedef boost::shared_ptr sptr; + + /*! + * Creates an FSK encoding block. Note that this block does NOT perform + * the frequency modulation. You can use the existing frequency modulation + * block shipped with the GNU Radio. + * + * @param preamble the bytes that consist the preamble of the frame + * + * @param sync_word the byte synchronization word + * + * @param append_crc if set to true the encoder will append a two byte + * CRC field at the end of the frame. The CRC algorithm is compatible + * with the CC1120 chip. + * + * @param whitening true if the transmitted data have been processed by + * the whitening algorithm of the CC1120 chip. False otherwise. + * + * @param manchester true if the transmitted data have been processed by + * the Manchester algorithm of the CC1120 chip. False otherwise. + */ + static sptr + make (const std::vector& preamble, + const std::vector& sync_word, bool append_crc = true, + bool whitening = false, bool manchester = false); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_H */ + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ada644d..d7e88bb 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -43,7 +43,8 @@ list(APPEND satnogs_sources frame_encoder_impl.cc doppler_fit.cc freq_drift.cc - upsat_fsk_frame_acquisition_impl.cc ) + upsat_fsk_frame_acquisition_impl.cc + upsat_fsk_frame_encoder_impl.cc ) set(satnogs_sources "${satnogs_sources}" PARENT_SCOPE) if(NOT satnogs_sources) diff --git a/lib/upsat_fsk_frame_acquisition_impl.cc b/lib/upsat_fsk_frame_acquisition_impl.cc index 6f63020..7a25203 100644 --- a/lib/upsat_fsk_frame_acquisition_impl.cc +++ b/lib/upsat_fsk_frame_acquisition_impl.cc @@ -57,6 +57,7 @@ namespace gr d_state (SEARCHING), d_shifting_byte (0x0), d_decoded_bytes (0), + d_decoded_bits (0), d_frame_len (0) { message_port_register_out (pmt::mp ("pdu")); @@ -195,6 +196,7 @@ namespace gr if (d_decoded_bytes == d_frame_len) { message_port_pub (pmt::mp ("pdu"), pmt::make_blob (d_pdu, d_frame_len)); + LOG_WARN("Packet of %u", d_frame_len); reset_state (); } } diff --git a/lib/upsat_fsk_frame_encoder_impl.cc b/lib/upsat_fsk_frame_encoder_impl.cc new file mode 100644 index 0000000..86dd223 --- /dev/null +++ b/lib/upsat_fsk_frame_encoder_impl.cc @@ -0,0 +1,129 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include "upsat_fsk_frame_encoder_impl.h" +#include + +namespace gr +{ + namespace satnogs + { + + upsat_fsk_frame_encoder::sptr + upsat_fsk_frame_encoder::make (const std::vector& preamble, + const std::vector& sync_word, + bool append_crc, bool whitening, + bool manchester) + { + return gnuradio::get_initial_sptr ( + new upsat_fsk_frame_encoder_impl (preamble, sync_word, append_crc, + whitening, manchester)); + } + + /* + * The private constructor + */ + upsat_fsk_frame_encoder_impl::upsat_fsk_frame_encoder_impl ( + const std::vector& preamble, + const std::vector& sync_word, bool append_crc, bool whitening, + bool manchester) : + gr::sync_block ("upsat_fsk_frame_encoder", + gr::io_signature::make (0, 0, 0), + gr::io_signature::make (1, 1, sizeof(float))), + d_preamble (preamble), + d_preamble_len (preamble.size ()), + d_sync_word (sync_word), + d_sync_word_len (sync_word.size ()), + d_append_crc(append_crc), + d_whitening(whitening), + d_manchester(manchester), + d_encoded(0), + d_pdu_len(0) + { + message_port_register_in (pmt::mp ("pdu")); + /* + * Allocate memory for the maximum possible frame WITH all headers and + * tail fields + * +-------------+----------+--------+--------------------------+------+ + * | Preamble | SFD | LENGTH | PDU | CRC | + * +-------------+----------+--------+--------------------------+------+ + * User def. User def. 1B 1-255 B 2 B + */ + d_pdu = new uint8_t[d_preamble_len + d_sync_word_len + sizeof(uint8_t) + + UPSAT_MAX_FRAME_LEN + sizeof(uint16_t)]; + + /* Copy the preamble at the start of the pdu */ + memcpy(d_pdu, d_preamble.data(), d_preamble_len); + memcpy(d_pdu + d_preamble_len, d_sync_word.data(), d_sync_word_len); + } + + /* + * Our virtual destructor. + */ + upsat_fsk_frame_encoder_impl::~upsat_fsk_frame_encoder_impl () + { + delete [] d_pdu; + } + + int + upsat_fsk_frame_encoder_impl::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + pmt::pmt_t pdu; + float *out = (float *) output_items[0]; + + /* + * If the whole previous frame has been successfully sent, block waiting + * for a new one + */ + if(d_encoded == 0){ + pdu = delete_head_blocking(pmt::mp("pdu")); + d_pdu_len = pmt::blob_length (pdu); + + if(d_pdu_len > UPSAT_MAX_FRAME_LEN){ + LOG_ERROR("PDU is greater than the supported. Dropping the PDU"); + return 0; + } + /* Set the frame length at the corresponding field */ + d_pdu[d_preamble_len + d_sync_word_len] = (uint8_t) d_pdu_len; + /* Append the variable length PDU */ + memcpy (d_pdu + d_preamble_len + d_sync_word_len + sizeof(uint8_t), + pmt::blob_data (pdu), + d_pdu_len * sizeof(uint8_t)); + d_pdu_len += d_preamble_len + d_sync_word_len + sizeof(uint8_t); + /* If it is necessary calculate and append the CRC */ + if(d_append_crc) { + /* TODO: Calc CRC */ + d_pdu_len += sizeof(uint16_t); + } + } + + return noutput_items; + } + + } /* namespace satnogs */ +} /* namespace gr */ + diff --git a/lib/upsat_fsk_frame_encoder_impl.h b/lib/upsat_fsk_frame_encoder_impl.h new file mode 100644 index 0000000..59e252f --- /dev/null +++ b/lib/upsat_fsk_frame_encoder_impl.h @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2016, Libre Space Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_IMPL_H +#define INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_IMPL_H + +#include + +namespace gr +{ + namespace satnogs + { + + class upsat_fsk_frame_encoder_impl : public upsat_fsk_frame_encoder + { + private: + const std::vector d_preamble; + const size_t d_preamble_len; + const std::vector d_sync_word; + const size_t d_sync_word_len; + const bool d_append_crc; + const bool d_whitening; + const bool d_manchester; + size_t d_encoded; + size_t d_pdu_len; + uint8_t *d_pdu; + + public: + upsat_fsk_frame_encoder_impl (const std::vector& preamble, + const std::vector& sync_word, + bool append_crc, bool whitening, + bool manchester); + ~upsat_fsk_frame_encoder_impl (); + + // Where all the action really happens + int + work (int noutput_items, gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_UPSAT_FSK_FRAME_ENCODER_IMPL_H */ + diff --git a/swig/satnogs_swig.i b/swig/satnogs_swig.i index fb2fba3..ebec1e7 100644 --- a/swig/satnogs_swig.i +++ b/swig/satnogs_swig.i @@ -26,6 +26,7 @@ #include "satnogs/doppler_correction_cc.h" #include "satnogs/frame_encoder.h" #include "satnogs/upsat_fsk_frame_acquisition.h" +#include "satnogs/upsat_fsk_frame_encoder.h" %} @@ -62,3 +63,5 @@ GR_SWIG_BLOCK_MAGIC2(satnogs, frame_encoder); GR_SWIG_BLOCK_MAGIC2(satnogs, doppler_correction_cc); %include "satnogs/upsat_fsk_frame_acquisition.h" GR_SWIG_BLOCK_MAGIC2(satnogs, upsat_fsk_frame_acquisition); +%include "satnogs/upsat_fsk_frame_encoder.h" +GR_SWIG_BLOCK_MAGIC2(satnogs, upsat_fsk_frame_encoder);