about summary refs log tree commit diff
path: root/broadlink
diff options
context:
space:
mode:
authorMatthew Garrett <mjg59@coreos.com>2016-11-20T19·16-0800
committerMatthew Garrett <mjg59@coreos.com>2016-11-20T19·18-0800
commitd066513d024101b0ae0a2683ac38dd0663e27e19 (patch)
tree04f3d6585d2174202437cc5be8a87b4d0e9b701b /broadlink
parent7ac243b838376da8735820fa5debe085a8c6f34d (diff)
Add packet retransmission and timeout
UDP doesn't guarantee delivery, so reattempt packet transmission if we don't
get a response and timeout if we still don't have anything after (by
default) 10 seconds.
Diffstat (limited to 'broadlink')
-rwxr-xr-xbroadlink/__init__.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 942b017ffcf6..12cba918a2a7 100755
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -124,9 +124,10 @@ def discover(timeout=None):
       devices.append(dev)
 
 class device:
-  def __init__(self, host, mac):
+  def __init__(self, host, mac, timeout=10):
     self.host = host
     self.mac = mac
+    self.timeout = timeout
     self.count = random.randrange(0xffff)
     self.key = bytearray([0x09, 0x76, 0x28, 0x34, 0x3f, 0xe9, 0x9e, 0x23, 0x76, 0x5c, 0x15, 0x13, 0xac, 0xcf, 0x8b, 0x02])
     self.iv = bytearray([0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58])
@@ -221,8 +222,17 @@ class device:
     packet[0x20] = checksum & 0xff
     packet[0x21] = checksum >> 8
 
-    self.cs.sendto(packet, self.host)
-    response = self.cs.recvfrom(1024)
+    starttime = time.time()
+    while True:
+      try:
+        self.cs.sendto(packet, self.host)
+        self.cs.settimeout(1)
+        response = self.cs.recvfrom(1024)
+        break
+      except socket.timeout:
+        if (time.time() - starttime) < self.timeout:
+          pass
+        raise
     return response[0]