2020-12-26 00:10:38 +00:00
|
|
|
#include "airtime.h"
|
|
|
|
#include <Arduino.h>
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2020-12-27 18:50:52 +00:00
|
|
|
#define periodsToLog 48
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2021-01-09 04:43:51 +00:00
|
|
|
AirTime *airTime;
|
2021-01-01 20:31:46 +00:00
|
|
|
|
2020-12-27 06:39:43 +00:00
|
|
|
// A reminder that there are 3600 seconds in an hour so I don't have
|
|
|
|
// to keep googling it.
|
|
|
|
// This can be changed to a smaller number to speed up testing.
|
|
|
|
//
|
2020-12-27 18:50:52 +00:00
|
|
|
uint32_t secondsPerPeriod = 3600;
|
2020-12-27 06:39:43 +00:00
|
|
|
uint32_t lastMillis = 0;
|
|
|
|
uint32_t secSinceBoot = 0;
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2021-01-01 20:31:46 +00:00
|
|
|
// AirTime at;
|
|
|
|
|
2020-12-26 00:10:38 +00:00
|
|
|
// Don't read out of this directly. Use the helper functions.
|
|
|
|
struct airtimeStruct {
|
2021-01-02 05:20:34 +00:00
|
|
|
uint16_t periodTX[periodsToLog]; // AirTime transmitted
|
|
|
|
uint16_t periodRX[periodsToLog]; // AirTime received and repeated (Only valid mesh packets)
|
|
|
|
uint16_t periodRX_ALL[periodsToLog]; // AirTime received regardless of valid mesh packet. Could include noise.
|
2020-12-27 18:50:52 +00:00
|
|
|
uint8_t lastPeriodIndex;
|
2020-12-26 00:10:38 +00:00
|
|
|
} airtimes;
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2021-01-01 20:31:46 +00:00
|
|
|
void AirTime::logAirtime(reportTypes reportType, uint32_t airtime_ms)
|
2020-12-25 06:12:59 +00:00
|
|
|
{
|
2021-01-02 05:20:34 +00:00
|
|
|
// DEBUG_MSG("Packet - logAirtime()\n");
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2020-12-26 00:10:38 +00:00
|
|
|
if (reportType == TX_LOG) {
|
2021-01-02 05:20:34 +00:00
|
|
|
DEBUG_MSG("AirTime - Packet transmitted : %us %ums\n", (uint32_t)round((float)airtime_ms / (float)1000), airtime_ms);
|
2020-12-27 18:50:52 +00:00
|
|
|
airtimes.periodTX[0] = airtimes.periodTX[0] + round(airtime_ms / 1000);
|
2020-12-26 00:10:38 +00:00
|
|
|
} else if (reportType == RX_LOG) {
|
2021-01-02 05:20:34 +00:00
|
|
|
DEBUG_MSG("AirTime - Packet received : %us %ums\n", (uint32_t)round((float)airtime_ms / (float)1000), airtime_ms);
|
2020-12-27 18:50:52 +00:00
|
|
|
airtimes.periodRX[0] = airtimes.periodRX[0] + round(airtime_ms / 1000);
|
2020-12-26 00:10:38 +00:00
|
|
|
} else if (reportType == RX_ALL_LOG) {
|
2021-01-02 05:20:34 +00:00
|
|
|
DEBUG_MSG("AirTime - Packet received (noise?) : %us %ums\n", (uint32_t)round((float)airtime_ms / (float)1000), airtime_ms);
|
2020-12-27 18:50:52 +00:00
|
|
|
airtimes.periodRX_ALL[0] = airtimes.periodRX_ALL[0] + round(airtime_ms / 1000);
|
2020-12-26 00:10:38 +00:00
|
|
|
} else {
|
2021-01-02 05:20:34 +00:00
|
|
|
DEBUG_MSG("AirTime - Unknown report time. This should never happen!!\n");
|
2020-12-26 00:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2020-12-27 18:50:52 +00:00
|
|
|
uint8_t currentPeriodIndex()
|
2020-12-26 00:10:38 +00:00
|
|
|
{
|
2020-12-27 18:50:52 +00:00
|
|
|
return ((getSecondsSinceBoot() / secondsPerPeriod) % periodsToLog);
|
2020-12-26 00:10:38 +00:00
|
|
|
}
|
2020-12-25 06:12:59 +00:00
|
|
|
|
2021-01-01 20:31:46 +00:00
|
|
|
void airtimeRotatePeriod()
|
2020-12-26 00:10:38 +00:00
|
|
|
{
|
2021-01-01 20:31:46 +00:00
|
|
|
|
|
|
|
if (airtimes.lastPeriodIndex != currentPeriodIndex()) {
|
|
|
|
DEBUG_MSG("Rotating airtimes to a new period = %u\n", currentPeriodIndex());
|
|
|
|
|
|
|
|
for (int i = periodsToLog - 2; i >= 0; --i) {
|
|
|
|
airtimes.periodTX[i + 1] = airtimes.periodTX[i];
|
|
|
|
airtimes.periodRX[i + 1] = airtimes.periodRX[i];
|
|
|
|
airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i];
|
2020-12-27 06:39:43 +00:00
|
|
|
}
|
2021-01-01 20:31:46 +00:00
|
|
|
airtimes.periodTX[0] = 0;
|
|
|
|
airtimes.periodRX[0] = 0;
|
|
|
|
airtimes.periodRX_ALL[0] = 0;
|
|
|
|
|
|
|
|
airtimes.lastPeriodIndex = currentPeriodIndex();
|
2020-12-26 00:10:38 +00:00
|
|
|
}
|
2020-12-25 06:12:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:10:38 +00:00
|
|
|
uint16_t *airtimeReport(reportTypes reportType)
|
2020-12-25 06:12:59 +00:00
|
|
|
{
|
2020-12-26 00:10:38 +00:00
|
|
|
|
2020-12-27 06:39:43 +00:00
|
|
|
if (reportType == TX_LOG) {
|
2020-12-27 18:50:52 +00:00
|
|
|
return airtimes.periodTX;
|
2020-12-27 06:39:43 +00:00
|
|
|
} else if (reportType == RX_LOG) {
|
2020-12-27 18:50:52 +00:00
|
|
|
return airtimes.periodRX;
|
2020-12-27 06:39:43 +00:00
|
|
|
} else if (reportType == RX_ALL_LOG) {
|
2020-12-27 18:50:52 +00:00
|
|
|
return airtimes.periodRX_ALL;
|
2020-12-26 00:10:38 +00:00
|
|
|
}
|
2020-12-27 06:39:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-26 00:10:38 +00:00
|
|
|
|
2020-12-27 18:50:52 +00:00
|
|
|
uint8_t getPeriodsToLog()
|
|
|
|
{
|
|
|
|
return periodsToLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getSecondsPerPeriod()
|
2020-12-27 06:39:43 +00:00
|
|
|
{
|
2020-12-27 18:50:52 +00:00
|
|
|
return secondsPerPeriod;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getSecondsSinceBoot()
|
|
|
|
{
|
|
|
|
return secSinceBoot;
|
|
|
|
}
|
2021-01-01 20:31:46 +00:00
|
|
|
|
|
|
|
AirTime::AirTime() : concurrency::OSThread("AirTime") {}
|
|
|
|
|
|
|
|
int32_t AirTime::runOnce()
|
|
|
|
{
|
2021-01-06 07:21:14 +00:00
|
|
|
//DEBUG_MSG("AirTime::runOnce()\n");
|
2021-01-01 20:31:46 +00:00
|
|
|
|
|
|
|
airtimeRotatePeriod();
|
|
|
|
secSinceBoot++;
|
|
|
|
|
2021-01-02 05:20:34 +00:00
|
|
|
/*
|
|
|
|
This actually doesn't need to be run once per second but we currently use it for the
|
|
|
|
secSinceBoot counter.
|
|
|
|
|
|
|
|
If we have a better counter of how long the device has been online (and not millis())
|
|
|
|
then we can change this to something less frequent. Maybe once ever 5 seconds?
|
|
|
|
*/
|
|
|
|
return (1000 * 1);
|
2021-01-01 20:31:46 +00:00
|
|
|
}
|