"""
Pythonified linux asm-generic/ioctl.h .

Produce IOCTL command numbers from their individual components, simplifying
C header conversion to python (keeping magic constants and differences to
C code to a minimum).

Common parameter meanings:
    type (8-bits unsigned integer)
        Driver-imposed ioctl number.
    nr (8-bits unsigned integer)
        Driver-imposed ioctl function number.
"""
import sys
import os
import fcntl 

_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_DIRBITS = 2

_IOC_NRMASK = (1 << _IOC_NRBITS) - 1
_IOC_TYPEMASK = (1 << _IOC_TYPEBITS) - 1
_IOC_SIZEMASK = (1 << _IOC_SIZEBITS) - 1
_IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1

_IOC_NRSHIFT = 0
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS

IOC_NONE = 0
IOC_WRITE = 1
IOC_READ = 2

def IOC(dir, type, nr, size):
    """
    dir
        One of IOC_NONE, IOC_WRITE, IOC_READ, or IOC_READ|IOC_WRITE.
        Direction is from the application's point of view, not kernel's.
    size (14-bits unsigned integer)
        Size of the buffer passed to ioctl's "arg" argument.
    """
    assert dir <= _IOC_DIRMASK, dir
    assert type <= _IOC_TYPEMASK, type
    assert nr <= _IOC_NRMASK, nr
    assert size <= _IOC_SIZEMASK, size
    return (dir << _IOC_DIRSHIFT) | (type << _IOC_TYPESHIFT) | (nr << _IOC_NRSHIFT) | (size << _IOC_SIZESHIFT)

def IO(type, nr):
    """
    An ioctl with no parameters.
    """
    return IOC(IOC_NONE, type, nr, 0)

# NOTE(sileht): default is 30 seconds

NBD_SET_TIMEOUT = IO( 0xab, 9 )

if len(sys.argv) == 2:
    with open(sys.argv[1], "wb") as fd:
        fcntl.ioctl(fd, NBD_SET_TIMEOUT, 60)
else:
    print("device missing")

