Skip to content

feat: support current CrossPoint host APIs - #26

Open
0x1abin wants to merge 5 commits into
crosspoint-reader:mainfrom
0x1abin:codex/crossmux-host-compat
Open

feat: support current CrossPoint host APIs#26
0x1abin wants to merge 5 commits into
crosspoint-reader:mainfrom
0x1abin:codex/crossmux-host-compat

Conversation

@0x1abin

@0x1abin 0x1abin commented Jul 29, 2026

Copy link
Copy Markdown

Summary

  • clean the PlatformIO build hook so it documents and performs only target registration
  • add the host clock, HAL, Arduino/ESP, OTA, network, and SHA-256 APIs required by current CrossPoint-based firmware
  • support reusable esp_http_client handles, request bodies, redirect/final response headers, timeouts, chunked reads, and connection state
  • add a dependency-free host compatibility self-test
  • align the PlatformIO samples and README with the implemented build flow

Why

CrossMux currently carries a second simulator implementation because the shared
simulator does not expose several APIs used by current firmware. Supplying those
host APIs here lets consumers use this repository directly as their PlatformIO
native dependency.

Redirect headers are still delivered to the firmware callback, while chunked
and persistent-connection state is derived only from the final response.

Validation

  • tests/run_host_compat_self_test.sh
  • LDFLAGS=-DNDEBUG tests/run_host_compat_self_test.sh
  • pio run -e simulator -e simulator_x3 in CrossMux using this branch as a local dependency
  • CrossMux CMake test suite: 191/191 passed

Copilot AI review requested due to automatic review settings July 29, 2026 16:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@0x1abin, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 31 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3044e879-3a03-4010-87ab-54e2b1191e47

📥 Commits

Reviewing files that changed from the base of the PR and between b1a6708 and 464d724.

📒 Files selected for processing (23)
  • README.md
  • run_simulator.py
  • sample-platformio-linux-wsl.ini
  • sample-platformio-macos.ini
  • src/Arduino.h
  • src/Client.h
  • src/HalClock.cpp
  • src/HalClock.h
  • src/HalOtaSlot.cpp
  • src/HalOtaSlot.h
  • src/IPAddress.h
  • src/NetworkClient.cpp
  • src/NetworkClient.h
  • src/SimHttpFetch.h
  • src/WiFi.h
  • src/esp_heap_caps.h
  • src/esp_http_client.h
  • src/esp_random.h
  • src/esp_wifi.h
  • src/mbedtls/sha256.h
  • src/simulator_ota.cpp
  • tests/host_compat_self_test.cpp
  • tests/run_host_compat_self_test.sh
📝 Walkthrough

Walkthrough

The simulator adds host-compatible Arduino, networking, clock, HTTP, crypto, heap, randomness, and OTA shims. PlatformIO settings are updated, and a host self-test validates clock synchronization, HTTP handling, and SHA-256 behavior.

Changes

Host compatibility

