Skip to content

Commit 9cf9820

Browse files
committed
Made advertised configuration index configurable
1 parent 10b5835 commit 9cf9820

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/main/java/com/beowulfe/hap/HomekitRoot.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class HomekitRoot {
3232
private final HomekitRegistry registry;
3333
private final SubscriptionManager subscriptions = new SubscriptionManager();
3434
private boolean started = false;
35+
private int configurationIndex = 1;
3536

3637
HomekitRoot(String label, HomekitWebHandler webHandler, InetAddress localhost,
3738
HomekitAuthInfo authInfo) throws IOException {
@@ -101,7 +102,7 @@ public void start() {
101102
registry, subscriptions, advertiser)).thenAccept(port -> {
102103
try {
103104
refreshAuthInfo();
104-
advertiser.advertise(label, authInfo.getMac(), port);
105+
advertiser.advertise(label, authInfo.getMac(), port, configurationIndex);
105106
} catch (Exception e) {
106107
throw new RuntimeException(e);
107108
}
@@ -120,7 +121,7 @@ public void stop() {
120121
/**
121122
* Refreshes auth info after it has been changed outside this library
122123
*
123-
* @throws IOException if the info cannot be read
124+
* @throws IOException if there is an error in the underlying protocol, such as a TCP error
124125
*/
125126
public void refreshAuthInfo() throws IOException {
126127
advertiser.setDiscoverable(!authInfo.hasUser());
@@ -136,6 +137,27 @@ public void allowUnauthenticatedRequests(boolean allow) {
136137
registry.setAllowUnauthenticatedRequests(allow);
137138
}
138139

140+
141+
/**
142+
* By default, the bridge advertises itself at revision 1. If you make changes to the accessories you're
143+
* including in the bridge after your first call to {@link start()}, you should increment this number. The
144+
* behavior of the client if the configuration index were to decrement is undefined, so this implementation will
145+
* not manage the configuration index by automatically incrementing - preserving this state across invocations should
146+
* be handled externally.
147+
*
148+
* @param revision an integer, greater than or equal to one, indicating the revision of the accessory information
149+
* @throws IOException if there is an error in the underlying protocol, such as a TCP error
150+
*/
151+
public void setConfigurationIndex(int revision) throws IOException {
152+
if (revision < 1) {
153+
throw new IllegalArgumentException("revision must be greater than or equal to 1");
154+
}
155+
this.configurationIndex = revision;
156+
if (this.started) {
157+
advertiser.setConfigurationIndex(revision);
158+
}
159+
}
160+
139161
HomekitRegistry getRegistry() {
140162
return registry;
141163
}

src/main/java/com/beowulfe/hap/impl/jmdns/JmdnsHomekitAdvertiser.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ public class JmdnsHomekitAdvertiser {
2424
private String label;
2525
private String mac;
2626
private int port;
27+
private int configurationIndex;
2728

2829
public JmdnsHomekitAdvertiser(InetAddress localAddress) throws UnknownHostException, IOException {
2930
jmdns = JmDNS.create(localAddress);
3031
}
3132

32-
public synchronized void advertise(String label, String mac, int port) throws Exception {
33+
public synchronized void advertise(String label, String mac, int port, int configurationIndex) throws Exception {
3334
if (isAdvertising) {
3435
throw new IllegalStateException("Homekit advertiser is already running");
3536
}
3637
this.label = label;
3738
this.mac = mac;
3839
this.port = port;
40+
this.configurationIndex = configurationIndex;
3941

4042
logger.info("Advertising accessory "+label);
4143

@@ -48,11 +50,11 @@ public synchronized void advertise(String label, String mac, int port) throws Ex
4850
isAdvertising = true;
4951
}
5052

51-
public void stop() {
53+
public synchronized void stop() {
5254
jmdns.unregisterAllServices();
5355
}
5456

55-
public void setDiscoverable(boolean discoverable) throws IOException {
57+
public synchronized void setDiscoverable(boolean discoverable) throws IOException {
5658
if (this.discoverable != discoverable) {
5759
this.discoverable = discoverable;
5860
if (isAdvertising) {
@@ -63,13 +65,24 @@ public void setDiscoverable(boolean discoverable) throws IOException {
6365
}
6466
}
6567

68+
public synchronized void setConfigurationIndex(int revision) throws IOException {
69+
if (this.configurationIndex != revision) {
70+
this.configurationIndex = revision;
71+
if (isAdvertising) {
72+
logger.info("Re-creating service due to change in configuration index to "+revision);
73+
jmdns.unregisterAllServices();
74+
registerService();
75+
}
76+
}
77+
}
78+
6679
private void registerService() throws IOException {
6780
logger.info("Registering "+SERVICE_TYPE+" on port "+port);
6881
Map<String, String> props = new HashMap<>();
6982
props.put("sf", discoverable ? "1" : "0");
7083
props.put("id", mac);
7184
props.put("md", label);
72-
props.put("c#", "1");
85+
props.put("c#", Integer.toString(configurationIndex));
7386
props.put("s#", "1");
7487
props.put("ff", "0");
7588
props.put("ci", "1");

src/test/java/com/beowulfe/hap/HomekitRootTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testAdvertiserStarts() throws Exception {
6565
String mac = "00:00:00:00:00:00";
6666
when(authInfo.getMac()).thenReturn(mac);
6767
root.start();
68-
verify(advertiser).advertise(eq(LABEL), eq(mac), eq(PORT));
68+
verify(advertiser).advertise(eq(LABEL), eq(mac), eq(PORT), eq(1));
6969
}
7070

7171
@Test

0 commit comments

Comments
 (0)