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

Detect USB device connected #20

Open
4 tasks
SrMouraSilva opened this issue Mar 2, 2018 · 0 comments
Open
4 tasks

Detect USB device connected #20

SrMouraSilva opened this issue Mar 2, 2018 · 0 comments
Milestone

Comments

@SrMouraSilva
Copy link
Member

SrMouraSilva commented Mar 2, 2018

# accessory.py
# License GPLv2
# (c) Manuel Di Cerbo, Nexus-Computing GmbH
# https://github.com/rosterloh/pyusb-android/blob/master/accessory.py
import os
import socket

NETLINK_KOBJECT_UEVENT = 15

def main():
    while True:
        sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, NETLINK_KOBJECT_UEVENT)
        sock.bind((os.getpid(), -1))

        while True:
            data = sock.recv(512)
            vid = parse_uevent(data)
            if vid is not None:
                break

        sock.close()
        accessory_task(vid)


def parse_uevent(data):
    """
    :param bytes data:
    :return:
    """

    lines = data.decode('UTF8', 'ignore').split('\0')

    #lines = data.decode('UTF8').split('\0')#.replace('\fe', b'').decode('UTF8')
    print(lines)
    keys = []
    for line in lines:
        val = line.split('=')
        if len(val) == 2:
            keys.append((val[0], val[1]))

    attributes = dict(keys)
    if 'ACTION' in attributes and 'PRODUCT' in attributes:
        if attributes['ACTION'] == 'add':
            parts = attributes['PRODUCT'].split('/')
            return int(parts[0], 16)

    return None

def accessory_task(teste):
    print(teste)

if __name__ == '__main__':
    main()
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