Layer / File(s) Summary
Arduino and platform compatibility shims
src/Arduino.h, src/Client.h, src/IPAddress.h, src/NetworkClient.*, src/WiFi.h, src/esp_*.h, src/HalOtaSlot.*
Adds simulator-compatible Arduino, networking, heap, random, Wi-Fi, OTA, and client APIs.
UTC clock and synchronization flow
src/HalClock.*
Adds trusted UTC conversion, validity checks, synchronization states, auto-sync controls, and synchronization execution.
HTTP transfer and response handling
src/SimHttpFetch.h, src/esp_http_client.h
Adds header parsing, configurable timeouts, transfer-state tracking, HTTP events, lifecycle updates, and cleanup.
Native SHA-256 compatibility
src/mbedtls/sha256.h
Replaces the digest stub with CommonCrypto or OpenSSL-backed SHA-256 operations and validation.
Simulator integration and host self-test
sample-platformio-*.ini, run_simulator.py, tests/*, README.md
Updates simulator flags and dependencies, documents target registration, and adds an executable host compatibility self-test covering clock, HTTP, and hashing.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant esp_http_client
  participant SimHttpFetch
  participant curl
  esp_http_client->>SimHttpFetch: fetch URL with timeout
  SimHttpFetch->>curl: execute request and capture body and headers
  curl-->>SimHttpFetch: response body, headers, and status
  SimHttpFetch-->>esp_http_client: parsed response
  esp_http_client->>esp_http_client: emit header/data events and update completion state
Loading

Possibly related PRs

Suggested reviewers: copilot, uxjulia

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed Concise and specific; it captures the main change of adding current CrossPoint host API support.
Description check ✅ Passed The description is clearly aligned with the changeset and summarizes the added host APIs, HTTP support, tests, and docs updates.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 7

🧹 Nitpick comments (3)
tests/host_compat_self_test.cpp (1)

5-11: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Make the self-test immune to NDEBUG.

Every check here is an assert, so the whole test silently passes if it is ever compiled with -DNDEBUG (e.g. someone adds release flags to the runner or reuses this file in a PlatformIO env). Forcing asserts on keeps it self-contained.

♻️ Proposed change
 `#include` <array>
+#undef NDEBUG
 `#include` <cassert>
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/host_compat_self_test.cpp` around lines 5 - 11, Make
tests/host_compat_self_test.cpp immune to NDEBUG by ensuring assertions remain
enabled within this self-test translation unit, while preserving the existing
assert-based checks and keeping the change self-contained.
sample-platformio-macos.ini (1)

32-34: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider adding -Wno-deprecated-declarations on macOS.

The new src/mbedtls/sha256.h calls CC_SHA256_*, which Apple has deprecated, so simulator builds will emit warnings for every translation unit that includes it. tests/run_host_compat_self_test.sh already passes this flag.

🔧 Proposed change
   -Wno-c++11-narrowing
+  -Wno-deprecated-declarations
   -DSIMULATOR
   -DCROSSPOINT_EMULATED=1
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sample-platformio-macos.ini` around lines 32 - 34, Add
-Wno-deprecated-declarations to the macOS simulator compiler flags alongside
-Wno-c++11-narrowing in sample-platformio-macos.ini, matching the existing
configuration used by tests/run_host_compat_self_test.sh.
tests/run_host_compat_self_test.sh (1)

8-18: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use an array for link flags to keep shellcheck quiet and splitting explicit.

Unquoted $link_flags (SC2086) relies on word splitting and also breaks if TMPDIR/LDFLAGS contain spaces.

♻️ Proposed change
-link_flags="${LDFLAGS:-}"
-if [[ "$(uname -s)" == "Linux" ]]; then
-  link_flags="$link_flags -lcrypto"
-fi
+read -r -a link_flags <<<"${LDFLAGS:-}"
+if [[ "$(uname -s)" == "Linux" ]]; then
+  link_flags+=(-lcrypto)
+fi
 
 "${CXX:-c++}" -std=gnu++20 -Wno-deprecated-declarations \
   -I"$repo_root/src" \
   "$repo_root/tests/host_compat_self_test.cpp" \
   "$repo_root/src/HalClock.cpp" \
-  $link_flags \
+  "${link_flags[@]}" \
   -o "$test_binary"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/run_host_compat_self_test.sh` around lines 8 - 18, Update the
link_flags handling in the host compatibility build script to use a shell array,
appending the Linux crypto flag as an element and expanding the array explicitly
in the compiler invocation. Preserve support for multiple flags and values
containing spaces from LDFLAGS while eliminating the unquoted link_flags
expansion.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sample-platformio-linux-wsl.ini`:
- Around line 33-34: Restore the Linux/WSL linker flags in the sample
configuration so the OpenSSL linkage includes -lssl, -lcrypto, and
-Wno-deprecated-declarations in the required order.

In `@src/Arduino.h`:
- Around line 54-57: Update both random overloads in Arduino.h to use an
ordered, range-capable uniform distribution rather than std::rand() modulo
arithmetic, supporting ranges larger than RAND_MAX without bias. Preserve the
existing return behavior for non-positive max and ensure the two-argument
overload handles min >= max before computing max - min, consistently returning
min.

In `@src/esp_http_client.h`:
- Around line 172-177: Update the failed-transfer path in the handle perform
logic to also clear responseBody, reset bodyOffset, and reset contentLength
alongside performed, complete, and statusCode. Ensure a reused handle exposes no
prior response bytes or length after perform() fails.

In `@src/esp_random.h`:
- Around line 7-17: Replace the MT19937-backed implementations in
simulatorRandomEngine, esp_random, and esp_fill_random with an operating-system
cryptographically secure random source such as getrandom, arc4random_buf, or
OpenSSL RAND_bytes. Ensure both APIs use that source for all output and handle
failures appropriately; do not retain MT19937 or document these APIs as suitable
for secrets, nonces, or key material unless cryptographic security is actually
provided.

In `@src/HalClock.cpp`:
- Around line 149-154: Update formatDate’s timestamp validation to check the raw
nowUtc() result before applying offsetQuarterHours, matching the trust gating
used by getTime/getDateTime. Return false when the unadjusted clock value is
untrusted, and only compute the offset timestamp and call toUtc for trusted
values.

In `@src/IPAddress.h`:
- Around line 24-25: Update both const and non-const IPAddress::operator[]
overloads to reject indices outside the valid 0–3 range instead of applying
modulo wrapping. Use the firmware API’s established bounds-checking or
invalid-index behavior, while preserving direct access for valid indices.
- Around line 17-21: Update IPAddress::toString so each bytes_ octet passed to
snprintf is explicitly converted with static_cast<unsigned>, matching the %u
format specifier for all four arguments.

---

Nitpick comments:
In `@sample-platformio-macos.ini`:
- Around line 32-34: Add -Wno-deprecated-declarations to the macOS simulator
compiler flags alongside -Wno-c++11-narrowing in sample-platformio-macos.ini,
matching the existing configuration used by tests/run_host_compat_self_test.sh.

In `@tests/host_compat_self_test.cpp`:
- Around line 5-11: Make tests/host_compat_self_test.cpp immune to NDEBUG by
ensuring assertions remain enabled within this self-test translation unit, while
preserving the existing assert-based checks and keeping the change
self-contained.

In `@tests/run_host_compat_self_test.sh`:
- Around line 8-18: Update the link_flags handling in the host compatibility
build script to use a shell array, appending the Linux crypto flag as an element
and expanding the array explicitly in the compiler invocation. Preserve support
for multiple flags and values containing spaces from LDFLAGS while eliminating
the unquoted link_flags expansion.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7bc76725-530f-474e-b1b0-141a00600294

📥 Commits

Reviewing files that changed from the base of the PR and between ed31bd9 and b1a6708.

📒 Files selected for processing (22)
  • README.md
  • run_simulator.py
  • sample-platformio-linux-wsl.ini
  • sample-platformio-macos.ini
  • src/Arduino.h
  • src/Client.h
  • src/HalClock.cpp
  • src/HalClock.h
  • src/HalOtaSlot.cpp
  • src/HalOtaSlot.h
  • src/IPAddress.h
  • src/NetworkClient.cpp
  • src/NetworkClient.h
  • src/SimHttpFetch.h
  • src/WiFi.h
  • src/esp_heap_caps.h
  • src/esp_http_client.h
  • src/esp_random.h
  • src/esp_wifi.h
  • src/mbedtls/sha256.h
  • tests/host_compat_self_test.cpp
  • tests/run_host_compat_self_test.sh
📜 Review details
🧰 Additional context used
📓 Path-based instructions (5)
src/**/*.{cpp,h}

