summaryrefslogtreecommitdiff
path: root/src/device_manager.c
blob: ee570d4cda58b2565dbdfd9633098a129bce4adf (plain)
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
/* Camicro - Microscope camera viewer.
 *
 * Copyright (C) 2020 Nicolas Schodet
 *
 * 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 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 */
#include "device_manager.h"
#include "usb_source.h"

#include <glib.h>

#include "moticam.h"
#include "dummy.h"

struct device_manager
{
    libusb_context *usb;
    GSource *usb_source;
    device_manager_callback_f callback;
    void *user_data;
};

static const struct device_driver *drivers[] = {
    &moticam_device_driver,
    &dummy_device_driver,
    NULL
};

static gboolean
device_manager_usb_source_cb(gpointer user_data)
{
    struct device_manager *dm = user_data;
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    libusb_handle_events_timeout(dm->usb, &tv);
    if (dm->callback)
	dm->callback(dm->user_data);
    return G_SOURCE_CONTINUE;
}

struct device_manager *
device_manager_new(GMainContext *context)
{
    /* Create context. */
    struct device_manager *dm = g_new(struct device_manager, 1);
    dm->usb = NULL;
    dm->usb_source = NULL;
    dm->callback = NULL;
    dm->user_data = NULL;
    /* Initialize USB. */
    int r = libusb_init(&dm->usb);
    if (r)
	g_error("unable to initialize libusb: %s", libusb_strerror(r));
    dm->usb_source = usb_source_new(dm->usb);
    g_source_set_callback(dm->usb_source, device_manager_usb_source_cb, dm,
	    NULL);
    g_source_attach(dm->usb_source, context);
    /* Done. */
    return dm;
}

void
device_manager_set_callback(struct device_manager *dm,
	device_manager_callback_f callback, void *user_data)
{
    g_assert(dm);
    dm->callback = callback;
    dm->user_data = user_data;
}

struct device *
device_manager_open(struct device_manager *dm, GError **error)
{
    g_assert(dm);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    libusb_device **list;
    libusb_device *found_usb_device = NULL;
    char *found_device_name = NULL;
    const struct device_driver *found_device_driver = NULL;
    ssize_t cnt = libusb_get_device_list(dm->usb, &list);
    if (cnt < 0) {
	g_set_error(error, DEVICE_ERROR, DEVICE_ERROR_LIST,
		"can not list devices: %s", libusb_strerror(cnt));
	return NULL;
    }
    for (ssize_t i = 0; !found_usb_device && i < cnt; i++) {
	libusb_device *usb_device = list[i];
	struct libusb_device_descriptor desc;
	int r = libusb_get_device_descriptor(usb_device, &desc);
	if (r) {
	    g_warning("can not get device descriptor: %s",
		    libusb_strerror(r));
	} else {
	    for (const struct device_driver **dd = drivers;
		    !found_usb_device && *dd; dd++) {
		if ((*dd)->usb_poll) {
		    char *device_name = (*dd)->usb_poll(&desc);
		    if (device_name) {
			found_usb_device = usb_device;
			found_device_name = device_name;
			found_device_driver = *dd;
		    }
		}
	    }
	}
    }
    struct device *device = NULL;
    if (found_usb_device) {
	g_free(found_device_name);
	device = found_device_driver->usb_open(dm->usb, found_usb_device,
		error);
    } else {
	for (const struct device_driver **dd = drivers;
		!found_device_name && *dd; dd++) {
	    if ((*dd)->poll) {
		char *device_name = (*dd)->poll();
		if (device_name) {
		    found_device_name = device_name;
		    found_device_driver = *dd;
		}
	    }
	}
	if (found_device_name) {
	    g_free(found_device_name);
	    device = found_device_driver->open(error);
	} else {
	    g_set_error(error, DEVICE_ERROR, DEVICE_ERROR_LIST,
		    "no device found");
	}
    }
    libusb_free_device_list(list, 1);
    return device;
}

void
device_manager_destroy(struct device_manager *dm)
{
    g_assert(dm);
    /* Shutdown USB. */
    g_source_destroy(dm->usb_source);
    g_source_unref(dm->usb_source);
    libusb_exit(dm->usb);
    /* Free context. */
    g_free(dm);
}