AJA NTV2 SDK  17.5.0.1242
NTV2 SDK 17.5.0.1242
pnpimpl.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdbool.h>
12 #include <math.h>
13 #include <libudev.h>
14 
16 
17 static pthread_t child_id = 0;
18 static bool run = false;
19 
20 AJAPnpImpl::AJAPnpImpl() : mRefCon(NULL), mCallback(NULL), mDevices(0)
21 {
22 }
23 
24 
26 {
27  Uninstall();
28 }
29 
30 
31 AJAStatus
32 AJAPnpImpl::Install(AJAPnpCallback callback, void* refCon, uint32_t devices)
33 {
34  mCallback = callback;
35  mRefCon = refCon;
36  mDevices = devices;
37 
38  if (!mCallback)
39  return AJA_STATUS_NULL; // NULL callback
40 
41  if (child_id)
42  Uninstall();
43 
44  // Linux only handles PCIe devices
45  if (mDevices & AJA_Pnp_PciVideoDevices)
46  {
47  run = true;
48  pthread_t child;
49  if (pthread_create(&child, NULL, *Worker, (void *)this) < 0)
50  return AJA_STATUS_FAIL; // Failed to create Worker thread
51 
52  (*(mCallback))(AJA_Pnp_DeviceAdded, mRefCon);
53 
54  child_id = child;
55  return AJA_STATUS_SUCCESS;
56  }
57  return AJA_STATUS_FAIL;
58 }
59 
60 
61 AJAStatus
63 {
64  if (child_id == 0)
65  return AJA_STATUS_SUCCESS;
66 
67  run = false;
68 
69  struct timespec tt;
71  tt.tv_sec += 1;;
72 
73  pthread_timedjoin_np(child_id, NULL, &tt);
74 
75  child_id = 0;
76  mCallback = NULL;
77  mRefCon = NULL;
78  mDevices = 0;
79 
80  return AJA_STATUS_SUCCESS;
81 }
82 
83 
84 void*
85 AJAPnpImpl::Worker(void* refCon)
86 {
87  AJAPnpImpl* pPnP = (AJAPnpImpl*)refCon;
88  struct udev *udev;
89  struct udev_device *dev;
90  struct udev_monitor *mon;
91  int fd;
92 
93  udev = udev_new();
94  if (udev == NULL)
95  {
96  return NULL;
97  }
98 
99  mon = udev_monitor_new_from_netlink(udev, "udev");
100  udev_monitor_filter_add_match_subsystem_devtype(mon, "ajantv2", NULL);
101  udev_monitor_enable_receiving(mon);
102  fd = udev_monitor_get_fd(mon);
103 
104  while (run)
105  {
106  fd_set fds;
107  struct timeval tv;
108  int ret;
109 
110  FD_ZERO(&fds);
111  FD_SET(fd, &fds);
112  tv.tv_sec = 0;
113  tv.tv_usec = 1000000;
114 
115  ret = select(fd+1, &fds, NULL, NULL, &tv);
116  if (ret > 0 && FD_ISSET(fd, &fds)) {
117  dev = udev_monitor_receive_device(mon);
118  if (dev) {
119 // printf("I: ACTION=%s\n", udev_device_get_action(dev));
120 // printf("I: DEVNAME=%s\n", udev_device_get_sysname(dev));
121 // printf("I: DEVPATH=%s\n", udev_device_get_devpath(dev));
123  if (strstr(udev_device_get_action(dev), "add") != NULL)
124  {
125  action = AJA_Pnp_DeviceAdded;
126 // printf("hotplug add %d\n", count++);
127  }
128  if (strstr(udev_device_get_action(dev), "remove") != NULL)
129  {
130  action = AJA_Pnp_DeviceRemoved;
131 // printf("hotplug remove %d\n", count++);
132  }
133  if ((action != AJA_Pnp_DeviceOffline) && (pPnP->mCallback != NULL))
134  (*(pPnP->mCallback))(action, pPnP->mRefCon);
135 
136  udev_device_unref(dev);
137  }
138  }
139  }
140 
141  udev_unref(udev);
142 
143  return NULL;
144 }
145 
146 
AJAPnpImpl::Uninstall
AJAStatus Uninstall(void)
Definition: pnpimpl.cpp:62
child_id
static pthread_t child_id
Definition: pnpimpl.cpp:17
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
AJAPnpCallback
void(* AJAPnpCallback)(AJAPnpMessage inMessage, void *inRefCon)
If installed (see AJAPnp::Install) in an AJAPnp instance, this function is called when an AJA device ...
Definition: pnp.h:39
AJAPnpMessage
AJAPnpMessage
Definition: pnp.h:22
pnpimpl.h
Declares the AJAPnpImpl class.
AJA_Pnp_PciVideoDevices
@ AJA_Pnp_PciVideoDevices
Definition: pnp.h:16
AJAStatus
AJAStatus
Definition: types.h:378
CLOCK_REALTIME
#define CLOCK_REALTIME
Definition: pthreadsextra.h:21
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:382
AJA_Pnp_DeviceOffline
@ AJA_Pnp_DeviceOffline
Definition: pnp.h:27
AJAPnpImpl::Install
AJAStatus Install(AJAPnpCallback callback, void *refCon, uint32_t devices)
Definition: pnpimpl.cpp:32
AJA_Pnp_DeviceRemoved
@ AJA_Pnp_DeviceRemoved
Definition: pnp.h:25
AJAPnpImpl
Definition: pnpimpl.h:13
AJAPnpImpl::AJAPnpImpl
AJAPnpImpl()
Definition: pnpimpl.cpp:20
AJA_STATUS_NULL
@ AJA_STATUS_NULL
Definition: types.h:387
AJAPnpImpl::~AJAPnpImpl
virtual ~AJAPnpImpl(void)
Definition: pnpimpl.cpp:25
AJA_Pnp_DeviceAdded
@ AJA_Pnp_DeviceAdded
Definition: pnp.h:24
clock_gettime
int clock_gettime(clockid_t clk_id, struct timespec *tp)
Definition: pthreadsextra.cpp:12
run
static bool run
Definition: pnpimpl.cpp:18