2012-03-16 10:55:58 +00:00
|
|
|
/*!
|
2012-03-24 02:53:00 +00:00
|
|
|
* \file tcp_communication.cc
|
|
|
|
* \brief Implementation of the TCP communication class
|
2012-03-16 10:55:58 +00:00
|
|
|
* \author David Pubill, 2011. dpubill(at)cttc.es
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2015-01-08 18:49:59 +00:00
|
|
|
* Copyright (C) 2010-2015 (see AUTHORS file for a list of contributors)
|
2012-03-16 10:55:58 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR 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
|
2015-01-08 18:49:59 +00:00
|
|
|
* (at your option) any later version.
|
2012-03-16 10:55:58 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR 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 GNSS-SDR. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tcp_packet_data.h"
|
|
|
|
#include "tcp_communication.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
tcp_communication::tcp_communication() : tcp_socket_(io_service_)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
}
|
2012-03-24 02:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tcp_communication::~tcp_communication()
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
}
|
2012-03-24 02:53:00 +00:00
|
|
|
|
|
|
|
|
2012-07-03 12:52:12 +00:00
|
|
|
int tcp_communication::listen_tcp_connection(size_t d_port_, size_t d_port_ch0_)
|
2012-03-16 10:55:58 +00:00
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
try
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
// Specify IP type and port
|
|
|
|
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), d_port_);
|
|
|
|
boost::asio::ip::tcp::acceptor acceptor(io_service_, endpoint);
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
if (d_port_ == d_port_ch0_)
|
|
|
|
{
|
|
|
|
std::cout << "Server ready. Listening for TCP connections..." << std::endl;
|
|
|
|
}
|
2012-07-03 12:52:12 +00:00
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
// Reuse the IP address for each connection
|
|
|
|
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
// Listen for a connection and accept it
|
|
|
|
acceptor.listen(12);
|
|
|
|
acceptor.accept(tcp_socket_);
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
std::cout << "Socket accepted on port " << d_port_ << std::endl;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-03-16 10:55:58 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
void tcp_communication::send_receive_tcp_packet_galileo_e1(boost::array<float, NUM_TX_VARIABLES_GALILEO_E1> buf, tcp_packet_data* tcp_data_)
|
2012-03-16 10:55:58 +00:00
|
|
|
{
|
2016-05-02 21:46:30 +00:00
|
|
|
int controlc = 0;
|
|
|
|
boost::array<float, NUM_RX_VARIABLES> readbuf;
|
|
|
|
float d_control_id_ = buf.data()[0];
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
try
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
// Send a TCP packet
|
|
|
|
tcp_socket_.write_some(boost::asio::buffer(buf));
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2012-03-24 02:53:00 +00:00
|
|
|
// Read the received TCP packet
|
|
|
|
tcp_socket_.read_some(boost::asio::buffer(readbuf));
|
2016-05-02 21:46:30 +00:00
|
|
|
|
|
|
|
//! Control. The GNSS-SDR program ends if an error in a TCP packet is detected.
|
|
|
|
if (d_control_id_ != readbuf.data()[0])
|
|
|
|
{
|
|
|
|
throw "Packet error!";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover the variables received
|
|
|
|
tcp_data_->proc_pack_code_error = readbuf.data()[1];
|
|
|
|
tcp_data_->proc_pack_carr_error = readbuf.data()[2];
|
|
|
|
tcp_data_->proc_pack_carrier_doppler_hz = readbuf.data()[3];
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << ". Please press Ctrl+C to end the program." << std::endl;
|
|
|
|
std::cin >> controlc;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-03-16 10:55:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
void tcp_communication::send_receive_tcp_packet_gps_l1_ca(boost::array<float, NUM_TX_VARIABLES_GPS_L1_CA> buf, tcp_packet_data* tcp_data_)
|
2012-10-19 13:38:25 +00:00
|
|
|
{
|
2016-05-02 21:46:30 +00:00
|
|
|
int controlc = 0;
|
|
|
|
boost::array<float, NUM_RX_VARIABLES> readbuf;
|
|
|
|
float d_control_id_ = buf.data()[0];
|
2012-10-19 13:38:25 +00:00
|
|
|
|
|
|
|
try
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
2012-10-19 13:38:25 +00:00
|
|
|
// Send a TCP packet
|
|
|
|
tcp_socket_.write_some(boost::asio::buffer(buf));
|
|
|
|
|
|
|
|
// Read the received TCP packet
|
|
|
|
tcp_socket_.read_some(boost::asio::buffer(readbuf));
|
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
//! Control. The GNSS-SDR program ends if an error in a TCP packet is detected.
|
|
|
|
if (d_control_id_ != readbuf.data()[0])
|
|
|
|
{
|
|
|
|
throw "Packet error!";
|
|
|
|
}
|
2012-10-19 13:38:25 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
// Recover the variables received
|
|
|
|
tcp_data_->proc_pack_code_error = readbuf.data()[1];
|
|
|
|
tcp_data_->proc_pack_carr_error = readbuf.data()[2];
|
|
|
|
tcp_data_->proc_pack_carrier_doppler_hz = readbuf.data()[3];
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-03-24 02:53:00 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2012-10-19 13:38:25 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << ". Please press Ctrl+C to end the program." << std::endl;
|
|
|
|
std::cin >> controlc;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2012-10-19 13:38:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-24 02:53:00 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
|
2012-03-16 10:55:58 +00:00
|
|
|
void tcp_communication::close_tcp_connection(size_t d_port_)
|
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
// Close the TCP connection
|
|
|
|
tcp_socket_.close();
|
|
|
|
std::cout << "Socket closed on port " << d_port_ << std::endl;
|
|
|
|
return;
|
2012-03-16 10:55:58 +00:00
|
|
|
}
|