diff --git a/.gitmodules b/.gitmodules index 1f29159..645c4ce 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,12 @@ [submodule "components/esp-cjson"] path = components/esp-cjson url = https://github.com/maximkulkin/esp-cjson +[submodule "components/esp-wifi-config"] + path = components/esp-wifi-config + url = https://github.com/maximkulkin/esp-wifi-config +[submodule "components/esp-homekit-common-functions"] + path = components/esp-homekit-common-functions + url = https://github.com/maccoylton/esp-homekit-common-functions.git +[submodule "components/UDPlogger"] + path = components/UDPlogger + url = https://github.com/HomeACcessoryKid/UDPlogger.git diff --git a/components/UDPlogger b/components/UDPlogger new file mode 160000 index 0000000..1dbf2df --- /dev/null +++ b/components/UDPlogger @@ -0,0 +1 @@ +Subproject commit 1dbf2df9c8f8fcb861aa8b787e9d17806a28ad41 diff --git a/components/esp-cjson b/components/esp-cjson index 2216b13..2b93171 160000 --- a/components/esp-cjson +++ b/components/esp-cjson @@ -1 +1 @@ -Subproject commit 2216b130ce726071407541680f5c763003e8d740 +Subproject commit 2b9317101a7ffbcf834eaebf7ef92b6d52d10ff4 diff --git a/components/esp-homekit b/components/esp-homekit index c2e67c4..8b72103 160000 --- a/components/esp-homekit +++ b/components/esp-homekit @@ -1 +1 @@ -Subproject commit c2e67c468890b376343bf867309b31390af0dad9 +Subproject commit 8b72103cdd2ff44f4e49c88f206e65589a256cfa diff --git a/components/esp-homekit-common-functions b/components/esp-homekit-common-functions new file mode 160000 index 0000000..23106a2 --- /dev/null +++ b/components/esp-homekit-common-functions @@ -0,0 +1 @@ +Subproject commit 23106a2b9b67a924c114825365935cc6e1a2037f diff --git a/components/esp-wifi-config b/components/esp-wifi-config new file mode 160000 index 0000000..22509d4 --- /dev/null +++ b/components/esp-wifi-config @@ -0,0 +1 @@ +Subproject commit 22509d40ba3e1dd790458f1b81dbb5be636f145c diff --git a/components/esp-wolfssl b/components/esp-wolfssl index 62252d7..e56663b 160000 --- a/components/esp-wolfssl +++ b/components/esp-wolfssl @@ -1 +1 @@ -Subproject commit 62252d705acc7156b1217a22e9bb78e3de26936a +Subproject commit e56663b0d66f23c38927bb00a0185acf61ab337b diff --git a/libs/check_wifi/check_wifi.c b/libs/check_wifi/check_wifi.c deleted file mode 100644 index 920cd5b..0000000 --- a/libs/check_wifi/check_wifi.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "check_wifi.h" - -bool wifi_connected; - -void checkWifiTask(void *pvParameters) -{ - uint8_t status ; - - wifi_connected = false; - - while (1) - { - status = sdk_wifi_station_get_connect_status(); - switch (status) - { - case STATION_WRONG_PASSWORD: - printf("WiFi: wrong password\n\r"); - break; - case STATION_NO_AP_FOUND: - printf("WiFi: AP not found\n\r"); - break; - case STATION_CONNECT_FAIL: - printf("WiFi: connection failed\r\n"); - break; - case STATION_GOT_IP: - wifi_connected = true; - break; - default: - printf("%s: status = %d\n\r", __func__, status); - break; - } - vTaskDelay(CHECK_INTERVAL / portTICK_PERIOD_MS); - } -} diff --git a/libs/check_wifi/check_wifi.h b/libs/check_wifi/check_wifi.h deleted file mode 100644 index 18c4164..0000000 --- a/libs/check_wifi/check_wifi.h +++ /dev/null @@ -1,3 +0,0 @@ -#define CHECK_INTERVAL 36000000 - -void checkWifiTask(void *pvParameters); diff --git a/libs/check_wifi/component.mk b/libs/check_wifi/component.mk deleted file mode 100644 index 6c2303e..0000000 --- a/libs/check_wifi/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -# Component makefile for check_wifi - -INC_DIRS += $(check_wifi_ROOT) - -check_wifi_INC_DIR = $(check_wifi_ROOT) -check_wifi_SRC_DIR = $(check_wifi_ROOT) - -$(eval $(call component_compile_rules,check_wifi)) diff --git a/libs/http_post/component.mk b/libs/http_post/component.mk deleted file mode 100644 index 89eb962..0000000 --- a/libs/http_post/component.mk +++ /dev/null @@ -1,10 +0,0 @@ -# Component makefile for http_post - - -INC_DIRS += $(http_post_ROOT) - - -http_post_INC_DIR = $(http_post_ROOT) -http_post_SRC_DIR = $(http_post_ROOT) - -$(eval $(call component_compile_rules,http_post)) diff --git a/libs/http_post/http_post.c b/libs/http_post/http_post.c deleted file mode 100644 index 78e4485..0000000 --- a/libs/http_post/http_post.c +++ /dev/null @@ -1,126 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include - -#include -#include -#include -#include -#include -#include - - -#define WEB_SERVER "192.168.1.2" -#define WEB_PORT "80" -#define WEB_URL "/esplogger/postsql.php" -char request[450]; -char details[150]; -float temperature; -float humidity; -float moisture; - -void http_post_task(void * pvParameters) -{ - int successes = 0, failures = 0; - printf("HTTP post task starting...\r\n"); - - while(1) { - - printf ("Suspending http_post_task\n"); - vTaskSuspend( NULL ); - - printf ("Resumed http_post_task\n"); - - const struct addrinfo hints = { - .ai_family = AF_INET, - .ai_socktype = SOCK_STREAM, - }; - struct addrinfo *res; - - printf("Running DNS lookup for %s...\r\n", WEB_SERVER); - int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res); - - if(err != 0 || res == NULL) { - printf("DNS lookup failed err=%d res=%p\r\n", err, res); - if(res) - freeaddrinfo(res); - vTaskDelay(1000 / portTICK_PERIOD_MS); - failures++; - continue; - } - /* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ - struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; - printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr)); - - int s = lwip_socket(res->ai_family, res->ai_socktype, 0); - if(s < 0) { - printf("... Failed to allocate socket\r\n"); - freeaddrinfo(res); - vTaskDelay(1000 / portTICK_PERIOD_MS); - failures++; - continue; - } - - printf("... allocated socket: %d \r\n", s); - - if(lwip_connect(s, res->ai_addr, res->ai_addrlen) != 0) { - lwip_close(s); - freeaddrinfo(res); - printf("... socket connect failed.\r\n"); - vTaskDelay(4000 / portTICK_PERIOD_MS); - failures++; - continue; - } - - printf("... connected\r\n"); - freeaddrinfo(res); - request[0] = "\0"; - - snprintf(details, 150, post_string); - snprintf(request, 450, "POST %s HTTP/1.0\r\n" - "Host: %s\r\n" - "Content-type: application/x-www-form-urlencoded\r\n" - "Content-Length: %d\r\n\r\n" - "%s", WEB_URL, WEB_SERVER, strlen(details), details); - printf(request); - if (lwip_write(s, request, strlen(request)) < 0) { - printf("... socket send failed\r\n"); - lwip_close(s); - vTaskDelay(4000 / portTICK_PERIOD_MS); - failures++; - continue; - } - printf("... socket send success\r\n"); - - static char recv_buf[200]; - int r; - do { - printf("receiving..."); - bzero(recv_buf, 200); - r = lwip_read(s, recv_buf, 199); - if(r > 0) { - printf("%s", recv_buf); - } - } while(r > 0); - - printf("... done reading from socket. Last read return=%d errno=%d\r\n", r, errno); - if(r != 0) - failures++; - else - successes++; - lwip_close(s); - printf("successes = %d failures = %d\r\n", successes, failures); - vTaskDelay(10000 / portTICK_PERIOD_MS); - printf("\r\nStarting again!\r\n"); - } -} - diff --git a/libs/http_post/http_post.h b/libs/http_post/http_post.h deleted file mode 100644 index 1f31af9..0000000 --- a/libs/http_post/http_post.h +++ /dev/null @@ -1,5 +0,0 @@ -void http_post_task (void * pvParameters ); -//void https_post_wolfssl_task (void * pvParameters ); - - -char post_string[150]; diff --git a/libs/http_post/https_post.c b/libs/http_post/https_post.c deleted file mode 100644 index f990639..0000000 --- a/libs/http_post/https_post.c +++ /dev/null @@ -1,356 +0,0 @@ -/* http_get_bearssl - HTTPS version of the http_get example, using BearSSL. - * - * Retrieves a JSON response from the howsmyssl.com API via HTTPS over TLS v1.2. - * - * Validates the server's certificate using a hardcoded public key. - * - * Adapted from the client_basic sample in BearSSL. - * - * Original Copyright (c) 2016 Thomas Pornin , MIT License. - * Additions Copyright (C) 2016 Stefan Schake, MIT License. - * Modified to POST Request Copyright (C) 2018 David B Brown, MIT License. - */ -#include "espressif/esp_common.h" -#include "esp/uart.h" -#include "esp/hwrand.h" - -#include -#include - -#include "FreeRTOS.h" -#include "task.h" - -#include "lwip/err.h" -#include "lwip/sockets.h" -#include "lwip/sys.h" -#include "lwip/netdb.h" -#include "lwip/dns.h" -#include "lwip/api.h" - -#include "bearssl.h" -#include - - -#define CLOCK_SECONDS_PER_MINUTE (60UL) -#define CLOCK_MINUTES_PER_HOUR (60UL) -#define CLOCK_HOURS_PER_DAY (24UL) -#define CLOCK_SECONDS_PER_HOUR (CLOCK_MINUTES_PER_HOUR*CLOCK_SECONDS_PER_MINUTE) -#define CLOCK_SECONDS_PER_DAY (CLOCK_HOURS_PER_DAY*CLOCK_SECONDS_PER_HOUR) -#define CONFIG_EPOCH_TIME 1 - -#define WEB_SERVER "192.168.1.2" -#define WEB_PORT "80" -#define WEB_URL "/esplogger/postsql.php" -#define POST_REQUEST "POST %s HTTP/1.0\r\nHost: %s\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s" - -char request[450]; -char details[150]; - -/* - * Low-level data read callback for the simplified SSL I/O API. - */ -static int -sock_read(void *ctx, unsigned char *buf, size_t len) -{ - for (;;) { - ssize_t rlen; - - rlen = read(*(int *)ctx, buf, len); - if (rlen <= 0) { - if (rlen < 0 && errno == EINTR) { - continue; - } - return -1; - } - return (int)rlen; - } -} - -/* - * Low-level data write callback for the simplified SSL I/O API. - */ -static int -sock_write(void *ctx, const unsigned char *buf, size_t len) -{ - for (;;) { - ssize_t wlen; - - wlen = write(*(int *)ctx, buf, len); - if (wlen <= 0) { - if (wlen < 0 && errno == EINTR) { - continue; - } - return -1; - } - return (int)wlen; - } -} - -/* - * The hardcoded trust anchors. These are the two DN + public key that - * correspond to the self-signed certificates cert-root-rsa.pem and - * cert-root-ec.pem. - * - * C code for hardcoded trust anchors can be generated with the "brssl" - * command-line tool (with the "ta" command). To build that tool run: - * - * $ cd /path/to/esp-open-rtos/extras/bearssl/BearSSL - * $ make build/brssl - * - * Below is the imported "Let's Encrypt" root certificate, as howsmyssl - * is depending on it: - * - * https://letsencrypt.org/certs/letsencryptauthorityx3.pem - * - * The generate the trust anchor code below, run: - * - * $ /path/to/esp-open-rtos/extras/bearssl/BearSSL/build/brssl \ - * ta letsencryptauthorityx3.pem - * - * To get the server certificate for a given https host: - * - * $ openssl s_client -showcerts -servername www.howsmyssl.com \ - * -connect www.howsmyssl.com:443 < /dev/null | \ - * openssl x509 -outform pem > server.pem - */ - -static const unsigned char TA0_DN[] = { - 0x30, 0x4A, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, - 0x02, 0x55, 0x53, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A, - 0x13, 0x0D, 0x4C, 0x65, 0x74, 0x27, 0x73, 0x20, 0x45, 0x6E, 0x63, 0x72, - 0x79, 0x70, 0x74, 0x31, 0x23, 0x30, 0x21, 0x06, 0x03, 0x55, 0x04, 0x03, - 0x13, 0x1A, 0x4C, 0x65, 0x74, 0x27, 0x73, 0x20, 0x45, 0x6E, 0x63, 0x72, - 0x79, 0x70, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69, 0x74, - 0x79, 0x20, 0x58, 0x33 -}; - -static const unsigned char TA0_RSA_N[] = { - 0x9C, 0xD3, 0x0C, 0xF0, 0x5A, 0xE5, 0x2E, 0x47, 0xB7, 0x72, 0x5D, 0x37, - 0x83, 0xB3, 0x68, 0x63, 0x30, 0xEA, 0xD7, 0x35, 0x26, 0x19, 0x25, 0xE1, - 0xBD, 0xBE, 0x35, 0xF1, 0x70, 0x92, 0x2F, 0xB7, 0xB8, 0x4B, 0x41, 0x05, - 0xAB, 0xA9, 0x9E, 0x35, 0x08, 0x58, 0xEC, 0xB1, 0x2A, 0xC4, 0x68, 0x87, - 0x0B, 0xA3, 0xE3, 0x75, 0xE4, 0xE6, 0xF3, 0xA7, 0x62, 0x71, 0xBA, 0x79, - 0x81, 0x60, 0x1F, 0xD7, 0x91, 0x9A, 0x9F, 0xF3, 0xD0, 0x78, 0x67, 0x71, - 0xC8, 0x69, 0x0E, 0x95, 0x91, 0xCF, 0xFE, 0xE6, 0x99, 0xE9, 0x60, 0x3C, - 0x48, 0xCC, 0x7E, 0xCA, 0x4D, 0x77, 0x12, 0x24, 0x9D, 0x47, 0x1B, 0x5A, - 0xEB, 0xB9, 0xEC, 0x1E, 0x37, 0x00, 0x1C, 0x9C, 0xAC, 0x7B, 0xA7, 0x05, - 0xEA, 0xCE, 0x4A, 0xEB, 0xBD, 0x41, 0xE5, 0x36, 0x98, 0xB9, 0xCB, 0xFD, - 0x6D, 0x3C, 0x96, 0x68, 0xDF, 0x23, 0x2A, 0x42, 0x90, 0x0C, 0x86, 0x74, - 0x67, 0xC8, 0x7F, 0xA5, 0x9A, 0xB8, 0x52, 0x61, 0x14, 0x13, 0x3F, 0x65, - 0xE9, 0x82, 0x87, 0xCB, 0xDB, 0xFA, 0x0E, 0x56, 0xF6, 0x86, 0x89, 0xF3, - 0x85, 0x3F, 0x97, 0x86, 0xAF, 0xB0, 0xDC, 0x1A, 0xEF, 0x6B, 0x0D, 0x95, - 0x16, 0x7D, 0xC4, 0x2B, 0xA0, 0x65, 0xB2, 0x99, 0x04, 0x36, 0x75, 0x80, - 0x6B, 0xAC, 0x4A, 0xF3, 0x1B, 0x90, 0x49, 0x78, 0x2F, 0xA2, 0x96, 0x4F, - 0x2A, 0x20, 0x25, 0x29, 0x04, 0xC6, 0x74, 0xC0, 0xD0, 0x31, 0xCD, 0x8F, - 0x31, 0x38, 0x95, 0x16, 0xBA, 0xA8, 0x33, 0xB8, 0x43, 0xF1, 0xB1, 0x1F, - 0xC3, 0x30, 0x7F, 0xA2, 0x79, 0x31, 0x13, 0x3D, 0x2D, 0x36, 0xF8, 0xE3, - 0xFC, 0xF2, 0x33, 0x6A, 0xB9, 0x39, 0x31, 0xC5, 0xAF, 0xC4, 0x8D, 0x0D, - 0x1D, 0x64, 0x16, 0x33, 0xAA, 0xFA, 0x84, 0x29, 0xB6, 0xD4, 0x0B, 0xC0, - 0xD8, 0x7D, 0xC3, 0x93 -}; - -static const unsigned char TA0_RSA_E[] = { - 0x01, 0x00, 0x01 -}; - -static const br_x509_trust_anchor TAs[1] = { - { - { (unsigned char *)TA0_DN, sizeof TA0_DN }, - BR_X509_TA_CA, - { - BR_KEYTYPE_RSA, - { .rsa = { - (unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N, - (unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E, - } } - } - } -}; - -#define TAs_NUM 1 - -/* - * Buffer to store a record + BearSSL state - * We use MONO mode to save 16k of RAM. - * This could be even smaller by using max_fragment_len, but - * the howsmyssl.com server doesn't seem to support it. - */ -static unsigned char bearssl_buffer[BR_SSL_BUFSIZE_MONO]; - -static br_ssl_client_context sc; -static br_x509_minimal_context xc; -static br_sslio_context ioc; - -void https_post_task(void *pvParameters) -{ - int successes = 0, failures = 0; - int provisional_time = 0; - - while (1) { - - printf ("Suspending http_post_task\n"); - vTaskSuspend( NULL ); - printf ("Resumed http_post_task\n"); - - /* - * Wait until we can resolve the DNS for the server, as an indication - * our network is probably working... - */ - const struct addrinfo hints = { - .ai_family = AF_INET, - .ai_socktype = SOCK_STREAM, - }; - struct addrinfo *res = NULL; - int dns_err = 0; - do { - if (res) - freeaddrinfo(res); - vTaskDelay(1000 / portTICK_PERIOD_MS); - dns_err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res); - } while(dns_err != 0 || res == NULL); - - int fd = socket(res->ai_family, res->ai_socktype, 0); - if (fd < 0) { - freeaddrinfo(res); - printf("socket failed\n"); - failures++; - continue; - } - - printf("Initializing BearSSL... "); - br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM); - - /* - * Set the I/O buffer to the provided array. We allocated a - * buffer large enough for full-duplex behaviour with all - * allowed sizes of SSL records, hence we set the last argument - * to 1 (which means "split the buffer into separate input and - * output areas"). - */ - br_ssl_engine_set_buffer(&sc.eng, bearssl_buffer, sizeof bearssl_buffer, 0); - - /* - * Inject some entropy from the ESP hardware RNG - * This is necessary because we don't support any of the BearSSL methods - */ - for (int i = 0; i < 10; i++) { - int rand = hwrand(); - br_ssl_engine_inject_entropy(&sc.eng, &rand, 4); - } - - /* - * Reset the client context, for a new handshake. We provide the - * target host name: it will be used for the SNI extension. The - * last parameter is 0: we are not trying to resume a session. - */ - br_ssl_client_reset(&sc, WEB_SERVER, 0); - - /* - * Initialise the simplified I/O wrapper context, to use our - * SSL client context, and the two callbacks for socket I/O. - */ - br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd); - printf("done.\r\n"); - - /* FIXME: set date & time using epoch time precompiler flag for now */ - provisional_time = CONFIG_EPOCH_TIME + (xTaskGetTickCount()/configTICK_RATE_HZ); - xc.days = (provisional_time / CLOCK_SECONDS_PER_DAY) + 719528; - xc.seconds = provisional_time % CLOCK_SECONDS_PER_DAY; - printf("Time: %02i:%02i\r\n", - (int)(xc.seconds / CLOCK_SECONDS_PER_HOUR), - (int)((xc.seconds % CLOCK_SECONDS_PER_HOUR)/CLOCK_SECONDS_PER_MINUTE) - ); - - if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) - { - close(fd); - freeaddrinfo(res); - printf("connect failed\n"); - failures++; - continue; - } - printf("Connected\r\n"); - - /* - * Note that while the context has, at that point, already - * assembled the ClientHello to send, nothing happened on the - * network yet. Real I/O will occur only with the next call. - * - * We write our simple HTTP request. We test the call - * for an error (-1), but this is not strictly necessary, since - * the error state "sticks": if the context fails for any reason - * (e.g. bad server certificate), then it will remain in failed - * state and all subsequent calls will return -1 as well. - */ - - request[0] = "\0"; - snprintf(details, 150, post_string); - snprintf(request, 450, "POST %s HTTP/1.0\r\n" - "Host: %s\r\n" - "Content-type: application/x-www-form-urlencoded\r\n" - "Content-Length: %d\r\n\r\n" - "%s", WEB_URL, WEB_SERVER, strlen(details), details); - printf(request); - - - if (br_sslio_write_all(&ioc, request, strlen(request)) != BR_ERR_OK) { - close(fd); - freeaddrinfo(res); - printf("br_sslio_write_all failed: %d\r\n", br_ssl_engine_last_error(&sc.eng)); - failures++; - continue; - } - - /* - * SSL is a buffered protocol: we make sure that all our request - * bytes are sent onto the wire. - */ - br_sslio_flush(&ioc); - - /* - * Read and print the server response - */ - for (;;) - { - int rlen; - unsigned char buf[128]; - - bzero(buf, 128); - // Leave the final byte for zero termination - rlen = br_sslio_read(&ioc, buf, sizeof(buf) - 1); - - if (rlen < 0) { - break; - } - if (rlen > 0) { - printf("%s", buf); - } - } - - /* - * If reading the response failed for any reason, we detect it here - */ - if (br_ssl_engine_last_error(&sc.eng) != BR_ERR_OK) { - close(fd); - freeaddrinfo(res); - printf("failure, error = %d\r\n", br_ssl_engine_last_error(&sc.eng)); - failures++; - continue; - } - - printf("\r\n\r\nfree heap pre = %u\r\n", xPortGetFreeHeapSize()); - - /* - * Close the connection and start over after a delay - */ - close(fd); - freeaddrinfo(res); - - printf("free heap post = %u\r\n", xPortGetFreeHeapSize()); - - successes++; - printf("successes = %d failures = %d\r\n", successes, failures); - - printf("Starting again!\r\n\r\n"); - } -} diff --git a/libs/http_post/https_post_wolfssl.c b/libs/http_post/https_post_wolfssl.c deleted file mode 100644 index 3875b70..0000000 --- a/libs/http_post/https_post_wolfssl.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define WEB_SERVER "192.168.1.2" -#define WEB_PORT 80 -#define WEB_URL "/esplogger/postsql.php" -#define POST_REQUEST "POST %s HTTP/1.0\r\nHost: %s\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s" - -char request[450]; -char details[150]; - - -void https_post_wolfssl_task(void * pvParameters) -{ - - - while (1) { - printf ("Suspending http_post_task\n"); - vTaskSuspend( NULL ); - printf ("Resumed http_post_task\n"); - - int sockfd; - - WOLFSSL_CTX* ctx; - - WOLFSSL* ssl; - - WOLFSSL_METHOD* method; - struct sockaddr_in servAddr; - - /* create and set up socket */ - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - memset(&servAddr, 0, sizeof(servAddr)); - servAddr.sin_family = AF_INET; - servAddr.sin_port = htons(WEB_PORT); - - /* connect to socket */ - connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)); - - /* initialize wolfssl library */ - - wolfSSL_Init(); - method = wolfTLSv1_2_client_method(); /* use TLS v1.2 */ - - /* make new ssl context */ - if ( (ctx = wolfSSL_CTX_new(method)) == NULL) { - printf("wolfSSL_CTX_new error"); - } - - /* make new wolfSSL struct */ - if ( (ssl = wolfSSL_new(ctx)) == NULL) { - printf("wolfSSL_new error"); - } - - /* Add cert to ctx */ - if (wolfSSL_CTX_load_verify_locations(ctx, "certs/ca-cert.pem", 0) !=SSL_SUCCESS) { - printf("Error loading certs/ca-cert.pem"); - } - - request[0] = "\0"; - snprintf(details, 150, post_string); - snprintf(request, 450, "POST %s HTTP/1.0\r\n" - "Host: %s\r\n" - "Content-type: application/x-www-form-urlencoded\r\n" - "Content-Length: %d\r\n\r\n" - "%s", WEB_URL, WEB_SERVER, strlen(details), details); - printf(request); - - - /* Connect wolfssl to the socket, server, then send message */ - wolfSSL_set_fd(ssl, sockfd); - wolfSSL_connect(ssl); - wolfSSL_write(ssl, request, strlen(request)); - - /* frees all data before client termination */ - wolfSSL_free(ssl); - wolfSSL_CTX_free(ctx); - wolfSSL_Cleanup(); - } -} diff --git a/libs/led_codes/component.mk b/libs/led_codes/component.mk deleted file mode 100644 index b39adc9..0000000 --- a/libs/led_codes/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -# Component makefile for led_codes - -INC_DIRS += $(led_codes_ROOT) - -led_codes_INC_DIR = $(led_codes_ROOT) -led_codes_SRC_DIR = $(led_codes_ROOT) - -$(eval $(call component_compile_rules,led_codes)) diff --git a/libs/led_codes/led_codes.c b/libs/led_codes/led_codes.c deleted file mode 100644 index da3b6a8..0000000 --- a/libs/led_codes/led_codes.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * LED Codes - * - * Copyright 2018 José A. Jiménez (@RavenSystem) - * - * 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. - */ - -#include -#include -#include -#include -#include - -#include "led_codes.h" - -#define DURATION_OFF 150 - -typedef struct led_params_t { - blinking_params_t blinking_params; - uint8_t gpio; - bool status; - ETSTimer timer; - uint8_t count; - uint32_t delay; -} led_params_t; - -led_params_t led_params; - -void led_code_run() { - led_params.status = !led_params.status; - - gpio_write(led_params.gpio, led_params.status); - - if (led_params.status == 0) { - led_params.delay = (led_params.blinking_params.duration * 1000) + 90; - } else { - led_params.delay = DURATION_OFF; - led_params.count++; - } - - sdk_os_timer_disarm(&led_params.timer); - - if (led_params.count < led_params.blinking_params.times) { - sdk_os_timer_arm(&led_params.timer, led_params.delay, 0); - } -} - -void led_code(uint8_t gpio, blinking_params_t blinking_params) { - led_params.gpio = gpio; - led_params.blinking_params = blinking_params; - - sdk_os_timer_disarm(&led_params.timer); - sdk_os_timer_setfn(&led_params.timer, led_code_run, NULL); - - led_params.status = 1; - led_params.count = 0; - - led_code_run(); -} diff --git a/libs/led_codes/led_codes.h b/libs/led_codes/led_codes.h deleted file mode 100644 index b054e13..0000000 --- a/libs/led_codes/led_codes.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * LED Codes - * - * Copyright 2018 José A. Jiménez (@RavenSystem) - * - * 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. - */ - -#pragma once - -typedef struct blinking_params_t { - uint8_t times; - uint8_t duration; -} blinking_params_t; - -#define GENERIC_ERROR (blinking_params_t){6,0} -#define SENSOR_ERROR (blinking_params_t){5,0} -#define WIFI_CONNECTED (blinking_params_t){1,2} -#define IDENTIFY_ACCESSORY (blinking_params_t){1,3} -#define RESTART_DEVICE (blinking_params_t){2,2} -#define WIFI_CONFIG_RESET (blinking_params_t){2,0} -#define EXTRA_CONFIG_RESET (blinking_params_t){2,1} -#define FUNCTION_A (blinking_params_t){1,0} -#define FUNCTION_B (blinking_params_t){2,0} -#define FUNCTION_C (blinking_params_t){3,0} -#define FUNCTION_D (blinking_params_t){4,0} - -void led_code(uint8_t gpio, blinking_params_t blinking_params); diff --git a/libs/ota/component.mk b/libs/ota/component.mk deleted file mode 100644 index 861596a..0000000 --- a/libs/ota/component.mk +++ /dev/null @@ -1,8 +0,0 @@ -# Component makefile for ota - -INC_DIRS += $(ota_ROOT) - -ota_INC_DIR = $(ota_ROOT) -ota_SRC_DIR = $(ota_ROOT) - -$(eval $(call component_compile_rules,ota)) diff --git a/libs/ota/ota-api.c b/libs/ota/ota-api.c deleted file mode 100644 index 5126078..0000000 --- a/libs/ota/ota-api.c +++ /dev/null @@ -1,75 +0,0 @@ -#include //for printf -#include -#include - -#include -#include -#include -#include - -// the first function is the ONLY thing needed for a repo to support ota after having started with ota-boot -// in ota-boot the user gets to set the wifi and the repository details and it then installs the ota-main binary - -void ota_update(void *arg) { //arg not used - rboot_set_temp_rom(1); //select the OTA main routine - sdk_system_restart(); //#include - // there is a bug in the esp SDK such that if you do not power cycle the chip after serial flashing, restart is unreliable -} - -// this function is optional to couple Homekit parameters to the sysparam variables and github parameters -unsigned int ota_read_sysparam(char **manufacturer,char **serial,char **model,char **revision) { - sysparam_status_t status; - char *value; - - status = sysparam_get_string("ota_repo", &value); - if (status == SYSPARAM_OK) { - strchr(value,'/')[0]=0; - *manufacturer=value; - *model=value+strlen(value)+1; - } else { - *manufacturer="manuf_unknown"; - *model="model_unknown"; - } - status = sysparam_get_string("ota_version", &value); - if (status == SYSPARAM_OK) { - *revision=value; - } else *revision="0.0.0"; - - uint8_t macaddr[6]; - sdk_wifi_get_macaddr(STATION_IF, macaddr); - *serial=malloc(18); - sprintf(*serial,"%02X:%02X:%02X:%02X:%02X:%02X",macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]); - - unsigned int c_hash=0; - char version[16]; - char* rev=version; - char* dot; - strncpy(rev,*revision,16); - if ((dot=strchr(rev,'.'))) {dot[0]=0; c_hash= atoi(rev); rev=dot+1;} - if ((dot=strchr(rev,'.'))) {dot[0]=0; c_hash=c_hash*1000+atoi(rev); rev=dot+1;} - c_hash=c_hash*1000+atoi(rev); - //c_hash=c_hash*10 +configuration_variant; //possible future extension - printf("manuf=\'%s\' serial=\'%s\' model=\'%s\' revision=\'%s\' c#=%d\n",*manufacturer,*serial,*model,*revision,c_hash); - return c_hash; -} - - - - -#include -#include -#include - -static ETSTimer update_timer; - -void ota_set(homekit_value_t value) { - if (value.format != homekit_format_bool) { - printf("Invalid ota-value format: %d\n", value.format); - return; - } - if (value.bool_value) { - //make a distinct light pattern or other feedback to the user = call identify routine - sdk_os_timer_setfn(&update_timer, ota_update, NULL); - sdk_os_timer_arm(&update_timer, 500, 0); //wait 0.5 seconds to trigger the reboot so gui can update and events sent - } -} diff --git a/libs/ota/ota-api.h b/libs/ota/ota-api.h deleted file mode 100644 index fd4d2e0..0000000 --- a/libs/ota/ota-api.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef __HOMEKIT_CUSTOM_CHARACTERISTICS__ -#define __HOMEKIT_CUSTOM_CHARACTERISTICS__ - -#define HOMEKIT_CUSTOM_UUID(value) (value "-0e36-4a42-ad11-745a73b84f2b") - -#define HOMEKIT_SERVICE_CUSTOM_SETUP HOMEKIT_CUSTOM_UUID("000000FF") - -#define HOMEKIT_CHARACTERISTIC_CUSTOM_OTA_TRIGGER HOMEKIT_CUSTOM_UUID("F0000001") -#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_OTA_TRIGGER(_value, ...) \ - .type = HOMEKIT_CHARACTERISTIC_CUSTOM_OTA_TRIGGER, \ - .description = "FirmwareUpdate", \ - .format = homekit_format_bool, \ - .permissions = homekit_permissions_paired_read \ - | homekit_permissions_paired_write \ - | homekit_permissions_notify, \ - .value = HOMEKIT_BOOL_(_value), \ - ##__VA_ARGS__ - -unsigned int ota_read_sysparam(char **manufacturer,char **serial,char **model,char **revision); - -void ota_update(void *arg); - -void ota_set(homekit_value_t value); - -#define API_OTA_TRIGGER HOMEKIT_CHARACTERISTIC_(CUSTOM_OTA_TRIGGER, false, .setter=ota_set) - -#endif diff --git a/src/Makefile b/src/Makefile index f40dbd0..1358388 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,29 +1,42 @@ PROGRAM = main EXTRA_COMPONENTS = \ - extras/dht \ extras/http-parser \ extras/rboot-ota \ - extras/bearssl \ - $(abspath ../components/esp-wolfssl) \ + extras/dhcpserver \ + $(abspath ../components/esp-wolfssl) \ $(abspath ../components/esp-cjson) \ - $(abspath ../components/esp-homekit) \ - $(abspath ../libs/led_codes) \ - $(abspath ../libs/check_wifi) \ - $(abspath ../libs/ota) \ - $(abspath ../libs/http_post) - - + $(abspath ../components/esp-homekit)\ + $(abspath ../components/esp-wifi-config)\ + $(abspath ../components/UDPlogger)\ + $(abspath ../components/esp-homekit-common-functions/led_codes)\ + $(abspath ../components/esp-homekit-common-functions/ota)\ + $(abspath ../components/esp-homekit-common-functions/custom_characteristics)\ + $(abspath ../components/esp-homekit-common-functions/shared_functions) FLASH_SIZE ?= 8 +FLASH_MODE ?= dout +FLASH_SPEED ?= 40 + +HOMEKIT_MAX_CLIENTS = 16 +HOMEKIT_SMALL = 0 HOMEKIT_SPI_FLASH_BASE_ADDR ?= 0x8c000 -EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS -DHOMEKIT_DEBUG +EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS +#EXTRA_CFLAGS += -DHOMEKIT_DEBUG +EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_VERIFY +EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_SETUP +EXTRA_CFLAGS += -DUDPLOG_PRINTF_TO_UDP +EXTRA_CFLAGS += -DUDPLOG_PRINTF_ALSO_SERIAL +EXTRA_CFLAGS += -DconfigUSE_TRACE_FACILITY + + +LIBS ?= m include $(SDK_PATH)/common.mk -signature: +signature: $(shell /usr/local/opt/openssl/bin/openssl sha384 -binary -out firmware/main.bin.sig firmware/main.bin) $(shell printf "%08x" `cat firmware/main.bin | wc -c`| xxd -r -p >>firmware/main.bin.sig) diff --git a/src/main.c b/src/main.c index a2877e0..2802bf5 100644 --- a/src/main.c +++ b/src/main.c @@ -9,11 +9,8 @@ #define DEVICE_NAME "Motion-Sensor" #define DEVICE_MODEL "Basic" #define DEVICE_SERIAL "12345678" -#define FW_VERSION "1.0" +#define FW_VERSION "2.0" #define MOTION_SENSOR_GPIO 12 -#define LED_GPIO 2 -#define MAX_NAME_LENGTH 63 - #include #include @@ -23,14 +20,17 @@ #include #include #include -#include #include #include #include -#include +#include + #include -#include +#include +#include +#include + // add this section to make your device OTA capable // create the extra characteristic &ota_trigger, at the end of the primary service (before the NULL) @@ -38,9 +38,15 @@ // and apply the four other parameters in the accessories_information section #include -char accessory_name[MAX_NAME_LENGTH+1]; +homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set); +homekit_characteristic_t wifi_check_interval = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_CHECK_INTERVAL, 10, .setter=wifi_check_interval_set); +/* checks the wifi is connected and flashes status led to indicated connected */ +homekit_characteristic_t task_stats = HOMEKIT_CHARACTERISTIC_(CUSTOM_TASK_STATS, false , .setter=task_stats_set); +homekit_characteristic_t ota_beta = HOMEKIT_CHARACTERISTIC_(CUSTOM_OTA_BETA, false, .setter=ota_beta_set); +homekit_characteristic_t lcm_beta = HOMEKIT_CHARACTERISTIC_(CUSTOM_LCM_BETA, false, .setter=lcm_beta_set); + homekit_characteristic_t ota_trigger = API_OTA_TRIGGER; homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, DEVICE_NAME); homekit_characteristic_t manufacturer = HOMEKIT_CHARACTERISTIC_(MANUFACTURER, DEVICE_MANUFACTURER); @@ -49,18 +55,9 @@ homekit_characteristic_t model = HOMEKIT_CHARACTERISTIC_(MODEL, homekit_characteristic_t revision = HOMEKIT_CHARACTERISTIC_(FIRMWARE_REVISION, FW_VERSION); homekit_characteristic_t motion_detected = HOMEKIT_CHARACTERISTIC_(MOTION_DETECTED, 0); -TaskHandle_t http_post_task_handle; -//TaskHandle_t https_post_wolfssl_task_handle; - -void identify_task(void *_args) { - vTaskDelete(NULL); -} +int led_off_value=1; /* global varibale to support LEDs set to 0 where the LED is connected to GND, 1 where +3.3v */ -void identify(homekit_value_t _value) { - printf("identify\n\n"); - led_code(LED_GPIO, IDENTIFY_ACCESSORY); - xTaskCreate(identify_task, "identify", 128, NULL, 2, NULL); -} +const int status_led_gpio = 2; /*set the gloabl variable for the led to be sued for showing status */ homekit_accessory_t *accessories[] = { @@ -77,7 +74,11 @@ homekit_accessory_t *accessories[] = { HOMEKIT_SERVICE(MOTION_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]){ HOMEKIT_CHARACTERISTIC(NAME, "Motion Sensor"), &motion_detected, - &ota_trigger, + &wifi_reset, + &wifi_check_interval, + &task_stats, + &ota_beta, + &lcm_beta, NULL }), NULL @@ -85,68 +86,27 @@ homekit_accessory_t *accessories[] = { NULL }; -homekit_server_config_t config = { - .accessories = accessories, - .password = "111-11-111" -}; void motion_sensor_callback(uint8_t gpio) { - - + if (gpio == MOTION_SENSOR_GPIO){ int new = 0; new = gpio_read(MOTION_SENSOR_GPIO); motion_detected.value = HOMEKIT_BOOL(new); homekit_characteristic_notify(&motion_detected, HOMEKIT_BOOL(new)); if (new == 1) { - printf("Motion Detected on %d\n", gpio); - snprintf (post_string, 150, "sql=insert into homekit.motionsensorlog (MotionSensorName, MotionDetectionState) values ('%s', 1)", accessory_name); - vTaskResume( http_post_task_handle ); + printf("Motion Detected on %d\n", gpio); } else { - printf("Motion Stopped on %d\n", gpio); - snprintf (post_string, 150, "sql=insert into homekit.motionsensorlog (MotionSensorName, MotionDetectionState) values ('%s', 0)", accessory_name); - vTaskResume( http_post_task_handle ); - -// vTaskResume( https_post_wolfssl_task_handle ); - } + printf("Motion Stopped on %d\n", gpio); + } } else { printf("Interrupt on %d", gpio); } - } -void create_accessory_name() { - - int serialLength = snprintf(NULL, 0, "%d", sdk_system_get_chip_id()); - - char *serialNumberValue = malloc(serialLength + 1); - - snprintf(serialNumberValue, serialLength + 1, "%d", sdk_system_get_chip_id()); - - int name_len = snprintf(NULL, 0, "%s-%s-%s", - DEVICE_NAME, - DEVICE_MODEL, - serialNumberValue); - - if (name_len > MAX_NAME_LENGTH) { - name_len = MAX_NAME_LENGTH; - } - - char *name_value = malloc(name_len + 1); - - snprintf(name_value, name_len + 1, "%s-%s-%s", - DEVICE_NAME, DEVICE_MODEL, serialNumberValue); - - strcpy (accessory_name, name_value); - - name.value = HOMEKIT_STRING(name_value); - serial.value = name.value; -} - - void gpio_init() { gpio_enable(MOTION_SENSOR_GPIO, GPIO_INPUT); @@ -155,24 +115,42 @@ void gpio_init() { } -void user_init(void) { - uart_set_baud(0, 115200); +void recover_from_reset (int reason) { + /* called if we restarted abnormally */ + printf ("%s: reason %d\n", __func__, reason); +} - gpio_init(); - create_accessory_name(); +void save_characteristics ( ) { + /* called by a timer function to save charactersitics */ +} + +void accessory_init_not_paired (void) { + /* initalise anything you don't want started until wifi and homekit imitialisation is confirmed, but not paired */ +} - int c_hash=ota_read_sysparam(&manufacturer.value.string_value,&serial.value.string_value, - &model.value.string_value,&revision.value.string_value); - if (c_hash==0) c_hash=1; - config.accessories[0]->config_number=c_hash; - homekit_server_init(&config); +void accessory_init (void ){ + /* initalise anything you don't want started until wifi and pairing is confirmed */ +} - xTaskCreate(checkWifiTask, "check_wifi_connection_task", 256, NULL, 2, NULL); - xTaskCreate(http_post_task, "http post task", 512, NULL, 2, &http_post_task_handle); -// xTaskCreate(https_post_wolfssl_task, "https post wolfssl task", 2048, NULL, 2, &https_post_wolfssl_task_handle); +homekit_server_config_t config = { + .accessories = accessories, + .password = "111-11-111", + .setupId = "1234", + .on_event = on_homekit_event +}; + + +void user_init(void) { + + standard_init (&name, &manufacturer, &model, &serial, &revision); + + gpio_init(); + + wifi_config_init(DEVICE_NAME, NULL, on_wifi_ready); + }