Repository navigation
Expand file tree
/
Copy pathJmdnsHomekitAdvertiser.java
More file actions
158 lines (137 loc) · 4.57 KB
/
Copy pathJmdnsHomekitAdvertiser.java
File metadata and controls
158 lines (137 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package io.github.hapjava.server.impl.jmdns;
import static io.github.hapjava.server.impl.crypto.HAPSetupCodeUtils.generateSHA512Hash;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JmdnsHomekitAdvertiser {
private static final String SERVICE_TYPE = "_hap._tcp.local.";
private final JmDNS jmdns;
private final boolean ownsJmdns;
private Thread shutdownHook;
private boolean discoverable = true;
private static final Logger logger = LoggerFactory.getLogger(JmdnsHomekitAdvertiser.class);
private boolean isAdvertising = false;
private String label;
private String mac;
private String setupId;
private int port;
private int configurationIndex;
private ServiceInfo serviceInfo;
private int category;
public JmdnsHomekitAdvertiser(JmDNS jmdns) {
this(jmdns, false);
}
public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostException, IOException {
this(JmDNS.create(localAddress), true);
}
JmdnsHomekitAdvertiser(JmDNS jmdns, boolean ownsJmdns) {
this.jmdns = jmdns;
this.ownsJmdns = ownsJmdns;
}
public synchronized void advertise(
String label, int category, String mac, int port, int configurationIndex, String setupId)
throws Exception {
if (isAdvertising) {
throw new IllegalStateException("HomeKit advertiser is already running");
}
this.label = label;
this.mac = mac;
this.port = port;
this.setupId = setupId;
this.category = category;
this.configurationIndex = configurationIndex;
logger.trace("Advertising accessory " + label);
registerService();
shutdownHook =
new Thread(
() -> {
logger.trace("Stopping advertising in response to shutdown.");
jmdns.unregisterAllServices();
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
isAdvertising = true;
}
public synchronized void stop() {
unregisterService();
isAdvertising = false;
if (shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
} catch (IllegalStateException e) {
// already shutting down
}
}
if (ownsJmdns) {
try {
jmdns.close();
} catch (IOException e) {
logger.warn("Failed to close mDNS", e);
}
}
}
public synchronized void setDiscoverable(boolean discoverable) throws IOException {
if (this.discoverable != discoverable) {
this.discoverable = discoverable;
if (isAdvertising) {
logger.trace("Re-creating service due to change in discoverability to " + discoverable);
unregisterService();
registerService();
}
}
}
public synchronized void setMac(String mac) throws IOException {
if (this.mac != mac) {
this.mac = mac;
if (isAdvertising) {
logger.trace("Re-creating service due to change in mac to " + mac);
unregisterService();
registerService();
}
}
}
public synchronized void setConfigurationIndex(int revision) throws IOException {
if (this.configurationIndex != revision) {
this.configurationIndex = revision;
if (isAdvertising) {
logger.trace("Re-creating service due to change in configuration index to " + revision);
unregisterService();
registerService();
}
}
}
private void unregisterService() {
if (serviceInfo != null) {
jmdns.unregisterService(serviceInfo);
serviceInfo = null;
}
}
private void registerService() throws IOException {
logger.info("Registering " + SERVICE_TYPE + " on port " + port);
if (this.serviceInfo != null) {
throw new AssertionError(
"Registering an already registered service without unregistering first is not allowed");
}
serviceInfo = buildServiceInfo();
jmdns.registerService(serviceInfo);
}
private ServiceInfo buildServiceInfo() {
logger.trace("MAC:" + mac + " Setup Id:" + setupId);
Map<String, String> props = new HashMap<>();
props.put("sf", discoverable ? "1" : "0");
props.put("id", mac);
props.put("md", label);
props.put("sh", generateSHA512Hash(setupId + mac));
props.put("c#", Integer.toString(configurationIndex));
props.put("s#", "1");
props.put("ff", "0");
props.put("ci", Integer.toString(category));
props.put("pv", "1.1");
return ServiceInfo.create(SERVICE_TYPE, label, port, 1, 1, props);
}
}