初始化提交
This commit is contained in:
168
arduino-cli/libraries/WifiEspNow/src/WifiEspNow.cpp
Normal file
168
arduino-cli/libraries/WifiEspNow/src/WifiEspNow.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
#include "WifiEspNow.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#include <c_types.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#include <esp_now.h>
|
||||
#else
|
||||
#error "This library supports ESP8266 and ESP32 only."
|
||||
#endif
|
||||
|
||||
WifiEspNowClass WifiEspNow;
|
||||
|
||||
bool
|
||||
WifiEspNowClass::begin()
|
||||
{
|
||||
end();
|
||||
m_ready =
|
||||
esp_now_init() == 0 &&
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
esp_now_set_self_role(ESP_NOW_ROLE_COMBO) == 0 &&
|
||||
#endif
|
||||
esp_now_register_recv_cb(reinterpret_cast<esp_now_recv_cb_t>(WifiEspNowClass::rx)) == 0 &&
|
||||
esp_now_register_send_cb(reinterpret_cast<esp_now_send_cb_t>(WifiEspNowClass::tx)) == 0;
|
||||
return m_ready;
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowClass::end()
|
||||
{
|
||||
if (!m_ready) {
|
||||
return;
|
||||
}
|
||||
esp_now_deinit();
|
||||
m_ready = false;
|
||||
}
|
||||
|
||||
bool
|
||||
WifiEspNowClass::setPrimaryKey(const uint8_t key[WIFIESPNOW_KEYLEN])
|
||||
{
|
||||
return m_ready && key != nullptr &&
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
esp_now_set_kok(const_cast<u8*>(key), WIFIESPNOW_KEYLEN) == 0;
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
esp_now_set_pmk(key) == ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
WifiEspNowClass::listPeers(WifiEspNowPeerInfo* peers, int maxPeers) const
|
||||
{
|
||||
if (!m_ready) {
|
||||
return 0;
|
||||
}
|
||||
int n = 0;
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
for (u8* mac = esp_now_fetch_peer(true); mac != nullptr; mac = esp_now_fetch_peer(false)) {
|
||||
uint8_t channel = static_cast<uint8_t>(esp_now_get_peer_channel(mac));
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
esp_now_peer_info_t peer;
|
||||
for (esp_err_t e = esp_now_fetch_peer(true, &peer); e == ESP_OK;
|
||||
e = esp_now_fetch_peer(false, &peer)) {
|
||||
uint8_t* mac = peer.peer_addr;
|
||||
uint8_t channel = peer.channel;
|
||||
#endif
|
||||
if (n < maxPeers) {
|
||||
memcpy(peers[n].mac, mac, 6);
|
||||
peers[n].channel = channel;
|
||||
}
|
||||
++n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
bool
|
||||
WifiEspNowClass::hasPeer(const uint8_t mac[WIFIESPNOW_ALEN]) const
|
||||
{
|
||||
return m_ready &&
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
esp_now_is_peer_exist(const_cast<u8*>(mac)) > 0;
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
esp_now_is_peer_exist(mac);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
bool
|
||||
WifiEspNowClass::addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel,
|
||||
const uint8_t key[WIFIESPNOW_KEYLEN])
|
||||
{
|
||||
if (!m_ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->hasPeer(mac)) {
|
||||
return esp_now_set_peer_channel(const_cast<u8*>(mac), static_cast<u8>(channel)) == 0 &&
|
||||
esp_now_set_peer_key(const_cast<u8*>(mac), const_cast<u8*>(key),
|
||||
key == nullptr ? 0 : WIFIESPNOW_KEYLEN) == 0;
|
||||
}
|
||||
return esp_now_add_peer(const_cast<u8*>(mac), ESP_NOW_ROLE_SLAVE, static_cast<u8>(channel),
|
||||
const_cast<u8*>(key), key == nullptr ? 0 : WIFIESPNOW_KEYLEN) == 0;
|
||||
}
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
bool
|
||||
WifiEspNowClass::addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel,
|
||||
const uint8_t key[WIFIESPNOW_KEYLEN], int netif)
|
||||
{
|
||||
if (!m_ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_now_peer_info_t pi{};
|
||||
static_assert(WIFIESPNOW_ALEN == sizeof(pi.peer_addr), "");
|
||||
std::copy_n(mac, WIFIESPNOW_ALEN, pi.peer_addr);
|
||||
if (key != nullptr) {
|
||||
static_assert(WIFIESPNOW_KEYLEN == sizeof(pi.lmk), "");
|
||||
std::copy_n(key, WIFIESPNOW_KEYLEN, pi.lmk);
|
||||
pi.encrypt = true;
|
||||
}
|
||||
pi.channel = static_cast<uint8_t>(channel);
|
||||
pi.ifidx = static_cast<wifi_interface_t>(netif);
|
||||
|
||||
if (hasPeer(mac)) {
|
||||
return esp_now_mod_peer(&pi) == ESP_OK;
|
||||
}
|
||||
return esp_now_add_peer(&pi) == ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool
|
||||
WifiEspNowClass::removePeer(const uint8_t mac[WIFIESPNOW_ALEN])
|
||||
{
|
||||
return m_ready && esp_now_del_peer(const_cast<uint8_t*>(mac)) == 0;
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowClass::onReceive(RxCallback cb, void* arg)
|
||||
{
|
||||
m_rxCb = cb;
|
||||
m_rxArg = arg;
|
||||
}
|
||||
|
||||
bool
|
||||
WifiEspNowClass::send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count)
|
||||
{
|
||||
if (!m_ready || count > WIFIESPNOW_MAXMSGLEN || count == 0) {
|
||||
return false;
|
||||
}
|
||||
WifiEspNow.m_txRes = WifiEspNowSendStatus::NONE;
|
||||
return esp_now_send(const_cast<uint8_t*>(mac), const_cast<uint8_t*>(buf),
|
||||
static_cast<int>(count)) == 0;
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowClass::rx(const uint8_t* mac, const uint8_t* data, uint8_t len)
|
||||
{
|
||||
if (WifiEspNow.m_rxCb != nullptr) {
|
||||
(*WifiEspNow.m_rxCb)(mac, data, len, WifiEspNow.m_rxArg);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowClass::tx(const uint8_t* mac, uint8_t status)
|
||||
{
|
||||
WifiEspNow.m_txRes = status == 0 ? WifiEspNowSendStatus::OK : WifiEspNowSendStatus::FAIL;
|
||||
}
|
||||
148
arduino-cli/libraries/WifiEspNow/src/WifiEspNow.h
Normal file
148
arduino-cli/libraries/WifiEspNow/src/WifiEspNow.h
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @mainpage WifiEspNow
|
||||
*
|
||||
* https://github.com/yoursunny/WifiEspNow
|
||||
*/
|
||||
|
||||
#ifndef WIFIESPNOW_H
|
||||
#define WIFIESPNOW_H
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
/** @brief Address length. */
|
||||
static const int WIFIESPNOW_ALEN = 6;
|
||||
|
||||
/** @brief Key length. */
|
||||
static const int WIFIESPNOW_KEYLEN = 16;
|
||||
|
||||
/** @brief Maximum message length. */
|
||||
static const int WIFIESPNOW_MAXMSGLEN = 250;
|
||||
|
||||
struct WifiEspNowPeerInfo {
|
||||
uint8_t mac[WIFIESPNOW_ALEN];
|
||||
uint8_t channel;
|
||||
};
|
||||
|
||||
/** @brief Result of send operation. */
|
||||
enum class WifiEspNowSendStatus : uint8_t {
|
||||
NONE = 0, ///< result unknown, send in progress
|
||||
OK = 1, ///< unicast message acknowledged by peer; multicast message transmitted
|
||||
FAIL = 2, ///< sending failed
|
||||
};
|
||||
|
||||
class WifiEspNowClass
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Initialize ESP-NOW.
|
||||
* @return whether success.
|
||||
*/
|
||||
bool
|
||||
begin();
|
||||
|
||||
/** @brief Stop ESP-NOW. */
|
||||
void
|
||||
end();
|
||||
|
||||
/**
|
||||
* @brief Set primary key, also known as KOK or PMK.
|
||||
* @param key primary encryption key.
|
||||
* @return whether success.
|
||||
*/
|
||||
bool
|
||||
setPrimaryKey(const uint8_t key[WIFIESPNOW_KEYLEN]);
|
||||
|
||||
/**
|
||||
* @brief List current peers.
|
||||
* @param[out] peers buffer for peer information.
|
||||
* @param maxPeers buffer size.
|
||||
* @return total number of peers, @c std::min(retval,maxPeers) is written to @p peers .
|
||||
*/
|
||||
int
|
||||
listPeers(WifiEspNowPeerInfo* peers, int maxPeers) const;
|
||||
|
||||
/**
|
||||
* @brief Test whether peer exists.
|
||||
* @param mac peer MAC address.
|
||||
* @return whether peer exists.
|
||||
*/
|
||||
bool
|
||||
hasPeer(const uint8_t mac[WIFIESPNOW_ALEN]) const;
|
||||
|
||||
/**
|
||||
* @brief Add a peer or change peer channel.
|
||||
* @param mac peer MAC address.
|
||||
* @param channel peer channel, 0 for current channel.
|
||||
* @param key encryption key, nullptr to disable encryption.
|
||||
* @param netif (ESP32 only) WiFi interface.
|
||||
* @return whether success.
|
||||
*/
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
bool
|
||||
addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel = 0, const uint8_t key[WIFIESPNOW_KEYLEN] = nullptr);
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
bool
|
||||
addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel = 0, const uint8_t key[WIFIESPNOW_KEYLEN] = nullptr, int netif = ESP_IF_WIFI_AP);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Remove a peer.
|
||||
* @param mac peer MAC address.
|
||||
* @return whether success.
|
||||
*/
|
||||
bool
|
||||
removePeer(const uint8_t mac[WIFIESPNOW_ALEN]);
|
||||
|
||||
using RxCallback = void (*)(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count, void* arg);
|
||||
|
||||
/**
|
||||
* @brief Set receive callback.
|
||||
* @param cb the callback.
|
||||
* @param arg an arbitrary argument passed to the callback.
|
||||
* @note Only one callback is allowed; this replaces any previous callback.
|
||||
*/
|
||||
void
|
||||
onReceive(RxCallback cb, void* arg);
|
||||
|
||||
/**
|
||||
* @brief Send a message.
|
||||
* @param mac destination MAC address, nullptr for all peers.
|
||||
* @param buf payload.
|
||||
* @param count payload size, must not exceed @p WIFIESPNOW_MAXMSGLEN .
|
||||
* @return whether success (message queued for transmission).
|
||||
*/
|
||||
bool
|
||||
send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count);
|
||||
|
||||
/** @brief Retrieve status of last sent message. */
|
||||
WifiEspNowSendStatus
|
||||
getSendStatus() const
|
||||
{
|
||||
return m_txRes;
|
||||
}
|
||||
|
||||
private:
|
||||
static void
|
||||
rx(const uint8_t* mac, const uint8_t* data, uint8_t len);
|
||||
|
||||
static void
|
||||
tx(const uint8_t* mac, uint8_t status);
|
||||
|
||||
private:
|
||||
RxCallback m_rxCb = nullptr;
|
||||
void* m_rxArg = nullptr;
|
||||
WifiEspNowSendStatus m_txRes = WifiEspNowSendStatus::NONE;
|
||||
bool m_ready = false;
|
||||
};
|
||||
|
||||
/** @brief ESP-NOW API. */
|
||||
extern WifiEspNowClass WifiEspNow;
|
||||
|
||||
#endif // WIFIESPNOW_H
|
||||
196
arduino-cli/libraries/WifiEspNow/src/WifiEspNowBroadcast.cpp
Normal file
196
arduino-cli/libraries/WifiEspNow/src/WifiEspNowBroadcast.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
#include "WifiEspNowBroadcast.h"
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <user_interface.h>
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#else
|
||||
#error "This library supports ESP8266 and ESP32 only."
|
||||
#endif
|
||||
|
||||
// #define WIFIESPNOW_DEBUG
|
||||
#ifdef WIFIESPNOW_DEBUG
|
||||
#define LOG(...) \
|
||||
do { \
|
||||
Serial.printf("[WifiEspNowBroadcast] " __VA_ARGS__); \
|
||||
Serial.println(); \
|
||||
} while (false)
|
||||
#else
|
||||
#define LOG(...) \
|
||||
do { \
|
||||
} while (false)
|
||||
#endif
|
||||
|
||||
WifiEspNowBroadcastClass WifiEspNowBroadcast;
|
||||
|
||||
bool
|
||||
WifiEspNowBroadcastClass::begin(const char* ssid, int channel, int scanFreq)
|
||||
{
|
||||
m_ssid = ssid;
|
||||
m_nextScan = 0;
|
||||
m_scanFreq = scanFreq;
|
||||
|
||||
// AP mode for announcing our presence, STA mode for scanning
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
// disconnect from any previously saved SSID, so that the specified channel can take effect
|
||||
WiFi.disconnect();
|
||||
// establish AP at the specified channel to announce our presence
|
||||
WiFi.softAP(ssid, nullptr, channel);
|
||||
|
||||
return WifiEspNow.begin();
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowBroadcastClass::end()
|
||||
{
|
||||
WifiEspNow.end();
|
||||
WiFi.softAPdisconnect();
|
||||
m_ssid = "";
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowBroadcastClass::loop()
|
||||
{
|
||||
if (millis() >= m_nextScan && !m_isScanning && WiFi.scanComplete() != WIFI_SCAN_RUNNING) {
|
||||
this->scan();
|
||||
}
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
if (m_isScanning && WiFi.scanComplete() >= 0) {
|
||||
this->processScan();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
WifiEspNowBroadcastClass::setKey(const uint8_t primary[WIFIESPNOW_KEYLEN],
|
||||
const uint8_t peer[WIFIESPNOW_KEYLEN])
|
||||
{
|
||||
if (peer == nullptr) {
|
||||
m_hasPeerKey = false;
|
||||
return true;
|
||||
}
|
||||
m_hasPeerKey = true;
|
||||
std::copy_n(peer, WIFIESPNOW_KEYLEN, m_peerKey);
|
||||
return WifiEspNow.setPrimaryKey(primary);
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowBroadcastClass::scan()
|
||||
{
|
||||
LOG("scan()");
|
||||
m_isScanning = true;
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
scan_config sc{};
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
wifi_scan_config_t sc{};
|
||||
#endif
|
||||
sc.ssid = reinterpret_cast<uint8_t*>(const_cast<char*>(m_ssid.c_str()));
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
wifi_station_scan(&sc, reinterpret_cast<scan_done_cb_t>(WifiEspNowBroadcastClass::processScan));
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
esp_wifi_scan_start(&sc, false);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
void
|
||||
WifiEspNowBroadcastClass::processScan(void* result, int status)
|
||||
{
|
||||
WifiEspNowBroadcast.processScan2(result, status);
|
||||
}
|
||||
|
||||
void
|
||||
WifiEspNowBroadcastClass::processScan2(void* result, int status)
|
||||
|
||||
#define FOREACH_AP(f) \
|
||||
do { \
|
||||
for (bss_info* it = reinterpret_cast<bss_info*>(result); it; it = STAILQ_NEXT(it, next)) { \
|
||||
(f)(it->bssid, it->channel); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#define DELETE_APS \
|
||||
do { \
|
||||
} while (false)
|
||||
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
void
|
||||
WifiEspNowBroadcastClass::processScan()
|
||||
|
||||
// ESP32 WiFiScanClass::_scanDone is always invoked after a scan complete event, so we can use
|
||||
// Arduino's copy of AP records, but we must check SSID, and should not always delete AP records.
|
||||
|
||||
#define FOREACH_AP(f) \
|
||||
do { \
|
||||
int nNetworks = WiFi.scanComplete(); \
|
||||
for (uint8_t i = 0; static_cast<int>(i) < nNetworks; ++i) { \
|
||||
if (WiFi.SSID(i) != m_ssid) { \
|
||||
continue; \
|
||||
} \
|
||||
(f)(WiFi.BSSID(i), static_cast<uint8_t>(WiFi.channel(i))); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#define DELETE_APS \
|
||||
do { \
|
||||
bool hasOtherSsid = false; \
|
||||
int nNetworks = WiFi.scanComplete(); \
|
||||
for (uint8_t i = 0; static_cast<int>(i) < nNetworks; ++i) { \
|
||||
if (WiFi.SSID(i) == m_ssid) { \
|
||||
continue; \
|
||||
} \
|
||||
hasOtherSsid = true; \
|
||||
break; \
|
||||
} \
|
||||
if (!hasOtherSsid) { \
|
||||
WiFi.scanDelete(); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#endif
|
||||
{
|
||||
m_isScanning = false;
|
||||
m_nextScan = millis() + m_scanFreq;
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
LOG("processScan()");
|
||||
|
||||
const int MAX_PEERS = 20;
|
||||
WifiEspNowPeerInfo oldPeers[MAX_PEERS];
|
||||
int nOldPeers = std::min(WifiEspNow.listPeers(oldPeers, MAX_PEERS), MAX_PEERS);
|
||||
const uint8_t PEER_FOUND = 0xFF; // assigned to .channel to indicate peer is matched
|
||||
|
||||
FOREACH_AP([&](const uint8_t* bssid, uint8_t channel) {
|
||||
for (int i = 0; i < nOldPeers; ++i) {
|
||||
WifiEspNowPeerInfo* p = &oldPeers[i];
|
||||
if (std::equal(p->mac, p->mac + WIFIESPNOW_ALEN, bssid)) {
|
||||
p->channel = PEER_FOUND;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < nOldPeers; ++i) {
|
||||
WifiEspNowPeerInfo* p = &oldPeers[i];
|
||||
if (p->channel == PEER_FOUND) {
|
||||
continue;
|
||||
}
|
||||
LOG("processScan removePeer(%02x:%02x:%02x:%02x:%02x:%02x)", p->mac[0], p->mac[1], p->mac[2],
|
||||
p->mac[3], p->mac[4], p->mac[5]);
|
||||
WifiEspNow.removePeer(p->mac);
|
||||
}
|
||||
|
||||
FOREACH_AP([&](const uint8_t* mac, uint8_t channel) {
|
||||
LOG("processScan addPeer(%02x:%02x:%02x:%02x:%02x:%02x)", mac[0], mac[1], mac[2], mac[3],
|
||||
mac[4], mac[5]);
|
||||
WifiEspNow.addPeer(mac, channel, m_hasPeerKey ? m_peerKey : nullptr);
|
||||
});
|
||||
|
||||
DELETE_APS;
|
||||
}
|
||||
102
arduino-cli/libraries/WifiEspNow/src/WifiEspNowBroadcast.h
Normal file
102
arduino-cli/libraries/WifiEspNow/src/WifiEspNowBroadcast.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef WIFIESPNOW_BROADCAST_H
|
||||
#define WIFIESPNOW_BROADCAST_H
|
||||
|
||||
#include "WifiEspNow.h"
|
||||
|
||||
#include <WString.h>
|
||||
|
||||
class WifiEspNowBroadcastClass
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Initialize ESP-NOW with pseudo broadcast.
|
||||
* @param ssid AP SSID to announce and find peers.
|
||||
* @param channel AP channel, used if there is no STA connection.
|
||||
* @param scanFreq how often to scan for peers (milliseconds).
|
||||
* @return whether success.
|
||||
*/
|
||||
bool
|
||||
begin(const char* ssid, int channel = 1, int scanFreq = 15000);
|
||||
|
||||
/** @brief Stop ESP-NOW. */
|
||||
void
|
||||
end();
|
||||
|
||||
/**
|
||||
* @brief Refresh peers if scanning is due.
|
||||
*
|
||||
* This should be invoked in Arduino sketch @c loop() function.
|
||||
*/
|
||||
void
|
||||
loop();
|
||||
|
||||
/**
|
||||
* @brief Set encryption keys.
|
||||
* @param primary primary key, also known as KOK or PMK.
|
||||
* @param peer peer key, also known as LMK; nullptr to disable encryption.
|
||||
* The same peer key is applied to every discovered peer.
|
||||
* @return whether success.
|
||||
*/
|
||||
bool
|
||||
setKey(const uint8_t primary[WIFIESPNOW_KEYLEN], const uint8_t peer[WIFIESPNOW_KEYLEN] = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Set receive callback.
|
||||
* @param cb the callback.
|
||||
* @param arg an arbitrary argument passed to the callback.
|
||||
* @note Only one callback is allowed; this replaces any previous callback.
|
||||
*/
|
||||
void
|
||||
onReceive(WifiEspNowClass::RxCallback cb, void* arg)
|
||||
{
|
||||
WifiEspNow.onReceive(cb, arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Broadcast a message.
|
||||
* @param buf payload.
|
||||
* @param count payload size, must not exceed @c WIFIESPNOW_MAXMSGLEN .
|
||||
* @return whether success (message queued for transmission).
|
||||
*/
|
||||
bool
|
||||
send(const uint8_t* buf, size_t count)
|
||||
{
|
||||
return WifiEspNow.send(nullptr, buf, count);
|
||||
}
|
||||
|
||||
private:
|
||||
void
|
||||
scan();
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
static void
|
||||
processScan(void* result, int status);
|
||||
|
||||
void
|
||||
processScan2(void* result, int status);
|
||||
#elif defined(ARDUINO_ARCH_ESP32)
|
||||
void
|
||||
processScan();
|
||||
#endif
|
||||
|
||||
private:
|
||||
String m_ssid;
|
||||
uint8_t m_peerKey[WIFIESPNOW_KEYLEN];
|
||||
int m_scanFreq = 0;
|
||||
unsigned long m_nextScan = 0;
|
||||
bool m_isScanning = false;
|
||||
bool m_hasPeerKey = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ESP-NOW pseudo broadcast.
|
||||
*
|
||||
* In pseudo broadcast mode, every node announces itself as a group member by advertising a
|
||||
* certain AP SSID. A node periodically scans other BSSIDs announcing the same SSID, and adds
|
||||
* them as ESP-NOW peers. Messages are sent to all known peers.
|
||||
*
|
||||
* Pseudo broadcast does not depend on ESP-NOW API to support broadcast.
|
||||
*/
|
||||
extern WifiEspNowBroadcastClass WifiEspNowBroadcast;
|
||||
|
||||
#endif // WIFIESPNOW_BROADCAST_H
|
||||
Reference in New Issue
Block a user