Skip to content

Refactor platform ports for bare-metal support#23

Merged
DemonGiggle merged 1 commit into
mainfrom
refactor/baremetal-platform
May 16, 2026
Merged

Refactor platform ports for bare-metal support#23
DemonGiggle merged 1 commit into
mainfrom
refactor/baremetal-platform

Conversation

@DemonGiggle
Copy link
Copy Markdown
Owner

@DemonGiggle DemonGiggle commented May 16, 2026

Summary

  • split shared runtime sources into src/core and platform-specific ports into src/platform/{posix,freertos,baremetal}
  • add EC_PLATFORM=BAREMETAL with UART HAL, socket HAL, and direct-MMIO support
  • preserve the FreeRTOS task entry point in the FreeRTOS platform port
  • update README/spec/plan docs and add a bare-metal CI build matrix entry

Verification

  • cmake -S . -B build-posix -DEC_PLATFORM=POSIX -DEC_ENABLE_TLS=OFF
  • cmake --build build-posix
  • ctest --test-dir build-posix --output-on-failure
  • cmake -S . -B build-host-sim -DEC_PLATFORM=POSIX -DEC_HOST_SIM=ON -DEC_ENABLE_TLS=OFF
  • cmake --build build-host-sim
  • ctest --test-dir build-host-sim --output-on-failure
  • cmake -S . -B build-baremetal -DEC_PLATFORM=BAREMETAL -DEC_ENABLE_TLS=OFF
  • cmake --build build-baremetal
  • cmake -S . -B build-baremetal-tls -DEC_PLATFORM=BAREMETAL -DEC_ENABLE_TLS=ON
  • cmake --build build-baremetal-tls

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the project to support bare-metal targets by separating core logic from platform-specific code and introducing HAL bridges for UART and sockets. Feedback focuses on optimizing performance by caching socket options, preventing CPU starvation in FreeRTOS tasks, and improving code safety and style through defensive pointer checks and consistent use of NULL.

ec_io_init(&ec_io_telnet_ops);
run_agent_loop(&config, EC_CONFIG_MODEL);

for (;;) {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The task enters a busy-wait loop if the agent loop exits. In a FreeRTOS environment, this will starve lower-priority tasks from executing if this task has a higher priority. It is recommended to delete the task or use a blocking delay to yield the CPU.

Suggested change
for (;;) {}
vTaskDelete(NULL);

struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
setsockopt(sock->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Calling setsockopt on every recv is inefficient as it involves a system call. Since the timeout is often constant for a session, consider caching the current timeout value in the ec_socket structure and only invoking setsockopt if the requested timeout_ms has changed.


int ec_socket_send(ec_socket_t *sock, const void *data, size_t len)
{
if (!sock || !sock->in_use || !s_hal || !s_hal->send) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this bare-metal port uses a static singleton s_socket, it is safer to verify that the provided sock pointer actually matches the address of the singleton. This provides a defensive guard against callers passing invalid or uninitialized pointers.

Suggested change
if (!sock || !sock->in_use || !s_hal || !s_hal->send) {
if (sock != &s_socket || !sock->in_use || !s_hal || !s_hal->send) {


#include <string.h>

static const ec_io_uart_hal_t *s_uart_hal = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use NULL instead of 0 for pointer initialization to improve code clarity and follow standard C idioms for pointer types.

Suggested change
static const ec_io_uart_hal_t *s_uart_hal = 0;
static const ec_io_uart_hal_t *s_uart_hal = NULL;

int in_use;
};

static const ec_socket_baremetal_hal_t *s_hal = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use NULL instead of 0 for pointer initialization to adhere to standard C style conventions.

Suggested change
static const ec_socket_baremetal_hal_t *s_hal = 0;
static const ec_socket_baremetal_hal_t *s_hal = NULL;

ec_socket_t *ec_socket_connect(const char *host, uint16_t port, int use_tls)
{
if (!s_hal || !s_hal->connect || s_socket.in_use) {
return 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function returns a pointer type (ec_socket_t *), so it should return NULL instead of 0 for consistency and readability.

Suggested change
return 0;
return NULL;

@DemonGiggle DemonGiggle merged commit f0c2f85 into main May 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant