Enable easier cross-compiling for embedded use

This commit is contained in:
Bertold Van den Bergh 2021-09-07 15:15:52 +02:00
parent a7a959a677
commit 9f068704fb
12 changed files with 229 additions and 78 deletions

View file

@ -1,41 +0,0 @@
package main
import (
"os"
"github.com/sstallion/go-hid"
"errors"
)
func SearchDevice(foundHandler func(info *hid.DeviceInfo) error) error {
return hid.Enumerate(uint16(CLI.VID), uint16(CLI.PID), func(info *hid.DeviceInfo) error {
if CLI.Serial != "" && info.SerialNbr != CLI.Serial {
return nil
}
if CLI.RawPath != "" && info.Path != CLI.RawPath {
return nil
}
return foundHandler(info)
})
}
func OpenDevice() (*hid.Device, error) {
var dev *hid.Device
err := SearchDevice(func(info *hid.DeviceInfo) error {
d, err := hid.Open(info.VendorID, info.ProductID, info.SerialNbr)
if err == nil {
dev = d
return errors.New("Done")
}
return err
})
if dev != nil {
return dev, nil
}
if err == nil {
err = os.ErrNotExist
}
return nil, err
}

71
cli/hid_cgo.go Normal file
View file

@ -0,0 +1,71 @@
// +build !puregohid
package main
import (
"fmt"
"os"
"errors"
"github.com/BertoldVdb/ms-tools/gohid"
"github.com/sstallion/go-hid"
)
func SearchDevice(foundHandler func(info *hid.DeviceInfo) error) error {
return hid.Enumerate(uint16(CLI.VID), uint16(CLI.PID), func(info *hid.DeviceInfo) error {
if CLI.Serial != "" && info.SerialNbr != CLI.Serial {
return nil
}
if CLI.RawPath != "" && info.Path != CLI.RawPath {
return nil
}
return foundHandler(info)
})
}
func OpenDevice() (gohid.HIDDevice, error) {
var dev *hid.Device
err := SearchDevice(func(info *hid.DeviceInfo) error {
d, err := hid.Open(info.VendorID, info.ProductID, info.SerialNbr)
if err == nil {
dev = d
return errors.New("Done")
}
return err
})
if dev != nil {
return dev, nil
}
if err == nil {
err = os.ErrNotExist
}
return nil, err
}
type ListHIDCmd struct {
}
func (l *ListHIDCmd) Run(c *Context) error {
return SearchDevice(func(info *hid.DeviceInfo) error {
fmt.Printf("%s: ID %04x:%04x %s %s\n",
info.Path, info.VendorID, info.ProductID, info.MfrStr, info.ProductStr)
fmt.Println("Device Information:")
fmt.Printf("\tPath %s\n", info.Path)
fmt.Printf("\tVendorID %04x\n", info.VendorID)
fmt.Printf("\tProductID %04x\n", info.ProductID)
fmt.Printf("\tSerialNbr %s\n", info.SerialNbr)
fmt.Printf("\tReleaseNbr %x.%x\n", info.ReleaseNbr>>8, info.ReleaseNbr&0xff)
fmt.Printf("\tMfrStr %s\n", info.MfrStr)
fmt.Printf("\tProductStr %s\n", info.ProductStr)
fmt.Printf("\tUsagePage %#x\n", info.UsagePage)
fmt.Printf("\tUsage %#x\n", info.Usage)
fmt.Printf("\tInterfaceNbr %d\n", info.InterfaceNbr)
fmt.Println()
return nil
})
return nil
}

24
cli/hid_pure.go Normal file
View file

@ -0,0 +1,24 @@
// +build puregohid
package main
import (
"errors"
"github.com/BertoldVdb/ms-tools/gohid"
)
func OpenDevice() (gohid.HIDDevice, error) {
if CLI.RawPath == "" {
return nil, errors.New("RawPath must be specified when using pure GO HID")
}
return gohid.OpenHID(CLI.RawPath)
}
type ListHIDCmd struct {
}
func (l *ListHIDCmd) Run(c *Context) error {
return errors.New("This command is not supported using pure GO HID")
}

View file

@ -1,31 +1 @@
package main package main
import (
"fmt"
"github.com/sstallion/go-hid"
)
type ListHIDCmd struct {
}
func (l *ListHIDCmd) Run(c *Context) error {
return SearchDevice(func(info *hid.DeviceInfo) error {
fmt.Printf("%s: ID %04x:%04x %s %s\n",
info.Path, info.VendorID, info.ProductID, info.MfrStr, info.ProductStr)
fmt.Println("Device Information:")
fmt.Printf("\tPath %s\n", info.Path)
fmt.Printf("\tVendorID %04x\n", info.VendorID)
fmt.Printf("\tProductID %04x\n", info.ProductID)
fmt.Printf("\tSerialNbr %s\n", info.SerialNbr)
fmt.Printf("\tReleaseNbr %x.%x\n", info.ReleaseNbr>>8, info.ReleaseNbr&0xff)
fmt.Printf("\tMfrStr %s\n", info.MfrStr)
fmt.Printf("\tProductStr %s\n", info.ProductStr)
fmt.Printf("\tUsagePage %#x\n", info.UsagePage)
fmt.Printf("\tUsage %#x\n", info.Usage)
fmt.Printf("\tInterfaceNbr %d\n", info.InterfaceNbr)
fmt.Println()
return nil
})
}

