Skip to content

Commit 4a165cb

Browse files
committed
Move heartbeat functions into heartbeat.c
1 parent 81ee171 commit 4a165cb

File tree

4 files changed

+185
-56
lines changed

4 files changed

+185
-56
lines changed

src/apps.c

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "apps.h"
2222
#include "config.h"
2323
#include "process.h"
24+
#include "heartbeat.h"
2425
#include "log.h"
2526
#include "utils.h"
2627

@@ -59,76 +60,43 @@ void print_app(int i)
5960

6061
//------------------------------------------------------------------
6162

63+
// Wrapper functions for backward compatibility - delegate to heartbeat module
6264
void update_heartbeat_time(int i)
6365
{
64-
apps[i].last_heartbeat = time(NULL);
65-
LOGD("Heartbeat time updated for %s", apps[i].name);
66-
}
67-
68-
int find_pid(int pid)
69-
{
70-
for(int i = 0; i < app_state.app_count; i++)
71-
{
72-
if(0 < apps[i].pid && pid == apps[i].pid)
73-
{
74-
return i;
75-
}
76-
}
77-
78-
return -1;
66+
heartbeat_update_time(i);
7967
}
8068

8169
time_t get_heartbeat_time(int i)
8270
{
83-
time_t now = time(NULL);
84-
return now - apps[i].last_heartbeat;
71+
return heartbeat_get_elapsed_time(i);
8572
}
8673

8774
bool is_timeup(int i)
8875
{
89-
const Application_t *app = &apps[i];
90-
91-
if(!app->started)
92-
{
93-
return false; // App not running yet
94-
}
95-
96-
if(app->heartbeat_interval <= 0)
97-
{
98-
return false; // Heartbeat not expected for this app
99-
}
100-
101-
const time_t now = time(NULL);
102-
103-
if(now < app->last_heartbeat)
104-
{
105-
LOGW("Time anomaly detected for %s (system clock changed?)", app->name);
106-
update_heartbeat_time(i);
107-
return false; // Reset and give another interval
108-
}
109-
110-
const time_t first_heartbeat_threshold = (time_t)MAX(app->heartbeat_interval, app->heartbeat_delay); // delay is designed to be larger than interval
111-
const time_t regular_threshold = (time_t)app->heartbeat_interval;
112-
const time_t threshold = app->first_heartbeat ? regular_threshold : first_heartbeat_threshold;
113-
const time_t elapsed = now - app->last_heartbeat;
114-
115-
if(elapsed >= threshold)
116-
{
117-
LOGD("Heartbeat time up for %s", app->name);
118-
return true;
119-
}
120-
121-
return false;
76+
return heartbeat_is_timeout(i);
12277
}
12378

12479
void set_first_heartbeat(int i)
12580
{
126-
apps[i].first_heartbeat = true;
81+
heartbeat_set_first_received(i);
12782
}
12883

12984
bool get_first_heartbeat(int i)
13085
{
131-
return apps[i].first_heartbeat;
86+
return heartbeat_get_first_received(i);
87+
}
88+
89+
int find_pid(int pid)
90+
{
91+
for(int i = 0; i < app_state.app_count; i++)
92+
{
93+
if(0 < apps[i].pid && pid == apps[i].pid)
94+
{
95+
return i;
96+
}
97+
}
98+
99+
return -1;
132100
}
133101

134102
//------------------------------------------------------------------

