From d9051d628e3b9d6ae026e617167da7dd5045bf66 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Fri, 24 Apr 2020 21:06:56 -0700 Subject: [PATCH] added packet ids and id based tracking --- include/lorcomm.h | 4 +- main/cmd_send.c | 2 +- main/lorcomm.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++- main/main.c | 54 +------------------------- 4 files changed, 100 insertions(+), 57 deletions(-) diff --git a/include/lorcomm.h b/include/lorcomm.h index 8beedf3..1867170 100644 --- a/include/lorcomm.h +++ b/include/lorcomm.h @@ -4,6 +4,7 @@ // disable memory alignment #pragma pack(push, 1) typedef struct { + uint8_t id; uint8_t src[3]; uint8_t dst[3]; uint8_t type; @@ -35,6 +36,7 @@ enum lora_msg_type { ROUTING, }; -void send_message(lora32_cfg_t *lora, char *text); +void lorcomm_handle_receive(uint8_t size); +void lorcomm_send_message(lora32_cfg_t *lora, char *text); #endif diff --git a/main/cmd_send.c b/main/cmd_send.c index b37bb35..e354233 100644 --- a/main/cmd_send.c +++ b/main/cmd_send.c @@ -23,7 +23,7 @@ int send(int argc, char **argv) { return 1; } - send_message((lora32_cfg_t*)send_args.lora, send_args.msg->sval[0]); + lorcomm_send_message((lora32_cfg_t*)send_args.lora, send_args.msg->sval[0]); return 0; } diff --git a/main/lorcomm.c b/main/lorcomm.c index 907fdb6..1b4a378 100644 --- a/main/lorcomm.c +++ b/main/lorcomm.c @@ -3,18 +3,110 @@ #include "esp_log.h" #include "esp32-lora.h" #include "lorcomm.h" +#include "main.h" #define TAG "lorcomm" -uint8_t mac[6] = {0}; +#define MSG_ID_TRACK (16) -void send_message(lora32_cfg_t *lora, char *text) { +uint8_t mac[6] = {0}; +static uint8_t msg_id = 0; +uint8_t msg_track[MSG_ID_TRACK] = { 0 }; + +void lorcomm_handle_receive(uint8_t size) { + printf("packet size: %d\n", size); + + uint8_t *buf = malloc(size); + lora_packet *packet = malloc(sizeof(lora_packet)); + + // get data from the lora driver + lora32_read_data(&lora, buf); + + // copy buffer onto message headers + memcpy(packet, buf, LORA_PACKET_HEADER_SIZE); + ESP_LOGI(TAG, "[%02X:%02X:%02X] id: %02d type: %02X", packet->src[0], packet->src[1], packet->src[2], packet->id, packet->type); + + for(uint8_t i = 0; i < MSG_ID_TRACK; i++) { + if(msg_track[i] == packet->id) { + ESP_LOGI(TAG, "already seen packet, not relaying"); + goto receive_cleanup; + } else if(msg_track[i] == 0) { + ESP_LOGI(TAG, "adding id to tracking"); + + // add id to array + msg_track[i] = packet->id; + // set next in queue to zero, rolling over as needed + msg_track[(i + 1) % MSG_ID_TRACK] = 0; + + break; + } + } + + // update local msg id to incoming +1 + // this will give a rolling local area id + if(packet->id < msg_id) { + msg_id = packet->id + 1; + + // 0 is an invalid id + if(msg_id == 0) msg_id = 1; + } + + if(memcmp(packet->src, &mac[3], 3) == 0) { + ESP_LOGI(TAG, "received own packet, done"); + goto receive_cleanup; + } + + if(packet->type == TEXT) { + lora_msg *msg = malloc(sizeof(lora_msg)); + memcpy(msg, buf + LORA_PACKET_HEADER_SIZE, LORA_MSG_HEADER_SIZE); + + // allocate memory for message payload + msg->msg = malloc(msg->length); + + memcpy(msg->msg, buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE, msg->length); + + ESP_LOGI(TAG, "msg (len: %d): %s", msg->length, msg->msg); + + free(msg->msg); + msg->msg = NULL; + free(msg); + } else if(packet->type == ROUTING) { + // future stuff + } else { + ESP_LOGW(TAG, "Unknown packet type: %02X", packet->type); + ESP_LOGW(TAG, "msg: %s", (char*)packet); + + goto receive_cleanup; + } + + vTaskDelay(3000/portTICK_PERIOD_MS); + ESP_LOGI(TAG, "relaying message"); + + lora32_send(&lora, buf, size); + +receive_cleanup: + free(buf); + + lora32_enable_continuous_rx(&lora); +} + +void lorcomm_send_message(lora32_cfg_t *lora, char *text) { + // allocate and zero out packet and msg payload uint8_t len = strlen(text) + 1; lora_packet packet = { 0 }; lora_msg msg = { 0 }; + // set packet tracking id + packet.id = msg_id; + + msg_id = msg_id + 1; + if(msg_id == 0) msg_id = 1; + + // set src and dest (broadcast) addresses memcpy(packet.src, &mac[3], 3); memset(packet.dst, 0xFF, 3); + + // indicate message payload packet.type = TEXT; packet.payload = malloc(LORA_PACKET_HEADER_SIZE + len); @@ -31,6 +123,7 @@ void send_message(lora32_cfg_t *lora, char *text) { memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE), (void*)&msg, LORA_MSG_HEADER_SIZE); memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE), (void*)msg.msg, len); + ESP_LOGI(TAG, "id: %02d", packet.id); ESP_LOGI(TAG, "src: [%02X:%02X:%02X]", packet.src[0], packet.src[1], packet.src[2]); ESP_LOGI(TAG, "dst: [%02X:%02X:%02X]", packet.dst[0], packet.dst[1], packet.dst[2]); diff --git a/main/main.c b/main/main.c index 894dc9e..88535bb 100644 --- a/main/main.c +++ b/main/main.c @@ -36,58 +36,6 @@ void handle_lora_caddone(bool detected) { } } -static void handle_lora_receive(uint8_t size) { - printf("packet size: %d\n", size); - - uint8_t *buf = malloc(size); - lora_packet *packet = malloc(sizeof(lora_packet)); - - // get data from the lora driver - lora32_read_data(&lora, buf); - - // copy buffer onto message headers - memcpy(packet, buf, LORA_PACKET_HEADER_SIZE); - ESP_LOGI(TAG, "[%02X:%02X:%02X] type: %02X", packet->src[0], packet->src[1], packet->src[2], packet->type); - - if(memcmp(packet->src, &mac[3], 3) == 0) { - ESP_LOGI(TAG, "received own packet, done"); - goto receive_cleanup; - } - - if(packet->type == TEXT) { - lora_msg *msg = malloc(sizeof(lora_msg)); - memcpy(msg, buf + LORA_PACKET_HEADER_SIZE, LORA_MSG_HEADER_SIZE); - - // allocate memory for message payload - msg->msg = malloc(msg->length); - - memcpy(msg->msg, buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE, msg->length); - - ESP_LOGI(TAG, "msg (len: %d): %s", msg->length, msg->msg); - - free(msg->msg); - msg->msg = NULL; - free(msg); - } else if(packet->type == ROUTING) { - // future stuff - } else { - ESP_LOGW(TAG, "Unknown packet type: %02X", packet->type); - ESP_LOGW(TAG, "msg: %s", (char*)packet); - - goto receive_cleanup; - } - - vTaskDelay(3000/portTICK_PERIOD_MS); - ESP_LOGI(TAG, "relaying message"); - - lora32_send(&lora, buf, size); - -receive_cleanup: - free(buf); - - lora32_enable_continuous_rx(&lora); -} - void app_main(void) { esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { @@ -121,7 +69,7 @@ void app_main(void) { lora.reset = CONFIG_LORA32_RESET_PIN; lora.useCRC = false; lora.fifoIdx = 0; - lora.receive = &handle_lora_receive; + lora.receive = &lorcomm_handle_receive; lora.tx_done = &handle_lora_txdone; //lora.cad_done = &handle_lora_caddone; //lora.cad_detected = &handle_lora_caddetected;