~ubuntu-branches/ubuntu/wily/libimobiledevice/wily

« back to all changes in this revision

Viewing changes to src/heartbeat.c

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2013-10-28 23:01:08 UTC
  • mfrom: (1.1.9) (6.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20131028230108-5175bco54jhkv693
Tags: 1.1.5-1
* Team upload.
* Ack NMU from Andreas Metzler
* [1282e33] Imported Upstream version 1.1.5 (Closes: #709369):
  - Adapt to libusbmuxd API changes (Closes: #682275)
* [27231df] Refresh or drop patches
  - 00git_ios5_support.patch: Drop, applied upstream
  - 01-libs.private.patch: Refresh
  - 02-add-missing-linking.patch: Drop, applied upstream
  - 03_ac_pkg_swig_m4_fixed.patch: Drop, no longer relevant
  - 04_libplist_DSO_linking.patch: Drop, applied upstream
  - 05_remove_gcry_need.patch: Drop, no longer relevant
* [0f497a0] Drop --host and --build arguments from configure.
  This is already applied by dh_auto_configure automatically.
* [a370ab0] Reindent build-depends and drop trailing whitespace
* [a3fffe5] Bump dh compat to 9 for buildflags
* [40725ee] Enable multi-arch
* [65d74c4] Move dh --with parameter after $@
* [596a2b7] Update command for removing *.la for multiarch path
* [979998b] Update .manpages file for new utilities
* [3c37d78] Don't ship embedded jquery.js

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * heartbeat.c 
 
3
 * com.apple.mobile.heartbeat service implementation.
 
4
 * 
 
5
 * Copyright (c) 2013 Martin Szulecki All Rights Reserved.
 
6
 *
 
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.
 
11
 * 
 
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.
 
16
 * 
 
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 
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
#include <string.h>
 
26
#include <stdlib.h>
 
27
#include <plist/plist.h>
 
28
 
 
29
#include "heartbeat.h"
 
30
#include "lockdown.h"
 
31
#include "debug.h"
 
32
 
 
33
/**
 
34
 * Convert a property_list_service_error_t value to a heartbeat_error_t value.
 
35
 * Used internally to get correct error codes.
 
36
 *
 
37
 * @param err An property_list_service_error_t error code
 
38
 *
 
39
 * @return A matching heartbeat_error_t error code,
 
40
 *     HEARTBEAT_E_UNKNOWN_ERROR otherwise.
 
41
 */
 
42
static heartbeat_error_t heartbeat_error(property_list_service_error_t err)
 
43
{
 
44
        switch (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;
 
55
                default:
 
56
                        break;
 
57
        }
 
58
        return HEARTBEAT_E_UNKNOWN_ERROR;
 
59
}
 
60
 
 
61
/**
 
62
 * Connects to the heartbeat service on the specified device.
 
63
 *
 
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.
 
69
 *
 
70
 * @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when
 
71
 *     client is NULL, or an HEARTBEAT_E_* error code otherwise.
 
72
 */
 
73
heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t service, heartbeat_client_t * client)
 
74
{
 
75
        *client = NULL;
 
76
 
 
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;
 
80
        }
 
81
 
 
82
        debug_info("Creating heartbeat_client, port = %d.", service->port);
 
83
 
 
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);
 
88
                return ret;
 
89
        }
 
90
 
 
91
        heartbeat_client_t client_loc = (heartbeat_client_t) malloc(sizeof(struct heartbeat_client_private));
 
92
        client_loc->parent = plclient;
 
93
 
 
94
        *client = client_loc;
 
95
 
 
96
        debug_info("heartbeat_client successfully created.");
 
97
        return 0;
 
98
}
 
99
 
 
100
/**
 
101
 * Starts a new heartbeat service on the specified device and connects to it.
 
102
 *
 
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.
 
109
 *
 
110
 * @return HEARTBEAT_E_SUCCESS on success, or an HEARTBEAT_E_* error
 
111
 *     code otherwise.
 
112
 */
 
113
heartbeat_error_t heartbeat_client_start_service(idevice_t device, heartbeat_client_t * client, const char* label)
 
114
{
 
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);
 
117
        return err;
 
118
}
 
119
 
 
120
/**
 
121
 * Disconnects a heartbeat client from the device and frees up the
 
122
 * heartbeat client data.
 
123
 *
 
124
 * @param client The heartbeat client to disconnect and free.
 
125
 *
 
126
 * @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when
 
127
 *     client is NULL, or an HEARTBEAT_E_* error code otherwise.
 
128
 */
 
129
heartbeat_error_t heartbeat_client_free(heartbeat_client_t client)
 
130
{
 
131
        if (!client)
 
132
                return HEARTBEAT_E_INVALID_ARG;
 
133
 
 
134
        heartbeat_error_t err = heartbeat_error(property_list_service_client_free(client->parent));
 
135
        free(client);
 
136
 
 
137
        return err;
 
138
}
 
139
 
 
140
/**
 
141
 * Sends a plist to the service.
 
142
 *
 
143
 * @param client The heartbeat client
 
144
 * @param plist The plist to send
 
145
 *
 
146
 * @return DIAGNOSTICS_RELAY_E_SUCCESS on success,
 
147
 *  DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL
 
148
 */
 
149
heartbeat_error_t heartbeat_send(heartbeat_client_t client, plist_t plist)
 
150
{
 
151
        heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR;
 
152
 
 
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);
 
156
                return res;
 
157
        }
 
158
 
 
159
        debug_plist(plist);
 
160
 
 
161
        return res;
 
162
}
 
163
 
 
164
/**
 
165
 * Receives a plist from the service.
 
166
 *
 
167
 * @param client The heartbeat client
 
168
 * @param plist The plist to store the received data
 
169
 *
 
170
 * @return DIAGNOSTICS_RELAY_E_SUCCESS on success,
 
171
 *  DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL
 
172
 */
 
173
heartbeat_error_t heartbeat_receive(heartbeat_client_t client, plist_t * plist)
 
174
{
 
175
        return heartbeat_receive_with_timeout(client, plist, 1000);
 
176
}
 
177
 
 
178
/**
 
179
 * Receives a plist using the given heartbeat client.
 
180
 *
 
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.
 
185
 *
 
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.
 
192
 */
 
193
heartbeat_error_t heartbeat_receive_with_timeout(heartbeat_client_t client, plist_t * plist, uint32_t timeout_ms)
 
194
{
 
195
        heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR;
 
196
        plist_t outplist = NULL;
 
197
 
 
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;
 
203
        }
 
204
 
 
205
        *plist = outplist;
 
206
 
 
207
        debug_plist(*plist);
 
208
 
 
209
        return res;
 
210
}