File: epson.c

package info (click to toggle)
libinklevel 0.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,840 kB
  • sloc: sh: 4,488; ansic: 4,422; python: 44; makefile: 26
file content (829 lines) | stat: -rw-r--r-- 18,338 bytes parent folder | download | duplicates (5)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* epson.c
 *
 * (c) 2006, 2014, 2015, 2016 Markus Heinz
 *
 * Code taken from escputil (gutenprint 5.0.0).
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdarg.h>

#include "inklevel.h"
#include "devices.h"
#include "epson.h"
#include "d4lib.h"
#include "printer-io.h"

static int do_status_command_internal(void);
static int initialize_printer();
static void exit_packet_mode_old(int do_init);
static int open_raw_device(void);
static int init_packet(printer_descriptor *desc, int force);
static void do_remote_cmd(const char *cmd, int nargs, ...);
static void add_resets(int count);
static void do_new_status(char *buf, int bytes);
static void do_old_status(const char *buf);
static void print_old_ink_levels(const char *ind);
static void start_remote_sequence(void);
static void end_remote_sequence(void);
static const char *looking_at_command(const char *buf, const char *cmd);
static const char *find_group(const char *buf);
static int get_digit(char digit);

extern int open_printer_device(const int port, const char* device_file, 
                               const int portnumber);

static char printer_cmd[1025];
static int bufpos = 0;
static int isnew = 0;
static volatile int alarm_interrupt;
static int send_size = 0x0200;
static int receive_size = 0x0200;
static int socket_id = -1;
static char *printer_model = NULL;
static int my_port;
static char my_device[256];
static int my_portnumber;
static struct ink_level *my_level;
static printer_descriptor my_printer_descriptor;

int get_ink_level_epson(const int port, const char *device_file, 
			const int portnumber, struct ink_level *level) {
  int result;

  my_port = port;
  strncpy(my_device, device_file, 255);
  my_device[255] = '\0';
  my_portnumber = portnumber;
  my_level = level;

  result = initialize_printer();
  if (result == OK) {
    result = do_status_command_internal();
    level = my_level;
  }

  return result;
}

static void alarm_handler(int sig) {
  alarm_interrupt = 1;
}

static void exit_packet_mode_old(int do_init) {
  static char hdr[] = "\000\000\000\033\001@EJL 1284.4\n@EJL     \n\033@";
  memcpy(printer_cmd + bufpos, hdr, sizeof(hdr) - 1); /* DON'T include null! */

#ifdef DEBUG
  printf("Exit packet mode (%d)\n", do_init);
#endif

  bufpos += sizeof(hdr) - 1;
  if (!do_init) {
    bufpos -= 2;
  }
}

static void initialize_print_cmd(int do_init) {
  bufpos = 0;

#ifdef DEBUG
  printf("Initialize print command\n");
#endif

  if (isnew) {
    exit_packet_mode_old(do_init);
  }
}

static int do_status_command_internal() {
  int result;
  int status;
  int credit;
  int retry = 4;
  char buf[1024];

#ifdef DEBUG
  printf("ink levels...\n");
#endif

  
  result = open_raw_device();
  if (result != OK) {
    return ERROR;
  }

  if (isnew) {
    credit = askForCredit(&my_printer_descriptor, socket_id, &send_size, 
			  &receive_size);
    if (credit < 0) {

#ifdef DEBUG
      printf("\nCannot get credit\n");
#endif

      return COULD_NOT_GET_CREDIT;
    }

    /* request status command */
    status = writeData(&my_printer_descriptor, socket_id, 
		       (const unsigned char*)"st\1\0\1", 5, 1);
    if (status <= 0) {

#ifdef DEBUG
      printf("\nCannot write to printer: %s\n", strerror(errno));
#endif
      return COULD_NOT_WRITE_TO_PRINTER;
    }

    do {
      status = readData(&my_printer_descriptor, socket_id, 
			(unsigned char*) buf, 1023);
      if (status < 0) {
        return COULD_NOT_READ_FROM_PRINTER;
      }

#ifdef DEBUG
      printf("readData try %d status %d\n", retry, status);
#endif

    } while ((retry-- != 0) && strncmp("st", buf, 2) && 
             strncmp("@BDC ST", buf, 7));

    /* "@BCD ST ST"  found */

    if (!retry)	{
      return COULD_NOT_PARSE_RESPONSE_FROM_PRINTER;
    }

    buf[status] = '\0';

    if (buf[7] == '2') {
      do_new_status(buf + 12, status - 12);
    } else {
      do_old_status(buf + 9);
    }
    
    CloseChannel(&my_printer_descriptor, socket_id);
  } else {
    do {
      add_resets(2);
      initialize_print_cmd(1);
      do_remote_cmd("ST", 2, 0, 1);
      add_resets(2);
      if (SafeWrite(&my_printer_descriptor, printer_cmd, bufpos) < bufpos) {

#ifdef DEBUG
        printf("Cannot write to printer: %s\n", strerror(errno));
#endif

        return COULD_NOT_WRITE_TO_PRINTER;
      }
      
      read_wrapper(&my_printer_descriptor, buf, 1024, &status, 1);

      if (status < 0) {
        return COULD_NOT_READ_FROM_PRINTER;
      }
    } while (--retry != 0 && !status);
    
    buf[status] = '\0';
    
    if (status > 9) {
      do_old_status(buf + 9);
    } else {
      return NO_INK_LEVEL_FOUND;
    }
  }

  close_wrapper(&my_printer_descriptor);

  return OK;
}

