void install_app(struct am_device *device) { connect_to_device(device); CFURLRef local_app_url = get_absolute_file_url(command.app_path); CFStringRef keys[] = { CFSTR("PackageType") }, values[] = { CFSTR("Developer") }; CFDictionaryRef options = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // copy .app to device ASSERT_OR_EXIT(!AMDeviceSecureTransferPath(0, device, local_app_url, options, NULL, 0), "!AMDeviceSecureTransferPath\n"); // install package on device ASSERT_OR_EXIT(!AMDeviceSecureInstallApplication(0, device, local_app_url, options, NULL, 0), "!AMDeviceSecureInstallApplication\n"); CFRelease(options); CFRelease(local_app_url); printf("OK\n"); unregister_device_notification(0); }
void handle_device(AMDeviceRef device) { if (found_device) return; // handle one device only CFStringRef found_device_id = AMDeviceCopyDeviceIdentifier(device); if (device_id != NULL) { if(strcmp(device_id, CFStringGetCStringPtr(found_device_id, CFStringGetSystemEncoding())) == 0) { found_device = true; } else { return; } } else { found_device = true; } CFRetain(device); // don't know if this is necessary? printf("[ 0%%] Found device (%s), beginning install\n", CFStringGetCStringPtr(found_device_id, CFStringGetSystemEncoding())); AMDeviceConnect(device); assert(AMDeviceIsPaired(device)); assert(AMDeviceValidatePairing(device) == 0); assert(AMDeviceStartSession(device) == 0); CFStringRef path = CFStringCreateWithCString(NULL, app_path, kCFStringEncodingASCII); CFURLRef relative_url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false); CFURLRef url = CFURLCopyAbsoluteURL(relative_url); CFRelease(path); CFRelease(relative_url); CFStringRef keys[] = { CFSTR("PackageType") }; CFStringRef values[] = { CFSTR("Developer") }; CFDictionaryRef options = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); mach_error_t transfer_error = AMDeviceSecureTransferPath(0, device, url, options, &transfer_callback, 0); if (transfer_error) { printf("[ !! ] Unable to transfer package to device. (%x)\n", transfer_error); exit(1); } mach_error_t install_error = AMDeviceSecureInstallApplication(0, device, url, options, &install_callback, 0); if (install_error) { printf("[ !! ] Unable to install package. (%x)\n", install_error); exit(1); } CFRelease(options); printf("[100%%] Installed package %s\n", app_path); if (!debug) exit(0); // no debug phase printf("------ Debug phase ------\n"); mount_developer_image(device); // put debugserver on the device start_remote_debug_server(device); // start debugserver write_gdb_prep_cmds(device, url); // dump the necessary gdb commands into a file CFRelease(url); printf("[100%%] Connecting to remote debug server\n"); printf("-------------------------\n"); pid_t parent = getpid(); int pid = fork(); if (pid == 0) { system(GDB_SHELL); // launch gdb kill(parent, SIGTERM); // "No. I am your father." _exit(0); } }
void handle_device(AMDeviceRef device) { if (found_device) return; // handle one device only CFStringRef found_device_id = AMDeviceCopyDeviceIdentifier(device); PRINT ("found device id\n"); if (device_id != NULL) { if(strcmp(device_id, CFStringGetCStringPtr(found_device_id, CFStringGetSystemEncoding())) == 0) { found_device = true; } else { return; } } else { if (operation == OP_LIST_DEVICES) { printf ("%s\n", CFStringGetCStringPtr(found_device_id, CFStringGetSystemEncoding())); CFRetain(device); // don't know if this is necessary? return; } found_device = true; } CFRetain(device); // don't know if this is necessary? PRINT("[ 0%%] Found device (%s), beginning install\n", CFStringGetCStringPtr(found_device_id, CFStringGetSystemEncoding())); AMDeviceConnect(device); assert(AMDeviceIsPaired(device)); assert(AMDeviceValidatePairing(device) == 0); assert(AMDeviceStartSession(device) == 0); CFStringRef path = CFStringCreateWithCString(NULL, app_path, kCFStringEncodingASCII); CFURLRef relative_url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false); CFURLRef url = CFURLCopyAbsoluteURL(relative_url); CFRelease(relative_url); int afcFd; int startServiceAFCRetval = AMDeviceStartService(device, CFSTR("com.apple.afc"), (service_conn_t *) &afcFd, NULL); printf("trying to start com.apple.afc : %d\n", startServiceAFCRetval); if( startServiceAFCRetval ) { sleep(1); //printf("trying to start com.apple.afc\n"); startServiceAFCRetval = AMDeviceStartService(device, CFSTR("com.apple.afc"), (service_conn_t *) &afcFd, NULL); } printf("trying to start com.apple.afc : %d\n", startServiceAFCRetval); assert(startServiceAFCRetval == 0); assert(AMDeviceStopSession(device) == 0); assert(AMDeviceDisconnect(device) == 0); if (operation == OP_INSTALL) { assert(AMDeviceTransferApplication(afcFd, path, NULL, transfer_callback, NULL) == 0); close(afcFd); } CFStringRef keys[] = { CFSTR("PackageType") }; CFStringRef values[] = { CFSTR("Developer") }; CFDictionaryRef options = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); AMDeviceConnect(device); assert(AMDeviceIsPaired(device)); assert(AMDeviceValidatePairing(device) == 0); assert(AMDeviceStartSession(device) == 0); int installFd; assert(AMDeviceStartService(device, CFSTR("com.apple.mobile.installation_proxy"), (service_conn_t *) &installFd, NULL) == 0); //assert(AMDeviceStopSession(device) == 0); //assert(AMDeviceDisconnect(device) == 0); if (operation == OP_INSTALL) { mach_error_t result = AMDeviceSecureInstallApplication(0, device, url, options, &operation_callback, 0); //mach_error_t result = AMDeviceInstallApplication(installFd, path, options, operation_callback, NULL); if (result != 0) { PRINT("AMDeviceInstallApplication failed: %d\n", result); exit(EXIT_FAILURE); } } else if (operation == OP_UNINSTALL) { mach_error_t result = AMDeviceUninstallApplication (installFd, path, NULL, operation_callback, NULL); if (result != 0) { PRINT("AMDeviceUninstallApplication failed: %d\n", result); exit(EXIT_FAILURE); } } assert(AMDeviceStopSession(device) == 0); assert(AMDeviceDisconnect(device) == 0); close(installFd); CFRelease(path); CFRelease(options); if (operation == OP_INSTALL) PRINT("[100%%] Installed package %s\n", app_path); else if (operation == OP_UNINSTALL) PRINT("[100%%] Uninstalled package %s\n", app_path); if (!debug) exit(EXIT_SUCCESS); // no debug phase AMDeviceConnect(device); assert(AMDeviceIsPaired(device)); assert(AMDeviceValidatePairing(device) == 0); assert(AMDeviceStartSession(device) == 0); PRINT("------ Debug phase ------\n"); mount_developer_image(device); // put debugserver on the device start_remote_debug_server(device); // start debugserver write_gdb_prep_cmds(device, url); // dump the necessary gdb commands into a file CFRelease(url); PRINT("[100%%] Connecting to remote debug server\n"); PRINT("-------------------------\n"); signal(SIGHUP, gdb_ready_handler); pid_t parent = getpid(); int pid = fork(); if (pid == 0) { system(GDB_SHELL); // launch gdb kill(parent, SIGHUP); // "No. I am your father." _exit(EXIT_SUCCESS); } }