diff --git a/linux/src/daemon_cl.cpp b/linux/src/daemon_cl.cpp index 37c9e49..6b62631 100644 --- a/linux/src/daemon_cl.cpp +++ b/linux/src/daemon_cl.cpp @@ -58,7 +58,7 @@ #include #ifdef SYSTEMD_WATCHDOG -#include +#include "watchdog.hpp" #endif #define PHY_DELAY_GB_TX_I20 184 //1G delay @@ -66,6 +66,24 @@ #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 ) { @@ -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; @@ -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; @@ -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 { @@ -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; @@ -200,8 +201,7 @@ 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(); @@ -209,10 +209,18 @@ int main(int argc, char **argv) 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]) ); @@ -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 ) { @@ -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); @@ -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 @@ -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); @@ -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; } @@ -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; } @@ -501,7 +504,7 @@ int main(int argc, char **argv) if (sigwait(&set, &sig) != 0) { perror("sigwait()"); - GPTP_LOG_UNREGISTER(); + CLEANUP(); return -1; } @@ -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; } diff --git a/linux/src/watchdog.cpp b/linux/src/watchdog.cpp index 1406fb5..b1eb48a 100644 --- a/linux/src/watchdog.cpp +++ b/linux/src/watchdog.cpp @@ -1,5 +1,4 @@ #include "watchdog.hpp" -#include "avbts_osthread.hpp" #include "gptp_log.hpp" #include @@ -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 @@ -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; + } +} diff --git a/linux/src/watchdog.hpp b/linux/src/watchdog.hpp index b2e05cc..6676405 100644 --- a/linux/src/watchdog.hpp +++ b/linux/src/watchdog.hpp @@ -2,20 +2,23 @@ #define SYSTEMDWATCHDOGHANDLER_H #include #include - +#include 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