summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libusbmuxd/libusbmuxd.c89
1 files changed, 59 insertions, 30 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c
index db8d1d4..f5e5d1b 100644
--- a/libusbmuxd/libusbmuxd.c
+++ b/libusbmuxd/libusbmuxd.c
@@ -46,6 +46,8 @@ static usbmuxd_event_cb_t event_cb = NULL;
46pthread_t devmon; 46pthread_t devmon;
47static int listenfd = -1; 47static int listenfd = -1;
48 48
49static int use_tag = 0;
50
49/** 51/**
50 * Finds a device info record by its handle. 52 * Finds a device info record by its handle.
51 * if the record is not found, NULL is returned. 53 * if the record is not found, NULL is returned.
@@ -152,6 +154,53 @@ static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t * result)
152 return -EPROTO; 154 return -EPROTO;
153} 155}
154 156
157static int send_packet(int sfd, uint32_t message, uint32_t tag, void *payload, uint32_t payload_size)
158{
159 struct usbmuxd_header header;
160
161 header.length = sizeof(struct usbmuxd_header);
162 header.version = USBMUXD_PROTOCOL_VERSION;
163 header.message = message;
164 header.tag = tag;
165 if (payload && (payload_size > 0)) {
166 header.length += payload_size;
167 }
168 int sent = send_buf(sfd, &header, sizeof(header));
169 if (sent != sizeof(header)) {
170 fprintf(stderr, "%s: ERROR: could not send packet header\n", __func__);
171 return -1;
172 }
173 if (payload && (payload_size > 0)) {
174 sent += send_buf(sfd, payload, payload_size);
175 }
176 if (sent != (int)header.length) {
177 fprintf(stderr, "%s: ERROR: could not send whole packet\n", __func__);
178 close(sfd);
179 return -1;
180 }
181 return sent;
182}
183
184static int send_listen_packet(int sfd, uint32_t tag)
185{
186 return send_packet(sfd, MESSAGE_LISTEN, tag, NULL, 0);
187}
188
189static int send_connect_packet(int sfd, uint32_t tag, uint32_t device_id, uint16_t port)
190{
191 struct {
192 uint32_t device_id;
193 uint16_t port;
194 uint16_t reserved;
195 } conninfo;
196
197 conninfo.device_id = device_id;
198 conninfo.port = htons(port);
199 conninfo.reserved = 0;
200
201 return send_packet(sfd, MESSAGE_CONNECT, tag, &conninfo, sizeof(conninfo));
202
203}
155 204
156/** 205/**
157 * Generates an event, i.e. calls the callback function. 206 * Generates an event, i.e. calls the callback function.
@@ -181,12 +230,6 @@ static int usbmuxd_listen()
181{ 230{
182 int sfd; 231 int sfd;
183 uint32_t res = -1; 232 uint32_t res = -1;
184 struct usbmuxd_listen_request req;
185
186 req.header.length = sizeof(struct usbmuxd_listen_request);
187 req.header.version = USBMUXD_PROTOCOL_VERSION;
188 req.header.message = MESSAGE_LISTEN;
189 req.header.tag = 2;
190 233
191 sfd = connect_usbmuxd_socket(); 234 sfd = connect_usbmuxd_socket();
192 if (sfd < 0) { 235 if (sfd < 0) {
@@ -203,12 +246,13 @@ static int usbmuxd_listen()
203 return sfd; 246 return sfd;
204 } 247 }
205 248
206 if (send_buf(sfd, &req, req.header.length) != (int)req.header.length) { 249 use_tag++;
250 if (send_listen_packet(sfd, use_tag) <= 0) {
207 fprintf(stderr, "%s: ERROR: could not send listen packet\n", __func__); 251 fprintf(stderr, "%s: ERROR: could not send listen packet\n", __func__);
208 close(sfd); 252 close(sfd);
209 return -1; 253 return -1;
210 } 254 }
211 if (usbmuxd_get_result(sfd, req.header.tag, &res) && (res != 0)) { 255 if (usbmuxd_get_result(sfd, use_tag, &res) && (res != 0)) {
212 close(sfd); 256 close(sfd);
213 fprintf(stderr, "%s: ERROR: did not get OK but %d\n", __func__, res); 257 fprintf(stderr, "%s: ERROR: did not get OK but %d\n", __func__, res);
214 return -1; 258 return -1;
@@ -343,7 +387,6 @@ int usbmuxd_unsubscribe()
343 387
344int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list) 388int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list)
345{ 389{
346 struct usbmuxd_listen_request s_req;
347 int sfd; 390 int sfd;
348 int listen_success = 0; 391 int listen_success = 0;
349 uint32_t res; 392 uint32_t res;
@@ -359,17 +402,11 @@ int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list)
359 return sfd; 402 return sfd;
360 } 403 }
361 404
362 s_req.header.length = sizeof(struct usbmuxd_listen_request); 405 use_tag++;
363 s_req.header.version = USBMUXD_PROTOCOL_VERSION; 406 if (send_listen_packet(sfd, use_tag) > 0) {
364 s_req.header.message = MESSAGE_LISTEN;
365 s_req.header.tag = 2;
366
367 // send scan request packet
368 if (send_buf(sfd, &s_req, s_req.header.length) ==
369 (int) s_req.header.length) {
370 res = -1; 407 res = -1;
371 // get response 408 // get response
372 if (usbmuxd_get_result(sfd, s_req.header.tag, &res) && (res == 0)) { 409 if (usbmuxd_get_result(sfd, use_tag, &res) && (res == 0)) {
373 listen_success = 1; 410 listen_success = 1;
374 } else { 411 } else {
375 close(sfd); 412 close(sfd);
@@ -481,7 +518,6 @@ int usbmuxd_get_device_by_uuid(const char *uuid, usbmuxd_device_info_t *device)
481int usbmuxd_connect(const int handle, const unsigned short port) 518int usbmuxd_connect(const int handle, const unsigned short port)
482{ 519{
483 int sfd; 520 int sfd;
484 struct usbmuxd_connect_request c_req;
485 int connected = 0; 521 int connected = 0;
486 uint32_t res = -1; 522 uint32_t res = -1;
487 523
@@ -492,20 +528,13 @@ int usbmuxd_connect(const int handle, const unsigned short port)
492 return sfd; 528 return sfd;
493 } 529 }
494 530
495 c_req.header.length = sizeof(c_req); 531 use_tag++;
496 c_req.header.version = USBMUXD_PROTOCOL_VERSION; 532 if (send_connect_packet(sfd, use_tag, (uint32_t)handle, (uint16_t)port) <= 0) {
497 c_req.header.message = MESSAGE_CONNECT; 533 fprintf(stderr, "%s: Error sending connect message!\n", __func__);
498 c_req.header.tag = 3;
499 c_req.device_id = (uint32_t) handle;
500 c_req.port = htons(port);
501 c_req.reserved = 0;
502
503 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) {
504 perror("send");
505 } else { 534 } else {
506 // read ACK 535 // read ACK
507 //fprintf(stderr, "%s: Reading connect result...\n", __func__); 536 //fprintf(stderr, "%s: Reading connect result...\n", __func__);
508 if (usbmuxd_get_result(sfd, c_req.header.tag, &res)) { 537 if (usbmuxd_get_result(sfd, use_tag, &res)) {
509 if (res == 0) { 538 if (res == 0) {
510 //fprintf(stderr, "%s: Connect success!\n", __func__); 539 //fprintf(stderr, "%s: Connect success!\n", __func__);
511 connected = 1; 540 connected = 1;