8 namespace wake_on_lan {
10 static const char *
const TAG =
"wake_on_lan.button";
11 static const uint8_t PREFIX[6] = {255, 255, 255, 255, 255, 255};
23 LOG_BUTTON(
"",
"Wake-on-LAN Button",
this);
24 ESP_LOGCONFIG(TAG,
" Target MAC address: %02X:%02X:%02X:%02X:%02X:%02X", this->
macaddr_[0], this->
macaddr_[1],
30 ESP_LOGW(TAG,
"Network not connected");
33 ESP_LOGI(TAG,
"Sending Wake-on-LAN Packet...");
34 #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) 38 uint8_t buffer[6 +
sizeof this->
macaddr_ * 16];
39 memcpy(buffer, PREFIX,
sizeof(PREFIX));
40 for (
size_t i = 0; i != 16; i++) {
43 if (this->
broadcast_socket_->sendto(buffer,
sizeof(buffer), 0, reinterpret_cast<const sockaddr *>(&saddr),
45 ESP_LOGW(TAG,
"sendto() error %d", errno);
47 IPAddress broadcast = IPAddress(255, 255, 255, 255);
50 if (this->
udp_client_.beginPacketMulticast(broadcast, 9, ip, 128) != 0) {
52 for (
size_t i = 0; i < 16; i++) {
57 ESP_LOGW(TAG,
"WOL broadcast failed");
62 ESP_LOGW(TAG,
"No ip4 addresses to broadcast to");
67 #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) 75 auto err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
80 err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_BROADCAST, &enable,
sizeof(
int));
void status_set_warning(const char *message="unspecified")
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
network::IPAddresses get_ip_addresses()
void status_set_error(const char *message="unspecified")
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.