From: Matt Webb Date: Fri, 26 Sep 2014 18:57:02 +0000 (+0100) Subject: fixed bug in making the hardware_xor, and made the add_device_encryption_key look... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=0c7db7fb9ff920eec061f02cbfb1db13caceb916;p=sirius.git fixed bug in making the hardware_xor, and made the add_device_encryption_key look up by device_address --- diff --git a/app/core/claiming.py b/app/core/claiming.py index dfed203..07f2df2 100755 --- a/app/core/claiming.py +++ b/app/core/claiming.py @@ -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 diff --git a/app/core/commands.py b/app/core/commands.py index 27349f3..71ae7b7 100644 --- a/app/core/commands.py +++ b/app/core/commands.py @@ -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 diff --git a/app/core/events.py b/app/core/events.py index 64fcfea..2a1d249 100644 --- a/app/core/events.py +++ b/app/core/events.py @@ -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) diff --git a/app/main/__init__.py b/app/main/__init__.py index 5a8cf53..7a2645c 100644 --- a/app/main/__init__.py +++ b/app/main/__init__.py @@ -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/') +def device(device_address): + device = Device.query.get_or_404(device_address) + + return render_template('dump.html', dump=pprint.pformat(device)) diff --git a/app/models/core.py b/app/models/core.py index d02dba0..7d95adc 100755 --- a/app/models/core.py +++ b/app/models/core.py @@ -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'