aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/sonix/sonix-beacon.py
blob: eb9bf42a1c22745810127af74159e04376a82a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python3
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Relays the Sonix player's discovery beacon to stdout, one line per
# datagram: QML has no UDP socket. SonixLink broadcasts
# "SONIXLINK1 <ip> <port> <name>" to UDP 7801 every 2s; anything else on
# the port is dropped. The port is an argument only for the test.
import socket
import sys

port = int(sys.argv[1]) if len(sys.argv) > 1 else 7801
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Several listeners may share the port; each still gets the broadcast.
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", port))
while True:
    data, _ = s.recvfrom(512)
    line = data.decode("utf-8", "replace").strip()
    if line.startswith("SONIXLINK1 "):
        print(line, flush=True)