Moved network and httpd tasks out of main source file

This commit is contained in:
Maurizio Porrato 2017-02-19 17:02:28 +00:00
parent 853266b151
commit 881aafee94
6 changed files with 125 additions and 99 deletions

9
src/common.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <portmacro.h>
#define TIME_MS(ms) ((ms) / portTICK_PERIOD_MS)
#define panic() { puts("\n\n*** PANIC! ***"); for (;;); }
#endif

View File

@ -7,6 +7,8 @@
#include <task.h>
#include <httpd/httpd.h>
#include "common.h"
#define LED_PIN 2
enum {

6
src/httpd.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _HTTPD_H_
#define _HTTPD_H_
void httpd_task(void *pvParameters);
#endif

98
src/network.c Normal file
View File

@ -0,0 +1,98 @@
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <espressif/esp_common.h>
#include <FreeRTOS.h>
#include <task.h>
#include <dhcpserver.h>
#include <lwip/api.h>
#include "common.h"
#include "httpd.h"
/* This task ensures the network connection is up.
* If the network has not been configured yet,
* setup an AP with captive portal to allow
* network configuration.
*/
void NetworkConnectionTask(void *p) {
struct sdk_softap_config apconf;
struct sdk_station_config staconf;
uint8_t mac[6];
if (1 || !sdk_wifi_station_get_config(&staconf)) {
/* Station mode not configured: fallback to SoftAP configuration */
printf("sdk_wifi_station_get_config() failed\n");
if (!sdk_wifi_softap_get_config(&apconf)) {
/* SoftAP mode not configured: initialize default configuration */
printf("sdk_wifi_softap_get_config() failed\n");
sdk_wifi_get_macaddr(SOFTAP_IF, mac);
sprintf((char *)apconf.ssid, "UT61e-%02x%02x%02x", mac[3], mac[4], mac[5]);
apconf.ssid_len = strlen((char *)apconf.ssid);
apconf.channel = 1; /* TODO: auto channel selection? */
apconf.authmode = AUTH_OPEN;
apconf.ssid_hidden = 0;
apconf.max_connection = 4;
apconf.beacon_interval = 100;
sdk_wifi_softap_set_config(&apconf);
} else {
struct ip_info ap_ip;
ip_addr_t first_client_ip;
IP4_ADDR(&ap_ip.ip, 192, 168, 13, 1);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0);
sdk_wifi_set_ip_info(1, &ap_ip);
IP4_ADDR(&first_client_ip, 192, 168, 13, 2);
dhcpserver_start(&first_client_ip, 4);
sdk_wifi_set_opmode(SOFTAP_MODE);
printf("SoftAP config:\n");
printf("SSID: %s\n", apconf.ssid);
printf("Password: %s\n", apconf.password);
printf("Channel: %d\n", apconf.channel);
printf("Authmode: %d\n", apconf.authmode);
vTaskDelay(TIME_MS(3000));
xTaskCreate(httpd_task, "httpd", 512, NULL, 1, NULL);
for (;;)
vTaskDelay(TIME_MS(5000));
}
} else {
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_connect();
printf("Station config:\n");
printf("SSID: %s\n", staconf.ssid);
printf("Password: %s\n", staconf.password);
printf("BSSID: %s\n", staconf.bssid);
printf("BSSID set: %d\n", staconf.bssid_set);
uint8_t st;
for (;;) {
switch (st = sdk_wifi_station_get_connect_status()) {
case STATION_IDLE:
printf("IDLE\n");
break;
case STATION_CONNECTING:
printf("CONNECTING\n");
break;
case STATION_WRONG_PASSWORD:
printf("WRONG_PASSWORD\n");
break;
case STATION_NO_AP_FOUND:
printf("NO_AP_FOUND\n");
break;
case STATION_CONNECT_FAIL:
printf("CONNECT_FAIL\n");
break;
case STATION_GOT_IP:
printf("GOT_IP\n");
break;
default:
printf("UNEXPECTED status: %d\n", st);
}
vTaskDelay(TIME_MS(750));
}
}
}

