-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhats.py
More file actions
226 lines (195 loc) · 8.64 KB
/
hats.py
File metadata and controls
226 lines (195 loc) · 8.64 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from bluezero import dbus_tools
from bluezero.central import Central
import threading
import time
from log_manager import LogManager
from uuids import UUID
class HATS():
"""
History And Trace Service
"""
def __init__(self, central:Central):
self.logger = LogManager.get_logger(self.__class__.__name__)
self.central = central
self.hat_slice_record = None
self.hat_rtmcp = None
self.hat_rmcpse = None
self.hat_racp = None
success = self._configure_characteristics()
assert success == True
def unsubscribe(self):
self.hat_slice_record.add_characteristic_cb(None)
self.hat_rtmcp.add_characteristic_cb(None)
self.hat_rmcpse.add_characteristic_cb(None)
self.hat_racp.add_characteristic_cb(None)
return
def send_request(self):
## RTMCP char
# Opcode: 0x02 (Repository Request)
# Request Type: 0x01 (Transactional Request)
# Control Flags: 0x00
# Device Source ID: 0x00000000
# History Type: 0x00 (Repository List)
# Instance ID: 0x00
# Token Type: 0x00 (Absolute Reference)
# Start Index: u32
# End Index: u32
#self.hat_rtmcp.write_value([
# 0x02,
# 0x01,
# 0x00,
# 0x00,0x00,0x00,0x00,
# 0x00,
# 0x00,
# 0x00,
# 0x00,0x00,0x00,0x00,
# 0x10,0x00,0x00,0x00
#])
# -> responds with RTMCP indication:
# 01 02 01
# 01 .. .. Opcode: Response Code
# .. 02 .. Request Opcode: Repository Request
# .. .. 01 Response Code Value: Opcode Not Supported
# Opcode: 0x05 (Get Repository Request Status)
self.hat_rtmcp.write_value([0x05])
# -> responds with RTMCP indication:
# 03 00 03
# 03 .. .. Opcode: Repository Request Status Response
# .. 00 .. Operand: Repository Status Flags
# .. .. 03 Operand: Repository Request Status: Transfer Session Expired
# Opcode: 0x07 (Get Slice Transport Parameters)
#self.hat_rtmcp.write_value([0x07])
# -> responds with RTMCP indication:
# 09 1200
# 09 .... Opcode: Slice Transport Parameters Response
# .. 1200 Operand: Slice Size: 18
# Opcode: 0x0a (Get Transfer Block Parameters)
#self.hat_rtmcp.write_value([0x0a])
# -> responds with RTMCP indication:
# 0c 00 00
# 0c .. .. Opcode: Transfer Block Parameter Response
# .. 00 .. Operand: Compression Setting: None
# .. .. 00 Operand: Encryption Setting: None
# Opcode: 0x0e (Get Session Metrics)
# Session Metric ID: 0x00
#self.hat_rtmcp.write_value([0x0e, 0x00])
# -> responds with RTMCP indication:
# 0f 00 ffffffff ffffffff
# 0f .. ........ ........ Opcode: Session Metrics Response
# .. 00 ........ ........ Operand: Session Metric ID
# .. .. ffffffff ........ Operand: Compression Time: Invalid
# .. .. ........ ffffffff Operand: Encryption Time: Invalid
## RMCP SE char
# Opcode: 0xa2 (Secure Repository Request)
# Request Type: 0x01 (Transactional Request)
# Control Flags: 0x00
# Token Size: 0x00
# Token Bytes: --
#self.hat_rmcpse.write_value([0xa2, 0x01, 0x00, 0x00])
# -> responds with RMCP SE indication:
# 01 a2 02
# 01 .. .. Opcode: Response Code
# .. a2 .. Request Opcode: Secure Repository Requst
# .. .. 02 Response Code Value: Invalid Operand
# Opcode: 0xa2 (Secure Repository Request)
# Request Type: 0x01 (Transactional Request)
# Control Flags: 0x00
# Token Size: 0x01
# Token Bytes: 0x00
#self.hat_rmcpse.write_value([0xa2, 0x01, 0x00, 0x01, 0x00])
# -> responds with *RTMCP* (not RMCP SE!) indication:
# 03 00 07
# 03 .. .. Opcode: Repository Request Status Response
# .. 00 .. Repository Status Flags
# .. .. 07 Repository Request Status: Secure Session Unavailable
## RACP char
# Opcode: 0x01 (Report Stored Records)
# Operator: 0x06 (Last Record)
#self.hat_racp.write_value([0x01, 0x06])
# -> responds with RACP indication:
# 06 00 01 06
# 06 .. .. .. Opcode: SliceRacpOpCode.RESPONSE_CODE
# .. 00 .. .. ignored
# .. .. 01 .. Request Opcode: SliceRacpOpCode.REPORT_STORED_RECORDS
# .. .. .. 06 Operand: SliceRacpResponseCode.NO_RECORDS_FOUND
def _configure_characteristics(self):
try:
# HAT service, Slice Record characteristic
self.logger.info("Adding characteristic Slice Record")
chrc = self.central.add_characteristic(
UUID.HAT_SERVICE, UUID.HAT_SLICE_RECORD_CHAR)
while not chrc.resolve_gatt():
time.sleep(0.2)
assert "notify" in dbus_tools.dbus_to_python(chrc.flags)
chrc.add_characteristic_cb(self._slice_record_cb)
chrc.start_notify()
self.hat_slice_record = chrc
except Exception as e:
self.logger.error("Failed to add characteristic Slice record")
self.logger.error(e)
return False
try:
# HAT service, RTMCP characteristic
self.logger.info("Adding characteristic RTMCP")
chrc = self.central.add_characteristic(
UUID.HAT_SERVICE, UUID.HAT_RTMCP_CHAR)
while not chrc.resolve_gatt():
time.sleep(0.2)
assert "write" in dbus_tools.dbus_to_python(chrc.flags)
assert "indicate" in dbus_tools.dbus_to_python(chrc.flags)
chrc.add_characteristic_cb(self._rtmcp_cb)
chrc.start_notify()
self.hat_rtmcp = chrc
except Exception as e:
self.logger.error("Failed to add characteristic RTMCP")
self.logger.error(e)
return False
try:
# HAT service, RMCPSE characteristic
self.logger.info("Adding characteristic RMCP SE")
chrc = self.central.add_characteristic(
UUID.HAT_SERVICE, UUID.HAT_RMCPSE_CHAR)
while not chrc.resolve_gatt():
time.sleep(0.2)
assert "write" in dbus_tools.dbus_to_python(chrc.flags)
assert "indicate" in dbus_tools.dbus_to_python(chrc.flags)
chrc.add_characteristic_cb(self._rmcpse_cb)
chrc.start_notify()
self.hat_rmcpse = chrc
except Exception as e:
self.logger.error("Failed to add characteristic RMCP SE")
self.logger.error(e)
return False
try:
# HAT service, RACP characteristic
self.logger.info("Adding characteristic RACP")
chrc = self.central.add_characteristic(
UUID.HAT_SERVICE, UUID.HAT_RACP_CHAR)
while not chrc.resolve_gatt():
time.sleep(0.2)
assert "write" in dbus_tools.dbus_to_python(chrc.flags)
assert "indicate" in dbus_tools.dbus_to_python(chrc.flags)
chrc.add_characteristic_cb(self._racp_cb)
chrc.start_notify()
self.hat_racp = chrc
except Exception as e:
self.logger.error("Failed to add characteristic RACP")
self.logger.error(e)
return False
return True
def _slice_record_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
value = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("Slice Record notification: " + value.hex())
def _rtmcp_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
value = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("RTMCP indication: " + value.hex())
def _rmcpse_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
value = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("RMCP SE indication: " + value.hex())
def _racp_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
value = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("RACP indication: " + value.hex())