From abecaabe4919deaff21f36f7997db35bcf965f56 Mon Sep 17 00:00:00 2001 From: "ruben.otto" <ruben.otto@informatik.hs-fulda.de> Date: Fri, 14 Feb 2025 03:28:54 +0100 Subject: [PATCH] feat: send data to kernel if measurement got updated --- daemon/src/main.c | 75 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/daemon/src/main.c b/daemon/src/main.c index 416a0c6..dedd2e9 100644 --- a/daemon/src/main.c +++ b/daemon/src/main.c @@ -85,6 +85,9 @@ int main() { sigaction(SIGINT, &signal_action, NULL); + measurement_t measurement_cache[sizeof(uint8_t)]; + uint8_t measurement_cache_size = 0; + printf("Started daemon\n"); while (1) { @@ -98,15 +101,73 @@ int main() { printf("Received data from %d: ", measurement.id); print_measurement_data(&measurement); - data_t data = { - .sender_id = SENDER_ID, - .count = 1, - .measurements = &measurement, - }; + // -1 = insert & update + // 0 = nothing + // 1 = update + int action = -1; + for (int i = 0; i < measurement_cache_size; i++) { + if (measurement_cache[i].id != measurement.id) { + continue; + } + + if (measurement_cache[i].datatype == measurement.datatype) { + switch (measurement.datatype) { + case INT8: + action = measurement_cache[i].data.int8 != measurement.data.int8; + break; + case UINT8: + action = measurement_cache[i].data.uint8 != measurement.data.uint8; + break; + case INT16: + action = measurement_cache[i].data.int16 != measurement.data.int16; + break; + case UINT16: + action = measurement_cache[i].data.uint16 != measurement.data.uint16; + break; + case INT32: + action = measurement_cache[i].data.int32 != measurement.data.int32; + break; + case UINT32: + action = measurement_cache[i].data.uint32 != measurement.data.uint32; + break; + case INT64: + action = measurement_cache[i].data.int64 != measurement.data.int64; + break; + case UINT64: + action = measurement_cache[i].data.uint64 != measurement.data.uint64; + break; + case FLOAT32: + action = + measurement_cache[i].data.float32 != measurement.data.float32; + break; + case FLOAT64: + action = + measurement_cache[i].data.float64 != measurement.data.float64; + break; + } + } else { + action = -1; + } + + measurement_cache[i] = measurement; + } - if (send_kernel_message(&kernel_send_handle, &data) == -1) { - printf(" (failed to send to kernel)"); + if (action == -1) { + measurement_cache[measurement_cache_size] = measurement; + measurement_cache_size++; } + if (action != 0) { + data_t data = { + .sender_id = SENDER_ID, + .count = measurement_cache_size, + .measurements = measurement_cache, + }; + + if (send_kernel_message(&kernel_send_handle, &data) == -1) { + printf(" (failed to send to kernel)"); + } + } + printf("\n"); } -- GitLab