summaryrefslogtreecommitdiffstats
path: root/reversing_tools/abbott/freestyle_hid_console.py
blob: 4697da9a9bef36d5b8fe3776060d5363d42c7cd5 (plain) (blame)
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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""CLI tool to send messages through FreeStyle HID protocol."""

import argparse
import logging
import sys

from glucometerutils import exceptions
from glucometerutils.support import freestyle

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--text_cmd_type', action='store', type=int, default=0x60,
        help='Message type for text commands sent to the device.')
    parser.add_argument(
        '--text_reply_type', action='store', type=int, default=0x60,
        help='Message type for text replies received from the device.')
    parser.add_argument(
        'device', action='store',
        help='Path to the HID device to open.')

    parser.add_argument(
        '--vlog', action='store', required=False, type=int,
        help=('Python logging level. See the levels at '
              'https://docs.python.org/3/library/logging.html#logging-levels'))

    args = parser.parse_args()

    logging.basicConfig(level=args.vlog)

    device = freestyle.FreeStyleHidDevice(args.device)
    device.TEXT_CMD = args.text_cmd_type
    device.TEXT_REPLY_CMD = args.text_reply_type

    device.connect()

    while True:
        if sys.stdin.isatty():
            command = input('>>> ')
        else:
            command = input()
            print('>>> {command}'.format(command=command))

        try:
            print(device._send_text_command(bytes(command, 'ascii')))
        except exceptions.InvalidResponse as error:
            print('! {error}'.format(error=error))

if __name__ == "__main__":
    main()