qpc/ports/win32-qv/qs_port.c

308 lines
10 KiB
C
Raw Normal View History

2018-10-25 11:11:36 -04:00
/**
* @file
2018-11-22 11:35:21 -05:00
* @brief QS/C port for Win32 API
2018-10-25 11:11:36 -04:00
* @ingroup ports
* @cond
******************************************************************************
* Last updated for version 6.9.3
* Last updated on 2021-03-16
2018-10-25 11:11:36 -04:00
*
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
*
2021-01-14 13:11:17 -05:00
* Copyright (C) 2005-2021 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
2020-04-02 21:21:11 -04:00
* along with this program. If not, see <www.gnu.org/licenses>.
2018-10-25 11:11:36 -04:00
*
* Contact information:
2019-12-31 15:55:08 -05:00
* <www.state-machine.com/licensing>
* <info@state-machine.com>
2018-10-25 11:11:36 -04:00
******************************************************************************
* @endcond
*/
#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 */
2021-01-14 13:11:17 -05:00
#include "qs_port.h" /* QS port */
#include "qs_pkg.h" /* QS package-scope interface */
2018-10-25 11:11:36 -04:00
2020-04-02 21:21:11 -04:00
#include "safe_std.h" /* portable "safe" <stdio.h>/<string.h> facilities */
2018-10-25 11:11:36 -04:00
#include <stdlib.h>
#include <time.h>
#include <conio.h>
2019-06-19 11:21:18 -04:00
/* Minimum required Windows version is Windows-XP or newer (0x0501) */
#ifdef WINVER
#undef WINVER
#endif
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define WINVER _WIN32_WINNT_WINXP
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#include <ws2tcpip.h>
2018-11-22 11:35:21 -05:00
2020-04-02 21:21:11 -04:00
/*Q_DEFINE_THIS_MODULE("qs_port")*/
2018-10-25 11:11:36 -04:00
2018-11-22 11:35:21 -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:21:18 -04:00
#define QS_TIMEOUT_MS 10
2018-10-25 11:11:36 -04:00
/* local variables .........................................................*/
static SOCKET l_sock = INVALID_SOCKET;
/*..........................................................................*/
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 */
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
BOOL sockopt_bool;
2018-11-22 11:35:21 -05:00
ULONG ioctl_opt;
WSADATA wsaData;
2018-10-25 11:11:36 -04:00
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
/* initialize Windows sockets version 2.2 */
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
2020-04-02 21:21:11 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR %s\n",
"Windows Sockets cannot be initialized");
2018-10-25 11:11:36 -04:00
goto error;
}
2018-11-22 11:35:21 -05:00
/* extract hostName from 'arg' (hostName:port_remote)... */
2019-12-31 15:55:08 -05:00
src = (arg != (void *)0)
2018-10-25 11:11:36 -04:00
? (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 serviceName from 'arg' (hostName:serviceName)... */
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
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:11 -04:00
FPRINTF_S(stderr,
2018-11-22 11:35:21 -05:00
"<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, (int)rp->ai_addrlen)
2018-11-22 11:35:21 -05:00
== SOCKET_ERROR)
{
closesocket(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) {
2020-04-02 21:21:11 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR cannot connect to QSPY at "
2018-11-22 11:35:21 -05:00
"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 */
ioctl_opt = 1;
if (ioctlsocket(l_sock, FIONBIO, &ioctl_opt) != NO_ERROR) {
FPRINTF_S(stderr, "<TARGET> ERROR %s WASErr=%d\n,",
"Failed to set non-blocking socket", WSAGetLastError());
2018-10-25 11:11:36 -04:00
goto error;
}
2018-11-22 11:35:21 -05:00
/* configure the socket to reuse the address and not to linger */
sockopt_bool = TRUE;
setsockopt(l_sock, SOL_SOCKET, SO_REUSEADDR,
(const char *)&sockopt_bool, sizeof(sockopt_bool));
sockopt_bool = TRUE;
setsockopt(l_sock, SOL_SOCKET, SO_DONTLINGER,
(const char *)&sockopt_bool, sizeof(sockopt_bool));
2018-10-25 11:11:36 -04:00
QS_onFlush();
2020-03-17 21:33:20 -04:00
return 1U; /* success */
2018-10-25 11:11:36 -04:00
error:
2020-03-17 21:33:20 -04:00
return 0U; /* failure */
2018-10-25 11:11:36 -04:00
}
/*..........................................................................*/
void QS_onCleanup(void) {
if (l_sock != INVALID_SOCKET) {
closesocket(l_sock);
l_sock = INVALID_SOCKET;
}
WSACleanup();
2020-04-02 21:21:11 -04:00
/*PRINTF_S("<TARGET> Disconnected from QSPY\n");*/
2018-10-25 11:11:36 -04:00
}
/*..........................................................................*/
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;
2019-06-19 11:21:18 -04:00
QS_CRIT_STAT_
2018-11-22 11:35:21 -05:00
2018-11-29 13:17:28 -05:00
if (l_sock == INVALID_SOCKET) { /* socket NOT initialized? */
2020-04-02 21:21:11 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR %s\n",
"invalid TCP socket");
2018-11-22 11:35:21 -05:00
return;
}
nBytes = QS_TX_CHUNK;
2020-10-01 12:48:48 -04:00
QS_CRIT_E_();
2018-11-22 11:35:21 -05:00
while ((data = QS_getBlock(&nBytes)) != (uint8_t *)0) {
2020-10-01 12:48:48 -04:00
QS_CRIT_X_();
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? */
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK) {
2019-06-19 11:21:18 -04:00
/* sleep for the timeout and then loop back
2018-11-29 13:17:28 -05:00
* to send() the SAME data again
*/
2019-06-19 11:21:18 -04:00
Sleep(QS_TIMEOUT_MS);
2018-11-29 13:17:28 -05:00
}
else { /* some other socket error... */
FPRINTF_S(stderr, "<TARGET> ERROR %s WASErr=%d\n",
"sending data over TCP", err);
2018-11-29 13:17:28 -05:00
return;
}
}
else if (nSent < (int)nBytes) { /* sent fewer than requested? */
2019-06-19 11:21:18 -04:00
Sleep(QS_TIMEOUT_MS); /* sleep for the timeout */
2018-11-29 13:17:28 -05:00
/* adjust the data and loop back to send() the rest */
data += nSent;
nBytes -= (uint16_t)nSent;
}
else {
break; /* break out of the for-ever loop */
2018-11-29 13:17:28 -05:00
}
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;
2020-10-01 12:48:48 -04:00
QS_CRIT_E_();
2018-10-25 11:11:36 -04:00
}
2020-10-01 12:48:48 -04:00
QS_CRIT_X_();
2018-10-25 11:11:36 -04:00
}
2018-11-22 11:35:21 -05:00
/*..........................................................................*/
QSTimeCtr QS_onGetTime(void) {
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
return (QSTimeCtr)time.QuadPart;
}
/*..........................................................................*/
void QS_output(void) {
uint16_t nBytes;
uint8_t const *data;
2019-06-19 11:21:18 -04:00
QS_CRIT_STAT_
2018-11-22 11:35:21 -05:00
2018-11-29 13:17:28 -05:00
if (l_sock == INVALID_SOCKET) { /* socket NOT initialized? */
2020-04-02 21:21:11 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR %s\n",
"invalid TCP socket");
2018-11-22 11:35:21 -05:00
return;
}
2018-10-25 11:11:36 -04:00
2018-11-22 11:35:21 -05:00
nBytes = QS_TX_CHUNK;
2020-10-01 12:48:48 -04:00
QS_CRIT_E_();
2018-11-22 11:35:21 -05:00
if ((data = QS_getBlock(&nBytes)) != (uint8_t *)0) {
2020-10-01 12:48:48 -04:00
QS_CRIT_X_();
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? */
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK) {
2019-06-19 11:21:18 -04:00
/* sleep for the timeout and then loop back
2018-11-29 13:17:28 -05:00
* to send() the SAME data again
*/
2019-06-19 11:21:18 -04:00
Sleep(QS_TIMEOUT_MS);
2018-11-29 13:17:28 -05:00
}
else { /* some other socket error... */
2020-04-02 21:21:11 -04:00
FPRINTF_S(stderr, "<TARGET> ERROR sending data over TCP,"
2018-11-29 13:17:28 -05:00
"WASErr=%d\n", err);
return;
}
}
else if (nSent < (int)nBytes) { /* sent fewer than requested? */
2019-06-19 11:21:18 -04:00
Sleep(QS_TIMEOUT_MS); /* sleep for the timeout */
2018-11-29 13:17:28 -05:00
/* adjust the data and loop back to send() the rest */
data += nSent;
nBytes -= (uint16_t)nSent;
}
else {
break;
}
2018-11-22 11:35:21 -05:00
}
}
2019-06-19 11:21:18 -04:00
else {
2020-10-01 12:48:48 -04:00
QS_CRIT_X_();
2019-06-19 11:21:18 -04:00
}
2018-11-22 11:35:21 -05:00
}
2018-10-25 11:11:36 -04:00
/*..........................................................................*/
void QS_rx_input(void) {
2021-01-14 13:11:17 -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:11:36 -04:00
}
}