summaryrefslogtreecommitdiffstats
path: root/src/AFC.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/AFC.c')
-rw-r--r--src/AFC.c68
1 files changed, 37 insertions, 31 deletions
diff --git a/src/AFC.c b/src/AFC.c
index d2761cf..ba436e7 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -68,18 +68,18 @@ afc_error_t afc_client_new(iphone_device_t device, int dst_port, afc_client_t *
68 return AFC_E_INVALID_ARGUMENT; 68 return AFC_E_INVALID_ARGUMENT;
69 69
70 /* attempt connection */ 70 /* attempt connection */
71 int sfd = usbmuxd_connect(device->handle, dst_port); 71 iphone_connection_t connection = NULL;
72 if (sfd < 0) { 72 if (iphone_device_connect(device, dst_port, &connection) != IPHONE_E_SUCCESS) {
73 return AFC_E_MUX_ERROR; 73 return AFC_E_MUX_ERROR;
74 } 74 }
75 75
76 afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_int)); 76 afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_int));
77 client_loc->sfd = sfd; 77 client_loc->connection = connection;
78 78
79 /* allocate a packet */ 79 /* allocate a packet */
80 client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); 80 client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket));
81 if (!client_loc->afc_packet) { 81 if (!client_loc->afc_packet) {
82 usbmuxd_disconnect(client_loc->sfd); 82 iphone_device_disconnect(client_loc->connection);
83 free(client_loc); 83 free(client_loc);
84 return AFC_E_NO_MEM; 84 return AFC_E_NO_MEM;
85 } 85 }
@@ -102,10 +102,10 @@ afc_error_t afc_client_new(iphone_device_t device, int dst_port, afc_client_t *
102 */ 102 */
103afc_error_t afc_client_free(afc_client_t client) 103afc_error_t afc_client_free(afc_client_t client)
104{ 104{
105 if (!client || client->sfd < 0 || !client->afc_packet) 105 if (!client || !client->connection || !client->afc_packet)
106 return AFC_E_INVALID_ARGUMENT; 106 return AFC_E_INVALID_ARGUMENT;
107 107
108 usbmuxd_disconnect(client->sfd); 108 iphone_device_disconnect(client->connection);
109 free(client->afc_packet); 109 free(client->afc_packet);
110 if (client->mutex) { 110 if (client->mutex) {
111 g_mutex_free(client->mutex); 111 g_mutex_free(client->mutex);
@@ -132,7 +132,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l
132 int bytes = 0, offset = 0; 132 int bytes = 0, offset = 0;
133 char *buffer; 133 char *buffer;
134 134
135 if (!client || client->sfd < 0 || !client->afc_packet) 135 if (!client || !client->connection || !client->afc_packet)
136 return 0; 136 return 0;
137 137
138 if (!data || !length) 138 if (!data || !length)
@@ -164,7 +164,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l
164 return -1; 164 return -1;
165 } 165 }
166 memcpy(buffer + sizeof(AFCPacket), data, offset); 166 memcpy(buffer + sizeof(AFCPacket), data, offset);
167 usbmuxd_send(client->sfd, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); 167 iphone_device_send(client->connection, buffer, client->afc_packet->this_length, (uint32_t*)&bytes);
168 free(buffer); 168 free(buffer);
169 if (bytes <= 0) { 169 if (bytes <= 0) {
170 return bytes; 170 return bytes;
@@ -175,7 +175,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l
175 log_debug_msg("%s: Buffer: \n", __func__); 175 log_debug_msg("%s: Buffer: \n", __func__);
176 log_debug_buffer(data + offset, length - offset); 176 log_debug_buffer(data + offset, length - offset);
177 177
178 usbmuxd_send(client->sfd, data + offset, length - offset, (uint32_t*)&bytes); 178 iphone_device_send(client->connection, data + offset, length - offset, (uint32_t*)&bytes);
179 return bytes; 179 return bytes;
180 } else { 180 } else {
181 log_debug_msg("%s: doin things the old way\n", __func__); 181 log_debug_msg("%s: doin things the old way\n", __func__);
@@ -188,7 +188,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l
188 } 188 }
189 log_debug_buffer(buffer, client->afc_packet->this_length); 189 log_debug_buffer(buffer, client->afc_packet->this_length);
190 log_debug_msg("\n"); 190 log_debug_msg("\n");
191 usbmuxd_send(client->sfd, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); 191 iphone_device_send(client->connection, buffer, client->afc_packet->this_length, (uint32_t*)&bytes);
192 192
193 if (buffer) { 193 if (buffer) {
194 free(buffer); 194 free(buffer);
@@ -220,7 +220,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
220 *bytes = 0; 220 *bytes = 0;
221 221
222 /* first, read the AFC header */ 222 /* first, read the AFC header */
223 usbmuxd_recv(client->sfd, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); 223 iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes);
224 if (*bytes <= 0) { 224 if (*bytes <= 0) {
225 log_debug_msg("%s: Just didn't get enough.\n", __func__); 225 log_debug_msg("%s: Just didn't get enough.\n", __func__);
226 *dump_here = NULL; 226 *dump_here = NULL;
@@ -273,7 +273,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
273 273
274 *dump_here = (char*)malloc(entire_len); 274 *dump_here = (char*)malloc(entire_len);
275 if (this_len > 0) { 275 if (this_len > 0) {
276 usbmuxd_recv(client->sfd, *dump_here, this_len, (uint32_t*)bytes); 276 iphone_device_recv(client->connection, *dump_here, this_len, (uint32_t*)bytes);
277 if (*bytes <= 0) { 277 if (*bytes <= 0) {
278 free(*dump_here); 278 free(*dump_here);
279 *dump_here = NULL; 279 *dump_here = NULL;
@@ -291,7 +291,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
291 291
292 if (entire_len > this_len) { 292 if (entire_len > this_len) {
293 while (current_count < entire_len) { 293 while (current_count < entire_len) {
294 usbmuxd_recv(client->sfd, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); 294 iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes);
295 if (*bytes <= 0) { 295 if (*bytes <= 0) {
296 log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); 296 log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes);
297 break; 297 break;
@@ -468,26 +468,32 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***infos)
468 return ret; 468 return ret;
469} 469}
470 470
471/** Get a specific field of the device info for a client connection to phone. 471/** Get a specific key of the device info list for a client connection.
472 * Known values are: Model, FSTotalBytes, FSFreeBytes and FSBlockSize. This is 472 * Known key values are: Model, FSTotalBytes, FSFreeBytes and FSBlockSize.
473 * a helper function for afc_get_device_info(). 473 * This is a helper function for afc_get_device_info().
474 * 474 *
475 * @param client The client to get device info for. 475 * @param client The client to get device info for.
476 * @param field The field to get the information for 476 * @param key The key to get the value of.
477 * @param value The value for the key if successful or NULL otherwise.
477 * 478 *
478 * @return A char * or NULL if there was an error. 479 * @return AFC_E_SUCCESS on success or an AFC_E_* error value.
479 */ 480 */
480char * afc_get_device_info_field(afc_client_t client, const char *field) 481afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char **value)
481{ 482{
482 char *ret = NULL; 483 afc_error_t ret = AFC_E_INTERNAL_ERROR;
483 char **kvps, **ptr; 484 char **kvps, **ptr;
484 485
485 if (field == NULL || afc_get_device_info(client, &kvps) != AFC_E_SUCCESS) 486 *value = NULL;
486 return NULL; 487 if (key == NULL)
488 return AFC_E_INVALID_ARGUMENT;
489
490 ret = afc_get_device_info(client, &kvps);
491 if (ret != AFC_E_SUCCESS)
492 return ret;
487 493
488 for (ptr = kvps; *ptr; ptr++) { 494 for (ptr = kvps; *ptr; ptr++) {
489 if (!strcmp(*ptr, field)) { 495 if (!strcmp(*ptr, key)) {
490 ret = strdup(*(ptr+1)); 496 *value = strdup(*(ptr+1));
491 break; 497 break;
492 } 498 }
493 } 499 }
@@ -511,7 +517,7 @@ afc_error_t afc_remove_path(afc_client_t client, const char *path)
511 int bytes; 517 int bytes;
512 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 518 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
513 519
514 if (!client || !path || !client->afc_packet || client->sfd < 0) 520 if (!client || !path || !client->afc_packet || !client->connection)
515 return AFC_E_INVALID_ARGUMENT; 521 return AFC_E_INVALID_ARGUMENT;
516 522
517 afc_lock(client); 523 afc_lock(client);
@@ -554,7 +560,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t
554 int bytes = 0; 560 int bytes = 0;
555 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 561 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
556 562
557 if (!client || !from || !to || !client->afc_packet || client->sfd < 0) 563 if (!client || !from || !to || !client->afc_packet || !client->connection)
558 return AFC_E_INVALID_ARGUMENT; 564 return AFC_E_INVALID_ARGUMENT;
559 565
560 afc_lock(client); 566 afc_lock(client);
@@ -681,7 +687,7 @@ afc_file_open(afc_client_t client, const char *filename,
681 // set handle to 0 so in case an error occurs, the handle is invalid 687 // set handle to 0 so in case an error occurs, the handle is invalid
682 *handle = 0; 688 *handle = 0;
683 689
684 if (!client || client->sfd < 0|| !client->afc_packet) 690 if (!client || !client->connection || !client->afc_packet)
685 return AFC_E_INVALID_ARGUMENT; 691 return AFC_E_INVALID_ARGUMENT;
686 692
687 afc_lock(client); 693 afc_lock(client);
@@ -736,7 +742,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint
736 const int MAXIMUM_READ_SIZE = 1 << 16; 742 const int MAXIMUM_READ_SIZE = 1 << 16;
737 afc_error_t ret = AFC_E_SUCCESS; 743 afc_error_t ret = AFC_E_SUCCESS;
738 744
739 if (!client || !client->afc_packet || client->sfd < 0 || handle == 0) 745 if (!client || !client->afc_packet || !client->connection || handle == 0)
740 return AFC_E_INVALID_ARGUMENT; 746 return AFC_E_INVALID_ARGUMENT;
741 log_debug_msg("%s: called for length %i\n", __func__, length); 747 log_debug_msg("%s: called for length %i\n", __func__, length);
742 748
@@ -813,7 +819,7 @@ afc_file_write(afc_client_t client, uint64_t handle,
813 char *out_buffer = NULL; 819 char *out_buffer = NULL;
814 afc_error_t ret = AFC_E_SUCCESS; 820 afc_error_t ret = AFC_E_SUCCESS;
815 821
816 if (!client || !client->afc_packet || client->sfd < 0 || !bytes || (handle == 0)) 822 if (!client || !client->afc_packet || !client->connection || !bytes || (handle == 0))
817 return AFC_E_INVALID_ARGUMENT; 823 return AFC_E_INVALID_ARGUMENT;
818 824
819 afc_lock(client); 825 afc_lock(client);
@@ -1133,7 +1139,7 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize)
1133 uint64_t size_requested = newsize; 1139 uint64_t size_requested = newsize;
1134 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1140 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1135 1141
1136 if (!client || !path || !client->afc_packet || client->sfd < 0) 1142 if (!client || !path || !client->afc_packet || !client->connection)
1137 return AFC_E_INVALID_ARGUMENT; 1143 return AFC_E_INVALID_ARGUMENT;
1138 1144
1139 afc_lock(client); 1145 afc_lock(client);
@@ -1177,7 +1183,7 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c
1177 uint64_t type = linktype; 1183 uint64_t type = linktype;
1178 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1184 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1179 1185
1180 if (!client || !target || !linkname || !client->afc_packet || client->sfd < 0) 1186 if (!client || !target || !linkname || !client->afc_packet || !client->connection)
1181 return AFC_E_INVALID_ARGUMENT; 1187 return AFC_E_INVALID_ARGUMENT;
1182 1188
1183 afc_lock(client); 1189 afc_lock(client);