src/heartbeat.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
@file heartbeat.c
3+
@brief Heartbeat Management Module
4+
5+
This module handles heartbeat timing logic including timeout calculations,
6+
timestamp updates, and first heartbeat tracking for the Process Watchdog
7+
application. It provides functions to manage heartbeat timing for individual
8+
application processes.
9+
10+
@date 2023-01-01
11+
@version 1.0
12+
@author by Eray Ozturk | [email protected]
13+
@url github.com/diffstorm
14+
@license GPL-3 License
15+
*/
16+
17+
#include "heartbeat.h"
18+
#include "apps.h" // For Application_t and access to application data
19+
#include "log.h"
20+
#include "utils.h"
21+
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <stdbool.h>
25+
#include <string.h>
26+
#include <time.h>
27+
28+
//------------------------------------------------------------------
29+
// External access to application data (from apps.c)
30+
//------------------------------------------------------------------
31+
32+
// These functions are implemented in apps.c to provide access to application data
33+
extern Application_t* apps_get_array(void);
34+
extern AppState_t* apps_get_state(void);
35+
36+
//------------------------------------------------------------------
37+
// Public interface functions
38+
//------------------------------------------------------------------
39+
40+
void heartbeat_update_time(int app_index)
41+
{
42+
Application_t* apps = apps_get_array();
43+
apps[app_index].last_heartbeat = time(NULL);
44+
LOGD("Heartbeat time updated for %s", apps[app_index].name);
45+
}
46+
47+
time_t heartbeat_get_elapsed_time(int app_index)
48+
{
49+
Application_t* apps = apps_get_array();
50+
time_t now = time(NULL);
51+
return now - apps[app_index].last_heartbeat;
52+
}
53+
54+
bool heartbeat_is_timeout(int app_index)
55+
{
56+
Application_t* apps = apps_get_array();
57+
const Application_t *app = &apps[app_index];
58+
59+
if(!app->started)
60+
{
61+
return false; // App not running yet
62+
}
63+
64+
if(app->heartbeat_interval <= 0)
65+
{
66+
return false; // Heartbeat not expected for this app
67+
}
68+
69+
const time_t now = time(NULL);
70+
71+
if(now < app->last_heartbeat)
72+
{
73+
LOGW("Time anomaly detected for %s (system clock changed?)", app->name);
74+
heartbeat_update_time(app_index);
75+
return false; // Reset and give another interval
76+
}
77+
78+
const time_t first_heartbeat_threshold = (time_t)MAX(app->heartbeat_interval, app->heartbeat_delay); // delay is designed to be larger than interval
79+
const time_t regular_threshold = (time_t)app->heartbeat_interval;
80+
const time_t threshold = app->first_heartbeat ? regular_threshold : first_heartbeat_threshold;
81+
const time_t elapsed = now - app->last_heartbeat;
82+
83+
if(elapsed >= threshold)
84+
{
85+
LOGD("Heartbeat time up for %s", app->name);
86+
return true;
87+
}
88+
89+
return false;
90+
}
91+
92+
void heartbeat_set_first_received(int app_index)
93+
{
94+
Application_t* apps = apps_get_array();
95+
apps[app_index].first_heartbeat = true;
96+
}
97+
98+
bool heartbeat_get_first_received(int app_index)
99+
{
100+
Application_t* apps = apps_get_array();
101+
return apps[app_index].first_heartbeat;
102+
}

src/heartbeat.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
@file heartbeat.h
3+
@brief Heartbeat Management Module
4+
5+
This module handles heartbeat timing logic including timeout calculations,
6+
timestamp updates, and first heartbeat tracking for the Process Watchdog
7+
application. It provides functions to manage heartbeat timing for individual
8+
application processes.
9+
10+
@date 2023-01-01
11+
@version 1.0
12+
@author by Eray Ozturk | [email protected]
13+
@url github.com/diffstorm
14+
@license GPL-3 License
15+
*/
16+
17+
#ifndef HEARTBEAT_H
18+
#define HEARTBEAT_H
19+
20+
#include <stdbool.h>
21+
#include <time.h>
22+
23+
/**
24+
@brief Updates the last heartbeat time for the specified application.
25+
26+
@param app_index Index of the application.
27+
*/
28+
void heartbeat_update_time(int app_index);
29+
30+
/**
31+
@brief Gets the elapsed time since the last heartbeat received from the specified application.
32+
33+
@param app_index Index of the application.
34+
@return Elapsed time in seconds.
35+
*/
36+
time_t heartbeat_get_elapsed_time(int app_index);
37+
38+
/**
39+
@brief Checks if it is time to expect a heartbeat from the specified application.
40+
41+
@param app_index Index of the application.
42+
@return true if it is time to expect a heartbeat, false otherwise.
43+
*/
44+
bool heartbeat_is_timeout(int app_index);
45+
46+
/**
47+
@brief Sets the flag indicating that the specified application has sent its first heartbeat.
48+
49+
@param app_index Index of the application.
50+
*/
51+
void heartbeat_set_first_received(int app_index);
52+
53+
/**
54+
@brief Gets the flag indicating whether the specified application has sent its first heartbeat.
55+
56+
@param app_index Index of the application.
57+
@return true if the application has sent its first heartbeat, false otherwise.
58+
*/
59+
bool heartbeat_get_first_received(int app_index);
60+
61+
#endif // HEARTBEAT_H

src/process.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ void process_start(int app_index)
118118
apps[app_index].first_heartbeat = false;
119119
apps[app_index].pid = pid;
120120
LOGI("Process %s started (PID %d): %s", apps[app_index].name, apps[app_index].pid, apps[app_index].cmd);
121-
// Note: update_heartbeat_time will be handled by heartbeat module in Phase 3
122-
// For now, we'll call it directly (it's still in apps.c)
121+
// Use heartbeat module to update heartbeat time
123122
extern void update_heartbeat_time(int i);
124123
update_heartbeat_time(app_index);
125124
}
@@ -257,8 +256,7 @@ void process_restart(int app_index)
257256
}
258257
else
259258
{
260-
// Note: update_heartbeat_time will be handled by heartbeat module in Phase 3
261-
// For now, we'll call it directly (it's still in apps.c)
259+
// Use heartbeat module to update heartbeat time
262260
extern void update_heartbeat_time(int i);
263261
update_heartbeat_time(app_index);
264262
LOGI("Process %s restarted successfully", apps[app_index].name);

0 commit comments

Comments
 (0)