📄 CodeRabbit inference engine (CLAUDE.md)

When adding an Arduino or ESP-IDF symbol, add the minimum matching stub to the corresponding header under src/, match the upstream signature, and return a sensible default.

Files:

  • src/esp_wifi.h
  • src/IPAddress.h
  • src/esp_heap_caps.h
  • src/esp_random.h
  • src/NetworkClient.cpp
  • src/Client.h
  • src/HalOtaSlot.h
  • src/Arduino.h
  • src/HalOtaSlot.cpp
  • src/NetworkClient.h
  • src/HalClock.h
  • src/HalClock.cpp
  • src/WiFi.h
  • src/SimHttpFetch.h
  • src/mbedtls/sha256.h
  • src/esp_http_client.h
src/{WebServer.cpp,WebSocketsServer.cpp,NetworkClient.cpp}

📄 CodeRabbit inference engine (CLAUDE.md)

Map firmware HTTP port 80 to http://127.0.0.1:8080/ and WebSocket port 81 to ws://127.0.0.1:8081/; CROSSPOINT_SIM_HTTP_PORT must move both ports together.

Files:

  • src/NetworkClient.cpp
src/Hal*.{cpp,h}

📄 CodeRabbit inference engine (CLAUDE.md)

Each simulator Hal*.cpp/.h must preserve the corresponding firmware HAL class's public surface. When firmware adds a HAL method, add a matching stub—usually a no-op—with the exact signature; do not invent public HAL methods.

Files:

  • src/HalOtaSlot.h
  • src/HalOtaSlot.cpp
  • src/HalClock.h
  • src/HalClock.cpp
src/Arduino.h

📄 CodeRabbit inference engine (CLAUDE.md)

Implement millis() and micros() using steady_clock, not system_clock, so wall-clock changes do not affect timing.

Files:

  • src/Arduino.h
sample-platformio-{macos,linux-wsl}.ini

📄 CodeRabbit inference engine (CLAUDE.md)

