From 09a5fe1f8d34ee9a9f8263624a9b92a2bcd96f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Thu, 28 Dec 2017 20:16:22 +0000 Subject: freestyle: replace the custom struct and bytemangling with construct. This simplifies the code and enforces the validation within the format documentation too. construct can handle the full package verification, including dealing with padding. --- README | 31 ++++++++++++++++--------------- glucometerutils/support/freestyle.py | 19 +++++++++++-------- setup.py | 6 +++--- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/README b/README index a070a05..1f42487 100644 --- a/README +++ b/README @@ -32,21 +32,21 @@ $ . glucometerutils-venv/bin/activate Please see the following table for the driver for each device that is known and supported. -| Manufacturer | Model Name | Driver | Dependencies | -| --- | --- | --- | --- | -| LifeScan | OneTouch Ultra 2 | `otultra2` | [pyserial] | -| LifeScan | OneTouch Ultra Easy | `otultraeasy` | [pyserial] | -| LifeScan | OneTouch Ultra Mini | `otultraeasy` | [pyserial] | -| LifeScan | OneTouch Verio (USB) | `otverio2015` | [python-scsi] | -| LifeScan | OneTouch Select Plus | `otverio2015` | [python-scsi] | -| Abbott | FreeStyle InsuLinx† | `fsinsulinx` | [hidapi]‡ | -| Abbott | FreeStyle Libre | `fslibre` | [hidapi]‡ | -| Abbott | FreeStyle Optium | `fsoptium` | [pyserial] | -| Abbott | FreeStyle Precision Neo | `fsprecisionneo` | [hidapi]‡ | -| Abbott | FreeStyle Optium Neo | `fsprecisionneo` | [hidapi]‡ | -| Abbott | FreeStyle Optium Neo H | `fsprecisionneo` | [hidapi]‡ | -| Roche | Accu-Chek Mobile | `accuchek_reports` | | -| SD Biosensor | SD CodeFree | `sdcodefree` | [pyserial] | +| Manufacturer | Model Name | Driver | Dependencies | +| --- | --- | --- | --- | +| LifeScan | OneTouch Ultra 2 | `otultra2` | [pyserial] | +| LifeScan | OneTouch Ultra Easy | `otultraeasy` | [pyserial] | +| LifeScan | OneTouch Ultra Mini | `otultraeasy` | [pyserial] | +| LifeScan | OneTouch Verio (USB) | `otverio2015` | [python-scsi] | +| LifeScan | OneTouch Select Plus | `otverio2015` | [python-scsi] | +| Abbott | FreeStyle InsuLinx† | `fsinsulinx` | [construct] [hidapi]‡ | +| Abbott | FreeStyle Libre | `fslibre` | [construct] [hidapi]‡ | +| Abbott | FreeStyle Optium | `fsoptium` | [pyserial] | +| Abbott | FreeStyle Precision Neo | `fsprecisionneo` | [construct] [hidapi]‡ | +| Abbott | FreeStyle Optium Neo | `fsprecisionneo` | [construct] [hidapi]‡ | +| Abbott | FreeStyle Optium Neo H | `fsprecisionneo` | [construct] [hidapi]‡ | +| Roche | Accu-Chek Mobile | `accuchek_reports` | | +| SD Biosensor | SD CodeFree | `sdcodefree` | [pyserial] | † Untested. ‡ Optional dependency on Linux; required on other operating systems. @@ -60,6 +60,7 @@ If you have knowledge of a protocol of a glucometer you would have supported, please provide a reference, possibly by writing a specification and contribute it to https://github.com/Flameeyes/glucometer-protocols/. +[construct]: https://construct.readthedocs.io/en/latest/ [pyserial]: https://pythonhosted.org/pyserial/ [python-scsi]: https://github.com/rosjat/python-scsi [hidapi]: https://pypi.python.org/pypi/hidapi diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index d722c35..7610729 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -15,7 +15,8 @@ import csv import datetime import logging import re -import struct + +import construct from glucometerutils import exceptions from glucometerutils.support import hiddevice @@ -24,7 +25,13 @@ from glucometerutils.support import hiddevice # protocol. _INIT_SEQUENCE = (0x04, 0x05, 0x15, 0x01) -_STRUCT_PREAMBLE = struct.Struct('