SolarEnergy/esp32_device/main/main.c

74 lines
1.6 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 "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");
if (storage_init() == false)
{
ESP_LOGE(TAG, "Failed to init storage!\n");
reboot();
}
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);
if (crypto_gen_priv_key())
{
ESP_LOGI(TAG, "Generated device private key!\n");
}
ESP_LOGI(TAG, "Device private key:\n");
crypto_print_priv_key();
char *csr = crypto_get_csr();
ESP_LOGI(TAG, "Current CSR:\n%s\n", csr);
free(csr);
ESP_LOGI(TAG, "Initialize network\n");
ethernet_init();
ethernet_wait_for_network();
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);
}
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);
system_sleep(120);
reboot();
}