diff --git a/components/esp_netif_lora b/components/esp_netif_lora index 990f338..a893586 160000 --- a/components/esp_netif_lora +++ b/components/esp_netif_lora @@ -1 +1 @@ -Subproject commit 990f338826b50c3bca9953dcea7411ac841cd1c1 +Subproject commit a893586025d4f8591de124b42a9ba529fd397a31 diff --git a/esp-idf b/esp-idf index 490691b..3b8741b 160000 --- a/esp-idf +++ b/esp-idf @@ -1 +1 @@ -Subproject commit 490691bc61802e28ddfc1997edc8df6c203c1537 +Subproject commit 3b8741b172dc951e18509698dee938304bcf1523 diff --git a/main/main.c b/main/main.c index 6ea4425..8db935c 100644 --- a/main/main.c +++ b/main/main.c @@ -1,14 +1,19 @@ -#include +#include "esp_random.h" #include "esp_log.h" #include "esp_mac.h" -#include "esp_wifi.h" - -#include "nvs_flash.h" +#include "socket.h" #include "esp_netif_lora.h" +#define TAG "netif" + void app_main() { - esp_netif_lora_config_t config; + static esp_netif_lora_config_t config; + + config.netif = NULL; + config.lora = NULL; + + ESP_LOGI(TAG, "config.lora: %p", (void*)config.lora); esp_efuse_mac_get_default((uint8_t*)&config.mac); @@ -16,6 +21,74 @@ void app_main() { esp_netif_set_ip4_addr(&config.ip_info.gw, 10, 10 , 0, 1); esp_netif_set_ip4_addr(&config.ip_info.netmask, 255, 255 , 255, 0); - esp_log_level_set("LoRa32", ESP_LOG_INFO); + //esp_log_level_set("spi_master", ESP_LOG_DEBUG); + //esp_log_level_set("LoRa32", ESP_LOG_DEBUG); + esp_netif_lora_init(&config); + + int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + struct sockaddr_in local_addr; + local_addr.sin_family = AF_INET; + //local_addr.sin_addr.s_addr = config.ip_info.ip.addr; + local_addr.sin_addr.s_addr = INADDR_ANY; + local_addr.sin_port = htons(666); + + //ESP_ERROR_CHECK(ret); + + struct timeval timeout; + + timeout.tv_sec = esp_random() % 4 + 1; + timeout.tv_usec = 0; + + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, NULL, 0); + + ip_mreq mreq; + mreq.imr_multiaddr.s_addr = inet_addr("224.0.1.187"); + mreq.imr_interface.s_addr = config.ip_info.ip.addr; + + setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); + + // TODO double check this is actually part of accepting multicast + // or if it was just binding to INADDR_ANY + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void*)1, 1); + + int ret = bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr)); + + ESP_LOGI(TAG, "looping"); + + uint8_t buf[200]; + uint8_t client_addr_len; + ssize_t recieved; + ssize_t sent; + + buf[0] = 0x06; + buf[1] = 0x06; + buf[2] = 0x06; + + struct sockaddr_in broadcast; + broadcast.sin_family = AF_INET; + broadcast.sin_addr.s_addr = inet_addr("224.0.1.187"); + broadcast.sin_port = htons(666); + + while(1) { + recieved = recvfrom(sock, &buf, 255, 0, (struct sockaddr *)&local_addr, (socklen_t *)&client_addr_len); + + if((int)recieved == -1) { + sent = sendto(sock, &buf, sizeof(buf), 0, (struct sockaddr *)&broadcast, sizeof(broadcast)); + + if(sent == -1) { + ESP_LOGI(TAG, "sendto errno: %d", errno); + } + } else { + ESP_LOGI(TAG, "buf[0] = %d", buf[0]); + + buf[0]++; + + sent = sendto(sock, &buf, sizeof(buf), 0, (struct sockaddr *)&broadcast, sizeof(broadcast)); + } + + vTaskDelay(10 / portTICK_PERIOD_MS); + } }