View file

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/BertoldVdb/ms-tools/gohid"
"github.com/BertoldVdb/ms-tools/mshal" "github.com/BertoldVdb/ms-tools/mshal"
"github.com/alecthomas/kong" "github.com/alecthomas/kong"
"github.com/sstallion/go-hid"
) )
type Context struct { type Context struct {
dev *hid.Device dev gohid.HIDDevice
hal *mshal.HAL hal *mshal.HAL
} }
@ -56,8 +56,8 @@ func main() {
return return
} }
hid.Init() // hid.Init()
defer hid.Exit() // defer hid.Exit()
c := &Context{} c := &Context{}
if ctx.Command() != "list-dev" { if ctx.Command() != "list-dev" {

4
cli/tmp/patch_sr.txt Normal file
View file

@ -0,0 +1,4 @@
sudo ./cli write RAM 0xc190 77
sudo ./cli write RAM 0xc191 01
sudo ./cli write RAM 0xc190 0x77

1
go.mod
View file

@ -8,4 +8,5 @@ require (
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3
github.com/sstallion/go-hid v0.0.0-20190621001400-1cf4630be9f4 github.com/sstallion/go-hid v0.0.0-20190621001400-1cf4630be9f4
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1
) )

11
gohid/gohid.go Normal file
View file

@ -0,0 +1,11 @@
package gohid
type HIDDevice interface {
GetFeatureReport(b []byte) (int, error)
SendFeatureReport(b []byte) (int, error)
Close() error
}
func OpenHID(path string) (HIDDevice, error) {
return openHIDInternal(path)
}

9
gohid/gohid_default.go Normal file
View file

@ -0,0 +1,9 @@
// +build !linux
package gohid
import "errors"
func openHIDInternal(path string) (HIDDevice, error) {
return nil, errors.New("Platform is not supported")
}

90
gohid/gohid_linux.go Normal file
View file

@ -0,0 +1,90 @@
// +build linux
package gohid
import (
"errors"
"fmt"
"os"
"runtime"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
type HIDRaw struct {
dev *os.File
}
func openHIDInternal(path string) (HIDDevice, error) {
dev, err := os.Open(path)
if err != nil {
return nil, err
}
return &HIDRaw{
dev: dev,
}, nil
}
var ErrorTooLong = errors.New("Transfer is too long")
/*
HIDIOCSFEATURE(0) = C0004806
HIDIOCSFEATURE(9) = C0094806
HIDIOCGFEATURE(0) = C0004807
HIDIOCGFEATURE(9) = C0094807
*/
func (h *HIDRaw) SendFeatureReport(b []byte) (int, error) {
var tmp [1024]byte
if len(b) > len(tmp) {
return 0, ErrorTooLong
}
copy(tmp[:], b)
_, _, errno := unix.Syscall(
syscall.SYS_IOCTL,
uintptr(h.dev.Fd()),
uintptr(uint32(0xC0004806)|uint32(len(b)<<16)),
uintptr(unsafe.Pointer(&tmp)),
)
runtime.KeepAlive(tmp)
if errno != 0 {
return 0, os.NewSyscallError("SendFeatureReport", fmt.Errorf("%d", int(errno)))
}
return len(b), nil
}
func (h *HIDRaw) GetFeatureReport(b []byte) (int, error) {
var tmp [256]byte
if len(b) > len(tmp) {
return 0, ErrorTooLong
}
_, _, errno := unix.Syscall(
syscall.SYS_IOCTL,
uintptr(h.dev.Fd()),
uintptr(uint32(0xC0004807)|uint32(len(b)<<16)),
uintptr(unsafe.Pointer(&tmp)),
)
if errno != 0 {
return 0, os.NewSyscallError("GetFeatureReport", fmt.Errorf("%d", int(errno)))
}
copy(b, tmp[:])
return len(b), nil
}
func (h *HIDRaw) Close() error {
return h.dev.Close()
}

12
gohid/ioctl_resolve.c.txt Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
int main(){
printf("HIDIOCSFEATURE(0) = %08X\n", HIDIOCSFEATURE(0));
printf("HIDIOCSFEATURE(9) = %08X\n", HIDIOCSFEATURE(9));
printf("HIDIOCGFEATURE(0) = %08X\n", HIDIOCGFEATURE(0));
printf("HIDIOCGFEATURE(9) = %08X\n", HIDIOCGFEATURE(9));
return 0;
}

View file

@ -1,11 +1,11 @@
package mshal package mshal
import ( import (
"github.com/sstallion/go-hid" "github.com/BertoldVdb/ms-tools/gohid"
) )
type HAL struct { type HAL struct {
dev *hid.Device dev gohid.HIDDevice
deviceType int deviceType int
deviceTypeExtra int deviceTypeExtra int
@ -33,7 +33,7 @@ type HALConfig struct {
LogFunc LogFunc LogFunc LogFunc
} }
func New(dev *hid.Device, config HALConfig) (*HAL, error) { func New(dev gohid.HIDDevice, config HALConfig) (*HAL, error) {
h := &HAL{ h := &HAL{
dev: dev, dev: dev,
config: config, config: config,