-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_usblp.c
367 lines (317 loc) · 11.1 KB
/
lib_usblp.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
360
361
362
363
364
365
366
367
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/**
* @file lib_usblp.c
* @author charles-park ([email protected])
* @brief usb label printer test app.
* @version 0.1
* @date 2024-11-07
*
* @copyright Copyright (c) 2022
*
*/
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <getopt.h>
#include "lib_usblp.h"
//------------------------------------------------------------------------------
// printer library path & filename
//------------------------------------------------------------------------------
const char *F_LPR = "/usr/bin/lpr";
const char *F_LPSTAT = "/usr/bin/lpstat";
const char *F_LPADMIN = "/usr/sbin/lpadmin";
const char *F_LPINFO = "/usr/sbin/lpinfo";
//------------------------------------------------------------------------------
#define TEXT_WIDTH 80
const char *EPL_FORM_START = "I8,0,001\nQ78,16\nq240\nrN\nS4\nD15\nZB\nJF\nO\nR304,10\nf100\nN\n";
/*
"< forum.odroid.com" or "forum.odroid.com >"
00:1E:06:xx:xx:xx
*/
const char *EPL_FORM_MAC_L = "A10,0,0,2,1,1,N,\"< forum.odroid.com\"\nA16,32,0,2,1,1,N,\"%s\"\n";
const char *EPL_FORM_MAC_R = "A10,0,0,2,1,1,N,\"forum.odroid.com >\"\nA16,32,0,2,1,1,N,\"%s\"\n";
/* %d = 20, 40, 60, (11 * i) * 2 */
const char *EPL_FORM_ERR = "A4,%d,0,2,1,1,N,\"%s\"\n";
const char *EPL_FORM_ERR_1 = "A4,00,0,2,1,1,N,\"%s\"\n";
const char *EPL_FORM_ERR_2 = "A4,22,0,2,1,1,N,\"%s\"\n";
const char *EPL_FORM_ERR_3 = "A4,44,0,2,1,1,N,\"%s\"\n";
/* Page end */
const char *EPL_FORM_END = "P1\n";
const char USBLP_EPL_FORM[][TEXT_WIDTH] = {
"I8,0,001\n",
"Q78,16\n",
"q240\n",
"rN\n",
"S4\n",
"D15\n",
"ZB\n",
"JF\n",
"O\n",
"R304,10\n",
"f100\n",
"N\n",
"A10,0,0,2,1,1,N,\"EPL Printer Test\"\n",
"A16,32,0,2,1,1,N,\"00:1E:06:xx:xx:xx\"\n",
"P1\n"
};
//------------------------------------------------------------------------------
const char *ZPL_FORM_START = "^XA\n^CFC\n^LH0,0\n";
/*
"< forum.odroid.com" or "forum.odroid.com >"
00:1E:06:xx:xx:xx
*/
const char *ZPL_FORM_MAC_L = "^FO310,25\n^FD< forum.odroid.com^FS\n^FO316,55\n^FD%s^FS\n";
const char *ZPL_FORM_MAC_R = "^FO310,25\n^FDforum.odroid.com >^FS\n^FO316,55\n^FD%s^FS\n";
/* %d = 20, 40, 60, (i * 2) * 10 + 20 */
const char *ZPL_FORM_ERR = "^FO304,%d\n^FD%s^FS\n";
const char *ZPL_FORM_ERR_1 = "^FO304,20\n^FD%c%s^FS\n";
const char *ZPL_FORM_ERR_2 = "^FO304,40\n^FD%c%s^FS\n";
const char *ZPL_FORM_ERR_3 = "^FO304,60\n^FD%c%s^FS\n";
/* Page end */
const char *ZPL_FORM_END = "^XZ\n";
const char USBLP_ZPL_FORM[][TEXT_WIDTH] = {
"I8,0,001\n",
"Q78,16\n",
"q240\n",
"rN\n",
"S4\n",
"D15\n",
"ZB\n",
"JF\n",
"O\n",
"R304,10\n",
"f100\n",
"N\n",
"A10,0,0,2,1,1,N,\"ZPL Printer Test\"\n",
"A16,32,0,2,1,1,N,\"00:1E:06:xx:xx:xx\"\n",
"P1\n"
};
#if 0
const int8_t USBLP_ZPL_INIT[TEXT_WIDTH] = {
"^XA^JUF^XZ\n"
};
const int8_t USBLP_ZPL_FORM[][TEXT_WIDTH] = {
"^XA\n",
"^CFC\n",
"^LH0,0\n",
"^FO310,25^FDZPL Printer Test^FS\n",
"^FO316,55^FD00:1E:06:xx:xx:xx^FS\n",
"^XZ\n"
};
#endif
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// check the usb label printer connection.
//------------------------------------------------------------------------------
static int check_usblp_connection (void)
{
FILE *fp;
char cmd_line[1024];
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "%s", "lsusb | grep Zebra 2<&1");
if ((fp = popen(cmd_line, "r")) != NULL) {
memset (cmd_line, 0x00, sizeof(cmd_line));
while (fgets (cmd_line, sizeof(cmd_line), fp) != NULL) {
if (strstr (cmd_line, "Zebra") != NULL) {
pclose (fp);
return 1;
}
memset (cmd_line, 0x00, sizeof(cmd_line));
}
pclose(fp);
}
return 0;
}
//------------------------------------------------------------------------------
// get the usb label printer info.
//------------------------------------------------------------------------------
static int get_usblp_device (char *lpname)
{
FILE *fp;
char cmd_line[1024], *ptr;
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "%s", "lpinfo -v | grep usb 2<&1");
if ((fp = popen(cmd_line, "r")) != NULL) {
memset (cmd_line, 0x00, sizeof(cmd_line));
while (fgets (cmd_line, sizeof(cmd_line), fp) != NULL) {
if ((ptr = strstr (cmd_line, "usb:")) != NULL) {
strncpy (lpname, ptr, strlen(ptr));
pclose (fp);
return 1;
}
memset (cmd_line, 0x00, sizeof(cmd_line));
}
pclose(fp);
}
return 0;
}
//------------------------------------------------------------------------------
// clr the usb label printer info.
//------------------------------------------------------------------------------
static int clr_usblp_device (void)
{
FILE *fp = NULL;
char cmd_line[1024];
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "lpadmin -x %s 2<&1", "zebra");
if ((fp = popen(cmd_line, "w")) != NULL) {
pclose(fp);
return 1;
}
return 0;
}
//------------------------------------------------------------------------------
// set the usb label printer info.
//------------------------------------------------------------------------------
static int set_usblp_device (char *lpname)
{
FILE *fp;
char cmd_line[1024], s_lpname[512];
memset (s_lpname, 0x00, sizeof(s_lpname));
{
int i, pos;
for (i = 0, pos = 0; i < (int)strlen(lpname); i++, pos++) {
if ((lpname[i] == '(') || (lpname[i] == ')'))
s_lpname [pos++] = '\\';
s_lpname [pos] = lpname[i];
}
}
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "lpadmin -p zebra -E -v %s 2<&1", s_lpname);
if ((fp = popen(cmd_line, "w")) != NULL) {
pclose(fp);
return 1;
}
return 0;
}
//------------------------------------------------------------------------------
// confirm the usb label printer info.
//------------------------------------------------------------------------------
static int confirm_usblp_device (char *lpname)
{
FILE *fp;
char cmd_line[1024], *ptr;
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "%s", "lpstat -v | grep usb 2<&1");
if ((fp = popen(cmd_line, "r")) != NULL) {
memset (cmd_line, 0x00, sizeof(cmd_line));
while (fgets (cmd_line, sizeof(cmd_line), fp) != NULL) {
if ((ptr = strstr (cmd_line, "usb:")) != NULL) {
if (!strncmp (lpname, ptr, strlen(ptr)-1)) {
pclose (fp);
return 1;
}
}
memset (cmd_line, 0x00, sizeof(cmd_line));
}
pclose(fp);
}
return 0;
}
//------------------------------------------------------------------------------
int usblp_print_mac (char *msg, int ch)
{
FILE *fp = fopen ("usblp_mac.txt", "w");
char cmd_line[1024];
if (fp == NULL) {
fprintf (stdout, "%s : couuld not create file for usblp test. ", __func__);
return 0;
}
fputs (ZPL_FORM_START, fp);
memset (cmd_line, 0, sizeof(cmd_line));
if (ch) sprintf(cmd_line, ZPL_FORM_MAC_R, msg);
else sprintf(cmd_line, ZPL_FORM_MAC_L, msg);
fputs (cmd_line, fp);
fputs (ZPL_FORM_END, fp);
fclose (fp);
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "%s", "lpr usblp_mac.txt -P zebra 2<&1");
if ((fp = popen(cmd_line, "w")) != NULL) {
pclose(fp);
}
return 1;
}
//------------------------------------------------------------------------------
int usblp_print_err (const char *msg1, const char *msg2, const char *msg3, int ch)
{
FILE *fp = fopen ("usblp_err.txt", "w");
char cmd_line[1024];
if (fp == NULL) {
fprintf (stdout, "%s : couuld not create file for usblp test. ", __func__);
return 0;
}
fputs (ZPL_FORM_START, fp);
memset (cmd_line, 0, sizeof(cmd_line));
sprintf(cmd_line, ZPL_FORM_ERR_1, ch ? '>' : '<', msg1);
fputs (cmd_line, fp);
sprintf(cmd_line, ZPL_FORM_ERR_2, ch ? '>' : '<', msg2);
fputs (cmd_line, fp);
sprintf(cmd_line, ZPL_FORM_ERR_3, ch ? '>' : '<', msg3);
fputs (cmd_line, fp);
fputs (ZPL_FORM_END, fp);
fclose (fp);
memset (cmd_line, 0x00, sizeof(cmd_line));
sprintf (cmd_line, "%s", "lpr usblp_err.txt -P zebra 2<&1");
if ((fp = popen(cmd_line, "w")) != NULL) {
pclose(fp);
}
return 1;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int get_usblp_ptype (void); /* epl, zpl */
//------------------------------------------------------------------------------
int usblp_config (void)
{
char usblp_device[512];
/*
ubuntu printer library(lpr, lpadmin, lpstat) check.
*/
if ( access (F_LPR, F_OK) || access (F_LPSTAT, F_OK) ||
access (F_LPADMIN, F_OK) || access (F_LPINFO, F_OK) ) {
fprintf (stderr, "Error : printer control file not found(lpr, lpstat, lpadmin, lpinfo)\n");
fprintf (stderr, "Need package : cups cups-bsd\n");
return 0;
}
memset (usblp_device, 0x00, sizeof(usblp_device));
if ( !check_usblp_connection () ) {
fprintf (stderr, "Error : Zebra USB Label Printer not found\n");
return 0;
}
if ( !get_usblp_device (usblp_device) ) {
fprintf (stderr, "Error : Unable to get usblp infomation.\n");
return 0;
}
/* Clears the current configuration if it is already configured for nlp. */
if ( confirm_usblp_device (usblp_device) )
fprintf (stderr, "usblp device removed. ret = %s\n", clr_usblp_device() ? "success" : "fail");
if ( !set_usblp_device (usblp_device) ) {
fprintf (stderr, "Error : Failed to configure usblp.\n");
return 0;
}
if ( !confirm_usblp_device (usblp_device) ) {
fprintf (stderr, "Error : The usblp settings have not been changed.\n");
return 0;
}
fprintf (stdout, "*** USB Label Printer setup is complete. ***\n");
fprintf (stdout, "*** Printer Device Name : %s\n", usblp_device);
// fprintf (stdout, "*** Printer Protocol Type : %s\n", get_usblp_ptype() ? "ZPL(ZD230D)" : "EPL(GC420D)");
return 1;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------