]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
ols: Do not randomly probe serial ports
[libsigrok.git] / hardware / common / serial.c
index 5a1d21af90c4cbebc9a593847f7b0fe1634c51e2..239c718125a911fb8f74d9e2e8820f0f7d987ec0 100644 (file)
 static HANDLE hdl;
 #endif
 
-static const char *serial_port_glob[] = {
-       /* Linux */
-       "/dev/ttyS*",
-       "/dev/ttyUSB*",
-       "/dev/ttyACM*",
-       /* MacOS X */
-       "/dev/ttys*",
-       "/dev/tty.USB-*",
-       "/dev/tty.Modem-*",
-       NULL,
-};
-
-SR_PRIV GSList *list_serial_ports(void)
-{
-       GSList *ports;
-
-       sr_dbg("Getting list of serial ports on the system.");
-
-#ifdef _WIN32
-       /* TODO */
-       ports = NULL;
-       ports = g_slist_append(ports, g_strdup("COM1"));
-#else
-       glob_t g;
-       unsigned int i, j;
-
-       ports = NULL;
-       for (i = 0; serial_port_glob[i]; i++) {
-               if (glob(serial_port_glob[i], 0, NULL, &g))
-                       continue;
-               for (j = 0; j < g.gl_pathc; j++) {
-                       ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
-                       sr_dbg("Found serial port '%s'.", g.gl_pathv[j]);
-               }
-               globfree(&g);
-       }
-#endif
-
-       return ports;
-}
-
 /**
  * Open the specified serial port.
  *
@@ -197,8 +156,6 @@ SR_PRIV int serial_flush(int fd)
  */
 SR_PRIV int serial_write(int fd, const void *buf, size_t count)
 {
-       sr_spew("FD %d: Writing %d bytes.", fd, count);
-
 #ifdef _WIN32
        DWORD tmp = 0;
 
@@ -212,8 +169,8 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count)
        ret = write(fd, buf, count);
        if (ret < 0)
                sr_err("FD %d: Write error: %s.", fd, strerror(errno));
-       else if ((size_t)ret != count)
-               sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count);
+       else
+               sr_spew("FD %d: Wrote %d/%d bytes.", fd, ret, count);
 
        return ret;
 #endif
@@ -230,8 +187,6 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count)
  */
 SR_PRIV int serial_read(int fd, void *buf, size_t count)
 {
-       sr_spew("FD %d: Reading %d bytes.", fd, count);
-
 #ifdef _WIN32
        DWORD tmp = 0;
 
@@ -249,76 +204,8 @@ SR_PRIV int serial_read(int fd, void *buf, size_t count)
                 * "Resource temporarily unavailable" messages.
                 */
                sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
-       } else if ((size_t)ret != count) {
-               sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count);
-       }
-
-       return ret;
-#endif
-}
-
-/**
- * Create a backup of the current parameters of the specified serial port.
- *
- * @param fd File descriptor of the serial port.
- *
- * @return Pointer to a struct termios upon success, NULL upon errors.
- *         It is the caller's responsibility to g_free() the pointer if no
- *         longer needed.
- */
-SR_PRIV void *serial_backup_params(int fd)
-{
-       sr_dbg("FD %d: Creating serial parameters backup.", fd);
-
-#ifdef _WIN32
-       /* TODO */
-#else
-       struct termios *term;
-
-       if (!(term = g_try_malloc(sizeof(struct termios)))) {
-               sr_err("termios struct malloc failed.");
-               return NULL;
-       }
-
-       /* Returns 0 upon success, -1 upon failure. */
-       if (tcgetattr(fd, term) < 0) {
-               sr_err("FD %d: Error getting serial parameters: %s.",
-                      fd, strerror(errno));
-               g_free(term);
-               return NULL;
-       }
-
-       return term;
-#endif
-}
-
-/**
- * Restore serial port settings from a previously created backup.
- *
- * @param fd File descriptor of the serial port.
- * @param backup Pointer to a struct termios which contains the settings
- *               to restore.
- *
- * @return 0 upon success, -1 upon failure.
- */
-SR_PRIV int serial_restore_params(int fd, void *backup)
-{
-       sr_dbg("FD %d: Restoring serial parameters from backup.", fd);
-
-       if (!backup) {
-               sr_err("FD %d: Cannot restore serial params (NULL).", fd);
-               return -1;
-       }
-
-#ifdef _WIN32
-       /* TODO */
-#else
-       int ret;
-
-       /* Returns 0 upon success, -1 upon failure. */
-       if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) {
-               sr_err("FD %d: Error restoring serial parameters: %s.",
-                      fd, strerror(errno));
+       } else {
+               sr_spew("FD %d: Read %d/%d bytes.", fd, ret, count);
        }
 
        return ret;
@@ -603,9 +490,9 @@ SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
 }
 
 SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
-                           uint64_t timeout_ms)
+                           gint64 timeout_ms)
 {
-       uint64_t start;
+       gint64 start;
        int maxlen, len;
 
        timeout_ms *= 1000;
@@ -621,8 +508,9 @@ SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
                if (len > 0) {
                        *buflen += len;
                        *(*buf + *buflen) = '\0';
-                       if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
-                               /* Strip LF and terminate. */
+                       if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
+                                       || *(*buf + *buflen - 1) == '\n')) {
+                               /* Strip CR/LF and terminate. */
                                *(*buf + --*buflen) = '\0';
                                break;
                        }
@@ -632,7 +520,8 @@ SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
                        break;
                g_usleep(2000);
        }
-       sr_dbg("Received %d: '%s'.", *buflen, *buf);
+       if (*buflen)
+               sr_dbg("Received %d: '%s'.", *buflen, *buf);
 
        return SR_OK;
 }