esp_netif_lora/main/esp_netif_lora.c

118 lines
3.4 KiB
C
Raw Permalink Normal View History

2023-12-30 00:08:35 -05:00
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "esp32-lora.h"
#include "esp_netif_lora.h"
#define TAG "esp_netif_lora"
//static uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
esp_netif_t* netif;
lora32_cfg_t lora = LORA32_DEFAULT_CONFIG();
void esp_netif_lora_io_free_rx_buffer(void *h, void *buffer);
static esp_err_t esp_netif_lora_io_transmit(void *h, void *buffer, size_t len);
static esp_err_t esp_netif_lora_io_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf);
static esp_err_t esp_netif_lora_io_attach(esp_netif_t * esp_netif, void * args);
const esp_netif_driver_ifconfig_t esp_netif_lora_ifconfig = {
.driver_free_rx_buffer = esp_netif_lora_io_free_rx_buffer,
.transmit = esp_netif_lora_io_transmit,
.transmit_wrap = esp_netif_lora_io_transmit_wrap,
.handle = "esp-netif-lora"
};
const esp_netif_driver_base_t esp_netif_lora_driver_base = {
.post_attach = esp_netif_lora_io_attach
};
void esp_netif_lora_io_free_rx_buffer(void *h, void *buffer) {
// TODO
}
static esp_err_t esp_netif_lora_io_transmit(void *h, void *buffer, size_t len) {
uint8_t *buf = buffer;
ESP_LOGI(TAG, "sending: %d",len);
for(uint8_t i = 0; i < len; i++)
printf("%02X ", buf[i]);
printf("\n");
vTaskDelay(1000/portTICK_PERIOD_MS);
lora32_send(&lora, (uint8_t*)buffer, len);
return ESP_OK;
}
static esp_err_t esp_netif_lora_io_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf) {
return esp_netif_lora_io_transmit(h, buffer, len);
}
static esp_err_t esp_netif_lora_io_attach(esp_netif_t * esp_netif, void * args) {
ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &esp_netif_lora_ifconfig));
return ESP_OK;
}
void * esp_netif_lora_new_handler(void) {
return (void*)&esp_netif_lora_driver_base;
}
static void esp_netif_lora_recv_cb(lora32_cfg_t *lora, uint8_t len) {
uint8_t *buf = (uint8_t*)malloc(len);
lora32_read_data(lora, buf);
esp_netif_receive(netif, (void*)buf, len, NULL);
//free(buf);
}
static void esp_netif_lora_txdn_cb(lora32_cfg_t *lora) {
ESP_LOGI(TAG, "tx_done");
lora32_enable_continuous_rx(lora);
}
esp_err_t esp_netif_lora_init(esp_netif_lora_config_t *netif_lora_config) {
esp_netif_inherent_config_t netif_common_config = {
.flags = ESP_NETIF_FLAG_AUTOUP,
.ip_info = &netif_lora_config->ip_info,
.if_key = "iflora",
.if_desc = "lora"
};
esp_netif_config_t config = {
.base = &netif_common_config, // use specific behaviour configuration
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, // use default WIFI-like network stack configuration
};
netif_lora_config->lora = &lora;
netif_lora_config->lora->receive = &esp_netif_lora_recv_cb;
netif_lora_config->lora->tx_done = &esp_netif_lora_txdn_cb;
lora32_spi_init(netif_lora_config->lora);
lora32_init(netif_lora_config->lora);
lora32_enable_continuous_rx(netif_lora_config->lora);
// Netif creation and configuration
//
ESP_ERROR_CHECK(esp_netif_init());
netif = esp_netif_new(&config);
esp_netif_attach(netif, esp_netif_lora_new_handler());
// Start the netif in a manual way, no need for events
//
esp_efuse_mac_get_default(&netif_lora_config->mac);
esp_netif_set_mac(netif, netif_lora_config->mac);
esp_netif_action_start(netif, NULL, 0, NULL);
return ESP_OK;
}