#include "esp_heap_caps.h" #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" typedef struct lora_netif_driver { esp_netif_driver_base_t base; esp_netif_lora_config_t *netif_lora_config; } *lora_netif_driver_t; //static uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 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); 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) { ESP_LOGD(TAG, "sending: %d", (int)len); lora_netif_driver_t driver = h; lora32_send(driver->netif_lora_config->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 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(lora->handle.driver, (void*)buf, len, NULL); // why? //free(buf); } static void esp_netif_lora_txdn_cb(lora32_cfg_t *lora) { lora32_enable_continuous_rx(lora); } static esp_err_t esp_netif_lora_io_attach(esp_netif_t * esp_netif, void * args) { lora_netif_driver_t driver = args; driver->base.netif = esp_netif; lora32_cfg_t *lora = malloc(sizeof(lora32_cfg_t)); if(driver->netif_lora_config->lora == NULL) { ESP_LOGI(TAG, "Using default lora config"); lora32_cfg_t default_lora = LORA32_DEFAULT_CONFIG(); memcpy(lora, &default_lora, sizeof(lora32_cfg_t)); driver->netif_lora_config->lora = lora; } else { ESP_LOGD(TAG, "Using lora cfg: %p", driver->netif_lora_config->lora); lora = driver->netif_lora_config->lora; } lora->handle.driver = esp_netif; lora->receive = &esp_netif_lora_recv_cb; lora->tx_done = &esp_netif_lora_txdn_cb; lora32_spi_init(lora); lora32_init(lora); lora32_enable_continuous_rx(lora); 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 = driver }; ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &esp_netif_lora_ifconfig)); return ESP_OK; } esp_err_t esp_netif_lora_init(esp_netif_lora_config_t *netif_lora_config) { esp_netif_t* netif; 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 creation and configuration // ESP_ERROR_CHECK(esp_netif_init()); netif = esp_netif_new(&config); lora_netif_driver_t driver = malloc(sizeof(struct lora_netif_driver)); driver->base.post_attach = esp_netif_lora_io_attach; driver->netif_lora_config = netif_lora_config; esp_netif_attach(netif, driver); // Start the netif in a manual way, no need for events // esp_efuse_mac_get_default((uint8_t*)&netif_lora_config->mac); esp_netif_set_mac(netif, netif_lora_config->mac); esp_netif_action_start(netif, NULL, 0, NULL); return ESP_OK; }