Skip to content

Commit

Permalink
New DDNS functionality, x509 root certificates bundle, scd4x measurem…
Browse files Browse the repository at this point in the history
…ents fix, and updated library
  • Loading branch information
ma-lwa-re committed Aug 9, 2022
1 parent a28625a commit 6bb5d61
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 79 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ set(SENSORS_SENSOR_ALTITUDE 0)
set(OTA_UPDATES ON)

# OPTIONAL: Set the project version
set(PROJECT_VER "2.4.0.3")
set(PROJECT_VER "2.4.0.4")

# OPTIONAL: Enable a dynamic DNS service provider (ON | OFF)
set(DDNS ON)

# Include Sensirion SCD4x sensors lib
include_directories(esp32-scd4x)
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ set(SENSORS_SCALE "C")
set(OTA_UPDATES ON)
# OPTIONAL: Set the project version
set(PROJECT_VER "2.4.0.2")
set(PROJECT_VER "2.4.0.4")
# OPTIONAL: Enable a dynamic DNS service provider (ON | OFF)
set(DDNS ON)
```

## Setup
Expand Down Expand Up @@ -93,6 +96,24 @@ python3 nvs_partition_gen.py generate wifi.csv wifi.bin 0x3000
esptool.py -p $ESPPORT write_flash 0x340000 wifi.bin
```

### Dynamic DNS
Dynamic DNS, or DDNS, is a DNS service that provides the option to change the IP address of one or multiple DNS records automatically when the IP address of your device is changed dynamically by your internet provider.

This feature can be enabled in the [`CMakeLists.txt`](CMakeLists.txt) file and is compatible with most of the well-known dynamic DNS providers. It is compatible with all services that allow updating a record using an `HTTPS` `GET` or `POST` request.

You can in some cases choose to set a specific IP address or leave it blank and let the server detect the incoming source IP.

Example of some configurable URLs scheme provided by `DynDNS`, `DuckDNS`, `No-Ip`, or `Freemyip`.

```
https://{user}:{updater_client_key}@members.dyndns.org/v3/update?hostname={mydomain}&myip={ip_address}
https://www.duckdns.org/update?domains={mydomain}&token={updater_client_token}&ip=
https://{username}:{password}@dynupdate.no-ip.com/nic/update?hostname={mydomain}&myip={ip_address}
https://freemyip.com/update?token={updater_client_token}&domain={mydomain}
```

