Add option for AP discovery type (#351)

This commit is contained in:
Jonny Bergdahl
2024-11-02 23:10:00 +01:00
committed by GitHub
parent a57c797542
commit d4ccaf6027
8 changed files with 75 additions and 36 deletions

3
.gitignore vendored
View File

@@ -31,3 +31,6 @@ $PROJECT_DIR/
# OS generated files
.DS_Store
# Jetbrains IDE
.idea/

View File

@@ -73,6 +73,7 @@ struct Config {
uint8_t sleepTime1;
uint8_t sleepTime2;
uint8_t ble;
uint8_t discovery;
String repo;
String env;
};

View File

@@ -1,10 +1,12 @@
#include <Arduino.h>
#include "AsyncUDP.h"
#include "tag_db.h"
#ifndef defudpcomm
#define defudpcomm
extern Config config;
class UDPcomm {
public:
UDPcomm();
@@ -16,9 +18,11 @@ class UDPcomm {
void netProcessXferTimeout(struct espXferComplete* xfc);
void netSendDataAvail(struct pendingData* pending);
void netTaginfo(struct TagInfo* taginfoitem);
private:
AsyncUDP udp;
void processPacket(AsyncUDPPacket packet);
void writeUdpPacket(uint8_t* buffer, uint16_t len, IPAddress senderIP);
};
#endif

View File

@@ -308,7 +308,7 @@ void clearPending(tagRecord* taginfo) {
}
void initAPconfig() {
DynamicJsonDocument APconfig(500);
DynamicJsonDocument APconfig(768);
File configFile = contentFS->open("/current/apconfig.json", "r");
if (configFile) {
DeserializationError error = deserializeJson(APconfig, configFile);
@@ -333,6 +333,7 @@ void initAPconfig() {
config.sleepTime1 = APconfig.containsKey("sleeptime1") ? APconfig["sleeptime1"] : 0;
config.sleepTime2 = APconfig.containsKey("sleeptime2") ? APconfig["sleeptime2"] : 0;
config.ble = APconfig.containsKey("ble") ? APconfig["ble"] : 0;
config.discovery = APconfig.containsKey("discovery") ? APconfig["discovery"] : 0;
#ifdef BLE_ONLY
config.ble = true;
#endif
@@ -370,6 +371,7 @@ void saveAPconfig() {
APconfig["ble"] = config.ble;
APconfig["repo"] = config.repo;
APconfig["env"] = config.env;
APconfig["discovery"] = config.discovery;
serializeJsonPretty(APconfig, configFile);
configFile.close();
xSemaphoreGive(fsMutex);

View File

@@ -31,6 +31,7 @@ UDPcomm::~UDPcomm() {
}
void UDPcomm::init() {
if (config.discovery == 0) {
if (udp.listenMulticast(UDPIP, UDPPORT)) {
udp.onPacket([this](AsyncUDPPacket packet) {
if (packet.remoteIP() != WiFi.localIP()) {
@@ -38,6 +39,15 @@ void UDPcomm::init() {
}
});
}
} else {
if (udp.listen(UDPPORT)) {
udp.onPacket([this](AsyncUDPPacket packet) {
if (packet.isBroadcast() && packet.remoteIP() != WiFi.localIP()) {
this->processPacket(packet);
}
});
}
}
setAPchannel();
}
@@ -87,7 +97,7 @@ void UDPcomm::processPacket(AsyncUDPPacket packet) {
uint8_t buffer[sizeof(struct APlist) + 1];
buffer[0] = PKT_APLIST_REPLY;
memcpy(buffer + 1, &APitem, sizeof(struct APlist));
udp.writeTo(buffer, sizeof(buffer), senderIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), senderIP);
break;
}
case PKT_APLIST_REPLY: {
@@ -158,40 +168,48 @@ void UDPcomm::getAPList() {
uint8_t buffer[sizeof(struct APlist) + 1];
buffer[0] = PKT_APLIST_REQ;
memcpy(buffer + 1, &APitem, sizeof(struct APlist));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::netProcessDataReq(struct espAvailDataReq* eadr) {
uint8_t buffer[sizeof(struct espAvailDataReq) + 1];
buffer[0] = PKT_AVAIL_DATA_INFO;
memcpy(buffer + 1, eadr, sizeof(struct espAvailDataReq));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::netProcessXferComplete(struct espXferComplete* xfc) {
uint8_t buffer[sizeof(struct espXferComplete) + 1];
buffer[0] = PKT_XFER_COMPLETE;
memcpy(buffer + 1, xfc, sizeof(struct espXferComplete));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::netProcessXferTimeout(struct espXferComplete* xfc) {
uint8_t buffer[sizeof(struct espXferComplete) + 1];
buffer[0] = PKT_XFER_TIMEOUT;
memcpy(buffer + 1, xfc, sizeof(struct espXferComplete));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::netSendDataAvail(struct pendingData* pending) {
uint8_t buffer[sizeof(struct pendingData) + 1];
buffer[0] = PKT_AVAIL_DATA_REQ;
memcpy(buffer + 1, pending, sizeof(struct pendingData));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::netTaginfo(struct TagInfo* taginfoitem) {
uint8_t buffer[sizeof(struct TagInfo) + 1];
buffer[0] = PKT_TAGINFO;
memcpy(buffer + 1, taginfoitem, sizeof(struct TagInfo));
udp.writeTo(buffer, sizeof(buffer), UDPIP, UDPPORT);
writeUdpPacket(buffer, sizeof(buffer), UDPIP);
}
void UDPcomm::writeUdpPacket(uint8_t *buffer, uint16_t len, IPAddress senderIP) {
if (config.discovery == 0) {
udp.writeTo(buffer, len, senderIP, UDPPORT);
} else {
udp.broadcastTo(buffer, len, UDPPORT);
}
}

View File

@@ -625,6 +625,9 @@ void init_web() {
setenv("TZ", config.timeZone, 1);
tzset();
}
if (request->hasParam("discovery", true)) {
config.discovery = static_cast<uint8_t>(request->getParam("discovery", true)->value().toInt());
}
if (request->hasParam("repo", true)) {
config.repo = request->getParam("repo", true)->value();
}

View File

@@ -471,6 +471,13 @@ options:
</optgroup>
</select>
</p>
<p title="Set to Broadcast if using a multi WiFi AP setup that does not support IGMP sniffing">
<label for="apcdiscovery">AP discovery method</label>
<select id="apcdiscovery">
<option value="0" selected>Multicast</option>
<option value="1">Broadcast</option>
</select>
</p>
<p>
<input type="button" value="Save" id="apcfgsave"><span id="apcfgmsg"></span>
</p>

View File

@@ -784,6 +784,7 @@ document.addEventListener("loadTab", function (event) {
$("#apctimezone").value = data.timezone;
$("#apcnight1").value = data.sleeptime1;
$("#apcnight2").value = data.sleeptime2;
$("#apcdiscovery").value = data.discovery;
}
})
$('#apcfgmsg').innerHTML = '';
@@ -821,7 +822,7 @@ $('#apcfgsave').onclick = function () {
formData.append('timezone', $('#apctimezone').value);
formData.append('sleeptime1', $('#apcnight1').value);
formData.append('sleeptime2', $('#apcnight2').value);
formData.append('discovery', $('#apcdiscovery').value)
fetch("save_apcfg", {
method: "POST",
body: formData