Skip to content

Commit

Permalink
Add CLI Support (thanks HeadoN)
Browse files Browse the repository at this point in the history
  • Loading branch information
spddl committed Dec 15, 2022
1 parent c84f802 commit 024939e
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
62 changes: 62 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"time"

Expand All @@ -16,6 +18,65 @@ import (
func main() {
var devices []Device
devices, handle = FindAllDevices()

if CLIMode {
var newItem *Device
for i := 0; i < len(devices); i++ {
if devices[i].DevObjName == flagDevObjName {
newItem = &devices[i]
break
}
}
if newItem == nil {
SetupDiDestroyDeviceInfoList(handle)
os.Exit(1)
}

var assignmentSetOverride Bits
if flagCPU != "" {
cpuarray := strings.Split(flagCPU, ",")
for _, val := range cpuarray {
i, err := strconv.Atoi(val)
if err != nil {
log.Println(err)
continue
}
assignmentSetOverride = Set(assignmentSetOverride, CPUBits[i])
}

}

if flagMsiSupported != -1 || flagMessageNumberLimit != -1 {
if flagMsiSupported != -1 {
newItem.MsiSupported = uint32(flagMsiSupported)
}
if flagMessageNumberLimit != -1 {
newItem.MessageNumberLimit = uint32(flagMessageNumberLimit)
}
setMSIMode(newItem)
}

if flagDevicePolicy != -1 || flagDevicePriority != -1 || assignmentSetOverride != ZeroBit {
if flagDevicePolicy != -1 {
newItem.DevicePolicy = uint32(flagDevicePolicy)
}
if flagDevicePriority != -1 {
newItem.DevicePriority = uint32(flagDevicePriority)
}
if assignmentSetOverride != ZeroBit {
newItem.AssignmentSetOverride = assignmentSetOverride
}
setAffinityPolicy(newItem)
}

if flagRestart {
if err := SetupDiRestartDevices(handle, &newItem.Idata); err != nil {
log.Println(err)
}
}
SetupDiDestroyDeviceInfoList(handle)
os.Exit(0)
}
defer SetupDiDestroyDeviceInfoList(handle)

// Sortiert das Array nach Namen
Expand All @@ -39,6 +100,7 @@ func main() {
Width: 750,
Height: 600,
},
Background: SolidColorBrush{Color: walk.RGB(32, 32, 32)},
Layout: VBox{
MarginsZero: true,
SpacingZero: true,
Expand Down
5 changes: 3 additions & 2 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func RunDialog(owner walk.Form, device *Device) (int, error) {
Label{
Text: "DevObj Name:",
},
Label{
Text: Bind("device.DevObjName == '' ? 'N/A' : device.DevObjName"),
LineEdit{
Text: Bind("device.DevObjName == '' ? 'N/A' : device.DevObjName"),
ReadOnly: true,
},
},
},
Expand Down
82 changes: 82 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"flag"
"fmt"
"os"
)

var (
flagDevObjName string
flagDevicePriority int
flagDevicePolicy int
flagMsiSupported int
flagMessageNumberLimit int
flagCPU string
flagRestart bool
flagHelp bool

CLIMode bool
)

func init() {
flag.StringVar(&flagDevObjName, "devobj", "", "\\Device\\00000123")
flag.StringVar(&flagCPU, "cpu", "", "e.g. 0,1,2,4")
flag.IntVar(&flagDevicePriority, "priority", -1, "0=Undefined, 1=Low, 2=Normal, 3=High")
flag.IntVar(&flagDevicePolicy, "policy", -1, "0=Default, 1=All Close Proc, 2=One Close Proc, 3=All Proc in Machine, 4=Specified Proc, 5=Spread Messages Across All Proc")
flag.IntVar(&flagMsiSupported, "msisupported", -1, "0=Off, 1=On")
flag.IntVar(&flagMessageNumberLimit, "msilimit", -1, "Message Signaled Interrupt Limit")
flag.BoolVar(&flagRestart, "restart", false, "Restart target device")
flag.BoolVar(&flagHelp, "help", false, "Print Defaults")

flag.Parse()
if flagHelp {
fmt.Printf("Usage: %s [OPTIONS] argument ...\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}

if flagDevObjName != "" || flagDevicePriority != -1 || flagDevicePolicy != -1 || flagMsiSupported != -1 || flagMessageNumberLimit != -1 || flagRestart {
CLIMode = true
}

if flagDevObjName != "" {
fmt.Println("DevObjName:", flagDevObjName)
}
if flagDevicePriority != -1 {
var prio string
switch flagDevicePriority {
case 0:
prio = "Undefined"
case 1:
prio = "Low"
case 2:
prio = "Normal"
case 3:
prio = "High"
default:
prio = fmt.Sprintf("%d", flagDevicePriority)
}
fmt.Println("DevicePriority:", prio)
}
if flagDevicePolicy != -1 {
var policy string
switch flagDevicePolicy {
case IrqPolicyMachineDefault: // 0x00
policy = "Default"
case IrqPolicyAllCloseProcessors: // 0x01
policy = "All Close Proc"
case IrqPolicyOneCloseProcessor: // 0x02
policy = "One Close Proc"
case IrqPolicyAllProcessorsInMachine: // 0x03
policy = "All Proc in Machine"
case IrqPolicySpecifiedProcessors: // 0x04
policy = "Specified Proc"
case IrqPolicySpreadMessagesAcrossAllProcessors: // 0x05
policy = "Spread Messages Across All Proc"
default:
policy = fmt.Sprintf("%d", flagDevicePolicy)
}
fmt.Println("DevicePolicy:", policy)
}
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module github.com/spddl/go-interruptPolicy

go 1.16
go 1.19

require (
github.com/lxn/walk v0.0.0-20210112085537-c389da54e794
golang.org/x/sys v0.3.0
)

require (
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac
gopkg.in/Knetic/govaluate.v3 v3.0.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ github.com/lxn/walk v0.0.0-20210112085537-c389da54e794/go.mod h1:E23UucZGqpuUANJ
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/Knetic/govaluate.v3 v3.0.0 h1:18mUyIt4ZlRlFZAAfVetz4/rzlJs9yhN+U02F4u1AOc=
gopkg.in/Knetic/govaluate.v3 v3.0.0/go.mod h1:csKLBORsPbafmSCGTEh3U7Ozmsuq8ZSIlKk1bcqph0E=
2 changes: 2 additions & 0 deletions run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cls
gocritic check -enableAll -disable="#experimental,#opinionated,#commentedOutCode" ./...
go build -tags debug -o %filename%.exe

@REM IF %ERRORLEVEL% EQU 0 %filename%.exe -devobj \Device\NTPNP_PCI0015 -policy 4 -cpu 1,2,3 -restart
@REM IF %ERRORLEVEL% E0QU 0 %filename%.exe -devobj \Device\NTPNP_PCI0015 -msisupported 0
IF %ERRORLEVEL% EQU 0 %filename%.exe

pause
Expand Down

0 comments on commit 024939e

Please sign in to comment.