The URL needs to be configured in the [`wifi.csv`](wifi.csv), by replacing the `DEFAULT_DDNS_UPDATE_URL` placeholder with your fully formatted URL. Then, generate a partition file and flash the device as explained in the [`Wi-Fi`](#wi-fi) chapter.

### Code Signing
The integrity of the application can be secure and checked using an RSA signature scheme. The binary is signed after compilation with the private key that can be generated with `espsecure.py` or `openssl`, and the corresponding public key is embedded into the binary for verification.

Expand Down Expand Up @@ -133,6 +154,8 @@ dreamdesk
│   └── scd4x.h
├── main
│   ├── CMakeLists.txt
│   ├── ddns.c
│   ├── ddns.h
│   ├── dreamdesk.c
│   ├── dreamdesk.h
│   ├── homekit.c
Expand Down Expand Up @@ -161,6 +184,7 @@ dreamdesk
- [x] PCB prototype
- [x] PCB assembly
- [x] OTA updates
- [x] DDNS updates
- [ ] NVS encryption
- [ ] HomeKit memory integration
- [x] HomeKit sensors integration
Expand Down
2 changes: 1 addition & 1 deletion lib/esp32-scd4x
Submodule esp32-scd4x updated 4 files
+5 −5 README.md
+13 −13 main.c
+91 −101 scd4x.c
+40 −31 scd4x.h
13 changes: 9 additions & 4 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ if(OTA_UPDATES)
set(INCLUDE_OTA_UPDATES ./ota.c)
endif()

if(DDNS)
set(WIFI ON)
set(INCLUDE_DDNS ./ddns.c)
endif()

if(WIFI)
set(INCLUDE_WIFI ./wifi.c)
endif()

idf_component_register(SRCS ./main.c ./dreamdesk.c ./lin.c ${INCLUDE_DESK} ${INCLUDE_WIFI}
${INCLUDE_HOME} ${INCLUDE_SENSORS} ${INCLUDE_OTA_UPDATES} INCLUDE_DIRS ".")
idf_component_register(SRCS ./main.c ./dreamdesk.c ./lin.c ${INCLUDE_DESK} ${INCLUDE_WIFI} ${INCLUDE_HOME}
${INCLUDE_SENSORS} ${INCLUDE_OTA_UPDATES} ${INCLUDE_DDNS} INCLUDE_DIRS ".")

add_definitions(-DPROJECT_NAME="${CMAKE_PROJECT_NAME}" -DPROJECT_VER="${PROJECT_VER}" -D${DESK_TYPE}
-D${HOME_AUTOMATION} -DSENSORS_${SENSORS} -DOTA_UPDATES_${OTA_UPDATES} -DWIFI_${WIFI})
add_definitions(-DPROJECT_NAME="${CMAKE_PROJECT_NAME}" -DPROJECT_VER="${PROJECT_VER}" -D${DESK_TYPE} -D${HOME_AUTOMATION}
-DSENSORS_${SENSORS} -DOTA_UPDATES_${OTA_UPDATES} -DDDNS_${DDNS} -DWIFI_${WIFI})
128 changes: 128 additions & 0 deletions main/ddns.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* MIT License
*
* Copyright (c) 2022 ma-lwa-re
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "ddns.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_client.h"
#include "esp_crt_bundle.h"

static const char *DDNS_TAG = "ddns";

esp_err_t nvs_ddns_get_str(char *key, char *value) {
esp_err_t err = nvs_flash_init_partition("wifi");

if(err != ESP_OK) {
ESP_LOGE(DDNS_TAG, "Error initializing nvs flash: %s", esp_err_to_name(err));
return ESP_ERR_INVALID_STATE;
}

nvs_handle_t nvs_handle;
err = nvs_open_from_partition("wifi", "wifi", NVS_READONLY, &nvs_handle);

if(err != ESP_OK) {
ESP_LOGE(DDNS_TAG, "Error opening NVS handle for key %s: %s", key, esp_err_to_name(err));
nvs_close(nvs_handle);
return ESP_ERR_NO_MEM;
}

size_t value_size;
err = nvs_get_str(nvs_handle, key, NULL, &value_size);
err = nvs_get_str(nvs_handle, key, value, &value_size);

if(err != ESP_OK) {
ESP_LOGE(DDNS_TAG, "Error reading key %s: %s", key, esp_err_to_name(err));
nvs_close(nvs_handle);
return ESP_ERR_NOT_FOUND;
}

nvs_close(nvs_handle);

err = nvs_flash_deinit_partition("wifi");

if(err != ESP_OK) {
ESP_LOGE(DDNS_TAG, "Error deinitializing nvs flash: %s", esp_err_to_name(err));
return ESP_ERR_INVALID_STATE;
}
return ESP_OK;
}

esp_http_client_config_t ddns_config = {
.url = "https://httpbin.org",
.user_agent = DDNS_USER_AGENT,
.crt_bundle_attach = esp_crt_bundle_attach
};

bool ddns_update(char *api_endpoint, esp_http_client_method_t http_method) {
esp_http_client_handle_t client = esp_http_client_init(&ddns_config);

ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_set_url(client, api_endpoint));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_set_method(client, http_method));

esp_err_t err = esp_http_client_perform(client);

if(err != ESP_OK) {
return false;
}

HttpStatus_Code status_code = esp_http_client_get_status_code(client);
ESP_ERROR_CHECK(esp_http_client_cleanup(client));

