Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other options than shutdown #4

Open
Lvl4Sword opened this issue Dec 27, 2022 · 0 comments
Open

Other options than shutdown #4

Lvl4Sword opened this issue Dec 27, 2022 · 0 comments

Comments

@Lvl4Sword
Copy link
Member

What are some other options? Here's unbinding the added device ( USB-only for now ).
Just an example, not for production:

import asyncio
import pyudev

usb_devices = {}
usb_ids = {}


def get_all_usb():
    all_usb_devices = context.list_devices(subsystem='usb')
    for each_device in all_usb_devices:
        if each_device.device_node is not None:
            this_device = each_device.get('DEVPATH')
            device_ids = f"{each_device.get('ID_VENDOR_ID')}{each_device.get('ID_MODEL_ID')}"
            if device_ids in usb_ids:
                usb_ids[device_ids] += 1
            else:
                usb_ids[device_ids] = 1
            if this_device in usb_devices:
                usb_devices[this_device]['amount'] += 1
            else:
                usb_devices[this_device] = {}
                usb_devices[this_device]['amount'] = 1
                usb_devices[this_device]['ids'] = device_ids


async def main(monitor):
    for device in iter(monitor.poll, None):
        if device.get("SUBSYSTEM") == "usb":
            the_devpath = device.get('DEVPATH')
            the_specific_port = the_devpath.split('/')[-1]
            device_ids = f"{device.get('ID_VENDOR_ID')}:{device.get('ID_MODEL_ID')}"
            if device.get('ID_VENDOR_ID') is not None and device.get('ID_MODEL_ID') is not None:
                if device.action == 'add':
                    usb_devices[the_devpath] = device_ids
                    with open("/sys/bus/usb/drivers/usb/unbind", 'w') as usb_unbind:
                        usb_unbind.write(the_specific_port)
                elif device.action == 'remove':
                    if device.get('DEVPATH') in usb_devices:
                        usb_devices.pop(the_devpath)


if __name__ == '__main__':
    context = pyudev.Context()
    get_all_usb()
    pyudev_monitor = pyudev.Monitor.from_netlink(context)
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.run(main(pyudev_monitor))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant