From 726b421824fabad12ad738ee7481729579de4ae2 Mon Sep 17 00:00:00 2001 From: logicog Date: Sat, 18 Oct 2025 09:55:38 +0200 Subject: [PATCH] Add EEE httpd support --- httpd/httpd.c | 2 ++ httpd/page_impl.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++ httpd/page_impl.h | 1 + tools/httpd_sim.c | 49 ++++++++++++++++++++++++++++++-- 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/httpd/httpd.c b/httpd/httpd.c index 289308a..10d6089 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -422,6 +422,8 @@ void httpd_appcall(void) send_vlan(short_parsed); } else if (is_word(q, "/counters.json")) { send_counters(q[19]-'0'); + } else if (is_word(q, "/eee.json")) { + send_eee(); } else { send_not_found(); } diff --git a/httpd/page_impl.c b/httpd/page_impl.c index dce8843..d23a888 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -7,8 +7,10 @@ #include "uip.h" #include "../html_data.h" #include +#include "../phy.h" #pragma codeseg BANK1 +#pragma constseg BANK1 extern __xdata uint8_t outbuf[TCP_OUTBUF_SIZE]; extern __xdata uint16_t slen; @@ -208,6 +210,76 @@ void send_counters(char port) } +void send_eee() +{ + print_string("send_eee called\n"); + slen = strtox(outbuf, HTTP_RESPONCE_JSON); + print_string("sending EEE status\n"); + + reg_read_m(RTL8373_PHY_EEE_ABLTY); + uint8_t eee_ablty = sfr_data[3]; + char_to_html('['); + for (uint8_t i = minPort; i <= maxPort; i++) { + slen += strtox(outbuf + slen, "{\"portNum\":"); + if (!isRTL8373) + itoa_html(log_to_phys_port[i]); + else + itoa_html(i + 1); + if (IS_SFP(i)) { + slen += strtox(outbuf + slen, " ,\"isSFP\": 1"); + } else { + slen += strtox(outbuf + slen, " ,\"isSFP\": 0 "); + uint16_t v; + phy_read(i, PHY_MMD_AN, PHY_EEE_ADV2); + v = SFR_DATA_U16; + slen += strtox(outbuf + slen, ",\"eee\":\""); + if (v & PHY_EEE_BIT_2G5) + char_to_html('1'); + else + char_to_html('0'); + phy_read(i, PHY_MMD_AN, PHY_EEE_ADV); + v = SFR_DATA_U16; + if (v & PHY_EEE_BIT_1G) + char_to_html('1'); + else + char_to_html('0'); + if (v & PHY_EEE_BIT_100M) + char_to_html('1'); + else + char_to_html('0'); + + phy_read(i, PHY_MMD_AN, PHY_EEE_LP_ABILITY2); + v = SFR_DATA_U16; + slen += strtox(outbuf + slen, "\", \"eee_lp\":\""); + if (v & PHY_EEE_BIT_2G5) + char_to_html('1'); + else + char_to_html('0'); + phy_read(i, PHY_MMD_AN, PHY_EEE_LP_ABILITY); + v = SFR_DATA_U16; + if (v & PHY_EEE_BIT_1G) + char_to_html('1'); + else + char_to_html('0'); + if (v & PHY_EEE_BIT_100M) + char_to_html('1'); + else + char_to_html('0'); + slen += strtox(outbuf + slen, "\", \"active\": "); + if (eee_ablty & (1 << i)) + char_to_html('1'); + else + char_to_html('0'); + } + char_to_html('}'); + if (i < maxPort) + char_to_html(','); + else + char_to_html(']'); + } +} + + void send_status(void) { slen = strtox(outbuf, HTTP_RESPONCE_JSON); diff --git a/httpd/page_impl.h b/httpd/page_impl.h index 16f776e..9403023 100644 --- a/httpd/page_impl.h +++ b/httpd/page_impl.h @@ -5,5 +5,6 @@ void send_counters(char port); void send_status(void); void send_vlan(register uint16_t vlan); void send_basic_info(void); +void send_eee(void); #endif diff --git a/tools/httpd_sim.c b/tools/httpd_sim.c index 9c54444..424921c 100644 --- a/tools/httpd_sim.c +++ b/tools/httpd_sim.c @@ -139,6 +139,43 @@ void send_status(int s) } +void send_eee(int s) +{ + struct json_object *ports, *v; + const char *jstring; + char *header = "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json; charset=UTF-8\r\n\r\n"; + + time_t now = time(NULL); + now = last_called ? last_called + 1 : now; // Make sure we don't divide by 0 for rates + + ports = json_object_new_array_ext(PORTS); + for (int i = 1; i <= PORTS; i++) { + v = json_object_new_object(); + json_object_object_add(v, "portNum", json_object_new_int(i)); + json_object_object_add(v, "isSFP", json_object_new_int(i < 5 ? 0 : 1)); + uint8_t eee = 0; + eee |= 0x02; + char eee_buf[20]; + sprintf(eee_buf, "%08b", eee); + json_object_object_add(v, "eee", json_object_new_string(eee_buf)); + uint8_t eee_lp = 0; + eee_lp |= 0x04; + sprintf(eee_buf, "%08b", eee_lp); + json_object_object_add(v, "eee_lp", json_object_new_string(eee_buf)); + json_object_object_add(v, "active", json_object_new_int((i % 2) ? 1 : 0)); + json_object_array_add(ports, v); + } + last_called = now; + + write(s, header, strlen(header)); + + jstring = json_object_to_json_string_ext(ports, JSON_C_TO_STRING_PLAIN); + write(s, jstring, strlen(jstring)); + json_object_put(v); +} + + struct Server serverConstructor(int port, void (*launch)(struct Server *server)) { struct Server server; @@ -256,6 +293,11 @@ void launch(struct Server *server) send_status(new_socket); goto done; } + if (!strncmp(&buffer[4], "/eee.json", 9)) { + printf("EEE request\n"); + send_eee(new_socket); + goto done; + } if (!strncmp(&buffer[4], "/information.json", 12)) { printf("Status request\n"); send_basic_info(new_socket); @@ -272,8 +314,11 @@ void launch(struct Server *server) i++; buffer[4+i] = '\0'; - printf("Serving file: >%s<\n", &buffer[5]); - inptr = fopen(&buffer[5], "rb"); + printf("Serving file: >%s<, name length %d\n", &buffer[5], i); + if (i > 1) + inptr = fopen(&buffer[5], "rb"); + else + inptr = fopen("/index.html", "rb"); if (inptr == NULL) { printf("Cannot open input file %s\n", &buffer[5]); send_not_found(new_socket);