1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-30 01:58:11 +00:00
trezor-firmware/core/tools/hid-bridge/udp_interface.py
2021-10-13 11:53:17 +02:00

36 lines
1006 B
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(
f"{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(
f"{self.bind_ip}:{self.bind_port} < {address[0]}:{address[1]}",
data.hex(),
)
return data