Skip to content

Commit ce25bb9

Browse files
committed
Build with constparam
1 parent 91c3ae2 commit ce25bb9

File tree

10 files changed

+46
-64
lines changed

10 files changed

+46
-64
lines changed

lib/apm_csh

lib/csp

Submodule csp updated 181 files

src/hk_param_sniffer.c

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <stdio.h>
99
#include <time.h>
1010
#include <pthread.h>
11-
#include <param/param_queue.h>
1211
#include <param/param_server.h>
12+
#include <param/param_queue.h>
1313
#include <param/param_serializer.h>
1414
#include <mpack/mpack.h>
1515
#include <csp/csp.h>
@@ -20,6 +20,7 @@
2020
#include <apm/csh_api.h>
2121

2222
#include "param_sniffer.h"
23+
#include "hk_param_sniffer.h"
2324

2425
pthread_t hk_param_sniffer_thread;
2526
#define MAX_HKS 16
@@ -38,41 +39,16 @@ typedef struct timesync_nodes_s {
3839
} timesync_nodes_t;
3940
static timesync_nodes_t timesync_nodes = {0};
4041

41-
void hk_set_utcparam(unsigned int node, unsigned int paramid) {
42-
43-
// update existing
44-
for (int i = 0; i < timesync_nodes.count; i++) {
45-
if (timesync_nodes.node[i] == node) {
46-
timesync_nodes.node[i] = node;
47-
timesync_nodes.paramid[i] = paramid;
48-
printf("Updating HK UTC parameter from node %u\n", node);
49-
return;
50-
}
51-
}
52-
53-
if (timesync_nodes.count >= MAX_HKS) {
54-
printf("Error: Maximum number of HK nodes reached (%d). Cannot set new utcparam for node %u\n", MAX_HKS, node);
55-
return;
56-
}
57-
58-
timesync_nodes.node[timesync_nodes.count] = node;
59-
timesync_nodes.paramid[timesync_nodes.count++] = paramid;
60-
61-
printf("Adding HK UTC parameter from node %u\n", node);
62-
}
63-
6442
void hk_set_epoch(time_t epoch, uint16_t node, bool auto_sync) {
6543

6644
time_t current_epoch;
6745
time(&current_epoch);
68-
char current_time_str[32];
69-
strftime(current_time_str, sizeof(current_time_str), "%Y-%m-%d %H:%M:%S", gmtime(&current_epoch));
7046

71-
/* 1577833200: Jan 1st 2020 */
72-
if (epoch > current_epoch || epoch < 1577833200) {
47+
/* 1577836800: Jan 1st 2020 */
48+
if (epoch > current_epoch || epoch < 1577836800) {
7349
char current_epoch_str[32];
7450
strftime(current_epoch_str, sizeof(current_epoch_str), "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
75-
printf("At %s: Illegal EPOCH %lu (%s) received\n", current_time_str, current_epoch, current_epoch_str);
51+
printf("HK: Illegal EPOCH %lu (%s) received\n", current_epoch, current_epoch_str);
7652
return;
7753
}
7854

@@ -85,15 +61,15 @@ void hk_set_epoch(time_t epoch, uint16_t node, bool auto_sync) {
8561
strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
8662
char time_current[32];
8763
strftime(time_current, sizeof(time_current), "%Y-%m-%d %H:%M:%S", gmtime(&hks.local_epoch[i]));
88-
printf("At %s: Skipping possible invalid EPOCH %s, current EPOCH for HK node %u is %s\n", current_time_str, time, node, time_current);
64+
printf("HK: Skipping possible invalid EPOCH %s, current EPOCH for HK node %u is %s (%ld)\n", time, node, time_current, hks.local_epoch[i]);
8965
return;
9066
}
9167

9268
if (labs(hks.local_epoch[i] - epoch) > 1 || !auto_sync) {
9369
/* get unix time to string time */
9470
char time[32];
9571
strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
96-
printf("At %s: Updating HK node %u EPOCH by %ld sec to %s\n", current_time_str, node, hks.local_epoch[i] - epoch, time);
72+
printf("HK: Updating HK node %u EPOCH by %ld sec to %s (%ld)\n", node, hks.local_epoch[i] - epoch, time, epoch);
9773
}
9874

9975
hks.local_epoch[i] = epoch;
@@ -102,14 +78,16 @@ void hk_set_epoch(time_t epoch, uint16_t node, bool auto_sync) {
10278
}
10379

10480
if (hks.count >= MAX_HKS) {
105-
printf("At %s: Error: Maximum number of HK nodes reached (%d). Cannot set new epoch for node %u\n", current_time_str, MAX_HKS, node);
81+
printf("HK: Error: Maximum number of HK nodes reached (%d). Cannot set new epoch for node %u\n", MAX_HKS, node);
10682
return;
10783
}
10884

10985
hks.node[hks.count] = node;
11086
hks.local_epoch[hks.count++] = epoch;
11187

112-
printf("At %s: Setting new hk node %u EPOCH to %ld\n", current_time_str, node, epoch);
88+
char new_epoch_str[32];
89+
strftime(new_epoch_str, sizeof(new_epoch_str), "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
90+
printf("HK: Setting new hk node %u EPOCH to %s (%ld)\n", node, new_epoch_str, epoch);
11391
}
11492

11593
bool hk_get_epoch(time_t * local_epoch, uint16_t node) {
@@ -151,33 +129,37 @@ bool hk_param_sniffer(csp_packet_t * packet) {
151129
if (node == 0) {
152130
node = packet->id.src;
153131
}
154-
param_t * param = param_list_find_id(node, id);
132+
const param_t * param = param_list_find_id(node, id);
155133
if (param) {
156134
*param->timestamp = timestamp;
157135
if (param->timestamp->tv_sec == 0) {
158136
printf("HK: Param timestamp is missing for %u:%s, logging is aborted\n", *(param->node), param->name);
159137
break;
160138
}
161139

162-
time_t local_epoch = -1;
163-
for (int i = 0; i < timesync_nodes.count; i++) {
164-
if (timesync_nodes.node[i] == node && timesync_nodes.paramid[i] == param->id) {
165-
mpack_tag_t tag = mpack_peek_tag(&reader);
166-
local_epoch = tag.v.i - timestamp.tv_sec;
167-
hk_set_epoch(local_epoch, packet->id.src, true);
168-
break;
140+
/* Only use local epoch if not receiving a UTC timestamp. 1577836800: Jan 1st 2020 */
141+
if (param->timestamp->tv_sec < 1577836800) {
142+
time_t local_epoch = -1;
143+
for (int i = 0; i < timesync_nodes.count; i++) {
144+
if (timesync_nodes.node[i] == node && timesync_nodes.paramid[i] == param->id) {
145+
mpack_tag_t tag = mpack_peek_tag(&reader);
146+
local_epoch = tag.v.i - timestamp.tv_sec;
147+
hk_set_epoch(local_epoch, packet->id.src, true);
148+
break;
149+
}
169150
}
170-
}
171-
if (local_epoch == -1 && !hk_get_epoch(&local_epoch, packet->id.src)) {
172-
if(!epoch_notfound_warning) {
173-
printf("HK: No local epoch found for node %u, skipping\n", packet->id.src);
174-
epoch_notfound_warning = true;
151+
152+
if (local_epoch == -1 && !hk_get_epoch(&local_epoch, packet->id.src)) {
153+
if(!epoch_notfound_warning) {
154+
printf("HK: No local epoch found for node %u, skipping %u %u %u\n", packet->id.src, *param->node, param->id, param->timestamp->tv_sec);
155+
epoch_notfound_warning = true;
156+
}
157+
mpack_discard(&reader);
158+
continue;
175159
}
176-
mpack_discard(&reader);
177-
continue;
178-
}
179160

180-
param->timestamp->tv_sec += local_epoch;
161+
param->timestamp->tv_sec += local_epoch;
162+
}
181163
param_sniffer_log(NULL, &queue, param, offset, &reader, param->timestamp);
182164
} else {
183165
printf("HK: Found unknown param node %d id %d\n", node, id);

src/hk_param_sniffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <csp/csp.h>
1313

1414
bool hk_get_epoch(time_t* epoch, uint16_t node);
15-
void hk_set_epoch(time_t epoch, uint16_t node);
15+
void hk_set_epoch(time_t epoch, uint16_t node, bool auto_sync);
1616
/* returns true if the packet was found to be for housekeeping */
1717
bool hk_param_sniffer(csp_packet_t * packet);
1818

src/param_sniffer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <stdio.h>
99
#include <sys/time.h>
1010
#include <pthread.h>
11-
#include <param/param_queue.h>
1211
#include <param/param_server.h>
12+
#include <param/param_queue.h>
1313
#include <param/param_serializer.h>
1414
#include <mpack/mpack.h>
1515
#include <csp/csp.h>
1616
#include <csp/csp_hooks.h>
1717
#include <csp/csp_crc32.h>
1818

19+
#include "param_sniffer.h"
1920
#include "hk_param_sniffer.h"
2021
#include "victoria_metrics.h"
2122
#include "vts.h"
@@ -27,9 +28,9 @@ int sniffer_running = 0;
2728
pthread_t param_sniffer_thread;
2829
FILE *logfile;
2930

30-
int param_sniffer_log(void * ctx, param_queue_t *queue, param_t *param, int offset, void *reader, csp_timestamp_t *timestamp) {
31+
int param_sniffer_log(void * ctx, param_queue_t *queue, const param_t *param, int offset, void *reader, csp_timestamp_t *timestamp) {
3132

32-
char tmp[1000] = {};
33+
char tmp[1000] = {0};
3334

3435
if (offset < 0)
3536
offset = 0;
@@ -136,7 +137,7 @@ int param_sniffer_crc(csp_packet_t * packet) {
136137
return 0;
137138
}
138139

139-
static void * param_sniffer(void * param) {
140+
static void * param_sniffer(void * arg) {
140141
csp_promisc_enable(100);
141142
while(1) {
142143
csp_packet_t * packet = csp_promisc_read(CSP_MAX_DELAY);
@@ -176,7 +177,6 @@ static void * param_sniffer(void * param) {
176177
csp_timestamp_t time_now;
177178
csp_clock_get_time(&time_now);
178179
queue.last_timestamp = time_now;
179-
queue.client_timestamp = time_now;
180180

181181
mpack_reader_t reader;
182182
mpack_reader_init_data(&reader, queue.buffer, queue.used);
@@ -192,7 +192,7 @@ static void * param_sniffer(void * param) {
192192
timestamp.tv_sec = packet->timestamp_rx;
193193
timestamp.tv_nsec = 0;
194194
}
195-
param_t * param = param_list_find_id(node, id);
195+
const param_t * param = param_list_find_id(node, id);
196196
if (param) {
197197
param_sniffer_log(NULL, &queue, param, offset, &reader, &timestamp);
198198
} else {

src/param_sniffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <csp/csp.h>
1212

1313
int param_sniffer_crc(csp_packet_t * packet);
14-
int param_sniffer_log(void * ctx, param_queue_t *queue, param_t *param, int offset, void *reader, csp_timestamp_t *timestamp);
14+
int param_sniffer_log(void * ctx, param_queue_t *queue, const param_t *param, int offset, void *reader, csp_timestamp_t *timestamp);
1515
void param_sniffer_init(int add_logfile);
1616

1717
#endif /* SRC_PARAM_SNIFFER_H_ */

src/python_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
static uint32_t _serial0;
2323

24-
PARAM_DEFINE_STATIC_RAM(PARAMID_SERIAL0, serial0, PARAM_TYPE_INT32, -1, 0, PM_HWREG, NULL, "", &_serial0, NULL);
24+
PARAM_DEFINE_STATIC_RAM(PARAMID_SERIAL0, serial0, PARAM_TYPE_XINT32, -1, 0, PM_HWREG, NULL, "", &_serial0, NULL);
2525
PARAM_DEFINE_STATIC_RAM(PARAMID_CSP_DBG_BUFFER_OUT, csp_buf_out, PARAM_TYPE_UINT8, 0, 0, PM_DEBUG | PM_ERRCNT, NULL, "", &csp_dbg_buffer_out, "Number of buffer overruns");
2626
PARAM_DEFINE_STATIC_RAM(PARAMID_CSP_DBG_CONN_OUT, csp_conn_out, PARAM_TYPE_UINT8, 0, 0, PM_DEBUG | PM_ERRCNT, NULL, "", &csp_dbg_conn_out, "Number of connection overruns");
2727
PARAM_DEFINE_STATIC_RAM(PARAMID_CSP_DBG_CONN_OVF, csp_conn_ovf, PARAM_TYPE_UINT8, 0, 0, PM_DEBUG | PM_ERRCNT, NULL, "", &csp_dbg_conn_ovf, "Number of rx-queue overflows");

0 commit comments

Comments
 (0)