Skip to content

Commit

Permalink
Added 'init-timeout' quirk
Browse files Browse the repository at this point in the history
This quirk defines timeout for the HTTP requests sent by the
ipp-usb to device during initialization.

Tests and documentation updated.
  • Loading branch information
alexpevzner committed Dec 3, 2024
1 parent d3651a7 commit ab37810
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ func NewDevice(desc UsbDeviceDesc) (*Device, error) {
var dnssdServices DNSSdServices
var log *LogMessage
var hwid string
var quirks Quirks

// Create USB transport
dev.UsbTransport, err = NewUsbTransport(desc)
if err != nil {
goto ERROR
}

// Obtain quirks
quirks = dev.UsbTransport.Quirks()

// Obtain device's logger
dev.Log = dev.UsbTransport.Log()

Expand All @@ -74,7 +78,7 @@ func NewDevice(desc UsbDeviceDesc) (*Device, error) {
}

// Configure transport for init
dev.UsbTransport.SetTimeout(DevInitTimeout)
dev.UsbTransport.SetTimeout(quirks.GetInitTimeout())

// Create HTTP server
dev.HTTPProxy = NewHTTPProxy(dev.Log, listener, dev.UsbTransport)
Expand Down
6 changes: 5 additions & 1 deletion ipp-usb.8
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,13 @@ Delay, between device is opened and, optionally, reset, and the first request is
.br
How to reset device during initialization\. Default is \fBnone\fR
.IP "\(bu" 4
\fBinit\-timeout\fR = DELAY
.br
Timeout for HTTP requests send by the \fBipp\-usb\fR during initialization\.
.IP "\(bu" 4
\fBrequest\-delay\fR = DELAY
.br
Delay, between subsequent requests\.
Delay between subsequent requests\.
.IP "\(bu" 4
\fBusb\-max\-interfaces = N\fR
.br
Expand Down
5 changes: 4 additions & 1 deletion ipp-usb.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,11 @@ The following parameters are defined:
* `init-reset = none | soft | hard`<br>
How to reset device during initialization. Default is `none`

* `init-timeout` = DELAY <br>
Timeout for HTTP requests send by the `ipp-usb` during initialization.

* `request-delay` = DELAY <br>
Delay, between subsequent requests.
Delay between subsequent requests.

* `usb-max-interfaces = N`<br>
Don't use more that N USB interfaces, even if more is available.
Expand Down
16 changes: 15 additions & 1 deletion quirks.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
QuirkNmIgnoreIppStatus = "ignore-ipp-status"
QuirkNmInitDelay = "init-delay"
QuirkNmInitReset = "init-reset"
QuirkNmInitTimeout = "init-timeout"
QuirkNmRequestDelay = "request-delay"
QuirkNmUsbMaxInterfaces = "usb-max-interfaces"
)
Expand All @@ -54,6 +55,7 @@ var quirkParse = map[string]func(*Quirk) error{
QuirkNmIgnoreIppStatus: (*Quirk).parseBool,
QuirkNmInitDelay: (*Quirk).parseDuration,
QuirkNmInitReset: (*Quirk).parseQuirkResetMethod,
QuirkNmInitTimeout: (*Quirk).parseDuration,
QuirkNmRequestDelay: (*Quirk).parseDuration,
QuirkNmUsbMaxInterfaces: (*Quirk).parseUint,
}
Expand All @@ -67,6 +69,7 @@ var quirkDefaultStrings = map[string]string{
QuirkNmIgnoreIppStatus: "false",
QuirkNmInitDelay: "0",
QuirkNmInitReset: "none",
QuirkNmInitTimeout: DevInitTimeout.String(),
QuirkNmRequestDelay: "0",
QuirkNmUsbMaxInterfaces: "0",
}
Expand All @@ -77,6 +80,7 @@ var quirkDefault = make(map[string]*Quirk)
// init populates quirkDefault using quirk values from quirkDefaultStrings.
func init() {
for name, value := range quirkDefaultStrings {
println(name, "=", value)
q := &Quirk{
Origin: "default",
Match: "*",
Expand All @@ -86,7 +90,11 @@ func init() {
}

parse := quirkParse[name]
parse(q)
err := parse(q)
if err != nil {
panic(err)
}

quirkDefault[name] = q
}
}
Expand Down Expand Up @@ -319,6 +327,12 @@ func (quirks Quirks) GetInitReset() QuirkResetMethod {
return quirks.Get(QuirkNmInitReset).Parsed.(QuirkResetMethod)
}

// GetInitTimeout returns effective "init-timeout" parameter
// taking the whole set into consideration.
func (quirks Quirks) GetInitTimeout() time.Duration {
return quirks.Get(QuirkNmInitTimeout).Parsed.(time.Duration)
}

// GetRequestDelay returns effective "request-delay" parameter
// taking the whole set into consideration.
func (quirks Quirks) GetRequestDelay() time.Duration {
Expand Down
11 changes: 11 additions & 0 deletions quirks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ func TestQuirksLookup(t *testing.T) {
origin: "default",
},

{
model: "Unknown Device",
param: QuirkNmInitTimeout,
get: func(quirks Quirks) interface{} {
return quirks.GetInitTimeout()
},
match: "*",
value: DevInitTimeout,
origin: "default",
},

{
model: "Unknown Device",
param: QuirkNmRequestDelay,
Expand Down

0 comments on commit ab37810

Please sign in to comment.