diff options
Diffstat (limited to 'src/idevice.c')
| -rw-r--r-- | src/idevice.c | 92 |
1 files changed, 71 insertions, 21 deletions
diff --git a/src/idevice.c b/src/idevice.c index 06991c5..d23c5b5 100644 --- a/src/idevice.c +++ b/src/idevice.c | |||
| @@ -190,7 +190,14 @@ static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) | |||
| 190 | 190 | ||
| 191 | ev.event = event->event; | 191 | ev.event = event->event; |
| 192 | ev.udid = event->device.udid; | 192 | ev.udid = event->device.udid; |
| 193 | ev.conn_type = CONNECTION_USBMUXD; | 193 | ev.conn_type = 0; |
| 194 | if (event->device.conn_type == CONNECTION_TYPE_USB) { | ||
| 195 | ev.conn_type = CONNECTION_USBMUXD; | ||
| 196 | } else if (event->device.conn_type == CONNECTION_TYPE_NETWORK) { | ||
| 197 | ev.conn_type = CONNECTION_NETWORK; | ||
| 198 | } else { | ||
| 199 | debug_info("Unknown connection type %d", event->device.conn_type); | ||
| 200 | } | ||
| 194 | 201 | ||
| 195 | if (event_cb) { | 202 | if (event_cb) { |
| 196 | event_cb(&ev, user_data); | 203 | event_cb(&ev, user_data); |
| @@ -236,9 +243,11 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list(char ***devices, in | |||
| 236 | int i, newcount = 0; | 243 | int i, newcount = 0; |
| 237 | 244 | ||
| 238 | for (i = 0; dev_list[i].handle > 0; i++) { | 245 | for (i = 0; dev_list[i].handle > 0; i++) { |
| 239 | newlist = realloc(*devices, sizeof(char*) * (newcount+1)); | 246 | if (dev_list[i].conn_type == CONNECTION_TYPE_USB) { |
| 240 | newlist[newcount++] = strdup(dev_list[i].udid); | 247 | newlist = realloc(*devices, sizeof(char*) * (newcount+1)); |
| 241 | *devices = newlist; | 248 | newlist[newcount++] = strdup(dev_list[i].udid); |
| 249 | *devices = newlist; | ||
| 250 | } | ||
| 242 | } | 251 | } |
| 243 | usbmuxd_device_list_free(&dev_list); | 252 | usbmuxd_device_list_free(&dev_list); |
| 244 | 253 | ||
| @@ -268,25 +277,66 @@ LIBIMOBILEDEVICE_API void idevice_set_debug_level(int level) | |||
| 268 | internal_set_debug_level(level); | 277 | internal_set_debug_level(level); |
| 269 | } | 278 | } |
| 270 | 279 | ||
| 271 | LIBIMOBILEDEVICE_API idevice_error_t idevice_new(idevice_t * device, const char *udid) | 280 | static idevice_t idevice_from_mux_device(usbmuxd_device_info_t *muxdev) |
| 281 | { | ||
| 282 | if (!muxdev) | ||
| 283 | return NULL; | ||
| 284 | |||
| 285 | idevice_t device = (idevice_t)malloc(sizeof(struct idevice_private)); | ||
| 286 | if (!device) | ||
| 287 | return NULL; | ||
| 288 | |||
| 289 | device->udid = strdup(muxdev->udid); | ||
| 290 | device->mux_id = muxdev->handle; | ||
| 291 | device->version = 0; | ||
| 292 | switch (muxdev->conn_type) { | ||
| 293 | case CONNECTION_TYPE_USB: | ||
| 294 | device->conn_type = CONNECTION_USBMUXD; | ||
| 295 | device->conn_data = NULL; | ||
| 296 | break; | ||
| 297 | case CONNECTION_TYPE_NETWORK: | ||
| 298 | device->conn_type = CONNECTION_NETWORK; | ||
| 299 | size_t len = ((uint8_t*)muxdev->conn_data)[0]; | ||
| 300 | device->conn_data = malloc(len); | ||
| 301 | memcpy(device->conn_data, muxdev->conn_data, len); | ||
| 302 | break; | ||
| 303 | default: | ||
| 304 | device->conn_type = 0; | ||
| 305 | device->conn_data = NULL; | ||
| 306 | break; | ||
| 307 | } | ||
| 308 | return device; | ||
| 309 | } | ||
| 310 | |||
| 311 | LIBIMOBILEDEVICE_API idevice_error_t idevice_new_with_options(idevice_t * device, const char *udid, enum idevice_options options) | ||
| 272 | { | 312 | { |
| 273 | usbmuxd_device_info_t muxdev; | 313 | usbmuxd_device_info_t muxdev; |
| 274 | int res = usbmuxd_get_device_by_udid(udid, &muxdev); | 314 | int usbmux_options = 0; |
| 315 | if (options & IDEVICE_LOOKUP_USBMUX) { | ||
| 316 | usbmux_options |= DEVICE_LOOKUP_USBMUX; | ||
| 317 | } | ||
| 318 | if (options & IDEVICE_LOOKUP_NETWORK) { | ||
| 319 | usbmux_options |= DEVICE_LOOKUP_NETWORK; | ||
| 320 | } | ||
| 321 | if (options & IDEVICE_LOOKUP_PREFER_NETWORK) { | ||
| 322 | usbmux_options |= DEVICE_LOOKUP_PREFER_NETWORK; | ||
| 323 | } | ||
| 324 | int res = usbmuxd_get_device(udid, &muxdev, usbmux_options); | ||
| 275 | if (res > 0) { | 325 | if (res > 0) { |
| 276 | idevice_t dev = (idevice_t) malloc(sizeof(struct idevice_private)); | 326 | *device = idevice_from_mux_device(&muxdev); |
| 277 | dev->udid = strdup(muxdev.udid); | 327 | if (!*device) { |
| 278 | dev->mux_id = muxdev.handle; | 328 | return IDEVICE_E_UNKNOWN_ERROR; |
| 279 | dev->conn_type = CONNECTION_USBMUXD; | 329 | } |
| 280 | dev->conn_data = NULL; | ||
| 281 | dev->version = 0; | ||
| 282 | *device = dev; | ||
| 283 | return IDEVICE_E_SUCCESS; | 330 | return IDEVICE_E_SUCCESS; |
| 284 | } | 331 | } |
| 285 | /* other connection types could follow here */ | ||
| 286 | |||
| 287 | return IDEVICE_E_NO_DEVICE; | 332 | return IDEVICE_E_NO_DEVICE; |
| 288 | } | 333 | } |
| 289 | 334 | ||
| 335 | LIBIMOBILEDEVICE_API idevice_error_t idevice_new(idevice_t * device, const char *udid) | ||
| 336 | { | ||
| 337 | return idevice_new_with_options(device, udid, 0); | ||
| 338 | } | ||
| 339 | |||
| 290 | LIBIMOBILEDEVICE_API idevice_error_t idevice_free(idevice_t device) | 340 | LIBIMOBILEDEVICE_API idevice_error_t idevice_free(idevice_t device) |
| 291 | { | 341 | { |
| 292 | if (!device) | 342 | if (!device) |
| @@ -310,7 +360,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connect(idevice_t device, uint16_t | |||
| 310 | return IDEVICE_E_INVALID_ARG; | 360 | return IDEVICE_E_INVALID_ARG; |
| 311 | } | 361 | } |
| 312 | 362 | ||
| 313 | if (device->conn_type == CONNECTION_USBMUXD) { | 363 | if (device->conn_type == CONNECTION_USBMUXD || device->conn_type == CONNECTION_NETWORK) { |
| 314 | int sfd = usbmuxd_connect(device->mux_id, port); | 364 | int sfd = usbmuxd_connect(device->mux_id, port); |
| 315 | if (sfd < 0) { | 365 | if (sfd < 0) { |
| 316 | debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd)); | 366 | debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd)); |
| @@ -340,7 +390,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_disconnect(idevice_connection_t con | |||
| 340 | idevice_connection_disable_ssl(connection); | 390 | idevice_connection_disable_ssl(connection); |
| 341 | } | 391 | } |
| 342 | idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; | 392 | idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; |
| 343 | if (connection->type == CONNECTION_USBMUXD) { | 393 | if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { |
| 344 | usbmuxd_disconnect((int)(long)connection->data); | 394 | usbmuxd_disconnect((int)(long)connection->data); |
| 345 | connection->data = NULL; | 395 | connection->data = NULL; |
| 346 | result = IDEVICE_E_SUCCESS; | 396 | result = IDEVICE_E_SUCCESS; |
| @@ -363,7 +413,7 @@ static idevice_error_t internal_connection_send(idevice_connection_t connection, | |||
| 363 | return IDEVICE_E_INVALID_ARG; | 413 | return IDEVICE_E_INVALID_ARG; |
| 364 | } | 414 | } |
| 365 | 415 | ||
| 366 | if (connection->type == CONNECTION_USBMUXD) { | 416 | if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { |
| 367 | int res = usbmuxd_send((int)(long)connection->data, data, len, sent_bytes); | 417 | int res = usbmuxd_send((int)(long)connection->data, data, len, sent_bytes); |
| 368 | if (res < 0) { | 418 | if (res < 0) { |
| 369 | debug_info("ERROR: usbmuxd_send returned %d (%s)", res, strerror(-res)); | 419 | debug_info("ERROR: usbmuxd_send returned %d (%s)", res, strerror(-res)); |
| @@ -434,7 +484,7 @@ static idevice_error_t internal_connection_receive_timeout(idevice_connection_t | |||
| 434 | return IDEVICE_E_INVALID_ARG; | 484 | return IDEVICE_E_INVALID_ARG; |
| 435 | } | 485 | } |
| 436 | 486 | ||
| 437 | if (connection->type == CONNECTION_USBMUXD) { | 487 | if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { |
| 438 | int conn_error = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout); | 488 | int conn_error = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout); |
| 439 | idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, *recv_bytes); | 489 | idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, *recv_bytes); |
| 440 | 490 | ||
| @@ -510,7 +560,7 @@ static idevice_error_t internal_connection_receive(idevice_connection_t connecti | |||
| 510 | return IDEVICE_E_INVALID_ARG; | 560 | return IDEVICE_E_INVALID_ARG; |
| 511 | } | 561 | } |
| 512 | 562 | ||
| 513 | if (connection->type == CONNECTION_USBMUXD) { | 563 | if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { |
| 514 | int res = usbmuxd_recv((int)(long)connection->data, data, len, recv_bytes); | 564 | int res = usbmuxd_recv((int)(long)connection->data, data, len, recv_bytes); |
| 515 | if (res < 0) { | 565 | if (res < 0) { |
| 516 | debug_info("ERROR: usbmuxd_recv returned %d (%s)", res, strerror(-res)); | 566 | debug_info("ERROR: usbmuxd_recv returned %d (%s)", res, strerror(-res)); |
| @@ -554,7 +604,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_get_fd(idevice_connectio | |||
| 554 | } | 604 | } |
| 555 | 605 | ||
| 556 | idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; | 606 | idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; |
| 557 | if (connection->type == CONNECTION_USBMUXD) { | 607 | if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { |
| 558 | *fd = (int)(long)connection->data; | 608 | *fd = (int)(long)connection->data; |
| 559 | result = IDEVICE_E_SUCCESS; | 609 | result = IDEVICE_E_SUCCESS; |
| 560 | } else { | 610 | } else { |
