context.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include <mutex>
5 #include <array>
6 #include <string>
7 
8 #include "r200.h"
9 #include "f200.h"
10 #include "sr300.h"
11 #include "zr300.h"
12 #include "uvc.h"
13 #include "context.h"
14 
15 
16 #define constexpr_support 1
17 
18 #ifdef _MSC_VER
19 #if (_MSC_VER <= 1800) // constexpr is not supported in MSVC2013
20 #undef constexpr_support
21 #define constexpr_support 0
22 #endif
23 #endif
24 
25 #if (constexpr_support == 1)
26 template<unsigned... Is> struct seq{};
27 template<unsigned N, unsigned... Is>
28 struct gen_seq : gen_seq<N-1, N-1, Is...>{};
29 template<unsigned... Is>
30 struct gen_seq<0, Is...> : seq<Is...>{};
31 
32 template<unsigned N1, unsigned... I1, unsigned N2, unsigned... I2>
33 constexpr std::array<char const, N1+N2-1> concat(char const (&a1)[N1], char const (&a2)[N2], seq<I1...>, seq<I2...>){
34  return {{ a1[I1]..., a2[I2]... }};
35 }
36 
37 template<unsigned N1, unsigned N2>
38 constexpr std::array<char const, N1+N2-1> concat(char const (&a1)[N1], char const (&a2)[N2]){
39  return concat(a1, a2, gen_seq<N1-1>{}, gen_seq<N2>{});
40 }
41 
42 constexpr auto rs_api_version = concat("VERSION: ",RS_API_VERSION_STR);
43 
44 #else // manual version tracking is required
45 static const std::string rs_api_version("VERSION: 1.11.1");
46 
47 #endif
48 
49 bool is_compatible(std::shared_ptr<rs_device> device)
50 {
51  return device->supports(RS_CAPABILITIES_ENUMERATION);
52 }
53 
55 {
56  context = rsimpl::uvc::create_context();
57 
58  for(auto device : query_devices(context))
59  {
60  LOG_INFO("UVC device detected with VID = 0x" << std::hex << get_vendor_id(*device) << " PID = 0x" << get_product_id(*device));
61 
62  if (get_vendor_id(*device) != VID_INTEL_CAMERA)
63  continue;
64 
65  std::shared_ptr<rs_device> rs_dev;
66 
67  switch(get_product_id(*device))
68  {
69  case R200_PRODUCT_ID: rs_dev = rsimpl::make_r200_device(device); break;
70  case LR200_PRODUCT_ID: rs_dev = rsimpl::make_lr200_device(device); break;
71  case ZR300_PRODUCT_ID: rs_dev = rsimpl::make_zr300_device(device); break;
72  case F200_PRODUCT_ID: rs_dev = rsimpl::make_f200_device(device); break;
73  case SR300_PRODUCT_ID: rs_dev = rsimpl::make_sr300_device(device); break;
74  }
75 
76  if (rs_dev && is_compatible(rs_dev))
77  {
78  devices.push_back(rs_dev);
79  }
80  else
81  {
82  LOG_ERROR("Device is not supported by librealsense!");
83  }
84  }
85 }
86 
87 // Enforce singleton semantics on rs_context
91 std::string rs_context_base::api_version = std::string(rs_api_version.begin(),rs_api_version.end());
92 
94 {
95  std::lock_guard<std::mutex> lock(instance_lock);
96  if (ref_count++ == 0)
97  {
98  instance = new rs_context_base();
99  }
100  return instance;
101 }
102 
104 {
105  std::lock_guard<std::mutex> lock(instance_lock);
106  if (--ref_count == 0)
107  {
108  delete instance;
109  }
110 }
111 
113 {
114  assert(ref_count == 0);
115 }
116 
118 {
119  return devices.size();
120 }
121 
123 {
124  return devices[index].get();
125 }
std::shared_ptr< context > create_context()
std::shared_ptr< rs_device > make_zr300_device(std::shared_ptr< uvc::device > device)
Definition: zr300.cpp:425
size_t get_device_count() const override
Definition: context.cpp:117
rs_device * get_device(int index) const override
Definition: context.cpp:122
static int ref_count
Definition: context.h:25
static std::mutex instance_lock
Definition: context.h:26
const uint16_t VID_INTEL_CAMERA
Definition: uvc.h:14
GLsizei const GLchar *const * string
Definition: glext.h:683
static rs_context * instance
Definition: context.h:27
#define F200_PRODUCT_ID
Definition: f200.h:15
Definition: context.cpp:26
constexpr auto rs_api_version
Definition: context.cpp:42
std::shared_ptr< rs_device > make_r200_device(std::shared_ptr< uvc::device > device)
Definition: r200.cpp:27
GLuint index
Definition: glext.h:655
#define R200_PRODUCT_ID
Definition: ds-device.h:11
#define SR300_PRODUCT_ID
Definition: sr300.h:12
std::shared_ptr< rs_device > make_f200_device(std::shared_ptr< uvc::device > device)
Definition: f200.cpp:265
std::shared_ptr< rs_device > make_lr200_device(std::shared_ptr< uvc::device > device)
Definition: r200.cpp:43
#define LOG_INFO(...)
Definition: types.h:78
GLenum array
Definition: glext.h:6669
constexpr std::array< char const, N1+N2-1 > concat(char const (&a1)[N1], char const (&a2)[N2], seq< I1... >, seq< I2... >)
Definition: context.cpp:33
int get_product_id(const device &device)
std::shared_ptr< rs_device > make_sr300_device(std::shared_ptr< uvc::device > device)
Definition: sr300.cpp:250
#define LR200_PRODUCT_ID
Definition: ds-device.h:12
std::vector< std::shared_ptr< device > > query_devices(std::shared_ptr< context > context)
static rs_context * acquire_instance()
Definition: context.cpp:93
#define RS_API_VERSION_STR
Definition: rs.h:30
bool is_compatible(std::shared_ptr< rs_device > device)
Definition: context.cpp:49
static std::string api_version
Definition: context.h:28
#define LOG_ERROR(...)
Definition: types.h:80
int get_vendor_id(const device &device)
#define ZR300_PRODUCT_ID
Definition: ds-device.h:13
static void release_instance()
Definition: context.cpp:103


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