3
* com.apple.mobile.heartbeat service implementation.
5
* Copyright (c) 2013 Martin Szulecki All Rights Reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
#include <plist/plist.h>
29
#include "heartbeat.h"
34
* Convert a property_list_service_error_t value to a heartbeat_error_t value.
35
* Used internally to get correct error codes.
37
* @param err An property_list_service_error_t error code
39
* @return A matching heartbeat_error_t error code,
40
* HEARTBEAT_E_UNKNOWN_ERROR otherwise.
42
static heartbeat_error_t heartbeat_error(property_list_service_error_t err)
45
case PROPERTY_LIST_SERVICE_E_SUCCESS:
46
return HEARTBEAT_E_SUCCESS;
47
case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
48
return HEARTBEAT_E_INVALID_ARG;
49
case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
50
return HEARTBEAT_E_PLIST_ERROR;
51
case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
52
return HEARTBEAT_E_MUX_ERROR;
53
case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
54
return HEARTBEAT_E_SSL_ERROR;
58
return HEARTBEAT_E_UNKNOWN_ERROR;
62
* Connects to the heartbeat service on the specified device.
64
* @param device The device to connect to.
65
* @param service The service descriptor returned by lockdownd_start_service.
66
* @param client Pointer that will point to a newly allocated
67
* heartbeat_client_t upon successful return. Must be freed using
68
* heartbeat_client_free() after use.
70
* @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when
71
* client is NULL, or an HEARTBEAT_E_* error code otherwise.
73
heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t service, heartbeat_client_t * client)
77
if (!device || !service || service->port == 0 || !client || *client) {
78
debug_info("Incorrect parameter passed to heartbeat_client_new.");
79
return HEARTBEAT_E_INVALID_ARG;
82
debug_info("Creating heartbeat_client, port = %d.", service->port);
84
property_list_service_client_t plclient = NULL;
85
heartbeat_error_t ret = heartbeat_error(property_list_service_client_new(device, service, &plclient));
86
if (ret != HEARTBEAT_E_SUCCESS) {
87
debug_info("Creating a property list client failed. Error: %i", ret);
91
heartbeat_client_t client_loc = (heartbeat_client_t) malloc(sizeof(struct heartbeat_client_private));
92
client_loc->parent = plclient;
96
debug_info("heartbeat_client successfully created.");
101
* Starts a new heartbeat service on the specified device and connects to it.
103
* @param device The device to connect to.
104
* @param client Pointer that will point to a newly allocated
105
* heartbeat_client_t upon successful return. Must be freed using
106
* heartbeat_client_free() after use.
107
* @param label The label to use for communication. Usually the program name.
108
* Pass NULL to disable sending the label in requests to lockdownd.
110
* @return HEARTBEAT_E_SUCCESS on success, or an HEARTBEAT_E_* error
113
heartbeat_error_t heartbeat_client_start_service(idevice_t device, heartbeat_client_t * client, const char* label)
115
heartbeat_error_t err = HEARTBEAT_E_UNKNOWN_ERROR;
116
service_client_factory_start_service(device, HEARTBEAT_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(heartbeat_client_new), &err);
121
* Disconnects a heartbeat client from the device and frees up the
122
* heartbeat client data.
124
* @param client The heartbeat client to disconnect and free.
126
* @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when
127
* client is NULL, or an HEARTBEAT_E_* error code otherwise.
129
heartbeat_error_t heartbeat_client_free(heartbeat_client_t client)
132
return HEARTBEAT_E_INVALID_ARG;
134
heartbeat_error_t err = heartbeat_error(property_list_service_client_free(client->parent));
141
* Sends a plist to the service.
143
* @param client The heartbeat client
144
* @param plist The plist to send
146
* @return DIAGNOSTICS_RELAY_E_SUCCESS on success,
147
* DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL
149
heartbeat_error_t heartbeat_send(heartbeat_client_t client, plist_t plist)
151
heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR;
153
res = heartbeat_error(property_list_service_send_binary_plist(client->parent, plist));
154
if (res != HEARTBEAT_E_SUCCESS) {
155
debug_info("Sending plist failed with error %d", res);
165
* Receives a plist from the service.
167
* @param client The heartbeat client
168
* @param plist The plist to store the received data
170
* @return DIAGNOSTICS_RELAY_E_SUCCESS on success,
171
* DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL
173
heartbeat_error_t heartbeat_receive(heartbeat_client_t client, plist_t * plist)
175
return heartbeat_receive_with_timeout(client, plist, 1000);
179
* Receives a plist using the given heartbeat client.
181
* @param client The heartbeat client to use for receiving
182
* @param plist pointer to a plist_t that will point to the received plist
183
* upon successful return
184
* @param timeout Maximum time in milliseconds to wait for data.
186
* @return HEARTBEAT_E_SUCCESS on success,
187
* HEARTBEAT_E_INVALID_ARG when client or *plist is NULL,
188
* HEARTBEAT_E_PLIST_ERROR when the received data cannot be
189
* converted to a plist, HEARTBEAT_E_MUX_ERROR when a
190
* communication error occurs, or HEARTBEAT_E_UNKNOWN_ERROR
191
* when an unspecified error occurs.
193
heartbeat_error_t heartbeat_receive_with_timeout(heartbeat_client_t client, plist_t * plist, uint32_t timeout_ms)
195
heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR;
196
plist_t outplist = NULL;
198
res = heartbeat_error(property_list_service_receive_plist_with_timeout(client->parent, &outplist, timeout_ms));
199
if (res != HEARTBEAT_E_SUCCESS || !outplist) {
200
debug_info("Could not receive plist, error %d", res);
201
plist_free(outplist);
202
return HEARTBEAT_E_MUX_ERROR;