|
ether2ser 0.1.0
Ethernet <-> synchronous V.24 bridge firmware for RP2040 + W5500
|
#include "system/event_loop.h"#include <inttypes.h>#include <stdbool.h>#include <stdint.h>#include <stdio.h>#include <string.h>#include "hardware/watchdog.h"#include "pico/time.h"#include "pico/types.h"#include "drivers/pio_tx_rx_driver.h"#include "drivers/tx_queue.h"#include "drivers/w5500_driver.h"#include "protocol/hdlc_common.h"#include "protocol/hdlc_decoder.h"#include "protocol/hdlc_sync.h"#include "system/app_context.h"#include "system/cli_usb_cdc.h"#include "system/common.h"#include "system/error.h"#include "system/event_dispatch.h"#include "system/event_queue.h"
Go to the source code of this file.
Data Structures | |
| struct | event_loop_runtime_t |
Macros | |
| #define | MAIN_LOOP_SLEEP_MS 1 |
| #define | MAIN_LOOP_SLEEP_US 50 |
| #define | TX_QUEUE_DRAIN_CHUNK_SIZE 32U |
| #define | TX_QUEUE_HIGH_WM 48U |
| #define | TX_QUEUE_LOW_WM 12U |
| #define | EVENT_LOOP_MAX_EVENTS_AT_ONCE 20 |
| #define | EVENT_LOOP_MAX_EVENTS_WHILE_TX_ACTIVE 2 |
| #define | HDLC_DECODE_FAIL_STREAK_LIMIT 4U |
| #define | HDLC_SYNC_IDLE_TIMEOUT_US 20000U |
| #define | HDLC_SYNC_NO_PROGRESS_MAX_BYTES 2048U |
| #define | HDLC_SYNC_NO_PROGRESS_MAX_BYTES_EXTERNAL 12288 |
Functions | |
| static void | print_prompt (app_ctx_t *app) |
| static bool | event_loop_should_drop_hunt_idle_byte (app_ctx_t *app, uint8_t rx_byte, uint32_t *idle_run_length) |
| static void | drain_rx_until_empty (app_ctx_t *app, event_loop_runtime_t *runtime, size_t *rx_drained) |
| static void | update_rx_drain_stats (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime, const size_t rx_drained_early, const uint64_t now_us) |
| static void | poll_and_enqueue_udp_rx (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime) |
| static size_t | poll_tx_pipeline (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime) |
| static void | poll_hdlc_idle_timeout (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime, const uint64_t now_us) |
| static void | decode_hdlc_to_udp_tx (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime) |
| static void | drain_hdlc_frames_to_udp (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime) |
| static void | poll_hdlc_no_progress (app_ctx_t *app, event_loop_runtime_t *event_loop_runtime) |
| static void | update_statistics (app_ctx_t *app) |
| static void | poll_and_dispatch_events (app_ctx_t *app) |
| static void | update_udp_rx_throttle_state (app_ctx_t *app, event_loop_runtime_t *runtime) |
| void | event_loop (app_ctx_t *app) |
| Run the main application polling loop. | |
| #define EVENT_LOOP_MAX_EVENTS_AT_ONCE 20 |
Definition at line 49 of file event_loop.c.
| #define EVENT_LOOP_MAX_EVENTS_WHILE_TX_ACTIVE 2 |
Definition at line 50 of file event_loop.c.
| #define HDLC_DECODE_FAIL_STREAK_LIMIT 4U |
Definition at line 51 of file event_loop.c.
| #define HDLC_SYNC_IDLE_TIMEOUT_US 20000U |
Definition at line 52 of file event_loop.c.
| #define HDLC_SYNC_NO_PROGRESS_MAX_BYTES 2048U |
Definition at line 53 of file event_loop.c.
| #define HDLC_SYNC_NO_PROGRESS_MAX_BYTES_EXTERNAL 12288 |
Definition at line 54 of file event_loop.c.
| #define MAIN_LOOP_SLEEP_MS 1 |
Definition at line 42 of file event_loop.c.
| #define MAIN_LOOP_SLEEP_US 50 |
Definition at line 43 of file event_loop.c.
| #define TX_QUEUE_DRAIN_CHUNK_SIZE 32U |
Definition at line 45 of file event_loop.c.
| #define TX_QUEUE_HIGH_WM 48U |
Definition at line 46 of file event_loop.c.
| #define TX_QUEUE_LOW_WM 12U |
Definition at line 47 of file event_loop.c.
|
static |
Definition at line 212 of file event_loop.c.
References app_ctx_t::accumulator, HDLC_SYNC_ACCUMULATOR_T::align_shift_right, HDLC_SYNC_ACCUMULATOR_T::bit_offset, HDLC_SYNC_ACCUMULATOR_T::candidate_end, HDLC_SYNC_ACCUMULATOR_T::candidate_start, app_ctx_t::destination_config, hdlc_decode(), payload_statistics_t::hdlc_decode_fail, event_loop_runtime_t::hdlc_decode_fail_streak, HDLC_DECODE_FAIL_STREAK_LIMIT, payload_statistics_t::hdlc_decode_ok, HDLC_FLAG_BYTE, payload_statistics_t::hdlc_frame_ready, hdlc_sync_acc_consume_candidate(), hdlc_sync_acc_init(), event_loop_runtime_t::last_frame_ready_bytes, UDP_FRAME_T::length, HDLC_FRAME_T::length, LOG_DEBUG, UDP_FRAME_T::payload, HDLC_FRAME_T::payload, HDLC_SYNC_ACCUMULATOR_T::position, HDLC_SYNC_ACCUMULATOR_T::processed, app_ctx_t::reconstructed_frame, payload_statistics_t::resync_hard_fail_count, payload_statistics_t::serial_rx_bytes, app_ctx_t::stats, TX_BUF_SIZE, app_ctx_t::tx_frame_buffer, payload_statistics_t::udp_tx_frames, w5500_udp_tx(), and event_loop_runtime_t::work_done.
Referenced by drain_hdlc_frames_to_udp().


