forked from OpenPrinting/ipp-usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
211 lines (172 loc) · 4.52 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Device object brings all parts together
*/
package main
import (
"context"
"net"
"net/http"
"time"
)
// Device object brings all parts together, namely:
// * HTTP proxy server
// * USB-backed http.Transport
// * DNS-SD advertiser
//
// There is one instance of Device object per USB device
type Device struct {
UsbAddr UsbAddr // Device's USB address
State *DevState // Persistent state
HTTPClient *http.Client // HTTP client for internal queries
HTTPProxy *HTTPProxy // HTTP proxy
UsbTransport *UsbTransport // Backing USB transport
DNSSdPublisher *DNSSdPublisher // DNS-SD publisher
Log *Logger // Device's logger
}
// NewDevice creates new Device object
func NewDevice(desc UsbDeviceDesc) (*Device, error) {
dev := &Device{
UsbAddr: desc.UsbAddr,
}
var err error
var info UsbDeviceInfo
var listener net.Listener
var ippinfo *IppPrinterInfo
var dnssdName string
var dnssdServices DNSSdServices
var log *LogMessage
// Create USB transport
dev.UsbTransport, err = NewUsbTransport(desc)
if err != nil {
goto ERROR
}
// Obtain device's logger
info = dev.UsbTransport.UsbDeviceInfo()
dev.Log = dev.UsbTransport.Log()
// Load persistent state
dev.State = LoadDevState(info.Ident(), info.Comment())
// Create HTTP client for local queries
dev.HTTPClient = &http.Client{
Transport: dev.UsbTransport,
}
// Create net.Listener
listener, err = dev.State.HTTPListen()
if err != nil {
goto ERROR
}
// Create HTTP server
dev.UsbTransport.SetDeadline(time.Now().Add(DevInitTimeout))
dev.HTTPProxy = NewHTTPProxy(dev.Log, listener, dev.UsbTransport)
// Obtain DNS-SD info for IPP
log = dev.Log.Begin()
defer log.Commit()
ippinfo, err = IppService(log, &dnssdServices,
dev.State.HTTPPort, info, dev.HTTPClient)
if err != nil {
dev.Log.Error('!', "IPP: %s", err)
}
log.Flush()
if dev.UsbTransport.DeadlineExpired() {
err = ErrInitTimedOut
goto ERROR
}
// Obtain DNS-SD name
if ippinfo != nil {
dnssdName = ippinfo.DNSSdName
} else {
dnssdName = info.DNSSdName()
}
// Update device state, if name changed
if dnssdName != dev.State.DNSSdName {
dev.State.DNSSdName = dnssdName
dev.State.DNSSdOverride = dnssdName
dev.State.Save()
}
// Obtain DNS-SD info for eSCL
err = EsclService(log, &dnssdServices, dev.State.HTTPPort, info,
ippinfo, dev.HTTPClient)
if err != nil {
dev.Log.Error('!', "ESCL: %s", err)
}
log.Flush()
if dev.UsbTransport.DeadlineExpired() {
err = ErrInitTimedOut
goto ERROR
}
// Update IPP service advertising for scanner presence
if ippinfo != nil {
if ippSvc := &dnssdServices[ippinfo.IppSvcIndex]; err == nil {
ippSvc.Txt.Add("Scan", "T")
} else {
ippSvc.Txt.Add("Scan", "F")
}
}
// Advertise Web service. Assume it always exists
dnssdServices.Add(DNSSdSvcInfo{Type: "_http._tcp", Port: dev.State.HTTPPort})
// Enable handling incoming requests
dev.UsbTransport.SetDeadline(time.Time{})
dev.HTTPProxy.Enable()
// Start DNS-SD publisher
for _, svc := range dnssdServices {
dev.Log.Debug('>', "%s: %s TXT record:", dnssdName, svc.Type)
for _, txt := range svc.Txt {
dev.Log.Debug(' ', " %s=%s", txt.Key, txt.Value)
}
}
if Conf.DNSSdEnable {
dev.DNSSdPublisher = NewDNSSdPublisher(dev.Log, dev.State,
dnssdServices)
err = dev.DNSSdPublisher.Publish()
if err != nil {
goto ERROR
}
}
return dev, nil
ERROR:
if dev.HTTPProxy != nil {
dev.HTTPProxy.Close()
}
if dev.UsbTransport != nil {
dev.UsbTransport.Close(true)
}
if listener != nil {
listener.Close()
}
return nil, err
}
// Shutdown gracefully shuts down the device. If provided context
// expires before the shutdown is complete, Shutdown returns the
// context's error
func (dev *Device) Shutdown(ctx context.Context) error {
if dev.DNSSdPublisher != nil {
dev.DNSSdPublisher.Unpublish()
dev.DNSSdPublisher = nil
}
if dev.HTTPProxy != nil {
dev.HTTPProxy.Close()
dev.HTTPProxy = nil
}
if dev.UsbTransport != nil {
return dev.UsbTransport.Shutdown(ctx)
}
return nil
}
// Close the Device
func (dev *Device) Close() {
if dev.DNSSdPublisher != nil {
dev.DNSSdPublisher.Unpublish()
dev.DNSSdPublisher = nil
}
if dev.HTTPProxy != nil {
dev.HTTPProxy.Close()
dev.HTTPProxy = nil
}
if dev.UsbTransport != nil {
dev.UsbTransport.Close(false)
dev.UsbTransport = nil
}
}