335 lines
11 KiB
C
Raw Normal View History

2012-08-14 18:07:04 -04:00
/*****************************************************************************
2015-04-28 13:45:35 -04:00
* Product: DPP example, Windows
2015-09-04 12:08:22 -04:00
* Last updated for version 5.5.0
* Last updated on 2015-08-19
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2014-04-06 11:43:13 -04:00
* Copyright (C) Quantum Leaps, www.state-machine.com.
2012-08-14 18:07:04 -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
2012-08-14 18:07:04 -04:00
* (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:
2014-04-06 11:43:13 -04:00
* Web: www.state-machine.com
* Email: info@state-machine.com
2012-08-14 18:07:04 -04:00
*****************************************************************************/
2015-04-28 13:45:35 -04:00
#include "qpc.h"
2012-08-14 18:07:04 -04:00
#include "dpp.h"
#include "bsp.h"
#include <conio.h>
#include <stdio.h>
2013-09-23 14:34:35 -04:00
#include <stdlib.h>
2012-08-14 18:07:04 -04:00
Q_DEFINE_THIS_FILE
/* local variables ---------------------------------------------------------*/
2015-06-04 22:47:13 -04:00
static uint32_t l_rnd; /* random seed */
2012-08-14 18:07:04 -04:00
#ifdef Q_SPY
enum {
2015-09-04 12:08:22 -04:00
PHILO_STAT = QS_USER,
COMMAND_STAT
2012-08-14 18:07:04 -04:00
};
2013-09-23 14:34:35 -04:00
static uint8_t l_running;
2012-08-14 18:07:04 -04:00
static uint8_t const l_clock_tick = 0U;
#endif
/*..........................................................................*/
void QF_onStartup(void) {
2015-06-04 22:47:13 -04:00
QF_setTickRate(BSP_TICKS_PER_SEC); /* set the desired tick rate */
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void QF_onCleanup(void) {
2015-06-04 22:47:13 -04:00
printf("\nBye! Bye!\n");
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void QF_onClockTick(void) {
2015-06-04 22:47:13 -04:00
QF_TICK_X(0U, &l_clock_tick); /* perform the QF clock tick processing */
if (_kbhit()) { /* any key pressed? */
2012-08-14 18:07:04 -04:00
int ch = _getch();
2015-06-04 22:47:13 -04:00
if (ch == '\33') { /* see if the ESC key pressed */
2012-08-14 18:07:04 -04:00
QF_PUBLISH(Q_NEW(QEvt, TERMINATE_SIG), &l_clock_tick);
}
else if (ch == 'p') {
QF_PUBLISH(Q_NEW(QEvt, PAUSE_SIG), &l_clock_tick);
}
2015-04-28 13:45:35 -04:00
else if (ch == 's') {
QF_PUBLISH(Q_NEW(QEvt, SERVE_SIG), &l_clock_tick);
}
2012-08-14 18:07:04 -04:00
}
}
/*..........................................................................*/
2015-09-04 12:08:22 -04:00
void Q_onAssert(char const Q_ROM * const module, int loc) {
QS_ASSERTION(module, loc, (uint32_t)10000U); /* report assertion to QS */
fprintf(stderr, "Assertion failed in %s, line %d", module, loc);
2013-09-23 14:34:35 -04:00
exit(-1);
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
2015-09-04 12:08:22 -04:00
void BSP_init(int argc, char *argv[]) {
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
printf("Dining Philosophers Problem example"
2015-06-04 22:47:13 -04:00
"\nQP %s\n"
2015-04-28 13:45:35 -04:00
"Press 'p' to pause\n"
"Press 's' to serve\n"
2012-08-14 18:07:04 -04:00
"Press ESC to quit...\n",
2015-06-04 22:47:13 -04:00
QP_versionStr);
2012-08-14 18:07:04 -04:00
BSP_randomSeed(1234U);
2015-09-04 12:08:22 -04:00
Q_ALLEGE(QS_INIT((argc > 1) ? argv[1] : ""));
2015-06-04 22:47:13 -04:00
QS_OBJ_DICTIONARY(&l_clock_tick); /* must be called *after* QF_init() */
2012-08-14 18:07:04 -04:00
QS_USR_DICTIONARY(PHILO_STAT);
2015-09-04 12:08:22 -04:00
QS_USR_DICTIONARY(COMMAND_STAT);
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void BSP_terminate(int16_t result) {
(void)result;
2013-09-23 14:34:35 -04:00
#ifdef Q_SPY
2015-05-14 15:45:53 -04:00
l_running = (uint8_t)0; /* stop the QS output thread */
2013-09-23 14:34:35 -04:00
#endif
2015-05-14 15:45:53 -04:00
QF_stop(); /* stop the main "ticker thread" */
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void BSP_displayPhilStat(uint8_t n, char const *stat) {
printf("Philosopher %2d is %s\n", (int)n, stat);
2015-06-04 22:47:13 -04:00
QS_BEGIN(PHILO_STAT, AO_Philo[n]) /* application-specific record begin */
2015-05-14 15:45:53 -04:00
QS_U8(1, n); /* Philosopher number */
QS_STR(stat); /* Philosopher status */
2012-08-14 18:07:04 -04:00
QS_END()
}
/*..........................................................................*/
void BSP_displayPaused(uint8_t paused) {
printf("Paused is %s\n", paused ? "ON" : "OFF");
}
/*..........................................................................*/
uint32_t BSP_random(void) { /* a very cheap pseudo-random-number generator */
/* "Super-Duper" Linear Congruential Generator (LCG)
* LCG(2^32, 3*7*11*13*23, 0, seed)
*/
l_rnd = l_rnd * (3*7*11*13*23);
return l_rnd >> 8;
}
/*..........................................................................*/
void BSP_randomSeed(uint32_t seed) {
l_rnd = seed;
}
/*--------------------------------------------------------------------------*/
2015-06-04 22:47:13 -04:00
#ifdef Q_SPY /* define QS callbacks */
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#include <time.h>
#define WIN32_LEAN_AND_MEAN
2015-09-04 12:08:22 -04:00
#include <windows.h> /* Win32 API for multithreading */
#include <winsock2.h> /* for Windows network facilities */
/*
* In this demo, the QS software tracing output is sent out of the application
* through a TCP/IP socket. This requires the QSPY host application to
* be started first to open a server socket (qspy -t ...) to wait for the
* incoming TCP/IP connection from the DPP demo.
*
* In an embedded target, the QS software tracing output can be sent out
* using any method available, such as a UART. This would require changing
* the implementation of the functions in this section, but the rest of the
* application code does not "know" (and should not care) how the QS ouptut
* is actually performed. In other words, the rest of the application does NOT
* need to change in any way to produce QS output.
*/
2013-09-23 14:34:35 -04:00
2015-09-04 12:08:22 -04:00
static SOCKET l_sock = INVALID_SOCKET;
2012-08-14 18:07:04 -04:00
/*..........................................................................*/
static DWORD WINAPI idleThread(LPVOID par) {/* signature for CreateThread() */
(void)par;
2015-09-04 12:08:22 -04:00
while (l_sock != INVALID_SOCKET) {
uint16_t nBytes;
2012-08-14 18:07:04 -04:00
uint8_t const *block;
2015-09-04 12:08:22 -04:00
/* try to receive bytes from the QS socket... */
nBytes = QS_rxGetNfree();
if (nBytes > 0U) {
uint8_t buf[64];
int status;
if (nBytes > sizeof(buf)) {
nBytes = sizeof(buf);
}
status = recv(l_sock, (char *)buf, (int)nBytes, 0);
if (status != SOCKET_ERROR) {
uint16_t i;
nBytes = (uint16_t)status;
for (i = 0U; i < nBytes; ++i) {
QS_RX_PUT(buf[i]);
}
}
}
QS_rxParse(); /* parse all the received bytes */
nBytes = 1024U;
2012-08-14 18:07:04 -04:00
QF_CRIT_ENTRY(dummy);
block = QS_getBlock(&nBytes);
QF_CRIT_EXIT(dummy);
2015-09-04 12:08:22 -04:00
2012-08-14 18:07:04 -04:00
if (block != (uint8_t *)0) {
2015-09-04 12:08:22 -04:00
send(l_sock, (char const *)block, nBytes, 0);
2012-08-14 18:07:04 -04:00
}
2015-09-04 12:08:22 -04:00
Sleep(20); /* sleep for xx milliseconds */
2012-08-14 18:07:04 -04:00
}
2015-09-04 12:08:22 -04:00
return (DWORD)0; /* return success */
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
uint8_t QS_onStartup(void const *arg) {
2015-09-04 12:08:22 -04:00
static uint8_t qsBuf[1024]; /* buffer for QS output */
static uint8_t qsRxBuf[100]; /* buffer for QS receive channel */
static WSADATA wsaData;
char hostName[64];
char const *src;
char *dst;
USHORT port = 6601; /* default QSPY server port */
ULONG ioctl_opt = 1;
struct sockaddr_in sockAddr;
struct hostent *server;
2012-08-14 18:07:04 -04:00
QS_initBuf(qsBuf, sizeof(qsBuf));
2015-09-04 12:08:22 -04:00
QS_rxInitBuf(qsRxBuf, sizeof(qsRxBuf));
/* initialize Windows sockets */
if (WSAStartup(MAKEWORD(2,0), &wsaData) == SOCKET_ERROR) {
printf("Windows Sockets cannot be initialized.");
return (uint8_t)0;
}
src = (arg != (void const *)0)
? (char const *)arg
: "localhost";
dst = hostName;
while ((*src != '\0') && (*src != ':') && (dst < &hostName[sizeof(hostName)])) {
*dst++ = *src++;
}
*dst = '\0';
if (*src == ':') {
port = (USHORT)strtoul(src + 1, NULL, 10);
}
l_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* TCP socket */
if (l_sock == INVALID_SOCKET){
printf("Socket cannot be created; error 0x%08X\n",
WSAGetLastError());
return (uint8_t)0; /* failure */
}
server = gethostbyname(hostName);
if (server == NULL) {
printf("QSpy host name %s cannot be resolved; error 0x%08X\n",
hostName, WSAGetLastError());
return (uint8_t)0;
}
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
memcpy(&sockAddr.sin_addr, server->h_addr, server->h_length);
sockAddr.sin_port = htons(port);
if (connect(l_sock, (struct sockaddr *)&sockAddr, sizeof(sockAddr))
== SOCKET_ERROR)
{
printf("Cannot connect to the QSPY server; error 0x%08X\n",
WSAGetLastError());
QS_EXIT();
return (uint8_t)0; /* failure */
}
/* Set the socket to non-blocking mode. */
if (ioctlsocket(l_sock, FIONBIO, &ioctl_opt) == SOCKET_ERROR) {
printf("Socket configuration failed.\n"
"Windows socket error 0x%08X.",
WSAGetLastError());
QS_EXIT();
return (uint8_t)0; /* failure */
}
2012-08-14 18:07:04 -04:00
2015-05-14 15:45:53 -04:00
/* set up the QS filters... */
2015-04-28 13:45:35 -04:00
QS_FILTER_ON(QS_QEP_STATE_ENTRY);
QS_FILTER_ON(QS_QEP_STATE_EXIT);
QS_FILTER_ON(QS_QEP_STATE_INIT);
QS_FILTER_ON(QS_QEP_INIT_TRAN);
QS_FILTER_ON(QS_QEP_INTERN_TRAN);
QS_FILTER_ON(QS_QEP_TRAN);
QS_FILTER_ON(QS_QEP_IGNORED);
QS_FILTER_ON(QS_QEP_DISPATCH);
QS_FILTER_ON(QS_QEP_UNHANDLED);
2015-09-04 12:08:22 -04:00
QS_FILTER_ON(QS_QF_ACTIVE_POST_FIFO);
QS_FILTER_ON(QS_QF_ACTIVE_POST_LIFO);
QS_FILTER_ON(QS_QF_PUBLISH);
2015-04-28 13:45:35 -04:00
2015-09-04 12:08:22 -04:00
QS_FILTER_ON(PHILO_STAT);
QS_FILTER_ON(COMMAND_STAT);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/* return the status of creating the idle thread */
return (CreateThread(NULL, 1024, &idleThread, (void *)0, 0, NULL)
!= (HANDLE)0) ? (uint8_t)1 : (uint8_t)0;
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void QS_onCleanup(void) {
2015-09-04 12:08:22 -04:00
if (l_sock != INVALID_SOCKET) {
closesocket(l_sock);
l_sock = INVALID_SOCKET;
}
WSACleanup();
2012-08-14 18:07:04 -04:00
}
/*..........................................................................*/
void QS_onFlush(void) {
2015-09-04 12:08:22 -04:00
uint16_t nBytes = 1000;
uint8_t const *block;
while ((block = QS_getBlock(&nBytes)) != (uint8_t *)0) {
send(l_sock, (char const *)block, nBytes, 0);
nBytes = 1000;
2012-08-14 18:07:04 -04:00
}
}
/*..........................................................................*/
QSTimeCtr QS_onGetTime(void) {
return (QSTimeCtr)clock();
}
/*..........................................................................*/
2015-09-04 12:08:22 -04:00
/*! callback function to reset the target (to be implemented in the BSP) */
void QS_onReset(void) {
//TBD
}
/*..........................................................................*/
/*! callback function to execute a uesr command (to be implemented in BSP) */
void QS_onCommand(uint8_t cmdId, uint32_t param) {
(void)cmdId;
(void)param;
QS_BEGIN(COMMAND_STAT, (void *)0) /* application-specific record begin */
QS_U8(2, cmdId);
QS_U32(8, param);
QS_END()
if (cmdId == 10U) {
Q_ERROR();
}
2012-08-14 18:07:04 -04:00
}
2015-09-04 12:08:22 -04:00
2015-05-14 15:45:53 -04:00
#endif /* Q_SPY */
2012-08-14 18:07:04 -04:00
/*--------------------------------------------------------------------------*/