]> granicus.if.org Git - esp-idf/blob - examples/protocols/asio/chat_server/components/wifi_asio.cpp
asio: initial idf port of asio library without ssl
[esp-idf] / examples / protocols / asio / chat_server / components / wifi_asio.cpp
1 /* Common WiFi Init as STA for ASIO examples
2
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/task.h"
11 #include "freertos/event_groups.h"
12 #include "esp_system.h"
13 #include "esp_wifi.h"
14 #include "esp_event_loop.h"
15 #include "esp_log.h"
16 #include "nvs_flash.h"
17 #include "driver/uart.h"
18 #include "esp_console.h"
19 #include "esp_vfs_dev.h"
20
21 #include "lwip/err.h"
22 #include "lwip/sys.h"
23
24 #include <cstring>
25
26 /* The examples use simple WiFi configuration that you can set via
27    'make menuconfig'.
28
29    If you'd rather not, just change the below entries to strings with
30    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
31 */
32 #define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
33 #define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
34
35 /* FreeRTOS event group to signal when we are connected*/
36 static EventGroupHandle_t wifi_event_group;
37
38 /* The event group allows multiple bits for each event,
39    but we only care about one event - are we connected
40    to the AP with an IP? */
41 const int WIFI_CONNECTED_BIT = BIT0;
42
43 static const char *TAG = "asio example wifi init";
44
45 /**
46  *  Definition of ASIO main method, which is called after network initialized
47  */
48 void asio_main();
49
50 static esp_err_t event_handler(void *ctx, system_event_t *event)
51 {
52     switch(event->event_id) {
53     case SYSTEM_EVENT_STA_START:
54         esp_wifi_connect();
55         break;
56     case SYSTEM_EVENT_STA_GOT_IP:
57         ESP_LOGI(TAG, "got ip:%s",
58                  ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
59         xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
60         break;
61     case SYSTEM_EVENT_AP_STACONNECTED:
62         ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d",
63                  MAC2STR(event->event_info.sta_connected.mac),
64                  event->event_info.sta_connected.aid);
65         break;
66     case SYSTEM_EVENT_AP_STADISCONNECTED:
67         ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d",
68                  MAC2STR(event->event_info.sta_disconnected.mac),
69                  event->event_info.sta_disconnected.aid);
70         break;
71     case SYSTEM_EVENT_STA_DISCONNECTED:
72         esp_wifi_connect();
73         xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
74         break;
75     default:
76         break;
77     }
78     return ESP_OK;
79 }
80
81 void wifi_init_sta()
82 {
83     wifi_event_group = xEventGroupCreate();
84
85     tcpip_adapter_init();
86     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
87
88     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
89     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
90     wifi_config_t wifi_config;
91     // zero out the config struct to ensure defaults are setup
92     memset(&wifi_config, 0, sizeof(wifi_sta_config_t));
93     // only copy ssid&password from example config
94     strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID);
95     strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS);
96
97     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
98     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
99     ESP_ERROR_CHECK(esp_wifi_start() );
100
101     ESP_LOGI(TAG, "wifi_init_sta finished.");
102     ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
103              EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
104 }
105
106 extern "C" void app_main()
107 {
108     //Initialize NVS
109     esp_err_t ret = nvs_flash_init();
110     if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
111       ESP_ERROR_CHECK(nvs_flash_erase());
112       ret = nvs_flash_init();
113     }
114     ESP_ERROR_CHECK(ret);
115     
116     ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
117     wifi_init_sta();
118
119     // Initialize VFS & UART so we can use std::cout/cin
120     setvbuf(stdin, NULL, _IONBF, 0);
121     setvbuf(stdout, NULL, _IONBF, 0);
122     /* Install UART driver for interrupt-driven reads and writes */
123     ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM,
124             256, 0, 0, NULL, 0) );
125     /* Tell VFS to use UART driver */
126     esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
127     esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
128     /* Move the caret to the beginning of the next line on '\n' */
129     esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
130
131     // wait till we receive IP, so asio realated code can be started
132     xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY);
133
134     // network is ready, let's proceed with ASIO example
135     asio_main();
136 }