upload mutimodal

This commit is contained in:
Summer
2025-06-11 04:55:38 -07:00
parent b5c6bc3765
commit 3a70577223
41 changed files with 2883 additions and 143 deletions

View File

@@ -1,8 +1,11 @@
#include "SerialManager.hpp"
#include "esp_log.h"
#include "main_globals.hpp"
#define BUF_SIZE (1024)
SerialManager::SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle) : commandManager(commandManager), timerHandle(timerHandle) {
SerialManager::SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle, std::shared_ptr<ProjectConfig> deviceConfig)
: commandManager(commandManager), timerHandle(timerHandle), deviceConfig(deviceConfig) {
this->data = static_cast<uint8_t *>(malloc(BUF_SIZE));
this->temp_data = static_cast<uint8_t *>(malloc(256));
}
@@ -20,9 +23,6 @@ void SerialManager::try_receive()
int current_position = 0;
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
if (len) {
esp_timer_stop(*timerHandle);
}
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
while (len)
@@ -38,19 +38,73 @@ void SerialManager::try_receive()
data[current_position] = '\0';
current_position = 0;
// Notify main that a command was received during startup
notify_startup_command_received();
const auto result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(this->data)));
const auto resultMessage = result.getResult();
usb_serial_jtag_write_bytes(resultMessage.c_str(), resultMessage.length(), 1000 / 20);
esp_timer_start_once(*timerHandle, 30000000); // 30s
}
}
void SerialManager::send_heartbeat()
{
// Get the MAC address as unique identifier
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
// Format as serial number string
char serial_number[18];
sprintf(serial_number, "%02X%02X%02X%02X%02X%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// Create heartbeat JSON with serial number
char heartbeat[128];
sprintf(heartbeat, "{\"heartbeat\":\"openiris_setup_mode\",\"serial\":\"%s\"}\n", serial_number);
usb_serial_jtag_write_bytes(heartbeat, strlen(heartbeat), 1000 / 20);
}
bool SerialManager::should_send_heartbeat()
{
// Always send heartbeat during startup delay or if no WiFi configured
extern bool startupCommandReceived;
extern esp_timer_handle_t startupTimerHandle;
// If startup timer is still running, always send heartbeat
if (startupTimerHandle != nullptr) {
return true;
}
// If in heartbeat mode after startup, continue sending
if (startupCommandReceived) {
return true;
}
// Otherwise, only send if no WiFi credentials configured
const auto wifiConfigs = deviceConfig->getWifiConfigs();
return wifiConfigs.empty();
}
void HandleSerialManagerTask(void *pvParameters)
{
auto const serialManager = static_cast<SerialManager *>(pvParameters);
TickType_t lastHeartbeat = xTaskGetTickCount();
const TickType_t heartbeatInterval = pdMS_TO_TICKS(2000); // 2 second heartbeat
while (true)
{
serialManager->try_receive();
// Send heartbeat every 2 seconds, but only if no WiFi credentials are set
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastHeartbeat) >= heartbeatInterval) {
if (serialManager->should_send_heartbeat()) {
serialManager->send_heartbeat();
}
lastHeartbeat = currentTime;
}
vTaskDelay(pdMS_TO_TICKS(50)); // Small delay to prevent busy waiting
}
}

View File

@@ -6,6 +6,7 @@
#include <string>
#include <memory>
#include <CommandManager.hpp>
#include <ProjectConfig.hpp>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
@@ -15,17 +16,21 @@
#include "driver/usb_serial_jtag.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "esp_vfs_dev.h"
#include "esp_mac.h"
class SerialManager
{
public:
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle);
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle, std::shared_ptr<ProjectConfig> deviceConfig);
void setup();
void try_receive();
void send_heartbeat();
bool should_send_heartbeat();
private:
std::shared_ptr<CommandManager> commandManager;
esp_timer_handle_t *timerHandle;
std::shared_ptr<ProjectConfig> deviceConfig;
uint8_t *data;
uint8_t *temp_data;
};