fiatlux/firmware/fiatlux.c

414 lines
14 KiB
C
Raw Normal View History

2021-06-19 14:42:38 +00:00
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <stdio.h>
2021-07-09 19:35:41 +00:00
#include "system.h"
2021-07-02 16:45:29 +00:00
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
#include "lux.h"
2021-06-19 14:42:38 +00:00
2021-06-21 19:35:22 +00:00
#define LED_PIN 2
#define SWITCH_PIN 2
2021-06-19 14:42:38 +00:00
2021-06-21 19:35:22 +00:00
/* Add extras/sntp component to makefile for this include to work */
#include <sntp.h>
#include <time.h>
2021-06-25 13:41:33 +00:00
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
2021-06-25 14:13:22 +00:00
#include "lux.h"
2021-06-25 13:41:33 +00:00
2021-06-21 19:35:22 +00:00
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
#define UNUSED_ARG(x) (void)x
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
void sntp_task(void *pvParameters) {
const char *servers[] = {SNTP_SERVERS};
UNUSED_ARG(pvParameters);
/* Wait until we have joined AP and are assigned an IP */
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelayMs(100);
}
/* Start SNTP */
printf("Starting SNTP... ");
/* SNTP will request an update each 5 minutes */
sntp_set_update_delay(5 * 60000);
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1 * 60, 1};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
printf("DONE!\n");
/* Print date and time each 5 seconds */
while (1) {
vTaskDelayMs(5000);
//time_t ts = time(NULL);
//int t = ts;
//printf("TIME: %d %d %s", t,(int) day_seconds(), ctime(&ts));
}
}
static void dns_task(void *pvParameters) {
char *wifi_ap_ip_addr = NULL;
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
if(!wifi_ap_ip_addr) {
printf("dns: no ip address\n");
vTaskDelete(NULL);
}
ip4_addr_t server_addr;
server_addr.addr = ipaddr_addr(wifi_ap_ip_addr);
#if LWIP_IPV6
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in6 serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_port = htons(53);
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_scope_id = IP6_NO_ZONE;
#else
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(53);
#endif
bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
const struct ifreq ifreq0 = {"en0"};
const struct ifreq ifreq1 = {"en1"};
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0,
sizeof(ifreq0));
for (;;) {
char buffer[96];
struct sockaddr_storage src_addr;
socklen_t src_addr_len = sizeof(src_addr);
ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &src_addr, &src_addr_len);
/* Drop messages that are too large to send a response in the buffer */
if(count > 0 && count <= sizeof(buffer) - 16) {
size_t qname_len = strlen(buffer + 12) + 1;
uint32_t reply_len = 2 + 10 + qname_len + 16 + 4;
char *head = buffer + 2;
*head++ = 0x80; // Flags
*head++ = 0x00;
*head++ = 0x00; // Q count
*head++ = 0x01;
*head++ = 0x00; // A count
*head++ = 0x01;
*head++ = 0x00; // Auth count
*head++ = 0x00;
*head++ = 0x00; // Add count
*head++ = 0x00;
head += qname_len;
*head++ = 0x00; // Q type
*head++ = 0x01;
*head++ = 0x00; // Q class
*head++ = 0x01;
*head++ = 0xC0; // LBL offs
*head++ = 0x0C;
*head++ = 0x00; // Type
*head++ = 0x01;
*head++ = 0x00; // Class
*head++ = 0x01;
*head++ = 0x00; // TTL
*head++ = 0x00;
*head++ = 0x00;
*head++ = 0x78;
*head++ = 0x00; // RD len
*head++ = 0x04;
*head++ = ip4_addr1(&server_addr);
*head++ = ip4_addr2(&server_addr);
*head++ = ip4_addr3(&server_addr);
*head++ = ip4_addr4(&server_addr);
sendto(fd, buffer, reply_len, 0, (struct sockaddr *) &src_addr, src_addr_len);
}
}
}
const char *wificfg_default_ssid = "fiatlux_%02X%02X%02X";
const char *wificfg_default_password = "fiatlux02";
const char *wificfg_default_hostname = "fiatlux-%02x%02x%02x";
void user_init(void) {
2021-06-19 14:42:38 +00:00
uart_set_baud(0, 115200);
printf("SDK version: %s\n", sdk_system_get_sdk_version());
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
2021-07-09 19:35:41 +00:00
system_init_config();
2021-06-21 19:35:22 +00:00
char *wifi_sta_ssid = NULL;
char *wifi_sta_password = NULL;
char *wifi_ap_ssid = NULL;
char *wifi_ap_password = NULL;
/* Default a hostname. */
char *hostname = NULL;
sysparam_get_string("hostname", &hostname);
if(!hostname && wificfg_default_hostname) {
uint8_t macaddr[6];
char name[32];
sdk_wifi_get_macaddr(1, macaddr);
snprintf(name, sizeof(name), wificfg_default_hostname, macaddr[3],
macaddr[4], macaddr[5]);
sysparam_set_string("hostname", name);
}
if(hostname) {
free(hostname);
}
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
sysparam_get_string("wifi_sta_ssid", &wifi_sta_ssid);
sysparam_get_string("wifi_sta_password", &wifi_sta_password);
int8_t wifi_sta_enable = 1;
int8_t wifi_ap_enable = 1;
sysparam_get_int8("wifi_sta_enable", &wifi_sta_enable);
sysparam_get_int8("wifi_ap_enable", &wifi_ap_enable);
if(!wifi_sta_enable)
wifi_ap_enable = 1;
int8_t wifi_sta_disabled_restarts = 0;
sysparam_get_int8("wifi_sta_disabled_restarts", &wifi_sta_disabled_restarts);
if(wifi_sta_disabled_restarts > 0) {
wifi_sta_enable = 0;
wifi_sta_disabled_restarts--;
sysparam_set_int8("wifi_sta_disabled_restarts", wifi_sta_disabled_restarts);
}
int8_t wifi_ap_disabled_restarts = 0;
sysparam_get_int8("wifi_ap_disabled_restarts", &wifi_ap_disabled_restarts);
if(wifi_ap_disabled_restarts > 0) {
wifi_ap_enable = 0;
wifi_ap_disabled_restarts--;
sysparam_set_int8("wifi_ap_disabled_restarts", wifi_ap_disabled_restarts);
}
/* Validate the configuration. */
if(wifi_sta_enable && (!wifi_sta_ssid || !wifi_sta_password ||
strlen(wifi_sta_ssid) < 1 ||
strlen(wifi_sta_ssid) > 32 ||
!wifi_sta_password ||
strlen(wifi_sta_password) < 8 ||
strlen(wifi_sta_password) >= 64)) {
wifi_sta_enable = 0;
}
if(wifi_ap_enable) {
/* Default AP ssid and password. */
if(!wifi_ap_ssid && wificfg_default_ssid) {
uint8_t macaddr[6];
char ssid[32];
sdk_wifi_get_macaddr(1, macaddr);
snprintf(ssid, sizeof(ssid), wificfg_default_ssid, macaddr[3],
macaddr[4], macaddr[5]);
sysparam_set_string("wifi_ap_ssid", ssid);
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
if(!wifi_ap_password && wificfg_default_password) {
sysparam_set_string("wifi_ap_password", wificfg_default_password);
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
}
}
printf("ssid: %s\n", wifi_ap_ssid);
/* If the ssid and password are not valid then disable the AP interface. */
if(!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 ||
!wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) {
printf("len err\n");
wifi_ap_enable = 0;
}
}
int8_t wifi_mode = NULL_MODE;
if(wifi_sta_enable && wifi_ap_enable)
wifi_mode = STATIONAP_MODE;
else if(wifi_sta_enable)
wifi_mode = STATION_MODE;
else if(wifi_ap_enable)
wifi_mode = SOFTAP_MODE;
sdk_wifi_set_opmode(wifi_mode);
if(wifi_sta_enable) {
printf("try STA Mode\n");
struct sdk_station_config config;
strcpy((char *) config.ssid, wifi_sta_ssid);
strcpy((char *) config.password, wifi_sta_password);
config.bssid_set = 0;
int8_t wifi_sta_dhcp = 1;
sysparam_get_int8("wifi_sta_dhcp", &wifi_sta_dhcp);
if(!wifi_sta_dhcp) {
char *wifi_sta_ip_addr = NULL;
char *wifi_sta_netmask = NULL;
char *wifi_sta_gateway = NULL;
sysparam_get_string("wifi_sta_ip_addr", &wifi_sta_ip_addr);
sysparam_get_string("wifi_sta_netmask", &wifi_sta_netmask);
sysparam_get_string("wifi_sta_gateway", &wifi_sta_gateway);
if(wifi_sta_ip_addr && strlen(wifi_sta_ip_addr) > 4 &&
wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 &&
wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) {
sdk_wifi_station_dhcpc_stop();
struct ip_info info;
memset(&info, 0x0, sizeof(info));
info.ip.addr = ipaddr_addr(wifi_sta_ip_addr);
info.netmask.addr = ipaddr_addr(wifi_sta_netmask);
info.gw.addr = ipaddr_addr(wifi_sta_gateway);
sdk_wifi_set_ip_info(STATION_IF, &info);
}
if(wifi_sta_ip_addr) free(wifi_sta_ip_addr);
if(wifi_sta_netmask) free(wifi_sta_netmask);
if(wifi_sta_gateway) free(wifi_sta_gateway);
}
sdk_wifi_station_set_config(&config);
}
if(wifi_ap_enable) {
printf("try AP Mode\n");
/* Read and validate paramenters. */
int8_t wifi_ap_ssid_hidden = 0;
sysparam_get_int8("wifi_ap_ssid_hidden", &wifi_ap_ssid_hidden);
if(wifi_ap_ssid_hidden < 0 || wifi_ap_ssid_hidden > 1) {
wifi_ap_ssid_hidden = 1;
}
int8_t wifi_ap_channel = 6;
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel);
#if 0
/* AU does not allow channels above 13, although 14 works. */
if(wifi_ap_channel > 13) {
wifi_ap_channel = 13;
}
/* US does not allow channels above 11, although they work. */
if (wifi_ap_channel > 11) {
wifi_ap_channel = 11;
}
#endif
if(wifi_ap_channel < 1 || wifi_ap_channel > 14) {
wifi_ap_channel = 6;
}
int8_t wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
sysparam_get_int8("wifi_ap_authmode", &wifi_ap_authmode);
if(wifi_ap_authmode != AUTH_OPEN && wifi_ap_authmode != AUTH_WPA_PSK &&
wifi_ap_authmode != AUTH_WPA2_PSK && wifi_ap_authmode != AUTH_WPA_WPA2_PSK) {
wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
}
int8_t wifi_ap_max_conn = 3;
sysparam_get_int8("wifi_ap_max_conn", &wifi_ap_max_conn);
if(wifi_ap_max_conn < 1 || wifi_ap_max_conn > 8) {
wifi_ap_max_conn = 3;
}
int32_t wifi_ap_beacon_interval = 100;
sysparam_get_int32("wifi_ap_beacon_interval", &wifi_ap_beacon_interval);
if(wifi_ap_beacon_interval < 0 || wifi_ap_beacon_interval > 1000) {
wifi_ap_beacon_interval = 100;
}
/* Default AP IP address and netmask. */
char *wifi_ap_ip_addr = NULL;
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
if(!wifi_ap_ip_addr) {
sysparam_set_string("wifi_ap_ip_addr", "172.16.0.1");
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
}
char *wifi_ap_netmask = NULL;
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
if(!wifi_ap_netmask) {
sysparam_set_string("wifi_ap_netmask", "255.255.0.0");
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
}
if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) {
struct ip_info ap_ip;
ap_ip.ip.addr = ipaddr_addr(wifi_ap_ip_addr);
ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
sdk_wifi_set_ip_info(1, &ap_ip);
struct sdk_softap_config ap_config = {
.ssid_hidden = wifi_ap_ssid_hidden,
.channel = wifi_ap_channel,
.authmode = wifi_ap_authmode,
.max_connection = wifi_ap_max_conn,
.beacon_interval = wifi_ap_beacon_interval,
};
strcpy((char *) ap_config.ssid, wifi_ap_ssid);
ap_config.ssid_len = strlen(wifi_ap_ssid);
strcpy((char *) ap_config.password, wifi_ap_password);
sdk_wifi_softap_set_config(&ap_config);
int8_t wifi_ap_dhcp_leases = 4;
sysparam_get_int8("wifi_ap_dhcp_leases", &wifi_ap_dhcp_leases);
if(wifi_ap_dhcp_leases) {
ip4_addr_t first_client_ip;
first_client_ip.addr = ap_ip.ip.addr + htonl(1);
int8_t wifi_ap_dns = 1;
sysparam_get_int8("wifi_ap_dns", &wifi_ap_dns);
if(wifi_ap_dns < 0 || wifi_ap_dns > 1)
wifi_ap_dns = 1;
dhcpserver_start(&first_client_ip, wifi_ap_dhcp_leases);
dhcpserver_set_router(&ap_ip.ip);
if(wifi_ap_dns) {
dhcpserver_set_dns(&ap_ip.ip);
xTaskCreate(dns_task, "WiFi Cfg DNS", 384, NULL, 2, NULL);
}
}
}
free(wifi_ap_ip_addr);
free(wifi_ap_netmask);
}
if(wifi_sta_ssid) free(wifi_sta_ssid);
if(wifi_sta_password) free(wifi_sta_password);
if(wifi_ap_ssid) free(wifi_ap_ssid);
if(wifi_ap_password) free(wifi_ap_password);
if(wifi_mode != NULL_MODE) {
/* turn off LED */
//gpio_enable(LED_PIN, GPIO_OUTPUT);
//gpio_write(LED_PIN, true);
2021-06-25 14:13:22 +00:00
xTaskCreate(&lux_task, "lux_task", 256, NULL, 2, NULL);
2021-06-21 19:35:22 +00:00
/* initialize tasks */
2021-06-25 14:13:22 +00:00
xTaskCreate(&httpd_task, "&httpd_task", 2048, NULL, 2, NULL);
2021-06-21 19:35:22 +00:00
xTaskCreate(&sntp_task, "SNTP", 512, NULL, 1, NULL);
}
>>>>>>> 4b8d354 (basic webconf)
2021-06-19 14:42:38 +00:00
}