示例#1
0
def test_run_command():
    args = types.SimpleNamespace()
    args.debug = True
    args.verbose = True

    daemon = Daemon("test_run_command", args)
    global logger
    logger = daemon.get_logger()

    result = daemon.run_command(["true"])
    assert result.returncode == 0

    try:
        result = daemon.run_command(["false"])
    except Exception as e:
        logger.debug(f"{e}")
def main():
    parser = argparse.ArgumentParser(
        description="Contact tracing beacon trasmitter")
    parser.add_argument("-d",
                        "--debug",
                        help="Run in debug mode logging to stderr",
                        action="store_true")
    parser.add_argument(
        "-D",
        "--database",
        help="Specify the database location",
        default="/var/lib/epidose/client-database.db",
    )
    parser.add_argument(
        "-i",
        "--iface",
        help="Transmit on the specified interface, not [hci]0",
        type=int,
        default=0,
    )
    parser.add_argument(
        "-n",
        "--dry-run",
        help="Do not execute the required command(s)",
        action="store_true",
    )
    parser.add_argument(
        "-r",
        "--rssi",
        help="Specify the received signal strength indication",
        type=int,
        default=0xC0,
    )
    parser.add_argument("-t",
                        "--test",
                        help="Test script",
                        action="store_true")
    parser.add_argument("-v",
                        "--verbose",
                        help="Set verbose logging",
                        action="store_true")
    args = parser.parse_args()

    if args.test:
        args.debug = True
        args.dry_run = True
        args.database = ":memory:"

    # Setup logging
    global daemon
    daemon = Daemon("beacon_tx", args)
    global logger
    logger = daemon.get_logger()

    global dry_run
    dry_run = args.dry_run

    interface = f"hci{args.iface}"

    # Transmit and store beacon packets
    current_ephid = None
    transmitter = ContactTracer(None, args.database, receiver=False)
    sleeper = InterruptibleSleep([signal.SIGTERM, signal.SIGINT])
    while True:
        now = datetime.now()
        transmitter.check_advance_day(now)
        ephid = transmitter.get_ephid_for_time(now)
        if ephid != current_ephid:

            # Generate random bdaddr
            bdaddr = generate_random_bdaddr()
            bdaddr = bdaddr.split()

            # Change local device bdaddr
            daemon.run_command([
                "hcitool",
                "-i",
                interface,
                "cmd",
                "0x3f",
                "0x001",
                bdaddr[5],
                bdaddr[4],
                bdaddr[3],
                bdaddr[2],
                bdaddr[1],
                bdaddr[0],
            ])

            # Enable the bluetooth interface
            daemon.run_command(["hciconfig", interface, "up"])

            # Enable LE advertising
            daemon.run_command(
                ["hcitool", "-i", interface, "cmd", "0x08", "0x000a", "01"])

            set_transmit(interface, ephid, args.rssi)
            current_ephid = ephid
            logger.debug(f"Change ephid to {ephid.hex()}")

        # Wait for the current epoch (e.g. 15 minutes) to pass
        # The API can't tell us TTL, so sample every minute
        sleeper.sleep(EPOCH_LENGTH / 60)
        if sleeper.signaled:
            # Stop advertising
            daemon.run_command(["hciconfig", interface, "noleadv"])
            exit(0)
        if args.test:
            break