summaryrefslogtreecommitdiffstats
path: root/libusbmuxd.c
diff options
context:
space:
mode:
Diffstat (limited to 'libusbmuxd.c')
-rw-r--r--libusbmuxd.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/libusbmuxd.c b/libusbmuxd.c
index 4e4ec10..ed331aa 100644
--- a/libusbmuxd.c
+++ b/libusbmuxd.c
@@ -7,7 +7,11 @@
7#include <arpa/inet.h> 7#include <arpa/inet.h>
8#include <unistd.h> 8#include <unistd.h>
9 9
10// usbmuxd public interface
11#include <libusbmuxd.h>
12// usbmuxd protocol
10#include <usbmuxd.h> 13#include <usbmuxd.h>
14// socket utility functions
11#include "sock_stuff.h" 15#include "sock_stuff.h"
12 16
13static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) 17static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
@@ -40,7 +44,7 @@ static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
40 return -1; 44 return -1;
41} 45}
42 46
43int usbmuxd_scan(usbmuxd_device_t **devices) 47int usbmuxd_scan(usbmuxd_scan_result **available_devices)
44{ 48{
45 struct usbmuxd_scan_request s_req; 49 struct usbmuxd_scan_request s_req;
46 int sfd; 50 int sfd;
@@ -48,7 +52,7 @@ int usbmuxd_scan(usbmuxd_device_t **devices)
48 uint32_t res; 52 uint32_t res;
49 uint32_t pktlen; 53 uint32_t pktlen;
50 int recv_len; 54 int recv_len;
51 usbmuxd_device_t *newlist = NULL; 55 usbmuxd_scan_result *newlist = NULL;
52 struct usbmuxd_device_info_record dev_info_pkt; 56 struct usbmuxd_device_info_record dev_info_pkt;
53 int dev_cnt = 0; 57 int dev_cnt = 0;
54 58
@@ -81,7 +85,7 @@ int usbmuxd_scan(usbmuxd_device_t **devices)
81 return -1; 85 return -1;
82 } 86 }
83 87
84 *devices = NULL; 88 *available_devices = NULL;
85 // receive device list 89 // receive device list
86 while (1) { 90 while (1) {
87 if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 500) == 4) { 91 if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 500) == 4) {
@@ -99,10 +103,14 @@ int usbmuxd_scan(usbmuxd_device_t **devices)
99 fprintf(stderr, "%s: received less data than specified in header!\n", __func__); 103 fprintf(stderr, "%s: received less data than specified in header!\n", __func__);
100 } else { 104 } else {
101 //fprintf(stderr, "%s: got device record with id %d, UUID=%s\n", __func__, dev_info_pkt.device_info.device_id, dev_info_pkt.device_info.serial_number); 105 //fprintf(stderr, "%s: got device record with id %d, UUID=%s\n", __func__, dev_info_pkt.device_info.device_id, dev_info_pkt.device_info.serial_number);
102 newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); 106 newlist = (usbmuxd_scan_result *)realloc(*available_devices, sizeof(usbmuxd_scan_result) * (dev_cnt+1));
103 if (newlist) { 107 if (newlist) {
104 memcpy(newlist+dev_cnt, &dev_info_pkt.device, sizeof(usbmuxd_device_t)); 108 newlist[dev_cnt].handle = (int)dev_info_pkt.device.device_id;
105 *devices = newlist; 109 newlist[dev_cnt].product_id = dev_info_pkt.device.product_id;
110 memset(newlist[dev_cnt].serial_number, '\0', sizeof(newlist[dev_cnt].serial_number));
111 memcpy(newlist[dev_cnt].serial_number, dev_info_pkt.device.serial_number,
112 sizeof(dev_info_pkt.device.serial_number));
113 *available_devices = newlist;
106 dev_cnt++; 114 dev_cnt++;
107 } else { 115 } else {
108 fprintf(stderr, "%s: ERROR: out of memory when trying to realloc!\n", __func__); 116 fprintf(stderr, "%s: ERROR: out of memory when trying to realloc!\n", __func__);
@@ -117,14 +125,14 @@ int usbmuxd_scan(usbmuxd_device_t **devices)
117 } 125 }
118 126
119 // terminating zero record 127 // terminating zero record
120 newlist = (usbmuxd_device_t*)realloc(*devices, sizeof(usbmuxd_device_t) * (dev_cnt+1)); 128 newlist = (usbmuxd_scan_result *)realloc(*available_devices, sizeof(usbmuxd_scan_result) * (dev_cnt+1));
121 memset(newlist+dev_cnt, 0, sizeof(usbmuxd_device_t)); 129 memset(newlist+dev_cnt, 0, sizeof(usbmuxd_scan_result));
122 *devices = newlist; 130 *available_devices = newlist;
123 131
124 return 0; 132 return dev_cnt;
125} 133}
126 134
127int usbmuxd_connect(uint32_t device_id, uint16_t port) 135int usbmuxd_connect(const int handle, const unsigned short tcp_port)
128{ 136{
129 int sfd; 137 int sfd;
130 struct usbmuxd_connect_request c_req; 138 struct usbmuxd_connect_request c_req;
@@ -141,8 +149,8 @@ int usbmuxd_connect(uint32_t device_id, uint16_t port)
141 c_req.header.reserved = 0; 149 c_req.header.reserved = 0;
142 c_req.header.type = USBMUXD_CONNECT; 150 c_req.header.type = USBMUXD_CONNECT;
143 c_req.header.tag = 3; 151 c_req.header.tag = 3;
144 c_req.device_id = device_id; 152 c_req.device_id = (uint32_t)handle;
145 c_req.tcp_dport = htons(port); 153 c_req.tcp_dport = htons(tcp_port);
146 c_req.reserved = 0; 154 c_req.reserved = 0;
147 155
148 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { 156 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) {