ioctl-opt


Nameioctl-opt JSON
Version 1.3 PyPI version JSON
download
home_pagehttp://github.com/vpelletier/python-ioctl-opt
SummaryFunctions to compute fnctl.ioctl's opt argument
upload_time2023-03-28 12:39:16
maintainer
docs_urlNone
authorVincent Pelletier
requires_python
licenseGPL 2+
keywords ioctl
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Pythonified linux ``asm-generic/ioctl.h`` .

So you can replicate driver's code computing ``fcntl.ioctl``'s ``opt`` argument.

For example, starting from the following IOCTL declaration (taken from ``input.h``):

.. code:: C

  #include <sys/ioctl.h>
  #define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */

you could write the following:

.. code:: python

  from ioctl_opt import IOC, IOC_READ
  EVIOCGNAME = lambda length: IOC(IOC_READ, ord('E'), 0x06, length)

The differences are minimal, and all come from python language or coding style:

- macros/constants to use from ``ioctl_opt`` for not start with an underscore
- defined macro becomes a callable (here a lambda, could be function)
- ``IOC``'s ``nr`` argument has to be an integer, so C's single-quote char becomes an ``ord`` call
- avoid shadowing built-in ``len`` function

The ``length`` argument is an object (typically serving as an I/O buffer with the ``fcntl.ioctl()``
call), the size of which is incorporated in the generated ioctl ``opt`` value.  ``IOC()`` will
calculate the size of the object.  Supported object types are:

- any ``ctypes`` type or instance
- ``memoryview``
- ``bytearray``
- ``struct.Struct``
- ``array.array``

You may want to then write a pythonic function to conveniently access that ioctl:

.. code:: python

  import ctypes
  import fcntl

  def getDeviceName(fd, length=1024):
      name = (ctypes.c_char * length)()
      actual_length = fcntl.ioctl(fd, EVIOCGNAME(length), name, True)
      if actual_length < 0:
          raise OSError(-actual_length)
      if name[actual_length - 1] == b'\x00':
          actual_length -= 1
      return name[:actual_length]

