Skip to content

Commit 4396674

Browse files
committed
patch #9304: [Bug #48767] Implemented WinSock variation of "ser_drain(...)" functionality
Submitted by Christopher Cooper: * ser_win32.c (ser_drain): Implement a network drain function. git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1499 81a1dc3b-b13d-400b-aceb-764788c761c2
1 parent c9a1405 commit 4396674

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2021-11-27 Joerg Wunsch <[email protected]>
2+
3+
Submitted by Christopher Cooper:
4+
patch #9304: [Bug #48767] Implemented WinSock variation of "ser_drain(...)" functionality
5+
* ser_win32.c (ser_drain): Implement a network drain
6+
function.
7+
18
2021-11-27 Joerg Wunsch <[email protected]>
29

310
Submitted by Lars Ollén:

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Current:
7373
bug #60863: avrftdi programming error probably caused by multiple, consecutive empty pages
7474
bug #50517: Reading fails if "immediate mode" for output file format is selected - fileio: invalid operation=1
7575
bug #50630: Erase Cycle Counter options ( -y -Y n ) should be removed from usage Message
76+
bug #48767: ser_drain() for TCP on Windows doesn't work
7677

7778
* Patches:
7879
patch #9482: Add support for UPDI and AVR8X
@@ -126,6 +127,7 @@ Current:
126127
patch #10031: linuxspi: Support GPIO uAPI v2
127128
(no-id): Improve documentation of linuxspi driver, provide portname default
128129
(no-id): Use -B <bitclock> rather than -b <baudrate> for linuxspi driver
130+
patch #9304: [Bug #48767] Implemented WinSock variation of "ser_drain(...)" functionality
129131

130132
* Internals:
131133
- New avrdude.conf keyword "family_id", used to verify SIB attributes

ser_win32.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,95 @@ static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen
628628
return 0;
629629
}
630630

631+
#ifdef HAVE_LIBWS2_32
632+
static int net_drain(union filedescriptor *fd, int display)
633+
{
634+
LPVOID lpMsgBuf;
635+
struct timeval timeout;
636+
fd_set rfds;
637+
int nfds;
638+
unsigned char buf;
639+
int rc;
640+
641+
if (fd->ifd < 0) {
642+
avrdude_message(MSG_INFO, "%s: ser_drain(): connection not open\n", progname);
643+
exit(1);
644+
}
645+
646+
if (display) {
647+
avrdude_message(MSG_INFO, "drain>");
648+
}
649+
650+
timeout.tv_sec = 0;
651+
timeout.tv_usec = 250000;
652+
653+
while (1) {
654+
FD_ZERO(&rfds);
655+
FD_SET(fd->ifd, &rfds);
656+
657+
reselect:
658+
nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &timeout);
659+
if (nfds == 0) {
660+
if (display) {
661+
avrdude_message(MSG_INFO, "<drain\n");
662+
}
663+
break;
664+
}
665+
else if (nfds == -1) {
666+
if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEINPROGRESS) {
667+
avrdude_message(MSG_NOTICE, "%s: ser_drain(): programmer is not responding, reselecting\n", progname);
668+
goto reselect;
669+
} else {
670+
FormatMessage(
671+
FORMAT_MESSAGE_ALLOCATE_BUFFER |
672+
FORMAT_MESSAGE_FROM_SYSTEM |
673+
FORMAT_MESSAGE_IGNORE_INSERTS,
674+
NULL,
675+
WSAGetLastError(),
676+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
677+
(LPTSTR)&lpMsgBuf,
678+
0,
679+
NULL);
680+
avrdude_message(MSG_INFO, "%s: ser_drain(): select(): %s\n", progname, (char *)lpMsgBuf);
681+
LocalFree(lpMsgBuf);
682+
exit(1);
683+
}
684+
}
685+
686+
rc = recv(fd->ifd, &buf, 1, 0);
687+
if (rc < 0) {
688+
FormatMessage(
689+
FORMAT_MESSAGE_ALLOCATE_BUFFER |
690+
FORMAT_MESSAGE_FROM_SYSTEM |
691+
FORMAT_MESSAGE_IGNORE_INSERTS,
692+
NULL,
693+
WSAGetLastError(),
694+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
695+
(LPTSTR)&lpMsgBuf,
696+
0,
697+
NULL);
698+
avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n", progname, (char *)lpMsgBuf);
699+
LocalFree(lpMsgBuf);
700+
exit(1);
701+
}
702+
703+
if (display) {
704+
avrdude_message(MSG_INFO, "%02x ", buf);
705+
}
706+
}
707+
708+
return 0;
709+
}
710+
#endif
631711

632712
static int ser_drain(union filedescriptor *fd, int display)
633713
{
714+
#ifdef HAVE_LIBWS2_32
715+
if (serial_over_ethernet) {
716+
return net_drain(fd, display);
717+
}
718+
#endif
719+
634720
// int rc;
635721
unsigned char buf[10];
636722
BOOL readres;

0 commit comments

Comments
 (0)