-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsg_reader.py
More file actions
150 lines (117 loc) · 5.56 KB
/
sg_reader.py
File metadata and controls
150 lines (117 loc) · 5.56 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
from bluezero import dbus_tools
from bluezero.central import Central
import threading
import time
from cgm_measurement import CGMMeasurement
from log_manager import LogManager
from sake_handler import SakeHandler
from uuids import UUID
class SGReader:
"""
Test for reading an SG value through the pump's CGM service
The latest record is requested on the Record Access Control Point.
We then expect the pump to answer with a CGM Measurement and to send
a final response on the Record Access Control Point which indicates
whether the operation succeeded or not.
The pump SAKE-encrypts the CGM Measurement data. The Record Access
Control Point does not use any encryption though.
Note that is very hackish and is intended to do one very specific
thing only. We may very much want to throw this away and completely
rewrite the approach for use in some actual production code.
"""
def __init__(self, central:Central):
self.logger = LogManager.get_logger(self.__class__.__name__)
self.central = central
self.cgm_measurement = None
self.cgm_racp = None
self.measurement_received = threading.Event()
self.operation_finished = threading.Event()
self.record:bytearray = None
self.response = None
self.sh = SakeHandler()
self._configure_characteristics()
return
def unsubscribe(self):
self.cgm_measurement.add_characteristic_cb(None)
self.cgm_racp.add_characteristic_cb(None)
return
def get_value(self, timeout:int=3) -> float | None:
self.measurement_received = threading.Event()
self.logger.info("Requesting last stored record")
# Op Code: 0x01 (Report Stored Records)
# Operator: 0x06 (Last Record)
self.cgm_racp.write_value([0x01, 0x06])
# wait for a response
if self.measurement_received.wait(timeout=timeout):
self.logger.debug("Measurement received")
if self.operation_finished.wait(timeout=timeout):
self.logger.debug("Operation finished")
else:
self.logger.error("Timeout while waiting for operation to finish")
return None
else:
self.logger.error("Timeout while waiting for measurement")
return None
# decrypt the record
#self.logger.debug("Decrypting: " + bytes(self.record).hex() + " ...")
data = self.sh.server.session.server_crypt.decrypt(bytes(self.record))
#self.logger.debug("Decrypting: " + bytes(self.record).hex() + " ... DONE")
# parse received record
#
# TODO: For simplicity, we hard-code use of the E2E-CRC for now
# because the 780G always seems to have that enabled. The value
# should be read from th CGM Feature characteristic instead.
self.logger.debug(f"read raw cgm measurement = {data.hex()}")
measurement_record = CGMMeasurement(data, use_crc=True)
if measurement_record.parse():
self.logger.debug(measurement_record)
else:
self.logger.error("Failed to parse measurement record")
return None
# parse received response
#
# see https://www.bluetooth.com/de/specifications/gss/,
# section 3.199 Record Access Control Point
#
# should be `06000101`:
# Op Code: 0x06 (Response Code)
# Operator: 0x00 (Null)
# Operand:
# Request Op Code: 0x01 (Report Stored Records)
# Response Code Value: 0x01 (Success)
if self.response != bytearray([6,0,1,1]):
self.logger.error("Unexpected response")
return float(measurement_record.glucose)
def _configure_characteristics(self):
# CGM service, CGM Measurement characteristic
self.logger.info("Adding characteristic CGM Measurement")
self.cgm_measurement = self.central.add_characteristic(
UUID.CGM_SERVICE, UUID.CGM_MEASUREMENT_CHAR)
while not self.cgm_measurement.resolve_gatt():
time.sleep(0.2)
assert "notify" in dbus_tools.dbus_to_python(self.cgm_measurement.flags)
self.cgm_measurement.add_characteristic_cb(self._measurement_cb)
self.logger.debug("measurement_cb added")
self.cgm_measurement.start_notify()
# CGM service, Record Access Control Point characteristic
self.logger.info("Adding characteristic RACP")
self.cgm_racp = self.central.add_characteristic(
UUID.CGM_SERVICE, UUID.CGM_RACP_CHAR)
while not self.cgm_racp.resolve_gatt():
time.sleep(0.2)
assert "write" in dbus_tools.dbus_to_python(self.cgm_racp.flags)
assert "indicate" in dbus_tools.dbus_to_python(self.cgm_racp.flags)
self.cgm_racp.add_characteristic_cb(self._racp_cb)
self.logger.debug("racp_cb added")
self.cgm_racp.start_notify()
return
def _racp_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
self.response = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("CGM RACP indication: " + self.response.hex())
self.operation_finished.set()
def _measurement_cb(self, iface, changed_props, invalidated_props):
if "Value" in changed_props:
self.record = dbus_tools.dbus_to_python(changed_props["Value"])
self.logger.debug("CGM Measurement notification: " + self.record.hex())
self.measurement_received.set()