qpcpp/ports/posix-qutest/qutest_port.cpp

292 lines
9.1 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
///***************************************************************************
2018-11-22 11:34:13 -05:00
/// Last updated for version 6.3.7
/// Last updated on 2018-11-09
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
///
2018-03-19 14:51:26 -04:00
/// Copyright (C) 2005-2018 Quantum Leaps, LLC. 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
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// Contact information:
2018-03-19 14:51:26 -04:00
/// https://www.state-machine.com
2017-05-17 13:15:09 -04:00
/// mailto:info@state-machine.com
///***************************************************************************
/// @endcond
///
#ifndef Q_SPY
2018-11-22 11:34:13 -05:00
#error "Q_SPY must be defined for QUTEST application"
2017-05-17 13:15:09 -04:00
#endif // Q_SPY
#define QP_IMPL // this is QP implementation
#include "qf_port.h" // QF port
#include "qassert.h" // QP embedded systems-friendly assertions
#include "qs_port.h" // include QS port
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <time.h>
#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>
2018-10-25 11:13:01 -04:00
#include <stdio.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
2018-03-19 14:51:26 -04:00
#define QS_IMEOUT_MS 100
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];
2018-11-22 11:34:13 -05: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;
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)...
src = (arg != static_cast<void const *>(0))
? 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
// extract port_remote from 'arg' (hostName:port_remote)...
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
}
2018-11-22 11:34:13 -05:00
//printf("<TARGET> Connecting to QSPY on Host=%s:%s...\n",
// hostName, serviceName);
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) {
2018-10-25 11:13:01 -04:00
fprintf(stderr,
2018-11-22 11:34:13 -05:00
"<TARGET> ERROR cannot resolve host Name=%s:%s,Err=%d\n",
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) {
fprintf(stderr, "<TARGET> ERROR cannot connect to QSPY at "
"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) {
2018-10-25 11:13:01 -04:00
fprintf(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) {
fprintf(stderr, "<TARGET> ERROR Failed to set non-blocking socket "
"errno=%d\n", errno);
2018-10-25 11:13:01 -04:00
QS_EXIT();
2017-05-17 13:15:09 -04:00
goto error;
}
2018-11-22 11:34:13 -05:00
/* configure the socket to reuse the address and not to linger */
sockopt_bool = 1;
setsockopt(l_sock, SOL_SOCKET, SO_REUSEADDR,
&sockopt_bool, sizeof(sockopt_bool));
sockopt_bool = 0; /* negative option */
setsockopt(l_sock, SOL_SOCKET, SO_LINGER,
&sockopt_bool, sizeof(sockopt_bool));
2018-03-19 14:51:26 -04:00
//printf("<TARGET> Connected to QSPY at Host=%s:%d\n",
// hostName, port_remote);
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;
}
2018-03-19 14:51:26 -04:00
//printf("<TARGET> Disconnected from QSPY\n");
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;
2017-05-17 13:15:09 -04:00
2018-11-22 11:34:13 -05:00
if (l_sock == INVALID_SOCKET) { // socket initialized?
return;
}
nBytes = QS_TX_CHUNK;
while ((data = getBlock(&nBytes)) != (uint8_t *)0) {
int nSent = send(l_sock, (char const *)data, (int)nBytes, 0);
// the driver buffers the output, so it should accept all the bytes
if (nSent < (int)nBytes) {
fprintf(stderr, "<TARGET> ERROR sending data over TCP,"
"errno=%d\n", errno);
}
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) {
static struct timeval timeout = {
(long)0, (long)(QS_IMEOUT_MS * 1000)
};
int nrec;
uint16_t nBytes;
uint8_t const *block;
2018-10-25 11:13:01 -04:00
FD_SET(l_sock, &readSet); // the socket
2017-05-17 13:15:09 -04:00
2018-10-25 11:13:01 -04:00
// selective, timed blocking on the TCP/IP socket...
2017-05-17 13:15:09 -04:00
timeout.tv_usec = (long)(QS_IMEOUT_MS * 1000);
nrec = select(l_sock + 1, &readSet,
(fd_set *)0, (fd_set *)0, &timeout);
if (nrec < 0) {
2018-10-25 11:13:01 -04:00
fprintf(stderr,
"<TARGET> ERROR socket select,errno=%d\n",
errno);
2017-05-17 13:15:09 -04:00
onCleanup();
exit(-2);
}
else if (nrec > 0) {
2018-10-25 11:13:01 -04:00
if (FD_ISSET(l_sock, &readSet)) { // socket ready to read?
2017-05-17 13:15:09 -04:00
uint8_t buf[QS_RX_SIZE];
int status = recv(l_sock, (char *)buf, (int)sizeof(buf), 0);
2018-10-25 11:13:01 -04:00
while (status > 0) { // any data received?
2017-05-17 13:15:09 -04:00
uint8_t *pb;
int i = (int)rxGetNfree();
if (i > status) {
i = status;
}
status -= i;
2018-10-25 11:13:01 -04:00
// reorder the received bytes into QS-RX buffer
2017-05-17 13:15:09 -04:00
for (pb = &buf[0]; i > 0; --i, ++pb) {
rxPut(*pb);
}
2018-03-19 14:51:26 -04:00
rxParse(); // parse all n-bytes of data
2017-05-17 13:15:09 -04:00
}
}
}
nBytes = QS_TX_SIZE;
//QF_CRIT_ENTRY(dummy);
block = getBlock(&nBytes);
//QF_CRIT_EXIT(dummy);
if (block != (uint8_t *)0) {
send(l_sock, (char const *)block, nBytes, 0);
}
}
// 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();
2018-10-25 11:13:01 -04:00
//printf("\n<TARGET> disconnecting from QSPY\n");
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