static int initialize_printer() {
  int packet_initialized = 0;
  int credit;
  int retry = 4;
  int tries = 0;
  int status;
  int forced_packet_mode = 0;
  char* pos;
  char* spos;
  unsigned char buf[1024];
  char init_str[] = "\033\1@EJL ID\r\n";
  int result;

#ifdef DEBUG
  int found = 0;
#endif

  result = open_raw_device();
  if (result != OK) {
    return ERROR;
  }

  do {
    alarm_interrupt = 0;
    signal(SIGALRM, alarm_handler);
    alarm(5);
    status = SafeWrite(&my_printer_descriptor, init_str, sizeof(init_str) - 1);
    alarm(0);
    signal(SIGALRM, SIG_DFL);

#ifdef DEBUG
    printf("status %d alarm %d\n", status, alarm_interrupt);
#endif

    if (status != sizeof(init_str) - 1 && (status != -1 || !alarm_interrupt)) {

#ifdef DEBUG
      printf("Cannot write to printer: %s\n", strerror(errno));
#endif
      return COULD_NOT_WRITE_TO_PRINTER;
    }

#ifdef DEBUG
    printf("Try old command %d alarm %d\n", tries, alarm_interrupt);
#endif

    read_wrapper(&my_printer_descriptor, (char *) buf, 1024, &status, 1);

    if (status <= 0 && tries > 0) {
      forced_packet_mode = !init_packet(&my_printer_descriptor, 1);
      status = 1;
    }

    if (!forced_packet_mode && status > 0 && 
        !strstr((char *) buf, "@EJL ID") && tries < 3) {

#ifdef DEBUG
      printf("Found bad data: %s\n", buf);
#endif
      
      /*
       * We know the printer's not dead.  Try to turn off status and try again.
       */
      initialize_print_cmd(1);
      do_remote_cmd("ST", 2, 0, 0);
      add_resets(2);
      SafeWrite(&my_printer_descriptor, printer_cmd, bufpos);
      status = 0;
    }
    
    tries++;
  } while (status <= 0);

  if (forced_packet_mode || ((buf[3] == status) && (buf[6] == 0x7f))) {

#ifdef DEBUG 
    printf("Printer in packet mode....\n");
#endif
  
    packet_initialized = 1;
    isnew = 1;
    
    credit = askForCredit(&my_printer_descriptor, socket_id, &send_size, 
			  &receive_size);
    if (credit < 0) {
      
#ifdef DEBUG
      printf("Cannot get credit\n");
#endif
      
      return COULD_NOT_GET_CREDIT;
    }

    /* request status command */
    status = writeData(&my_printer_descriptor, socket_id, 
		       (const unsigned char*)"di\1\0\1", 5, 1);
    if (status <= 0) {
      
#ifdef DEBUG
      printf("\nCannot write to printer: %s\n", strerror(errno));
#endif

      return COULD_NOT_WRITE_TO_PRINTER;
    }
    
    do {
      status = readData(&my_printer_descriptor, socket_id, (unsigned char*)buf, 
			1023);
      if (status <= -1 ) {
        return COULD_NOT_READ_FROM_PRINTER;
      }

#ifdef DEBUG
      printf("readData try %d status %d\n", retry, status);
#endif
      
    } while ((retry-- != 0) && strncmp("di", (char*)buf, 2) &&
             strncmp("@EJL ID", (char*)buf, 7));

    if (!retry) {
      return COULD_NOT_PARSE_RESPONSE_FROM_PRINTER;
    }
  }

#ifdef DEBUG
  printf("status: %i\n", status);
  printf("Buf: %s\n", buf);
#endif

  if (status > 0) {
    pos = strstr((char*)buf, "@EJL ID");

#ifdef DEBUG
    printf("pos: %s\n", pos);
#endif

    if (pos) {
      pos = strchr(pos, (int) ';');
    }
     
#ifdef DEBUG
    printf("pos: %s\n", pos);
#endif

    if (pos) {
      pos = strchr(pos + 1, (int) ';');
    }

#ifdef DEBUG
    printf("pos: %s\n", pos);
#endif
    
    if (pos) {
      pos = strchr(pos, (int) ':');
    }
     
#ifdef DEBUG
    printf("pos: %s\n", pos);
#endif
    
    if (pos) {
      spos = strchr(pos, (int) ';');
    }
    
    if (!pos) {
      /*
       * Some printers seem to return status, but don't respond
       * usefully to @EJL ID.  The Stylus Pro 7500 seems to be
       * one of these.
       */
      if (status > 0 && strstr((char *) buf, "@BDC ST")) {
        
        /*
         * Set the printer model to something rational so that
         * attempts to describe parameters will succeed.
         * However, make it clear that this is a dummy,
         * so we don't actually try to print it out.
         */
         
#ifdef DEBUG
        printf("Can't find printer name, assuming Stylus Photo\n");
#endif

        printer_model = strdup("escp2-photo");
      } else {
        return ERROR;
      }
    } else {
      if (spos) {
        *spos = '\000';
      }

      printer_model = pos + 1;

#ifdef DEBUG
      printf("printer model: %s\n", printer_model);
#endif
      
    }
  }
  
  if (isnew && !packet_initialized) {
    isnew = !init_packet(&my_printer_descriptor, 0);
  }

  close_wrapper(&my_printer_descriptor);

#ifdef DEBUG
  printf("new? %s found? %s\n", isnew ? "yes" : "no", found ? "yes" : "no");
#endif
  return OK;
}

