From 441a53c604f5ec2c89bc43561a336355f5ca2334 Mon Sep 17 00:00:00 2001 From: Krzysztof Budzynski Date: Mon, 16 Jan 2017 23:08:35 +0100 Subject: [PATCH] Moved examples to new folders / categories. Removed example numbers from example names * Original commit: espressif/esp-idf@382999b378dfeb1dc224c74e8830e64fa1ed0d61 --- examples/protocols/mdns/Makefile | 9 + examples/protocols/mdns/README.md | 5 + .../protocols/mdns/main/Kconfig.projbuild | 29 +++ examples/protocols/mdns/main/component.mk | 4 + .../protocols/mdns/main/mdns_example_main.c | 184 ++++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 examples/protocols/mdns/Makefile create mode 100644 examples/protocols/mdns/README.md create mode 100644 examples/protocols/mdns/main/Kconfig.projbuild create mode 100644 examples/protocols/mdns/main/component.mk create mode 100644 examples/protocols/mdns/main/mdns_example_main.c diff --git a/examples/protocols/mdns/Makefile b/examples/protocols/mdns/Makefile new file mode 100644 index 000000000..0353c51c0 --- /dev/null +++ b/examples/protocols/mdns/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := mdns-test + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/protocols/mdns/README.md b/examples/protocols/mdns/README.md new file mode 100644 index 000000000..1c298604b --- /dev/null +++ b/examples/protocols/mdns/README.md @@ -0,0 +1,5 @@ +# 30_mdns example + +Shows how to use mDNS to advertise lookup services and hosts + +See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/examples/protocols/mdns/main/Kconfig.projbuild b/examples/protocols/mdns/main/Kconfig.projbuild new file mode 100644 index 000000000..3122e0309 --- /dev/null +++ b/examples/protocols/mdns/main/Kconfig.projbuild @@ -0,0 +1,29 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "myssid" + help + WiFi password (WPA or WPA2) for the example to use. + + Can be left blank if the network has no security set. + +config MDNS_HOSTNAME + string "mDNS Hostname" + default "esp32-mdns" + help + mDNS Hostname for example to use + +config MDNS_INSTANCE + string "mDNS Instance Name" + default "ESP32 with mDNS" + help + mDNS Instance Name for example to use + +endmenu \ No newline at end of file diff --git a/examples/protocols/mdns/main/component.mk b/examples/protocols/mdns/main/component.mk new file mode 100644 index 000000000..a98f634ea --- /dev/null +++ b/examples/protocols/mdns/main/component.mk @@ -0,0 +1,4 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c new file mode 100644 index 000000000..d19fd1ae6 --- /dev/null +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -0,0 +1,184 @@ +/* MDNS-SD Query and advertise Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event_loop.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "mdns.h" + +/* The examples use simple WiFi configuration that you can set via + 'make menuconfig'. + + If you'd rather not, just change the below entries to strings with + the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" +*/ +#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID +#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD + +#define EXAMPLE_MDNS_HOSTNAME CONFIG_MDNS_HOSTNAME +#define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE + +/* FreeRTOS event group to signal when we are connected & ready to make a request */ +static EventGroupHandle_t wifi_event_group; + +/* The event group allows multiple bits for each event, + but we only care about one event - are we connected + to the AP with an IP? */ +const int CONNECTED_BIT = BIT0; + +static const char *TAG = "mdns-test"; + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + /* enable ipv6 */ + tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + /* This is a workaround as ESP32 WiFi libs don't currently + auto-reassociate. */ + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void initialise_wifi(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); + wifi_config_t wifi_config = { + .sta = { + .ssid = EXAMPLE_WIFI_SSID, + .password = EXAMPLE_WIFI_PASS, + }, + }; + ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_start() ); +} + +static void query_mdns_service(mdns_server_t * mdns, const char * service, const char * proto) +{ + if(!mdns) { + return; + } + uint32_t res; + if (!proto) { + ESP_LOGI(TAG, "Host Lookup: %s", service); + res = mdns_query(mdns, service, 0, 1000); + if (res) { + size_t i; + for(i=0; iaddr), IPV62STR(r->addrv6)); + } + } + mdns_result_free(mdns); + } else { + ESP_LOGI(TAG, " Not Found"); + } + } else { + ESP_LOGI(TAG, "Service Lookup: %s.%s ", service, proto); + res = mdns_query(mdns, service, proto, 1000); + if (res) { + size_t i; + for(i=0; ihost)?r->host:"", (r->instance)?r->instance:"", + IP2STR(&r->addr), IPV62STR(r->addrv6), + r->port, (r->txt)?r->txt:""); + } + } + mdns_result_free(mdns); + } + } +} + +static void mdns_task(void *pvParameters) +{ + mdns_server_t * mdns = NULL; + while(1) { + /* Wait for the callback to set the CONNECTED_BIT in the + event group. + */ + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, + false, true, portMAX_DELAY); + ESP_LOGI(TAG, "Connected to AP"); + + if (!mdns) { + esp_err_t err = mdns_init(TCPIP_ADAPTER_IF_STA, &mdns); + if (err) { + ESP_LOGE(TAG, "Failed starting MDNS: %u", err); + continue; + } + + ESP_ERROR_CHECK( mdns_set_hostname(mdns, EXAMPLE_MDNS_HOSTNAME) ); + ESP_ERROR_CHECK( mdns_set_instance(mdns, EXAMPLE_MDNS_INSTANCE) ); + + const char * arduTxtData[4] = { + "board=esp32", + "tcp_check=no", + "ssh_upload=no", + "auth_upload=no" + }; + + ESP_ERROR_CHECK( mdns_service_add(mdns, "_arduino", "_tcp", 3232) ); + ESP_ERROR_CHECK( mdns_service_txt_set(mdns, "_arduino", "_tcp", 4, arduTxtData) ); + ESP_ERROR_CHECK( mdns_service_add(mdns, "_http", "_tcp", 80) ); + ESP_ERROR_CHECK( mdns_service_instance_set(mdns, "_http", "_tcp", "ESP32 WebServer") ); + ESP_ERROR_CHECK( mdns_service_add(mdns, "_smb", "_tcp", 445) ); + } else { + query_mdns_service(mdns, "esp32", NULL); + query_mdns_service(mdns, "_arduino", "_tcp"); + query_mdns_service(mdns, "_http", "_tcp"); + query_mdns_service(mdns, "_printer", "_tcp"); + query_mdns_service(mdns, "_ipp", "_tcp"); + query_mdns_service(mdns, "_afpovertcp", "_tcp"); + query_mdns_service(mdns, "_smb", "_tcp"); + query_mdns_service(mdns, "_ftp", "_tcp"); + query_mdns_service(mdns, "_nfs", "_tcp"); + } + + ESP_LOGI(TAG, "Restarting in 10 seconds!"); + vTaskDelay(10000 / portTICK_PERIOD_MS); + ESP_LOGI(TAG, "Starting again!"); + } +} + +void app_main() +{ + nvs_flash_init(); + initialise_wifi(); + xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL); +}