from typing import Tuple
from typing import Optional
import struct


def _unwrap_apdu(
    packet: bytes,
) -> Tuple[Optional[int], Optional[bytes], Optional[int], Optional[int], Optional[bytes]]:
    """
    Given a packet from the device, extract and return relevant info
    """
    if not packet:
        return None, None, None, None, None

    (channel, tag, packet_id, reply_size) = struct.unpack(">HBHH", packet[:7])

    if packet_id == 0:
        return channel, tag, packet_id, reply_size, packet[7:]

    return channel, tag, packet_id, None, packet[5:]