static int open_raw_device(void) {
  /* Prepare printer descripton */
  my_printer_descriptor.port = my_port;
  my_printer_descriptor.device_file = my_device;
  my_printer_descriptor.portnumber = my_portnumber;

  if (open_wrapper(&my_printer_descriptor) != PRINTER_SUCCESS) {
    return ERROR;
  } else {
    return OK;
  }
}

static int init_packet(printer_descriptor *desc, int force) {
  if (!force) {

#ifdef DEBUG
    printf("Flushing data...\n");
#endif

    flushData(desc, (unsigned char) -1);
  }

#ifdef DEBUG
  printf("EnterIEEE...\n");
#endif

  if (!EnterIEEE(desc)) {
    return ERROR;
  }

#ifdef DEBUG
  printf("Init...\n");
#endif

  if (!Init(desc)) {
    return ERROR;
  }

#ifdef DEBUG
  printf("GetSocket...\n");
#endif

  socket_id = GetSocketID(desc, "EPSON-CTRL");
  if (!socket_id) {
    return ERROR;
  }

#ifdef DEBUG
  printf("OpenChannel...\n");
#endif

  switch (OpenChannel(desc, socket_id, &send_size, &receive_size)) {
  case -1:

#ifdef DEBUG
    printf("Fatal Error return 1\n");
#endif

    return ERROR; /* unrecoverable error */
    break;

  case  0:

#ifdef DEBUG  
    printf("Error\n"); /* recoverable error ? */
#endif

    return ERROR;
    break;
  }

#ifdef DEBUG
  printf("Flushing data...\n");
#endif

  flushData(desc, socket_id);
  return OK;
}

static void do_remote_cmd(const char *cmd, int nargs, ...) {
  int i;
  va_list args;

  va_start(args, nargs);

  start_remote_sequence();
  memcpy(printer_cmd + bufpos, cmd, 2);

#ifdef DEBUG
  printf("Remote command: %s", cmd);
#endif

  bufpos += 2;
  printer_cmd[bufpos] = nargs % 256;
  printer_cmd[bufpos + 1] = (nargs >> 8) % 256;
  

#ifdef DEBUG
  printf(" %02x %02x", (unsigned) printer_cmd[bufpos], 
         (unsigned) printer_cmd[bufpos + 1]);
#endif

  if (nargs > 0) {
    for (i = 0; i < nargs; i++) {
      printer_cmd[bufpos + 2 + i] = va_arg(args, int);

#ifdef DEBUG
      printf(" %02x", (unsigned) printer_cmd[bufpos + 2 + i]);
#endif
    }
  }

#ifdef DEBUG
  printf("\n");
#endif

  bufpos += 2 + nargs;
  end_remote_sequence();
}

