forked from albertcsm/go-selphy-cp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
selphy.go
514 lines (414 loc) · 11.7 KB
/
selphy.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*********************************************************************\
* *
* selphy.go *
* Client implementation of the Canon Selphy CP900 network protocol *
* *
* Copyright 2013 Wilmer van der Gaast <[email protected]> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of version 2 of the GNU General Public *
* License as published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301, USA. *
* *
\*********************************************************************/
/* Maybe needless to say, this is my first time using Go. Apologies for
not being bothered to split this into multiple files. */
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"log"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"unicode/utf16"
)
const (
CPNP_ADDR = "255.255.255.255"
CPNP_PORT = 8609
CPNP_MSG_DISCOVER = 0x101
CPNP_MSG_STARTTCP = 0x110
CPNP_MSG_ID = 0x130
CPNP_MSG_STATUS = 0x120
CPNP_MSG_DATA = 0x121
)
type cmd_handler func(head []byte, body []byte)
func cpnp_packet(command int, payload []byte) []byte {
ret := make([]byte, 16+len(payload))
copy(ret[0:], []byte("CPNP"))
binary.BigEndian.PutUint16(ret[4:], uint16(command))
binary.BigEndian.PutUint16(ret[14:], uint16(len(payload)))
copy(ret[16:], payload)
return ret
}
type device struct {
mac []byte
udps *net.UDPConn
dest *net.UDPAddr
tcps *net.TCPConn
tcpd *net.TCPAddr
tcpbuf []byte
cmdseq uint16
jobseq uint16
handlers map[uint16]cmd_handler
props map[string]string
last_status []byte
chunk []byte
last_wait uint
job *imgreader
cb func()
}
func new_device(printer_mac, printer_ip *string) *device {
var err error
c := new(device)
if *printer_mac != "" {
c.mac, err = hex.DecodeString(strings.Replace(*printer_mac, ":", "", -1))
checkError(err)
}
c.dest, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", *printer_ip, CPNP_PORT))
checkError(err)
c.udps, err = net.ListenUDP("udp", nil)
checkError(err)
c.handlers = make(map[uint16]cmd_handler)
c.props = make(map[string]string)
return c
}
func (c *device) send(msg []byte, h cmd_handler) {
var err error
c.cmdseq++
binary.BigEndian.PutUint16(msg[8:], c.cmdseq)
c.handlers[c.cmdseq] = h
if c.tcps != nil {
/* Job number is sent in the TCP start response, and is (I
suppose) used to track state instead of just the TCP
port number. */
binary.BigEndian.PutUint16(msg[10:], c.jobseq)
_, err = c.tcps.Write(msg)
} else {
_, err = c.udps.WriteTo(msg, c.dest)
}
checkError(err)
}
func (c *device) wait() {
for {
if c.tcps != nil {
c.wait_tcp()
} else {
c.wait_udp()
}
}
}
func (c *device) wait_udp() {
buf := make([]byte, 5120, 5120)
n, err := c.udps.Read(buf[0:])
log.Println("Read UDP")
checkError(err)
if bytes.Compare(buf[0:4], []byte("CPNP")) != 0 {
log.Println("UDP protocol error!")
} else {
c.handle_message(buf[0:n])
}
}
func (c *device) wait_tcp() {
buf := make([]byte, 5120, 5120)
n, err := c.tcps.Read(buf[0:])
checkError(err)
c.tcpbuf = c.tcpbuf[0 : len(c.tcpbuf)+n]
copy(c.tcpbuf[len(c.tcpbuf)-n:], buf[0:n])
if len(c.tcpbuf) < 16 {
return
}
if bytes.Compare(c.tcpbuf[0:4], []byte("CPNP")) != 0 {
log.Println("TCP protocol error!")
return
}
msglen := 16 + binary.BigEndian.Uint32(c.tcpbuf[12:])
if len(c.tcpbuf) < int(msglen) {
return
}
c.handle_message(c.tcpbuf[0:msglen])
buf = make([]byte, 0, 5120)
copy(buf[0:], c.tcpbuf[msglen:])
c.tcpbuf = buf
}
func (c *device) handle_message(buf []byte) {
cmdseq := binary.BigEndian.Uint16(buf[8:])
c.handlers[cmdseq](buf[0:16], buf[16:])
delete(c.handlers, cmdseq)
}
func (c *device) discover(cb func()) {
c.cb = cb
log.Println("Trying to connect to IP: ", c.dest)
p := cpnp_packet(CPNP_MSG_DISCOVER, []byte{})
c.udps.SetReadDeadline(time.Now().Add(time.Second * 2))
c.send(p, c.discover_reply)
}
func (c *device) discover_reply(head []byte, body []byte) {
if body[4] == 6 {
mac := ""
for i := 0; i < 6; i++ {
mac += fmt.Sprintf(":%02x", body[6+i])
}
mac = mac[1:]
log.Println("Found printer with MAC address", mac)
} /* else meh? */
if c.mac != nil && bytes.Compare(c.mac, body[6:12]) != 0 {
log.Println("Not the MAC address we're looking for, " +
"waiting for more responses")
return
}
var ip net.IP = body[6+body[4] : 6+body[4]+body[5]]
log.Println("Switching to IP address", ip.String())
c.dest.IP = ip
p := cpnp_packet(CPNP_MSG_ID, []byte{0, 0, 0, 0})
c.send(p, c.id_reply)
}
func (c *device) id_reply(head []byte, body []byte) {
props := string(body[2:])
for _, bit := range strings.Split(props, ";") {
if len(bit) > 0 {
kv := strings.Split(bit, ":")
c.props[kv[0]] = kv[1]
}
}
p := cpnp_packet(CPNP_MSG_STATUS, []byte{})
c.send(p, c.status_reply)
}
func (c *device) status_reply(head []byte, body []byte) {
/* Might tell us stuff like "go away I'm busy!"? */
c.cb()
}
func (c *device) start_job(job *imgreader) {
c.job = job
u, _ := user.Current()
_, fn := filepath.Split(job.fn)
b := make([]byte, 0x188)
utf16_write(b[0x008:0x048], "selphy.go")
utf16_write(b[0x048:0x088], u.Username)
utf16_write(b[0x088:0x188], fn)
p := cpnp_packet(CPNP_MSG_STARTTCP, b)
c.send(p, c.start_tcp)
}
func (c *device) start_tcp(head []byte, body []byte) {
c.jobseq = binary.BigEndian.Uint16(head[10:])
port := binary.BigEndian.Uint16(body[4:])
if port == 0 {
/* TODO: Throw a big fat error. */
log.Fatalln("Help! No TCP port to connect to..")
}
log.Print("Should connect to TCP %s:%d ... ", c.dest.IP, port)
c.tcpd = new(net.TCPAddr)
c.tcpd.IP = c.dest.IP
c.tcpd.Port = int(port)
var e error
c.tcps, e = net.DialTCP("tcp", nil, c.tcpd)
checkError(e)
log.Println("Done")
c.tcpbuf = make([]byte, 0, 51200)
c.print_poll()
}
func (c *device) print_poll() {
p := cpnp_packet(CPNP_MSG_STATUS, []byte{})
c.send(p, c.print_data_request)
}
func (c *device) send_flags() {
b := make([]byte, 0x40)
binary.LittleEndian.PutUint32(b[0x04:], uint32(len(b)))
binary.LittleEndian.PutUint32(b[0x0c:], 1) // ?
if c.job.border {
binary.LittleEndian.PutUint32(b[0x12:], 3)
} else {
binary.LittleEndian.PutUint32(b[0x12:], 2)
}
p := cpnp_packet(CPNP_MSG_DATA, b)
c.send(p, c.send_flags_cb)
}
func (c *device) send_flags_cb(head []byte, body []byte) {
c.print_poll()
}
func (c *device) send_chunk() {
len := len(c.chunk)
if len > 4096 {
len = 4096
}
p := cpnp_packet(CPNP_MSG_DATA, c.chunk[0:len])
c.send(p, c.send_chunk_cb)
c.chunk = c.chunk[len:]
}
func (c *device) send_chunk_cb(head []byte, body []byte) {
if len(c.chunk) > 0 {
c.send_chunk()
} else {
c.print_poll()
}
}
func (c *device) job_done(head []byte, body []byte) {
c.tcps.Close()
c.tcpd = nil
c.tcps = nil
c.cb()
}
func (c *device) print_data_request(head []byte, body []byte) {
/* It frequently seems to repeat the last status response. */
if bytes.Compare(c.last_status, body) == 0 {
c.print_poll()
return
}
c.last_status = body
state := int(body[0x12])
switch state {
case 0x00:
/* Wait */
if c.last_wait == 0 {
c.last_wait = 50
} else {
c.last_wait = c.last_wait * 2
if c.last_wait > 2000 {
c.last_wait = 2000
}
}
time.Sleep(time.Duration(c.last_wait) * time.Millisecond)
c.print_poll()
case 0x01:
/* Job flags */
c.last_wait = 0
log.Println("Sending flags")
c.send_flags()
case 0x02:
/* File data request */
c.last_wait = 0
offset := binary.LittleEndian.Uint32(body[0x18:])
length := binary.LittleEndian.Uint32(body[0x1c:])
log.Println("Will send", length, "bytes starting from", offset)
/* Save the whole chunk and have it sent in 4KB steps. */
c.chunk = c.job.get_chunk(offset, length)
c.send_chunk()
case 0x03:
/* DONE! */
log.Println("Job done, closing connection.")
b := make([]byte, 0x40)
binary.LittleEndian.PutUint32(b[0x04:], uint32(len(b)))
b[2] = 0x03 // Echo status code? No clue..
p := cpnp_packet(CPNP_MSG_DATA, b)
c.send(p, c.job_done)
}
}
func utf16_write(buf []byte, val string) {
enc := utf16.Encode([]rune(val))
for i, c := range enc {
binary.BigEndian.PutUint16(buf[2*i:], uint16(c))
}
}
type imgreader struct {
fn string
fp *os.File
w, h int
fsize int64
border bool
}
func new_imgreader(fn string) *imgreader {
r := new(imgreader)
r.fn = fn
r.fp, _ = os.Open(r.fn)
fi, _ := r.fp.Stat()
r.fsize = fi.Size()
cfg, f, e := image.DecodeConfig(r.fp)
checkError(e)
log.Printf("File %s, %s file, %d bytes, %d×%d\n", fn, f, r.fsize, cfg.Width, cfg.Height)
r.w = cfg.Width
r.h = cfg.Height
return r
}
func (r *imgreader) file_header(offset uint32, length uint32) []byte {
buf := make([]byte, 0x68)
buf[0x02] = 1
binary.LittleEndian.PutUint32(buf[0x04:], length+uint32(len(buf)))
buf[0x0c] = 1
binary.LittleEndian.PutUint32(buf[0x14:], uint32(r.fsize))
binary.LittleEndian.PutUint32(buf[0x18:], uint32(r.w))
binary.LittleEndian.PutUint32(buf[0x1c:], uint32(r.h))
binary.LittleEndian.PutUint32(buf[0x60:], offset)
binary.LittleEndian.PutUint32(buf[0x64:], length)
return buf
}
func (r *imgreader) get_chunk(offset uint32, length uint32) []byte {
head := r.file_header(offset, length)
buf := make([]byte, len(head)+int(length))
copy(buf[0:], head)
r.fp.Seek(int64(offset), 0)
_, err := r.fp.Read(buf[len(head):])
checkError(err)
/* Read might have read less than the number of bytes requested, but
that's okay, IIRC the original implementation has 0/junk-padded
chunk as well after EOF.
Note that the printer does parse the JPEG as it comes in, and
will stop asking for more chunks when it doesn't need more
(skipping thumbnail info at the end or whatever it was?). */
return buf
}
type printer struct {
dev *device
jobs []*imgreader
}
func new_printer() *printer {
p := new(printer)
p.jobs = make([]*imgreader, 0, 10)
return p
}
func (p *printer) add_job(job *imgreader) {
p.jobs = append(p.jobs, job)
}
func (p *printer) start() {
p.dev.discover(p.start_job)
p.dev.wait()
}
func (p *printer) start_job() {
log.Println("It's a", p.dev.props["DES"])
if len(p.jobs) == 0 {
log.Println("Ran out of stuff to do, exiting")
os.Exit(0)
}
job := p.jobs[0]
log.Println("Will send", job.fn)
p.dev.start_job(job)
p.jobs = p.jobs[1:]
}
func main() {
printer_mac := flag.String("printer_mac", "", "MAC address of printer")
printer_ip := flag.String("printer_ip", CPNP_ADDR, "IP addres of printer")
border := flag.Bool("border", false, "Allow white borders, don't crop")
flag.Parse()
p := new_printer()
for _, fn := range flag.Args() {
job := new_imgreader(fn)
job.border = *border
p.add_job(job)
}
p.dev = new_device(printer_mac, printer_ip)
p.start()
os.Exit(0)
}
func checkError(err error) {
if err != nil {
log.Panicln("Fatal error ", err.Error())
os.Exit(1)
}
}