Skip to content
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
99 changes: 50 additions & 49 deletions linux/src/daemon_cl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,32 @@
#include <string.h>

#ifdef SYSTEMD_WATCHDOG
#include <watchdog.hpp>
#include "watchdog.hpp"
#endif

#define PHY_DELAY_GB_TX_I20 184 //1G delay
#define PHY_DELAY_GB_RX_I20 382 //1G delay
#define PHY_DELAY_MB_TX_I20 1044//100M delay
#define PHY_DELAY_MB_RX_I20 2133//100M delay

#define CLEANUP()\
do { \
delete pPort; \
delete pClock; \
delete ifname; \
delete timestamper; \
delete ipc; \
delete condition_factory; \
delete timer_factory; \
delete lock_factory; \
delete timerq_factory; \
delete default_factory; \
delete thread_factory; \
delete watchdog; \
GPTP_LOG_INFO("Clean up"); \
GPTP_LOG_UNREGISTER(); \
} while(0)

void gPTPPersistWriteCB(char *bufPtr, uint32_t bufSize);

void print_usage( char *arg0 ) {
Expand Down Expand Up @@ -100,34 +118,6 @@ void print_usage( char *arg0 ) {
);
}

int watchdog_setup(OSThreadFactory *thread_factory)
{
#ifdef SYSTEMD_WATCHDOG
SystemdWatchdogHandler *watchdog = new SystemdWatchdogHandler();
OSThread *watchdog_thread = thread_factory->createThread();
int watchdog_result;
long unsigned int watchdog_interval;
watchdog_interval = watchdog->getSystemdWatchdogInterval(&watchdog_result);
if (watchdog_result) {
GPTP_LOG_INFO("Watchtog interval read from service file: %lu us", watchdog_interval);
watchdog->update_interval = watchdog_interval / 2;
GPTP_LOG_STATUS("Starting watchdog handler (Update every: %lu us)", watchdog->update_interval);
watchdog_thread->start(watchdogUpdateThreadFunction, watchdog);
return 0;
} else if (watchdog_result < 0) {
GPTP_LOG_ERROR("Watchdog settings read error.");
delete watchdog;
return -1;
} else {
GPTP_LOG_STATUS("Watchdog disabled");
delete watchdog;
return 0;
}
#else
return 0;
#endif
}

static IEEE1588Clock *pClock = NULL;
static EtherPort *pPort = NULL;

Expand All @@ -136,7 +126,7 @@ int main(int argc, char **argv)
PortInit_t portInit;

sigset_t set;
InterfaceName *ifname;
InterfaceName *ifname = NULL;
int sig;

bool syntonize = false;
Expand All @@ -156,7 +146,6 @@ int main(int argc, char **argv)
memset(config_file_path, 0, 512);

GPTPPersist *pGPTPPersist = NULL;
LinuxThreadFactory *thread_factory = new LinuxThreadFactory();

// Block SIGUSR1
{
Expand All @@ -171,10 +160,22 @@ int main(int argc, char **argv)

GPTP_LOG_REGISTER();
GPTP_LOG_INFO("gPTP starting");
if (watchdog_setup(thread_factory) != 0) {

LinuxThreadFactory *thread_factory = new LinuxThreadFactory();

#ifdef SYSTEMD_WATCHDOG
SystemdWatchdogHandler *watchdog = new SystemdWatchdogHandler();
if (watchdog->watchdog_setup(thread_factory) != 0) {
GPTP_LOG_ERROR("Watchdog handler setup error");
delete watchdog;
delete thread_factory;
return -1;
}
#else
// dummy pointer for CLEANUP macro
int *watchdog = NULL;
#endif

phy_delay_map_t ether_phy_delay;
bool input_delay=false;

Expand All @@ -200,19 +201,26 @@ int main(int argc, char **argv)
portInit.neighborPropDelayThreshold =
CommonPort::NEIGHBOR_PROP_DELAY_THRESH;

LinuxNetworkInterfaceFactory *default_factory =
new LinuxNetworkInterfaceFactory;
LinuxNetworkInterfaceFactory *default_factory = new LinuxNetworkInterfaceFactory;
OSNetworkInterfaceFactory::registerFactory
(factory_name_t("default"), default_factory);
LinuxTimerQueueFactory *timerq_factory = new LinuxTimerQueueFactory();
LinuxLockFactory *lock_factory = new LinuxLockFactory();
LinuxTimerFactory *timer_factory = new LinuxTimerFactory();
LinuxConditionFactory *condition_factory = new LinuxConditionFactory();
LinuxSharedMemoryIPC *ipc = new LinuxSharedMemoryIPC();

#ifdef ARCH_INTELCE
EtherTimestamper *timestamper = new LinuxTimestamperIntelCE();
#else
EtherTimestamper *timestamper = new LinuxTimestamperGeneric();
#endif

/* Create Low level network interface object */
if( argc < 2 ) {
printf( "Interface name required\n" );
GPTP_LOG_ERROR( "Interface name required" );
print_usage( argv[0] );
CLEANUP();
return -1;
}
ifname = new InterfaceName( argv[1], strlen(argv[1]) );
Expand Down Expand Up @@ -258,7 +266,7 @@ int main(int argc, char **argv)
}
else if( strcmp(argv[i] + 1, "H") == 0 ) {
print_usage( argv[0] );
GPTP_LOG_UNREGISTER();
CLEANUP();
return 0;
}
else if( strcmp(argv[i] + 1, "R") == 0 ) {
Expand Down Expand Up @@ -286,7 +294,7 @@ int main(int argc, char **argv)
{
printf("Too many values\n");
print_usage( argv[0] );
GPTP_LOG_UNREGISTER();
CLEANUP();
return 0;
}
phy_delay[delay_count]=atoi(cli_inp_delay);
Expand All @@ -297,7 +305,7 @@ int main(int argc, char **argv)
{
printf("All four delay values must be specified\n");
print_usage( argv[0] );
GPTP_LOG_UNREGISTER();
CLEANUP();
return 0;
}
ether_phy_delay[LINKSPEED_1G].set_delay
Expand Down Expand Up @@ -365,11 +373,6 @@ int main(int argc, char **argv)
restoredataptr = (char *)restoredata;
}

#ifdef ARCH_INTELCE
EtherTimestamper *timestamper = new LinuxTimestamperIntelCE();
#else
EtherTimestamper *timestamper = new LinuxTimestamperGeneric();
#endif

sigemptyset(&set);
sigaddset(&set, SIGINT);
Expand All @@ -378,7 +381,7 @@ int main(int argc, char **argv)
sigaddset(&set, SIGUSR2);
if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
perror("pthread_sigmask()");
GPTP_LOG_UNREGISTER();
CLEANUP();
return -1;
}

