1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-16 04:29:08 +00:00
trezor-firmware/core/tools/hid-bridge/udp_interface.py
2019-04-15 19:14:40 +02:00

40 lines
1.1 KiB
Python

import socket
import logger
class UDPInterface:
def __init__(self, destination_port):
self.bind_ip = "127.0.0.1"
self.bind_port = 21423
self.destination_ip = "127.0.0.1"
self.destination_port = destination_port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind((self.bind_ip, self.bind_port))
self.file_descriptor = self.socket.fileno()
def write(self, data):
bytes_sent = self.socket.sendto(
data, ((self.destination_ip, self.destination_port))
)
assert bytes_sent == len(data)
logger.log_raw(
"{}:{} < {}:{}".format(
self.destination_ip, self.destination_port, self.bind_ip, self.bind_port
),
data.hex(),
)
def read(self, length):
data, address = self.socket.recvfrom(length)
logger.log_raw(
"{}:{} < {}:{}".format(
self.bind_ip, self.bind_port, address[0], address[1]
),
data.hex(),
)
return data