Skip to content

Commit

Permalink
Merge pull request #41 from tonyp7/release/v2.0
Browse files Browse the repository at this point in the history
Release/v2.0
  • Loading branch information
tonyp7 authored May 11, 2019
2 parents 5e4c4f0 + dc66d77 commit 1a710da
Show file tree
Hide file tree
Showing 9 changed files with 678 additions and 320 deletions.
16 changes: 14 additions & 2 deletions wifi_manager/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
menu "Wifi Manager Configuration"

config WIFI_MANAGER_TASK_PRIORITY
int "RTOS Task Priority for the wifi_manager"
default 5
help
Tasks spawn by the manager will have a priority of WIFI_MANAGER_TASK_PRIORITY-1. For this particular reason, minimum recommended task priority is 2.

config WIFI_MANAGER_MAX_RETRY
int "Max Retry on failed connection"
default 2
help
Defines when a connection is lost/attempt to connect is made, how many retries should be made before giving up.

config DEFAULT_AP_SSID
string "Access Point SSID"
default "esp32"
Expand All @@ -20,13 +32,13 @@ config DEFAULT_AP_CHANNEL

config DEFAULT_AP_IP
string "Access Point IP Address"
default "192.168.1.1"
default "10.10.0.1"
help
This is used for the redirection to the captive portal. It is recommended to leave unchanged.

config DEFAULT_AP_GATEWAY
string "Access Point IP Gateway"
default "192.168.1.1"
default "10.10.0.1"
help
This is used for the redirection to the captive portal. It is recommended to leave unchanged.

Expand Down
19 changes: 16 additions & 3 deletions wifi_manager/main/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,27 @@ Contains the freeRTOS task for the DNS server that processes the requests.

static const char TAG[] = "dns_server";
static TaskHandle_t task_dns_server = NULL;
int socket_fd;

void dns_server_start() {
xTaskCreate(&dns_server, "dns_server", 3072, NULL, 5, &task_dns_server);
xTaskCreate(&dns_server, "dns_server", 3072, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_dns_server);
}

void dns_server_stop(){
if(task_dns_server){
vTaskDelete(task_dns_server);
close(socket_fd);
task_dns_server = NULL;
}

}



void dns_server(void *pvParameters) {


int socket_fd;

struct sockaddr_in sa, ra;

/* Set redirection DNS hijack to the access point IP */
Expand Down Expand Up @@ -108,6 +118,7 @@ void dns_server(void *pvParameters) {

/* Start loop to process DNS requests */
for(;;) {

memset(data, 0x00, sizeof(data)); /* reset buffer */
length = recvfrom(socket_fd, data, sizeof(data), 0, (struct sockaddr *)&client, &client_len); /* read udp request */

Expand Down Expand Up @@ -159,10 +170,12 @@ void dns_server(void *pvParameters) {
}
}

vTaskDelay( (TickType_t)10); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. DNS daemon should not be taxing on the system */

}
close(socket_fd);

vTaskDelete ( NULL );
}


