qpc/ports/posix-qv/qs_port.c

307 lines
10 KiB
C
Raw Normal View History

2018-10-25 11:11:36 -04:00
/**
* @file
* @brief QS/C port to POSIX
* @ingroup ports
* @cond
******************************************************************************
2019-02-10 21:00:39 -05:00
* Last updated for version 6.4.0
* Last updated on 2019-02-10
2018-10-25 11:11:36 -04:00
*
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
*
2019-02-10 21:00:39 -05:00
* Copyright (C) 2005-2019 Quantum Leaps, LLC. All rights reserved.
2018-10-25 11:11:36 -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:
* https://www.state-machine.com
* mailto:info@state-machine.com
******************************************************************************
* @endcond
*/
2019-02-10 21:00:39 -05:00
/* expose features from the 2008 POSIX standard (IEEE Standard 1003.1-2008) */
#define _POSIX_C_SOURCE 200809L
2018-10-25 11:11:36 -04:00
#ifndef Q_SPY
#error "Q_SPY must be defined to compile qs_port.c"
#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 */
2018-11-22 11:35:21 -05:00
#include <stdio.h>
2018-10-25 11:11:36 -04:00
#include <stdlib.h>
#include <string.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:35:21 -05:00
//Q_DEFINE_THIS_MODULE("qs_port")
#define QS_TX_SIZE (8*1024)
#define QS_RX_SIZE (2*1024)
#define QS_TX_CHUNK QS_TX_SIZE
2018-10-25 11:11:36 -04:00
#define INVALID_SOCKET -1
2018-11-22 11:35:21 -05:00
#define SOCKET_ERROR -1
2018-10-25 11:11:36 -04:00
/* local variables .........................................................*/
static int l_sock = INVALID_SOCKET;
2018-11-29 13:17:28 -05:00
static struct timespec const c_10ms = { 0, 10000000L };
2018-10-25 11:11:36 -04:00
/*..........................................................................*/
uint8_t 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-11-22 11:35:21 -05:00
char hostName[128];
char const *serviceName = "6601"; /* default QSPY server port */
2018-10-25 11:11:36 -04:00
char const *src;
char *dst;
2018-11-22 11:35:21 -05:00
int status;
2018-10-25 11:11:36 -04:00
2018-11-22 11:35:21 -05:00
struct addrinfo *result = NULL;
struct addrinfo *rp = NULL;
struct addrinfo hints;
2018-10-25 11:11:36 -04:00
int sockopt_bool;
2018-11-22 11:35:21 -05:00
/* initialize the QS transmit and receive buffers */
2018-10-25 11:11:36 -04:00
QS_initBuf(qsBuf, sizeof(qsBuf));
QS_rxInitBuf(qsRxBuf, sizeof(qsRxBuf));
2018-11-22 11:35:21 -05:00
/* extract hostName from 'arg' (hostName:port_remote)... */
2018-10-25 11:11:36 -04:00
src = (arg != (void const *)0)
? (char const *)arg
2018-11-22 11:35:21 -05:00
: "localhost"; /* default QSPY host */
2018-10-25 11:11:36 -04:00
dst = hostName;
while ((*src != '\0')
&& (*src != ':')
2018-11-22 11:35:21 -05:00
&& (dst < &hostName[sizeof(hostName) - 1]))
2018-10-25 11:11:36 -04:00
{
*dst++ = *src++;
}
2018-11-22 11:35:21 -05:00
*dst = '\0'; /* zero-terminate hostName */
/* extract port_remote from 'arg' (hostName:port_remote)... */
2018-10-25 11:11:36 -04:00
if (*src == ':') {
2018-11-22 11:35:21 -05:00
serviceName = src + 1;
2018-10-25 11:11:36 -04:00
}
2018-11-22 11:35:21 -05:00
//printf("<TARGET> Connecting to QSPY on Host=%s:%s...\n",
// hostName, serviceName);
2018-10-25 11:11:36 -04:00
2018-11-22 11:35:21 -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) {
fprintf(stderr,
"<TARGET> ERROR cannot resolve host Name=%s:%s,Err=%d\n",
hostName, serviceName, status);
2018-10-25 11:11:36 -04:00
goto error;
}
2018-11-22 11:35:21 -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:11:36 -04:00
}
2018-11-22 11:35:21 -05:00
freeaddrinfo(result);
2018-10-25 11:11:36 -04:00
2018-11-22 11:35:21 -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:11:36 -04:00
goto error;
}
2018-11-22 11:35:21 -05:00
/* set the socket to non-blocking mode */
status = fcntl(l_sock, F_GETFL, 0);
if (status == -1) {
fprintf(stderr,
"<TARGET> ERROR Socket configuration failed errno=%d\n",
errno);
2018-10-25 11:11:36 -04:00
QS_EXIT();
goto error;
}
2018-11-22 11:35:21 -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:11:36 -04:00
QS_EXIT();
goto error;
}
2018-11-22 11:35:21 -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-10-25 11:11:36 -04:00
//printf("<TARGET> Connected to QSPY at Host=%s:%d\n",
// hostName, port_remote);
QS_onFlush();
2018-11-22 11:35:21 -05:00
return (uint8_t)1; /* success */
2018-10-25 11:11:36 -04:00
error:
return (uint8_t)0; /* failure */
}
/*..........................................................................*/
void QS_onCleanup(void) {
if (l_sock != INVALID_SOCKET) {
close(l_sock);
l_sock = INVALID_SOCKET;
}
//printf("<TARGET> Disconnected from QSPY\n");
}
/*..........................................................................*/
void QS_onReset(void) {
QS_onCleanup();
exit(0);
}
/*..........................................................................*/
void QS_onFlush(void) {
2018-11-22 11:35:21 -05:00
uint16_t nBytes;
uint8_t const *data;
2018-11-29 13:17:28 -05:00
if (l_sock == INVALID_SOCKET) { /* socket NOT initialized? */
fprintf(stderr, "<TARGET> ERROR invalid TCP socket\n");
2018-11-22 11:35:21 -05:00
return;
}
nBytes = QS_TX_CHUNK;
while ((data = QS_getBlock(&nBytes)) != (uint8_t *)0) {
2018-11-29 13:17:28 -05:00
for (;;) { /* for-ever until break or return */
int nSent = send(l_sock, (char const *)data, (int)nBytes, 0);
if (nSent == SOCKET_ERROR) { /* sending failed? */
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
/* sleep for 10ms and then loop back
* to send() the SAME data again
*/
nanosleep(&c_10ms, NULL);
}
else { /* some other socket error... */
fprintf(stderr, "<TARGET> ERROR sending data over TCP,"
"errno=%d\n", errno);
return;
}
}
else if (nSent < (int)nBytes) { /* sent fewer than requested? */
nanosleep(&c_10ms, NULL); /* sleep for 10ms */
/* adjust the data and loop back to send() the rest */
data += nSent;
nBytes -= (uint16_t)nSent;
}
else {
break;
}
2018-10-25 11:11:36 -04:00
}
2018-11-29 13:17:28 -05:00
/* set nBytes for the next call to QS_getBlock() */
2018-11-22 11:35:21 -05:00
nBytes = QS_TX_CHUNK;
2018-10-25 11:11:36 -04:00
}
}
/*..........................................................................*/
QSTimeCtr QS_onGetTime(void) {
2018-11-22 11:35:21 -05:00
struct timespec tspec;
QSTimeCtr time;
clock_gettime(CLOCK_MONOTONIC_RAW, &tspec);
/* convert to units of 0.1 microsecond */
time = (QSTimeCtr)(tspec.tv_sec * 10000000 + tspec.tv_nsec / 100);
return time;
2018-10-25 11:11:36 -04:00
}
/*..........................................................................*/
void QS_output(void) {
2018-11-22 11:35:21 -05:00
uint16_t nBytes;
uint8_t const *data;
2018-11-29 13:17:28 -05:00
if (l_sock == INVALID_SOCKET) { /* socket NOT initialized? */
fprintf(stderr, "<TARGET> ERROR invalid TCP socket\n");
2018-11-22 11:35:21 -05:00
return;
}
nBytes = QS_TX_CHUNK;
if ((data = QS_getBlock(&nBytes)) != (uint8_t *)0) {
2018-11-29 13:17:28 -05:00
for (;;) { /* for-ever until break or return */
int nSent = send(l_sock, (char const *)data, (int)nBytes, 0);
if (nSent == SOCKET_ERROR) { /* sending failed? */
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
/* sleep for 10ms and then loop back
* to send() the SAME data again
*/
nanosleep(&c_10ms, NULL);
}
else { /* some other socket error... */
fprintf(stderr, "<TARGET> ERROR sending data over TCP,"
"errno=%d\n", errno);
return;
}
}
else if (nSent < (int)nBytes) { /* sent fewer than requested? */
nanosleep(&c_10ms, NULL); /* sleep for 10ms */
/* adjust the data and loop back to send() the rest */
data += nSent;
nBytes -= (uint16_t)nSent;
}
else {
break;
}
2018-10-25 11:11:36 -04:00
}
}
}
/*..........................................................................*/
void QS_rx_input(void) {
uint8_t buf[QS_RX_SIZE];
int status = recv(l_sock, (char *)buf, (int)sizeof(buf), 0);
2018-11-22 11:35:21 -05:00
if (status != SOCKET_ERROR) { /* any data received? */
2018-10-25 11:11:36 -04:00
uint8_t *pb;
int i = (int)QS_rxGetNfree();
if (i > status) {
i = status;
}
status -= i;
/* reorder the received bytes into QS-RX buffer */
for (pb = &buf[0]; i > 0; --i, ++pb) {
QS_RX_PUT(*pb);
}
QS_rxParse(); /* parse all n-bytes of data */
}
}