summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2009-07-25 02:08:12 +0200
committerGravatar Martin Szulecki2009-07-25 02:08:12 +0200
commit40a0abaa1c7f2196d40276b0689b18a04e468acd (patch)
treeaee97118e637ccc7b8ab638a77a154b6bbed4f4b
parent66695e6b7db9457d5ecfe6f4f6624dc195e5274d (diff)
downloadlibimobiledevice-40a0abaa1c7f2196d40276b0689b18a04e468acd.tar.gz
libimobiledevice-40a0abaa1c7f2196d40276b0689b18a04e468acd.tar.bz2
Update MobileSync API and introduce mobilesync error codes
-rw-r--r--dev/msyncclient.c12
-rw-r--r--include/libiphone/mobilesync.h20
-rw-r--r--src/MobileSync.c51
-rw-r--r--src/MobileSync.h4
4 files changed, 48 insertions, 39 deletions
diff --git a/dev/msyncclient.c b/dev/msyncclient.c
index 5eb105d..7651002 100644
--- a/dev/msyncclient.c
+++ b/dev/msyncclient.c
@@ -28,12 +28,12 @@
#include <libiphone/lockdown.h>
#include <libiphone/mobilesync.h>
-static iphone_error_t mobilesync_get_all_contacts(mobilesync_client_t client)
+static mobilesync_error_t mobilesync_get_all_contacts(mobilesync_client_t client)
{
if (!client)
- return IPHONE_E_INVALID_ARG;
+ return MOBILESYNC_E_INVALID_ARG;
- iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
+ mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR;
plist_t array = NULL;
array = plist_new_array();
@@ -124,7 +124,6 @@ static iphone_error_t mobilesync_get_all_contacts(mobilesync_client_t client)
plist_free(array);
array = NULL;
-
return ret;
}
@@ -137,7 +136,6 @@ int main(int argc, char *argv[])
if (argc > 1 && !strcasecmp(argv[1], "--debug"))
iphone_set_debug_mask(DBGMASK_MOBILESYNC);
-
if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
printf("No iPhone found, is it plugged in?\n");
return -1;
@@ -152,10 +150,10 @@ int main(int argc, char *argv[])
if (port) {
mobilesync_client_t msync = NULL;
- mobilesync_new_client(phone, port, &msync);
+ mobilesync_client_new(phone, port, &msync);
if (msync) {
mobilesync_get_all_contacts(msync);
- mobilesync_free_client(msync);
+ mobilesync_client_free(msync);
}
} else {
printf("Start service failure.\n");
diff --git a/include/libiphone/mobilesync.h b/include/libiphone/mobilesync.h
index 128be45..f8fa711 100644
--- a/include/libiphone/mobilesync.h
+++ b/include/libiphone/mobilesync.h
@@ -27,14 +27,24 @@ extern "C" {
#include <libiphone/libiphone.h>
+/* Error Codes */
+#define MOBILESYNC_E_SUCCESS 0
+#define MOBILESYNC_E_INVALID_ARG -1
+#define MOBILESYNC_E_PLIST_ERROR -2
+#define MOBILESYNC_E_MUX_ERROR -3
+#define MOBILESYNC_E_BAD_VERSION -4
+
+#define MOBILESYNC_E_UNKNOWN_ERROR -256
+
+typedef int16_t mobilesync_error_t;
+
struct mobilesync_client_int;
typedef struct mobilesync_client_int *mobilesync_client_t;
-iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port,
- mobilesync_client_t * client);
-iphone_error_t mobilesync_free_client(mobilesync_client_t client);
-iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t *plist);
-iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist);
+mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port, mobilesync_client_t * client);
+mobilesync_error_t mobilesync_client_free(mobilesync_client_t client);
+mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t *plist);
+mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist);
#ifdef __cplusplus
}
diff --git a/src/MobileSync.c b/src/MobileSync.c
index f544400..4463251 100644
--- a/src/MobileSync.c
+++ b/src/MobileSync.c
@@ -19,25 +19,27 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "MobileSync.h"
#include <plist/plist.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
+#include "MobileSync.h"
+#include "iphone.h"
+#include "utils.h"
#define MSYNC_VERSION_INT1 100
#define MSYNC_VERSION_INT2 100
-iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port,
+mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port,
mobilesync_client_t * client)
{
if (!device || dst_port == 0 || !client || *client)
- return IPHONE_E_INVALID_ARG;
+ return MOBILESYNC_E_INVALID_ARG;
- iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
+ mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR;
- // Attempt connection
+ /* Attempt connection */
int sfd = usbmuxd_connect(device->handle, dst_port);
if (sfd < 0) {
return ret;
@@ -46,10 +48,10 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port,
mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int));
client_loc->sfd = sfd;
- //perform handshake
+ /* perform handshake */
plist_t array = NULL;
- //first receive version
+ /* first receive version */
ret = mobilesync_recv(client_loc, &array);
plist_t msg_node = plist_find_node_by_string(array, "DLMessageVersionExchange");
@@ -86,17 +88,20 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port,
plist_t rep_node = plist_find_node_by_string(array, "DLMessageDeviceReady");
if (rep_node) {
- ret = IPHONE_E_SUCCESS;
+ ret = MOBILESYNC_E_SUCCESS;
*client = client_loc;
}
+ else
+ {
+ ret = MOBILESYNC_E_BAD_VERSION;
+ }
plist_free(array);
array = NULL;
-
}
}
- if (IPHONE_E_SUCCESS != ret)
- mobilesync_free_client(client_loc);
+ if (MOBILESYNC_E_SUCCESS != ret)
+ mobilesync_client_free(client_loc);
return ret;
}
@@ -115,13 +120,13 @@ static void mobilesync_disconnect(mobilesync_client_t client)
array = NULL;
}
-iphone_error_t mobilesync_free_client(mobilesync_client_t client)
+mobilesync_error_t mobilesync_client_free(mobilesync_client_t client)
{
if (!client)
return IPHONE_E_INVALID_ARG;
mobilesync_disconnect(client);
- return usbmuxd_disconnect(client->sfd);
+ return (usbmuxd_disconnect(client->sfd) == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR);
}
/** Polls the iPhone for MobileSync data.
@@ -131,11 +136,11 @@ iphone_error_t mobilesync_free_client(mobilesync_client_t client)
*
* @return an error code
*/
-iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
+mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
{
if (!client || !plist || (plist && *plist))
- return IPHONE_E_INVALID_ARG;
- iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
+ return MOBILESYNC_E_INVALID_ARG;
+ mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR;
char *receive = NULL;
uint32_t datalen = 0, bytes = 0, received_bytes = 0;
@@ -145,14 +150,14 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
receive = (char *) malloc(sizeof(char) * datalen);
/* fill buffer and request more packets if needed */
- while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) {
+ while ((received_bytes < datalen) && (ret == MOBILESYNC_E_SUCCESS)) {
ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes);
received_bytes += bytes;
}
- if (ret != IPHONE_E_SUCCESS) {
+ if (ret != MOBILESYNC_E_SUCCESS) {
free(receive);
- return ret;
+ return MOBILESYNC_E_MUX_ERROR;
}
plist_from_bin(receive, received_bytes, plist);
@@ -177,10 +182,10 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
*
* @return an error code
*/
-iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
+mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
{
if (!client || !plist)
- return IPHONE_E_INVALID_ARG;
+ return MOBILESYNC_E_INVALID_ARG;
char *XMLContent = NULL;
uint32_t length = 0;
@@ -195,7 +200,7 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
char *real_query;
int bytes;
- iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
+ mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR;
real_query = (char *) malloc(sizeof(char) * (length + 4));
length = htonl(length);
@@ -204,6 +209,6 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes);
free(real_query);
- return ret;
+ return (ret == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR);
}
diff --git a/src/MobileSync.h b/src/MobileSync.h
index 5279ce0..6347399 100644
--- a/src/MobileSync.h
+++ b/src/MobileSync.h
@@ -21,12 +21,8 @@
#ifndef MOBILESYNC_H
#define MOBILESYNC_H
-#include "iphone.h"
-#include "utils.h"
#include "libiphone/mobilesync.h"
-#include <plist/plist.h>
-
struct mobilesync_client_int {
int sfd;
};