From 881aafee945514e614beb1659ef721d7b91a3eb2 Mon Sep 17 00:00:00 2001 From: Maurizio Porrato Date: Sun, 19 Feb 2017 17:02:28 +0000 Subject: [PATCH] Moved network and httpd tasks out of main source file --- src/common.h | 9 +++++ src/httpd.c | 2 + src/httpd.h | 6 +++ src/network.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++ src/network.h | 6 +++ src/wifidmm.c | 103 ++------------------------------------------------ 6 files changed, 125 insertions(+), 99 deletions(-) create mode 100644 src/common.h create mode 100644 src/httpd.h create mode 100644 src/network.c create mode 100644 src/network.h diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..845e181 --- /dev/null +++ b/src/common.h @@ -0,0 +1,9 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include + +#define TIME_MS(ms) ((ms) / portTICK_PERIOD_MS) +#define panic() { puts("\n\n*** PANIC! ***"); for (;;); } + +#endif diff --git a/src/httpd.c b/src/httpd.c index 6781ad5..26acaaa 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -7,6 +7,8 @@ #include #include +#include "common.h" + #define LED_PIN 2 enum { diff --git a/src/httpd.h b/src/httpd.h new file mode 100644 index 0000000..3cd1682 --- /dev/null +++ b/src/httpd.h @@ -0,0 +1,6 @@ +#ifndef _HTTPD_H_ +#define _HTTPD_H_ + +void httpd_task(void *pvParameters); + +#endif diff --git a/src/network.c b/src/network.c new file mode 100644 index 0000000..d21ff85 --- /dev/null +++ b/src/network.c @@ -0,0 +1,98 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#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)); + } + } +} diff --git a/src/network.h b/src/network.h new file mode 100644 index 0000000..daaf032 --- /dev/null +++ b/src/network.h @@ -0,0 +1,6 @@ +#ifndef _NETWORK_H_ +#define _NETWORK_H_ + +void NetworkConnectionTask(void *p); + +#endif diff --git a/src/wifidmm.c b/src/wifidmm.c index 2fd8ace..190f9dd 100644 --- a/src/wifidmm.c +++ b/src/wifidmm.c @@ -6,105 +6,9 @@ #include #include #include -#include -#include -#include - -#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); } } }