|
| 1 | +"""Swiss army knife for QMI command line monitoring and control. |
| 2 | +
|
| 3 | +Run with `qmi_tool ls` or `qmi_tool lsqmi` to list all QMI contexts present on the network with the default |
| 4 | +workgroup name ("default"). To see other contexts in other workgroups, use: |
| 5 | +`qmi_tool ls <workgroup-name>`. |
| 6 | +You can also give timeout: |
| 7 | +`qmi_tool ls <workgroup-name> 10`. |
| 8 | +
|
| 9 | +You can also use this tool to kill all visible QMI contexts with the default workgroup name with: |
| 10 | +`qmi_tool hard-kill`. Use with care and never use without first checking which contexts will be killed, |
| 11 | +with `qmi_tool ls`. |
| 12 | +""" |
| 13 | + |
| 14 | +import random |
| 15 | +import sys |
| 16 | +import socket |
| 17 | +import time |
| 18 | +import re |
| 19 | + |
| 20 | +from qmi.core.config_defs import CfgQmi |
| 21 | +from qmi.core.context import ping_qmi_contexts |
| 22 | +from qmi.core.udp_responder_packets import QMI_UdpResponderKillRequestPacket |
| 23 | + |
| 24 | + |
| 25 | +UDP_RESPONDER_PORT = 35999 |
| 26 | + |
| 27 | + |
| 28 | +def lsqmi(workgroup_name: str = CfgQmi.workgroup, timeout: float = 0.1) -> None: |
| 29 | + """List all QMI contexts on the network. |
| 30 | +
|
| 31 | + Parameters: |
| 32 | + workgroup_name: The name of the workgroup to be searched for. Default is the CfgQmi.workgroup default. |
| 33 | + timeout: Timeout to wait for answers (default: 0.1). |
| 34 | + """ |
| 35 | + # Ping and collect responses. |
| 36 | + timestamped_packets = ping_qmi_contexts(workgroup_name_filter=workgroup_name, timeout=timeout) |
| 37 | + |
| 38 | + # Print info. |
| 39 | + print("Number of contexts found: {}".format(len(timestamped_packets))) |
| 40 | + |
| 41 | + for (response_packet_received_timestamp, incoming_address, response_packet) in timestamped_packets: |
| 42 | + # Estimate, on our local clock, at what time the packet was processed on the other side. |
| 43 | + t_at_remote = 0.5 * (response_packet.request_pkt_timestamp + response_packet_received_timestamp) # average |
| 44 | + |
| 45 | + # Determine clock deviation (their clock - our clock). |
| 46 | + clock_deviation = response_packet.pkt_timestamp - t_at_remote |
| 47 | + |
| 48 | + # Roundtrip time. |
| 49 | + roundtrip_time = response_packet_received_timestamp - response_packet.request_pkt_timestamp |
| 50 | + |
| 51 | + print("QMI_Context found: {}:{}, pid {!r}, name {!r}; workgroup {!r}; round-trip time {:.3f} ms; clock deviation {:+.3f} ms.".format( |
| 52 | + incoming_address[0], |
| 53 | + response_packet.context.port, |
| 54 | + response_packet.context.pid, |
| 55 | + response_packet.context.name.decode(), |
| 56 | + response_packet.context.workgroup_name.decode(), |
| 57 | + 1e3 * roundtrip_time, |
| 58 | + 1e3 * clock_deviation |
| 59 | + )) |
| 60 | + |
| 61 | + |
| 62 | +def hard_kill() -> None: |
| 63 | + """Broadcast a hard-kill request.""" |
| 64 | + # Preparing outgoing socket. |
| 65 | + udp_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) |
| 66 | + |
| 67 | + try: |
| 68 | + # Allow broadcasts on the socket. |
| 69 | + udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) |
| 70 | + |
| 71 | + # Send packet. |
| 72 | + address_out = ('<broadcast>', UDP_RESPONDER_PORT) |
| 73 | + request_pkt_id = random.randint(1, 2**64 - 1) |
| 74 | + request_packet = QMI_UdpResponderKillRequestPacket.create( |
| 75 | + request_pkt_id, |
| 76 | + time.time() |
| 77 | + ) |
| 78 | + udp_socket.sendto(request_packet, address_out) |
| 79 | + |
| 80 | + finally: |
| 81 | + udp_socket.close() |
| 82 | + |
| 83 | + |
| 84 | +def run() -> None | str: |
| 85 | + for e, arg in enumerate(sys.argv[1:]): |
| 86 | + if arg not in ["ls", "lsqmi", "hard-kill", "hard-kill-yes-really-i-am-sure"]: |
| 87 | + return f"Invalid argument {arg}." |
| 88 | + |
| 89 | + if arg == "ls" or arg == "lsqmi": |
| 90 | + if len(sys.argv) == (e + 3): |
| 91 | + # we have one input and need to check if it is timeout or workgroup name |
| 92 | + if len(re.findall(r"[a-zA-z]+", sys.argv[e+2])) == 0: |
| 93 | + # no alphabet characters so it must be a number |
| 94 | + lsqmi(timeout=float(sys.argv[e+2])) |
| 95 | + else: |
| 96 | + lsqmi(sys.argv[e+2]) |
| 97 | + |
| 98 | + elif len(sys.argv) == (e + 4): |
| 99 | + # we have two inputs and need to check which is timeout and which workgroup name |
| 100 | + if len(re.findall(r"[a-zA-z]+", sys.argv[e + 2])) == 0: |
| 101 | + # no alphabet characters so it must be a number and second argument must be the workgroup name |
| 102 | + lsqmi(sys.argv[e + 3], timeout=float(sys.argv[e + 2])) |
| 103 | + else: |
| 104 | + lsqmi(sys.argv[e + 2], timeout=float(sys.argv[e + 3])) |
| 105 | + |
| 106 | + else: |
| 107 | + # We use defaults |
| 108 | + lsqmi() |
| 109 | + |
| 110 | + if arg == "hard-kill": |
| 111 | + print("This kills all the contexts that are visible (see output of `ls`)!") |
| 112 | + print("If you are sure, use `qmi_tool hard-kill-yes-really-i-am-sure`") |
| 113 | + |
| 114 | + if arg == "hard-kill-yes-really-i-am-sure": |
| 115 | + hard_kill() |
| 116 | + |
| 117 | + return None |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + sys.exit(run()) |
0 commit comments