• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utils/Log.h>
29 #include <termios.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <sys/eventfd.h>
34 #include "userial.h"
35 #include "userial_vendor.h"
36 #include "rtk_socket.h"
37 
38 /******************************************************************************
39 **  Constants & Macros
40 ******************************************************************************/
41 
42 /******************************************************************************
43 **  Extern functions
44 ******************************************************************************/
45 
46 /******************************************************************************
47 **  Local type definitions
48 ******************************************************************************/
49 
50 /******************************************************************************
51 **  Static functions
52 ******************************************************************************/
53 
54 /******************************************************************************
55 **  functions
56 ******************************************************************************/
Skt_Read(int fd,uint8_t * p_buf,uint32_t len,bool * condition)57 uint32_t Skt_Read(int fd, uint8_t *p_buf, uint32_t len, bool *condition)
58 {
59     int n_read = 0;
60     struct pollfd pfd;
61 
62     if (fd == -1)
63     {
64         return 0;
65     }
66 
67     while (n_read < (int)len)
68     {
69         if (condition && !(*condition))
70             return n_read;
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         RTK_NO_INTR(poll_ret = poll(&pfd, 1, 100));
79         if (poll_ret == 0)
80         {
81             continue;
82         }
83         if (poll_ret < 0)
84         {
85             HILOGE("%s(): poll() failed: return %d errno %d (%s)",
86                    __func__, poll_ret, errno, strerror(errno));
87             break;
88         }
89 
90         if (pfd.revents & (POLLHUP | POLLNVAL | POLLRDHUP))
91         {
92             return 0;
93         }
94 
95         ssize_t n;
96         RTK_NO_INTR(n = recv(fd, p_buf + n_read, len - n_read, 0));
97 
98         if (n == 0)
99         {
100             HILOGE("Skt_Read : channel detached remotely");
101             return 0;
102         }
103 
104         if (n < 0)
105         {
106             HILOGE("Skt_Read : read failed (%s)", strerror(errno));
107             return 0;
108         }
109 
110         n_read += n;
111     }
112 
113     return n_read;
114 }
115 
Skt_Read_noblock(int fd,uint8_t * p_buf,uint32_t len)116 int Skt_Read_noblock(int fd, uint8_t *p_buf, uint32_t len)
117 {
118     int n_read = 0;
119     struct pollfd pfd;
120 
121     if (fd == -1)
122     {
123         HILOGE("UIPC_Read_noblock closed");
124         return 0;
125     }
126 
127     pfd.fd = fd;
128     pfd.events = POLLIN | POLLHUP | POLLRDHUP;
129 
130     if (poll(&pfd, 1, 0) == 0)
131     {
132         return 0;
133     }
134 
135     if (pfd.revents & (POLLHUP | POLLNVAL | POLLRDHUP))
136     {
137         return 0;
138     }
139 
140     n_read = recv(fd, p_buf, len, MSG_DONTWAIT | MSG_NOSIGNAL);
141 
142     return n_read;
143 }
144 
Skt_Send(int fd,uint8_t * p_buf,uint16_t msglen)145 bool Skt_Send(int fd, uint8_t *p_buf, uint16_t msglen)
146 {
147     ssize_t ret;
148     RTK_NO_INTR(ret = write(fd, p_buf, msglen));
149     if (ret < 0)
150     {
151         HILOGE("failed to write (%s)", strerror(errno));
152     }
153 
154     return false;
155 }
156 
Skt_Send_noblock(int fd,uint8_t * p_buf,uint16_t msglen)157 int Skt_Send_noblock(int fd, uint8_t *p_buf, uint16_t msglen)
158 {
159     int res = 0;
160     struct pollfd pfd;
161 
162     pfd.fd = fd;
163     pfd.events = POLLOUT | POLLHUP;
164     if (poll(&pfd, 1, 0) == 0)
165     {
166         return 0;
167     }
168 
169     if (pfd.revents & (POLLHUP | POLLNVAL))
170     {
171         HILOGE("poll : channel detached remotely");
172         return 0;
173     }
174 
175     res = send(fd, p_buf, msglen, MSG_DONTWAIT);
176     if (res < 0)
177     {
178         HILOGE("failed to write (%s)", strerror(errno));
179     }
180 
181     return res;
182 }
183 
184 /******************************************************************************
185 **  Static variables
186 ******************************************************************************/
187 
188 /*****************************************************************************
189 **   Helper Functions
190 *****************************************************************************/
191