Merge pull request #31 from logicog/eee_status

Eee status display
This commit is contained in:
René van Dorst
2025-10-22 09:12:02 +02:00
committed by GitHub
20 changed files with 347 additions and 47 deletions
+16 -5
View File
@@ -48,6 +48,7 @@ __xdata uint8_t hexvalue[4] = { 0 };
// Buffer for writing to flash 0x1fd000, copy to 0x1fe000 // Buffer for writing to flash 0x1fd000, copy to 0x1fe000
__xdata uint8_t cmd_buffer[SBUF_SIZE]; __xdata uint8_t cmd_buffer[SBUF_SIZE];
__xdata uint8_t cmd_available;
__xdata uint8_t l; __xdata uint8_t l;
__xdata uint8_t line_ptr; __xdata uint8_t line_ptr;
@@ -469,9 +470,16 @@ void cmd_parser(void) __banked
print_string("Parsing command\n"); print_string("Parsing command\n");
print_string_x(&cmd_buffer[0]); print_string_x(&cmd_buffer[0]);
write_char('<'); write_char('\n'); write_char('<'); write_char('\n');
print_string("CMD-words: ");
print_byte(cmd_words_b[0]); write_char(' ');
print_byte(cmd_words_b[1]); write_char(' ');
print_byte(cmd_words_b[2]); write_char(' ');
print_byte(cmd_words_b[3]); write_char(' ');
print_byte(cmd_words_b[4]); write_char(' ');
print_byte(cmd_words_b[5]); write_char(' ');
print_byte(cmd_words_b[6]); write_char('\n');
#endif #endif
signed char i = cmd_words_b[0]; if (cmd_words_b[0] >= 0 && cmd_words_b[1] >= 0) {
if (i >= 0 && cmd_words_b[1] >= 0) {
if (cmd_compare(0, "reset")) { if (cmd_compare(0, "reset")) {
print_string("\nRESET\n\n"); print_string("\nRESET\n\n");
reset_chip(); reset_chip();
@@ -636,23 +644,26 @@ void cmd_parser(void) __banked
} }
if (cmd_compare(0, "eee")) { if (cmd_compare(0, "eee")) {
int8_t port = -1; int8_t port = -1;
if (cmd_words_b[2] > 0) { if (cmd_words_b[3] > 0) {
port = cmd_buffer[cmd_words_b[2]] - '1'; port = cmd_buffer[cmd_words_b[2]] - '1';
if (!isRTL8373) if (!isRTL8373)
port = phys_to_log_port[port]; port = phys_to_log_port[port];
} }
if (cmd_words_b[1] > 0 && cmd_compare(1, "on")) { if (cmd_words_b[1] > 0 && cmd_compare(1, "on")) {
print_string("EEE enabled\n");
if (port >= 0) if (port >= 0)
port_eee_enable(port); port_eee_enable(port);
else else
port_eee_enable_all(); port_eee_enable_all();
} else if (cmd_words_b[1] > 0 && cmd_compare(1, "off")) { } else if (cmd_words_b[1] > 0 && cmd_compare(1, "off")) {
print_string("EEE disabled\n");
if (port >= 0) if (port >= 0)
port_eee_disable(port); port_eee_disable(port);
else else
port_eee_disable_all(); port_eee_disable_all();
} else if (cmd_words_b[1] > 0 && cmd_compare(1, "status")) {
if (port >= 0)
port_eee_status(port);
else
port_eee_status_all();
} }
} }
} }
+1
View File
@@ -6,6 +6,7 @@
#include "rtl837x_common.h" #include "rtl837x_common.h"
extern __xdata uint8_t cmd_buffer[SBUF_SIZE]; extern __xdata uint8_t cmd_buffer[SBUF_SIZE];
extern __xdata uint8_t cmd_available;
uint8_t cmd_tokenize(void) __banked; uint8_t cmd_tokenize(void) __banked;
void cmd_parser(void) __banked; void cmd_parser(void) __banked;
+26
View File
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<script src="/ports.js"></script>
<script src="/main.js"></script>
<link rel="stylesheet" href="style.css">
<title>FreeSwitchOS Port Statistics</title>
</head>
<body>
<nav id="sidebar"></nav>
<div style="margin-left:16%;padding:1px 16px;height:1000px;">
<div id="ports"></div>
<h1>EEE Status</h1>
<table id="eeetable">
<tr> <th> </th> <th colspan="3"> Advertising </th> <th colspan="3">Link-Partner advertises</th> <th></th></tr>
<tr> <th>Port</th> <th>2.5G</th> <th>1G</th> <th>100M</th> <th>2.5G</th> <th>1G</th> <th>100M</th> <th>Active?</th></tr>
</table>
<div>
<input style="width:20%;padding: 8px 16px;margin-top: 2em;margin-right: 3em;" id="eee_enable" onclick="eeeSub(0, 1);" type="button" value="Enable EEE">
<input style="width:20%;padding: 8px 16px;margin-top: 2em;" id="eee_enable" onclick="eeeSub(0, 0);" type="button" value="Disable EEE">
</div>
<script src="/eee.js"></script>
<script src="/eee_sub.js"></script>
</div>
<script src="/navigation.js"></script>
</body>
</html>
+49
View File
@@ -0,0 +1,49 @@
function createEEE() {
var tbl = document.getElementById('eeetable');
if (tbl.rows.length <= 2) {
console.log("CREATING TABLE ", tbl.rows.length);
for (let i = 2; i < 8; i++) {
console.log("Table row: " + i + "pState: " + pState[i-2]);
const tr = tbl.insertRow();
let td = tr.insertCell(); td.appendChild(document.createTextNode(`Port ${i-1}`));
for (let j = 0; j < 7; j++) {
td = tr.insertCell(); td.appendChild(document.createTextNode(" "));
}
}
}
}
function getEEE() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const s = JSON.parse(xhttp.responseText);
console.log("EEE: ", JSON.stringify(s));
var tbl = document.getElementById('eeetable');
if (tbl.rows.length > 2) {
for (let i = 2; i < 8; i++) {
p = s[i-2];
let n = p.portNum;
console.log("Table Update row: " + i + " portNum is " + n + ", pState is " + pState[i-2]);
let tr = tbl.rows[n+1];
if (!p.isSFP) {
let eee = parseInt(p.eee,2); let lp = parseInt(p.eee_lp,2);
tr.cells[1].innerHTML = `${eee&4?"ON":"OFF"}`; tr.cells[2].innerHTML = `${eee&2?"ON":"OFF"}`; tr.cells[3].innerHTML = `${eee&1?"ON":"OFF"}`;
tr.cells[4].innerHTML = `${lp&4?"ON":"OFF"}`; tr.cells[5].innerHTML = `${lp&2?"ON":"OFF"}`; tr.cells[6].innerHTML = `${lp&1?"ON":"OFF"}`;
tr.cells[7].innerHTML = `${p.active}`;
tr.classList.toggle('disabled', pState[i-2] < 0); tr.classList.toggle('isNOK', !p.active); tr.classList.toggle('isOK', p.active);
}
tr.classList.toggle('isSFP', p.isSFP);
}
}
}
};
xhttp.open("GET", "/eee.json", true);
xhttp.timeout = 1500; xhttp.send();
}
window.addEventListener("load", function() {
getEEE();
const iCount = setInterval(getEEE, 2000);
});
const stat = setInterval(createEEE, 1000);
+18
View File
@@ -0,0 +1,18 @@
async function eeeSub(port, enable) {
var cmd = "eee ";
if (enable)
cmd = cmd + "on";
else
cmd = cmd + "off";
console.log("eeeSub port " + port, ", value " + enable);
try {
const response = await fetch('/cmd', {
method: 'POST',
body: cmd
});
console.log('Completed!', response);
} catch(err) {
console.error(`Error: ${err}`);
}
}
+2 -2
View File
@@ -51,10 +51,10 @@ function update() {
} }
}; };
xhttp.open("GET", "/status.json", true); xhttp.open("GET", "/status.json", true);
xhttp.send(); xhttp.timeout = 5000; xhttp.send();
} }
window.addEventListener("load", function() { window.addEventListener("load", function() {
update(); update();
const interval = setInterval(update, 1000); const interval = setInterval(update, 2000);
}); });
+1 -1
View File
@@ -4,6 +4,6 @@ document.getElementById('sidebar').innerHTML =
+ "<li><a href='vlan.html'>VLAN</a></li>" + "<li><a href='vlan.html'>VLAN</a></li>"
+ "<li><a href='mirror.html'>Mirroring</a></li>" + "<li><a href='mirror.html'>Mirroring</a></li>"
+ "<li><a href='trunk.html'>Port Aggregation</a></li>" + "<li><a href='trunk.html'>Port Aggregation</a></li>"
+ "<li><a href='mirror.html'>Mirroring</a></li>" + "<li><a href='eee.html'>EEE</a></li>"
+ "<li><a href='update.html'>Firmware Update</a></li></ul>"; + "<li><a href='update.html'>Firmware Update</a></li></ul>";
-1
View File
@@ -17,6 +17,5 @@ function drawPorts() {
l.id="port" + (i+1); l.id="port" + (i+1);
f.appendChild(l); f.appendChild(l);
} }
console.log("DRAWING DONE "); console.log("DRAWING DONE ");
} }
+1 -18
View File
@@ -23,21 +23,4 @@ function fillStats() {
} }
} }
function getCounts() { const stat = setInterval(fillStats, 1000);
for (let port = 1; port <= numPorts; port++) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const s = JSON.parse(xhttp.responseText);
console.log("Counters: ", JSON.stringify(s));
}
}
xhttp.open("GET", `/counters.json?port=${port}`, true);
xhttp.send();
}
}
window.addEventListener("load", function() {
const iCount = setInterval(getCounts, 1000);
});
const stat = setInterval(fillStats, 500);
+5
View File
@@ -69,3 +69,8 @@ object {
.cbgroup label { .cbgroup label {
display: block; display: block;
} }
.disabled{ opacity: .4; background-color: #f88; color: #000}
.isSFP{ opacity: .4; background-color: #660;}
.isNOK{ color: #900;}
.isOK{ color: #090;}
+4 -3
View File
@@ -288,9 +288,8 @@ void handle_post(void)
while (*p && *p != '\n' && *p != '\r') while (*p && *p != '\n' && *p != '\r')
cmd_buffer[i++] = *p++; cmd_buffer[i++] = *p++;
cmd_buffer[i] = '\0'; cmd_buffer[i] = '\0';
if (i)
if (i && !cmd_tokenize()) cmd_available = 1;
cmd_parser();
} else if (is_word(request_path, "upload")) { } else if (is_word(request_path, "upload")) {
print_string("POST upload request\n"); print_string("POST upload request\n");
if (!boundary[0]) { if (!boundary[0]) {
@@ -422,6 +421,8 @@ void httpd_appcall(void)
send_vlan(short_parsed); send_vlan(short_parsed);
} else if (is_word(q, "/counters.json")) { } else if (is_word(q, "/counters.json")) {
send_counters(q[19]-'0'); send_counters(q[19]-'0');
} else if (is_word(q, "/eee.json")) {
send_eee();
} else { } else {
send_not_found(); send_not_found();
} }
+72
View File
@@ -7,8 +7,10 @@
#include "uip.h" #include "uip.h"
#include "../html_data.h" #include "../html_data.h"
#include <stdint.h> #include <stdint.h>
#include "../phy.h"
#pragma codeseg BANK1 #pragma codeseg BANK1
#pragma constseg BANK1
extern __xdata uint8_t outbuf[TCP_OUTBUF_SIZE]; extern __xdata uint8_t outbuf[TCP_OUTBUF_SIZE];
extern __xdata uint16_t slen; 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) void send_status(void)
{ {
slen = strtox(outbuf, HTTP_RESPONCE_JSON); slen = strtox(outbuf, HTTP_RESPONCE_JSON);
+1
View File
@@ -5,5 +5,6 @@ void send_counters(char port);
void send_status(void); void send_status(void);
void send_vlan(register uint16_t vlan); void send_vlan(register uint16_t vlan);
void send_basic_info(void); void send_basic_info(void);
void send_eee(void);
#endif #endif
+14 -4
View File
@@ -12,15 +12,25 @@
* Define PHY pages * Define PHY pages
*/ */
#define PHY_MMD_AN 7 #define PHY_MMD_AN 7
#define PHY_SDS_CTRL 30
#define PHY_MMD_CTRL 31 #define PHY_MMD_CTRL 31
/* /*
* Define registers in Auto-Negotiation page * Define registers in Auto-Negotiation page
*/ */
#define PHY_ANEG_CTRL 0x00 #define PHY_ANEG_CTRL 0x00
#define PHY_EEE_ADV 0x3c #define PHY_EEE_ADV 0x3c
#define PHY_EEE_ABILITY 0x3d #define PHY_EEE_LP_ABILITY 0x3d
#define PHY_EEE_ADV2 0x3e #define PHY_EEE_ADV2 0x3e
#define PHY_EEE_LP_ABILITY2 0x3f
// Register bits for EEE capabilities at a given speed
#define PHY_EEE_BIT_2G5 0x01
#define PHY_EEE_BIT_1G 0x04
#define PHY_EEE_BIT_100M 0x02
/*
* Define registers in Control page
*/
#define PHY_CTRL_5 0x7582
#endif #endif
+1 -1
View File
@@ -236,7 +236,7 @@ void phy_reset(uint8_t port) __banked
// Disable PHY // Disable PHY
phy_write(port, PHY_MMD_CTRL, 0xa610, v | 0x0800); phy_write(port, PHY_MMD_CTRL, 0xa610, v | 0x0800);
delay(5); delay(2);
// Re-enable PHY // Re-enable PHY
phy_write(port, PHY_MMD_CTRL, 0xa610, v & 0xf7ff); phy_write(port, PHY_MMD_CTRL, 0xa610, v & 0xf7ff);
} }
+66 -5
View File
@@ -485,12 +485,11 @@ void port_eee_enable(uint8_t port) __banked
if (is_sfp[port]) if (is_sfp[port])
return; return;
print_string("EEE on for "); print_byte(port); write_char('\n');
REG_SET(RTL8373_EEE_CTRL_BASE + (port << 2), EEE_100 | EEE_1000 | EEE_2G5); REG_SET(RTL8373_EEE_CTRL_BASE + (port << 2), EEE_100 | EEE_1000 | EEE_2G5);
// Enable EEE advertisement for 100/1000BASE-T via EEE Advertisement Reg // Enable EEE advertisement for 100/1000BASE-T via EEE Advertisement Reg
phy_write(port, PHY_MMD_AN, PHY_EEE_ADV, 6); // Bit 1: 100BASE-T, Bit 2: 1000BASE-T phy_write(port, PHY_MMD_AN, PHY_EEE_ADV, PHY_EEE_BIT_1G | PHY_EEE_BIT_100M);
// Enable EEE advertisement for 2.5GBASE-T via EEE Advertisement Reg 2 // Enable EEE advertisement for 2.5GBASE-T via EEE Advertisement Reg 2
phy_write(port, PHY_MMD_AN, PHY_EEE_ADV2, 1); phy_write(port, PHY_MMD_AN, PHY_EEE_ADV2, PHY_EEE_BIT_2G5);
phy_reset(port); phy_reset(port);
} }
@@ -502,14 +501,69 @@ void port_eee_disable(uint8_t port) __banked
print_string("EEE off for "); print_byte(port); write_char('\n'); print_string("EEE off for "); print_byte(port); write_char('\n');
REG_SET(RTL8373_EEE_CTRL_BASE + (port << 2), 0); REG_SET(RTL8373_EEE_CTRL_BASE + (port << 2), 0);
// Enable EEE advertisement for 100/1000BASE-T via EEE Advertisement Reg // Disable EEE advertisement for 100/1000BASE-T via EEE Advertisement Reg
phy_write(port, PHY_MMD_AN, PHY_EEE_ADV, 0); phy_write(port, PHY_MMD_AN, PHY_EEE_ADV, 0);
// Enable EEE advertisement for 2.5GBASE-T via EEE Advertisement Reg 2 // Disable EEE advertisement for 2.5GBASE-T via EEE Advertisement Reg 2
phy_write(port, PHY_MMD_AN, PHY_EEE_ADV2, 0); phy_write(port, PHY_MMD_AN, PHY_EEE_ADV2, 0);
phy_reset(port); phy_reset(port);
} }
void port_eee_status(uint8_t port) __banked
{
print_string("Port: "); write_char('0' + log_to_phys_port[port]);
print_string(": ");
if (is_sfp[port]) {
print_string("SFP\n");
return;
}
uint16_t v;
print_string("Advertising: ");
phy_read(port, PHY_MMD_AN, PHY_EEE_ADV2);
v = SFR_DATA_U16;
if (v & PHY_EEE_BIT_2G5)
print_string(" 2.5G");
else
print_string(" ");
phy_read(port, PHY_MMD_AN, PHY_EEE_ADV);
v = SFR_DATA_U16;
if (v & PHY_EEE_BIT_1G)
print_string(" 1G ");
else
print_string(" ");
if (v & PHY_EEE_BIT_100M)
print_string(" 100M");
else
print_string(" ");
print_string(" Link Partner: ");
phy_read(port, PHY_MMD_AN, PHY_EEE_LP_ABILITY2);
v = SFR_DATA_U16;
if (v & PHY_EEE_BIT_2G5)
print_string(" 2.5G");
else
print_string(" ");
phy_read(port, PHY_MMD_AN, PHY_EEE_LP_ABILITY);
v = SFR_DATA_U16;
if (v & PHY_EEE_BIT_1G)
print_string(" 1G ");
else
print_string(" ");
if (v & PHY_EEE_BIT_100M)
print_string(" 100M");
else
print_string(" ");
reg_read_m(RTL8373_PHY_EEE_ABLTY);
if (sfr_data[3] & (1 << port))
print_string(" ACTIVE ");
else
print_string(" INACTIVE ");
write_char('\n');
}
void port_eee_enable_all(void) __banked void port_eee_enable_all(void) __banked
{ {
for (uint8_t i = minPort; i <= maxPort; i++) { for (uint8_t i = minPort; i <= maxPort; i++) {
@@ -526,6 +580,13 @@ void port_eee_disable_all(void) __banked
} }
void port_eee_status_all(void) __banked
{
for (uint8_t i = minPort; i <= maxPort; i++) {
port_eee_status(i);
}
}
/* /*
* Enable RLDP, Realtek's version of LLDP * Enable RLDP, Realtek's version of LLDP
*/ */
+2
View File
@@ -29,6 +29,8 @@ void port_l2_setup(void) __banked;
void trunk_set(uint8_t group, uint16_t mask) __banked; void trunk_set(uint8_t group, uint16_t mask) __banked;
void port_eee_enable_all(void) __banked; void port_eee_enable_all(void) __banked;
void port_eee_disable_all(void) __banked; void port_eee_disable_all(void) __banked;
void port_eee_status_all(void) __banked;
void port_eee_enable(uint8_t port) __banked; void port_eee_enable(uint8_t port) __banked;
void port_eee_disable(uint8_t port) __banked; void port_eee_disable(uint8_t port) __banked;
void port_eee_status(uint8_t port) __banked;
#endif #endif
+3
View File
@@ -158,6 +158,9 @@
/* /*
* EEE * EEE
*/ */
#define RTL837X_EEE_STATUS 0x125C
#define RTL837X_MAC_EEE_ABLTY 0x6404
#define RTL8373_PHY_EEE_ABLTY 0x642C
#define RTL8373_EEE_CTRL_BASE 0x606c #define RTL8373_EEE_CTRL_BASE 0x606c
#define EEE_100 0x01 #define EEE_100 0x01
#define EEE_1000 0x04 #define EEE_1000 0x04
+17 -4
View File
@@ -67,7 +67,6 @@ __xdata volatile uint8_t sbuf_ptr;
__xdata uint8_t sbuf[SBUF_SIZE]; __xdata uint8_t sbuf[SBUF_SIZE];
__xdata uint8_t sfr_data[4]; __xdata uint8_t sfr_data[4];
extern __xdata uint8_t cmd_buffer[SBUF_SIZE];
extern __xdata uint8_t gpio_last_value[8]; extern __xdata uint8_t gpio_last_value[8];
extern __xdata struct flash_region_t flash_region; extern __xdata struct flash_region_t flash_region;
@@ -1021,6 +1020,13 @@ void idle(void)
stp_clock--; stp_clock--;
} }
} }
// Check whether a command is waiting in the cmd_buffer and execute
if (cmd_available) {
cmd_available = 0;
if (!cmd_tokenize())
cmd_parser();
print_string("\n> ");
}
} }
@@ -1805,8 +1811,12 @@ void bootloader(void)
// printed out the entered characters // printed out the entered characters
__xdata uint8_t l = sbuf_ptr; // We have printed out entered characters until l __xdata uint8_t l = sbuf_ptr; // We have printed out entered characters until l
__xdata uint8_t line_start = sbuf_ptr; // This is where the current line starts __xdata uint8_t line_start = sbuf_ptr; // This is where the current line starts
cmd_available = 0;
while (1) { while (1) {
while (l != sbuf_ptr) { while (l != sbuf_ptr) {
// If the command buffer is currently in use, we cannot copy to it
if (cmd_available)
break;
write_char(sbuf[l]); write_char(sbuf[l]);
// Check whether there is a full line: // Check whether there is a full line:
if (sbuf[l] == '\n' || sbuf[l] == '\r') { if (sbuf[l] == '\n' || sbuf[l] == '\r') {
@@ -1819,9 +1829,12 @@ void bootloader(void)
line_start++; line_start++;
line_start &= (SBUF_SIZE - 1); line_start &= (SBUF_SIZE - 1);
cmd_buffer[i] = '\0'; cmd_buffer[i] = '\0';
if (i && !cmd_tokenize()) // If there is a command we print the prompt after execution
cmd_parser(); // otherwise immediately because there is nothing to execute
print_string("\n> "); if (i)
cmd_available = 1;
else
print_string("\n> ");
} }
l++; l++;
l &= (SBUF_SIZE - 1); l &= (SBUF_SIZE - 1);
+47 -2
View File
@@ -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 serverConstructor(int port, void (*launch)(struct Server *server)) {
struct Server server; struct Server server;
@@ -256,6 +293,11 @@ void launch(struct Server *server)
send_status(new_socket); send_status(new_socket);
goto done; 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)) { if (!strncmp(&buffer[4], "/information.json", 12)) {
printf("Status request\n"); printf("Status request\n");
send_basic_info(new_socket); send_basic_info(new_socket);
@@ -272,8 +314,11 @@ void launch(struct Server *server)
i++; i++;
buffer[4+i] = '\0'; buffer[4+i] = '\0';
printf("Serving file: >%s<\n", &buffer[5]); printf("Serving file: >%s<, name length %d\n", &buffer[5], i);
inptr = fopen(&buffer[5], "rb"); if (i > 1)
inptr = fopen(&buffer[5], "rb");
else
inptr = fopen("/index.html", "rb");
if (inptr == NULL) { if (inptr == NULL) {
printf("Cannot open input file %s\n", &buffer[5]); printf("Cannot open input file %s\n", &buffer[5]);
send_not_found(new_socket); send_not_found(new_socket);