Posted to tcl by schelte at Mon Oct 31 08:10:04 GMT 2022view pretty

package require cffi

namespace eval hid {
    namespace ensemble create -subcommands {open enumerate}

    cffi::Wrapper create hidapi /usr/lib64/libhidapi-hidraw.so
    cffi::Struct create hid_device_info {
        path string
        vid ushort
        pid ushort
        serial pointer.wchar_t
        release ushort
        mfr pointer.wchar_t
        product pointer.wchar_t
        page ushort
        usage ushort
        intf int
        next pointer.hid_device_info
    }
    hidapi functions {
        hid_init int {}
        hid_open_path pointer.hid_device {
            path string
        }
        hid_enumerate pointer.hid_device_info {
            vid ushort
            pid ushort
        }
        hid_free_enumeration void {
            devs {pointer.hid_device_info dispose}
        }
    }

    hid_init

    proc open {path} {
        return [hid_open_path $path]
    }

    proc enumerate {vid pid} {
        set devs [hid_enumerate $vid $pid]
        try {
            set rc {}
            set devptr $devs
            set size [dict get [hid_device_info info] Size]
            while {![cffi::pointer isnull $devptr]} {
                set data [cffi::memory tobinary $devptr $size]]
                set struct [hid_device_info frombinary $data]
                set devptr [dict get $struct next]
                lappend rc [dict remove $struct next]
            }
            return $rc
        } finally {
            # Commenting out this makes the error go away
            hid_free_enumeration $devs
        }
    }
}

set list [hid enumerate 0x1209 0x5432]
set path [dict get [lindex $list 0] path]
hid open $path


Value has the wrong type. Pointer tag does not match registered tag.
    while executing
"hid_open_path $path"
    (procedure "open" line 2)
    invoked from within
"hid open $path"
    (file "hidapi-bug.tcl" line 64)