6
src/network.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _NETWORK_H_
#define _NETWORK_H_
void NetworkConnectionTask(void *p);
#endif

View File

@ -6,105 +6,9 @@
#include <esp/uart.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <dhcpserver.h>
#include <lwip/api.h>
#define TIME_MS(ms) ((ms) / portTICK_PERIOD_MS)
extern void httpd_task(void *);
void panic() {
printf("\n\n*** PANIC! ***\n");
for (;;);
}
/* This task ensures the network connection is up.
* If the network has not been configured yet,
* setup an AP with captive portal to allow
* network configuration.
*/
void NetworkConnectionTask(void *p) {
struct sdk_softap_config apconf;
struct sdk_station_config staconf;
uint8_t mac[6];
if (1 || !sdk_wifi_station_get_config(&staconf)) {
/* Station mode not configured: fallback to SoftAP configuration */
printf("sdk_wifi_station_get_config() failed\n");
if (!sdk_wifi_softap_get_config(&apconf)) {
/* SoftAP mode not configured: initialize default configuration */
printf("sdk_wifi_softap_get_config() failed\n");
sdk_wifi_get_macaddr(SOFTAP_IF, mac);
sprintf((char *)apconf.ssid, "UT61e-%02x%02x%02x", mac[3], mac[4], mac[5]);
apconf.ssid_len = strlen((char *)apconf.ssid);
apconf.channel = 1; /* TODO: auto channel selection? */
apconf.authmode = AUTH_OPEN;
apconf.ssid_hidden = 0;
apconf.max_connection = 4;
apconf.beacon_interval = 100;
sdk_wifi_softap_set_config(&apconf);
} else {
struct ip_info ap_ip;
ip_addr_t first_client_ip;
IP4_ADDR(&ap_ip.ip, 192, 168, 13, 1);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0);
sdk_wifi_set_ip_info(1, &ap_ip);
IP4_ADDR(&first_client_ip, 192, 168, 13, 2);
dhcpserver_start(&first_client_ip, 4);
sdk_wifi_set_opmode(SOFTAP_MODE);
printf("SoftAP config:\n");
printf("SSID: %s\n", apconf.ssid);
printf("Password: %s\n", apconf.password);
printf("Channel: %d\n", apconf.channel);
printf("Authmode: %d\n", apconf.authmode);
vTaskDelay(TIME_MS(3000));
xTaskCreate(httpd_task, "httpd", 512, NULL, 1, NULL);
for (;;)
vTaskDelay(TIME_MS(5000));
}
} else {
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_connect();
printf("Station config:\n");
printf("SSID: %s\n", staconf.ssid);
printf("Password: %s\n", staconf.password);
printf("BSSID: %s\n", staconf.bssid);
printf("BSSID set: %d\n", staconf.bssid_set);
uint8_t st;
for (;;) {
switch (st = sdk_wifi_station_get_connect_status()) {
case STATION_IDLE:
printf("IDLE\n");
break;
case STATION_CONNECTING:
printf("CONNECTING\n");
break;
case STATION_WRONG_PASSWORD:
printf("WRONG_PASSWORD\n");
break;
case STATION_NO_AP_FOUND:
printf("NO_AP_FOUND\n");
break;
case STATION_CONNECT_FAIL:
printf("CONNECT_FAIL\n");
break;
case STATION_GOT_IP:
printf("GOT_IP\n");
break;
default:
printf("UNEXPECTED status: %d\n", st);
}
vTaskDelay(TIME_MS(750));
}
}
}
#include "common.h"
#include "network.h"
/* This task continuosly listen for incoming data
* on the UART line, parses the packets and feeds
@ -118,13 +22,14 @@ void SerialDataCollectionTask(void *p) {
for (;;) {
if (read(0, (void *)&c, 1)) {
printf("%c", c);
printf("%02x", c);
if (bufpos < sizeof(buffer) / sizeof(buffer[0]) - 1)
buffer[bufpos++] = c;
if (c == '\n') {
buffer[bufpos] = '\0';
/* TODO: Send the frame to the shipping task */
bufpos = 0;
printf("Measurement: %s\n", buffer);
}
}
}