Files
bobbycar-boardcomputer-firm…/main/espnowfunctions.cpp

317 lines
9.3 KiB
C++
Raw Permalink Normal View History

2021-12-16 20:42:47 +01:00
#include "espnowfunctions.h"
2022-03-24 22:09:51 +01:00
// 3rdparty lib includes
2021-12-13 23:09:37 +01:00
#include <espchrono.h>
2021-12-16 20:42:47 +01:00
#include <esp_log.h>
2021-12-13 23:09:37 +01:00
#include <numberparsing.h>
2021-12-14 22:00:18 +01:00
#include <espwifistack.h>
2021-12-16 20:42:47 +01:00
2022-03-24 22:09:51 +01:00
// local includes
#include "globals.h"
2021-12-13 23:09:37 +01:00
#include "utils.h"
#include "time_bobbycar.h"
2021-12-29 00:53:06 +01:00
#include "newsettings.h"
2022-03-24 22:09:51 +01:00
#include "bobbyhupe.h"
2022-05-03 01:33:15 +02:00
#include "bobbyblinker.h"
2021-12-16 20:42:47 +01:00
namespace espnow {
uint16_t lastYear; // Used for esp-now timesync
2022-01-03 00:07:46 +01:00
std::deque<esp_now_message_t> message_queue{};
2021-12-18 00:10:58 +01:00
std::vector<esp_now_peer_info_t> peers{};
2021-12-13 23:09:37 +01:00
uint8_t initialized{0};
bool receiveTimeStamp{true};
bool receiveTsFromOtherBobbycars{true};
2022-01-04 19:13:20 +01:00
namespace {
constexpr const char * const TAG = "BOBBY_ESP_NOW";
2022-01-05 01:21:42 +01:00
} // namespace
2022-01-04 19:13:20 +01:00
2022-01-05 01:21:42 +01:00
bool espnow_init_allowed()
{
2022-01-04 19:13:20 +01:00
const auto wifi_mode = wifi_stack::get_wifi_mode();
return
(
2022-04-29 22:40:49 +02:00
(configs.espnow.syncBlink.value() || configs.espnow.syncTime.value() || configs.espnow.syncTimeWithOthers.value())
2022-01-04 19:13:20 +01:00
&&
(
2022-04-29 22:40:49 +02:00
(configs.wifiApEnabled.value() && wifi_stack::get_wifi_mode() == WIFI_MODE_AP)
2022-01-04 19:13:20 +01:00
||
(
2022-04-29 22:40:49 +02:00
configs.wifiStaEnabled.value()
2022-01-04 19:13:20 +01:00
&&
wifi_stack::get_sta_status() != wifi_stack::WiFiStaStatus::NO_SHIELD
&&
(
wifi_mode == WIFI_MODE_STA ||
wifi_mode == WIFI_MODE_APSTA
)
)
)
);
}
2021-12-18 00:24:56 +01:00
namespace {
2022-12-23 00:02:52 +01:00
extern "C" void onReceive(const esp_now_recv_info* info, const uint8_t *data, int data_len)
2021-12-16 20:42:47 +01:00
{
2021-12-13 23:09:37 +01:00
ESP_LOGD(TAG, "Received data");
2021-12-18 00:24:56 +01:00
const std::string_view data_str{(const char *)data, size_t(data_len)};
2021-12-16 20:42:47 +01:00
size_t sep_pos = data_str.find(":");
2021-12-18 00:10:58 +01:00
if (std::string_view::npos != sep_pos)
2021-12-16 20:42:47 +01:00
{
2022-01-01 19:59:49 +01:00
esp_now_message_t msg{
.content = std::string{data_str.substr(sep_pos+1, data_str.length()-sep_pos-1)},
.type = std::string{data_str.substr(0, sep_pos)}
};
ESP_LOGD(TAG, "Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str());
2022-01-03 00:07:46 +01:00
message_queue.push_back(msg);
2021-12-16 20:42:47 +01:00
}
else
{
2021-12-18 00:10:58 +01:00
ESP_LOGW(TAG, "Invalid message: Could not find ':' (%.*s)", data_str.size(), data_str.data());
2021-12-16 20:42:47 +01:00
}
}
2021-12-18 00:24:56 +01:00
} // namespace
2021-12-16 20:42:47 +01:00
void initESPNow()
{
ESP_LOGI(TAG, "Initializing esp-now...");
2021-12-13 23:09:37 +01:00
if (initialized < 1)
{
2022-04-29 22:40:49 +02:00
if (!configs.wifiApEnabled.value() && (!configs.wifiStaEnabled.value() && wifi_stack::get_sta_status() == wifi_stack::WiFiStaStatus::NO_SHIELD) || (wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_STA && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_AP && wifi_stack::get_wifi_mode() != wifi_mode_t::WIFI_MODE_APSTA) && (configs.espnow.syncBlink.value() || configs.espnow.syncTime.value() || configs.espnow.syncTimeWithOthers.value()))
2021-12-13 23:09:37 +01:00
{
ESP_LOGW(TAG, "cannot execute esp_now_init(): tcp stack is down.");
return;
}
else
initialized = 1;
}
2021-12-13 23:09:37 +01:00
if (initialized < 2)
{
2021-12-13 23:09:37 +01:00
if (const auto error = esp_now_init(); error != ESP_OK)
{
ESP_LOGE(TAG, "esp_now_init() failed with %s", esp_err_to_name(error));
return;
}
else
initialized = 2;
}
2021-12-13 23:09:37 +01:00
if (initialized < 3)
{
2021-12-13 23:09:37 +01:00
if (const auto error = esp_now_register_recv_cb(onReceive); error != ESP_OK)
{
ESP_LOGE(TAG, "esp_now_register_recv_cb() failed with %s", esp_err_to_name(error));
return;
}
else
initialized = 3;
}
if (initialized < 4)
{
peers.push_back(esp_now_peer_info_t{});
esp_now_peer_info_t &peer = peers.back();
std::memcpy(peer.peer_addr, broadcast_address, sizeof(peer.peer_addr));
peer.channel = 0;
2021-12-17 23:28:48 +01:00
2022-04-29 22:40:49 +02:00
if (configs.wifiApEnabled.value())
2021-12-13 23:09:37 +01:00
peer.ifidx = WIFI_IF_AP;
2022-04-29 22:40:49 +02:00
else if (configs.wifiStaEnabled.value())
2021-12-13 23:09:37 +01:00
peer.ifidx = WIFI_IF_STA;
2021-12-17 23:28:48 +01:00
else
{
ESP_LOGE(TAG, "Interfaces not ready.");
return;
}
2021-12-13 23:09:37 +01:00
if (const auto error = esp_now_add_peer(&peers.back()); error != ESP_OK)
{
ESP_LOGE(TAG, "esp_now_add_peer() failed with %s", esp_err_to_name(error));
return;
}
else
initialized = 4;
}
2021-12-13 23:09:37 +01:00
initialized = 255;
2021-12-14 22:00:18 +01:00
ESP_LOGI(TAG, "Init done.");
}
void handle()
{
2022-03-24 22:09:51 +01:00
bobbyhupe::handle_hupe();
2022-05-03 01:33:15 +02:00
bobbyblinker::handle_blinker();
2022-01-04 19:13:20 +01:00
if (initialized < 255 && espnow_init_allowed())
2021-12-13 23:09:37 +01:00
{
initESPNow();
return;
}
2022-01-04 19:13:20 +01:00
else if (!espnow_init_allowed() && initialized >= 255)
2021-12-14 22:00:18 +01:00
{
if (initialized > 0)
{
if (initialized >= 4) // peer
{
for (const auto &peer : peers)
{
if (const auto error = esp_now_del_peer(peer.peer_addr); error != ESP_OK)
{
2021-12-16 21:10:11 +01:00
if (error == ESP_ERR_ESPNOW_NOT_FOUND)
{
initialized = 0;
return;
}
2021-12-14 22:00:18 +01:00
ESP_LOGE(TAG, "esp_now_del_peer() failed with %s", esp_err_to_name(error));
return;
}
}
initialized--;
}
if (initialized >= 3) // callback
{
if (const auto error = esp_now_unregister_recv_cb(); error != ESP_OK)
{
ESP_LOGE(TAG, "esp_now_unregister_recv_cb() failed with %s", esp_err_to_name(error));
return;
}
else if (error == ESP_ERR_ESPNOW_NOT_FOUND)
{
initialized = 0;
}
else
initialized--;
}
if (initialized >= 2) // esp deinit
{
if (const auto error = esp_now_deinit(); error != ESP_OK)
{
ESP_LOGE(TAG, "esp_now_deinit() failed with %s", esp_err_to_name(error));
return;
}
else if (error == ESP_ERR_ESPNOW_NOT_FOUND)
{
initialized = 0;
}
else
initialized--;
}
initialized = 0;
ESP_LOGI(TAG, "Deinit done.");
}
return;
}
2022-01-03 00:07:46 +01:00
if (message_queue.size())
{
for (const esp_now_message_t &msg : message_queue)
{
if (msg.type == "T")
{
2022-04-29 22:40:49 +02:00
if (!receiveTimeStamp || !configs.espnow.syncTime.value())
2022-01-04 19:13:20 +01:00
goto clear;
2022-01-03 00:07:46 +01:00
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
{
onRecvTs(*result);
}
else
{
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
}
}
else if (msg.type == "BOBBYT")
{
2022-04-29 22:40:49 +02:00
if (!receiveTsFromOtherBobbycars || !configs.espnow.syncTimeWithOthers.value())
2022-01-04 19:13:20 +01:00
goto clear;
2022-01-03 00:07:46 +01:00
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
{
ESP_LOGI(TAG, "setting current time to %" PRIu64, *result);
onRecvTs(*result, true);
}
else
{
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
}
}
else
{
ESP_LOGI(TAG, "Unknown Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str());
2022-01-03 00:07:46 +01:00
}
}
2022-01-04 19:13:20 +01:00
clear:
message_queue.erase(std::begin(message_queue), std::end(message_queue));
2022-01-03 00:07:46 +01:00
}
2021-12-16 20:42:47 +01:00
}
2021-12-14 19:15:14 +01:00
void onRecvTs(uint64_t millis, bool isFromBobbycar)
2021-12-16 20:42:47 +01:00
{
2021-12-13 23:09:37 +01:00
const auto milliseconds = std::chrono::milliseconds(millis);
const auto timepoint = espchrono::utc_clock::time_point(milliseconds);
2021-12-17 22:39:36 +01:00
// ESP_LOGW(TAG, "setting current time to %s", espchrono::toString(timepoint.time_since_epoch()).c_str());
2021-12-13 23:09:37 +01:00
time_set_now(timepoint);
2021-12-14 19:15:14 +01:00
if (receiveTimeStamp)
{
if (const auto thisYear = int(espchrono::toDateTime(espchrono::utc_clock::now()).date.year()); abs(thisYear - espnow::lastYear) > 1)
{
espnow::lastYear = thisYear;
receiveTimeStamp = false;
}
}
receiveTsFromOtherBobbycars = false;
2021-12-13 23:09:37 +01:00
}
2021-12-18 00:10:58 +01:00
esp_err_t send_espnow_message(std::string_view message)
2021-12-13 23:09:37 +01:00
{
if (initialized < 255)
return ESP_ERR_ESPNOW_NOT_INIT;
2022-04-29 22:40:49 +02:00
if (!configs.wifiApEnabled.value() && !configs.wifiStaEnabled.value())
2021-12-13 23:09:37 +01:00
{
return ESP_ERR_ESPNOW_IF;
}
if (peers.size() < 1)
{
return ESP_FAIL;
}
2021-12-16 20:42:47 +01:00
2021-12-17 23:28:48 +01:00
for (auto &peer : peers)
2021-12-13 23:09:37 +01:00
{
2021-12-17 23:28:48 +01:00
2022-04-29 22:40:49 +02:00
if (configs.wifiApEnabled.value())
2021-12-17 23:28:48 +01:00
peer.ifidx = WIFI_IF_AP;
2022-04-29 22:40:49 +02:00
else if (configs.wifiStaEnabled.value())
2021-12-17 23:28:48 +01:00
peer.ifidx = WIFI_IF_STA;
else
return ESP_ERR_ESPNOW_IF;
const auto timeBefore = espchrono::millis_clock::now();
if(const auto error = esp_now_send(broadcast_address, (const uint8_t*)message.data(), message.size()); error != ESP_OK)
2021-12-13 23:09:37 +01:00
{
return error;
}
else
2021-12-17 23:28:48 +01:00
{
const auto timeAfter = espchrono::millis_clock::now();
2022-05-11 20:29:09 +02:00
ESP_LOGI(TAG, "Successfully executed esp_now_send(): Took %lldms", std::chrono::floor<std::chrono::milliseconds>(timeAfter-timeBefore).count());
2021-12-17 23:28:48 +01:00
}
2021-12-13 23:09:37 +01:00
}
return ESP_OK;
2021-12-16 20:42:47 +01:00
}
} // namespace espnow