2021-12-16 20:42:47 +01:00
|
|
|
#ifdef FEATURE_ESPNOW
|
|
|
|
|
#include "espnowfunctions.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
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
|
|
|
|
2021-12-13 02:14:03 +01:00
|
|
|
#include "globals.h"
|
2021-12-13 23:09:37 +01:00
|
|
|
#include "utils.h"
|
2021-12-14 14:16:53 +01:00
|
|
|
#include "time_bobbycar.h"
|
2021-12-13 02:14:03 +01:00
|
|
|
|
2021-12-16 20:42:47 +01:00
|
|
|
namespace espnow {
|
2021-12-17 23:30:53 +01:00
|
|
|
namespace {
|
|
|
|
|
constexpr const char * const TAG = "BOBBY_ESP_NOW";
|
|
|
|
|
} // namespace
|
2021-12-16 20:42:47 +01:00
|
|
|
|
2021-12-14 14:16:53 +01:00
|
|
|
uint16_t lastYear; // Used for esp-now timesync
|
|
|
|
|
|
2021-12-13 02:14:03 +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};
|
2021-12-13 02:14:03 +01:00
|
|
|
|
2021-12-14 15:23:58 +01:00
|
|
|
bool receiveTimeStamp{true};
|
|
|
|
|
bool receiveTsFromOtherBobbycars{true};
|
|
|
|
|
|
2021-12-18 00:24:56 +01:00
|
|
|
namespace {
|
|
|
|
|
extern "C" void onReceive(const uint8_t *mac_addr, 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
|
|
|
{
|
2021-12-18 00:10:58 +01:00
|
|
|
std::string_view msg_type = data_str.substr(0, sep_pos);
|
|
|
|
|
std::string_view msg = data_str.substr(sep_pos+1, data_str.length()-3); // - 3 may needs to be converted to sep_pos+1
|
|
|
|
|
ESP_LOGD(TAG, "Type: %.*s - Message: %.*s", msg_type.size(), msg_type.data(), msg.size(), msg.data());
|
2021-12-13 02:14:03 +01:00
|
|
|
|
2021-12-18 00:10:58 +01:00
|
|
|
message_queue.push_back(esp_now_message_t {
|
|
|
|
|
.content = std::string{msg},
|
|
|
|
|
.type = std::string{msg_type}
|
|
|
|
|
});
|
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()
|
|
|
|
|
{
|
2021-12-13 02:14:03 +01:00
|
|
|
ESP_LOGI(TAG, "Initializing esp-now...");
|
|
|
|
|
|
2021-12-13 23:09:37 +01:00
|
|
|
if (initialized < 1)
|
2021-12-13 02:14:03 +01:00
|
|
|
{
|
2021-12-14 22:00:18 +01:00
|
|
|
if (!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && 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))
|
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 02:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-13 23:09:37 +01:00
|
|
|
if (initialized < 2)
|
2021-12-13 02:14:03 +01:00
|
|
|
{
|
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 02:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-13 23:09:37 +01:00
|
|
|
if (initialized < 3)
|
2021-12-13 02:14:03 +01:00
|
|
|
{
|
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;
|
2021-12-13 02:14:03 +01:00
|
|
|
}
|
2021-12-13 23:09:37 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-13 23:09:37 +01:00
|
|
|
if (settings.wifiSettings.wifiApEnabled)
|
|
|
|
|
peer.ifidx = WIFI_IF_AP;
|
2021-12-17 23:28:48 +01:00
|
|
|
else if (settings.wifiSettings.wifiStaEnabled)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initialized = 255;
|
2021-12-14 22:00:18 +01:00
|
|
|
ESP_LOGI(TAG, "Init done.");
|
2021-12-13 02:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handle()
|
|
|
|
|
{
|
2021-12-14 22:00:18 +01:00
|
|
|
if (initialized < 255 && !(!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && 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)))
|
2021-12-13 23:09:37 +01:00
|
|
|
{
|
|
|
|
|
initESPNow();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-14 22:00:18 +01:00
|
|
|
else if (!settings.wifiSettings.wifiApEnabled && (!settings.wifiSettings.wifiStaEnabled && 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))
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-12-13 23:09:37 +01:00
|
|
|
|
2021-12-13 02:14:03 +01:00
|
|
|
if(message_queue.size())
|
|
|
|
|
{
|
|
|
|
|
for (const esp_now_message_t &msg : message_queue)
|
|
|
|
|
{
|
2021-12-13 23:09:37 +01:00
|
|
|
ESP_LOGD(TAG, "queue has processed message of type '%s' with content '%s'", msg.type.c_str(), msg.content.c_str());
|
2021-12-13 02:14:03 +01:00
|
|
|
message_queue.pop_front();
|
|
|
|
|
|
|
|
|
|
if (msg.type == "T")
|
|
|
|
|
{
|
2021-12-14 19:15:14 +01:00
|
|
|
if (!receiveTimeStamp || !settings.espnow.syncTime)
|
2021-12-14 14:16:53 +01:00
|
|
|
return;
|
|
|
|
|
|
2021-12-13 23:09:37 +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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-14 15:23:58 +01:00
|
|
|
else if (msg.type == "BOBBYT")
|
|
|
|
|
{
|
2021-12-14 19:15:14 +01:00
|
|
|
if (!receiveTsFromOtherBobbycars || !settings.espnow.syncTimeWithOthers)
|
2021-12-14 15:23:58 +01:00
|
|
|
return;
|
2021-12-14 19:15:14 +01:00
|
|
|
|
|
|
|
|
if (const auto result = cpputils::fromString<uint64_t>(msg.content); result)
|
|
|
|
|
{
|
2021-12-17 22:39:36 +01:00
|
|
|
ESP_LOGI(TAG, "setting current time to %" PRIu64, *result);
|
2021-12-14 19:15:14 +01:00
|
|
|
onRecvTs(*result, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGW(TAG, "could not parse number: %.*s", result.error().size(), result.error().data());
|
|
|
|
|
}
|
2021-12-14 15:23:58 +01:00
|
|
|
}
|
2021-12-13 23:09:37 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Unkown Type: %s - Message: %s", msg.type.c_str(), msg.content.c_str());
|
2021-12-13 02:14:03 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 15:23:58 +01:00
|
|
|
receiveTsFromOtherBobbycars = false;
|
2021-12-13 23:09:37 +01:00
|
|
|
}
|
2021-12-16 20:42:47 +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;
|
|
|
|
|
|
|
|
|
|
if (!settings.wifiSettings.wifiApEnabled && !settings.wifiSettings.wifiStaEnabled)
|
|
|
|
|
{
|
|
|
|
|
return ESP_ERR_ESPNOW_IF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (peers.size() < 1)
|
|
|
|
|
{
|
|
|
|
|
return ESP_FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (settings.wifiSettings.wifiApEnabled)
|
|
|
|
|
peer.ifidx = WIFI_IF_AP;
|
|
|
|
|
else if (settings.wifiSettings.wifiStaEnabled)
|
|
|
|
|
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();
|
|
|
|
|
ESP_LOGI(TAG, "Successfully executed esp_now_send(): Took %lldms", std::chrono::milliseconds{timeAfter-timeBefore}.count());
|
|
|
|
|
}
|
2021-12-13 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
2021-12-16 20:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace espnow
|
|
|
|
|
#endif
|