fix(sbgecom): fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions#27831
Open
tolesam wants to merge 7 commits into
Conversation
added 7 commits
July 7, 2026 11:32
The sbgECom library allocates SBG_ECOM_MAX_BUFFER_SIZE (4096) bytes on the stack in sbgEComProtocolSendStandardFrame() whenever a frame is sent (mag/air data aiding logs, configuration commands at init). The TTY work queues only have a 1728 byte stack by default (CONFIG_WQ_TTY_STACKSIZE), so every send overflowed the stack by more than 2 KB, corrupting adjacent heap memory and causing spurious reception errors. Run on the INS0 work queue instead (6000 bytes by default). Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The custom termios block applied 'c_cflag &= CSIZE', which keeps only the character size bits and clears everything else: CREAD (receiver enable) and, on Linux where the baud rate lives in c_cflag, the CBAUD bits, so the port would be reconfigured to B0 in SITL. The intent was most likely '&= ~CSIZE' followed by '|= CS8'. The whole block is redundant anyway: sbgInterfaceSerialCreate() already configures the port in raw 8N1 mode with software/hardware flow control disabled, OPOST cleared and the buffers flushed, so remove it entirely. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The mag log was sent with status = 0, which reports the magnetometers as failing built-in test, out of operating range and not calibrated, so the INS is expected to reject the measurements even when the frame is received correctly. vehicle_magnetometer contains calibrated data, so set the BIT, in-range and calibration-ok flags. The accelerometer bits are left cleared as no accelerometer data is sent with this log. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The mag log timeStamp field is defined as time since the sensor powered up, but it was filled with PX4 hrt time truncated to 32 bits, which is meaningless in the INS timebase and wraps every ~71 minutes. Unlike SbgEComLogAirData there is no time-is-delay convention for this log, so send 0 and let the INS timestamp the measurement on arrival. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
sensor_mag was published with the INS timestamp, which is the time since the INS powered up and not in the PX4 hrt timebase, so downstream consumers received wildly wrong timestamps. Use hrt_absolute_time() like the accel and gyro handlers do. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The sample and write elapsed counters were only ended on the success paths, leaving dangling perf_begin() calls on reception or send failures. Cancel them explicitly so the counters only account for completed operations. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
sendAirDataLog() and sendMagLog() already log the specific failure (payload serialization vs protocol send), so the additional generic warnings in Run() only duplicated every error message. Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solved Problem
The sbgECom driver had several defects breaking both directions of the link:
sbgECom library puts a 4096-byte buffer on a 1728-byte stack), corrupting
heap memory.
c_cflag &= CSIZE) clearedCREADand set theport to B0 in SITL.
status = 0(failing BIT, out ofrange, uncalibrated) and a bogus timestamp, so the INS rejected them.
time.
Solution
sbgInterfaceSerialCreate()already configuresraw 8N1.
and let the INS stamp on arrival.
hrt_absolute_time(), like accel/gyro.warnings.
Changelog Entry
For release notes:
Alternatives
N/A
Test coverage
publish with coherent PX4 timestamps; outgoing mag frames captured and
checked for status flags and zero timestamp.
errors; INS accepts the mag aiding logs.
Context
N/A