esp-idf/components/esp32/wifi.c

115 lines
2.9 KiB
C
Raw Normal View History

2016-08-17 11:08:22 -04:00
// 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.
2016-08-17 11:08:22 -04:00
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
2016-08-17 11:08:22 -04:00
#include "esp_err.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#if CONFIG_WIFI_ENABLED
static bool wifi_startup_flag = false;
2016-08-17 11:08:22 -04:00
static wifi_startup_cb_t startup_cb;
static void *startup_ctx;
2016-08-17 11:08:22 -04:00
#define WIFI_DEBUG(...)
2016-08-17 11:08:22 -04:00
#define WIFI_API_CALL_CHECK(info, api_call, ret) \
do{\
esp_err_t __err = (api_call);\
if ((ret) != __err) {\
WIFI_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
return __err;\
}\
} while(0)
2016-08-17 04:28:57 -04:00
2016-08-17 11:08:22 -04:00
static void esp_wifi_task(void *pvParameters)
{
esp_err_t err;
wifi_init_config_t cfg;
cfg.event_q = (xQueueHandle)esp_event_get_handler();
do {
err = esp_wifi_init(&cfg);
if (err != ESP_OK) {
2016-08-17 04:28:57 -04:00
WIFI_DEBUG("esp_wifi_init fail, ret=%d\n", err);
2016-08-17 11:08:22 -04:00
break;
}
if (startup_cb) {
err = (*startup_cb)(startup_ctx);
2016-08-17 11:08:22 -04:00
if (err != ESP_OK) {
2016-08-17 04:28:57 -04:00
WIFI_DEBUG("startup_cb fail, ret=%d\n", err);
2016-08-17 11:08:22 -04:00
break;
}
}
err = esp_wifi_start();
2016-08-17 04:28:57 -04:00
if (err != ESP_OK) {
WIFI_DEBUG("esp_wifi_start fail, ret=%d\n", err);
2016-08-17 11:08:22 -04:00
break;
}
#if CONFIG_WIFI_AUTO_CONNECT
wifi_mode_t mode;
2016-08-30 06:04:22 -04:00
bool auto_connect;
2016-08-17 04:28:57 -04:00
err = esp_wifi_get_mode(&mode);
if (err != ESP_OK) {
2016-08-17 04:28:57 -04:00
WIFI_DEBUG("esp_wifi_get_mode fail, ret=%d\n", err);
}
2016-08-17 11:08:22 -04:00
2016-08-30 06:04:22 -04:00
err = esp_wifi_get_auto_connect(&auto_connect);
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
2016-08-17 11:08:22 -04:00
err = esp_wifi_connect();
if (err != ESP_OK) {
2016-08-17 04:28:57 -04:00
WIFI_DEBUG("esp_wifi_connect fail, ret=%d\n", err);
2016-08-17 11:08:22 -04:00
break;
}
}
#endif
} while (0);
if (err != ESP_OK) {
2016-08-17 04:28:57 -04:00
WIFI_DEBUG("wifi startup fail, deinit\n");
2016-08-17 11:08:22 -04:00
esp_wifi_deinit();
}
vTaskDelete(NULL);
}
esp_err_t esp_wifi_startup(wifi_startup_cb_t cb, void *ctx)
2016-08-17 11:08:22 -04:00
{
if (wifi_startup_flag) {
return ESP_FAIL;
}
2016-08-17 11:08:22 -04:00
startup_cb = cb;
startup_ctx = ctx;
xTaskCreatePinnedToCore(esp_wifi_task, "wifiTask", 4096, NULL, configMAX_PRIORITIES-7, NULL, 0);
return ESP_OK;
2016-08-17 11:08:22 -04:00
}
#endif