barback32/main/main.c

129 lines
3.1 KiB
C
Raw Normal View History

2020-02-26 00:31:55 -05:00
#include <stdio.h>
#include <string.h>
#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 "main.h"
#include "lorcomm.h"
2020-02-26 00:31:55 -05:00
#include "esp32-lora.h"
2020-02-26 10:11:57 -05:00
#include "console.h"
#include "ble.h"
2020-02-26 00:31:55 -05:00
static const char *TAG = "loracom-main";
2020-02-26 00:31:55 -05:00
lora32_cfg_t lora;
uint8_t mac[6];
2020-02-26 00:31:55 -05:00
static void handle_lora_txdone() {
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 {
2020-03-03 20:38:04 -05:00
ESP_LOGI(TAG, "candone (no channel activity): enabling continuous RX");
2020-02-26 00:31:55 -05:00
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) {
2020-03-03 20:38:04 -05:00
// future stuff
2020-02-26 00:31:55 -05:00
} else {
ESP_LOGW(TAG, "Unknown packet type: %02X", packet->type);
2020-03-03 20:42:07 -05:00
ESP_LOGW(TAG, "msg: %s", (char*)packet);
2020-03-03 20:38:04 -05:00
2020-02-26 00:31:55 -05:00
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_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 = B208;
//lora.codingRate = 5;
lora.frequency = 915000000;
//lora.spreadingFactor = 8;
//lora.preamble = DEFAULT_PREAMBLE;
2020-02-26 00:31:55 -05:00
lora.dio0 = CONFIG_LORA32_DIO0_PIN;
lora.implicitHeader = false;
lora.nss = CONFIG_LORA32_NSS_PIN;
lora.reset = CONFIG_LORA32_RESET_PIN;
lora.useCRC = false;
lora.fifoIdx = 0;
lora.receive = &handle_lora_receive;
lora.tx_done = &handle_lora_txdone;
//lora.cad_done = &handle_lora_caddone;
2020-02-26 00:31:55 -05:00
//lora.cad_detected = &handle_lora_caddetected;
lora32_init(&lora);
//double dr = lora32_calc_datarate(&lora);
//ESP_LOGI(TAG, "data rate: %fbps", dr);
lora32_dump_regs(&lora);
2020-02-26 10:11:57 -05:00
xTaskCreate(console_task, "console", 4048, &lora, tskIDLE_PRIORITY + 3, NULL);
2020-02-26 00:31:55 -05:00
//lora32_send(&lora, (uint8_t*)"hi\0", 3);
//ble_init();
lora32_enable_continuous_rx(&lora);
2020-02-29 18:43:15 -05:00
2020-02-26 00:31:55 -05:00
while(1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
2020-02-26 00:31:55 -05:00
}
}