2020-10-08 14:34:54 +00:00
|
|
|
#include "NetworkIOMeter.h"
|
|
|
|
|
2020-11-18 13:26:30 +00:00
|
|
|
#include <stdbool.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2020-10-08 14:34:54 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
2020-10-08 14:34:54 +00:00
|
|
|
#include "Platform.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "RichString.h"
|
|
|
|
#include "XUtils.h"
|
2020-10-08 14:34:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const int NetworkIOMeter_attributes[] = {
|
|
|
|
METER_VALUE_IOREAD,
|
|
|
|
METER_VALUE_IOWRITE,
|
|
|
|
};
|
|
|
|
|
2020-10-20 19:40:51 +00:00
|
|
|
static bool hasData = false;
|
2020-11-21 23:53:12 +00:00
|
|
|
|
2021-03-02 01:14:44 +00:00
|
|
|
static uint32_t cached_rxb_diff;
|
|
|
|
static uint32_t cached_rxp_diff;
|
|
|
|
static uint32_t cached_txb_diff;
|
|
|
|
static uint32_t cached_txp_diff;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2020-10-06 11:13:16 +00:00
|
|
|
static void NetworkIOMeter_updateValues(Meter* this) {
|
2021-03-23 06:27:05 +00:00
|
|
|
const ProcessList* pl = this->pl;
|
2021-03-02 01:14:44 +00:00
|
|
|
static uint64_t cached_last_update = 0;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-03-30 04:55:48 +00:00
|
|
|
uint64_t passedTimeInMs = pl->realtimeMs - cached_last_update;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
|
|
|
/* update only every 500ms */
|
|
|
|
if (passedTimeInMs > 500) {
|
2021-03-02 01:14:44 +00:00
|
|
|
static uint64_t cached_rxb_total;
|
|
|
|
static uint64_t cached_rxp_total;
|
|
|
|
static uint64_t cached_txb_total;
|
|
|
|
static uint64_t cached_txp_total;
|
|
|
|
uint64_t diff;
|
2020-11-21 23:53:12 +00:00
|
|
|
|
2021-03-30 04:55:48 +00:00
|
|
|
cached_last_update = pl->realtimeMs;
|
2020-10-20 19:40:51 +00:00
|
|
|
|
2021-03-01 00:55:15 +00:00
|
|
|
NetworkIOData data;
|
|
|
|
hasData = Platform_getNetworkIO(&data);
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!hasData) {
|
2020-10-06 11:13:16 +00:00
|
|
|
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data");
|
2020-10-20 19:40:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-03-01 00:55:15 +00:00
|
|
|
if (data.bytesReceived > cached_rxb_total) {
|
|
|
|
diff = data.bytesReceived - cached_rxb_total;
|
|
|
|
diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_rxb_diff = (uint32_t)diff;
|
2020-11-02 13:46:42 +00:00
|
|
|
} else {
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_rxb_diff = 0;
|
2020-11-02 13:46:42 +00:00
|
|
|
}
|
2021-03-01 00:55:15 +00:00
|
|
|
cached_rxb_total = data.bytesReceived;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-03-01 00:55:15 +00:00
|
|
|
if (data.packetsReceived > cached_rxp_total) {
|
|
|
|
diff = data.packetsReceived - cached_rxp_total;
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_rxp_diff = (uint32_t)diff;
|
2020-11-02 13:46:42 +00:00
|
|
|
} else {
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_rxp_diff = 0;
|
2020-11-02 13:46:42 +00:00
|
|
|
}
|
2021-03-01 00:55:15 +00:00
|
|
|
cached_rxp_total = data.packetsReceived;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-03-01 00:55:15 +00:00
|
|
|
if (data.bytesTransmitted > cached_txb_total) {
|
|
|
|
diff = data.bytesTransmitted - cached_txb_total;
|
|
|
|
diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_txb_diff = (uint32_t)diff;
|
2020-11-02 13:46:42 +00:00
|
|
|
} else {
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_txb_diff = 0;
|
2020-11-02 13:46:42 +00:00
|
|
|
}
|
2021-03-01 00:55:15 +00:00
|
|
|
cached_txb_total = data.bytesTransmitted;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-03-01 00:55:15 +00:00
|
|
|
if (data.packetsTransmitted > cached_txp_total) {
|
|
|
|
diff = data.packetsTransmitted - cached_txp_total;
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_txp_diff = (uint32_t)diff;
|
2020-11-02 13:46:42 +00:00
|
|
|
} else {
|
2021-03-02 01:14:44 +00:00
|
|
|
cached_txp_diff = 0;
|
2020-11-02 13:46:42 +00:00
|
|
|
}
|
2021-03-01 00:55:15 +00:00
|
|
|
cached_txp_total = data.packetsTransmitted;
|
2020-10-08 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 21:19:34 +00:00
|
|
|
this->values[0] = cached_rxb_diff;
|
|
|
|
this->values[1] = cached_txb_diff;
|
|
|
|
if (cached_rxb_diff + cached_txb_diff > this->total) {
|
|
|
|
this->total = cached_rxb_diff + cached_txb_diff;
|
|
|
|
}
|
|
|
|
|
2020-10-08 14:34:54 +00:00
|
|
|
char bufferBytesReceived[12], bufferBytesTransmitted[12];
|
|
|
|
Meter_humanUnit(bufferBytesReceived, cached_rxb_diff, sizeof(bufferBytesReceived));
|
|
|
|
Meter_humanUnit(bufferBytesTransmitted, cached_txb_diff, sizeof(bufferBytesTransmitted));
|
2020-10-06 11:13:16 +00:00
|
|
|
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s", bufferBytesReceived, bufferBytesTransmitted);
|
2020-10-08 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
|
2020-10-20 19:40:51 +00:00
|
|
|
if (!hasData) {
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data");
|
2020-10-20 19:40:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:20:19 +00:00
|
|
|
char buffer[64];
|
2021-04-14 18:47:42 +00:00
|
|
|
int len;
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeAscii(out, CRT_colors[METER_TEXT], "rx: ");
|
2020-10-08 14:34:54 +00:00
|
|
|
Meter_humanUnit(buffer, cached_rxb_diff, sizeof(buffer));
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer);
|
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], "iB/s");
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " tx: ");
|
2020-10-08 14:34:54 +00:00
|
|
|
Meter_humanUnit(buffer, cached_txb_diff, sizeof(buffer));
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer);
|
|
|
|
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s");
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2021-04-14 18:47:42 +00:00
|
|
|
len = xSnprintf(buffer, sizeof(buffer), " (%u/%u packets) ", cached_rxp_diff, cached_txp_diff);
|
|
|
|
RichString_appendnAscii(out, CRT_colors[METER_TEXT], buffer, len);
|
2020-10-08 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MeterClass NetworkIOMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = NetworkIOMeter_display
|
|
|
|
},
|
|
|
|
.updateValues = NetworkIOMeter_updateValues,
|
|
|
|
.defaultMode = TEXT_METERMODE,
|
2021-02-26 21:19:34 +00:00
|
|
|
.maxItems = 2,
|
2020-10-08 14:34:54 +00:00
|
|
|
.total = 100.0,
|
|
|
|
.attributes = NetworkIOMeter_attributes,
|
|
|
|
.name = "NetworkIO",
|
|
|
|
.uiName = "Network IO",
|
|
|
|
.caption = "Network: "
|
|
|
|
};
|