2019-08-26 13:45:14 +02:00
|
|
|
#include "esp32_platform.h"
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
|
|
|
#include "knx/bits.h"
|
|
|
|
|
2021-04-01 09:34:26 +02:00
|
|
|
Esp32Platform::Esp32Platform()
|
|
|
|
#ifndef KNX_NO_DEFAULT_UART
|
|
|
|
: ArduinoPlatform(&Serial1)
|
|
|
|
#endif
|
2019-09-01 20:49:28 +02:00
|
|
|
{
|
|
|
|
}
|
2019-08-26 13:45:14 +02:00
|
|
|
|
2021-02-05 15:57:45 +01:00
|
|
|
Esp32Platform::Esp32Platform(HardwareSerial* s) : ArduinoPlatform(s)
|
2019-08-26 13:45:14 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Esp32Platform::currentIpAddress()
|
|
|
|
{
|
|
|
|
return WiFi.localIP();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Esp32Platform::currentSubnetMask()
|
|
|
|
{
|
|
|
|
return WiFi.subnetMask();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Esp32Platform::currentDefaultGateway()
|
|
|
|
{
|
|
|
|
return WiFi.gatewayIP();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Esp32Platform::macAddress(uint8_t * addr)
|
|
|
|
{
|
|
|
|
esp_wifi_get_mac(WIFI_IF_STA, addr);
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:40:56 +02:00
|
|
|
uint32_t Esp32Platform::uniqueSerialNumber()
|
|
|
|
{
|
|
|
|
uint64_t chipid = ESP.getEfuseMac();
|
|
|
|
uint32_t upperId = (chipid >> 32) & 0xFFFFFFFF;
|
|
|
|
uint32_t lowerId = (chipid & 0xFFFFFFFF);
|
2021-04-17 11:12:19 +02:00
|
|
|
return (upperId ^ lowerId);
|
2021-04-12 11:40:56 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 13:45:14 +02:00
|
|
|
void Esp32Platform::restart()
|
|
|
|
{
|
2019-09-09 20:10:56 +02:00
|
|
|
println("restart");
|
2019-08-26 13:45:14 +02:00
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Esp32Platform::setupMultiCast(uint32_t addr, uint16_t port)
|
|
|
|
{
|
2019-12-10 22:25:33 +01:00
|
|
|
IPAddress mcastaddr(htonl(addr));
|
2019-08-26 13:45:14 +02:00
|
|
|
|
|
|
|
Serial.printf("setup multicast addr: %s port: %d ip: %s\n", mcastaddr.toString().c_str(), port,
|
|
|
|
WiFi.localIP().toString().c_str());
|
|
|
|
uint8_t result = _udp.beginMulticast(mcastaddr, port);
|
|
|
|
Serial.printf("result %d\n", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Esp32Platform::closeMultiCast()
|
|
|
|
{
|
|
|
|
_udp.stop();
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:29:14 +01:00
|
|
|
bool Esp32Platform::sendBytesMultiCast(uint8_t * buffer, uint16_t len)
|
2019-08-26 13:45:14 +02:00
|
|
|
{
|
|
|
|
//printHex("<- ",buffer, len);
|
2021-02-05 15:57:45 +01:00
|
|
|
_udp.beginMulticastPacket();
|
|
|
|
_udp.write(buffer, len);
|
|
|
|
_udp.endPacket();
|
2019-08-26 13:45:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:29:14 +01:00
|
|
|
int Esp32Platform::readBytesMultiCast(uint8_t * buffer, uint16_t maxLen)
|
2019-08-26 13:45:14 +02:00
|
|
|
{
|
|
|
|
int len = _udp.parsePacket();
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (len > maxLen)
|
|
|
|
{
|
|
|
|
Serial.printf("udp buffer to small. was %d, needed %d\n", maxLen, len);
|
|
|
|
fatalError();
|
|
|
|
}
|
|
|
|
|
|
|
|
_udp.read(buffer, len);
|
|
|
|
//printHex("-> ", buffer, len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2021-06-04 17:23:23 +02:00
|
|
|
bool Esp32Platform::sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len)
|
|
|
|
{
|
|
|
|
IPAddress ucastaddr(htonl(addr));
|
|
|
|
println("sendBytesUniCast endPacket fail");
|
|
|
|
if(_udp.beginPacket(ucastaddr, port) == 1) {
|
|
|
|
_udp.write(buffer, len);
|
|
|
|
if(_udp.endPacket() == 0) println("sendBytesUniCast endPacket fail");
|
|
|
|
}
|
|
|
|
else println("sendBytesUniCast beginPacket fail");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:45:14 +02:00
|
|
|
uint8_t * Esp32Platform::getEepromBuffer(uint16_t size)
|
|
|
|
{
|
|
|
|
EEPROM.begin(size);
|
|
|
|
return EEPROM.getDataPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Esp32Platform::commitToEeprom()
|
|
|
|
{
|
2021-04-14 23:46:03 +02:00
|
|
|
EEPROM.getDataPtr(); // trigger dirty flag in EEPROM lib to make sure data will be written to flash
|
2019-08-26 13:45:14 +02:00
|
|
|
EEPROM.commit();
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:19:09 +02:00
|
|
|
#endif
|