-
Notifications
You must be signed in to change notification settings - Fork 0
/
agwpe_client.py
104 lines (84 loc) · 3.19 KB
/
agwpe_client.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import threading
import struct
from agwpe_tcp_client import AgwpeTcpClient
from agwpe_packet import AgwpePacket
# Just to make sure all AgwpeClients call the events one-by-one
AgwpeClientMutex = threading.Lock()
class AgwpeClient:
def __init__(self):
self.tcp_client = AgwpeTcpClient()
self.on_version_info = []
self.on_unknown_packet = []
self.on_raw_packet = []
self.on_outstanding_frames = []
def connect(self, host: str, port: int):
self.tcp_client.on_data.append(self._handle_data)
self.tcp_client.connect(host, port)
self.tcp_client.start_read_loop()
def disconnect(self):
self.tcp_client.stop()
@staticmethod
def _trigger_event(what, *args):
for f in what:
f(*args)
def _handle_data(self, data: bytes):
try:
AgwpeClientMutex.acquire()
packet = AgwpePacket.decode(data)
if packet.kind == b'R': # version response
(major, minor) = struct.unpack('HxxHxx', packet.data) # this is wrong, or at least weird? 2005 127 ?
self._trigger_event(self.on_version_info, major, minor)
elif packet.kind == b'K':
self._trigger_event(self.on_raw_packet, packet.data[1:]) # first byte is port number (so ignore)
elif packet.kind == b'y':
self._trigger_event(self.on_outstanding_frames, struct.unpack('I', packet.data)[0])
else:
print(packet)
self._trigger_event(self.on_unknown_packet, packet)
except Exception as e:
print("Unable to decode packet")
print(e)
finally:
AgwpeClientMutex.release()
pass
def request_version_info(self):
packet = AgwpePacket()
packet.set_kind('R')
self.tcp_client.send(packet.encode())
def request_port_info(self):
packet = AgwpePacket()
packet.set_kind('G')
self.tcp_client.send(packet.encode())
def request_port_capabilities(self):
packet = AgwpePacket()
packet.set_kind('g')
self.tcp_client.send(packet.encode())
def request_outstanding_frames(self):
packet = AgwpePacket()
packet.set_kind('y')
self.tcp_client.send(packet.encode())
def register_callsign(self, callsign):
packet = AgwpePacket()
packet.set_kind('X')
packet.set_call_from(callsign)
self.tcp_client.send(packet.encode())
def enable_monitoring(self):
packet = AgwpePacket()
packet.set_kind('m')
self.tcp_client.send(packet.encode())
def enable_raw_monitoring(self):
packet = AgwpePacket()
packet.set_kind('k')
self.tcp_client.send(packet.encode())
def send_unproto(self, call_from, call_to, data: bytes):
packet = AgwpePacket()
packet.set_kind('M')
packet.set_call_from(call_from)
packet.set_call_to(call_to)
packet.set_data(data)
self.tcp_client.send(packet.encode())
def send_raw_packet(self, data: bytes):
packet = AgwpePacket()
packet.set_kind('K')
packet.set_data(data)
self.tcp_client.send(packet.encode())