File: devices.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 (63 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (3)
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
/* devices.c
 *
 * (c) 2015, 2018, 2022 Markus Heinz
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>

#include "inklevel.h"
#include "internal.h"
#include "devices.h"
#include "bjnp.h"
#include "libusb-utils.h"

int get_device_id(const int port, const char *device_file, 
                  const int portnumber, char *device_id) {
  int result = COULD_NOT_GET_DEVICE_ID;

  if (port == USB) {
    usb_printer *printer = NULL;

    if (init_usb() != USB_SUCCESS) {
      return COULD_NOT_GET_DEVICE_ID;
    }

    printer = find_printer(portnumber);

    if (printer != NULL) {
      result = open_device_handle(printer);

      if (result == USB_SUCCESS) {
	result = get_usb_device_id(printer, device_id, BUFLEN);
        release_device_handle(printer);
      }
      
      free(printer);
    }

    shutdown_usb();

    if (result == USB_SUCCESS)
      return OK;
    else
      return COULD_NOT_GET_DEVICE_ID;

  } else if (port == CUSTOM_BJNP)  {
    return bjnp_get_id_from_named_printer(portnumber, device_file, device_id);
  } else if (port == BJNP) {
    return bjnp_get_id_from_printer_port(portnumber, device_id);
  } else {
    return UNKNOWN_PORT_SPECIFIED;
  }
}