diff --git a/ffi/examples/CMakeLists.txt b/ffi/examples/CMakeLists.txt new file mode 100644 index 0000000..d9ea0ae --- /dev/null +++ b/ffi/examples/CMakeLists.txt @@ -0,0 +1,45 @@ +# Jackson Coxson + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +cmake_minimum_required(VERSION 3.10) +project(IdeviceFFI C) + +# Set the paths +set(HEADER_FILE ${CMAKE_SOURCE_DIR}/../idevice.h) +set(STATIC_LIB ${CMAKE_SOURCE_DIR}/target/release/libidevice_ffi.a) +set(EXAMPLES_DIR ${CMAKE_SOURCE_DIR}/../examples) + +# Find all C example files +file(GLOB EXAMPLE_SOURCES ${EXAMPLES_DIR}/*.c) +find_package(OpenSSL REQUIRED) + +# Create an executable for each example file +foreach(EXAMPLE_FILE ${EXAMPLE_SOURCES}) + # Extract the filename without the path + get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE) + + # Create an executable for this example + add_executable(${EXAMPLE_NAME} ${EXAMPLE_FILE}) + + # Include the generated header + target_include_directories(${EXAMPLE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/..) + + # Link the static Rust library + target_link_libraries(${EXAMPLE_NAME} PRIVATE ${STATIC_LIB}) + + # Link OpenSSL + target_link_libraries(${EXAMPLE_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto) + + # Bulk-link common macOS system frameworks + if(APPLE) + target_link_libraries(${EXAMPLE_NAME} PRIVATE + "-framework CoreFoundation" + "-framework Security" + "-framework SystemConfiguration" + "-framework CoreServices" + "-framework IOKit" + "-framework CFNetwork" + ) + endif() +endforeach() + diff --git a/ffi/examples/connect.c b/ffi/examples/connect.c new file mode 100644 index 0000000..8574e78 --- /dev/null +++ b/ffi/examples/connect.c @@ -0,0 +1,49 @@ +// Jackson Coxson + +#include "idevice.h" +#include +#include +#include +#include + +int main() { + // Create the socket address (IPv4 example) + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(LOCKDOWN_PORT); + inet_pton(AF_INET, "10.7.0.2", &addr.sin_addr); // Replace with actual IP + + // Allocate device handle + IdeviceHandle *idevice = NULL; + + // Call the Rust function to connect + IdeviceErrorCode err = idevice_new_tcp_socket( + (struct sockaddr *)&addr, sizeof(addr), "TestDevice", &idevice); + + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to connect to device: %d\n", err); + return 1; + } + + printf("Connected to device successfully!\n"); + + // Get device type + char *device_type = NULL; + err = idevice_get_type(idevice, &device_type); + + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to get device type: %d\n", err); + return 1; + } + + printf("Service Type: %s\n", device_type); + + // Free the string + idevice_string_free(device_type); + + // Close the device connection + idevice_free(idevice); + + return 0; +}