File: obex-check-device.c

package info (click to toggle)
libopenobex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 972 kB
  • ctags: 1,552
  • sloc: ansic: 9,988; xml: 407; makefile: 96
file content (75 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download
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
/** \file apps/obex_find.c
 * Help udev to find connectable OBEX USB devices.
 *
 * Copyright (c) 2011 Hendrik Sattler
 *
 * OpenOBEX 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 2 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 OpenOBEX. If not, see <http://www.gnu.org/>.
 */

#include <openobex/obex.h>
#include <openobex/obex_const.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void obex_event_cb(obex_t *handle, obex_object_t *obj, int mode,
			  int event, int obex_cmd, int obex_rsp)
{
}

static int match_usb_interface(obex_usb_intf_t *intf, uint16_t vendor,
			       uint16_t product)
{
	return (intf->idVendor == vendor && intf->idProduct == product);
}

static int find_usb_device(uint16_t vendor, uint16_t product)
{
	obex_t *handle;
	int found;
	int matched = 0;
	int i = 0;

	handle = OBEX_Init(OBEX_TRANS_USB, obex_event_cb, 0);
	if (!handle)
		return 0;

	found = OBEX_EnumerateInterfaces(handle);
	for (; !matched && i < found; ++i) {
		obex_interface_t *intf = OBEX_GetInterfaceByIndex(handle, i);

		matched = match_usb_interface(&intf->usb, vendor, product);
	}
	OBEX_Cleanup(handle);

	return matched;
}

int main (int argc, char **argv)
{
	unsigned long vendor;
	unsigned long product;

	if (argc < 3)
		return EXIT_FAILURE;

	vendor = strtoul(argv[1], NULL, 16);
	product = strtoul(argv[2], NULL, 16);
	if (vendor <= 0xFFFF && product <= 0xFFFF &&
	    find_usb_device((uint16_t)vendor, (uint16_t)product))
		return EXIT_SUCCESS;

	return EXIT_FAILURE;
}