commit 871ef2c35fc9f34271eccdcfa6650247a31f174f Author: Morgan 'ARR\!' Allen Date: Tue Feb 25 21:31:55 2020 -0800 this is lorcomm diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5cce37d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(lora-bigbin) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..cf2c455 --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.c" + INCLUDE_DIRS ".") diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..46f823b --- /dev/null +++ b/main/main.c @@ -0,0 +1,190 @@ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "driver/uart.h" +#include "esp_log.h" +#include "esp_debug_helpers.h" + +#include "esp32-lora.h" + +static const char *TAG = "lora-bigbin"; + +// disable memory alignment +#pragma pack(push, 1) +typedef struct { + uint8_t src[3]; + uint8_t dst[3]; + uint8_t type; + void *payload; +} lora_packet; + +// this is wrong, why is working correctly? should be 7 +#define LORA_PACKET_HEADER_SIZE (8) + +typedef struct { + uint8_t length; + char *msg; +} lora_msg; + +#define LORA_MSG_HEADER_SIZE (1) + +typedef struct { + uint8_t origin[3]; + uint8_t type; // same as packet type + void *payload; +} lora_routing; + +#define LORA_ROUTING_HEADER_SIZE (4) + +#pragma pack(pop) + +enum lora_msg_type { + TEXT = 0, + ROUTING, +}; + +static lora32_cfg_t lora; + +uint8_t mac[6] = {0}; + +static void handle_lora_txdone() { + ESP_LOGI(TAG, "txdone: enabling single RX"); + + vTaskDelay(100/portTICK_PERIOD_MS); + lora32_enable_continuous_rx(&lora); +} + +void handle_lora_caddone(bool detected) { + //ESP_LOGI(TAG, "CAD done: detected? %s", detected ? "true" : "false"); + + if(detected) { + lora32_enable_cad(&lora); + } else { + ESP_LOGI(TAG, "txdone: enabling single RX"); + lora32_enable_continuous_rx(&lora); + } +} + +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) { + } else { + ESP_LOGW(TAG, "Unknown packet type: %02X", packet->type); + 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 send_message(lora32_cfg_t *lora, char *text) { + uint8_t len = strlen(text) + 1; + lora_packet packet = { 0 }; + lora_msg msg = { 0 }; + + memcpy(packet.src, &mac[3], 3); + memset(packet.dst, 0xFF, 3); + packet.type = TEXT; + packet.payload = malloc(LORA_PACKET_HEADER_SIZE + len); + + msg.length = len; + msg.msg = malloc(LORA_MSG_HEADER_SIZE + len); + memcpy(msg.msg, text, len); + + uint8_t buf_len = LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE + len; + uint8_t *send_buf = malloc(buf_len); + ESP_LOGI(TAG, "packet length: %d", buf_len); + + // set src, dst, length + memcpy(send_buf, (uint8_t*)&packet, LORA_PACKET_HEADER_SIZE); + 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, "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]); + + lora32_send(lora, send_buf, buf_len); + + free(packet.payload); + packet.payload = NULL; + + free(msg.msg); + msg.msg = NULL; + + free(send_buf); +} + +void app_main(void) { + esp_efuse_mac_get_default((uint8_t*)&mac); + + ESP_LOGI(TAG, "MAC: [%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + lora.bandwidth = bandwidths[F866]; + lora.codingRate = DEFAULT_CR; + lora.dio0 = CONFIG_LORA32_DIO0_PIN; + lora.implicitHeader = false; + lora.nss = CONFIG_LORA32_NSS_PIN; + lora.reset = CONFIG_LORA32_RESET_PIN; + lora.frequency = 866000000; + lora.preamble = DEFAULT_PREAMBLE; + lora.spreadingFactor = DEFAULT_SF; + lora.receive = NULL; + lora.useCRC = false; + lora.fifoIdx = 0; + lora.receive = &handle_lora_receive; + lora.tx_done = &handle_lora_txdone; + lora.cad_done = &handle_lora_caddone; + //lora.cad_detected = &handle_lora_caddetected; + + lora32_init(&lora); + + lora32_dump_regs(&lora); + + ESP_LOGI(TAG, "sizeof(lora_msg): %d", sizeof(lora_msg)); + + send_message(&lora, "an even, super duuper, very much, longer message. and some\0"); + + vTaskDelay(1000 / portTICK_PERIOD_MS); + + while(1) { + vTaskDelay(100 / portTICK_PERIOD_MS); + } +}