|
static |
Definition at line 257 of file event_loop.c.
References app_ctx_t::accumulator, decode_hdlc_to_udp_tx(), E2S_ERR_HDLC_ACC_FRAME_READY, E2S_OK, HDLC_FLAG_BYTE, hdlc_sync_acc_init(), hdlc_sync_acc_poll(), HDLC_FRAME_T::length, and app_ctx_t::reconstructed_frame.
Referenced by event_loop().


|
static |
Definition at line 106 of file event_loop.c.
References app_ctx_t::accumulator, event_loop_should_drop_hunt_idle_byte(), hdlc_sync_acc_process_byte(), payload_statistics_t::hunt_idle_drop_bytes, event_loop_runtime_t::hunt_idle_run_length, event_loop_runtime_t::rx_byte, rx_get(), payload_statistics_t::serial_rx_drop_acc_full, app_ctx_t::stats, and event_loop_runtime_t::work_done.
Referenced by event_loop().


| void event_loop | ( | app_ctx_t * | app | ) |
Run the main application polling loop.
| app | Application context. |
TODO: Its not good that the event loop pokes around in hdlc state. We should add a helper like hdlc_sync_acc_reset_frame that hides these details and just call that from here. This shall be refactored in the next iteration.
Definition at line 383 of file event_loop.c.
References cli_poll(), drain_hdlc_frames_to_udp(), drain_rx_until_empty(), log_take_emitted_flag(), MAIN_LOOP_SLEEP_US, app_ctx_t::need_prompt, poll_and_dispatch_events(), poll_and_enqueue_udp_rx(), poll_hdlc_idle_timeout(), poll_hdlc_no_progress(), poll_tx_pipeline(), print_prompt(), event_loop_runtime_t::rx_byte, rx_clock_poll_stall(), payload_statistics_t::rx_fifo_stall_events, app_ctx_t::stats, payload_statistics_t::udp_rx_buffer_full_counts, payload_statistics_t::udp_rx_throttle_skips, event_loop_runtime_t::udp_rx_throttled, payload_statistics_t::udp_tx_buffer_full_counts, update_rx_drain_stats(), update_statistics(), update_udp_rx_throttle_state(), w5500_poll_udp_buffer_full_events(), and event_loop_runtime_t::work_done.
Referenced by main().


|
static |
Definition at line 65 of file event_loop.c.
References app_ctx_t::accumulator, V24_CONFIG_T::external_clock, HDLC_SYNC_STATE_HUNTING, HDLC_SYNC_ACCUMULATOR_T::state, and app_ctx_t::v24_config.
Referenced by drain_rx_until_empty().

|
static |
Definition at line 348 of file event_loop.c.
References event_dispatch(), EVENT_LOOP_MAX_EVENTS_AT_ONCE, EVENT_LOOP_MAX_EVENTS_WHILE_TX_ACTIVE, event_queue_pop(), app_ctx_t::tx_queue, and tx_queue_is_empty().
Referenced by event_loop().


|
static |
Definition at line 140 of file event_loop.c.
References E2S_ERR_TX_QUEUE_FULL, E2S_OK, payload_statistics_t::hdlc_tx_frames, UDP_FRAME_T::length, LOG_ERROR, UDP_FRAME_T::payload, app_ctx_t::rx_frame_buffer, app_ctx_t::sender_config, app_ctx_t::stats, app_ctx_t::tx_queue, payload_statistics_t::tx_queue_drop_frames, tx_queue_enqueue_udp_frame(), payload_statistics_t::udp_rx_frames, w5500_poll_rx(), and event_loop_runtime_t::work_done.
Referenced by event_loop().


