1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2018 Realtek Corporation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /******************************************************************************
19 *
20 * Filename: userial_vendor.c
21 *
22 * Description: Contains vendor-specific userial functions
23 *
24 ******************************************************************************/
25 #undef NDEBUG
26 #define LOG_TAG "rtk_socket"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <sys/eventfd.h>
32 #include <termios.h>
33 #include <utils/Log.h>
34 #include <poll.h>
35 #include "userial.h"
36 #include "userial_vendor.h"
37 #include "rtk_socket.h"
38
39 /******************************************************************************
40 ** Constants & Macros
41 ******************************************************************************/
42
43 /******************************************************************************
44 ** Extern functions
45 ******************************************************************************/
46
47 /******************************************************************************
48 ** Local type definitions
49 ******************************************************************************/
50
51 /******************************************************************************
52 ** Static functions
53 ******************************************************************************/
54
55 /******************************************************************************
56 ** functions
57 ******************************************************************************/
Skt_Read(int fd,uint8_t * p_buf,uint32_t len,bool * condition)58 uint32_t Skt_Read(int fd, uint8_t *p_buf, uint32_t len, bool *condition)
59 {
60 int n_read = 0;
61 struct pollfd pfd;
62
63 if (fd == -1) {
64 return 0;
65 }
66
67 while (n_read < (int)len) {
68 if (condition && !(*condition)) {
69 return n_read;
70 }
71 pfd.fd = fd;
72 pfd.events = POLLIN | POLLHUP | POLLNVAL | POLLRDHUP;
73
74 /* make sure there is data prior to attempting read to avoid blocking
75 a read for more than poll timeout */
76
77 int poll_ret;
78 #define POLL_100 100
79 RTK_NO_INTR(poll_ret = poll(&pfd, 1, POLL_100));
80 if (poll_ret == 0) {
81 continue;
82 }
83 if (poll_ret < 0) {
84 HILOGE("%s(): poll() failed: return %d errno %d (%s)", __func__, poll_ret, errno, strerror(errno));
85 break;
86 }
87
88 if (pfd.revents & (POLLHUP | POLLNVAL | POLLRDHUP)) {
89 return 0;
90 }
91
92 ssize_t n;
93 RTK_NO_INTR(n = recv(fd, p_buf + n_read, len - n_read, 0));
94
95 if (n == 0) {
96 HILOGE("Skt_Read : channel detached remotely");
97 return 0;
98 }
99
100 if (n < 0) {
101 HILOGE("Skt_Read : read failed (%s)", strerror(errno));
102 return 0;
103 }
104
105 n_read += n;
106 }
107
108 return n_read;
109 }
110
Skt_Read_noblock(int fd,uint8_t * p_buf,uint32_t len)111 int Skt_Read_noblock(int fd, uint8_t *p_buf, uint32_t len)
112 {
113 int n_read = 0;
114 struct pollfd pfd;
115
116 if (fd == -1) {
117 HILOGE("UIPC_Read_noblock closed");
118 return 0;
119 }
120
121 pfd.fd = fd;
122 pfd.events = POLLIN | POLLHUP | POLLRDHUP;
123
124 if (poll(&pfd, 1, 0) == 0) {
125 return 0;
126 }
127
128 if (pfd.revents & (POLLHUP | POLLNVAL | POLLRDHUP)) {
129 return 0;
130 }
131
132 n_read = recv(fd, p_buf, len, MSG_DONTWAIT | MSG_NOSIGNAL);
133
134 return n_read;
135 }
136
Skt_Send(int fd,uint8_t * p_buf,uint16_t msglen)137 bool Skt_Send(int fd, uint8_t *p_buf, uint16_t msglen)
138 {
139 ssize_t ret;
140 RTK_NO_INTR(ret = write(fd, p_buf, msglen));
141 if (ret < 0) {
142 HILOGE("failed to write (%s)", strerror(errno));
143 }
144
145 return false;
146 }
147
Skt_Send_noblock(int fd,uint8_t * p_buf,uint16_t msglen)148 int Skt_Send_noblock(int fd, uint8_t *p_buf, uint16_t msglen)
149 {
150 int res = 0;
151 struct pollfd pfd;
152
153 pfd.fd = fd;
154 pfd.events = POLLOUT | POLLHUP;
155 if (poll(&pfd, 1, 0) == 0) {
156 return 0;
157 }
158
159 if (pfd.revents & (POLLHUP | POLLNVAL)) {
160 HILOGE("poll : channel detached remotely");
161 return 0;
162 }
163
164 res = send(fd, p_buf, msglen, MSG_DONTWAIT);
165 if (res < 0) {
166 HILOGE("failed to write (%s)", strerror(errno));
167 }
168
169 return res;
170 }
171
172 /******************************************************************************
173 ** Static variables
174 ******************************************************************************/
175
176 /*****************************************************************************
177 ** Helper Functions
178 *****************************************************************************/
179