108 lines
2.7 KiB
C
Executable File
108 lines
2.7 KiB
C
Executable File
#include <stdio.h>
|
|
#include "esp_system.h"
|
|
#include "esp_log.h"
|
|
|
|
#include "dev_name.h"
|
|
#include "storage.h"
|
|
#include "system.h"
|
|
#include "crypto.h"
|
|
#include "unsecure_api.h"
|
|
#include "secure_api.h"
|
|
#include "ethernet.h"
|
|
#include "constants.h"
|
|
|
|
static const char *TAG = "main";
|
|
|
|
void app_main(void)
|
|
{
|
|
esp_log_level_set("*", ESP_LOG_VERBOSE);
|
|
|
|
ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device");
|
|
|
|
// Initialize storage
|
|
if (storage_init() == false)
|
|
{
|
|
ESP_LOGE(TAG, "Failed to init storage!\n");
|
|
reboot();
|
|
}
|
|
|
|
// Give a name to the device
|
|
if (dev_generate_name())
|
|
{
|
|
ESP_LOGI(TAG, "Generated a new device name\n");
|
|
}
|
|
|
|
char *name = dev_name();
|
|
ESP_LOGI(TAG, "Dev name: %s\n", name);
|
|
free(name);
|
|
|
|
// Generate private key, if needed
|
|
if (crypto_gen_priv_key())
|
|
{
|
|
ESP_LOGI(TAG, "Generated device private key!\n");
|
|
}
|
|
ESP_LOGI(TAG, "Device private key:\n");
|
|
crypto_print_priv_key();
|
|
|
|
// Show current private key
|
|
char *csr = crypto_get_csr();
|
|
ESP_LOGI(TAG, "Current CSR:\n%s\n", csr);
|
|
free(csr);
|
|
|
|
// Initialize network stack
|
|
ESP_LOGI(TAG, "Initialize network\n");
|
|
ethernet_init();
|
|
ethernet_wait_for_network();
|
|
|
|
// Get if secure origin endpoint is known
|
|
ESP_LOGI(TAG, "Check secure origin\n");
|
|
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);
|
|
}
|
|
|
|
// Print secure origin endpoint for debugging purposes
|
|
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);
|
|
|
|
// Check if root CA is available locally
|
|
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);
|
|
}
|
|
|
|
// Print root CA for debugging purposes
|
|
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);
|
|
|
|
// 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);
|
|
|
|
system_sleep(120);
|
|
|
|
reboot();
|
|
}
|