if((status_code & HttpStatus_Ok) == HttpStatus_Ok) {
ESP_LOGI(DDNS_TAG, "DDNS record successfully updated with return code %d", status_code);
} else {
ESP_LOGW(DDNS_TAG, "DDNS record update error with return code %d", status_code);
}

return (status_code & HttpStatus_Ok) == HttpStatus_Ok ? true : false;
}

bool ddns_get_update(char *api_endpoint) {
return ddns_update(api_endpoint, HTTP_METHOD_GET);
}

bool ddns_post_update(char *api_endpoint) {
return ddns_update(api_endpoint, HTTP_METHOD_POST);
}

void ddns_task(void *arg) {
vTaskDelay(DDNS_INIT_DELAY / portTICK_PERIOD_MS);
char api_endpoint[0xFF];

esp_err_t err = nvs_ddns_get_str("ddns", api_endpoint);

if(err != ESP_OK) {
ESP_LOGE(DDNS_TAG, "DDNS endpoint not found! Canceling record update.");
vTaskDelete(NULL);
return;
}

for(;;) {
ESP_LOGI(DDNS_TAG, "Updating the DDNS IP record...");

if(!ddns_post_update(api_endpoint)) {
ESP_LOGE(DDNS_TAG, "Error updating the DDNS record!");
}

vTaskDelay(SLEEP_DELAY_8_HOURS / portTICK_PERIOD_MS);
}
}
30 changes: 30 additions & 0 deletions main/ddns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* MIT License
*
* Copyright (c) 2022 ma-lwa-re
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>

#define DDNS_USER_AGENT ("ESP32 HTTP Client/1.0 - " PROJECT_NAME " v" PROJECT_VER)
#define SLEEP_DELAY_8_HOURS (1000 * 60 * 60 * 8)
#define DDNS_INIT_DELAY (1000 * 20)

void ddns_task(void *arg);
7 changes: 7 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#if defined(OTA_UPDATES_ON)
#include "ota.h"
#endif
#if defined(DDNS_ON)
#include "ddns.h"
#endif
#if defined(HOMEKIT)
#include "homekit.h"
#endif
Expand Down Expand Up @@ -89,6 +92,10 @@ void app_main() {
xTaskCreate(ota_task, "ota_task", OTA_STACK_SIZE, NULL, configMAX_PRIORITIES-8, NULL);
#endif

#if defined(DDNS_ON)
xTaskCreate(ddns_task, "ddns_task", OTA_STACK_SIZE, NULL, configMAX_PRIORITIES-9, NULL);
#endif

gpio_config(&(gpio_config_t){
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = ((1ULL << LED_STATUS) | (1ULL << LED_ACTIVITY))
Expand Down
48 changes: 3 additions & 45 deletions main/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
#include "ota.h"
#include "esp_log.h"
#include "esp_crt_bundle.h"

static const char *OTA_TAG = "ota_updates";

Expand All @@ -35,51 +36,8 @@ void print_app_desc(esp_app_desc_t app_desc, char *app, esp_log_level_t log_leve
esp_err_t validate_image_header(esp_https_ota_handle_t *https_ota_handle) {
esp_http_client_config_t ota_client_config = {
.url = OTA_UPDATE_URL,
/*
* Certificate:
* Data:
* Version: 3 (0x2)
* Serial Number:
* 82:10:cf:b0:d2:40:e3:59:44:63:e0:bb:63:82:8b:00
* Signature Algorithm: sha256WithRSAEncryption
* Issuer: C = US, O = Internet Security Research Group, CN = ISRG Root X1
* Validity
* Not Before: Jun 4 11:04:38 2015 GMT
* Not After : Jun 4 11:04:38 2035 GMT
* Subject: C = US, O = Internet Security Research Group, CN = ISRG Root X1
*/
.cert_pem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ota_config_t ota_config = {
Expand Down
Loading

0 comments on commit 6bb5d61

Please sign in to comment.