diff --git a/components/esp32-lora b/components/esp32-lora index c71ed05..5fd28a9 160000 --- a/components/esp32-lora +++ b/components/esp32-lora @@ -1 +1 @@ -Subproject commit c71ed05f8f189984e15c7885e9ca1f0b3b6f33cc +Subproject commit 5fd28a946f6e421b0cee629f3ebe434df124534e diff --git a/include/esp_netif_lora.h b/include/esp_netif_lora.h index 2575945..b3d8951 100644 --- a/include/esp_netif_lora.h +++ b/include/esp_netif_lora.h @@ -8,6 +8,7 @@ typedef struct esp_netif_lora_config_t { uint8_t mac[6]; esp_netif_ip_info_t ip_info; lora32_cfg_t *lora; + esp_netif_t *netif; } esp_netif_lora_config_t; esp_err_t esp_netif_lora_init(esp_netif_lora_config_t *netif_lora_config); diff --git a/main/esp_netif_lora.c b/main/esp_netif_lora.c index 06f79f8..20ad896 100644 --- a/main/esp_netif_lora.c +++ b/main/esp_netif_lora.c @@ -1,3 +1,4 @@ +#include "esp_heap_caps.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_netif.h" @@ -11,40 +12,27 @@ #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(); +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); -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); + ESP_LOGD(TAG, "sending: %d", (int)len); - for(uint8_t i = 0; i < len; i++) - printf("%02X ", buf[i]); - printf("\n"); + lora_netif_driver_t driver = h; - vTaskDelay(1000/portTICK_PERIOD_MS); - lora32_send(&lora, (uint8_t*)buffer, len); + lora32_send(driver->netif_lora_config->lora, (uint8_t*)buffer, len); return ESP_OK; } @@ -53,31 +41,66 @@ static esp_err_t esp_netif_lora_io_transmit_wrap(void *h, void *buffer, size_t l 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; } -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_t* netif; + esp_netif_inherent_config_t netif_common_config = { .flags = ESP_NETIF_FLAG_AUTOUP, .ip_info = &netif_lora_config->ip_info, @@ -90,26 +113,22 @@ esp_err_t esp_netif_lora_init(esp_netif_lora_config_t *netif_lora_config) { .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()); + 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(&netif_lora_config->mac); + 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);