static void add_resets(int count) {
  int i;

#ifdef DEBUG
  printf("Add %d resets\n", count);
#endif

  for (i = 0; i < count; i++) {
    printer_cmd[bufpos++] = '\033';
    printer_cmd[bufpos++] = '\000';
  }
}

static void do_new_status(char *buf, int bytes) {
  /* static const char *colors_new[] = { */
  /*   "Black",		/\* 0 *\/ */
  /*   "Photo Black",	/\* 1 *\/ */
  /*   "Unknown",		/\* 2 *\/ */
  /*   "Cyan",		/\* 3 *\/ */
  /*   "Magenta",		/\* 4 *\/ */
  /*   "Yellow",		/\* 5 *\/ */
  /*   "Light Cyan",	        /\* 6 *\/ */
  /*   "Light Magenta",	/\* 7 *\/ */
  /*   "Unknown",		/\* 8 *\/ */
  /*   "Unknown",		/\* 9 *\/ */
  /*   "Light Black",	/\* a *\/ */
  /*   "Matte Black",	/\* b *\/ */
  /*   "Red",		/\* c *\/ */
  /*   "Blue",		/\* d *\/ */
  /*   "Gloss Optimizer",	/\* e *\/ */
  /*   "Light Light Black"	/\* f *\/ */
  /* }; */

  /* static int color_count = sizeof(colors_new) / sizeof(const char *); */

  static const int colors_new[] = {
    CARTRIDGE_BLACK,
    CARTRIDGE_PHOTOBLACK,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_CYAN,
    CARTRIDGE_MAGENTA,
    CARTRIDGE_YELLOW,
    CARTRIDGE_LIGHTCYAN,
    CARTRIDGE_LIGHTMAGENTA,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_LIGHTBLACK,
    CARTRIDGE_MATTEBLACK,
    CARTRIDGE_RED,
    CARTRIDGE_BLUE,
    CARTRIDGE_GLOSSOPTIMIZER,
    CARTRIDGE_LIGHTLIGHTBLACK,
    CARTRIDGE_ORANGE
  };

  static int color_count = sizeof(colors_new) / sizeof(int);

  /* static const char *aux_colors[] = { */
  /*   "Black",		/\* 0 *\/ */
  /*   "Cyan",		/\* 1 *\/ */
  /*   "Magenta",		/\* 2 *\/ */
  /*   "Yellow",		/\* 3 *\/ */
  /*   "Unknown",		/\* 4 *\/ */
  /*   "Unknown",		/\* 5 *\/ */
  /*   "Unknown",		/\* 6 *\/ */
  /*   "Unknown",		/\* 7 *\/ */
  /*   "Unknown",		/\* 8 *\/ */
  /*   "Red",		/\* 9 *\/ */
  /*   "Blue",		/\* a *\/ */
  /*   "Unknown",		/\* b *\/ */
  /*   "Unknown",		/\* c *\/ */
  /*   "Unknown",		/\* d *\/ */
  /*   "Unknown",		/\* e *\/ */
  /*   "Unknown"		/\* f *\/ */
  /* }; */

  /* static int aux_color_count = sizeof(aux_colors) / sizeof(const char *); */

  static const int aux_colors[] = {
    CARTRIDGE_BLACK,
    CARTRIDGE_CYAN,
    CARTRIDGE_MAGENTA,
    CARTRIDGE_YELLOW,
    CARTRIDGE_LIGHTCYAN,
    CARTRIDGE_LIGHTMAGENTA,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_RED,
    CARTRIDGE_BLUE,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
    CARTRIDGE_UNKNOWN,
  };

  static int aux_color_count = sizeof(aux_colors) / sizeof(int);

  int i = 0;
  int j;
  int c = 0;
  const char *ind;

#ifdef DEBUG
  printf("New format bytes: %d bytes\n", bytes);
#endif

  while (i < bytes) {
    unsigned hdr = buf[i];
    unsigned total_param_count = buf[i + 1];
    unsigned param = buf[i + 2];

#ifdef DEBUG
    printf("Header: %x param count: %d\n", hdr, total_param_count);
#endif

    if (hdr == 0x0f) {	/* Always report ink */
      size_t count = (total_param_count - 1) / param;
      ind = buf + i + 3;

#ifdef DEBUG
      printf("%18s    %20s\n", "Ink color", "Percent remaining");
#endif

      for (j = 0; j < count; j++) {
        if (ind[0] < color_count) {

#ifdef DEBUG
          printf("%18d    %20d\n",colors_new[(int) ind[0]], ind[2]);
#endif
          
          my_level->status = RESPONSE_VALID;
          my_level->levels[c][INDEX_TYPE] = colors_new[(int) ind[0]];
          my_level->levels[c][INDEX_LEVEL] = (short) ind[2];
          c++;
        } else if (ind[j] == 0x40 && ind[1] < aux_color_count) {

#ifdef DEBUG
          printf("%18d    %20d\n", aux_colors[(int) ind[1]], ind[2]);
#endif

          my_level->status = RESPONSE_VALID;
          my_level->levels[c][INDEX_TYPE] = aux_colors[(int) ind[1]];
          my_level->levels[c][INDEX_LEVEL] = (short) ind[2];
          c++;
        } else {

#ifdef DEBUG
          printf("%8s 0x%2x 0x%2x    %20d\n",
                 "Unknown", (unsigned char) ind[0],
                 (unsigned char) ind[1], ind[2]);
#endif
        }

        ind += param;
      }
    }
    i += total_param_count + 2;
  }
}

