summaryrefslogtreecommitdiffstats
path: root/src/device.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/src/device.c b/src/device.c
index ce0f28f..15f829b 100644
--- a/src/device.c
+++ b/src/device.c
@@ -329,6 +329,13 @@ int device_start_connect(int device_id, uint16_t dport, struct mux_client *clien
329 return 0; 329 return 0;
330} 330}
331 331
332/**
333 * Examine the state of a connection's buffers and
334 * update all connection flags and masks accordingly.
335 * Does not do I/O.
336 *
337 * @param conn The connection to update.
338 */
332static void update_connection(struct mux_connection *conn) 339static void update_connection(struct mux_connection *conn)
333{ 340{
334 uint32_t sent = conn->tx_seq - conn->rx_ack; 341 uint32_t sent = conn->tx_seq - conn->rx_ack;
@@ -362,8 +369,18 @@ static void update_connection(struct mux_connection *conn)
362 client_set_events(conn->client, conn->events); 369 client_set_events(conn->client, conn->events);
363} 370}
364 371
372/**
373 * Flush input and output buffers for a client connection.
374 *
375 * @param device_id Numeric id for the device.
376 * @param client The client to flush buffers for.
377 * @param events event mask for the client. POLLOUT means that
378 * the client is ready to receive data, POLLIN that it has
379 * data to be read (and send along to the device).
380 */
365void device_client_process(int device_id, struct mux_client *client, short events) 381void device_client_process(int device_id, struct mux_client *client, short events)
366{ 382{
383 // Find the connection for the given device_id
367 struct mux_connection *conn = NULL; 384 struct mux_connection *conn = NULL;
368 pthread_mutex_lock(&device_list_mutex); 385 pthread_mutex_lock(&device_list_mutex);
369 FOREACH(struct mux_device *dev, &device_list) { 386 FOREACH(struct mux_device *dev, &device_list) {
@@ -388,6 +405,8 @@ void device_client_process(int device_id, struct mux_client *client, short event
388 int res; 405 int res;
389 int size; 406 int size;
390 if(events & POLLOUT) { 407 if(events & POLLOUT) {
408 // Client is ready to receive data, send what we have
409 // in the client's connection buffer
391 size = client_write(conn->client, conn->ib_buf, conn->ib_size); 410 size = client_write(conn->client, conn->ib_buf, conn->ib_size);
392 if(size <= 0) { 411 if(size <= 0) {
393 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size); 412 usbmuxd_log(LL_DEBUG, "error writing to client (%d)", size);
@@ -403,6 +422,8 @@ void device_client_process(int device_id, struct mux_client *client, short event
403 } 422 }
404 } 423 }
405 if(events & POLLIN) { 424 if(events & POLLIN) {
425 // There is inbound trafic on the client socket,
426 // convert it to tcp and send to the device
406 size = client_read(conn->client, conn->ob_buf, conn->sendable); 427 size = client_read(conn->client, conn->ob_buf, conn->sendable);
407 if(size <= 0) { 428 if(size <= 0) {
408 if (size < 0) { 429 if (size < 0) {
@@ -422,6 +443,23 @@ void device_client_process(int device_id, struct mux_client *client, short event
422 update_connection(conn); 443 update_connection(conn);
423} 444}
424 445
446/**
447 * Copy a payload to a connection's in-buffer and
448 * set the POLLOUT event mask on the connection so
449 * the next main_loop iteration will dispatch the
450 * buffer if the connection socket is writable.
451 *
452 * Connection buffers are flushed in the
453 * device_client_process() function.
454 *
455 * @param conn The connection to add incoming data to.
456 * @param payload Payload to prepare for writing.
457 * The payload will be copied immediately so you are
458 * free to alter or free the payload buffer when this
459 * function returns.
460 * @param payload_length number of bytes to copy from from
461 * the payload.
462 */
425static void connection_device_input(struct mux_connection *conn, unsigned char *payload, uint32_t payload_length) 463static void connection_device_input(struct mux_connection *conn, unsigned char *payload, uint32_t payload_length)
426{ 464{
427 if((conn->ib_size + payload_length) > conn->ib_capacity) { 465 if((conn->ib_size + payload_length) > conn->ib_capacity) {
@@ -483,6 +521,14 @@ static void device_version_input(struct mux_device *dev, struct version_header *
483 preflight_worker_device_add(&info); 521 preflight_worker_device_add(&info);
484} 522}
485 523
524/**
525 * Handle an incoming TCP packet from the device.
526 *
527 * @param dev The device handle TCP input on.
528 * @param th Pointer to the TCP header struct.
529 * @param payload Payload data.
530 * @param payload_length Number of bytes in payload.
531 */
486static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length) 532static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned char *payload, uint32_t payload_length)
487{ 533{
488 uint16_t sport = ntohs(th->th_dport); 534 uint16_t sport = ntohs(th->th_dport);
@@ -561,6 +607,14 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned
561 } 607 }
562} 608}
563 609
610/**
611 * Take input data from the device that has been read into a buffer
612 * and dispatch it to the right protocol backend (eg. TCP).
613 *
614 * @param usbdev
615 * @param buffer
616 * @param length
617 */
564void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) 618void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length)
565{ 619{
566 struct mux_device *dev = NULL; 620 struct mux_device *dev = NULL;
@@ -595,7 +649,7 @@ void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_
595 dev->pktlen = 0; 649 dev->pktlen = 0;
596 return; 650 return;
597 } 651 }
598 memcpy(dev->pktbuf + dev->pktlen, buffer, length); 652 memcpy(dev->pktbuf + dev->pktlen, buffer, length);
599 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf; 653 struct mux_header *mhdr = (struct mux_header *)dev->pktbuf;
600 if((length < USB_MRU) || (ntohl(mhdr->length) == (length + dev->pktlen))) { 654 if((length < USB_MRU) || (ntohl(mhdr->length) == (length + dev->pktlen))) {
601 buffer = dev->pktbuf; 655 buffer = dev->pktbuf;