Skip to content

Commit

Permalink
Creation of new usbmuxd context, which to be initialized and required…
Browse files Browse the repository at this point in the history
… for all function call. New function to initialize and unintialize usbmuxd context.
  • Loading branch information
alexander committed Apr 17, 2018
1 parent 23d9abb commit f7a93de
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions common/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct collection {
int capacity;
};


void collection_init(struct collection *col);
void collection_add(struct collection *col, void *element);
void collection_remove(struct collection *col, void *element);
Expand All @@ -45,3 +46,5 @@ void collection_free(struct collection *col);
} while(0);

#endif

typedef struct collection collection_t;
32 changes: 32 additions & 0 deletions include/usbmuxd.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ typedef struct {
*/
typedef void (*usbmuxd_event_cb_t) (const usbmuxd_event_t *event, void *user_data);

/**
* usbmuxd context structure which need to be created and passed on to all functions.
*/
typedef struct {
void *devices;
usbmuxd_event_cb_t event_cb;
#ifdef WIN32
HANDLE devmon = NULL;
#else
pthread_t devmon;
#endif
int listenfd;
int use_tag;
int proto_version;
} usbmuxd_t;

/**
* Initialize usbmuxd context.
*
* @return A pointer to allocated usbmuxd_t context on success or NULL on error.
*/
usbmuxd_t* usbmuxd_init(void);

/**
* UnInitialize an existing usbmuxd context.
*
* @param usbmuxd A pointer to previously allocated usbmuxd_t context.
*
* @return 0 on success or negative on error.
*/
int usbmuxd_uninit(usbmuxd_t *usbmuxd);

/**
* Subscribe a callback function so that applications get to know about
* device add/remove events.
Expand Down
43 changes: 43 additions & 0 deletions src/libusbmuxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,49 @@ static volatile int use_tag = 0;
static volatile int proto_version = 1;
static volatile int try_list_devices = 1;


USBMUXD_API usbmuxd_t* usbmuxd_init(void)
{
usbmuxd_t *usbmuxd;

usbmuxd = (usbmuxd_t*) malloc(sizeof(usbmuxd_t));
if (usbmuxd == NULL)
return NULL;

usbmuxd->event_cb = NULL;
#ifdef WIN32
usbmuxd->devmon = NULL;
#endif
usbmuxd->listenfd = -1;
usbmuxd->use_tag = 0;
usbmuxd->proto_version = 1;

usbmuxd->devices = (void*) malloc(sizeof(collection_t));
if (usbmuxd->devices == NULL) {
free(usbmuxd);
return NULL;
}


return usbmuxd;
}


USBMUXD_API int usbmuxd_uninit(usbmuxd_t *usbmuxd)
{
if (usbmuxd == NULL)
return 0;

if(usbmuxd->devices)
free(usbmuxd->devices);

usbmuxd->devices = NULL;
usbmuxd->event_cb = NULL;

free(usbmuxd);
return 0;
}

/**
* Finds a device info record by its handle.
* if the record is not found, NULL is returned.
Expand Down

0 comments on commit f7a93de

Please sign in to comment.