This commit is contained in:
2026-02-27 16:22:11 +01:00
parent 125d11fa46
commit 09a2d1d82d
10 changed files with 218 additions and 40 deletions

View File

@@ -6,8 +6,8 @@
#include <app_version.h>
#include <zephyr/sys/crc.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/sys/reboot.h>
#include <utils.h>
#include <usb.h>
#include <protocol.h>
#include <audio.h>
@@ -41,7 +41,7 @@ void send_error(int32_t error_code)
usb_write_buffer((const uint8_t *)response, strlen(response));
}
int send_ls(const char *path)
int cmd_ls(const char *path)
{
struct fs_dir_t dirp;
struct fs_dirent entry;
@@ -65,7 +65,7 @@ int send_ls(const char *path)
return 0;
}
int send_info()
int cmd_info()
{
char info[112];
struct fs_statvfs stat;
@@ -80,7 +80,7 @@ int send_info()
return 0;
}
int put_binary_file(const char *filename, ssize_t filesize, uint32_t expected_crc32)
int cmd_put_binary_file(const char *filename, ssize_t filesize, uint32_t expected_crc32)
{
int rc;
ssize_t bytes_written = 0;
@@ -228,9 +228,7 @@ int put_binary_file(const char *filename, ssize_t filesize, uint32_t expected_cr
}
send_ok();
LOG_INF("Firmware upgrade requested, rebooting into bootloader...");
k_sleep(K_MSEC(100)); // Kurze Pause, damit die OK-Antwort noch rausgeht
while (log_process());
sys_reboot(SYS_REBOOT_COLD);
reboot_with_status(REBOOT_STATUS_FIRMWARE_UPDATE);
}
else
{
@@ -247,7 +245,7 @@ int put_binary_file(const char *filename, ssize_t filesize, uint32_t expected_cr
return 0;
}
int mkdir(const char *path) {
int cmd_mkdir(const char *path) {
int rc = fs_pm_mkdir(path);
if (rc < 0)
{
@@ -257,7 +255,7 @@ int mkdir(const char *path) {
return rc;
}
int rm(const char *path) {
int cmd_rm(const char *path) {
int rc = fs_pm_unlink(path);
if (rc < 0)
{
@@ -267,7 +265,7 @@ int rm(const char *path) {
return rc;
}
int confirm_firmware() {
int cmd_confirm_firmware() {
int rc = boot_write_img_confirmed();
if (rc < 0)
{
@@ -275,19 +273,50 @@ int confirm_firmware() {
return rc;
}
LOG_INF("Firmware confirmed successfully");
audio_play("/lfs/sys/confirm");
reboot_with_status(REBOOT_STATUS_FIRMWARE_CONFIRMED);
return 0;
}
int reboot_device() {
int cmd_reboot_device() {
LOG_INF("Rebooting device as requested by host...");
send_ok();
k_sleep(K_MSEC(100)); // Kurze Pause, damit die OK-Antwort noch rausgeht
while (log_process());
sys_reboot(SYS_REBOOT_COLD);
reboot_with_status(REBOOT_STATUS_NORMAL);
return 0; // Dieser Code wird nie erreicht, aber wir geben ihn der Vollständigkeit halber zurück
}
void cmd_play(const char *filename) {
LOG_DBG("Play command received with filename: '%s'", filename);
audio_play(filename);
}
int cmd_check(const char *param) {
LOG_DBG("Check command received with parameter: '%s'", param);
struct fs_file_t file;
fs_file_t_init(&file);
int rc = fs_pm_open(&file, param, FS_O_READ);
if (rc < 0) {
LOG_ERR("Check failed: file '%s' not found", param);
return -ENOENT;
}
uint32_t crc32 = 0;
uint8_t buffer[256];
ssize_t read;
while ((read = fs_read(&file, buffer, sizeof(buffer))) > 0)
{
crc32 = crc32_ieee_update(crc32, buffer, read);
}
fs_pm_close(&file);
if (read < 0) {
LOG_ERR("Check failed: error reading file '%s': %d", param, (int)read);
return (int)read;
}
LOG_DBG("Check successful: file '%s' has CRC32 0x%08x", param, crc32);
char response[64];
snprintf(response, sizeof(response), "CRC32 %s 0x%08x\n", param, crc32);
usb_write_buffer((const uint8_t *)response, strlen(response));
return 0;
}
void execute_current_command(void)
{
int rc;
@@ -295,7 +324,7 @@ void execute_current_command(void)
{
case CMD_LS:
LOG_DBG("Executing LS command with parameters: '%s'", buffer);
rc = send_ls((char *)buffer);
rc = cmd_ls((char *)buffer);
if (rc == 0)
{
send_ok();
@@ -311,7 +340,7 @@ void execute_current_command(void)
LOG_WRN("INFO command received with unexpected parameters: '%s'", buffer);
}
LOG_DBG("Executing INFO command");
rc = send_info();
rc = cmd_info();
if (rc == 0)
{
send_ok();
@@ -334,7 +363,7 @@ void execute_current_command(void)
break;
}
LOG_DBG("Executing PUT_BINARY_FILE command filename: '%s', filesize: %zd, crc32: 0x%08x", filename, filesize, crc32);
rc = put_binary_file(filename, filesize, crc32);
rc = cmd_put_binary_file(filename, filesize, crc32);
if (rc == 0)
{
send_ok();
@@ -348,7 +377,7 @@ void execute_current_command(void)
break;
case CMD_MKDIR:
LOG_DBG("Executing MKDIR command with parameters: '%s'", buffer);
rc = mkdir((char *)buffer);
rc = cmd_mkdir((char *)buffer);
if (rc == 0) {
send_ok();
}
@@ -358,7 +387,7 @@ void execute_current_command(void)
break;
case CMD_RM:
LOG_DBG("Executing RM command with parameters: '%s'", buffer);
rc = rm((char *)buffer);
rc = cmd_rm((char *)buffer);
if (rc == 0) {
send_ok();
audio_refresh_file_count(); // Nach erfolgreichem Löschen die Anzahl der verfügbaren Audiodateien aktualisieren
@@ -369,22 +398,36 @@ void execute_current_command(void)
break;
case CMD_CONFIRM:
LOG_DBG("Executing CONFIRM command");
rc = confirm_firmware();
if (rc == 0)
{ send_ok();
}
else
{
rc = cmd_confirm_firmware();
if (rc != 0) {
send_error(rc);
break;
}
send_ok();
break;
case CMD_REBOOT:
LOG_DBG("Executing REBOOT command");
rc = reboot_device();
rc = cmd_reboot_device();
if (rc != 0) {
send_error(rc);
}
break;
case CMD_PLAY:
LOG_DBG("Executing PLAY command");
cmd_play((char *)buffer);
send_ok();
break;
case CMD_CHECK:
LOG_DBG("Executing CHECK command");
rc = cmd_check((char *)buffer);
if (rc == 0) {
send_ok();
}
else
{
send_error(rc);
}
break;
default:
LOG_ERR("No execution logic for command %d", current_command);
send_error(ENOSYS);
@@ -435,15 +478,27 @@ protocol_state_t reading_command(uint8_t byte)
{
LOG_DBG("Received RM command");
current_command = CMD_RM;
} else if (strcmp((char *)buffer, "confirm") == 0)
}
else if (strcmp((char *)buffer, "confirm") == 0)
{
LOG_DBG("Received CONFIRM command");
current_command = CMD_CONFIRM;
} else if (strcmp((char *)buffer, "reboot") == 0)
}
else if (strcmp((char *)buffer, "reboot") == 0)
{
LOG_DBG("Received REBOOT command");
current_command = CMD_REBOOT;
}
else if (strcmp((char *)buffer, "play") == 0)
{
LOG_DBG("Received PLAY command");
current_command = CMD_PLAY;
}
else if (strcmp((char *)buffer, "check") == 0)
{
LOG_DBG("Received CHECK command");
current_command = CMD_CHECK;
}
else
{
LOG_DBG("Unknown command: %s", buffer);