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.unifiedremote.internal;
15 import static org.openhab.binding.unifiedremote.internal.UnifiedRemoteBindingConstants.*;
17 import java.io.IOException;
18 import java.net.DatagramPacket;
19 import java.net.DatagramSocket;
20 import java.net.InetAddress;
21 import java.net.SocketException;
22 import java.net.SocketTimeoutException;
23 import java.text.ParseException;
24 import java.util.Arrays;
25 import java.util.HashMap;
27 import java.util.concurrent.TimeUnit;
28 import java.util.function.Consumer;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.thing.ThingUID;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link UnifiedRemoteDiscoveryService} discover Unified Remote Server Instances in the network.
42 * @author Miguel Alvarez - Initial contribution
44 @Component(service = DiscoveryService.class, configurationPid = "discovery.unifiedremote")
46 public class UnifiedRemoteDiscoveryService extends AbstractDiscoveryService {
48 private Logger logger = LoggerFactory.getLogger(UnifiedRemoteDiscoveryService.class);
49 static final int TIMEOUT_MS = 20000;
50 private static final long DISCOVERY_RESULT_TTL_SEC = TimeUnit.MINUTES.toSeconds(5);
53 * Port used for broadcast and listening.
55 public static final int DISCOVERY_PORT = 9511;
57 * String the client sends, to disambiguate packets on this port.
59 public static final String DISCOVERY_REQUEST = "6N T|-Ar-A6N T|-Ar-A6N T|-Ar-A";
61 * String the client sends, to disambiguate packets on this port.
63 public static final String DISCOVERY_RESPONSE_PREFIX = ")-b@ h): :)i)-b@ h): :)i)-b@ h): :)";
65 * String used to replace non printable characters on service response
67 public static final String NON_PRINTABLE_CHARTS_REPLACEMENT = ": :";
69 private static final int MAX_PACKET_SIZE = 2048;
71 * maximum time to wait for a reply, in milliseconds.
73 private static final int SOCKET_TIMEOUT_MS = 3000;
75 public UnifiedRemoteDiscoveryService() {
76 super(SUPPORTED_THING_TYPES, TIMEOUT_MS, false);
80 protected void startScan() {
81 sendBroadcast(this::addNewServer);
84 private void addNewServer(ServerInfo serverInfo) {
85 Map<String, Object> properties = new HashMap<>();
86 properties.put(PARAMETER_MAC_ADDRESS, serverInfo.macAddress);
87 properties.put(PARAMETER_HOSTNAME, serverInfo.host);
88 properties.put(PARAMETER_TCP_PORT, serverInfo.tcpPort);
89 properties.put(PARAMETER_UDP_PORT, serverInfo.udpPort);
91 DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_UNIFIED_REMOTE_SERVER, serverInfo.macAddress))
92 .withTTL(DISCOVERY_RESULT_TTL_SEC).withRepresentationProperty(PARAMETER_MAC_ADDRESS)
93 .withProperties(properties).withLabel(serverInfo.name).build());
97 * Create a UDP socket on the service discovery broadcast port.
99 * @return open DatagramSocket if successful
100 * @throws RuntimeException if cannot create the socket
102 public DatagramSocket createSocket() throws SocketException {
103 DatagramSocket socket;
104 socket = new DatagramSocket();
105 socket.setBroadcast(true);
106 socket.setSoTimeout(TIMEOUT_MS);
110 private ServerInfo tryParseServerDiscovery(DatagramPacket receivePacket) throws ParseException {
111 String host = receivePacket.getAddress().getHostAddress();
112 String reply = new String(receivePacket.getData()).replaceAll("[\\p{C}]", NON_PRINTABLE_CHARTS_REPLACEMENT)
113 .replaceAll("[^\\x00-\\x7F]", NON_PRINTABLE_CHARTS_REPLACEMENT);
114 if (!reply.startsWith(DISCOVERY_RESPONSE_PREFIX)) {
115 throw new ParseException("Bad discovery response prefix", 0);
117 String[] parts = Arrays
118 .stream(reply.replace(DISCOVERY_RESPONSE_PREFIX, "").split(NON_PRINTABLE_CHARTS_REPLACEMENT))
119 .filter((String e) -> e.length() != 0).toArray(String[]::new);
120 String name = parts[0];
121 int tcpPort = Integer.parseInt(parts[1]);
122 int udpPort = Integer.parseInt(parts[3]);
123 String macAddress = parts[2];
124 return new ServerInfo(host, tcpPort, udpPort, name, macAddress);
128 * Send broadcast packets with service request string until a response
129 * is received. Return the response as String (even though it should
130 * contain an internet address).
132 * @return String received from server. Should be server IP address.
133 * Returns empty string if failed to get valid reply.
135 public void sendBroadcast(Consumer<ServerInfo> listener) {
136 byte[] receiveBuffer = new byte[MAX_PACKET_SIZE];
137 DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
139 DatagramSocket socket = null;
141 socket = createSocket();
142 } catch (SocketException e) {
143 logger.debug("Error creating discovery socket: {}", e.getMessage());
146 byte[] packetData = DISCOVERY_REQUEST.getBytes();
148 InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
149 int servicePort = DISCOVERY_PORT;
150 DatagramPacket packet = new DatagramPacket(packetData, packetData.length, broadcastAddress, servicePort);
152 logger.debug("Sent packet to {}:{}", broadcastAddress.getHostAddress(), servicePort);
153 for (int i = 0; i < 20; i++) {
154 socket.receive(receivePacket);
155 String host = receivePacket.getAddress().getHostAddress();
156 logger.debug("Received reply from {}", host);
158 ServerInfo serverInfo = tryParseServerDiscovery(receivePacket);
159 listener.accept(serverInfo);
160 } catch (ParseException ex) {
161 logger.debug("Unable to parse server discovery response from {}: {}", host, ex.getMessage());
164 } catch (SocketTimeoutException ste) {
165 logger.debug("SocketTimeoutException during socket operation: {}", ste.getMessage());
166 } catch (IOException ioe) {
167 logger.debug("IOException during socket operation: {}", ioe.getMessage());
173 public class ServerInfo {
180 ServerInfo(String host, int tcpPort, int udpPort, String name, String macAddress) {
182 this.tcpPort = tcpPort;
183 this.udpPort = udpPort;
185 this.macAddress = macAddress;