diff options
-rw-r--r-- | src/heartbeat.c | 33 | ||||
-rw-r--r-- | src/service.c | 24 | ||||
-rw-r--r-- | src/service.h | 4 |
3 files changed, 21 insertions, 40 deletions
diff --git a/src/heartbeat.c b/src/heartbeat.c index 817c6ad..0f8d2f3 100644 --- a/src/heartbeat.c +++ b/src/heartbeat.c @@ -112,36 +112,9 @@ heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descr */ heartbeat_error_t heartbeat_client_start_service(idevice_t device, heartbeat_client_t * client, const char* label) { - *client = NULL; - - lockdownd_client_t lckd = NULL; - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, label)) { - idevice_free(device); - debug_info("Could not create a lockdown client."); - return HEARTBEAT_E_UNKNOWN_ERROR; - } - - lockdownd_service_descriptor_t service = NULL; - lockdownd_start_service(lckd, HEARTBEAT_SERVICE_NAME, &service); - lockdownd_client_free(lckd); - - if (service->port <= 0) { - debug_info("Could not start heartbeat service!"); - return HEARTBEAT_E_UNKNOWN_ERROR; - } - - heartbeat_error_t res = heartbeat_client_new(device, service, client); - if (res != HEARTBEAT_E_SUCCESS) { - debug_info("Could not connect to heartbeat! Port: %i, error: %i", service->port, res); - return res; - } - - if (service) { - lockdownd_service_descriptor_free(service); - service = NULL; - } - - return HEARTBEAT_E_SUCCESS; + heartbeat_error_t err = HEARTBEAT_E_UNKNOWN_ERROR; + service_client_factory_start_service(device, HEARTBEAT_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(heartbeat_client_new), &err); + return err; } /** diff --git a/src/service.c b/src/service.c index af37a83..738db02 100644 --- a/src/service.c +++ b/src/service.c @@ -103,7 +103,7 @@ service_error_t service_client_new(idevice_t device, lockdownd_service_descripto * @return SERVICE_E_SUCCESS on success, or a SERVICE_E_* error code * otherwise. */ -service_error_t service_client_start_service(idevice_t device, const char* service_name, service_client_t *client, const char* label) +service_error_t service_client_factory_start_service(idevice_t device, const char* service_name, void **client, const char* label, uint16_t (*constructor_func)(idevice_t, lockdownd_service_descriptor_t, void**), uint16_t *error_code) { *client = NULL; @@ -122,18 +122,24 @@ service_error_t service_client_start_service(idevice_t device, const char* servi return SERVICE_E_START_SERVICE_ERROR; } - service_error_t res = service_client_new(device, service, client); - if (res != SERVICE_E_SUCCESS) { - debug_info("Could not connect to service %s! Port: %i, error: %i", service_name, service->port, res); - return res; + uint16_t ec; + if (constructor_func) { + ec = (uint16_t)constructor_func(device, service, client); + } else { + ec = service_client_new(device, service, (service_client_t*)client); + } + if (error_code) { + *error_code = ec; } - if (service) { - lockdownd_service_descriptor_free(service); - service = NULL; + if (ec != SERVICE_E_SUCCESS) { + debug_info("Could not connect to service %s! Port: %i, error: %i", service_name, service->port, ec); } - return SERVICE_E_SUCCESS; + lockdownd_service_descriptor_free(service); + service = NULL; + + return (ec == SERVICE_E_SUCCESS) ? SERVICE_E_SUCCESS : SERVICE_E_START_SERVICE_ERROR; } /** diff --git a/src/service.h b/src/service.h index 982f96a..3a22bed 100644 --- a/src/service.h +++ b/src/service.h @@ -40,9 +40,11 @@ typedef struct service_client_private *service_client_t; typedef int16_t service_error_t; +#define SERVICE_CONSTRUCTOR(x) (uint16_t (*)(idevice_t, lockdownd_service_descriptor_t, void**))(x) + /* creation and destruction */ service_error_t service_client_new(idevice_t device, lockdownd_service_descriptor_t service, service_client_t *client); -service_error_t service_client_start_service(idevice_t device, const char* service_name, service_client_t *client, const char* label); +service_error_t service_client_factory_start_service(idevice_t device, const char* service_name, void **client, const char* label, uint16_t (*constructor_func)(idevice_t, lockdownd_service_descriptor_t, void**), uint16_t *error_code); service_error_t service_client_free(service_client_t client); /* sending */ |