]> git.aero2k.de Git - sirius.git/commitdiff
fixed bug in making the hardware_xor, and made the add_device_encryption_key look...
authorMatt Webb <matt@interconnected.org>
Fri, 26 Sep 2014 18:57:02 +0000 (19:57 +0100)
committerMatt Webb <matt@interconnected.org>
Fri, 26 Sep 2014 18:57:02 +0000 (19:57 +0100)
app/core/claiming.py
app/core/commands.py
app/core/events.py
app/main/__init__.py
app/models/core.py

index dfed20339e12feea29948fd938244288709760de..07f2df23dc24fdfeb5f1726926e6026eb5a33f67 100755 (executable)
@@ -147,20 +147,6 @@ def process_claim_code(claim_code):
     
     return (hardware_xor, link_key_b64)
 
-def make_hardware_xor(device_address):
-    # the hardware_xor from device_address 000d6f000273ce0b
-    # should match the hardware_xor from claim_code
-    # ps2f-gsjg-8wsq-7hc4
-    b = bytearray.fromhex(device_address)
-    
-    claim_address = bytearray(3)
-    claim_address[0] = b[0] ^ b[5]
-    claim_address[1] = b[1] ^ b[3] ^ b[6]
-    claim_address[2] = b[2] ^ b[4] ^ b[7]
-    
-    result = claim_address[2] << 16 | claim_address[1] << 8 | claim_address[0]
-    
-    return result
 
 # ~~~~
 # Test
index 27349f3965af79c90a3fc569cbaf2eb1fc2a1551..71ae7b745dfefaf5b3aa66502a64de0b3fb5063d 100644 (file)
@@ -1,7 +1,6 @@
 from app import db
 
-from ..models.core import BridgeCommand
-from ..models.core import DeviceCommand
+from app.models.core import BridgeCommand, DeviceCommand, Bridge, Device
 from ..image_encoding import rle_image
 
 from .claiming import process_claim_code
@@ -31,32 +30,39 @@ COMMAND_ID = {
     'set_delivery_and_print': 0x0001
 }
 
-def add_device_encryption_key():
-    bridge_address = "000d6f00026c6edd"
-    device_address = "000d6f000273ce0b"
-    #claim_code = "6xwh-441j-8115-zyrh"
-    claim_code = "ps2f-gsjg-8wsq-7hc4"
-    _, encryption_key = process_claim_code(claim_code)
-    
-    payload = {
-        'name': 'add_device_encryption_key',
-        'params': {
-            'device_address': device_address,
-            'encryption_key': encryption_key
-        }
-    }
-
-    command = BridgeCommand(
-        bridge_address='000d6f0001b397c3',
-        json_payload=json.dumps(payload),
-        timestamp=datetime.utcnow(),
-        state='ready'
-    )
-    
-    db.session.add(command)
-    db.session.commit()
-    
-    return command   
+def add_device_encryption_key(bridge, device):
+       # populate pending_claims. @TODO back onto database instead
+       claim_codes = ['6xwh-441j-8115-zyrh', 'ps2f-gsjg-8wsq-7hc4']
+       pending_claims = {} # hardware_xor: encryption_key
+       for c in claim_codes:
+               hw, key = process_claim_code(c)
+               pending_claims[hw] = key
+       
+       if not pending_claims.has_key(device.hardware_xor):
+               print "Device %r %r" % (device.device_address, device.hardware_xor)
+               for hw, key in pending_claims.items():
+                       print "Pending claim %r %r" % (hw, key)
+               raise Exception("No pending claim for device")
+       
+       payload = {
+               'name': 'add_device_encryption_key',
+               'params': {
+                       'device_address': device.device_address,
+                       'encryption_key': pending_claims[device.hardware_xor]
+               }
+       }
+       
+       command = BridgeCommand(
+               bridge_address=bridge.bridge_address,
+               json_payload=json.dumps(payload),
+               timestamp=datetime.utcnow(),
+               state=u'ready'
+       )
+       
+       db.session.add(command)
+       db.session.commit()
+       
+       return command
 
 def set_delivery_and_print_payload(file_id, png_fn):
     # file ID is a 32 bit ID which is returned as a did_print event
index 64fcfea3283eb2d240a7b265866bb38afaffd170..2a1d24902b1c2548a5d483e7d143bbaa8f2d4255 100644 (file)
@@ -76,7 +76,8 @@ def process_event(ws, event, sender):
             sender.register(ws, bridge)
         elif event_name == 'encryption_key_required':
             print "Sending add_device_encryption_key"
-            bc = commands.add_device_encryption_key()
+            device = Device.get_or_create(event['json_payload']['device_address'])
+            bc = commands.add_device_encryption_key(bridge, device)
             sender.for_bridge(bridge).queue_bridge_command(bc)
         else:
             raise EventProcessingException("Unknown BridgeEvent command name: %r" % command_name)
index 5a8cf53ad2bd95de2be3da7c8044c752bd02f213..7a2645c7e76fc001ec13d6f609441a03f24b3565 100644 (file)
@@ -1,7 +1,21 @@
 from flask import Blueprint, render_template
 
+from app.models.core import Device
+
+import pprint
+
 main = Blueprint('main', __name__)
 
 @main.route('/')
 def index():
-    return render_template('index.html')
+       devices = []
+       
+       
+       
+       return render_template('index.html', devices=devices)
+
+@main.route('/device/<device_address>')
+def device(device_address):
+       device = Device.query.get_or_404(device_address)
+
+       return render_template('dump.html', dump=pprint.pformat(device))
index d02dba0c3da96871d59c4764f48e43107fde956a..7d95adce9f0d244998decfe803d6f9f6bf8d599a 100755 (executable)
@@ -1,5 +1,4 @@
 from app import db
-from app.core import claiming
 
 import json
 
@@ -74,18 +73,40 @@ class Bridge(db.Model):
 class Device(db.Model):
     __tablename__ = 'devices'
     device_address = db.Column(db.String, primary_key=True)
-    hardware_xor = db.Column(db.String, nullable=False)
+    hardware_xor = db.Column(db.Integer, nullable=False)
     
     @classmethod
     def get_or_create(cls, device_address):
         d = cls.query.get(device_address)
         if d is None:
             d = cls(device_address=device_address,
-                    hardware_xor=claiming.make_hardware_xor(device_address))
+                    hardware_xor=Device.make_hardware_xor(device_address))
             db.session.add(d)
             db.session.commit()
         return d
     
+    @staticmethod
+    def make_hardware_xor(device_address):
+        # the hardware_xor from device_address 000d6f000273ce0b
+        # should match the hardware_xor from claim_code
+        # ps2f-gsjg-8wsq-7hc4
+        # and (I think) 000d6f00015ff77f should lead to 6290192
+        # and match claim code 6xwh441j8115zyrh
+        # and this 0011223344aabb01 leads to 10087971
+        b = bytearray.fromhex(device_address)
+        
+        # little endian
+        b.reverse()
+    
+        claim_address = bytearray(3)
+        claim_address[0] = b[0] ^ b[5]
+        claim_address[1] = b[1] ^ b[3] ^ b[6]
+        claim_address[2] = b[2] ^ b[4] ^ b[7]
+
+        result = claim_address[2] << 16 | claim_address[1] << 8 | claim_address[0]
+    
+        return result
+
 
 class PendingClaim(db.Model):
     __tablename__ = 'pending_claims'