qpcpp/ports/posix-qutest/qutest_port.cpp

306 lines
9.9 KiB
C++
Raw Normal View History

2018-10-25 11:13:01 -04:00
/// @file
2018-11-22 11:34:13 -05:00
/// @brief QUTEST port for POSIX
/// @ingroup ports
2017-05-17 13:15:09 -04:00
/// @cond
///***************************************************************************
2020-03-17 21:33:58 -04:00
/// Last updated for version 6.8.0
2020-04-02 21:21:53 -04:00
/// Last updated on 2020-03-31
2017-05-17 13:15:09 -04:00
///
2018-10-25 11:13:01 -04:00
/// Q u a n t u m L e a P s
/// ------------------------
/// Modern Embedded Software
2017-05-17 13:15:09 -04:00
///
2020-03-17 21:33:58 -04:00
/// Copyright (C) 2005-2020 Quantum Leaps. All rights reserved.
2017-05-17 13:15:09 -04:00
///
/// This program is open source 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.
///
/// Alternatively, this program may be distributed and modified under the
/// terms of Quantum Leaps commercial licenses, which expressly supersede
/// the GNU General Public License and are specifically designed for
/// licensees interested in retaining the proprietary status of their code.
///
/// 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
2019-10-27 12:26:31 -04:00
/// along with this program. If not, see <www.gnu.org/licenses>.
2017-05-17 13:15:09 -04:00
///
/// Contact information:
2019-12-31 15:56:23 -05:00
/// <www.state-machine.com/licensing>
2019-10-27 12:26:31 -04:00
/// <info@state-machine.com>
2017-05-17 13:15:09 -04:00
///***************************************************************************
/// @endcond
///
2019-02-10 21:01:38 -05:00
// expose features from the 2008 POSIX standard (IEEE Standard 1003.1-2008)
#define _POSIX_C_SOURCE 200809L
2017-05-17 13:15:09 -04:00
#ifndef Q_SPY
2019-06-19 11:20:12 -04:00
#error "Q_SPY must be defined for QUTest application"
2017-05-17 13:15:09 -04:00
#endif // Q_SPY
2019-11-04 12:29:48 -05:00
#define QP_IMPL // this is QP implementation
2019-10-27 12:26:31 -04:00
#include "qf_port.hpp" // QF port
2019-11-04 12:29:48 -05:00
#include "qassert.h" // QP embedded systems-friendly assertions
2020-03-17 21:33:58 -04:00
#include "qs_port.hpp" // QS port
#include "qs_pkg.hpp" // QS package-scope internal interface
2017-05-17 13:15:09 -04:00
2020-04-02 21:21:53 -04:00
#include "safe_std.h" // portable "safe" <stdio.h>/<string.h> facilities
2017-05-17 13:15:09 -04:00
#include <stdlib.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
2019-11-04 12:29:48 -05:00
#include <time.h>
2017-05-17 13:15:09 -04:00
#include <unistd.h>
2018-10-25 11:13:01 -04:00
#include <fcntl.h>
2018-03-19 14:51:26 -04:00
#include <signal.h>
2017-05-17 13:15:09 -04:00
2018-11-22 11:34:13 -05:00
#define QS_TX_SIZE (8*1024)
#define QS_RX_SIZE (2*1024)
#define QS_TX_CHUNK QS_TX_SIZE
2019-06-19 11:20:12 -04:00
#define QS_TIMEOUT_MS 10
2018-11-22 11:34:13 -05:00
2017-05-17 13:15:09 -04:00
#define INVALID_SOCKET -1
2018-11-22 11:34:13 -05:00
#define SOCKET_ERROR -1
2017-05-17 13:15:09 -04:00
namespace QP {
2018-11-22 11:34:13 -05:00
//Q_DEFINE_THIS_MODULE("qutest_port")
2017-05-17 13:15:09 -04:00
// local variables ...........................................................
static int l_sock = INVALID_SOCKET;
2018-11-22 11:34:13 -05:00
static void sigIntHandler(int dummy);
2017-05-17 13:15:09 -04:00
//............................................................................
bool QS::onStartup(void const *arg) {
static uint8_t qsBuf[QS_TX_SIZE]; // buffer for QS-TX channel
static uint8_t qsRxBuf[QS_RX_SIZE]; // buffer for QS-RX channel
2018-03-19 14:51:26 -04:00
char hostName[128];
2019-06-19 11:20:12 -04:00
char const *serviceName = "6601"; // default QSPY server port
2017-05-17 13:15:09 -04:00
char const *src;
char *dst;
2018-11-22 11:34:13 -05:00
int status;
2017-05-17 13:15:09 -04:00
2018-11-22 11:34:13 -05:00
struct addrinfo *result = NULL;
struct addrinfo *rp = NULL;
struct addrinfo hints;
2017-05-17 13:15:09 -04:00
int sockopt_bool;
2019-11-04 12:29:48 -05:00
2018-03-19 14:51:26 -04:00
struct sigaction sig_act;
2017-05-17 13:15:09 -04:00
2018-11-22 11:34:13 -05:00
// initialize the QS transmit and receive buffers
2017-05-17 13:15:09 -04:00
initBuf(qsBuf, sizeof(qsBuf));
rxInitBuf(qsRxBuf, sizeof(qsRxBuf));
2018-11-22 11:34:13 -05:00
// extract hostName from 'arg' (hostName:port_remote)...
2020-03-17 21:33:58 -04:00
src = (arg != nullptr)
2018-11-22 11:34:13 -05:00
? static_cast<char const *>(arg)
: "localhost"; // default QSPY host
2017-05-17 13:15:09 -04:00
dst = hostName;
while ((*src != '\0')
&& (*src != ':')
2018-11-22 11:34:13 -05:00
&& (dst < &hostName[sizeof(hostName) - 1]))
2017-05-17 13:15:09 -04:00
{
*dst++ = *src++;
}
2018-11-22 11:34:13 -05:00
*dst = '\0'; // zero-terminate hostName
2020-04-02 21:21:53 -04:00
// extract serviceName from 'arg' (hostName:serviceName)...
2017-05-17 13:15:09 -04:00
if (*src == ':') {
2018-11-22 11:34:13 -05:00
serviceName = src + 1;
2017-05-17 13:15:09 -04:00
}
2020-04-02 21:21:53 -04:00
//PRINTF_S("<TARGET> Connecting to QSPY on Host=%s:%s...\n",
// hostName, serviceName);
2018-11-22 11:34:13 -05:00
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
status = getaddrinfo(hostName, serviceName, &hints, &result);
if (status != 0) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr,
2018-11-22 11:34:13 -05:00
"<TARGET> ERROR cannot resolve host Name=%s:%s,Err=%d\n",
2020-04-02 21:21:53 -04:00
hostName, serviceName, status);
2017-05-17 13:15:09 -04:00
goto error;
}
2018-11-22 11:34:13 -05:00
for (rp = result; rp != NULL; rp = rp->ai_next) {
l_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (l_sock != INVALID_SOCKET) {
if (connect(l_sock, rp->ai_addr, rp->ai_addrlen)
== SOCKET_ERROR)
{
close(l_sock);
l_sock = INVALID_SOCKET;
}
break;
}
2017-05-17 13:15:09 -04:00
}
2018-03-19 14:51:26 -04:00
2018-11-22 11:34:13 -05:00
freeaddrinfo(result);
2017-05-17 13:15:09 -04:00
2018-11-22 11:34:13 -05:00
// socket could not be opened & connected?
if (l_sock == INVALID_SOCKET) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR cannot connect to QSPY at "
2018-11-22 11:34:13 -05:00
"host=%s:%s\n",
hostName, serviceName);
2018-10-25 11:13:01 -04:00
goto error;
}
2018-11-22 11:34:13 -05:00
// set the socket to non-blocking mode
status = fcntl(l_sock, F_GETFL, 0);
if (status == -1) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr,
2018-11-22 11:34:13 -05:00
"<TARGET> ERROR Socket configuration failed errno=%d\n",
2018-10-25 11:13:01 -04:00
errno);
QS_EXIT();
goto error;
}
2018-11-22 11:34:13 -05:00
if (fcntl(l_sock, F_SETFL, status | O_NONBLOCK) != 0) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR Failed to set non-blocking socket "
2018-11-22 11:34:13 -05:00
"errno=%d\n", errno);
2018-10-25 11:13:01 -04:00
QS_EXIT();
2017-05-17 13:15:09 -04:00
goto error;
}
2019-11-04 12:29:48 -05:00
// configure the socket to reuse the address and not to linger
2018-11-22 11:34:13 -05:00
sockopt_bool = 1;
setsockopt(l_sock, SOL_SOCKET, SO_REUSEADDR,
&sockopt_bool, sizeof(sockopt_bool));
2019-11-04 12:29:48 -05:00
sockopt_bool = 0; // negative option
2018-11-22 11:34:13 -05:00
setsockopt(l_sock, SOL_SOCKET, SO_LINGER,
&sockopt_bool, sizeof(sockopt_bool));
2020-04-02 21:21:53 -04:00
//PRINTF_S("<TARGET> Connected to QSPY at Host=%s:%d\n",
// hostName, port_remote);
2018-03-19 14:51:26 -04:00
onFlush();
2017-05-17 13:15:09 -04:00
2018-03-19 14:51:26 -04:00
// install the SIGINT (Ctrl-C) signal handler
sig_act.sa_handler = &sigIntHandler;
sigaction(SIGINT, &sig_act, NULL);
2017-05-17 13:15:09 -04:00
return true; // success
error:
return false; // failure
}
//............................................................................
void QS::onCleanup(void) {
if (l_sock != INVALID_SOCKET) {
close(l_sock);
l_sock = INVALID_SOCKET;
}
2020-04-02 21:21:53 -04:00
//PRINTF_S("%s\n", "<TARGET> Disconnected from QSPY");
2017-05-17 13:15:09 -04:00
}
//............................................................................
void QS::onReset(void) {
onCleanup();
exit(0);
}
2018-11-22 11:34:13 -05:00
//............................................................................
void QS::onFlush(void) {
uint16_t nBytes;
uint8_t const *data;
2019-06-19 11:20:12 -04:00
static struct timespec const c_timeout = { 0, QS_TIMEOUT_MS * 1000000L };
2017-05-17 13:15:09 -04:00
2018-11-29 13:16:29 -05:00
if (l_sock == INVALID_SOCKET) { // socket NOT initialized?
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr, "%s\n", "<TARGET> ERROR invalid TCP socket");
2018-11-22 11:34:13 -05:00
return;
}
nBytes = QS_TX_CHUNK;
while ((data = getBlock(&nBytes)) != (uint8_t *)0) {
2018-11-29 13:16:29 -05:00
for (;;) { // for-ever until break or return
int nSent = send(l_sock, (char const *)data, (int)nBytes, 0);
2020-04-02 21:21:53 -04:00
if (nSent == SOCKET_ERROR) { // sending failed?
2018-11-29 13:16:29 -05:00
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
2019-06-19 11:20:12 -04:00
// sleep for the timeout and then loop back
2018-11-29 13:16:29 -05:00
// to send() the SAME data again
//
2019-06-19 11:20:12 -04:00
nanosleep(&c_timeout, NULL);
2018-11-29 13:16:29 -05:00
}
else { // some other socket error...
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr,
"<TARGET> ERROR sending data over TCP,errno=%d\n",
errno);
2018-11-29 13:16:29 -05:00
return;
}
}
else if (nSent < (int)nBytes) { // sent fewer than requested?
2020-04-02 21:21:53 -04:00
nanosleep(&c_timeout, NULL); // sleep for the timeout
2018-11-29 13:16:29 -05:00
// adjust the data and loop back to send() the rest
data += nSent;
nBytes -= (uint16_t)nSent;
}
else {
break;
}
2018-11-22 11:34:13 -05:00
}
2018-11-29 13:16:29 -05:00
// set nBytes for the next call to QS::getBlock()
2018-11-22 11:34:13 -05:00
nBytes = QS_TX_CHUNK;
}
}
2017-05-17 13:15:09 -04:00
//............................................................................
void QS::onTestLoop() {
fd_set readSet;
FD_ZERO(&readSet);
rxPriv_.inTestLoop = true;
while (rxPriv_.inTestLoop) {
2019-06-19 11:20:12 -04:00
FD_SET(l_sock, &readSet);
2017-05-17 13:15:09 -04:00
2018-10-25 11:13:01 -04:00
// selective, timed blocking on the TCP/IP socket...
2019-06-19 11:20:12 -04:00
struct timeval timeout = {
(long)0, (long)(QS_TIMEOUT_MS * 1000)
};
int nrec = select(l_sock + 1, &readSet,
(fd_set *)0, (fd_set *)0, &timeout);
2017-05-17 13:15:09 -04:00
if (nrec < 0) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR socket select,errno=%d\n",
2018-10-25 11:13:01 -04:00
errno);
2017-05-17 13:15:09 -04:00
onCleanup();
exit(-2);
}
2019-06-19 11:20:12 -04:00
else if ((nrec > 0) && FD_ISSET(l_sock, &readSet)) { // socket ready?
uint8_t buf[QS_RX_SIZE];
int status = recv(l_sock, (char *)buf, (int)sizeof(buf), 0);
while (status > 0) { // any data received?
uint8_t *pb;
int i = (int)rxGetNfree();
if (i > status) {
i = status;
}
status -= i;
// reorder the received bytes into QS-RX buffer
for (pb = &buf[0]; i > 0; --i, ++pb) {
rxPut(*pb);
2017-05-17 13:15:09 -04:00
}
2019-06-19 11:20:12 -04:00
rxParse(); // parse all n-bytes of data
2017-05-17 13:15:09 -04:00
}
}
2018-11-29 13:16:29 -05:00
// flush the QS TX buffer
onFlush();
2017-05-17 13:15:09 -04:00
}
// set inTestLoop to true in case calls to QS_onTestLoop() nest,
// which can happen through the calls to QS_TEST_PAUSE().
rxPriv_.inTestLoop = true;
}
2018-03-19 14:51:26 -04:00
//............................................................................
2018-10-25 11:13:01 -04:00
static void sigIntHandler(int /*dummy*/) {
2018-03-19 14:51:26 -04:00
QS::onCleanup();
2020-04-02 21:21:53 -04:00
//PRINTF_S("\n%s\n","<TARGET> disconnecting from QSPY");
2018-03-19 14:51:26 -04:00
exit(-1);
}
2017-05-17 13:15:09 -04:00
} // namespace QP
2018-03-19 14:51:26 -04:00