2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2022-08-11 15:36:19 -04:00
|
|
|
// QP/C++ Real-Time Embedded Framework (RTEF)
|
|
|
|
// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
|
|
|
|
//
|
|
|
|
// This software is dual-licensed under the terms of the open source GNU
|
|
|
|
// General Public License version 3 (or any later version), or alternatively,
|
|
|
|
// under the terms of one of the closed source Quantum Leaps commercial
|
|
|
|
// licenses.
|
|
|
|
//
|
|
|
|
// The terms of the open source GNU General Public License version 3
|
|
|
|
// can be found at: <www.gnu.org/licenses/gpl-3.0>
|
|
|
|
//
|
|
|
|
// The terms of the closed source Quantum Leaps commercial licenses
|
|
|
|
// can be found at: <www.state-machine.com/licensing>
|
|
|
|
//
|
|
|
|
// Redistributions in source code must retain this top-level comment block.
|
|
|
|
// Plagiarizing this software to sidestep the license obligations is illegal.
|
|
|
|
//
|
|
|
|
// Contact information:
|
|
|
|
// <www.state-machine.com>
|
|
|
|
// <info@state-machine.com>
|
2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2023-12-14 16:55:58 -05:00
|
|
|
//! @date Last updated on: 2023-12-13
|
|
|
|
//! @version Last updated for: @ref qpcpp_7_3_2
|
2022-04-19 19:23:30 -04:00
|
|
|
//!
|
2022-08-11 15:36:19 -04:00
|
|
|
//! @file
|
2023-01-19 21:14:02 -05:00
|
|
|
//! @brief QS/C++ port to POSIX
|
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
|
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
#ifndef Q_SPY
|
2018-11-22 11:34:13 -05:00
|
|
|
#error "Q_SPY must be defined to compile qs_port.cpp"
|
2018-10-25 11:13:01 -04:00
|
|
|
#endif // Q_SPY
|
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qp_port.hpp" // QP port
|
|
|
|
#include "qsafe.h" // QP Functional Safety (FuSa) Subsystem
|
|
|
|
#include "qs_port.hpp" // QS port
|
2018-10-25 11:13:01 -04:00
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
#include "safe_std.h" // portable "safe" <stdio.h>/<string.h> facilities
|
2018-10-25 11:13:01 -04:00
|
|
|
#include <stdlib.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>
|
|
|
|
#include <fcntl.h>
|
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
|
2023-01-19 21:14:02 -05:00
|
|
|
#define QS_TIMEOUT_MS 10L
|
2018-10-25 11:13:01 -04:00
|
|
|
|
|
|
|
#define INVALID_SOCKET -1
|
2018-11-22 11:34:13 -05:00
|
|
|
#define SOCKET_ERROR -1
|
2018-10-25 11:13:01 -04:00
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
namespace { // unnamed local namespace
|
2018-10-25 11:13:01 -04:00
|
|
|
|
2018-11-22 11:34:13 -05:00
|
|
|
//DEFINE_THIS_MODULE("qs_port")
|
2018-10-25 11:13:01 -04:00
|
|
|
|
|
|
|
// local variables ...........................................................
|
|
|
|
static int l_sock = INVALID_SOCKET;
|
2019-11-04 12:29:48 -05:00
|
|
|
static struct timespec const c_timeout = { 0, QS_TIMEOUT_MS*1000000L };
|
2018-10-25 11:13:01 -04:00
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
|
|
|
|
} // unnamed local namespace
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
namespace QP {
|
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
//............................................................................
|
|
|
|
bool QS::onStartup(void const *arg) {
|
2023-01-19 21:14:02 -05:00
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
static uint8_t qsBuf[QS_TX_SIZE]; // buffer for QS-TX channel
|
2023-01-19 21:14:02 -05:00
|
|
|
initBuf(qsBuf, sizeof(qsBuf));
|
|
|
|
|
2018-10-25 11:13:01 -04:00
|
|
|
static uint8_t qsRxBuf[QS_RX_SIZE]; // buffer for QS-RX channel
|
2023-01-19 21:14:02 -05:00
|
|
|
rxInitBuf(qsRxBuf, sizeof(qsRxBuf));
|
|
|
|
|
2018-11-22 11:34:13 -05:00
|
|
|
char hostName[128];
|
2019-11-04 12:29:48 -05:00
|
|
|
char const *serviceName = "6601"; // default QSPY server port
|
2018-10-25 11:13:01 -04:00
|
|
|
char const *src;
|
|
|
|
char *dst;
|
2018-11-22 11:34:13 -05:00
|
|
|
int status;
|
2018-10-25 11:13:01 -04:00
|
|
|
|
2018-11-22 11:34:13 -05:00
|
|
|
struct addrinfo *result = NULL;
|
|
|
|
struct addrinfo *rp = NULL;
|
|
|
|
struct addrinfo hints;
|
2018-10-25 11:13:01 -04:00
|
|
|
int sockopt_bool;
|
|
|
|
|
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
|
2018-10-25 11:13:01 -04:00
|
|
|
dst = hostName;
|
|
|
|
while ((*src != '\0')
|
|
|
|
&& (*src != ':')
|
2018-11-22 11:34:13 -05:00
|
|
|
&& (dst < &hostName[sizeof(hostName) - 1]))
|
2018-10-25 11:13:01 -04:00
|
|
|
{
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
2018-11-22 11:34:13 -05:00
|
|
|
*dst = '\0'; // zero-terminate hostName
|
|
|
|
|
2019-06-19 11:20:12 -04:00
|
|
|
// extract serviceName from 'arg' (hostName:serviceName)...
|
2018-10-25 11:13:01 -04:00
|
|
|
if (*src == ':') {
|
2018-11-22 11:34:13 -05:00
|
|
|
serviceName = src + 1;
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
|
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",
|
2023-01-19 21:14:02 -05:00
|
|
|
hostName, serviceName, status);
|
2018-10-25 11:13:01 -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;
|
|
|
|
}
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:34:13 -05:00
|
|
|
freeaddrinfo(result);
|
2018-10-25 11:13:01 -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",
|
|
|
|
errno);
|
2018-10-25 11:13:01 -04:00
|
|
|
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);
|
2023-01-19 21:14:02 -05:00
|
|
|
QF::stop(); // <== stop and exit the application
|
2018-10-25 11:13:01 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-06-19 11:20:12 -04: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-06-19 11:20:12 -04: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));
|
2018-10-25 11:13:01 -04:00
|
|
|
onFlush();
|
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
return true; // success
|
2018-10-25 11:13:01 -04:00
|
|
|
|
|
|
|
error:
|
|
|
|
return false; // failure
|
|
|
|
}
|
|
|
|
//............................................................................
|
2023-01-19 21:14:02 -05:00
|
|
|
void QS::onCleanup() {
|
2018-10-25 11:13:01 -04:00
|
|
|
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");
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
//............................................................................
|
2023-01-19 21:14:02 -05:00
|
|
|
void QS::onReset() {
|
2018-10-25 11:13:01 -04:00
|
|
|
onCleanup();
|
2023-01-19 21:14:02 -05:00
|
|
|
//PRINTF_S("\n%s\n", "QS_onReset");
|
2018-10-25 11:13:01 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
//............................................................................
|
2023-12-14 16:55:58 -05:00
|
|
|
// NOTE:
|
|
|
|
// No critical section in QS::onFlush() to avoid nesting of critical sections
|
|
|
|
// in case QS::onFlush() is called from Q_onError().
|
2023-01-19 21:14:02 -05:00
|
|
|
void QS::onFlush() {
|
2018-11-29 13:16:29 -05:00
|
|
|
if (l_sock == INVALID_SOCKET) { // socket NOT initialized?
|
2023-01-19 21:14:02 -05:00
|
|
|
FPRINTF_S(stderr, "<TARGET> ERROR %s\n",
|
|
|
|
"invalid TCP socket");
|
|
|
|
QF::stop(); // <== stop and exit the application
|
2018-11-22 11:34:13 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
std::uint16_t nBytes = QS_TX_CHUNK;
|
|
|
|
std::uint8_t const *data;
|
|
|
|
while ((data = getBlock(&nBytes)) != nullptr) {
|
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);
|
2019-06-19 11:20:12 -04:00
|
|
|
if (nSent == SOCKET_ERROR) { // sending failed?
|
2018-11-29 13:16:29 -05:00
|
|
|
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
2019-11-04 12:29:48 -05:00
|
|
|
// sleep for the timeout and then loop back
|
2018-11-29 13:16:29 -05:00
|
|
|
// to send() the SAME data again
|
2019-11-04 12:29:48 -05:00
|
|
|
nanosleep(&c_timeout, NULL);
|
2018-11-29 13:16:29 -05:00
|
|
|
}
|
|
|
|
else { // some other socket error...
|
2023-01-19 21:14:02 -05:00
|
|
|
FPRINTF_S(stderr, "<TARGET> ERROR sending data over TCP,"
|
|
|
|
"errno=%d\n", errno);
|
|
|
|
QF::stop(); // <== stop and exit the application
|
2018-11-29 13:16:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nSent < (int)nBytes) { // sent fewer than requested?
|
2019-11-04 12:29:48 -05: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 {
|
2023-01-19 21:14:02 -05:00
|
|
|
break; // break out of the for-ever loop
|
2018-11-29 13:16:29 -05:00
|
|
|
}
|
2018-10-25 11:13:01 -04: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;
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//............................................................................
|
2023-01-19 21:14:02 -05:00
|
|
|
QSTimeCtr QS::onGetTime() {
|
2018-11-22 11:34:13 -05:00
|
|
|
struct timespec tspec;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, &tspec);
|
|
|
|
|
|
|
|
// convert to units of 0.1 microsecond
|
2023-01-19 21:14:02 -05:00
|
|
|
QSTimeCtr time = (QSTimeCtr)(tspec.tv_sec * 10000000 + tspec.tv_nsec / 100);
|
2018-11-22 11:34:13 -05:00
|
|
|
return time;
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//............................................................................
|
2023-01-19 21:14:02 -05:00
|
|
|
void QS::doOutput() {
|
2018-11-29 13:16:29 -05:00
|
|
|
if (l_sock == INVALID_SOCKET) { // socket NOT initialized?
|
2023-01-19 21:14:02 -05:00
|
|
|
FPRINTF_S(stderr, "<TARGET> ERROR %s\n",
|
|
|
|
"invalid TCP socket");
|
|
|
|
QF::stop(); // <== stop and exit the application
|
2018-11-22 11:34:13 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:14:02 -05:00
|
|
|
QS_CRIT_STAT
|
|
|
|
QS_CRIT_ENTRY();
|
|
|
|
std::uint16_t nBytes = QS_TX_CHUNK;
|
|
|
|
std::uint8_t const *data = getBlock(&nBytes);
|
|
|
|
QS_CRIT_EXIT();
|
|
|
|
|
|
|
|
if (nBytes > 0U) { // any bytes to send?
|
2018-11-29 13:16:29 -05:00
|
|
|
for (;;) { // for-ever until break or return
|
2023-01-19 21:14:02 -05:00
|
|
|
int nSent = send(l_sock, reinterpret_cast<char const *>(data),
|
|
|
|
static_cast<int>(nBytes), 0);
|
2019-06-19 11:20:12 -04:00
|
|
|
if (nSent == SOCKET_ERROR) { // sending failed?
|
2018-11-29 13:16:29 -05:00
|
|
|
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
2023-01-19 21:14:02 -05:00
|
|
|
// sleep for the timeout and then loop back
|
2018-11-29 13:16:29 -05:00
|
|
|
// to send() the SAME data again
|
2019-11-04 12:29:48 -05:00
|
|
|
nanosleep(&c_timeout, NULL);
|
2018-11-29 13:16:29 -05:00
|
|
|
}
|
|
|
|
else { // some other socket error...
|
2023-01-19 21:14:02 -05:00
|
|
|
FPRINTF_S(stderr, "<TARGET> ERROR sending data over TCP,"
|
|
|
|
"errno=%d\n", errno);
|
|
|
|
QF::stop(); // <== stop and exit the application
|
2018-11-29 13:16:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nSent < (int)nBytes) { // sent fewer than requested?
|
2019-11-04 12:29:48 -05: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 {
|
2023-01-19 21:14:02 -05:00
|
|
|
break; // break out of the for-ever loop
|
2018-11-29 13:16:29 -05:00
|
|
|
}
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
2019-06-19 11:20:12 -04:00
|
|
|
}
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
//............................................................................
|
2022-08-11 15:36:19 -04:00
|
|
|
void QS::doInput(void) {
|
2021-01-14 13:10:48 -05:00
|
|
|
int status = recv(l_sock,
|
|
|
|
(char *)QS::rxPriv_.buf, (int)QS::rxPriv_.end, 0);
|
|
|
|
if (status > 0) { // any data received?
|
|
|
|
QS::rxPriv_.tail = 0U;
|
|
|
|
QS::rxPriv_.head = status; // # bytes received
|
|
|
|
QS::rxParse(); // parse all received bytes
|
2018-10-25 11:13:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:34:13 -05:00
|
|
|
} // namespace QP
|
2023-01-19 21:14:02 -05:00
|
|
|
|