summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/src/CAmWatchdog.cpp
blob: 4ac890fe16730f93af1dfc6f9bdc94e385a629bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012, BMW AG
 *
 * This file is part of GENIVI Project AudioManager.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
 * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
 *
 * \file CAmWatchdog.cpp
 * For further information see http://www.genivi.org/.
 *
 */

#include "CAmWatchdog.h"
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include "audiomanagerconfig.h"
#include "CAmDltWrapper.h"

#ifdef SYSTEMD_FOUND
    #include <systemd/sd-daemon.h>
#else
    #include "sd-daemon.h"
#endif

namespace am
{

CAmWatchdog::CAmWatchdog(CAmSocketHandler* CAmSocketHandler) :
        TimerCallback(this, &CAmWatchdog::timerCallback), //
        mpCAmSocketHandler(CAmSocketHandler), //
        mHandle(0) //
{
    assert(mpCAmSocketHandler);


    //first retrieve the timeout interval

    int watchdogTimeout = 0;

    char* wusec=getenv("WATCHDOG_USEC");
    if (wusec)
        watchdogTimeout=atoi(wusec);

    if (watchdogTimeout > 0)
    {
        timespec timeout;

        //calculate the half cycle as the right interval to trigger the watchdog.
        timeout.tv_sec =   (watchdogTimeout / 2) / 1000000;
        timeout.tv_nsec = ((watchdogTimeout / 2) % 1000000) * 1000;
        logInfo("CAmWatchdog::CAmWatchdog setting watchdog timeout:", watchdogTimeout, "us. Notification set to:",
                timeout.tv_sec, "sec and", timeout.tv_nsec, "ns");

        //add the timer here
        if (mpCAmSocketHandler->addTimer(timeout, &TimerCallback, mHandle, NULL))
        {
            logError("CAmWatchdog::CAmWatchdog failed to add timer");
            throw std::runtime_error("CAmWatchdog::CAmWatchdog failed to add timer");
        }
    }

    else
    {
        logInfo("CAmWatchdog::CAmWatchdog watchdog timeout was ", watchdogTimeout, " museconds, no watchdog active");
    }
}

CAmWatchdog::~CAmWatchdog()
{
    //remove the timer again.
    mpCAmSocketHandler->removeTimer(mHandle);
}

void CAmWatchdog::timerCallback(sh_timerHandle_t handle, void* userData)
{
    (void) userData;
    int error(sd_notify(0, "WATCHDOG=1"));
    if (error < 0)
    {
        logError("CAmWatchdog::timerCallback could not reset watchdog, error ", error);
        throw std::runtime_error("CAmWatchdog::timerCallback could not reset watchdog");
    }

    mpCAmSocketHandler->restartTimer(handle);
}

void CAmWatchdog::startWatchdog()
{
    int error(sd_notify(0, "READY=1"));
    if (error < 0)
    {
        logError("CAmWatchdog::startWatchdog could not start watchdog, error ", error);
        throw std::runtime_error("CAmWatchdog::startWatchdog could not start watchdog");
    }
    logInfo("READY=1 was sent to systemd");
}

}

/* namespace am */