Expand Down Expand Up @@ -448,7 +451,7 @@ int main(int argc, char **argv)

if (!pPort->init_port()) {
GPTP_LOG_ERROR("failed to initialize port");
GPTP_LOG_UNREGISTER();
CLEANUP();
return -1;
}

Expand Down Expand Up @@ -501,7 +504,7 @@ int main(int argc, char **argv)

if (sigwait(&set, &sig) != 0) {
perror("sigwait()");
GPTP_LOG_UNREGISTER();
CLEANUP();
return -1;
}

Expand Down Expand Up @@ -539,9 +542,7 @@ int main(int argc, char **argv)
pPort->joinLinkWatchThread(linkExitCode);
GPTP_LOG_INFO("All threads terminated");

if( ipc ) delete ipc;

GPTP_LOG_UNREGISTER();
CLEANUP();
return 0;
}

Expand Down
42 changes: 38 additions & 4 deletions linux/src/watchdog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "watchdog.hpp"
#include "avbts_osthread.hpp"
#include "gptp_log.hpp"
#include <systemd/sd-daemon.h>

Expand All @@ -11,17 +10,23 @@ OSThreadExitCode watchdogUpdateThreadFunction(void *arg)
return osthread_ok;
}

bool SystemdWatchdogHandler::startSystemdWatchdogThread(OSThreadFactory *thread_factory)
{
watchdog_thread = thread_factory->createThread();
return watchdog_thread->start(watchdogUpdateThreadFunction, this);
}

SystemdWatchdogHandler::SystemdWatchdogHandler()
{
GPTP_LOG_INFO("Creating Systemd watchdog handler.");
LinuxTimerFactory timer_factory = LinuxTimerFactory();
timer = timer_factory.createTimer();
watchdog_timer = timer_factory.createTimer();
}

SystemdWatchdogHandler::~SystemdWatchdogHandler()
{
//Do nothing
delete watchdog_timer;
delete watchdog_thread;
}

long unsigned int
Expand All @@ -39,8 +44,37 @@ void SystemdWatchdogHandler::run_update()
GPTP_LOG_DEBUG("NOTIFYING WATCHDOG.");
sd_notify(0, "WATCHDOG=1");
GPTP_LOG_DEBUG("GOING TO SLEEP %lld", update_interval);
timer->sleep(update_interval);
watchdog_timer->sleep(update_interval);
GPTP_LOG_DEBUG("WATCHDOG WAKE UP");
}
}

int SystemdWatchdogHandler::watchdog_setup(OSThreadFactory *thread_factory)
{
int watchdog_result;
long unsigned int watchdog_interval;

if (!thread_factory) {
GPTP_LOG_ERROR("Watchog setup invalid argument (thread_factory)");
return -1;
}

watchdog_interval = getSystemdWatchdogInterval(&watchdog_result);
if (watchdog_result) {
GPTP_LOG_INFO("Watchdog interval read from service file: %lu us", watchdog_interval);
update_interval = watchdog_interval / 2;
GPTP_LOG_STATUS("Starting watchdog handler (Update every: %lu us)", update_interval);
if (startSystemdWatchdogThread(thread_factory)) {
return 0;
} else {
GPTP_LOG_ERROR("Starting watchdog thread failed");
return -1;
}
} else if (watchdog_result < 0) {
GPTP_LOG_ERROR("Watchdog settings read error.");
return -1;
} else {
GPTP_LOG_STATUS("Watchdog disabled");
return 0;
}
}
11 changes: 7 additions & 4 deletions linux/src/watchdog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
#define SYSTEMDWATCHDOGHANDLER_H
#include <linux_hal_common.hpp>
#include <avbts_ostimer.hpp>

#include <avbts_osthread.hpp>

OSThreadExitCode watchdogUpdateThreadFunction(void *arg);

class SystemdWatchdogHandler
{
public:
long unsigned int update_interval;
long unsigned int getSystemdWatchdogInterval(int *result);
int watchdog_setup(OSThreadFactory *thread_factory);
void run_update();
SystemdWatchdogHandler();
virtual ~SystemdWatchdogHandler();
private:
OSTimer *timer;
OSTimer *watchdog_timer;
OSThread *watchdog_thread;
long unsigned int update_interval;
long unsigned int getSystemdWatchdogInterval(int *result);
bool startSystemdWatchdogThread(OSThreadFactory *thread_factory);
};

#endif // SYSTEMDWATCHDOGHANDLER_H