]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/beaglelogic/beaglelogic_tcp.c
beaglelogic: Coding style fixes
[libsigrok.git] / src / hardware / beaglelogic / beaglelogic_tcp.c
index 8baf4eef055702dd23bc3e43b9fd8c2c89eaf534..dfd8c624d6f946652e1d2c12c34531963961a8d9 100644 (file)
@@ -2,7 +2,8 @@
  * This file is part of the libsigrok project.
  *
  * Copyright (C) 2017 Kumar Abhishek <abhishek@theembeddedkitchen.net>
- * Portions of the code are adopted from scpi_tcp.c and scpi.c
+ * Portions of the code are adapted from scpi_tcp.c and scpi.c, their
+ * copyright notices are listed below:
  *
  * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
@@ -41,7 +42,8 @@
 #include "protocol.h"
 #include "beaglelogic.h"
 
-static int beaglelogic_tcp_open(struct dev_context *devc) {
+static int beaglelogic_tcp_open(struct dev_context *devc)
+{
        struct addrinfo hints;
        struct addrinfo *results, *res;
        int err;
@@ -83,7 +85,8 @@ static int beaglelogic_tcp_open(struct dev_context *devc) {
 }
 
 static int beaglelogic_tcp_send_cmd(struct dev_context *devc,
-                                   const char *format, ...) {
+                                   const char *format, ...)
+{
        int len, out;
        va_list args, args_copy;
        char *buf;
@@ -108,7 +111,7 @@ static int beaglelogic_tcp_send_cmd(struct dev_context *devc,
        }
 
        if (out < (int)strlen(buf)) {
-               sr_dbg("Only sent %d/%d bytes of command: '%s'.", out,
+               sr_dbg("Only sent %d/%lu bytes of command: '%s'.", out,
                       strlen(buf), buf);
        }
 
@@ -119,7 +122,8 @@ static int beaglelogic_tcp_send_cmd(struct dev_context *devc,
 }
 
 static int beaglelogic_tcp_read_data(struct dev_context *devc, char *buf,
-               int maxlen) {
+                                    int maxlen)
+{
        int len;
 
        len = recv(devc->socket, buf, maxlen, 0);
@@ -132,8 +136,36 @@ static int beaglelogic_tcp_read_data(struct dev_context *devc, char *buf,
        return len;
 }
 
+SR_PRIV int beaglelogic_tcp_drain(struct dev_context *devc)
+{
+       char *buf = g_malloc(1024);
+       fd_set rset;
+       int ret, len = 0;
+       struct timeval tv;
+
+       FD_ZERO(&rset);
+       FD_SET(devc->socket, &rset);
+
+       /* 25ms timeout */
+       tv.tv_sec = 0;
+       tv.tv_usec = 25 * 1000;
+
+       do {
+               ret = select(devc->socket + 1, &rset, NULL, NULL, &tv);
+               if (ret > 0) {
+                       len += beaglelogic_tcp_read_data(devc, buf, 1024);
+               }
+       } while (ret > 0);
+
+       sr_spew("Drained %d bytes of data.", len);
+
+       g_free(buf);
+       return SR_OK;
+}
+
 static int beaglelogic_tcp_get_string(struct dev_context *devc, const char *cmd,
-               char **tcp_resp) {
+                                     char **tcp_resp)
+{
        GString *response = g_string_sized_new(1024);
        int len;
        gint64 timeout;
@@ -179,7 +211,8 @@ static int beaglelogic_tcp_get_string(struct dev_context *devc, const char *cmd,
 }
 
 static int beaglelogic_tcp_get_int(struct dev_context *devc,
-                                  const char *cmd, int *response) {
+                                  const char *cmd, int *response)
+{
        int ret;
        char *resp = NULL;
 
@@ -197,7 +230,8 @@ static int beaglelogic_tcp_get_int(struct dev_context *devc,
        return ret;
 }
 
-SR_PRIV int beaglelogic_tcp_detect(struct dev_context *devc) {
+SR_PRIV int beaglelogic_tcp_detect(struct dev_context *devc)
+{
        char *resp = NULL;
        int ret;
 
@@ -211,26 +245,27 @@ SR_PRIV int beaglelogic_tcp_detect(struct dev_context *devc) {
        return ret;
 }
 
-static int beaglelogic_open(struct dev_context *devc) {
+static int beaglelogic_open(struct dev_context *devc)
+{
        return beaglelogic_tcp_open(devc);
 }
 
-static int beaglelogic_close(struct dev_context *devc) {
-       g_free(devc->address);
-       g_free(devc->port);
-
+static int beaglelogic_close(struct dev_context *devc)
+{
        if (close(devc->socket) < 0)
                return SR_ERR;
 
        return SR_OK;
 }
 
-static int beaglelogic_get_buffersize(struct dev_context *devc) {
+static int beaglelogic_get_buffersize(struct dev_context *devc)
+{
        return beaglelogic_tcp_get_int(devc, "memalloc",
                (int *)&devc->buffersize);
 }
 
-static int beaglelogic_set_buffersize(struct dev_context *devc) {
+static int beaglelogic_set_buffersize(struct dev_context *devc)
+{
        int ret;
        char *resp;
 
@@ -245,14 +280,16 @@ static int beaglelogic_set_buffersize(struct dev_context *devc) {
        return ret;
 }
 
-static int beaglelogic_get_samplerate(struct dev_context *devc) {
+static int beaglelogic_get_samplerate(struct dev_context *devc)
+{
        int arg, err;
        err = beaglelogic_tcp_get_int(devc, "samplerate", &arg);
        devc->cur_samplerate = arg;
        return err;
 }
 
-static int beaglelogic_set_samplerate(struct dev_context *devc) {
+static int beaglelogic_set_samplerate(struct dev_context *devc)
+{
        int ret;
        char *resp;
 
@@ -268,12 +305,14 @@ static int beaglelogic_set_samplerate(struct dev_context *devc) {
        return ret;
 }
 
-static int beaglelogic_get_sampleunit(struct dev_context *devc) {
+static int beaglelogic_get_sampleunit(struct dev_context *devc)
+{
        return beaglelogic_tcp_get_int(devc, "sampleunit",
                (int *)&devc->sampleunit);
 }
 
-static int beaglelogic_set_sampleunit(struct dev_context *devc) {
+static int beaglelogic_set_sampleunit(struct dev_context *devc)
+{
        int ret;
        char *resp;
 
@@ -288,12 +327,14 @@ static int beaglelogic_set_sampleunit(struct dev_context *devc) {
        return ret;
 }
 
-static int beaglelogic_get_triggerflags(struct dev_context *devc) {
+static int beaglelogic_get_triggerflags(struct dev_context *devc)
+{
        return beaglelogic_tcp_get_int(devc, "triggerflags",
                (int *)&devc->triggerflags);
 }
 
-static int beaglelogic_set_triggerflags(struct dev_context *devc) {
+static int beaglelogic_set_triggerflags(struct dev_context *devc)
+{
        int ret;
        char *resp;
 
@@ -308,25 +349,31 @@ static int beaglelogic_set_triggerflags(struct dev_context *devc) {
        return ret;
 }
 
-static int beaglelogic_get_lasterror(struct dev_context *devc) {
+static int beaglelogic_get_lasterror(struct dev_context *devc)
+{
        devc->last_error = 0;
        return SR_OK;
 }
 
-static int beaglelogic_start(struct dev_context *devc) {
+static int beaglelogic_start(struct dev_context *devc)
+{
+       beaglelogic_tcp_drain(devc);
        return beaglelogic_tcp_send_cmd(devc, "get");
 }
 
-static int beaglelogic_stop(struct dev_context *devc) {
+static int beaglelogic_stop(struct dev_context *devc)
+{
        return beaglelogic_tcp_send_cmd(devc, "close");
 }
 
-static int beaglelogic_get_bufunitsize(struct dev_context *devc) {
+static int beaglelogic_get_bufunitsize(struct dev_context *devc)
+{
        return beaglelogic_tcp_get_int(devc, "bufunitsize",
                (int *)&devc->bufunitsize);
 }
 
-static int beaglelogic_set_bufunitsize(struct dev_context *devc) {
+static int beaglelogic_set_bufunitsize(struct dev_context *devc)
+{
        int ret;
        char *resp;
 
@@ -341,8 +388,9 @@ static int beaglelogic_set_bufunitsize(struct dev_context *devc) {
        return ret;
 }
 
-static int dummy(struct dev_context *devc) {
-       (devc);
+static int dummy(struct dev_context *devc)
+{
+       (void)devc;
        return SR_ERR_NA;
 }