From c1a414833d723d4896172f000d524ab5d76834c2 Mon Sep 17 00:00:00 2001 From: feelfree69 Date: Mon, 2 Mar 2026 19:42:40 +0100 Subject: [PATCH] Various refactorings --- cmd_parser.c | 14 ++++------ httpd/page_impl.c | 13 ++++----- rtl837x_common.h | 2 ++ rtl837x_flash.h | 1 + rtlplayground.c | 31 ++++++++++++++------- syslog.c | 70 +++++++++++++++++++++++------------------------ syslog.h | 13 +++++++++ uip/uip.c | 4 +-- 8 files changed, 84 insertions(+), 64 deletions(-) diff --git a/cmd_parser.c b/cmd_parser.c index de47e93..6e13f90 100644 --- a/cmd_parser.c +++ b/cmd_parser.c @@ -15,6 +15,7 @@ #include "rtl837x_igmp.h" #include "rtl837x_bandwidth.h" #include "dhcp.h" +#include "syslog.h" #include "uip/uip.h" #include "version.h" @@ -41,8 +42,6 @@ extern __xdata char passwd[21]; extern __xdata struct dhcp_state dhcp_state; -extern __xdata uip_ipaddr_t syslog_addr; - __xdata uint8_t vlan_names[VLAN_NAMES_SIZE]; __xdata uint16_t vlan_ptr; extern __xdata uint16_t management_vlan; @@ -1208,15 +1207,14 @@ void cmd_parser(void) __banked } else if (cmd_words_b[1] > 0 && cmd_compare(1, "ip")) { if (cmd_words_b[3] < 0) { print_string("Current syslog IP: "); - itoa(syslog_addr[0]); write_char('.'); itoa(syslog_addr[0] >> 8); write_char('.'); - itoa(syslog_addr[1]); write_char('.'); itoa(syslog_addr[1] >> 8); + itoa(syslog_state.server_ip[0]); write_char('.'); itoa(syslog_state.server_ip[1]); write_char('.'); + itoa(syslog_state.server_ip[2]); write_char('.'); itoa(syslog_state.server_ip[3]); return; } else if (!parse_ip(cmd_words_b[2])) { syslog_stop(); - uip_ipaddr(&syslog_addr, ip[0], ip[1], ip[2], ip[3]); - print_string("Setting syslog IP: "); - itoa(ip[0]); write_char('.'); itoa(ip[1]); write_char('.'); - itoa(ip[2]); write_char('.'); itoa(ip[3]); write_char('\n'); + print_string("Setting new syslog IP.\n"); + syslog_state.server_ip[0] = ip[0]; syslog_state.server_ip[1] = ip[1]; + syslog_state.server_ip[2] = ip[2]; syslog_state.server_ip[3] = ip[3]; syslog_start(); } else { print_string("Invalid IP address\n"); diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 590fa64..3273e57 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -13,12 +13,11 @@ #include "version.h" #include "machine.h" #include "page_impl.h" +#include "syslog.h" // #define DEBUG #include "debug.h" -extern __xdata uip_ipaddr_t syslog_addr; - #define L2_MAX_TRANSFER 30 #pragma codeseg BANK1 @@ -228,11 +227,11 @@ void send_basic_info(void) itoa_html(uip_netmask[0] >> 8); char_to_html('.'); itoa_html(uip_netmask[1]); char_to_html('.'); itoa_html(uip_netmask[1] >> 8); - slen += strtox(outbuf + slen, "\",\"syslog_address\":\""); - itoa_html(syslog_addr[0]); char_to_html('.'); - itoa_html(syslog_addr[0] >> 8); char_to_html('.'); - itoa_html(syslog_addr[1]); char_to_html('.'); - itoa_html(syslog_addr[1] >> 8); + slen += strtox(outbuf + slen, "\",\"syslog_server_ip\":\""); + itoa_html(syslog_state.server_ip[0]); char_to_html('.'); + itoa_html(syslog_state.server_ip[1]); char_to_html('.'); + itoa_html(syslog_state.server_ip[2]); char_to_html('.'); + itoa_html(syslog_state.server_ip[3]); slen += strtox(outbuf + slen, "\",\"mac_address\":\""); byte_to_html(uip_ethaddr.addr[0]); char_to_html(':'); byte_to_html(uip_ethaddr.addr[1]); char_to_html(':'); diff --git a/rtl837x_common.h b/rtl837x_common.h index 5b73b84..10cf1f8 100644 --- a/rtl837x_common.h +++ b/rtl837x_common.h @@ -99,6 +99,7 @@ extern __xdata uint8_t uip_buf[UIP_CONF_BUFFER_SIZE+2]; extern __xdata struct uip_eth_addr uip_ethaddr; // Headers for calls in the common code area (HOME/BANK0) +void print_string_no_syslog(__code char *p); void print_string(__code char *p); void print_string_x(__xdata char *p); void print_long(uint32_t a); @@ -120,6 +121,7 @@ void sds_read(uint8_t sds_id, uint8_t page, uint8_t reg); void sds_write_v(uint8_t sds_id, uint8_t page, uint8_t reg, uint16_t v); void delay(uint16_t t); void sleep(uint16_t t); +void write_char_no_syslog(char c); void write_char(char c); void print_reg(uint16_t reg); uint8_t sfp_read_reg(uint8_t slot, uint8_t reg); diff --git a/rtl837x_flash.h b/rtl837x_flash.h index 2267b23..60de64f 100644 --- a/rtl837x_flash.h +++ b/rtl837x_flash.h @@ -13,3 +13,4 @@ void flash_write_bytes(__xdata uint8_t *ptr); __code char* get_flash_size_str(void); #endif + diff --git a/rtlplayground.c b/rtlplayground.c index e37751d..c50d7ae 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -34,11 +34,6 @@ void crc16(__xdata uint8_t *v) __naked; void flash_default_config(void); void early_boot_handle_button(void); -extern __xdata char logbuf[LOGBUF_SIZE]; -extern __xdata uint16_t logptr_w; -extern __xdata uint8_t full_line_available; -extern __xdata uint8_t syslog_enabled; - // See setup_serial_timer1() for valid baudrate settings! #define SERIAL_BAUD_RATE 115200 @@ -179,7 +174,7 @@ void isr_serial(void) __interrupt(4) } -void write_char(char c) +void write_char_no_syslog(char c) { do { } while (tx_buf_empty == 0); @@ -191,12 +186,17 @@ void write_char(char c) } tx_buf_empty = 0; SBUF = c; +} - if (syslog_enabled) { - logbuf[logptr_w++] = c; - logptr_w &= (LOGBUF_SIZE - 1); +void write_char(char c) +{ + write_char_no_syslog(c); + + if (syslog_state.enabled) { + logbuf[syslog_state.writeptr++] = c; + syslog_state.writeptr &= (LOGBUF_SIZE - 1); if (c == '\n') - full_line_available = 1; + syslog_state.line_available = 1; } } @@ -222,6 +222,12 @@ void print_string(__code char *p) write_char(*p++); } +void print_string_no_syslog(__code char *p) +{ + while (*p) + write_char_no_syslog(*p++); +} + void print_string_x(__xdata char *p) { while (*p) @@ -319,6 +325,11 @@ void print_byte(uint8_t a) write_char(low); } +void print_cmd_prompt(void) +{ + print_string_no_syslog("\n> "); +} + /* * External IRQ 0 Service Routine: Called on link change? * Note that all registers are being put on the STACK because of calling a subroutine diff --git a/syslog.c b/syslog.c index a661840..ad97fd2 100644 --- a/syslog.c +++ b/syslog.c @@ -9,74 +9,72 @@ #define SYSLOG_P ((__xdata uint8_t *)uip_appdata) __xdata char logbuf[LOGBUF_SIZE]; -__xdata uint16_t logptr_w ; -__xdata uint16_t logptr_r; -__xdata uint8_t full_line_available; -__xdata uint8_t syslog_enabled; -__xdata uip_ipaddr_t syslog_addr; +__xdata struct syslog_state syslog_state; +__xdata uip_ipaddr_t server_ip; -struct uip_udp_conn *syslog_conn; +#define state syslog_state void syslog_init(void) __banked { - syslog_enabled = 0; - syslog_conn = 0; - logptr_w = 0; - logptr_r = 0; - full_line_available = 0; - syslog_addr[0] = 0; syslog_addr[1] = 0; // Default to 0.0.0.0 + state.enabled = 0; + state.syslog_conn = 0; + state.writeptr = 0; + state.readptr = 0; + state.line_available = 0; + state.server_ip[0] = 0; state.server_ip[1] = 0; state.server_ip[2] = 0; state.server_ip[3] = 0;// Default to 0.0.0.0 } void syslog_start(void) __banked { - if (syslog_conn == 0) { - syslog_conn = uip_udp_new(&syslog_addr, HTONS(514)); - if (syslog_conn == 0) { - print_string("Failed to create a new UDP client\n"); + if (state.syslog_conn == 0) { + uip_ipaddr(server_ip, state.server_ip[0], state.server_ip[1], state.server_ip[2], state.server_ip[3]); + state.syslog_conn = uip_udp_new(&server_ip, HTONS(514)); + if (state.syslog_conn == 0) { + print_string_no_syslog("Failed to create a new UDP client\n"); return; } - print_string("Started syslog to IP "); - itoa(syslog_addr[0]); write_char('.'); itoa(syslog_addr[0]>>8); write_char('.'); - itoa(syslog_addr[1]); write_char('.'); itoa(syslog_addr[1]>>8); write_char('\n'); - syslog_enabled = 1; + print_string_no_syslog("Started syslog to IP "); + itoa(state.server_ip[0]); write_char('.'); itoa(state.server_ip[1]); write_char('.'); + itoa(state.server_ip[2]); write_char('.'); itoa(state.server_ip[3]); write_char('\n'); + state.enabled = 1; } else { - print_string("Syslog is already running\n"); + print_string_no_syslog("Syslog is already running\n"); } } void syslog_stop(void) __banked { - syslog_enabled = 0; - if (syslog_conn != 0) { - uip_udp_remove(syslog_conn); - syslog_conn = 0; - print_string("Stopped syslog\n"); + state.enabled = 0; + if (state.syslog_conn != 0) { + uip_udp_remove(state.syslog_conn); + state.syslog_conn = 0; + print_string_no_syslog("Stopped syslog\n"); } else { - print_string("Syslog is not running\n"); + print_string_no_syslog("Syslog is not running\n"); } } void syslog_callback(uint16_t lport) __banked { - if (lport != syslog_conn->lport) + if (lport != state.syslog_conn->lport) return; - if ((logptr_r != logptr_w) && full_line_available) + if ((state.readptr != state.writeptr) && state.line_available) { - int16_t log_size = logptr_w - logptr_r; + int16_t log_size = state.writeptr - state.readptr; if (log_size < 0) log_size += LOGBUF_SIZE; // Skipping linefeeds at the start of the log line - uint16_t log_start = logptr_r; + uint16_t log_start = state.readptr; while (log_size > 0 && logbuf[log_start] == '\n') { log_start = (log_start + 1) & (LOGBUF_SIZE - 1); log_size--; } // Skipping linefeeds and whitespaces at the end of the log line - uint16_t log_end = logptr_w; + uint16_t log_end = state.writeptr; while ( (log_size > 0) && ((logbuf[(log_end-1) & (LOGBUF_SIZE - 1)] == '\n') || (logbuf[(log_end-1) & (LOGBUF_SIZE - 1)] == ' '))) @@ -86,8 +84,8 @@ void syslog_callback(uint16_t lport) __banked } if (log_size == 0) { - logptr_r = logptr_w; - full_line_available = 0; + state.readptr = state.writeptr; + state.line_available = 0; return; } @@ -101,7 +99,7 @@ void syslog_callback(uint16_t lport) __banked } uip_udp_send(log_size+4); - logptr_r = logptr_w; - full_line_available = 0; + state.readptr = state.writeptr; + state.line_available = 0; } } diff --git a/syslog.h b/syslog.h index 26e2981..f3666da 100644 --- a/syslog.h +++ b/syslog.h @@ -5,6 +5,19 @@ #define LOGBUF_SIZE 512 +struct syslog_state { + uint8_t enabled; + uint8_t line_available; + uint16_t writeptr ; + uint16_t readptr; + uint8_t server_ip[4]; + + struct uip_udp_conn *syslog_conn; +}; + +extern __xdata struct syslog_state syslog_state; +extern __xdata char logbuf[LOGBUF_SIZE]; + void syslog_init(void) __banked; void syslog_start(void) __banked; void syslog_stop(void) __banked; diff --git a/uip/uip.c b/uip/uip.c index d50ead7..d65d5d7 100644 --- a/uip/uip.c +++ b/uip/uip.c @@ -96,8 +96,6 @@ #include "../rtl837x_common.h" -extern __xdata uint8_t syslog_enabled; - /*---------------------------------------------------------------------------*/ /* Variable definitions. */ @@ -236,7 +234,7 @@ __xdata struct uip_stats uip_stat; #endif /* UIP_STATISTICS == 1 */ #if UIP_LOGGING == 1 -#define UIP_LOG(m) uint8_t tmp = syslog_enabled; syslog_enabled = 0; print_string(m); syslog_enabled = tmp; +#define UIP_LOG(m) print_string_no_syslog(m); #else #define UIP_LOG(m) #endif /* UIP_LOGGING == 1 */