espilon-source/espilon_bot/components/mod_system/cmd_system.c
Eun0us 8b6c1cd53d ε - ChaCha20-Poly1305 AEAD + HKDF crypto upgrade + C3PO rewrite + docs
Crypto:
- Replace broken ChaCha20 (static nonce) with ChaCha20-Poly1305 AEAD
- HKDF-SHA256 key derivation from per-device factory NVS master keys
- Random 12-byte nonce per message (ESP32 hardware RNG)
- crypto_init/encrypt/decrypt API with mbedtls legacy (ESP-IDF v5.3.2)
- Custom partition table with factory NVS (fctry at 0x10000)

Firmware:
- crypto.c full rewrite, messages.c device_id prefix + AEAD encrypt
- crypto_init() at boot with esp_restart() on failure
- Fix command_t initializations across all modules (sub/help fields)
- Clean CMakeLists dependencies for ESP-IDF v5.3.2

C3PO (C2):
- Rename tools/c2 + tools/c3po -> tools/C3PO
- Per-device CryptoContext with HKDF key derivation
- KeyStore (keys.json) for master key management
- Transport parses device_id:base64(...) wire format

Tools:
- New tools/provisioning/provision.py for factory NVS key generation
- Updated flasher with mbedtls config for v5.3.2

Docs:
- Update all READMEs for new crypto, C3PO paths, provisioning
- Update roadmap, architecture diagrams, security sections
- Update CONTRIBUTING.md project structure
2026-02-10 21:28:45 +01:00

179 lines
4.5 KiB
C

/*
* cmd_system.c
* Refactored for new command system (flat commands)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "esp_log.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "esp_chip_info.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "command.h"
#include "utils.h"
#define TAG "SYSTEM"
/* ============================================================
* COMMAND: system_reboot
* ============================================================ */
static int cmd_system_reboot(
int argc,
char **argv,
const char *req,
void *ctx
) {
(void)argc;
(void)argv;
(void)ctx;
msg_info(TAG, "Rebooting device", req);
vTaskDelay(pdMS_TO_TICKS(250));
esp_restart();
return 0;
}
/* ============================================================
* COMMAND: system_mem
* ============================================================ */
static int cmd_system_mem(
int argc,
char **argv,
const char *req,
void *ctx
) {
(void)argc;
(void)argv;
(void)ctx;
uint32_t heap_free = esp_get_free_heap_size();
uint32_t heap_min = esp_get_minimum_free_heap_size();
size_t internal_free = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
char buf[256];
snprintf(buf, sizeof(buf),
"heap_free=%" PRIu32 " heap_min=%" PRIu32 " internal_free=%u",
heap_free,
heap_min,
(unsigned)internal_free
);
msg_info(TAG, buf, req);
return 0;
}
/* ============================================================
* COMMAND: system_uptime
* ============================================================ */
static int cmd_system_uptime(
int argc,
char **argv,
const char *req,
void *ctx
) {
(void)argc;
(void)argv;
(void)ctx;
uint64_t sec = esp_timer_get_time() / 1000000ULL;
char buf[128];
snprintf(buf, sizeof(buf),
"uptime=%llu days=%llu h=%02llu m=%02llu s=%02llu",
(unsigned long long)sec,
(unsigned long long)(sec / 86400),
(unsigned long long)((sec / 3600) % 24),
(unsigned long long)((sec / 60) % 60),
(unsigned long long)(sec % 60)
);
msg_info(TAG, buf, req);
return 0;
}
/* ============================================================
* COMMAND: system_info
* ============================================================ */
static int cmd_system_info(
int argc,
char **argv,
const char *req,
void *ctx
) {
(void)argc;
(void)argv;
(void)ctx;
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
uint32_t heap_free = esp_get_free_heap_size();
uint64_t uptime_sec = esp_timer_get_time() / 1000000ULL;
char buf[512];
int len = 0;
len += snprintf(buf + len, sizeof(buf) - len,
"chip=%s cores=%d flash=%s heap=%"PRIu32" uptime=%llus modules=",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external",
heap_free,
(unsigned long long)uptime_sec
);
// List loaded modules
int first = 1;
#ifdef CONFIG_MODULE_NETWORK
len += snprintf(buf + len, sizeof(buf) - len, "%snetwork", first ? "" : ",");
first = 0;
#endif
#ifdef CONFIG_MODULE_FAKEAP
len += snprintf(buf + len, sizeof(buf) - len, "%sfakeap", first ? "" : ",");
first = 0;
#endif
#ifdef CONFIG_MODULE_RECON
#ifdef CONFIG_RECON_MODE_CAMERA
len += snprintf(buf + len, sizeof(buf) - len, "%scamera", first ? "" : ",");
first = 0;
#endif
#ifdef CONFIG_RECON_MODE_MLAT
len += snprintf(buf + len, sizeof(buf) - len, "%smlat", first ? "" : ",");
first = 0;
#endif
#endif
if (first) {
len += snprintf(buf + len, sizeof(buf) - len, "none");
}
msg_info(TAG, buf, req);
return 0;
}
/* ============================================================
* COMMAND REGISTRATION
* ============================================================ */
static const command_t system_cmds[] = {
{ "system_reboot", NULL, NULL, 0, 0, cmd_system_reboot, NULL, false },
{ "system_mem", NULL, NULL, 0, 0, cmd_system_mem, NULL, false },
{ "system_uptime", NULL, NULL, 0, 0, cmd_system_uptime, NULL, false },
{ "system_info", NULL, NULL, 0, 0, cmd_system_info, NULL, false }
};
void mod_system_register_commands(void)
{
ESPILON_LOGI_PURPLE(TAG, "Registering system commands");
for (size_t i = 0; i < sizeof(system_cmds)/sizeof(system_cmds[0]); i++) {
command_register(&system_cmds[i]);
}
}