Added lockfile to dev

This commit is contained in:
Matthias Fulz 2022-06-27 02:07:55 +02:00
parent afca7b34aa
commit 90ae4ac116
2 changed files with 41 additions and 1 deletions

View File

@ -285,6 +285,9 @@ func setCfg(dev *QEncDevice, cfg QEncCfg, val uint8) (err error) {
} }
func sendDataWrapper(dev *QEncDevice, data []byte, cmd QEncCmd, id uint32) (ret []byte, err error) { func sendDataWrapper(dev *QEncDevice, data []byte, cmd QEncCmd, id uint32) (ret []byte, err error) {
dev.Lock()
defer dev.UnLock()
done := make(chan bool) done := make(chan bool)
msgc := make(chan string) msgc := make(chan string)

View File

@ -3,6 +3,7 @@ package qmkenc
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os"
"time" "time"
"github.com/sstallion/go-hid" "github.com/sstallion/go-hid"
@ -21,6 +22,7 @@ type QEncDevice struct {
timeout int timeout int
retryTimeout int retryTimeout int
retryWait int retryWait int
lockfile string
} }
func (d *QEncDevice) GetPath() string { return d.path } func (d *QEncDevice) GetPath() string { return d.path }
@ -42,9 +44,16 @@ func (d *QEncDevice) Open() (err error) {
} }
func (d *QEncDevice) Close() (err error) { func (d *QEncDevice) Close() (err error) {
if d.lockfile != "" {
os.Remove(d.lockfile)
}
return d.dev.Close() return d.dev.Close()
} }
func (d *QEncDevice) SetLockfile(lockfile string) {
d.lockfile = lockfile
}
func (d *QEncDevice) SendBuffer(data []byte, msg chan string) (ret []byte, err error) { func (d *QEncDevice) SendBuffer(data []byte, msg chan string) (ret []byte, err error) {
startTime := time.Now() startTime := time.Now()
for { for {
@ -130,13 +139,41 @@ func (d *QEncDevice) setBufSize() (err error) {
return fmt.Errorf("Unsupported device: '%w'", err) return fmt.Errorf("Unsupported device: '%w'", err)
} }
func QEncNewDevice(path string, bufSize uint8, timeout, retryTimeout, retryWait int) *QEncDevice { func (d *QEncDevice) Lock() (err error) {
if d.lockfile == "" {
return nil
}
for {
if _, err := os.Stat(d.lockfile); err == nil {
time.Sleep(time.Second * 1)
continue
} else if os.IsNotExist(err) {
file, err := os.Create(d.lockfile)
if err != nil {
return err
}
file.Close()
return nil
}
}
}
func (d *QEncDevice) UnLock() {
if d.lockfile == "" {
return
}
os.Remove(d.lockfile)
}
func QEncNewDevice(path string, bufSize uint8, timeout, retryTimeout, retryWait int, lockfile string) *QEncDevice {
return &QEncDevice{ return &QEncDevice{
path: path, path: path,
timeout: timeout, timeout: timeout,
retryTimeout: retryTimeout, retryTimeout: retryTimeout,
retryWait: retryWait, retryWait: retryWait,
bufSize: bufSize, bufSize: bufSize,
lockfile: lockfile,
} }
} }