Skip to content

Commit d4412b2

Browse files
committed
Implement unpairing and connection cleanup
1 parent 17dcdaa commit d4412b2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

HomeKitDotNet/Connection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace HomeKitDotNet
2121
{
22-
public class Connection
22+
public class Connection : IDisposable
2323
{
2424
TcpClient client;
2525
EncryptedStreamWriter writer;
@@ -37,6 +37,11 @@ public Connection(IPEndPoint ep)
3737
reader = new EncryptedStreamReader(client.GetStream());
3838
}
3939

40+
public void Dispose()
41+
{
42+
client.Dispose();
43+
}
44+
4045
public void EnableEncryption(byte[] writeKey, byte[] readKey)
4146
{
4247

HomeKitDotNet/Controller.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace HomeKitDotNet
2525
{
26-
public class Controller
26+
public class Controller : IDisposable
2727
{
2828
private byte[] LTPK;
2929
private byte[] LTSK;
@@ -120,6 +120,19 @@ public async Task<HomeKitEndPoint> Connect(IPEndPoint destination, byte[] access
120120
throw new Exception("Failed to Connect - Cause Unknown");
121121
}
122122

123+
public async Task<bool> UnPair(HomeKitEndPoint endpoint)
124+
{
125+
HttpResponseMessage msg = await endpoint.Connection.Post("/pair-setup",
126+
new TLVInt(TLVType.kTLVType_State, (byte)PairingState.M1),
127+
new TLVInt(TLVType.kTLVType_Method, (byte)Method.RemovePairing),
128+
new TLVBytes(TLVType.kTLVType_Identifier, DeviceID));
129+
if (!msg.IsSuccessStatusCode)
130+
return false;
131+
endpoints.Remove(endpoint);
132+
endpoint.Connection.Dispose();
133+
return true;
134+
}
135+
123136
public async Task<AccessoryInfo> Pair(long setupPin, IPEndPoint destination)
124137
{
125138
string I = "Pair-Setup";
@@ -211,6 +224,15 @@ public async Task<AccessoryInfo> Pair(long setupPin, IPEndPoint destination)
211224
return new AccessoryInfo(accessoryPairingID, AccessoryLTPK);
212225
}
213226

227+
public void Dispose()
228+
{
229+
foreach (HomeKitEndPoint endPoint in EndPoints)
230+
endPoint.Connection.Dispose();
231+
endpoints.Clear();
232+
LTPK = new byte[0];
233+
LTSK = new byte[0];
234+
}
235+
214236
public HomeKitEndPoint[] EndPoints {get { return endpoints.ToArray(); } }
215237
}
216238
}

0 commit comments

Comments
 (0)