blob: 14182f8e42cdd0c216ed97640e3a33d4822ba4b3 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 *
3 * Copyright (C) 2023
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
14 *
15 * FileName : log_usb.c
16 * This program Capture module's trace log.
17 */
18
19#include "log_usb.h"
20
21int zLogAgt_Usb_Close(usbdev_t *udev)
22{
23 if (udev == NULL)
24 return -1;
25
26 if (udev->ttyfd >= 0) {
27 close(udev->ttyfd);
28 udev->ttyfd = -1;
29 }
30 return 0;
31}
32
33static int poll_wait(int poll_fd, short events, int timeout_msec)
34{
35 struct pollfd pollfds[] = {{poll_fd, events, 0}};
36
37 int ret = poll(pollfds, 1, timeout_msec);
38 if (ret == 0) {
39 return ETIMEDOUT;
40 } else if (ret < 0) {
41 dbg_time("%s errno:%d(%s) \n", __func__, errno, strerror(errno));
42 }
43
44 if (pollfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
45 return EIO;
46 }
47
48 if (pollfds[0].revents & (events)) {
49 return 0;
50 }
51
52 return EIO;
53}
54
55int zLogAgt_Usb_Write(usbdev_t *udev, char *pbuf, int len)
56{
57 int write_size = 0;
58 int timeout_msec = 3000;
59
60 while (write_size < len)
61 {
62 int retval = -1;
63
64 if (udev->ttyfd >= 0) {
65 if (poll_wait(udev->ttyfd, POLLOUT, timeout_msec)) {
66 break;
67 }
68 retval = write(udev->ttyfd, pbuf + write_size, len - write_size);
69 if (retval < 0 && errno != EINTR) {
70 dbg_time("%s write:%d, errno:%d(%s)\n", __func__, retval, errno, strerror(errno));
71 return retval;
72 }
73 }
74 write_size += retval;
75 }
76
77 return write_size;
78}
79
80int zLogAgt_Usb_Read(usbdev_t *udev, char *pbuf, int len)
81{
82 int read_size = 0;
83 int timeout_msec = 1000;
84
85 while (read_size < len)
86 {
87 int retval = -1;
88 if (udev->ttyfd >= 0) {
89 if (poll_wait(udev->ttyfd, POLLIN, timeout_msec)) {
90 break;
91 }
92
93 retval = read(udev->ttyfd, pbuf + read_size, len - read_size);
94 if (retval <= 0 && errno != EINTR) {
95 dbg_time("%s read:%d, errno:%d(%s)\n", __func__, retval, errno, strerror(errno));
96 return retval;
97 }
98 }
99 read_size += retval;
100 }
101 return read_size;
102}
103