-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_logger.c
More file actions
90 lines (71 loc) · 2.43 KB
/
event_logger.c
File metadata and controls
90 lines (71 loc) · 2.43 KB
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
#include "event_logger.h"
#include "mockup_flash/flash_using_driver.h"
#include "fits_in_bits.h"
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
struct LocalEventLogs local_event_logs = {
.num_logs = LOCAL_EVENT_LOG_COUNT,
.tail = 0,
.logs = {{0, 0, 0, 0, 0}}
};
// TODO: log overflows
void handle_event_overflow() {
FLASH_push_event_logs_to_flash(&local_event_logs);
}
uint8_t build_and_add_event_log(
unsigned int rtc_datetime, // Date+Hr+Min+Sec
unsigned int current_mode,
unsigned int action,
unsigned int details,
unsigned int extra
) {
if ( !fits_in_bits(rtc_datetime, 22)
|| !fits_in_bits(current_mode, 4)
|| !fits_in_bits(action, 3)
|| !fits_in_bits(details, 24)
|| !fits_in_bits(extra, 11)
) {
// Gave too large a value somewhere :(
printf("Improper value\n");
return 1;
}
uint64_t current_log_index = local_event_logs.tail;
local_event_logs.logs[current_log_index].rtc_datetime = rtc_datetime,
local_event_logs.logs[current_log_index].current_mode = current_mode;
local_event_logs.logs[current_log_index].action = action;
local_event_logs.logs[current_log_index].details = details;
local_event_logs.logs[current_log_index].extra = extra;
current_log_index++;
if( current_log_index >= local_event_logs.num_logs ) {
local_event_logs.tail = 0;
handle_event_overflow();
} else {
local_event_logs.tail = current_log_index;
}
return 0;
}
/*
int main() {
init_flash_header();
fetch_flash_header();
for (int i = 0; i < 128; ++i) {
build_and_add_event_log(i, 0, 0, 0, i);
}
printf("Local Logs:\n");
union EventLog ev_log;
for(uint32_t i = 0; i < local_event_logs.buffer_size; ++i) {
ev_log = (union EventLog)local_event_logs.buffer[i];
printf("Local ev Log #%u - rtc_time: %u, extra: %u\n", i, ev_log.as_struct.rtc_datetime, ev_log.as_struct.extra );
}
puts("A");
uint64_t block_buff[32];
uint32_t block_addr;
enum LogType log_type = get_oldest_page(block_buff, &block_addr);
assert(log_type == EVENT);
union EventLog ev_logs[sizeof(block_buff)/sizeof(union EventLog)];
static_assert(sizeof(ev_logs) >= sizeof(block_buff), "Hmm");
memcpy(ev_logs, block_buff, sizeof(ev_logs));
}
*/