Skip to content

Commit

Permalink
Changed signature of usbmuxd_subscribe() and usbmuxd_unsubscribe() to…
Browse files Browse the repository at this point in the history
… include a usbmuxd context.
  • Loading branch information
alexander committed Apr 19, 2018
1 parent f7a93de commit 92db0f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
8 changes: 6 additions & 2 deletions include/usbmuxd.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@ int usbmuxd_uninit(usbmuxd_t *usbmuxd);
* Subscribe a callback function so that applications get to know about
* device add/remove events.
*
* @param usbmuxd A pointer to previously initialized usbmuxd context.
*
* @param callback A callback function that is executed when an event occurs.
*
* @return 0 on success or negative on error.
*/
int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data);
int usbmuxd_subscribe(usbmuxd_t *usbmuxd, usbmuxd_event_cb_t callback, void *user_data);

/**
* Unsubscribe callback.
*
* @param usbmuxd A pointer to previously initialized usbmuxd context.
*
* @return only 0 for now.
*/
int usbmuxd_unsubscribe();
int usbmuxd_unsubscribe(usbmuxd_t *usbmuxd);

/**
* Contacts usbmuxd and retrieves a list of connected devices.
Expand Down
33 changes: 22 additions & 11 deletions src/libusbmuxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,23 +825,28 @@ static void *device_monitor(void *data)
return NULL;
}

USBMUXD_API int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data)
USBMUXD_API int usbmuxd_subscribe(usbmuxd_t *usbmuxd, usbmuxd_event_cb_t callback, void *user_data)
{
int res;

/* usbmuxd context don't exist. */
if (usbmuxd == NULL) {
return -EINVAL;
}

if (!callback) {
return -EINVAL;
}
event_cb = callback;

#ifdef WIN32
res = 0;
devmon = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)device_monitor, user_data, 0, NULL);
if (devmon == NULL) {
usbmuxd->devmon = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)device_monitor, user_data, 0, NULL);
if (usbmuxd->devmon == NULL) {
res = GetLastError();
}
#else
res = pthread_create(&devmon, NULL, device_monitor, user_data);
res = pthread_create(&(usbmuxd->devmon), NULL, device_monitor, user_data);
#endif
if (res != 0) {
DEBUG(1, "%s: ERROR: Could not start device watcher thread!\n", __func__);
Expand All @@ -850,25 +855,31 @@ USBMUXD_API int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data)
return 0;
}

USBMUXD_API int usbmuxd_unsubscribe()

USBMUXD_API int usbmuxd_unsubscribe(usbmuxd_t *usbmuxd)
{
int res;
event_cb = NULL;

socket_shutdown(listenfd, SHUT_RDWR);
/* usbmuxd context don't exist. */
if (usbmuxd == NULL) {
return -EINVAL;
}

socket_shutdown(usbmuxd->listenfd, SHUT_RDWR);

#ifdef WIN32
if (devmon != NULL) {
res = WaitForSingleObject(devmon, INFINITE);
if (usbmuxd->devmon != NULL) {
res = WaitForSingleObject(usbmuxd->devmon, INFINITE);
if (res != 0) {
return res;
}
}
#else
res = pthread_kill(devmon, 0);
res = pthread_kill(usbmuxd->devmon, 0);
if (res == 0) {
pthread_cancel(devmon);
res = pthread_join(devmon, NULL);
pthread_cancel(usbmuxd->devmon);
res = pthread_join(usbmuxd->devmon, NULL);
}
if ((res != 0) && (res != ESRCH)) {
return res;
Expand Down

0 comments on commit 92db0f3

Please sign in to comment.