diff options
-rw-r--r-- | include/libimobiledevice/libimobiledevice.h | 36 | ||||
-rw-r--r-- | src/idevice.c | 59 | ||||
-rw-r--r-- | tools/idevicebtlogger.c | 5 | ||||
-rw-r--r-- | tools/idevicesyslog.c | 5 |
4 files changed, 89 insertions, 16 deletions
diff --git a/include/libimobiledevice/libimobiledevice.h b/include/libimobiledevice/libimobiledevice.h index c3b87cd..6851145 100644 --- a/include/libimobiledevice/libimobiledevice.h +++ b/include/libimobiledevice/libimobiledevice.h | |||
@@ -94,6 +94,9 @@ typedef struct { | |||
94 | /** Callback to notifiy if a device was added or removed. */ | 94 | /** Callback to notifiy if a device was added or removed. */ |
95 | typedef void (*idevice_event_cb_t) (const idevice_event_t *event, void *user_data); | 95 | typedef void (*idevice_event_cb_t) (const idevice_event_t *event, void *user_data); |
96 | 96 | ||
97 | /** Event subscription context type */ | ||
98 | typedef struct idevice_subscription_context* idevice_subscription_context_t; | ||
99 | |||
97 | /* functions */ | 100 | /* functions */ |
98 | 101 | ||
99 | /** | 102 | /** |
@@ -104,9 +107,36 @@ typedef void (*idevice_event_cb_t) (const idevice_event_t *event, void *user_dat | |||
104 | void idevice_set_debug_level(int level); | 107 | void idevice_set_debug_level(int level); |
105 | 108 | ||
106 | /** | 109 | /** |
107 | * Register a callback function that will be called when device add/remove | 110 | * Subscribe a callback function that will be called when device add/remove |
108 | * events occur. | 111 | * events occur. |
109 | * | 112 | * |
113 | * @param context A pointer to a idevice_subscription_context_t that will be | ||
114 | * set upon creation of the subscription. The returned context must be | ||
115 | * passed to idevice_events_unsubscribe() to unsubscribe the callback. | ||
116 | * @param callback Callback function to call. | ||
117 | * @param user_data Application-specific data passed as parameter | ||
118 | * to the registered callback function. | ||
119 | * | ||
120 | * @return IDEVICE_E_SUCCESS on success or an error value when an error occurred. | ||
121 | */ | ||
122 | idevice_error_t idevice_events_subscribe(idevice_subscription_context_t *context, idevice_event_cb_t callback, void *user_data); | ||
123 | |||
124 | /** | ||
125 | * Unsubscribe the event callback function that has been registered with | ||
126 | * idevice_events_subscribe(). | ||
127 | * | ||
128 | * @param context A valid context as returned from idevice_events_subscribe(). | ||
129 | * | ||
130 | * @return IDEVICE_E_SUCCESS on success or an error value when an error occurred. | ||
131 | */ | ||
132 | idevice_error_t idevice_events_unsubscribe(idevice_subscription_context_t context); | ||
133 | |||
134 | /** | ||
135 | * (DEPRECATED) Register a callback function that will be called when device add/remove | ||
136 | * events occur. | ||
137 | * | ||
138 | * @deprecated Use idevice_events_subscribe() instead. | ||
139 | * | ||
110 | * @param callback Callback function to call. | 140 | * @param callback Callback function to call. |
111 | * @param user_data Application-specific data passed as parameter | 141 | * @param user_data Application-specific data passed as parameter |
112 | * to the registered callback function. | 142 | * to the registered callback function. |
@@ -116,9 +146,11 @@ void idevice_set_debug_level(int level); | |||
116 | idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data); | 146 | idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data); |
117 | 147 | ||
118 | /** | 148 | /** |
119 | * Release the event callback function that has been registered with | 149 | * (DEPRECATED) Release the event callback function that has been registered with |
120 | * idevice_event_subscribe(). | 150 | * idevice_event_subscribe(). |
121 | * | 151 | * |
152 | * @deprecated Use idevice_events_unsubscribe() instead. | ||
153 | * | ||
122 | * @return IDEVICE_E_SUCCESS on success or an error value when an error occurred. | 154 | * @return IDEVICE_E_SUCCESS on success or an error value when an error occurred. |
123 | */ | 155 | */ |
124 | idevice_error_t idevice_event_unsubscribe(void); | 156 | idevice_error_t idevice_event_unsubscribe(void); |
diff --git a/src/idevice.c b/src/idevice.c index 22d57e3..c8574fc 100644 --- a/src/idevice.c +++ b/src/idevice.c | |||
@@ -215,10 +215,17 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) | |||
215 | #warning No compiler support for constructor/destructor attributes, some features might not be available. | 215 | #warning No compiler support for constructor/destructor attributes, some features might not be available. |
216 | #endif | 216 | #endif |
217 | 217 | ||
218 | static idevice_event_cb_t event_cb = NULL; | 218 | struct idevice_subscription_context { |
219 | idevice_event_cb_t callback; | ||
220 | void *user_data; | ||
221 | usbmuxd_subscription_context_t ctx; | ||
222 | }; | ||
223 | |||
224 | static idevice_subscription_context_t event_ctx = NULL; | ||
219 | 225 | ||
220 | static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) | 226 | static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) |
221 | { | 227 | { |
228 | idevice_subscription_context_t context = (idevice_subscription_context_t)user_data; | ||
222 | idevice_event_t ev; | 229 | idevice_event_t ev; |
223 | 230 | ||
224 | ev.event = event->event; | 231 | ev.event = event->event; |
@@ -232,34 +239,66 @@ static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) | |||
232 | debug_info("Unknown connection type %d", event->device.conn_type); | 239 | debug_info("Unknown connection type %d", event->device.conn_type); |
233 | } | 240 | } |
234 | 241 | ||
235 | if (event_cb) { | 242 | if (context->callback) { |
236 | event_cb(&ev, user_data); | 243 | context->callback(&ev, context->user_data); |
237 | } | 244 | } |
238 | } | 245 | } |
239 | 246 | ||
240 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data) | 247 | LIBIMOBILEDEVICE_API idevice_error_t idevice_events_subscribe(idevice_subscription_context_t *context, idevice_event_cb_t callback, void *user_data) |
241 | { | 248 | { |
242 | event_cb = callback; | 249 | if (!context || !callback) { |
243 | int res = usbmuxd_subscribe(usbmux_event_cb, user_data); | 250 | return IDEVICE_E_INVALID_ARG; |
251 | } | ||
252 | *context = malloc(sizeof(struct idevice_subscription_context)); | ||
253 | if (!*context) { | ||
254 | debug_info("ERROR: %s: Failed to allocate subscription context\n", __func__); | ||
255 | return IDEVICE_E_UNKNOWN_ERROR; | ||
256 | } | ||
257 | (*context)->callback = callback; | ||
258 | (*context)->user_data = user_data; | ||
259 | int res = usbmuxd_events_subscribe(&(*context)->ctx, usbmux_event_cb, *context); | ||
244 | if (res != 0) { | 260 | if (res != 0) { |
245 | event_cb = NULL; | 261 | free(*context); |
262 | *context = NULL; | ||
246 | debug_info("ERROR: usbmuxd_subscribe() returned %d!", res); | 263 | debug_info("ERROR: usbmuxd_subscribe() returned %d!", res); |
247 | return IDEVICE_E_UNKNOWN_ERROR; | 264 | return IDEVICE_E_UNKNOWN_ERROR; |
248 | } | 265 | } |
249 | return IDEVICE_E_SUCCESS; | 266 | return IDEVICE_E_SUCCESS; |
250 | } | 267 | } |
251 | 268 | ||
252 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_unsubscribe(void) | 269 | LIBIMOBILEDEVICE_API idevice_error_t idevice_events_unsubscribe(idevice_subscription_context_t context) |
253 | { | 270 | { |
254 | event_cb = NULL; | 271 | if (!context) { |
255 | int res = usbmuxd_unsubscribe(); | 272 | return IDEVICE_E_INVALID_ARG; |
273 | } | ||
274 | int res = usbmuxd_events_unsubscribe(context->ctx); | ||
256 | if (res != 0) { | 275 | if (res != 0) { |
257 | debug_info("ERROR: usbmuxd_unsubscribe() returned %d!", res); | 276 | debug_info("ERROR: usbmuxd_unsubscribe() returned %d!", res); |
258 | return IDEVICE_E_UNKNOWN_ERROR; | 277 | return IDEVICE_E_UNKNOWN_ERROR; |
259 | } | 278 | } |
279 | if (context == event_ctx) { | ||
280 | event_ctx = NULL; | ||
281 | } | ||
282 | free(context); | ||
260 | return IDEVICE_E_SUCCESS; | 283 | return IDEVICE_E_SUCCESS; |
261 | } | 284 | } |
262 | 285 | ||
286 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data) | ||
287 | { | ||
288 | if (event_ctx) { | ||
289 | idevice_events_unsubscribe(event_ctx); | ||
290 | } | ||
291 | return idevice_events_subscribe(&event_ctx, callback, user_data); | ||
292 | } | ||
293 | |||
294 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_unsubscribe(void) | ||
295 | { | ||
296 | if (!event_ctx) { | ||
297 | return IDEVICE_E_SUCCESS; | ||
298 | } | ||
299 | return idevice_events_unsubscribe(event_ctx); | ||
300 | } | ||
301 | |||
263 | LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list_extended(idevice_info_t **devices, int *count) | 302 | LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list_extended(idevice_info_t **devices, int *count) |
264 | { | 303 | { |
265 | usbmuxd_device_info_t *dev_list; | 304 | usbmuxd_device_info_t *dev_list; |
diff --git a/tools/idevicebtlogger.c b/tools/idevicebtlogger.c index 421ce98..8de6b22 100644 --- a/tools/idevicebtlogger.c +++ b/tools/idevicebtlogger.c | |||
@@ -440,13 +440,14 @@ int main(int argc, char *argv[]) | |||
440 | assert(0); | 440 | assert(0); |
441 | return -2; | 441 | return -2; |
442 | } | 442 | } |
443 | idevice_event_subscribe(device_event_cb, NULL); | 443 | idevice_subscription_context_t context = NULL; |
444 | idevice_events_subscribe(&context, device_event_cb, NULL); | ||
444 | 445 | ||
445 | while (!quit_flag) { | 446 | while (!quit_flag) { |
446 | sleep(1); | 447 | sleep(1); |
447 | } | 448 | } |
448 | 449 | ||
449 | idevice_event_unsubscribe(); | 450 | idevice_events_unsubscribe(context); |
450 | stop_logging(); | 451 | stop_logging(); |
451 | 452 | ||
452 | fclose(packetlogger_file); | 453 | fclose(packetlogger_file); |
diff --git a/tools/idevicesyslog.c b/tools/idevicesyslog.c index f85c7cc..3084b97 100644 --- a/tools/idevicesyslog.c +++ b/tools/idevicesyslog.c | |||
@@ -709,12 +709,13 @@ int main(int argc, char *argv[]) | |||
709 | line_buffer_size = 1024; | 709 | line_buffer_size = 1024; |
710 | line = malloc(line_buffer_size); | 710 | line = malloc(line_buffer_size); |
711 | 711 | ||
712 | idevice_event_subscribe(device_event_cb, NULL); | 712 | idevice_subscription_context_t context = NULL; |
713 | idevice_events_subscribe(&context, device_event_cb, NULL); | ||
713 | 714 | ||
714 | while (!quit_flag) { | 715 | while (!quit_flag) { |
715 | sleep(1); | 716 | sleep(1); |
716 | } | 717 | } |
717 | idevice_event_unsubscribe(); | 718 | idevice_events_unsubscribe(context); |
718 | stop_logging(); | 719 | stop_logging(); |
719 | 720 | ||
720 | if (num_proc_filters > 0) { | 721 | if (num_proc_filters > 0) { |