X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/blobdiff_plain/47ea478b44e3c529201c6678f5e2cbbe0fc60bf9..b8d1d0c3fc27e563273a0c6d0a10ebe673ea2904:/culfw.c diff --git a/culfw.c b/culfw.c index 1904bb7..99b20cb 100644 --- a/culfw.c +++ b/culfw.c @@ -1,6 +1,6 @@ /* culfw driver * - * Copyright (c) 2013 Michael Gernoth + * Copyright (c) 2014-16 Michael Gernoth * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -110,7 +110,7 @@ out: return NULL; } -int culfw_send(struct culfw_dev *dev, char *cmd, int cmdlen) +int culfw_send(struct culfw_dev *dev, const char *cmd, int cmdlen) { int w = 0; int ret; @@ -141,7 +141,7 @@ int culfw_poll(struct culfw_dev *dev, int timeout) pfds[0].fd = dev->fd; pfds[0].events = POLLIN; - ret = poll(pfds, 1, timeout * 1000); + ret = poll(pfds, 1, timeout); if (ret == -1) return -1; @@ -167,10 +167,42 @@ int culfw_poll(struct culfw_dev *dev, int timeout) dev->cb(buf, r, dev->cb_data); - return pfds[0].fd; + errno = 0; + return -1; } void culfw_close(struct culfw_dev *dev) { close(dev->fd); } + +void culfw_flush(struct culfw_dev *dev) +{ + struct pollfd pfds[1]; + int ret; + int r = 0; + uint8_t buf[1024]; + + tcflush(dev->fd, TCIOFLUSH); + + while(1) { + memset(pfds, 0, sizeof(struct pollfd) * 1); + + pfds[0].fd = dev->fd; + pfds[0].events = POLLIN; + + ret = poll(pfds, 1, 100); + if (ret <= 0) + break; + + if (!(pfds[0].revents & POLLIN)) + break; + + memset(buf, 0, sizeof(buf)); + r = read(dev->fd, buf, sizeof(buf)); + if (r <= 0) + break; + } + + return; +}