Keep macOS and Linux/WSL PlatformIO build flags synchronized when changing flags; retain architecture-correct SDL flags, and Linux/WSL OpenSSL linkage with -lssl -lcrypto -Wno-deprecated-declarations. Native Windows is unsupported.

Files:

  • sample-platformio-macos.ini
  • sample-platformio-linux-wsl.ini
🪛 ast-grep (0.45.0)
src/Arduino.h

[warning] 55-55: rand(), random(), and the *rand48 family are non-cryptographic pseudo-random number generators. Their output is predictable and must not be used for security tokens, keys, nonces, salts, or session identifiers. Use a cryptographically secure source such as getrandom(2), arc4random() / arc4random_uniform(), or RAND_bytes() from OpenSSL.
Context: random(max - min)
Note: [CWE-338] Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG).

(insecure-random-rand-c)

src/SimHttpFetch.h

[warning] 295-295: Avoid atoi/atol/atoll/atof: they perform no error detection, returning 0 on non-numeric input and invoking undefined behavior on out-of-range values. This rule flags every use of these functions regardless of data provenance. Convert strings with strtol/strtoul/strtod and check errno (and the endptr) so malformed or overflowing input is rejected.
Context: atoi(statusText.c_str())
Note: [CWE-20] Improper Input Validation.

(atoi-no-error-detection-c)

🪛 Ruff (0.16.0)
run_simulator.py

[error] 3-3: Undefined name Import

(F821)

🪛 Shellcheck (0.11.0)
tests/run_host_compat_self_test.sh

[info] 17-17: Double quote to prevent globbing and word splitting.

(SC2086)

🔇 Additional comments (23)
src/HalClock.h (1)

7-37: LGTM!

src/HalClock.cpp (1)

8-84: LGTM!

Also applies to: 95-98, 173-173

src/SimHttpFetch.h (1)

3-11: LGTM!

Also applies to: 24-25, 103-143, 233-257, 272-273, 286-309

src/esp_http_client.h (1)

3-29: LGTM!

Also applies to: 52-52, 74-85, 95-152, 178-200, 203-266, 288-315, 317-357, 367-373, 386-387

src/mbedtls/sha256.h (2)

16-23: 📐 Maintainability & Code Quality

Confirm <cstring> is included in this header.

std::memset is used here and in mbedtls_sha256_free; the prior version's <cstring> need may have come via another include. Lines 1-5 aren't in the review context.

#!/bin/bash
fd -p 'src/mbedtls/sha256.h' --exec sed -n '1,20p'

6-14: LGTM!

Also applies to: 25-62

sample-platformio-linux-wsl.ini (1)

37-37: LGTM!

Also applies to: 53-53

sample-platformio-macos.ini (1)

50-50: LGTM!

run_simulator.py (1)

1-5: LGTM!

tests/host_compat_self_test.cpp (1)

13-25: LGTM!

Also applies to: 27-117

tests/run_host_compat_self_test.sh (1)

1-7: LGTM!

Also applies to: 19-20

README.md (2)

119-121: 📐 Maintainability & Code Quality

Verify the script's executable bit.

The documented invocation relies on the file mode; otherwise the snippet should be bash tests/run_host_compat_self_test.sh.

#!/bin/bash
git ls-files -s tests/run_host_compat_self_test.sh

15-15: LGTM!

Also applies to: 38-39, 115-118, 122-122

src/Arduino.h (1)

10-23: LGTM!

src/Client.h (1)

1-5: LGTM!

src/IPAddress.h (1)

9-15: LGTM!

Also applies to: 26-30

src/NetworkClient.h (1)

10-18: LGTM!

Also applies to: 41-41

src/NetworkClient.cpp (1)

11-11: LGTM!

Also applies to: 61-63

src/WiFi.h (1)

2-2: LGTM!

Also applies to: 42-42, 182-186

src/esp_heap_caps.h (1)

1-22: LGTM!

src/esp_wifi.h (1)

13-13: LGTM!

src/HalOtaSlot.h (1)

1-27: LGTM!

src/HalOtaSlot.cpp (1)

1-13: LGTM!

Comment thread sample-platformio-linux-wsl.ini
Comment thread src/Arduino.h Outdated
Comment thread src/esp_http_client.h
Comment thread src/esp_random.h Outdated
Comment thread src/HalClock.cpp
Comment thread src/IPAddress.h
Comment thread src/IPAddress.h Outdated
@0x1abin
0x1abin force-pushed the codex/crossmux-host-compat branch from 4462f0c to 464d724 Compare July 29, 2026 16:46
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.

2 participants