AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
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  pthread_t child;
35 
36  mCallback = callback;
37  mRefCon = refCon;
38  mDevices = devices;
39 
40  if (child_id != 0)
41  Uninstall();
42 
43  if (mCallback)
44  (*(mCallback))(AJA_Pnp_DeviceAdded, mRefCon);
45 
46  run = true;
47  if(pthread_create(&child, NULL, *Worker, (void *)this) < 0)
48  return AJA_STATUS_FAIL;
49 
50  child_id = child;
51 
52  return AJA_STATUS_SUCCESS;
53 }
54 
55 
56 AJAStatus
58 {
59  if (child_id == 0)
60  return AJA_STATUS_SUCCESS;
61 
62  run = false;
63 
64  struct timespec tt;
66  tt.tv_sec += 1;;
67 
68  pthread_timedjoin_np(child_id, NULL, &tt);
69 
70  child_id = 0;
71  mCallback = NULL;
72  mRefCon = NULL;
73  mDevices = 0;
74 
75  return AJA_STATUS_SUCCESS;
76 }
77 
78 
81 {
82  return mCallback;
83 }
84 
85 
86 void*
88 {
89  return mRefCon;
90 }
91 
92 
93 uint32_t
95 {
96  return mDevices;
97 }
98 
99 
100 void*
101 AJAPnpImpl::Worker(void* refCon)
102 {
103  AJAPnpImpl* pPnP = (AJAPnpImpl*)refCon;
104  struct udev *udev;
105  struct udev_device *dev;
106  struct udev_monitor *mon;
107  int fd;
108 
109  udev = udev_new();
110  if (udev == NULL)
111  {
112  return NULL;
113  }
114 
115  mon = udev_monitor_new_from_netlink(udev, "udev");
116  udev_monitor_filter_add_match_subsystem_devtype(mon, "ajantv2", NULL);
117  udev_monitor_enable_receiving(mon);
118  fd = udev_monitor_get_fd(mon);
119 
120  while (run)
121  {
122  fd_set fds;
123  struct timeval tv;
124  int ret;
125 
126  FD_ZERO(&fds);
127  FD_SET(fd, &fds);
128  tv.tv_sec = 0;
129  tv.tv_usec = 1000000;
130 
131  ret = select(fd+1, &fds, NULL, NULL, &tv);
132  if (ret > 0 && FD_ISSET(fd, &fds)) {
133  dev = udev_monitor_receive_device(mon);
134  if (dev) {
135 // printf("I: ACTION=%s\n", udev_device_get_action(dev));
136 // printf("I: DEVNAME=%s\n", udev_device_get_sysname(dev));
137 // printf("I: DEVPATH=%s\n", udev_device_get_devpath(dev));
139  if (strstr(udev_device_get_action(dev), "add") != NULL)
140  {
141  action = AJA_Pnp_DeviceAdded;
142 // printf("hotplug add %d\n", count++);
143  }
144  if (strstr(udev_device_get_action(dev), "remove") != NULL)
145  {
146  action = AJA_Pnp_DeviceRemoved;
147 // printf("hotplug remove %d\n", count++);
148  }
149  if ((action != AJA_Pnp_DeviceOffline) && (pPnP->mCallback != NULL))
150  (*(pPnP->mCallback))(action, pPnP->mRefCon);
151 
152  udev_device_unref(dev);
153  }
154  }
155  }
156 
157  udev_unref(udev);
158 
159  return NULL;
160 }
161 
162 
AJAPnpImpl::Uninstall
AJAStatus Uninstall(void)
Definition: pnpimpl.cpp:57
AJAPnpImpl::GetPnpDevices
uint32_t GetPnpDevices()
Definition: pnpimpl.cpp:94
child_id
static pthread_t child_id
Definition: pnpimpl.cpp:17
AJAPnpImpl::GetRefCon
void * GetRefCon()
Definition: pnpimpl.cpp:87
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.
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
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
AJAPnpImpl::GetCallback
AJAPnpCallback GetCallback()
Definition: pnpimpl.cpp:80
run
static bool run
Definition: pnpimpl.cpp:18