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
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
'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
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)
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))
from app import db
-from app.core import claiming
import json
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'