SolarEnergy/esp32_device/main/main.c

172 lines
4.6 KiB
C
Raw Normal View History

2024-07-16 19:05:20 +00:00
#include <stdio.h>
#include "esp_system.h"
2024-08-17 15:19:47 +00:00
#include "esp_log.h"
2024-07-16 19:05:20 +00:00
2024-07-27 13:31:17 +00:00
#include "dev_name.h"
#include "storage.h"
2024-07-27 14:15:35 +00:00
#include "system.h"
2024-07-27 14:34:41 +00:00
#include "crypto.h"
2024-08-17 11:49:55 +00:00
#include "unsecure_api.h"
2024-08-18 18:13:03 +00:00
#include "secure_api.h"
2024-08-17 15:19:47 +00:00
#include "ethernet.h"
2024-08-18 15:40:41 +00:00
#include "constants.h"
2024-08-17 15:19:47 +00:00
static const char *TAG = "main";
2024-07-27 13:31:17 +00:00
2024-07-16 19:05:20 +00:00
void app_main(void)
{
2024-08-17 15:19:47 +00:00
esp_log_level_set("*", ESP_LOG_VERBOSE);
2024-08-18 19:01:34 +00:00
system_show_free_memory();
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device");
2024-07-27 13:31:17 +00:00
2024-08-18 18:13:03 +00:00
// Initialize storage
2024-07-27 13:31:17 +00:00
if (storage_init() == false)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, "Failed to init storage!\n");
2024-07-27 14:15:35 +00:00
reboot();
2024-07-27 13:31:17 +00:00
}
2024-08-18 18:13:03 +00:00
// Give a name to the device
2024-07-27 14:15:35 +00:00
if (dev_generate_name())
2024-07-27 13:31:17 +00:00
{
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Generated a new device name\n");
2024-07-27 13:31:17 +00:00
}
2024-07-27 14:34:41 +00:00
char *name = dev_name();
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Dev name: %s\n", name);
2024-07-27 14:34:41 +00:00
free(name);
2024-08-18 18:13:03 +00:00
// Generate private key, if needed
2024-07-27 14:15:35 +00:00
if (crypto_gen_priv_key())
{
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Generated device private key!\n");
2024-07-27 14:15:35 +00:00
}
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Device private key:\n");
2024-08-15 11:32:01 +00:00
crypto_print_priv_key();
2024-07-27 14:15:35 +00:00
2024-08-18 18:13:03 +00:00
// Show current private key
2024-08-16 09:51:33 +00:00
char *csr = crypto_get_csr();
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Current CSR:\n%s\n", csr);
2024-08-16 09:51:33 +00:00
free(csr);
2024-08-18 18:13:03 +00:00
// Initialize network stack
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Initialize network\n");
ethernet_init();
2024-08-17 15:40:14 +00:00
ethernet_wait_for_network();
2024-08-17 15:19:47 +00:00
2024-08-18 18:13:03 +00:00
// Get if secure origin endpoint is known
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Check secure origin\n");
2024-08-18 15:40:41 +00:00
if (storage_get_secure_origin(NULL) == 0)
{
char *sec_ori = unsecure_api_get_secure_origin();
if (!sec_ori)
{
ESP_LOGE(TAG, "Failed to fetch secure origin!");
reboot();
}
storage_set_secure_origin(sec_ori);
free(sec_ori);
}
2024-08-18 18:13:03 +00:00
// Print secure origin endpoint for debugging purposes
2024-08-18 15:40:41 +00:00
ESP_LOGI(TAG, "Get secure origin\n");
char *sec_ori = calloc(SEC_ORIG_LEN, 1);
assert(storage_get_secure_origin(sec_ori) > 0);
ESP_LOGI(TAG, "Current secure origin: %s", sec_ori);
free(sec_ori);
2024-08-17 11:49:55 +00:00
2024-08-18 18:13:03 +00:00
// Check if root CA is available locally
2024-08-18 17:42:40 +00:00
ESP_LOGI(TAG, "Check root CA");
if (storage_get_root_ca(NULL) == 0)
{
char *root_ca = unsecure_api_get_root_ca();
if (!root_ca)
{
ESP_LOGE(TAG, "Failed to fetch root CA!");
reboot();
}
storage_set_root_ca(root_ca);
free(root_ca);
}
2024-08-18 18:13:03 +00:00
// Print root CA for debugging purposes
2024-08-18 17:42:40 +00:00
ESP_LOGI(TAG, "Get root CA");
char *root_ca = calloc(ROOT_CA_MAX_BYTES, 1);
assert(storage_get_root_ca(root_ca) > 0);
ESP_LOGI(TAG, "Current root CA:\n%s", root_ca);
free(root_ca);
2024-08-23 19:00:18 +00:00
bool validated = false;
while (!validated)
{
// Check current device enrollment status
ESP_LOGI(TAG, "Check enrollment status");
enum DevEnrollmentStatus status = secure_api_get_device_enrollment_status();
ESP_LOGI(TAG, "Current enrollment status: %d\n", status);
switch (status)
{
case DevEnrollError:
ESP_LOGE(TAG, "Failed to retrieve device enrollment status!");
break;
case DevEnrollPending:
ESP_LOGI(TAG, "Device enrolled, but not validated yet. Please accept device on central system web UI");
break;
case DevEnrollValidated:
ESP_LOGI(TAG, "Device enrolled and validated. Ready to operate!");
validated = true;
break;
case DevEnrollUnknown:
ESP_LOGI(TAG, "Device unknown, need to enroll!");
2024-08-28 22:09:47 +00:00
// Remove certificate if present
storage_set_dev_cert("");
2024-08-23 19:00:18 +00:00
2024-08-23 21:06:14 +00:00
// Enroll device
ESP_LOGI(TAG, "Enroll device");
2024-08-23 19:00:18 +00:00
if (secure_api_enroll_device() != 0)
{
ESP_LOGE(TAG, "Failed to enroll device!");
reboot();
}
2024-08-23 21:06:14 +00:00
ESP_LOGI(TAG, "Requested device enrollment.");
2024-08-23 19:00:18 +00:00
break;
}
// Wait before next try
2024-08-28 22:09:47 +00:00
if (!validated)
system_sleep(60);
2024-08-23 19:00:18 +00:00
};
2024-08-28 22:09:47 +00:00
// Retrieve device certificate if missing
ESP_LOGI(TAG, "Check device certificate");
2024-08-28 22:27:06 +00:00
if (storage_get_dev_cert(NULL) < 2)
2024-08-28 22:09:47 +00:00
{
char *dev_cert = secure_api_get_dev_certificate();
if (!dev_cert)
{
ESP_LOGE(TAG, "Failed to fetch device certificate!");
reboot();
}
storage_set_dev_cert(dev_cert);
free(dev_cert);
}
// Print device certificate for debugging purposes
ESP_LOGI(TAG, "Get device certificate");
char *dev_certificate = calloc(DEV_CERT_MAX_BYTES, 1);
assert(storage_get_dev_cert(dev_certificate) > 0);
ESP_LOGI(TAG, "Current device certificate:\n%s", dev_certificate);
free(dev_certificate);
2024-08-18 18:13:03 +00:00
2024-08-23 19:00:18 +00:00
ESP_LOGI(TAG, "Starting main loop");
2024-08-18 19:04:23 +00:00
system_sleep(120);
2024-08-17 15:19:47 +00:00
2024-07-27 14:34:41 +00:00
reboot();
2024-07-16 19:05:20 +00:00
}