src/libuvc/init.c
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (C) 2010-2012 Ken Tossell
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the author nor other contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
78 #include "libuvc.h"
79 #include "libuvc_internal.h"
80 
86 void *_uvc_handle_events(void *arg) {
87  uvc_context_t *ctx = (uvc_context_t *) arg;
88 
89  while (!ctx->kill_handler_thread)
90  {
91  if (ctx->usb_ctx) libusb_handle_events(ctx->usb_ctx);
92  else break;
93  }
94 
95  return NULL;
96 }
97 
108 uvc_error_t uvc_init(uvc_context_t **pctx, struct libusb_context *usb_ctx) {
109  uvc_error_t ret = UVC_SUCCESS;
110  uvc_context_t *ctx = calloc(1, sizeof(*ctx));
111 
112  if (usb_ctx == NULL) {
113  ret = libusb_init(&ctx->usb_ctx);
114  ctx->own_usb_ctx = 1;
115  if (ret != UVC_SUCCESS) {
116  free(ctx);
117  ctx = NULL;
118  }
119  } else {
120  ctx->own_usb_ctx = 0;
121  ctx->usb_ctx = usb_ctx;
122  }
123 
124  if (ctx != NULL)
125  *pctx = ctx;
126 
127  return ret;
128 }
129 
143  uvc_device_handle_t *devh;
144 
145  DL_FOREACH(ctx->open_devices, devh) {
146  uvc_close(devh);
147  }
148 
149  if (ctx->own_usb_ctx)
150  libusb_exit(ctx->usb_ctx);
151 
152  free(ctx);
153 }
154 
164  if (ctx->own_usb_ctx)
165  pthread_create(&ctx->handler_thread, NULL, _uvc_handle_events, (void*) ctx);
166 }
167 
uvc_error_t uvc_init(uvc_context_t **pctx, struct libusb_context *usb_ctx)
Initializes the UVC context.
enum uvc_error uvc_error_t
#define DL_FOREACH(head, el)
Definition: utlist.h:76
void * _uvc_handle_events(void *arg)
Implementation-specific UVC constants and structures.
auto ctx
void uvc_close(uvc_device_handle_t *devh)
Close a device.
Definition: dev.c:1412
void uvc_start_handler_thread(uvc_context_t *ctx)
struct uvc_context uvc_context_t
Definition: libuvc.h:253
void uvc_exit(uvc_context_t *ctx)
Closes the UVC context, shutting down any active cameras.
struct uvc_device_handle uvc_device_handle_t
Definition: libuvc.h:268


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17