Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic to skip ahead to next intact message after overflow #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions ports/posix-qv/qf_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// <www.state-machine.com/licensing>
// <[email protected]>
//============================================================================
//! @date Last updated on: 2024-06-11
//! @date Last updated on: 2024-07-19
//! @version Last updated for: @ref qpc_7_4_0
//!
//! @file
Expand Down Expand Up @@ -61,9 +61,47 @@ static struct timespec l_tick; // structure for the clock tick
static int_t l_tickPrio; // priority of the ticker thread

#define NSEC_PER_SEC 1000000000L
#define DEFAULT_TICKS_PER_SEC 100
#define DEFAULT_TICKS_PER_SEC 100L

//============================================================================
//----------------------------------------------------------------------------
#ifdef __APPLE__

#define TIMER_ABSTIME 0

// emulate clock_nanosleep() for CLOCK_MONOTONIC and TIMER_ABSTIME
static inline int clock_nanosleep(clockid_t clockid, int flags,
const struct timespec* t,
struct timespec* remain)
{
Q_UNUSED_PAR(clockid);
Q_UNUSED_PAR(flags);
Q_UNUSED_PAR(remain);

struct timespec ts_delta;
clock_gettime(CLOCK_MONOTONIC, &ts_delta);

ts_delta.tv_sec = t->tv_sec - ts_delta.tv_sec;
ts_delta.tv_nsec = t->tv_nsec - ts_delta.tv_nsec;
if (ts_delta.tv_sec < 0) {
ts_delta.tv_sec = 0;
ts_delta.tv_nsec = 0;
}
else if (ts_delta.tv_nsec < 0) {
if (ts_delta.tv_sec == 0) {
ts_delta.tv_sec = 0;
ts_delta.tv_nsec = 0;
}
else {
ts_delta.tv_sec = ts_delta.tv_sec - 1;
ts_delta.tv_nsec = ts_delta.tv_nsec + NSEC_PER_SEC;
}
}

return nanosleep(&ts_delta, NULL);
}
#endif

