Skip to content

[Issue #5] Implemented WinSock variation of "ser_drain(...)" functionality #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions avrdude-6.3-patches/99-Add-WinSock-variation-of-ser_drain.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
diff --git a/avrdude/ser_win32.c b/avrdude/ser_win32.c
index fcaa55e..5fc17b1 100644
--- ser_win32.c
+++ ser_win32.c
@@ -628,9 +628,95 @@ static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen
return 0;
}

+#ifdef HAVE_LIBWS2_32
+static int net_drain(union filedescriptor *fd, int display)
+{
+ LPVOID lpMsgBuf;
+ struct timeval timeout;
+ fd_set rfds;
+ int nfds;
+ unsigned char buf;
+ int rc;
+
+ if (fd->ifd < 0) {
+ avrdude_message(MSG_INFO, "%s: ser_drain(): connection not open\n", progname);
+ exit(1);
+ }
+
+ if (display) {
+ avrdude_message(MSG_INFO, "drain>");
+ }
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 250000;
+
+ while (1) {
+ FD_ZERO(&rfds);
+ FD_SET(fd->ifd, &rfds);
+
+ reselect:
+ nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &timeout);
+ if (nfds == 0) {
+ if (display) {
+ avrdude_message(MSG_INFO, "<drain\n");
+ }
+ break;
+ }
+ else if (nfds == -1) {
+ if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEINPROGRESS) {
+ avrdude_message(MSG_NOTICE, "%s: ser_drain(): programmer is not responding, reselecting\n", progname);
+ goto reselect;
+ } else {
+ FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ WSAGetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)&lpMsgBuf,
+ 0,
+ NULL);
+ avrdude_message(MSG_INFO, "%s: ser_drain(): select(): %s\n", progname, (char *)lpMsgBuf);
+ LocalFree(lpMsgBuf);
+ exit(1);
+ }
+ }
+
+ rc = recv(fd->ifd, &buf, 1, 0);
+ if (rc < 0) {
+ FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ WSAGetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)&lpMsgBuf,
+ 0,
+ NULL);
+ avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n", progname, (char *)lpMsgBuf);
+ LocalFree(lpMsgBuf);
+ exit(1);
+ }
+
+ if (display) {
+ avrdude_message(MSG_INFO, "%02x ", buf);
+ }
+ }
+
+ return 0;
+}
+#endif

static int ser_drain(union filedescriptor *fd, int display)
{
+#ifdef HAVE_LIBWS2_32
+ if (serial_over_ethernet) {
+ return net_drain(fd, display);
+ }
+#endif
+
// int rc;
unsigned char buf[10];
BOOL readres;