MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / recv_comm

Function recv_comm

examples/libc/termios_test.c:158–258  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

156}
157
158int recv_comm(int fd, unsigned char *buffer, rt_size_t size, struct timeval *timeout)
159{
160 struct timeval t;
161 int ret = 0;
162 rt_size_t drv_recved = 0;
163 int recved = 0, need = size;
164 int timeout_cnt = 0;
165 unsigned char *c = RT_NULL;
166 fd_set readSet;
167
168 RT_ASSERT(RT_NULL != buffer);
169
170 if(fd == -1)
171 {
172 return -1;
173 }
174
175 t.tv_sec = 0;
176 t.tv_usec = 100000;
177
178 if(RT_NULL == timeout)
179 {
180 /* Wait forever approximate, it's a large time. */
181 timeout_cnt = 0xffffffff;
182 }
183 else
184 {
185 timeout_cnt = (timeout->tv_sec * 1000 * 1000 + timeout->tv_usec)/(t.tv_sec * 1000 * 1000 + t.tv_usec);
186 }
187
188 while(1)
189 {
190 FD_ZERO(&readSet);
191 FD_SET(fd, &readSet);
192
193 ret = select(fd+1,&readSet,RT_NULL,RT_NULL,&t);
194 if(ret < 0)
195 {
196 rt_kprintf("select error %d\n",ret);
197 break;
198 }
199 else if(ret == 0)
200 {
201 /* timeout */
202 timeout_cnt--;
203
204 if(timeout_cnt == 0)
205 {
206 rt_kprintf("need %d data in timeout %d ms,but only %d recved.\n",
207 size,
208 timeout->tv_sec * 1000 + timeout->tv_usec / 1000,
209 recved);
210
211 recved = 0;
212
213 break;
214 }
215 }

Callers 1

termios_test_entryFunction · 0.85

Calls 4

rt_kprintfFunction · 0.85
selectFunction · 0.50
ioctlFunction · 0.50
readFunction · 0.50

Tested by

no test coverage detected