Skip to content

Commit 7d94f3d

Browse files
committed
Improve JSON response.
Remove all the extra spaces from the json. Also simplify the generation of the json. Codespace is cheap so duplicated strings is not an issue. But generating strings from different parts is more expensive in clock cycles. Add a helper funcion `bool_to_html()` to generate bool value for html output.
1 parent d0b6a73 commit 7d94f3d

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

httpd/page_impl.c

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ void charhex_to_html(char c)
5353
}
5454

5555

56+
// Convert (uint8_t) bool to ascii '0' or '1' char push on html-buffer.
57+
void bool_to_html(char c)
58+
{
59+
outbuf[slen++] = c ? '1' : '0';
60+
}
61+
62+
5663
void char_to_html(char c)
5764
{
5865
outbuf[slen++] = c;
@@ -212,64 +219,46 @@ void send_counters(char port)
212219

213220
void send_eee()
214221
{
215-
print_string("send_eee called\n");
222+
print_string("send_eee called\nsending EEE status\n");
216223
slen = strtox(outbuf, HTTP_RESPONCE_JSON);
217-
print_string("sending EEE status\n");
218224

219225
reg_read_m(RTL8373_PHY_EEE_ABLTY);
220226
uint8_t eee_ablty = sfr_data[3];
227+
221228
char_to_html('[');
222229
for (uint8_t i = minPort; i <= maxPort; i++) {
223230
slen += strtox(outbuf + slen, "{\"portNum\":");
224231
if (!isRTL8373)
225232
itoa_html(log_to_phys_port[i]);
226233
else
227234
itoa_html(i + 1);
235+
228236
if (IS_SFP(i)) {
229-
slen += strtox(outbuf + slen, " ,\"isSFP\": 1");
237+
slen += strtox(outbuf + slen, ",\"isSFP\":1");
230238
} else {
231-
slen += strtox(outbuf + slen, " ,\"isSFP\": 0 ");
239+
slen += strtox(outbuf + slen, ",\"isSFP\":0,\"eee\":\"");
232240
uint16_t v;
233241
phy_read(i, PHY_MMD_AN, PHY_EEE_ADV2);
234242
v = SFR_DATA_U16;
235-
slen += strtox(outbuf + slen, ",\"eee\":\"");
236-
if (v & PHY_EEE_BIT_2G5)
237-
char_to_html('1');
238-
else
239-
char_to_html('0');
243+
bool_to_html(v & PHY_EEE_BIT_2G5);
244+
240245
phy_read(i, PHY_MMD_AN, PHY_EEE_ADV);
241246
v = SFR_DATA_U16;
242-
if (v & PHY_EEE_BIT_1G)
243-
char_to_html('1');
244-
else
245-
char_to_html('0');
246-
if (v & PHY_EEE_BIT_100M)
247-
char_to_html('1');
248-
else
249-
char_to_html('0');
247+
bool_to_html(v & PHY_EEE_BIT_1G);
248+
bool_to_html(v & PHY_EEE_BIT_100M);
250249

251250
phy_read(i, PHY_MMD_AN, PHY_EEE_LP_ABILITY2);
252251
v = SFR_DATA_U16;
253-
slen += strtox(outbuf + slen, "\", \"eee_lp\":\"");
254-
if (v & PHY_EEE_BIT_2G5)
255-
char_to_html('1');
256-
else
257-
char_to_html('0');
252+
slen += strtox(outbuf + slen, "\",\"eee_lp\":\"");
253+
bool_to_html (v & PHY_EEE_BIT_2G5);
254+
258255
phy_read(i, PHY_MMD_AN, PHY_EEE_LP_ABILITY);
259256
v = SFR_DATA_U16;
260-
if (v & PHY_EEE_BIT_1G)
261-
char_to_html('1');
262-
else
263-
char_to_html('0');
264-
if (v & PHY_EEE_BIT_100M)
265-
char_to_html('1');
266-
else
267-
char_to_html('0');
268-
slen += strtox(outbuf + slen, "\", \"active\": ");
269-
if (eee_ablty & (1 << i))
270-
char_to_html('1');
271-
else
272-
char_to_html('0');
257+
bool_to_html(v & PHY_EEE_BIT_1G);
258+
bool_to_html(v & PHY_EEE_BIT_100M);
259+
260+
slen += strtox(outbuf + slen, "\",\"active\":");
261+
bool_to_html(eee_ablty & (1 << i));
273262
}
274263
char_to_html('}');
275264
if (i < maxPort)
@@ -293,15 +282,10 @@ void send_status(void)
293282
else
294283
itoa_html(i + 1);
295284

296-
slen += strtox(outbuf + slen, ",\"isSFP\":");
297285
if (IS_SFP(i)) {
298-
char_to_html('1');
299-
slen += strtox(outbuf + slen, ",\"enabled\":");
300-
if (!((sfp_pins_last >> (i == maxPort ? 0 : 4)) & 1)) {
301-
char_to_html('1');
302-
} else {
303-
char_to_html('0');
304-
}
286+
slen += strtox(outbuf + slen, ",\"isSFP\":1,\"enabled\":");
287+
bool_to_html(!((sfp_pins_last >> (i == maxPort ? 0 : 4)) & 1));
288+
305289
slen += strtox(outbuf + slen, ",\"link\":");
306290
uint8_t rate = sfp_read_reg(i == maxPort ? 0 : 1, 12);
307291
if (rate == 0xd)
@@ -313,13 +297,10 @@ void send_status(void)
313297
else
314298
char_to_html('1'); // 100M ???
315299
} else {
316-
char_to_html('0');
317-
slen += strtox(outbuf + slen, ",\"enabled\":");
300+
slen += strtox(outbuf + slen, ",\"isSFP\":0,\"enabled\":");
318301
phy_read(i, 0x1f, 0xa610);
319-
if (SFR_DATA_8 == 0x20)
320-
char_to_html('1');
321-
else
322-
char_to_html('0');
302+
bool_to_html(SFR_DATA_8 == 0x20);
303+
323304
slen += strtox(outbuf + slen, ",\"link\":");
324305
reg_read_m(RTL837X_REG_LINKS);
325306
uint8_t b = sfr_data[3 - (i >> 1)];

0 commit comments

Comments
 (0)