core.c
Go to the documentation of this file.
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 
32 #include <unistd.h>
33 
34 #include "freenect_internal.h"
35 #include "registration.h"
36 #include "cameras.h"
37 #include "loader.h"
38 
39 
41 {
42  int res;
43 
44  *ctx = (freenect_context*)malloc(sizeof(freenect_context));
45  if (*ctx == NULL)
46  return -1;
47 
48  memset(*ctx, 0, sizeof(freenect_context));
49 
50  (*ctx)->log_level = LL_WARNING;
52  res = fnusb_init(&(*ctx)->usb, usb_ctx);
53  if (res < 0) {
54  free(*ctx);
55  *ctx = NULL;
56  }
57  return res;
58 }
59 
61 {
62  while (ctx->first) {
63  FN_NOTICE("Device %p open during shutdown, closing...\n", ctx->first);
65  }
66 
67  fnusb_shutdown(&ctx->usb);
68  free(ctx);
69  return 0;
70 }
71 
73 {
74  struct timeval timeout;
75  timeout.tv_sec = 60;
76  timeout.tv_usec = 0;
77  return freenect_process_events_timeout(ctx, &timeout);
78 }
79 
81 {
82  int res = fnusb_process_events_timeout(&ctx->usb, timeout);
83  // Iterate over the devices in ctx. If any of them are flagged as
84  freenect_device* dev = ctx->first;
85  while(dev) {
86  if (dev->usb_cam.device_dead) {
87  FN_ERROR("USB camera marked dead, stopping streams\n");
88  res = -1;
91  }
92  if (dev->usb_audio.device_dead) {
93  FN_ERROR("USB audio marked dead, stopping streams\n");
94  res = -1; // Or something else to tell the user that the device just vanished.
96  }
97  dev = dev->next;
98  }
99  return res;
100 }
101 
103 {
104  return fnusb_num_devices(&ctx->usb);
105 }
106 
108 {
109  return fnusb_list_device_attributes(&ctx->usb, attribute_list);
110 }
111 
113 {
114  // Iterate over list, freeing contents of each item as we go.
115  struct freenect_device_attributes* to_free;
116  while(attribute_list != NULL) {
117  to_free = attribute_list;
118  if (attribute_list->camera_serial != NULL) {
119  free((char*)attribute_list->camera_serial);
120  attribute_list->camera_serial = NULL;
121  }
122  attribute_list = attribute_list->next;
123  free(to_free);
124  }
125  return;
126 }
127 
129 {
131 }
132 
134 {
136 }
137 
139 {
140  return ctx->enabled_subdevices;
141 }
142 
144 {
145  int res;
146  freenect_device *pdev = (freenect_device*)malloc(sizeof(freenect_device));
147  if (!pdev)
148  return -1;
149 
150  memset(pdev, 0, sizeof(*pdev));
151 
152  pdev->parent = ctx;
153 
154  res = fnusb_open_subdevices(pdev, index);
155  if (res < 0) {
156  free(pdev);
157  return res;
158  }
159 
160  if (!ctx->first) {
161  ctx->first = pdev;
162  } else {
163  freenect_device *prev = ctx->first;
164  while (prev->next)
165  prev = prev->next;
166  prev->next = pdev;
167  }
168 
169  *dev = pdev;
170 
171  // Do device-specific initialization
172  if (pdev->usb_cam.dev) {
173  if (freenect_camera_init(pdev) < 0) {
174  return -1;
175  }
176  }
177 
178  return 0;
179 }
180 
182 {
183  // This is implemented by listing the devices and seeing which index (if
184  // any) has a camera with a matching serial number, and then punting to
185  // freenect_open_device with that index.
186  struct freenect_device_attributes* attrlist;
187  struct freenect_device_attributes* item;
188  int count = fnusb_list_device_attributes(&ctx->usb, &attrlist);
189  if (count < 0) {
190  FN_ERROR("freenect_open_device_by_camera_serial: Couldn't enumerate serial numbers\n");
191  return -1;
192  }
193  int index = 0;
194  for(item = attrlist ; item != NULL; item = item->next , index++) {
195  if (strlen(item->camera_serial) == strlen(camera_serial) && strcmp(item->camera_serial, camera_serial) == 0) {
197  return freenect_open_device(ctx, dev, index);
198  }
199  }
201  FN_ERROR("freenect_open_device_by_camera_serial: Couldn't find a device with serial %s\n", camera_serial);
202  return -1;
203 }
204 
206 {
207  freenect_context *ctx = dev->parent;
208  int res;
209 
210  if (dev->usb_cam.dev) {
212  }
213 
214  res = fnusb_close_subdevices(dev);
215  if (res < 0) {
216  FN_ERROR("fnusb_close_subdevices failed: %d\n", res);
217  return res;
218  }
219 
220  freenect_device *last = NULL;
221  freenect_device *cur = ctx->first;
222 
223  while (cur && cur != dev) {
224  last = cur;
225  cur = cur->next;
226  }
227 
228  if (!cur) {
229  FN_ERROR("device %p not found in linked list for this context!\n", dev);
230  return -1;
231  }
232 
233  if (last)
234  last->next = cur->next;
235  else
236  ctx->first = cur->next;
237 
238  free(dev);
239  return 0;
240 }
241 
243 {
244  dev->user_data = user;
245 }
246 
248 {
249  return dev->user_data;
250 }
251 
253 {
254  ctx->log_level = level;
255 }
256 
258 {
259  ctx->log_cb = cb;
260 }
261 
262 FN_INTERNAL void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...)
263 {
264  va_list ap;
265 
266  if (level > ctx->log_level)
267  return;
268 
269  if (ctx->log_cb) {
270  char msgbuf[1024];
271 
272  va_start(ap, fmt);
273  vsnprintf(msgbuf, 1024, fmt, ap);
274  msgbuf[1023] = 0;
275  va_end(ap);
276 
277  ctx->log_cb(ctx, level, msgbuf);
278  } else {
279  va_start(ap, fmt);
280  vfprintf(stderr, fmt, ap);
281  va_end(ap);
282  }
283 }
284 
285 
286 FREENECTAPI void freenect_set_fw_address_nui(freenect_context * ctx, unsigned char * fw_ptr, unsigned int num_bytes)
287 {
288  ctx->fn_fw_nui_ptr = fw_ptr;
289  ctx->fn_fw_nui_size = num_bytes;
290 }
291 
292 FREENECTAPI void freenect_set_fw_address_k4w(freenect_context * ctx, unsigned char * fw_ptr, unsigned int num_bytes)
293 {
294  ctx->fn_fw_k4w_ptr = fw_ptr;
295  ctx->fn_fw_k4w_size = num_bytes;
296 }
297 
freenect_device_flags enabled_subdevices
FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs)
Definition: core.c:133
FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
Definition: usb_libusb10.c:255
#define FREENECTAPI
If Win32, export all functions for DLL usage.
Definition: libfreenect.h:187
freenect_context * parent
unsigned char * fn_fw_k4w_ptr
FREENECTAPI int freenect_stop_audio(freenect_device *dev)
Definition: audio.c:198
FREENECTAPI int freenect_num_devices(freenect_context *ctx)
Definition: core.c:102
int freenect_stop_video(freenect_device *dev)
Definition: fakenect.c:383
FN_INTERNAL void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt,...)
Definition: core.c:262
unsigned int fn_fw_k4w_size
freenect_device * first
void freenect_usb_context
Definition: libfreenect.h:182
#define FN_ERROR(...)
const char * camera_serial
Definition: libfreenect.h:70
freenect_device * next
int device_dead
Definition: usb_libusb10.h:71
FREENECTAPI void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level)
Definition: core.c:252
FREENECTAPI int freenect_process_events(freenect_context *ctx)
Definition: core.c:72
FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval *timeout)
Definition: core.c:80
freenect_loglevel
Enumeration of message logging levels.
Definition: libfreenect.h:200
FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
Definition: core.c:40
freenect_log_cb log_cb
libusb_device_handle * dev
Definition: usb_libusb10.h:70
FN_INTERNAL int fnusb_close_subdevices(freenect_device *dev)
Definition: usb_libusb10.c:650
FREENECTAPI int freenect_shutdown(freenect_context *ctx)
Definition: core.c:60
FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, freenect_device **dev, const char *camera_serial)
Definition: core.c:181
FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx)
Definition: core.c:138
unsigned char * fn_fw_nui_ptr
FREENECTAPI void freenect_set_fw_address_k4w(freenect_context *ctx, unsigned char *fw_ptr, unsigned int num_bytes)
Definition: core.c:292
FREENECTAPI int freenect_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes **attribute_list)
Definition: core.c:107
FN_INTERNAL int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes **attribute_list)
Definition: usb_libusb10.c:110
FREENECTAPI void * freenect_get_user(freenect_device *dev)
Definition: core.c:247
struct freenect_device_attributes * next
Definition: libfreenect.h:69
void(* freenect_log_cb)(freenect_context *dev, freenect_loglevel level, const char *msg)
Typedef for logging callback functions.
Definition: libfreenect.h:232
FREENECTAPI void freenect_set_fw_address_nui(freenect_context *ctx, unsigned char *fw_ptr, unsigned int num_bytes)
Definition: core.c:286
#define FN_NOTICE(...)
FN_INTERNAL int fnusb_shutdown(fnusb_ctx *ctx)
Definition: usb_libusb10.c:235
#define LL_WARNING
FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attributes *attribute_list)
Definition: core.c:112
static freenect_context * ctx
FREENECTAPI int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index)
Definition: core.c:143
FN_INTERNAL int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval *timeout)
Definition: usb_libusb10.c:250
#define FN_INTERNAL
unsigned int fn_fw_nui_size
freenect_loglevel log_level
FREENECTAPI int freenect_supported_subdevices(void)
Definition: core.c:128
FN_INTERNAL int freenect_camera_teardown(freenect_device *dev)
Definition: cameras.c:1267
freenect_device_flags
Definition: libfreenect.h:58
int freenect_stop_depth(freenect_device *dev)
Definition: fakenect.c:377
FREENECTAPI void freenect_set_user(freenect_device *dev, void *user)
Definition: core.c:242
FREENECTAPI void freenect_set_log_callback(freenect_context *ctx, freenect_log_cb cb)
Definition: core.c:257
FN_INTERNAL int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx)
Definition: usb_libusb10.c:214
FN_INTERNAL int fnusb_num_devices(fnusb_ctx *ctx)
Definition: usb_libusb10.c:40
FN_INTERNAL int freenect_camera_init(freenect_device *dev)
Definition: cameras.c:1243
FREENECTAPI int freenect_close_device(freenect_device *dev)
Definition: core.c:205


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Thu Jun 6 2019 19:25:38