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
|
|
|
|
2020-10-08 14:34:54 +00:00
|
|
|
static unsigned long int cached_rxb_diff = 0;
|
|
|
|
static unsigned long int cached_rxp_diff = 0;
|
|
|
|
static unsigned long int cached_txb_diff = 0;
|
|
|
|
static unsigned long int cached_txp_diff = 0;
|
|
|
|
|
2020-11-24 17:31:03 +00:00
|
|
|
static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) {
|
2020-10-08 14:34:54 +00:00
|
|
|
static unsigned long long int cached_last_update = 0;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
unsigned long long int timeInMilliSeconds = (unsigned long long int)tv.tv_sec * 1000 + (unsigned long long int)tv.tv_usec / 1000;
|
|
|
|
unsigned long long int passedTimeInMs = timeInMilliSeconds - cached_last_update;
|
|
|
|
|
|
|
|
/* update only every 500ms */
|
|
|
|
if (passedTimeInMs > 500) {
|
2020-11-21 23:53:12 +00:00
|
|
|
static unsigned long int cached_rxb_total = 0;
|
|
|
|
static unsigned long int cached_rxp_total = 0;
|
|
|
|
static unsigned long int cached_txb_total = 0;
|
|
|
|
static unsigned long int cached_txp_total = 0;
|
|
|
|
|
2020-10-20 19:40:51 +00:00
|
|
|
cached_last_update = timeInMilliSeconds;
|
|
|
|
|
2020-10-08 14:34:54 +00:00
|
|
|
unsigned long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted;
|
|
|
|
|
2020-10-20 19:40:51 +00:00
|
|
|
hasData = Platform_getNetworkIO(&bytesReceived, &packetsReceived, &bytesTransmitted, &packetsTransmitted);
|
|
|
|
if (!hasData) {
|
|
|
|
xSnprintf(buffer, len, "no data");
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (bytesReceived > cached_rxb_total) {
|
|
|
|
cached_rxb_diff = (bytesReceived - cached_rxb_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
cached_rxb_diff = 1000.0 * cached_rxb_diff / passedTimeInMs; /* convert to per second */
|
|
|
|
} else {
|
|
|
|
cached_rxb_diff = 0;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
cached_rxb_total = bytesReceived;
|
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (packetsReceived > cached_rxp_total) {
|
|
|
|
cached_rxp_diff = packetsReceived - cached_rxp_total;
|
|
|
|
} else {
|
|
|
|
cached_rxp_diff = 0;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
cached_rxp_total = packetsReceived;
|
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (bytesTransmitted > cached_txb_total) {
|
|
|
|
cached_txb_diff = (bytesTransmitted - cached_txb_total) / 1024; /* Meter_humanUnit() expects unit in kilo */
|
|
|
|
cached_txb_diff = 1000.0 * cached_txb_diff / passedTimeInMs; /* convert to per second */
|
|
|
|
} else {
|
|
|
|
cached_txb_diff = 0;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
cached_txb_total = bytesTransmitted;
|
|
|
|
|
2020-11-02 13:46:42 +00:00
|
|
|
if (packetsTransmitted > cached_txp_total) {
|
|
|
|
cached_txp_diff = packetsTransmitted - cached_txp_total;
|
|
|
|
} else {
|
|
|
|
cached_txp_diff = 0;
|
|
|
|
}
|
2020-10-08 14:34:54 +00:00
|
|
|
cached_txp_total = packetsTransmitted;
|
|
|
|
}
|
|
|
|
|
|
|
|
char bufferBytesReceived[12], bufferBytesTransmitted[12];
|
|
|
|
Meter_humanUnit(bufferBytesReceived, cached_rxb_diff, sizeof(bufferBytesReceived));
|
|
|
|
Meter_humanUnit(bufferBytesTransmitted, cached_txb_diff, sizeof(bufferBytesTransmitted));
|
|
|
|
xSnprintf(buffer, len, "rx:%siB/s tx:%siB/s", bufferBytesReceived, bufferBytesTransmitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
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
|
|
|
|
|
|
|
xSnprintf(buffer, sizeof(buffer), " (%lu/%lu packets) ", cached_rxp_diff, cached_txp_diff);
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], buffer);
|
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,
|
|
|
|
.maxItems = 0,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = NetworkIOMeter_attributes,
|
|
|
|
.name = "NetworkIO",
|
|
|
|
.uiName = "Network IO",
|
|
|
|
.caption = "Network: "
|
|
|
|
};
|