tcpip-to-serial-agent.py 2.38 KB
Newer Older
Robin Mueller's avatar
Robin Mueller committed
1
#!/usr/bin/env python3
Robin Mueller's avatar
Robin Mueller committed
2
3
import argparse
import threading
Robin Mueller's avatar
Robin Mueller committed
4
5
import signal
import time
Robin Mueller's avatar
Robin Mueller committed
6

Robin Mueller's avatar
Robin Mueller committed
7
8
from agent.log import get_console_logger
from agent.tcp_handler import TcpHandler
Robin Mueller's avatar
Robin Mueller committed
9
from agent.conf import JSON_CONF_NAME
Robin Mueller's avatar
Robin Mueller committed
10
from agent.json import check_json_file
Robin Mueller's avatar
Robin Mueller committed
11
from agent.serial_handler import SerialHandler, determine_com_port
Robin Mueller's avatar
Robin Mueller committed
12
from agent.buffer import init_reception_buffer, set_max_slots
Robin Mueller's avatar
Robin Mueller committed
13
from agent.exit_handler import service_shutdown, ServiceExit, self_destruct_signalled
Robin Mueller's avatar
Robin Mueller committed
14
15
16
17


def main():
    logger = get_console_logger()
Robin Mueller's avatar
Robin Mueller committed
18
    print('-- Starting TCP/IP to Serial Agent for the iOBC --')
Robin Mueller's avatar
Robin Mueller committed
19
20
21
22
23
    # atexit.register(keyboard_interrupt_handler)
    # Register the signal handlers
    signal.signal(signal.SIGTERM, service_shutdown)
    signal.signal(signal.SIGINT, service_shutdown)

Robin Mueller's avatar
Robin Mueller committed
24
25
    arg_parser = argparse.ArgumentParser(description='TCP/IP to Serial Bridge for the iOBC')
    arg_parser.add_argument('-p', '--port', dest='port', type=str, help='Serial port')
Robin Mueller's avatar
Robin Mueller committed
26
    arg_parser.add_argument('--hint', dest='hint', type=str, help='Hint for serial port name')
Robin Mueller's avatar
Robin Mueller committed
27
    args = arg_parser.parse_args()
Robin Mueller's avatar
Robin Mueller committed
28
29

    check_json_file(JSON_CONF_NAME)
Robin Mueller's avatar
Robin Mueller committed
30
31
32
    if args.port is not None:
        logger.info(f'Serial port {args.port} was specified')
        serial_port = args.port
Robin Mueller's avatar
Robin Mueller committed
33
34
    else:
        serial_port = determine_com_port(json_cfg_path=JSON_CONF_NAME)
Robin Mueller's avatar
Robin Mueller committed
35
36
37
38
    # TODO: Use JSON file for non-volatile configuration.
    #       This file will also have a hint for the serial port name.
    #       Configuration: Serial port, reception buffer slots, custom port, baudrate,
    #       serial port hint
Robin Mueller's avatar
Robin Mueller committed
39
40
41
    set_max_slots(max_slots=100)
    init_reception_buffer()

Robin Mueller's avatar
Robin Mueller committed
42
43
44
    ser_handler = SerialHandler(serial_port=serial_port)
    tcp_handler = TcpHandler()

Robin Mueller's avatar
Robin Mueller committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    serial_polling_thread = None
    tcp_thread = None
    try:
        serial_polling_thread = threading.Thread(target=ser_handler.run)
        tcp_thread = threading.Thread(target=tcp_handler.run)
        serial_polling_thread.start()
        tcp_thread.start()
        # Keep the main thread running, otherwise signals are ignored.
        while True:
            if self_destruct_signalled():
                raise ServiceExit
            time.sleep(0.5)
    except ServiceExit:
        # Graceful exit
        ser_handler.stop()
        tcp_handler.stop()
        serial_polling_thread.join()
        tcp_thread.join()
Robin Mueller's avatar
Robin Mueller committed
63
    logger.warning('TCP/IP to Serial Agent exited unexpectedly')
Robin Mueller's avatar
Robin Mueller committed
64
65
66
67


if __name__ == "__main__":
    main()