added packet ids and id based tracking

This commit is contained in:
Morgan 'ARR\!' Allen 2020-04-24 21:06:56 -07:00
parent f1292d1605
commit d9051d628e
4 changed files with 100 additions and 57 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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]);

View file

@ -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;