forked from espressif/esp-iot-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.c
148 lines (139 loc) · 5.11 KB
/
wifi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "iot_wifi_conn.h"
#include "nvs_flash.h"
#define IOT_CHECK(tag, a, ret) if(!(a)) { \
ESP_LOGE(tag,"%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
return (ret); \
}
#define ERR_ASSERT(tag, param) IOT_CHECK(tag, (param) == ESP_OK, ESP_FAIL)
#define POINT_ASSERT(tag, param) IOT_CHECK(tag, (param) != NULL, ESP_FAIL)
#define RES_ASSERT(tag, res, ret) IOT_CHECK(tag, (res) != pdFALSE, ret)
// Debug tag in esp log
static const char* TAG = "wifi";
// Create an event group to handle different WiFi events.
static EventGroupHandle_t s_wifi_event_group = NULL;
// Mutex to protect WiFi connect
static xSemaphoreHandle s_wifi_mux = NULL;
//static bool wifi_sta_auto_reconnect = false;
static wifi_sta_status_t s_wifi_sta_st = WIFI_STATUS_STA_DISCONNECTED;
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
s_wifi_sta_st = WIFI_STATUS_STA_CONNECTING;
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START\n");
break;
case SYSTEM_EVENT_STA_GOT_IP:
s_wifi_sta_st = WIFI_STATUS_STA_CONNECTED;
ESP_LOGI(TAG, "SYSREM_EVENT_STA_GOT_IP\n");
// Set event bit to sync with other tasks.
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_EVT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
s_wifi_sta_st = WIFI_STATUS_STA_DISCONNECTED;
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED\n");
esp_wifi_connect();
// Clear event bit so WiFi task knows the disconnect-event
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_EVT);
break;
default:
printf("Get default WiFi event: %d\n", event->event_id);
break;
}
return ESP_OK;
}
esp_err_t iot_wifi_setup(wifi_mode_t wifi_mode)
{
nvs_flash_init();
#if DEBUG_EN
esp_log_level_set(TAG, ESP_LOG_DEBUG);
#endif
if (s_wifi_mux == NULL) {
s_wifi_mux = xSemaphoreCreateMutex();
POINT_ASSERT(TAG, s_wifi_mux);
}
tcpip_adapter_init();
// hoop WiFi event handler
ERR_ASSERT(TAG, esp_event_loop_init(wifi_event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
// Init WiFi
ERR_ASSERT(TAG, esp_wifi_init(&cfg));
ERR_ASSERT(TAG, esp_wifi_set_storage(WIFI_STORAGE_RAM));
esp_wifi_set_mode(wifi_mode);
esp_wifi_start();
// Init event group
s_wifi_event_group = xEventGroupCreate();
POINT_ASSERT(TAG, s_wifi_event_group);
return ESP_OK;
}
void iot_wifi_disconnect()
{
esp_wifi_disconnect();
xEventGroupSetBits(s_wifi_event_group, WIFI_STOP_REQ_EVT);
}
esp_err_t iot_wifi_connect(const char *ssid, const char *pwd, uint32_t ticks_to_wait)
{
// Take mutex
BaseType_t res = xSemaphoreTake(s_wifi_mux, ticks_to_wait);
RES_ASSERT(TAG, res, ESP_ERR_TIMEOUT);
// Clear stop event bit
esp_wifi_disconnect();
xEventGroupClearBits(s_wifi_event_group, WIFI_STOP_REQ_EVT | WIFI_CONNECTED_EVT);
wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config_t));
// Connect router
strcpy((char * )wifi_config.sta.ssid, ssid);
strcpy((char * )wifi_config.sta.password, pwd);
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_start();
esp_wifi_connect();
// Wait event bits
EventBits_t uxBits;
uxBits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_EVT | WIFI_STOP_REQ_EVT, false, false, ticks_to_wait);
esp_err_t ret;
// WiFi connected event
if (uxBits & WIFI_CONNECTED_EVT) {
ESP_LOGI(TAG, "WiFi connected");
ret = ESP_OK;
}
// WiFi stop connecting event
else if (uxBits & WIFI_STOP_REQ_EVT) {
ESP_LOGI(TAG, "WiFi connecting stop.");
// Clear stop event bit
xEventGroupClearBits(s_wifi_event_group, WIFI_STOP_REQ_EVT);
ret = ESP_FAIL;
}
// WiFi connect timeout
else {
iot_wifi_disconnect();
ESP_LOGW(TAG, "WiFi connect fail");
ret = ESP_ERR_TIMEOUT;
}
xSemaphoreGive(s_wifi_mux);
return ret;
}
wifi_sta_status_t iot_wifi_get_status()
{
return s_wifi_sta_st;
}