forked from agalakhov/captdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prn_lbp2900.c
359 lines (308 loc) · 10.2 KB
/
prn_lbp2900.c
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
/*
* Copyright (C) 2013 Alexey Galakhov <agalakhov@gmail.com>
* Copyright (C) 2016 Alexei Gordeev <KP1533TM2@gmail.com>
*
* Licensed under the GNU General Public License Version 3
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "std.h"
#include "word.h"
#include "capt-command.h"
#include "capt-status.h"
#include "generic-ops.h"
#include "hiscoa-common.h"
#include "hiscoa-compress.h"
#include "paper.h"
#include "printer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
uint16_t job;
struct lbp2900_ops_s {
struct printer_ops_s ops;
const struct capt_status_s * (*get_status) (void);
void (*wait_ready) (void);
};
static const struct capt_status_s *lbp2900_get_status(const struct printer_ops_s *ops)
{
const struct lbp2900_ops_s *lops = container_of(ops, struct lbp2900_ops_s, ops);
return lops->get_status();
}
static void lbp2900_wait_ready(const struct printer_ops_s *ops)
{
const struct lbp2900_ops_s *lops = container_of(ops, struct lbp2900_ops_s, ops);
lops->wait_ready();
}
static void send_job_start(uint8_t fg, uint16_t page)
{
uint8_t ml = 0x00; /* host name lenght */
uint8_t ul = 0x00; /* user name lenght */
uint8_t nl = 0x00; /* document name lenght */
time_t rawtime = time(NULL);
const struct tm *tm = localtime(&rawtime);
uint8_t buf[32 + 40 + ml + ul + nl];
uint8_t head[32] = {
0x00, 0x00, 0x00, 0x00, LO(page), HI(page), 0x00, 0x00,
ml, 0x00, ul, 0x00, nl, 0x00, 0x00, 0x00,
fg, 0x01, LO(job), HI(job),
/*-60 */ 0xC4, 0xFF,
/*-120*/ 0x88, 0xFF,
LO(tm->tm_year), HI(tm->tm_year), (uint8_t) tm->tm_mon, (uint8_t) tm->tm_mday,
(uint8_t) tm->tm_hour, (uint8_t) tm->tm_min, (uint8_t) tm->tm_sec,
0x01,
};
memcpy(buf, head, sizeof(head));
memset(buf + 32, 0, 40 + ml + ul + nl);
capt_sendrecv(CAPT_JOB_SETUP, buf, sizeof(buf), NULL, 0);
}
static const uint8_t magicbuf_0[] = {
0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t magicbuf_2[] = {
0xEE, 0xDB, 0xEA, 0xAD, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t blinkonbuf[] = {
/* led */ 0x31, 0x00, 0x00, /* S6 */ 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* S7 */ 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
static const uint8_t blinkoffbuf[] = {
/* led */ 0x13, 0x00, 0x00, /* S6 */ 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* S7 */ 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
static void lbp2900_job_prologue(struct printer_state_s *state)
{
(void) state;
uint8_t buf[8];
size_t size;
capt_sendrecv(CAPT_IDENT, NULL, 0, NULL, 0);
sleep(1);
capt_init_status();
lbp2900_get_status(state->ops);
capt_sendrecv(CAPT_START_0, NULL, 0, NULL, 0);
capt_sendrecv(CAPT_JOB_BEGIN, magicbuf_0, ARRAY_SIZE(magicbuf_0), buf, &size);
job=WORD(buf[2], buf[3]);
capt_sendrecv(CAPT_GPIO, blinkoffbuf, ARRAY_SIZE(blinkoffbuf), NULL, 0);
lbp2900_wait_ready(state->ops);
send_job_start(1, 0);
lbp2900_wait_ready(state->ops);
}
static void lbp3000_job_prologue(struct printer_state_s *state)
{
(void) state;
capt_sendrecv(CAPT_IDENT, NULL, 0, NULL, 0);
sleep(1);
capt_init_status();
lbp2900_get_status(state->ops);
capt_sendrecv(CAPT_START_0, NULL, 0, NULL, 0);
capt_sendrecv(CAPT_JOB_BEGIN, magicbuf_0, ARRAY_SIZE(magicbuf_0), NULL, 0);
/* LBP-3000 prints the very first printjob perfectly
* and then proceeds to hang at this (commented out)
* spot. That's the difference, or so it seems. */
/* lbp2900_wait_ready(state->ops); */
send_job_start(1, 0);
/* There's also that command, that apparently does something, and does something,
* but it's there in the Wireshark logs. Response data == command data. */
uint8_t dummy[2] = {0, 0};
capt_sendrecv(0xE0A6, dummy, sizeof(dummy), NULL, 0);
lbp2900_wait_ready(state->ops);
}
static bool lbp2900_page_prologue(struct printer_state_s *state, const struct page_dims_s *dims)
{
const struct capt_status_s *status;
size_t s;
uint8_t buf[16];
uint8_t pt1 = 0x01;
uint8_t save = 0x00;
uint8_t pt2 = 0x01;
uint8_t pageparms[] = {
0x00, 0x00, 0x30, 0x2A, /* sz */ 0x02, 0x00, 0x00, 0x00,
0x1C, 0x1C, 0x1C, 0x1C, pt1, /* adapt */ 0x11, 0x04, 0x00,
0x01, 0x01, /* img ref */ 0x00, save, 0x00, 0x00,
/* height margin 118 */ 0x76, 0x00,
/* width margin 78 */ 0x4e, 0x00,
LO(dims->line_size), HI(dims->line_size), LO(dims->num_lines), HI(dims->num_lines),
LO(dims->paper_width), HI(dims->paper_width),
LO(dims->paper_height), HI(dims->paper_height),
/* 34 bytes */
0x00, 0x00, pt2, 0x00, 0x00, 0x00,
/* 72 bytes */
/*
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
*/
};
(void) state;
status = lbp2900_get_status(state->ops);
if (FLAG(status, CAPT_FL_UNINIT1) || FLAG(status, CAPT_FL_UNINIT2)) {
capt_sendrecv(CAPT_START_1, NULL, 0, NULL, 0);
capt_sendrecv(CAPT_START_2, NULL, 0, NULL, 0);
capt_sendrecv(CAPT_START_3, NULL, 0, NULL, 0);
/* FIXME: wait for printer is free (could it be potentially dangerous or really mandatory?) */
while ( ! FLAG(lbp2900_get_status(state->ops), ((4 << 16) | (1 << 0)) ) )
sleep(1);
lbp2900_get_status(state->ops);
lbp2900_wait_ready(state->ops);
capt_sendrecv(CAPT_UPLOAD_2, magicbuf_2, ARRAY_SIZE(magicbuf_2), NULL, 0);
lbp2900_wait_ready(state->ops);
}
while (1) {
if (! FLAG(lbp2900_get_status(state->ops), CAPT_FL_BUFFERFULL))
break;
sleep(1);
}
capt_multi_begin(CAPT_SET_PARMS);
capt_multi_add(CAPT_SET_PARM_PAGE, pageparms, sizeof(pageparms));
s = hiscoa_format_params(buf, sizeof(buf), &hiscoa_default_params);
capt_multi_add(CAPT_SET_PARM_HISCOA, buf, s);
capt_multi_add(CAPT_SET_PARM_1, NULL, 0);
capt_multi_add(CAPT_SET_PARM_2, NULL, 0);
capt_multi_send();
return true;
}
static bool lbp2900_page_epilogue(struct printer_state_s *state, const struct page_dims_s *dims)
{
(void) dims;
const struct capt_status_s *status;
capt_send(CAPT_PRINT_DATA_END, NULL, 0);
/* waiting until the page is received */
while (1) {
sleep(1);
status = lbp2900_get_status(state->ops);
if (status->page_received == status->page_decoding)
break;
}
send_job_start(2, status->page_decoding);
lbp2900_wait_ready(state->ops);
uint8_t buf[2] = { LO(status->page_decoding), HI(status->page_decoding) };
capt_sendrecv(CAPT_FIRE, buf, 2, NULL, 0);
lbp2900_wait_ready(state->ops);
send_job_start(6, status->page_decoding);
while (1) {
const struct capt_status_s *status = lbp2900_get_status(state->ops);
/* Interesting. Using page_printing here results in shifted print */
if (status->page_out == status->page_decoding)
return true;
if (FLAG(status, CAPT_FL_NOPAPER2) || FLAG(status, CAPT_FL_NOPAPER1)) {
fprintf(stderr, "DEBUG: CAPT: no paper\n");
if (FLAG(status, CAPT_FL_PRINTING) || FLAG(status, CAPT_FL_PROCESSING1))
continue;
return false;
}
sleep(1);
}
}
static void lbp2900_job_epilogue(struct printer_state_s *state)
{
uint8_t jbuf[2] = { LO(job), HI(job) };
while (1) {
const struct capt_status_s *status = lbp2900_get_status(state->ops);
if (status->page_completed == status->page_decoding)
break;
sleep(1);
}
capt_sendrecv(CAPT_JOB_END, jbuf, 2, NULL, 0);
}
static void lbp2900_page_setup(struct printer_state_s *state,
struct page_dims_s *dims,
unsigned width, unsigned height)
{
/*
A4 4736x6778 or 4736x4520
Letter 4864x6368
Legal 4864x8192
Executive 4128x6080
3x5 1344x2528
*/
(void) state;
(void) width;
dims->band_size = 70;
dims->line_size = 4736 / 8;
if (height > 6778)
dims->num_lines = 6778;
else
dims->num_lines = height;
}
static void lbp2900_wait_user(struct printer_state_s *state)
{
(void) state;
capt_sendrecv(CAPT_GPIO, blinkonbuf, ARRAY_SIZE(blinkonbuf), NULL, 0);
lbp2900_wait_ready(state->ops);
while (1) {
const struct capt_status_s *status = lbp2900_get_status(state->ops);
if (FLAG(status, CAPT_FL_BUTTON)) {
fprintf(stderr, "DEBUG: CAPT: button pressed\n");
// break;
}
if (FLAG(status, CAPT_FL_nERROR)) {
fprintf(stderr, "DEBUG: CAPT: virtual button pressed\n");
break;
}
sleep(1);
}
capt_sendrecv(CAPT_GPIO, blinkoffbuf, ARRAY_SIZE(blinkoffbuf), NULL, 0);
lbp2900_wait_ready(state->ops);
}
static struct lbp2900_ops_s lbp2900_ops = {
.ops = {
.job_prologue = lbp2900_job_prologue,
.job_epilogue = lbp2900_job_epilogue,
.page_setup = lbp2900_page_setup,
.page_prologue = lbp2900_page_prologue,
.page_epilogue = lbp2900_page_epilogue,
.compress_band = ops_compress_band_hiscoa,
.send_band = ops_send_band_hiscoa,
.wait_user = lbp2900_wait_user,
},
.get_status = capt_get_xstatus,
.wait_ready = capt_wait_ready,
};
static struct lbp2900_ops_s lbp3000_ops = {
.ops = {
.job_prologue = lbp3000_job_prologue, /* different job prologue */
.job_epilogue = lbp2900_job_epilogue,
.page_setup = lbp2900_page_setup,
.page_prologue = lbp2900_page_prologue,
.page_epilogue = lbp2900_page_epilogue,
.compress_band = ops_compress_band_hiscoa,
.send_band = ops_send_band_hiscoa,
.wait_user = lbp2900_wait_user,
},
.get_status = capt_get_xstatus,
.wait_ready = capt_wait_ready,
};
register_printer("LBP2900", lbp2900_ops.ops, WORKS);
register_printer("LBP3000", lbp3000_ops.ops, EXPERIMENTAL);
static struct lbp2900_ops_s lbp3010_ops = {
.ops = {
.job_prologue = lbp2900_job_prologue,
.job_epilogue = lbp2900_job_epilogue,
.page_setup = lbp2900_page_setup,
.page_prologue = lbp2900_page_prologue,
.page_epilogue = lbp2900_page_epilogue,
.compress_band = ops_compress_band_hiscoa,
.send_band = ops_send_band_hiscoa,
.wait_user = lbp2900_wait_user,
},
.get_status = capt_get_xstatus_only,
.wait_ready = capt_wait_xready_only,
};
register_printer("LBP3010/LBP3018/LBP3050", lbp3010_ops.ops, EXPERIMENTAL);