static void do_old_status(const char *buf) {
  do {
    const char *ind;
    
    if ((ind = looking_at_command(buf, "IQ")) != 0)  {
	  
#ifdef DEBUG
      printf("%18s    %20s\n", "Ink color", "Percent remaining");
#endif

      print_old_ink_levels(ind);
    }

#ifdef DEBUG
    printf("looking at %s\n", buf);
#endif

  } while ((buf = find_group(buf)) != NULL);
}  

static void print_old_ink_levels(const char *ind) {
  /* static const char *old_colors[] = { */
  /*   "Black",        /\* 0 *\/ */
  /*   "Cyan",         /\* 1 *\/ */
  /*   "Magenta",      /\* 2 *\/ */
  /*   "Yellow",       /\* 3 *\/ */
  /*   "LightCyan",    /\* 4 *\/ */
  /*   "Light Magenta" /\* 5 *\/ */
  /* }; */

  /* static int old_color_count = sizeof(old_colors) / sizeof(const char *); */

  static const int old_colors[] = {
    CARTRIDGE_BLACK,
    CARTRIDGE_CYAN,
    CARTRIDGE_MAGENTA,
    CARTRIDGE_YELLOW,
    CARTRIDGE_LIGHTCYAN,
    CARTRIDGE_LIGHTMAGENTA
  };
  
  static int old_color_count = sizeof(old_colors) / sizeof(int);

  int i;
  int c = 0;

  for (i = 0; i < old_color_count; i++) {
    int val;
    
    if (!ind[0] || ind[0] == ';')
      return;
    val = (get_digit(ind[0]) << 4) + get_digit(ind[1]);

#ifdef DEBUG
    printf("%18d    %20d\n", old_colors[i], val);
#endif

    my_level->status = RESPONSE_VALID;
    my_level->levels[c][INDEX_TYPE] = old_colors[i];
    my_level->levels[c][INDEX_LEVEL] = val;
    c++;

    ind += 2;
  }
}

static void start_remote_sequence(void) {
  static char remote_hdr[] = "\033@\033(R\010\000\000REMOTE1";
  memcpy(printer_cmd + bufpos, remote_hdr, sizeof(remote_hdr) - 1);
  bufpos += sizeof(remote_hdr) - 1;

#ifdef DEBUG
  printf("Start remote sequence\n");
#endif
}

static void end_remote_sequence(void) {  
  static char remote_trailer[] = "\033\000\000\000\033\000";
  memcpy(printer_cmd + bufpos, remote_trailer, sizeof(remote_trailer) - 1);
  bufpos += sizeof(remote_trailer) - 1;

#ifdef DEBUG
  printf("End remote sequence\n");
#endif
}

static const char *looking_at_command(const char *buf, const char *cmd) {
  if (!strncmp(buf, cmd, strlen(cmd))) {
    const char *answer = buf + strlen(cmd);
    if (answer[0] == ':') {
      return (answer + 1);
    }
  }
  return NULL;
}

static const char *find_group(const char *buf) {
  while (buf[0] == ';') {
    buf++;
  }

  buf = strchr(buf, ';');
  if (buf && buf[1]) {
    return buf + 1;
  }  else {
    return NULL;
  }
}

static int get_digit(char digit) {
  if (digit >= '0' && digit <= '9') {
    return digit - '0';
  } else if (digit >= 'A' && digit <= 'F') {
    return digit - 'A' + 10;
  } else if (digit >= 'a' && digit <= 'f') {
    return digit - 'a' + 10;
  } else {
    return 0;
  }
}