SolarEnergy/esp32_device/main/main.c

108 lines
2.7 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);
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-18 18:13:03 +00:00
// Check current device enrollment status
ESP_LOGI(TAG, "Check enrollment status");
int status = secure_api_get_device_enrollment_status();
printf("Current enrollment status: %d\n", status);
2024-08-17 15:40:14 +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
}