|
static |
Definition at line 194 of file event_loop.c.
References app_ctx_t::accumulator, event_loop_runtime_t::hdlc_decode_fail_streak, HDLC_FLAG_BYTE, hdlc_sync_acc_init(), HDLC_SYNC_IDLE_TIMEOUT_US, HDLC_SYNC_STATE_HUNTING, event_loop_runtime_t::last_rx_byte_us, HDLC_FRAME_T::length, LOG_DEBUG, HDLC_FRAME_T::payload, HDLC_SYNC_ACCUMULATOR_T::position, app_ctx_t::reconstructed_frame, payload_statistics_t::resync_idle_timeout_count, HDLC_SYNC_ACCUMULATOR_T::state, and app_ctx_t::stats.
Referenced by event_loop().


|
static |
Definition at line 275 of file event_loop.c.
References app_ctx_t::accumulator, HDLC_SYNC_ACCUMULATOR_T::candidate_valid, V24_CONFIG_T::external_clock, event_loop_runtime_t::hdlc_decode_fail_streak, payload_statistics_t::hdlc_decode_ok, HDLC_FLAG_BYTE, hdlc_sync_acc_init(), HDLC_SYNC_NO_PROGRESS_MAX_BYTES, HDLC_SYNC_NO_PROGRESS_MAX_BYTES_EXTERNAL, HDLC_SYNC_STATE_HUNTING, event_loop_runtime_t::last_frame_ready_bytes, HDLC_FRAME_T::length, LOG_DEBUG, HDLC_FRAME_T::payload, PRIu64, app_ctx_t::reconstructed_frame, payload_statistics_t::resync_no_progress_count, rx_clock_hard_reset(), payload_statistics_t::serial_rx_bytes, HDLC_SYNC_ACCUMULATOR_T::state, app_ctx_t::stats, and app_ctx_t::v24_config.
Referenced by event_loop().


|
static |
Definition at line 166 of file event_loop.c.
References E2S_OK, LOG_ERROR, poll_queue_stats(), tx_poll(), app_ctx_t::tx_queue, tx_queue_drain(), TX_QUEUE_DRAIN_CHUNK_SIZE, tx_queue_is_empty(), and event_loop_runtime_t::work_done.
Referenced by event_loop().


|
static |
Definition at line 56 of file event_loop.c.
References LOG_PLAIN, and app_ctx_t::need_prompt.
Referenced by event_loop().

|
static |
Definition at line 125 of file event_loop.c.
References app_ctx_t::accumulator, payload_statistics_t::accumulator_pos_max, event_loop_runtime_t::last_rx_byte_us, HDLC_SYNC_ACCUMULATOR_T::position, payload_statistics_t::serial_rx_bytes, and app_ctx_t::stats.
Referenced by event_loop().

|
static |
Definition at line 312 of file event_loop.c.
References app_ctx_t::accumulator, HDLC_SYNC_ACCUMULATOR_T::consume_count, hdlc_decode_stats_t::crc_mismatch, payload_statistics_t::decode_fail_crc_mismatch, payload_statistics_t::decode_fail_invalid_frame, payload_statistics_t::decode_fail_payload_too_long, payload_statistics_t::decode_fail_too_short, payload_statistics_t::decode_fail_unstuff_error, payload_statistics_t::event_queue_drop_events, event_queue_get_count(), event_queue_get_high_water_mark(), event_queue_get_push_drop_count(), payload_statistics_t::event_queue_used_max, HDLC_SYNC_ACCUMULATOR_T::hardcap_drop_bytes, HDLC_SYNC_ACCUMULATOR_T::hardcap_drop_events, hdlc_decode_stats_snapshot(), hdlc_decode_stats_t::invalid_frame, payload_statistics_t::log_drop_lines, log_get_high_water_mark(), payload_statistics_t::log_queue_used_max, log_take_dropped_count(), HDLC_SYNC_ACCUMULATOR_T::lookahead_wait_synced, HDLC_SYNC_ACCUMULATOR_T::lookahead_wait_syncing, hdlc_decode_stats_t::payload_too_long, app_ctx_t::stats, payload_statistics_t::sync_candidate_consume, payload_statistics_t::sync_hardcap_drop_bytes, payload_statistics_t::sync_hardcap_drop_events, payload_statistics_t::sync_lookahead_wait_synced, payload_statistics_t::sync_lookahead_wait_syncing, hdlc_decode_stats_t::too_short, app_ctx_t::tx_queue, tx_queue_get_count(), payload_statistics_t::tx_queue_used_max, and hdlc_decode_stats_t::unstuff_error.
Referenced by event_loop().


|
static |
TODO: This curerntly only schedules tx_drain with rx_udp. Ideally we should add a simple scheduler to the event loop.
Definition at line 365 of file event_loop.c.
References app_ctx_t::stats, app_ctx_t::tx_queue, tx_queue_get_count(), TX_QUEUE_HIGH_WM, TX_QUEUE_LOW_WM, payload_statistics_t::udp_rx_throttle_enter, and event_loop_runtime_t::udp_rx_throttled.
Referenced by event_loop().

