diff --git a/include/lorcomm.h b/include/lorcomm.h index 1867170..3cf7f94 100644 --- a/include/lorcomm.h +++ b/include/lorcomm.h @@ -36,6 +36,8 @@ enum lora_msg_type { ROUTING, }; +extern uint8_t lormac[3]; + void lorcomm_handle_receive(uint8_t size); void lorcomm_send_message(lora32_cfg_t *lora, char *text); diff --git a/main/lorcomm.c b/main/lorcomm.c index 02b189f..48c5a55 100644 --- a/main/lorcomm.c +++ b/main/lorcomm.c @@ -9,9 +9,23 @@ #define MSG_ID_TRACK (16) +typedef struct { + uint8_t mac[3]; + uint8_t ids[MSG_ID_TRACK]; +} id_cache_t; + uint8_t mac[6] = {0}; static uint8_t msg_id = 1; -uint8_t msg_track[MSG_ID_TRACK] = { 0 }; + +id_cache_t id_cache[MSG_ID_TRACK] = { 0 }; + +uint8_t acmp(uint8_t a[], uint8_t b[], uint8_t len) { + for(uint8_t i = 0; i < len; i++) { + if(a[i] != b[i]) return 0; + } + + return 1; +} void lorcomm_handle_receive(uint8_t size) { printf("packet size: %d\n", size); @@ -27,21 +41,35 @@ void lorcomm_handle_receive(uint8_t 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"); + if(id_cache[i].mac[0]+id_cache[i].mac[1]+id_cache[i].mac[2] == 0) { + memcpy(id_cache[i].mac, &packet->src, 3); - // 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; + // this is assumed to be empty + id_cache[i].ids[0] = packet->id; + ESP_LOGI(TAG, "adding new tracking entity"); break; } + + for(uint8_t j = 0; j < MSG_ID_TRACK; j++) { + if(id_cache[i].ids[j] == packet->id) { + ESP_LOGI(TAG, "already seen packet, not relaying"); + goto receive_cleanup; + } else if(id_cache[i].ids[j] == 0) { + ESP_LOGI(TAG, "adding id to tracking"); + + // add id to array + id_cache[i].ids[i] = packet->id; + // set next in queue to zero, rolling over as needed + id_cache[i].ids[(i + 1) % MSG_ID_TRACK] = 0; + + goto break_outer; + } + } } +break_outer: + // update local msg id to incoming +1 // this will give a rolling local area id if(packet->id < msg_id) {