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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * @file dxWatchdog.js
 * @module dxWatchdog
 * @description
 * Watchdog module based on the native watchdog C library.
 * This module provides a handle-based interface for watchdog operations,
 * including software watchdog and hardware watchdog support.
 *
 * It is designed for high-performance, multi-threaded environments, allowing different
 * watchdog instances to be operated in parallel safely.
 *
 * @usage
 * // 1. Import the module
 * import watchdog from 'dxWatchdog.js';
 *
 * // 2. Init the watchdog (single instance)
 * try {
 *   watchdog.init();
 * } catch (e) {
 *   logger.error("Failed to init watchdog:", e);
 * }
 *
 * try {
 *   // 3. Start the watchdog timer with a timeout of 5000ms
 *   const started = watchdog.start(5000);
 *   logger.info("Watchdog started:", started);
 *
 *   // 4. Check if watchdog is powered on
 *   const isPoweron = watchdog.isPoweron();
 *   logger.info("Watchdog is poweron:", isPoweron);
 *
 *   // 5. Restart the watchdog timer for channel 0
 *   const restarted = watchdog.restart(0);
 *   logger.info("Watchdog restarted:", restarted);
 *
 *   // 6. Enable channel 0
 *   const enabled = watchdog.enable(0, true);
 *   logger.info("Channel enabled:", enabled);
 *
 *   // 7. Stop the watchdog timer
 *   watchdog.stop();
 * } catch (e) {
 *   console.error("Watchdog operation failed:", e);
 * }
 *
 * // 8. Deinit the watchdog when done
 * watchdog.deinit();
 */
import { watchdog as nativeWatchdog } from './libvbar-m-dxwatchdog.so'
 
const watchdog = {}
 
/**
 * @readonly
 * @enum {number}
 * Watchdog type enumeration.
 */
watchdog.TYPE = Object.freeze({
    SOFTWARE: 1,  // Software watchdog
    HARDWARE: 2,  // Hardware watchdog
});
 
/**
 * Initializes a watchdog device and returns its handle.
 * @throws {Error} If the watchdog fails to init or if parameters are invalid.
 */
watchdog.init = function () {
    try {
        nativeWatchdog.init();
    } catch (e) {
        // The C layer throws an exception on failure. We catch it and re-throw a more user-friendly JS error.
        throw new Error(`watchdog.init: failed to init watchdog. Reason: ${e.message}`);
    }
}
 
/**
 * Deinits the watchdog device.
 * @returns {boolean} Always returns true on success.
 */
watchdog.deinit = function () {
    return nativeWatchdog.deinit()
}
 
/**
 * Checks if the watchdog device is powered on.
 * @returns {boolean} True if the device is powered on, false otherwise.
 */
watchdog.isPoweron = function () {
    return nativeWatchdog.isPoweron()
}
 
/**
 * Starts the watchdog timer.
 * @param {number} timeout_ms - The timeout in milliseconds. Must be greater than 0.
 * @returns {boolean} True on success, false on failure.
 * @throws {Error} If timeout_ms is invalid.
 */
watchdog.start = function (timeout_ms) {
    if (timeout_ms === undefined || timeout_ms === null) {
        throw new Error("watchdog.start: 'timeout_ms' parameter is required.")
    }
    if (timeout_ms <= 0) {
        throw new Error("watchdog.start: 'timeout_ms' must be greater than 0.")
    }
    return nativeWatchdog.start(timeout_ms)
}
 
/**
 * Stops the watchdog timer.
 */
watchdog.stop = function () {
    nativeWatchdog.stop()
}
 
/**
 * Enables or disables a specific channel of the watchdog.
 * @param {number} chan - The channel number to enable/disable.
 * @param {boolean} toset - True to enable the channel, false to disable it.
 * @returns {boolean} True on success, false on failure.
 */
watchdog.enable = function (chan = 0, toset = true) {
    if (chan === undefined || chan === null) {
        throw new Error("watchdog.enable: 'chan' parameter is required.")
    }
    return nativeWatchdog.enable(chan, toset)
}
 
/**
 * Restarts the watchdog timer for a specific channel.
 * @param {number} chan - The channel number to restart.
 * @returns {boolean} True on success, false on failure.
 */
watchdog.restart = function (chan = 0) {
    if (chan === undefined || chan === null) {
        throw new Error("watchdog.restart: 'chan' parameter is required.")
    }
    return nativeWatchdog.restart(chan)
}
 
export default watchdog;