Examples for FFI bindings

This commit is contained in:
Jackson Coxson
2025-03-24 15:30:26 -06:00
parent 55e7d6366f
commit f3761a5d44
2 changed files with 94 additions and 0 deletions

49
ffi/examples/connect.c Normal file
View File

@@ -0,0 +1,49 @@
// Jackson Coxson
#include "idevice.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}