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

Support for RTEMS #85

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef CONSTANTS_H_
#define CONSTANTS_H_

#ifdef __rtems__
#include <ptpd/config.h>
#endif /* __rtems__ */

/**
*\file
* \brief Default values and constants used in ptpdv2
Expand Down
5 changes: 3 additions & 2 deletions src/dep/constants_dep.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
/* platform dependent */

#if !defined(linux) && !defined(__NetBSD__) && !defined(__FreeBSD__) && \
!defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNXNTO__)
!defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNXNTO__) && \
!defined(__rtems__)
#error PTPD hasn't been ported to this OS - should be possible \
if it's POSIX compatible, if you succeed, report it to ptpd-devel@sourceforge.net
#endif
Expand All @@ -34,7 +35,7 @@ if it's POSIX compatible, if you succeed, report it to ptpd-devel@sourceforge.ne
#define octet ether_addr_octet
#endif /* linux */

#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__sun) || defined(__QNXNTO__)
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__sun) || defined(__QNXNTO__) || defined(__rtems__)
# include <sys/types.h>
# include <sys/socket.h>
#ifdef HAVE_SYS_SOCKIO_H
Expand Down
17 changes: 17 additions & 0 deletions src/dep/datatypes_dep.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

#include "../ptp_primitives.h"

#ifdef __rtems__
#include <net/ethernet.h>
#endif /* __rtems__ */

#ifdef HAVE_KQUEUE
#include <sys/event.h>
#endif /* HAVE_KQUEUE */
/**
*\file
* \brief Implementation specific datatype
Expand Down Expand Up @@ -43,6 +50,16 @@ typedef struct {
int ifIndex;
} InterfaceInfo;

/**
* \brief Support for kqueue or select
*/
#ifdef HAVE_KQUEUE
#define PTPD_KQUEUE_EVENTS (4) /* 2 for PTP, 2 for PCAP */
typedef struct kevent PtpNetWaitEvents;
#else
typedef struct fd_set PtpNetWaitEvents;
#endif

