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
|
|
|
|
*
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2012-03-16 10:55:58 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2012-03-16 10:55:58 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2012-03-16 10:55:58 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2012-03-16 10:55:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tcp_communication.h"
|
2018-12-09 21:00:09 +00:00
|
|
|
#include "tcp_packet_data.h"
|
2012-03-16 10:55:58 +00:00
|
|
|
#include <iostream>
|
2019-07-30 10:51:36 +00:00
|
|
|
#include <stdexcept>
|
2012-03-16 10:55:58 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2019-05-08 13:23:55 +00:00
|
|
|
Tcp_Communication::Tcp_Communication() : tcp_socket_(io_context_) {} // NOLINT
|
2012-03-24 02:53:00 +00:00
|
|
|
|
|
|
|
|
2019-02-22 09:47:24 +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_);
|
2019-05-08 13:23:55 +00:00
|
|
|
boost::asio::ip::tcp::acceptor acceptor(io_context_, endpoint);
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
if (d_port_ == d_port_ch0_)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Server ready. Listening for TCP connections...\n";
|
2016-05-02 21:46:30 +00:00
|
|
|
}
|
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
|
|
|
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Socket accepted on port " << d_port_ << '\n';
|
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)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << '\n';
|
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
|
|
|
|
2019-02-22 09:47:24 +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;
|
2019-07-20 09:13:28 +00:00
|
|
|
boost::array<float, NUM_RX_VARIABLES> readbuf{};
|
2016-05-02 21:46:30 +00:00
|
|
|
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])
|
|
|
|
{
|
2019-07-30 10:51:36 +00:00
|
|
|
throw std::runtime_error("Packet error!");
|
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-16 10:55:58 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << ". Please press Ctrl+C to end the program.\n";
|
2012-03-24 02:53:00 +00:00
|
|
|
std::cin >> controlc;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2019-09-11 22:31:34 +00:00
|
|
|
}
|
2012-03-16 10:55:58 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
|
2019-02-22 09:47:24 +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;
|
2019-07-20 09:13:28 +00:00
|
|
|
boost::array<float, NUM_RX_VARIABLES> readbuf{};
|
2016-05-02 21:46:30 +00:00
|
|
|
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])
|
|
|
|
{
|
2019-07-30 10:51:36 +00:00
|
|
|
throw std::runtime_error("Packet error!");
|
2016-05-02 21:46:30 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cerr << "Exception: " << e.what() << ". Please press Ctrl+C to end the program.\n";
|
2012-10-19 13:38:25 +00:00
|
|
|
std::cin >> controlc;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2019-09-11 22:31:34 +00:00
|
|
|
}
|
2012-03-24 02:53:00 +00:00
|
|
|
|
2016-05-02 21:46:30 +00:00
|
|
|
|
2019-02-22 09:47:24 +00:00
|
|
|
void Tcp_Communication::close_tcp_connection(size_t d_port_)
|
2012-03-16 10:55:58 +00:00
|
|
|
{
|
2012-03-24 02:53:00 +00:00
|
|
|
// Close the TCP connection
|
|
|
|
tcp_socket_.close();
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Socket closed on port " << d_port_ << '\n';
|
2012-03-16 10:55:58 +00:00
|
|
|
}
|