2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.magentatv.internal.network;
15 import java.io.IOException;
16 import java.net.DatagramPacket;
17 import java.net.DatagramSocket;
18 import java.net.InetAddress;
19 import java.net.NetworkInterface;
20 import java.net.SocketException;
21 import java.net.UnknownHostException;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.magentatv.internal.MagentaTVException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link MagentaTVNetwork} supplies network functions.
32 * @author Markus Michels - Initial contribution
35 public class MagentaTVNetwork {
36 private final Logger logger = LoggerFactory.getLogger(MagentaTVNetwork.class);
38 private String localIP = "";
39 private String localPort = "";
40 private String localMAC = "";
41 private @Nullable NetworkInterface localInterface;
44 * Init local network interface, determine local IP and MAC address
46 * @param networkAddressService
49 public void initLocalNet(String localIP, String localPort) throws MagentaTVException {
51 if (localIP.isEmpty() || localIP.equals("0.0.0.0") || localIP.equals("127.0.0.1")) {
52 throw new MagentaTVException("Unable to detect local IP address!");
54 this.localPort = localPort;
55 this.localIP = localIP;
58 InetAddress ip = InetAddress.getByName(localIP);
59 localInterface = NetworkInterface.getByInetAddress(ip);
60 if (localInterface != null) {
61 byte[] mac = localInterface.getHardwareAddress();
62 StringBuilder sb = new StringBuilder();
63 for (int i = 0; i < mac.length; i++) {
64 sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
66 localMAC = sb.toString().toUpperCase();
67 logger.debug("Local IP address={}, Local MAC address = {}", localIP, localMAC);
70 } catch (UnknownHostException | SocketException e) {
71 throw new MagentaTVException(e);
74 throw new MagentaTVException(
75 "Unable to get local IP / MAC address, check network settings in openHAB system configuration!");
79 public NetworkInterface getLocalInterface() {
80 return localInterface;
83 public String getLocalIP() {
87 public String getLocalPort() {
91 public String getLocalMAC() {
95 public static final int WOL_PORT = 9;
98 * Send a Wake-on-LAN packet
100 * @param ipAddr destination ip
101 * @param macAddress destination MAC address
102 * @throws MagentaTVException
104 public void sendWakeOnLAN(String ipAddr, String macAddress) throws MagentaTVException {
106 byte[] macBytes = getMacBytes(macAddress);
107 byte[] bytes = new byte[6 + 16 * macBytes.length];
108 for (int i = 0; i < 6; i++) {
109 bytes[i] = (byte) 0xff;
111 for (int i = 6; i < bytes.length; i += macBytes.length) {
112 System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
115 InetAddress address = InetAddress.getByName(ipAddr);
116 DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, WOL_PORT);
117 try (DatagramSocket socket = new DatagramSocket()) {
121 logger.debug("Wake-on-LAN packet sent to {} / {}", ipAddr, macAddress);
122 } catch (IOException e) {
123 throw new MagentaTVException(e, "Unable to send Wake-on-LAN packet to {} / {}", ipAddr, macAddress);
128 * Convert MAC address from string to byte array
130 * @param macStr MAC address as string
131 * @return MAC address as byte array
132 * @throws IllegalArgumentException
134 private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
135 byte[] bytes = new byte[6];
136 String[] hex = macStr.split("(\\:|\\-)");
137 if (hex.length != 6) {
138 throw new IllegalArgumentException("Invalid MAC address.");
141 for (int i = 0; i < 6; i++) {
142 bytes[i] = (byte) Integer.parseInt(hex[i], 16);
144 } catch (NumberFormatException e) {
145 throw new IllegalArgumentException("Invalid hex digit in MAC address.", e);