1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-15 12:40:35 +00:00

Adding missing files

This commit is contained in:
Javier 2018-10-21 19:33:02 +02:00
parent ae7be10325
commit 146363920d
2 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,187 @@
/*!
* \file tcp_cmd_interface.cc
*
* \brief Class that implements a TCP telecontrol command line interface
* for GNSS-SDR
* \author Javier Arribas jarribas (at) cttc.es
* -------------------------------------------------------------------------
*
* Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors)
*
* 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
* (at your option) any later version.
*
* 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 <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#include "tcp_cmd_interface.h"
std::string TcpCmdInterface::stop(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
std::string TcpCmdInterface::status(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
std::string TcpCmdInterface::assistedstart(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
std::string TcpCmdInterface::warmstart(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
std::string TcpCmdInterface::coldstart(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
std::string TcpCmdInterface::set_ch_satellite(const std::vector<std::string> &commandLine)
{
std::string response;
response = "Not implemented\n";
return response;
}
void TcpCmdInterface::register_functions()
{
functions["status"] = status;
functions["stop"] = stop;
functions["assistedstart"] = assistedstart;
functions["warmstart"] = warmstart;
functions["coldstart"] = coldstart;
functions["set_ch_satellite"] = set_ch_satellite;
}
TcpCmdInterface::TcpCmdInterface()
{
register_functions();
}
void TcpCmdInterface::run_cmd_server(int tcp_port)
{
// Get the port from the parameters
uint16_t port = tcp_port;
// Error to not throw exception
boost::system::error_code not_throw;
// Socket and acceptor
boost::asio::io_service service;
boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
bool keep_running = true;
while (keep_running)
{
try
{
std::cout << "Telecommand TCP interface listening on port " << tcp_port << std::endl;
boost::asio::ip::tcp::socket socket(service);
acceptor.accept(socket, not_throw);
if (not_throw)
{
std::cerr << "Error when binding the port in the socket" << std::endl;
continue;
}
// Read a message
boost::system::error_code error = boost::asio::error::eof;
do
{
std::string response;
boost::asio::streambuf b;
boost::asio::read_until(socket, b, '\n');
std::istream is(&b);
std::string line;
std::getline(is, line);
std::cout << "received command: " << line << std::endl;
std::istringstream iss(line);
std::vector<std::string> cmd_vector(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if (cmd_vector.size() > 0)
{
try
{
response = functions[cmd_vector.at(0)](cmd_vector);
}
catch (const std::exception &ex)
{
response = "ERROR: command execution error: " + std::string(ex.what()) + "\n";
}
}
else
{
response = "ERROR: empty command\n";
}
//send cmd response
socket.write_some(boost::asio::buffer(response), not_throw);
if (not_throw)
{
std::cerr << "Error sending(" << not_throw.value() << "): " << not_throw.message() << std::endl;
break;
}
}
while (error > 0); // && error != boost::asio::error::eof);
if (error == boost::asio::error::eof)
{
std::cout << "EOF detected\n";
}
else
{
std::cout << "error: " << error << std::endl;
}
// Close socket
socket.close();
}
catch (const std::exception &ex)
{
std::cout << "Exception " << ex.what() << std::endl;
}
}
}
TcpCmdInterface::~TcpCmdInterface()
{
// TODO Auto-generated destructor stub
}

View File

@ -0,0 +1,64 @@
/*!
* \file tcp_cmd_interface.h
*
* \brief Class that implements a TCP telecontrol command line interface
* for GNSS-SDR
* \author Javier Arribas jarribas (at) cttc.es
* -------------------------------------------------------------------------
*
* Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors)
*
* 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
* (at your option) any later version.
*
* 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 <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef TCPCMDINTERFACE_H_
#define TCPCMDINTERFACE_H_
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <boost/asio.hpp>
#include <stdint.h>
class TcpCmdInterface
{
public:
TcpCmdInterface();
virtual ~TcpCmdInterface();
void run_cmd_server(int tcp_port);
private:
std::unordered_map<std::string, std::function<std::string(const std::vector<std::string> &)>>
functions;
static std::string status(const std::vector<std::string> &commandLine);
static std::string stop(const std::vector<std::string> &commandLine);
static std::string assistedstart(const std::vector<std::string> &commandLine);
static std::string warmstart(const std::vector<std::string> &commandLine);
static std::string coldstart(const std::vector<std::string> &commandLine);
static std::string set_ch_satellite(const std::vector<std::string> &commandLine);
void register_functions();
};
#endif /* TCPCMDINTERFACE_H_ */