More advanced example defining hidraw ioctls, requiring structures (for more on how structures are defined, check python's ctype documentation for your python version):

.. code:: python

  import ctypes
  from ioctl_opt import IOR, IOC, IOC_READ, IOC_WRITE

  # hid.h
  HID_MAX_DESCRIPTOR_SIZE = 4096

  # hidraw.h
  class hidraw_report_descriptor(ctypes.Structure):
      _fields_ = [
          ('size', ctypes.c_uint),
          ('value', ctypes.c_ubyte * HID_MAX_DESCRIPTOR_SIZE),
      ]

  class hidraw_devinfo(ctypes.Structure):
      _fields_ = [
          ('bustype', ctypes.c_uint),
          ('vendor', ctypes.c_short),
          ('product', ctypes.c_short),
      ]

  HIDIOCGRDESCSIZE = IOR(ord('H'), 0x01, ctypes.c_int)
  HIDIOCGRDESC = IOR(ord('H'), 0x02, hidraw_report_descriptor)
  HIDIOCGRAWINFO = IOR(ord('H'), 0x03, hidraw_devinfo)
  HIDIOCGRAWNAME = lambda length: IOC(IOC_READ, ord('H'), 0x04, length)
  HIDIOCGRAWPHYS = lambda length: IOC(IOC_READ, ord('H'), 0x05, length)
  HIDIOCSFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x06, length)
  HIDIOCGFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x07, length)

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/vpelletier/python-ioctl-opt",
    "name": "ioctl-opt",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ioctl",
    "author": "Vincent Pelletier",
    "author_email": "plr.vincent@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/33/58/4e7c0921c0e92dd18928043ce167952d6201206703ff31c44e3477523362/ioctl-opt-1.3.tar.gz",
    "platform": null,
    "description": "Pythonified linux ``asm-generic/ioctl.h`` .\n\nSo you can replicate driver's code computing ``fcntl.ioctl``'s ``opt`` argument.\n\nFor example, starting from the following IOCTL declaration (taken from ``input.h``):\n\n.. code:: C\n\n  #include <sys/ioctl.h>\n  #define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */\n\nyou could write the following:\n\n.. code:: python\n\n  from ioctl_opt import IOC, IOC_READ\n  EVIOCGNAME = lambda length: IOC(IOC_READ, ord('E'), 0x06, length)\n\nThe differences are minimal, and all come from python language or coding style:\n\n- macros/constants to use from ``ioctl_opt`` for not start with an underscore\n- defined macro becomes a callable (here a lambda, could be function)\n- ``IOC``'s ``nr`` argument has to be an integer, so C's single-quote char becomes an ``ord`` call\n- avoid shadowing built-in ``len`` function\n\nThe ``length`` argument is an object (typically serving as an I/O buffer with the ``fcntl.ioctl()``\ncall), the size of which is incorporated in the generated ioctl ``opt`` value.  ``IOC()`` will\ncalculate the size of the object.  Supported object types are:\n\n- any ``ctypes`` type or instance\n- ``memoryview``\n- ``bytearray``\n- ``struct.Struct``\n- ``array.array``\n\nYou may want to then write a pythonic function to conveniently access that ioctl:\n\n.. code:: python\n\n  import ctypes\n  import fcntl\n\n  def getDeviceName(fd, length=1024):\n      name = (ctypes.c_char * length)()\n      actual_length = fcntl.ioctl(fd, EVIOCGNAME(length), name, True)\n      if actual_length < 0:\n          raise OSError(-actual_length)\n      if name[actual_length - 1] == b'\\x00':\n          actual_length -= 1\n      return name[:actual_length]\n\nMore advanced example defining hidraw ioctls, requiring structures (for more on how structures are defined, check python's ctype documentation for your python version):\n\n.. code:: python\n\n  import ctypes\n  from ioctl_opt import IOR, IOC, IOC_READ, IOC_WRITE\n\n  # hid.h\n  HID_MAX_DESCRIPTOR_SIZE = 4096\n\n  # hidraw.h\n  class hidraw_report_descriptor(ctypes.Structure):\n      _fields_ = [\n          ('size', ctypes.c_uint),\n          ('value', ctypes.c_ubyte * HID_MAX_DESCRIPTOR_SIZE),\n      ]\n\n  class hidraw_devinfo(ctypes.Structure):\n      _fields_ = [\n          ('bustype', ctypes.c_uint),\n          ('vendor', ctypes.c_short),\n          ('product', ctypes.c_short),\n      ]\n\n  HIDIOCGRDESCSIZE = IOR(ord('H'), 0x01, ctypes.c_int)\n  HIDIOCGRDESC = IOR(ord('H'), 0x02, hidraw_report_descriptor)\n  HIDIOCGRAWINFO = IOR(ord('H'), 0x03, hidraw_devinfo)\n  HIDIOCGRAWNAME = lambda length: IOC(IOC_READ, ord('H'), 0x04, length)\n  HIDIOCGRAWPHYS = lambda length: IOC(IOC_READ, ord('H'), 0x05, length)\n  HIDIOCSFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x06, length)\n  HIDIOCGFEATURE = lambda length: IOC(IOC_WRITE|IOC_READ, ord('H'), 0x07, length)\n",
    "bugtrack_url": null,
    "license": "GPL 2+",
    "summary": "Functions to compute fnctl.ioctl's opt argument",
    "version": "1.3",
    "split_keywords": [
        "ioctl"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "33584e7c0921c0e92dd18928043ce167952d6201206703ff31c44e3477523362",
                "md5": "0a8991b5f58ff91bd55916eb143a65af",
                "sha256": "5ed4f9a80d2e02e152a43d3648d7ed8821a0aac5ea88ecc5fcc14460ff7cf2f9"
            },
            "downloads": -1,
            "filename": "ioctl-opt-1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "0a8991b5f58ff91bd55916eb143a65af",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10519,
            "upload_time": "2023-03-28T12:39:16",
            "upload_time_iso_8601": "2023-03-28T12:39:16.556690Z",
            "url": "https://files.pythonhosted.org/packages/33/58/4e7c0921c0e92dd18928043ce167952d6201206703ff31c44e3477523362/ioctl-opt-1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-28 12:39:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "vpelletier",
    "github_project": "python-ioctl-opt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ioctl-opt"
}
        
Elapsed time: 0.04800s