Skip to content

Commit

Permalink
WifiEspNowBroadcast.setKey
Browse files Browse the repository at this point in the history
refs #5
  • Loading branch information
yoursunny committed Aug 21, 2021
1 parent ea74e12 commit b57561b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=WifiEspNow
version=0.0.20201226
version=0.0.20210821
author=Junxiao Shi
maintainer=Junxiao Shi
sentence=ESP-NOW for ESP8266 and ESP32.
paragraph=This library supports ESP-NOW, a connectionless WiFi communication protocol defined by Espressif. It contains a simple wrapper of ESP-NOW functions, as well as a custom pseudo broadcast protocol.
category=Communication
url=https://yoursunny.com
url=https://wifiespnow.yoursunny.dev/
architectures=esp8266,esp32
2 changes: 0 additions & 2 deletions src/WifiEspNow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

WifiEspNowClass WifiEspNow;

WifiEspNowClass::WifiEspNowClass() = default;

bool
WifiEspNowClass::begin()
{
Expand Down
2 changes: 0 additions & 2 deletions src/WifiEspNow.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ enum class WifiEspNowSendStatus : uint8_t {
class WifiEspNowClass
{
public:
WifiEspNowClass();

/**
* @brief Initialize ESP-NOW.
* @return whether success.
Expand Down
21 changes: 15 additions & 6 deletions src/WifiEspNowBroadcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#error "This library supports ESP8266 and ESP32 only."
#endif

#define WIFIESPNOW_DEBUG
// #define WIFIESPNOW_DEBUG
#ifdef WIFIESPNOW_DEBUG
#define LOG(...) \
do { \
Expand All @@ -25,10 +25,6 @@

WifiEspNowBroadcastClass WifiEspNowBroadcast;

WifiEspNowBroadcastClass::WifiEspNowBroadcastClass()
: m_isScanning(false)
{}

bool
WifiEspNowBroadcastClass::begin(const char* ssid, int channel, int scanFreq)
{
Expand Down Expand Up @@ -67,6 +63,19 @@ WifiEspNowBroadcastClass::loop()
#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()
{
Expand Down Expand Up @@ -180,7 +189,7 @@ WifiEspNowBroadcastClass::processScan()
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);
WifiEspNow.addPeer(mac, channel, m_hasPeerKey ? m_peerKey : nullptr);
});

DELETE_APS;
Expand Down
20 changes: 15 additions & 5 deletions src/WifiEspNowBroadcast.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
class WifiEspNowBroadcastClass
{
public:
WifiEspNowBroadcastClass();

/**
* @brief Initialize ESP-NOW with pseudo broadcast.
* @param ssid AP SSID to announce and find peers.
Expand All @@ -32,6 +30,16 @@ class WifiEspNowBroadcastClass
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.
Expand Down Expand Up @@ -73,9 +81,11 @@ class WifiEspNowBroadcastClass

private:
String m_ssid;
int m_scanFreq;
unsigned long m_nextScan;
bool m_isScanning;
uint8_t m_peerKey[WIFIESPNOW_KEYLEN];
int m_scanFreq = 0;
unsigned long m_nextScan = 0;
bool m_isScanning = false;
bool m_hasPeerKey = false;
};

/**
Expand Down

0 comments on commit b57561b

Please sign in to comment.