diff --git a/src/lora/encryption.c b/src/lora/encryption.c new file mode 100644 index 0000000..b723cc2 --- /dev/null +++ b/src/lora/encryption.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +const uint8_t XOR_KEY = 0x5A; // Einfacher, geheimer Schlüssel + +void xorCrypt(const char *input, char *output, uint8_t key) { + size_t len = strlen(input); + for (size_t i = 0; i < len; i++) { + output[i] = input[i] ^ key; + } + output[len] = '\0'; // Null-Terminierung für gültige C-Strings +} + +uint16_t computeCRC16(const char *data) { + uint16_t crc = 0xFFFF; + for (size_t i = 0; i < strlen(data); i++) { + crc ^= (uint8_t)data[i] << 8; + for (int j = 0; j < 8; j++) { + if (crc & 0x8000) + crc = (crc << 1) ^ 0x1021; + else + crc <<= 1; + } + } + return crc; +} \ No newline at end of file diff --git a/src/lora/synch.c b/src/lora/synch.c new file mode 100644 index 0000000..e024e2d --- /dev/null +++ b/src/lora/synch.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_RADIO_NODE DT_ALIAS(lora0) +#define LED0_NODE DT_ALIAS(led0) +#define SW0_NODE DT_ALIAS(sw0) +#define MAX_SAMPLES 10 + +// 🔧 Vorwärtsdeklarationen +void synchronite(struct k_work *work); +K_WORK_DEFINE(sync_work, synchronite); + +// 📟 Hardware +static const struct gpio_dt_spec led_green = GPIO_DT_SPEC_GET(LED0_NODE, gpios); +static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0}); +static struct gpio_callback button_cb_data; + +// 📊 Sync-Variablen +uint32_t offsets[MAX_SAMPLES]; +uint8_t offset_count = 0; +uint32_t sync_offset = 0; +uint32_t sync_offset_s = 0; +bool synchronized = false; + +uint32_t samples[MAX_SAMPLES]; + +const struct device *lora_dev = DEVICE_DT_GET(DEFAULT_RADIO_NODE); + +struct lora_modem_config config = { + .frequency = 868100000, + .bandwidth = BW_125_KHZ, + .datarate = SF_7, + .preamble_len = 8, + .coding_rate = CR_4_5, + .tx_power = 14, + .tx = false, +}; + +BUILD_ASSERT(DT_NODE_HAS_STATUS_OKAY(DEFAULT_RADIO_NODE), + "No default LoRa radio specified in DT"); + +// 📡 Callback +void lora_receive_cb(const struct device *dev, uint8_t *data, uint16_t size, + int16_t rssi, int8_t snr, void *user_data) +{ + if ((size >= 4) && (!synchronized)) { + uint32_t master_timer_value = sys_get_le32(data); //uint8_t master_timer_value = data[0] % 60; + uint32_t local_now = k_uptime_get(); + + printk("📡 Empfangen: %u ms\n", master_timer_value); + gpio_pin_toggle_dt(&led_green); + + if (offset_count < MAX_SAMPLES) { + samples[offset_count] = local_now + master_timer_value; + offset_count++; + } + + if (offset_count >= MAX_SAMPLES) { + uint32_t offsets = 0; + + printk("Intervall: %d: %d: %d\n",(MAX_SAMPLES/5), (MAX_SAMPLES*4/5), (MAX_SAMPLES * 3/5)); + for (int i = (MAX_SAMPLES/5); i < (MAX_SAMPLES*4/5); i++) { + offsets += samples[i]; + } + + sync_offset_s = offsets /( MAX_SAMPLES * 3/5); + + sync_offset_s -= local_now; + + synchronized = true; + + int ret = lora_recv_async(lora_dev, NULL, NULL); + + if (ret != 0){ + printk("Problem beim deaktivieren der Synchronsation: %d\n", ret); + } + + printk("✅ Synchronisiert! Offset: %u Sekunden\n", sync_offset_s); + gpio_pin_set_dt(&led_green, 1); + return; + } + } +} + +// 🕒 Hilfsfunktion: synchronisierter Timerwert +uint32_t get_synced_timer() { + if (!synchronized) return 0; + uint32_t now = k_uptime_get(); + return (sync_offset > now) ? (sync_offset - now) : 0; +} + +// 🔘 Button Interrupt +void button_pressed(const struct device *dev, struct gpio_callback *cb, + uint32_t pins) +{ + printk("🔘 Button gedrückt\n"); + k_work_submit(&sync_work); // startet synchronite() +} + + +// ⚙️ GPIO Initialisierung +int init_led_and_button(void) { + int ret; + + if (!gpio_is_ready_dt(&led_green)) return -1; + ret = gpio_pin_configure_dt(&led_green, GPIO_OUTPUT_INACTIVE); + if (ret < 0) return ret; + + if (!gpio_is_ready_dt(&button)) return -1; + ret = gpio_pin_configure_dt(&button, GPIO_INPUT); + if (ret < 0) return ret; + + ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); + if (ret < 0) return ret; + + gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); + gpio_add_callback(button.port, &button_cb_data); + + return 0; +} + +void synchronite(struct k_work *work){ + offset_count = 0; + synchronized = false; + memset(offsets, 0, sizeof(offsets)); + printk("Initilisierung abgeschlossen"); + + int ret = lora_config(lora_dev, &config); + + ret = lora_recv_async(lora_dev, lora_receive_cb, NULL); + printk("Empfang gestartet (ret = %d)\n", ret); +} + +// 🚀 Main +int init(void) { + printk("🔋 Starte LoRa-Empfänger\n"); + + if (init_led_and_button() != 0) { + printk("❌ GPIO-Initialisierung fehlgeschlagen\n"); + return -1; + } + + if (!device_is_ready(lora_dev)) { + printk("❌ LoRa nicht bereit\n"); + return -1; + } + + gpio_pin_set_dt(&led_green, 0); + + while (1) { + k_msleep(1000); + } +} \ No newline at end of file