//----------------------------------------------------------------------------
static void *ticker_thread(void *arg); // prototype
static void *ticker_thread(void *arg) { // for pthread_create()
Q_UNUSED_PAR(arg);
Expand Down Expand Up @@ -91,7 +129,8 @@ static void *ticker_thread(void *arg) { // for pthread_create()
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
&next_tick, NULL) == 0) // success?
{
QF_onClockTick(); // must call QTIMEEVT_TICK_X()
// clock tick callback (must call QTIMEEVT_TICK_X())
QF_onClockTick();
}
}
return (void *)0; // return success
Expand Down Expand Up @@ -121,12 +160,12 @@ static int_t l_critSectNest; // critical section nesting up-down counter
//............................................................................
void QF_enterCriticalSection_(void) {
pthread_mutex_lock(&l_critSectMutex_);
Q_ASSERT_INCRIT(100, l_critSectNest == 0); // NO nesting of crit.sect!
Q_ASSERT_INCRIT(101, l_critSectNest == 0); // NO nesting of crit.sect!
++l_critSectNest;
}
//............................................................................
void QF_leaveCriticalSection_(void) {
Q_ASSERT_INCRIT(200, l_critSectNest == 1); // crit.sect. must ballace!
Q_ASSERT_INCRIT(102, l_critSectNest == 1); // crit.sect. must balance!
if ((--l_critSectNest) == 0) {
pthread_mutex_unlock(&l_critSectMutex_);
}
Expand Down Expand Up @@ -166,6 +205,7 @@ void QF_init(void) {

//............................................................................
int QF_run(void) {
l_isRunning = true; // QF is running

QF_onStartup(); // application-specific startup callback

Expand Down Expand Up @@ -218,7 +258,6 @@ int QF_run(void) {
QS_BEGIN_PRE_(QS_QF_RUN, 0U)
QS_END_PRE_()

l_isRunning = true; // QF is running
while (l_isRunning) {
Q_ASSERT_INCRIT(300, QPSet_verify_(&QF_readySet_, &QF_readySet_dis_));

Expand All @@ -233,8 +272,7 @@ int QF_run(void) {
QF_CRIT_EXIT();

QEvt const *e = QActive_get_(a);
// dispatch event (virtual call)
(*a->super.vptr->dispatch)(&a->super, e, a->prio);
QASM_DISPATCH(&a->super, e, a->prio); // dispatch to the HSM
QF_gc(e);

QF_CRIT_ENTRY();
Expand Down Expand Up @@ -329,7 +367,6 @@ int QF_consoleWaitForKey(void) {
#endif

// QActive functions =========================================================

void QActive_start_(QActive * const me, QPrioSpec const prioSpec,
QEvt const * * const qSto, uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const stkSize,
Expand All @@ -354,7 +391,6 @@ void QActive_start_(QActive * const me, QPrioSpec const prioSpec,
(*me->super.vptr->init)(&me->super, par, me->prio);
QS_FLUSH(); // flush the QS trace buffer to the host
}

//............................................................................
#ifdef QACTIVE_CAN_STOP
void QActive_stop(QActive * const me) {
Expand Down
8 changes: 4 additions & 4 deletions ports/posix-qv/qs_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// <www.state-machine.com>
// <[email protected]>
//============================================================================
//! @date Last updated on: 2024-06-11
//! @date Last updated on: 2024-07-18
//! @version Last updated for: @ref qpc_7_4_0
//!
//! @file
Expand Down Expand Up @@ -151,7 +151,7 @@ uint8_t QS_onStartup(void const *arg) {
if (fcntl(l_sock, F_SETFL, status | O_NONBLOCK) != 0) {
FPRINTF_S(stderr, "<TARGET> ERROR Failed to set non-blocking socket "
"errno=%d\n", errno);
QS_EXIT();
QF_stop(); // <== stop and exit the application
goto error;
}

Expand All @@ -177,7 +177,7 @@ void QS_onCleanup(void) {
close(l_sock);
l_sock = INVALID_SOCKET;
}
//PRINTF_S("<TARGET> Disconnected from QSPY\n");
//PRINTF_S("%s\n", "<TARGET> Disconnected from QSPY");
}
//............................................................................
void QS_onReset(void) {
Expand Down Expand Up @@ -232,7 +232,7 @@ void QS_onFlush(void) {
//............................................................................
QSTimeCtr QS_onGetTime(void) {
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC_RAW, &tspec);
clock_gettime(CLOCK_MONOTONIC, &tspec);

// convert to units of 0.1 microsecond
QSTimeCtr time = (QSTimeCtr)(tspec.tv_sec * 10000000 + tspec.tv_nsec / 100);
Expand Down
57 changes: 49 additions & 8 deletions ports/posix/qf_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// <www.state-machine.com/licensing>
// <[email protected]>
//============================================================================
//! @date Last updated on: 2024-06-11
//! @date Last updated on: 2024-07-18
//! @version Last updated for: @ref qpc_7_4_0
//!
//! @file
Expand Down Expand Up @@ -72,7 +72,46 @@ static void sigIntHandler(int dummy) {
exit(-1);
}

//----------------------------------------------------------------------------
#ifdef __APPLE__

#define TIMER_ABSTIME 0

// emulate clock_nanosleep() for CLOCK_MONOTONIC and TIMER_ABSTIME
static inline int clock_nanosleep(clockid_t clockid, int flags,
const struct timespec* t,
struct timespec* remain)
{
Q_UNUSED_PAR(clockid);
Q_UNUSED_PAR(flags);
Q_UNUSED_PAR(remain);

struct timespec ts_delta;
clock_gettime(CLOCK_MONOTONIC, &ts_delta);

ts_delta.tv_sec = t->tv_sec - ts_delta.tv_sec;
ts_delta.tv_nsec = t->tv_nsec - ts_delta.tv_nsec;
if (ts_delta.tv_sec < 0) {
ts_delta.tv_sec = 0;
ts_delta.tv_nsec = 0;
}
else if (ts_delta.tv_nsec < 0) {
if (ts_delta.tv_sec == 0) {
ts_delta.tv_sec = 0;
ts_delta.tv_nsec = 0;
}
else {
ts_delta.tv_sec = ts_delta.tv_sec - 1;
ts_delta.tv_nsec = ts_delta.tv_nsec + NSEC_PER_SEC;
}
}

return nanosleep(&ts_delta, NULL);
}
#endif

//============================================================================
// QF functions

// NOTE: initialize the critical section mutex as non-recursive,
// but check that nesting of critical sections never occurs
Expand All @@ -83,12 +122,12 @@ int_t QF_critSectNest_;
//............................................................................
void QF_enterCriticalSection_(void) {
pthread_mutex_lock(&QF_critSectMutex_);
Q_ASSERT_INCRIT(100, QF_critSectNest_ == 0); // NO nesting of crit.sect!
Q_ASSERT_INCRIT(101, QF_critSectNest_ == 0); // NO nesting of crit.sect!
++QF_critSectNest_;
}
//............................................................................
void QF_leaveCriticalSection_(void) {
Q_ASSERT_INCRIT(200, QF_critSectNest_ == 1); // crit.sect. must ballace!
Q_ASSERT_INCRIT(102, QF_critSectNest_ == 1); // crit.sect. must balance!
if ((--QF_critSectNest_) == 0) {
pthread_mutex_unlock(&QF_critSectMutex_);
}
Expand All @@ -112,7 +151,7 @@ void QF_init(void) {
}

l_tick.tv_sec = 0;
l_tick.tv_nsec = NSEC_PER_SEC / DEFAULT_TICKS_PER_SEC; // default tick
l_tick.tv_nsec = NSEC_PER_SEC / DEFAULT_TICKS_PER_SEC; // default rate
l_tickPrio = sched_get_priority_min(SCHED_FIFO); // default ticker prio

// install the SIGINT (Ctrl-C) signal handler
Expand Down Expand Up @@ -167,10 +206,12 @@ int QF_run(void) {
}

// sleep without drifting till next_time (absolute), see NOTE03
(void)clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
&next_tick, NULL);
// clock tick callback (must call QTIMEEVT_TICK_X() once)
QF_onClockTick();
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
&next_tick, NULL) == 0) // success?
{
// clock tick callback (must call QTIMEEVT_TICK_X() once)
QF_onClockTick();
}
}
}
else { // The provided system clock tick NOT configured
Expand Down
6 changes: 3 additions & 3 deletions ports/posix/qs_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// <www.state-machine.com>
// <[email protected]>
//============================================================================
//! @date Last updated on: 2024-06-11
//! @date Last updated on: 2024-07-18
//! @version Last updated for: @ref qpc_7_4_0
//!
//! @file
Expand Down Expand Up @@ -151,7 +151,7 @@ uint8_t QS_onStartup(void const *arg) {
if (fcntl(l_sock, F_SETFL, status | O_NONBLOCK) != 0) {
FPRINTF_S(stderr, "<TARGET> ERROR Failed to set non-blocking socket "
"errno=%d\n", errno);
QS_EXIT();
QF_stop(); // <== stop and exit the application
goto error;
}

Expand Down Expand Up @@ -232,7 +232,7 @@ void QS_onFlush(void) {
//............................................................................
QSTimeCtr QS_onGetTime(void) {
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC_RAW, &tspec);
clock_gettime(CLOCK_MONOTONIC, &tspec);

// convert to units of 0.1 microsecond
QSTimeCtr time = (QSTimeCtr)(tspec.tv_sec * 10000000 + tspec.tv_nsec / 100);
Expand Down
3 changes: 1 addition & 2 deletions ports/win32-qv/qf_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ int QF_run(void) {
QF_CRIT_EXIT();

QEvt const *e = QActive_get_(a);
// dispatch event (virtual call)
(*a->super.vptr->dispatch)(&a->super, e, a->prio);
QASM_DISPATCH(&a->super, e, a->prio); // dispatch to the HSM
QF_gc(e);

QF_CRIT_ENTRY();
Expand Down
13 changes: 12 additions & 1 deletion src/qs/qs.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,18 @@ void QS_endRec_(void) {
// overrun over the old data?
if (QS_priv_.used > end) {
QS_priv_.used = end; // the whole buffer is used
QS_priv_.tail = head; // shift the tail to the old data
for (;;) {
uint8_t discarded_b = buf[head];
++head;
if (head == end) {
head = 0U;
}
--QS_priv_.used;
if (discarded_b == QS_FRAME) {
break;
}
}
QS_priv_.tail = head; // shift the tail to start of oldest record
}
}

Expand Down