-
Notifications
You must be signed in to change notification settings - Fork 2.1k
drivers/slipdev: Refactor parser state machine for readability #21953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,7 +85,7 @@ extern "C" { | |
| * sized packets. | ||
| */ | ||
| #ifdef CONFIG_SLIPDEV_BUFSIZE_EXP | ||
| #define CONFIG_SLIPDEV_BUFSIZE (1<<CONFIG_SLIPDEV_BUFSIZE_EXP) | ||
| #define CONFIG_SLIPDEV_BUFSIZE (1 << CONFIG_SLIPDEV_BUFSIZE_EXP) | ||
| #endif | ||
|
|
||
| #ifndef CONFIG_SLIPDEV_BUFSIZE | ||
|
|
@@ -94,15 +94,26 @@ extern "C" { | |
| /** @} */ | ||
|
|
||
| /** | ||
| * @name Device state definitions | ||
| * @anchor drivers_slipdev_states | ||
| * @{ | ||
| * @brief States for the slipdev and its parser | ||
| */ | ||
| enum { | ||
| typedef enum { | ||
| /** | ||
| * @brief Device is in no mode (currently did not receiving any data frame) | ||
| * @brief Device is in no mode (currently not receiving any frame), this is the idle state. | ||
| * | ||
| * Waits for any byte, if the byte is a valid frame start byte (diagnostic, configuration, | ||
| * IP packet), it starts a new frame of the respective type and switches to the corresponding | ||
| * state. If the byte is a frame end byte, it is a no-op (we received an empty frame). | ||
| * If the byte has any other value, an unknown frame type is assumed. The state switches | ||
| * to UNKNOWN state. | ||
| */ | ||
| SLIPDEV_STATE_NONE = 0, | ||
| /** | ||
| * @brief Device discards incoming data. | ||
| * | ||
| * It switches back to the NONE (idle) state once the unknown frame is ended via a frame | ||
| * end byte. | ||
| */ | ||
| SLIPDEV_STATE_UNKNOWN, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference in behavior to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For someone who spotted the tiniest white space issue, you surely missed an entire gigantic path in the state machine 😄 State
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok so do I get that right:
So you lose the next frame if you lose the END byte, but you also don't get garbage frames if the START bytes is inside another frame where you missed the start sequence.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Almost, right. "Any other byte starts an
Yes. One could also do un-escaping here (
Yes. Which is roughly equal to the regular frames. You don't react on a
Yes. Equal behaviour to the regular frames.
Yes. True for all frames. And has also been true before my changes. If you missed the END byte, your parsing is out of sync and will produce a garbage frame until it re-syncs with the next END byte.
Yes. This is among the biggest reasons to have this extra UNKNOWN frame type. Serial connections do not sync, right? They just stream. So it is actually common / likely to be in the middle of a frame when physically connecting the uarts. In the past, where an unknown start byte always started an IP packet, this resulted in gnrc being sad about unparsable packets. Another reason is future proofing. Should slipmux evolve and include a new frame type like DNS or IPv7 is invented and send via slip: This will result in sane behaviour, which is silent discarding. |
||
| /** | ||
| * @brief Device writes handles data as network device | ||
| */ | ||
|
|
@@ -135,8 +146,7 @@ enum { | |
| * @brief Device is in sleep mode | ||
| */ | ||
| SLIPDEV_STATE_SLEEP, | ||
| }; | ||
| /** @} */ | ||
| } slipdev_state_t; | ||
|
|
||
| /** | ||
| * @brief Configuration parameters for a slipdev | ||
|
|
@@ -163,15 +173,15 @@ typedef struct { | |
| uint8_t rxmem[CONFIG_SLIPDEV_BUFSIZE]; /**< memory used by RX buffer */ | ||
|
|
||
| #if IS_USED(MODULE_SLIPDEV_CONFIG) | ||
| chunk_ringbuf_t rb_config; /**< Ringbuffer stores received configuration frames */ | ||
| uint8_t rxmem_config[CONFIG_SLIPDEV_BUFSIZE]; /**< memory used by RX buffer */ | ||
| kernel_pid_t coap_server_pid; /**< The PID of the CoAP server */ | ||
| chunk_ringbuf_t rb_config; /**< Ringbuffer stores received configuration frames */ | ||
| uint8_t rxmem_config[CONFIG_SLIPDEV_BUFSIZE]; /**< memory used by RX buffer */ | ||
| kernel_pid_t coap_server_pid; /**< The PID of the CoAP server */ | ||
| #endif | ||
| /** | ||
| * @brief Device state | ||
| * @see [Device state definitions](@ref drivers_slipdev_states) | ||
| * @see [Device state definitions](@ref slipdev_state_t) | ||
| */ | ||
| uint8_t state; | ||
| slipdev_state_t state; | ||
| } slipdev_t; | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.