/**
* \brief Struct describing network transport data
*/
Expand Down
6 changes: 4 additions & 2 deletions src/dep/eventtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ struct EventTimer {
Boolean (*isRunning) (EventTimer* timer);

/* implementation data */
#ifdef PTPD_PTIMERS
#ifdef HAVE_KQUEUE
int timerId;
#elif defined PTPD_PTIMERS
timer_t timerId;
#else
#else /* HAVE_KQUEUE */
int32_t itimerInterval;
int32_t itimerLeft;
#endif /* PTPD_PTIMERS */
Expand Down
184 changes: 184 additions & 0 deletions src/dep/eventtimer_kqueue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*-
* Copyright (c) 2020 Chris Johns,
*
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @file eventtimer_kqueue.c
* @date
*
* @brief EventTimer implementation using kqueue timers
*
* Needs support in the kqueue to pass timer events to here.
*/

#include "../ptpd.h"

static void eventTimerStart_kqueue(EventTimer *timer, double interval);
static void eventTimerStop_kqueue(EventTimer *timer);
static void eventTimerReset_kqueue(EventTimer *timer);
static void eventTimerShutdown_kqueue(EventTimer *timer);
static Boolean eventTimerIsRunning_kqueue(EventTimer *timer);
static Boolean eventTimerIsExpired_kqueue(EventTimer *timer);

static int timerInstance;

void
setupEventTimer(EventTimer *timer)
{

struct kevent evset;

if(timer == NULL) {
return;
}

memset(timer, 0, sizeof(EventTimer));

timer->start = eventTimerStart_kqueue;
timer->stop = eventTimerStop_kqueue;
timer->reset = eventTimerReset_kqueue;
timer->shutdown = eventTimerShutdown_kqueue;
timer->isExpired = eventTimerIsExpired_kqueue;
timer->isRunning = eventTimerIsRunning_kqueue;

timer->timerId = ++timerInstance;

EV_SET(&evset, timer->timerId, EVFILT_TIMER, EV_ADD | EV_DISABLE, 0, 0, timer);

if (kevent(ptpKqueueGet(), &evset, 1, NULL, 0, NULL) < 0)
PERROR("kevent timer add | disable");

DBGV("Created kqueue timer %s (%d)", timer->id, timer->timerId);
}

static void
eventTimerStart_kqueue(EventTimer *timer, double interval)
{
int kq = ptpKqueueGet();
if (kq >= 0) {
struct kevent evset;
intptr_t data = interval * 1000000;

EV_SET(&evset, timer->timerId,
EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_USECONDS, data, timer);

DBGV("Timer %s start requested at %ld us interval\n", timer->id, data);

if (kevent(kq, &evset, 1, NULL, 0, NULL) < 0) {
PERROR("kevent timer add | enable");
return;
}

DBG2("timerStart: Set timer %s to %f\n", timer->id, interval);

timer->expired = FALSE;
timer->running = TRUE;
}
}

static void
eventTimerStop_kqueue(EventTimer *timer)
{
int kq = ptpKqueueGet();
if (kq >= 0) {
struct kevent evset;

EV_SET(&evset, timer->timerId, EVFILT_TIMER, EV_ADD | EV_DISABLE, 0, 0, timer);

DBGV("Timer %s stop requested\n", timer->id);

if (kevent(kq, &evset, 1, NULL, 0, NULL) < 0)
PERROR("kevent timer add | disable");

timer->running = FALSE;

DBG2("timerStop: stopped timer %s\n", timer->id);
}
}

static void
eventTimerReset_kqueue(EventTimer *timer)
{
}

static void
eventTimerShutdown_kqueue(EventTimer *timer)
{
int kq = ptpKqueueGet();
if (kq >= 0) {
struct kevent evset;

EV_SET(&evset, timer->timerId, EVFILT_TIMER, EV_DELETE, 0, 0, timer);

if (kevent(kq, &evset, 1, NULL, 0, NULL) < 0)
PERROR("kevent timer disable");

timer->running = FALSE;

DBG2("timerShutdown: %s\n", timer->id);
}
}

static Boolean
eventTimerIsRunning_kqueue(EventTimer *timer)
{
DBG2("timerIsRunning: Timer %s %s running\n", timer->id,
timer->running ? "is" : "is not");

return timer->running;
}

static Boolean
eventTimerIsExpired_kqueue(EventTimer *timer)
{

Boolean ret;

ret = timer->expired;

DBG2("timerIsExpired: Timer %s %s expired\n", timer->id,
timer->expired ? "is" : "is not");

/* the five monkeys experiment */
if(ret) {
timer->expired = FALSE;
}

return ret;

}

void
startEventTimers(void)
{
DBG("initTimer\n");
}

void
shutdownEventTimers(void)
{
}
2 changes: 1 addition & 1 deletion src/dep/iniparser/dictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void dictionary_replace(dictionary * d, const char * search, const char * replac
do {
pos=found;

for (j=0; j < strlen(search); j++) {
for (j=0; j < (int) strlen(search); j++) {
*pos='\0';
pos++;
}
Expand Down
80 changes: 80 additions & 0 deletions src/dep/kqueue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*-
* Copyright (c) 2020 Chris Johns,
*
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @file kqueue.c
* @date
*
* @brief Kqueue support code
*
* The kqueue is shared between the networking and timer code and there
* no shared data in their interfaces so kqueue handle is held here.
*/

#include "../ptpd.h"

static int kq = -1;

int
ptpKqueueGet(void)
{
#ifdef HAVE_KQUEUE
if(kq < 0) {
kq = kqueue();
if (kq < 0)
PERROR("kqueue failed");
}
#endif
return kq;
}

int
ptpKqueueWait(struct timespec* ts, struct kevent* events, int nevents)
{
int ret;

if (kq < 0) {
PERROR("kqueue not initialised");
return -1;
}

ret = kevent(kq, NULL, 0, events, nevents, ts);

if (ret > 0) {
int e;
for (e = 0; e < ret; ++e) {
DBG2("kevent: %d: %2d: %s\n", e, events[e].ident,
events[e].filter == EVFILT_TIMER ? "timer" : "sock");
if (events[e].filter == EVFILT_TIMER) {
EventTimer* timer = events[e].udata;
timer->expired = TRUE;
}
}
}

return ret;
}