Expand Down
1 change: 1 addition & 0 deletions wifi_manager/main/dns_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ typedef struct __attribute__((__packed__)) dns_answer_t{

void dns_server(void *pvParameters);
void dns_server_start();
void dns_server_stop();



Expand Down
173 changes: 88 additions & 85 deletions wifi_manager/main/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ function to process requests, decode URLs, serve files, etc. etc.
#include "wifi_manager.h"


EventGroupHandle_t http_server_event_group = NULL;
EventBits_t uxBits;
//EventGroupHandle_t http_server_event_group = NULL;
//EventBits_t uxBits;

static const char TAG[] = "http_server";
static TaskHandle_t task_http_server = NULL;


/* embedded binary data */
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
Expand All @@ -90,21 +92,14 @@ const static char http_redirect_hdr_end[] = "/\n\n";



void http_server_set_event_start(){
if(!http_server_event_group) http_server_event_group = xEventGroupCreate();
xEventGroupSetBits(http_server_event_group, HTTP_SERVER_START_BIT_0 );
void http_server_start(){
if(task_http_server == NULL){
xTaskCreate(&http_server, "http_server", 2048, NULL, WIFI_MANAGER_TASK_PRIORITY-1, &task_http_server);
}
}


void http_server(void *pvParameters) {

if(!http_server_event_group) http_server_event_group = xEventGroupCreate();

/* do not start the task until wifi_manager says it's safe to do so! */
ESP_LOGD(TAG, "waiting for start bit");
uxBits = xEventGroupWaitBits(http_server_event_group, HTTP_SERVER_START_BIT_0, pdFALSE, pdTRUE, portMAX_DELAY );
ESP_LOGD(TAG, "received start bit, starting server");

struct netconn *conn, *newconn;
err_t err;
conn = netconn_new(NETCONN_TCP);
Expand All @@ -117,10 +112,12 @@ void http_server(void *pvParameters) {
http_server_netconn_serve(newconn);
netconn_delete(newconn);
}
vTaskDelay( (TickType_t)10); /* allows the freeRTOS scheduler to take over if needed */
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
} while(err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);

vTaskDelete( NULL );
}


Expand Down Expand Up @@ -162,98 +159,104 @@ void http_server_netconn_serve(struct netconn *conn) {

if(line) {


/* captive portal functionality: redirect to the access point IP addresss */
/* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
int lenH = 0;
char *host = http_server_get_header(save_ptr, "Host: ", &lenH);
if ((sizeof(host) > 0) && !strstr(host, DEFAULT_AP_IP)) {
/* determine if Host is from the STA IP address */
wifi_manager_lock_sta_ip_string(portMAX_DELAY);
bool access_from_sta_ip = lenH > 0?strstr(host, wifi_manager_get_sta_ip_string()):false;
wifi_manager_unlock_sta_ip_string();

if (lenH > 0 && !strstr(host, DEFAULT_AP_IP) && !access_from_sta_ip) {
netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
netconn_write(conn, DEFAULT_AP_IP, sizeof(DEFAULT_AP_IP) - 1, NETCONN_NOCOPY);
netconn_write(conn, http_redirect_hdr_end, sizeof(http_redirect_hdr_end) - 1, NETCONN_NOCOPY);
}
/* default page */
else if(strstr(line, "GET / ")) {
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /jquery.js ")) {
netconn_write(conn, http_jquery_gz_hdr, sizeof(http_jquery_gz_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, jquery_gz_start, jquery_gz_end - jquery_gz_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /code.js ")) {
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /ap.json ")) {
/* if we can get the mutex, write the last version of the AP list */
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
char *buff = wifi_manager_get_ap_list_json();
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
wifi_manager_unlock_json_buffer();
else{
/* default page */
if(strstr(line, "GET / ")) {
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGD(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
else if(strstr(line, "GET /jquery.js ")) {
netconn_write(conn, http_jquery_gz_hdr, sizeof(http_jquery_gz_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, jquery_gz_start, jquery_gz_end - jquery_gz_start, NETCONN_NOCOPY);
}
/* request a wifi scan */
wifi_manager_scan_async();
}
else if(strstr(line, "GET /style.css ")) {
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /status.json ")){
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
char *buff = wifi_manager_get_ip_info_json();
if(buff){
else if(strstr(line, "GET /code.js ")) {
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /ap.json ")) {
/* if we can get the mutex, write the last version of the AP list */
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
char *buff = wifi_manager_get_ap_list_json();
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
wifi_manager_unlock_json_buffer();
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGD(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
}
/* request a wifi scan */
wifi_manager_scan_async();
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGD(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
else if(strstr(line, "GET /style.css ")) {
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
}
}
else if(strstr(line, "DELETE /connect.json ")) {
ESP_LOGD(TAG, "http_server_netconn_serve: DELETE /connect.json");
/* request a disconnection from wifi and forget about it */
wifi_manager_disconnect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
}
else if(strstr(line, "POST /connect.json ")) {
ESP_LOGD(TAG, "http_server_netconn_serve: POST /connect.json");

bool found = false;
int lenS = 0, lenP = 0;
char *ssid = NULL, *password = NULL;
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);

if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call");
wifi_manager_connect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
else if(strstr(line, "GET /status.json ")){
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
char *buff = wifi_manager_get_ip_info_json();
if(buff){
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
wifi_manager_unlock_json_buffer();
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
}
else{
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGD(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
}
}
else if(strstr(line, "DELETE /connect.json ")) {
ESP_LOGD(TAG, "http_server_netconn_serve: DELETE /connect.json");
/* request a disconnection from wifi and forget about it */
wifi_manager_disconnect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
}
else if(strstr(line, "POST /connect.json ")) {
ESP_LOGD(TAG, "http_server_netconn_serve: POST /connect.json");

bool found = false;
int lenS = 0, lenP = 0;
char *ssid = NULL, *password = NULL;
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);

if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call");
wifi_manager_connect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
}

if(!found){
/* bad request the authentification header is not complete/not the correct format */
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
}

if(!found){
/* bad request the authentification header is not complete/not the correct format */
}
else{
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
}

}
else{
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
}
}
else{
Expand Down
3 changes: 2 additions & 1 deletion wifi_manager/main/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ extern "C" {

void http_server(void *pvParameters);
void http_server_netconn_serve(struct netconn *conn);
void http_server_set_event_start();
//void http_server_set_event_start(); //TODO: delete
void http_server_start();

/**
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
Expand Down
2 changes: 1 addition & 1 deletion wifi_manager/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h1>About this app...</h1>
</header>
<h2></h2>
<section>
<p><strong>esp32-wifi-manager</strong>, &copy; 2017, Tony Pottier<br />Licender under the MIT License.</p>
<p><strong>esp32-wifi-manager</strong>, &copy; 2017-2019, Tony Pottier<br />Licender under the MIT License.</p>
<p>
This app would not be possible without the following libraries:
</p>
Expand Down
18 changes: 5 additions & 13 deletions wifi_manager/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ SOFTWARE.



static TaskHandle_t task_http_server = NULL;
static TaskHandle_t task_wifi_manager = NULL;


/* @brief tag used for ESP serial console messages */
static const char TAG[] = "main";
Expand All @@ -65,8 +64,8 @@ static const char TAG[] = "main";
void monitoring_task(void *pvParameter)
{
for(;;){
ESP_LOGD(TAG, "free heap: %d",esp_get_free_heap_size());
vTaskDelay(10000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "free heap: %d",esp_get_free_heap_size());
vTaskDelay( pdMS_TO_TICKS(10000) );
}
}

Expand All @@ -75,17 +74,10 @@ void app_main()
{


/* disable the default wifi logging */
esp_log_level_set("wifi", ESP_LOG_NONE);

/* initialize flash memory */
nvs_flash_init();

/* start the HTTP Server task */
xTaskCreate(&http_server, "http_server", 2048, NULL, 5, &task_http_server);

/* start the wifi manager task */
xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, 4, &task_wifi_manager);
/* start the wifi manager */
wifi_manager_start();

/* your code should go here. Here we simply create a task on core 2 that monitors free heap memory */
xTaskCreatePinnedToCore(&monitoring_task, "monitoring_task", 2048, NULL, 1, NULL, 1);
Expand Down
Loading

0 comments on commit 1a710da

Please sign in to comment.