]> sigrok.org Git - libsigrok.git/blobdiff - src/scpi/scpi_tcp.c
scpi_vxi: fix memory leak for SCPI response data in VXI support code
[libsigrok.git] / src / scpi / scpi_tcp.c
index 7b3c9155d2026e99472365215f8cf9da1510312d..29e7a4394aa8a54c1c69871f5362151ea6ff66db 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #ifdef _WIN32
 #define _WIN32_WINNT 0x0501
 #include <winsock2.h>
@@ -32,8 +33,9 @@
 #include <netdb.h>
 #endif
 #include <errno.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
+#include "scpi.h"
 
 #define LOG_PREFIX "scpi_tcp"
 
@@ -64,15 +66,15 @@ static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
        }
 
        tcp->address = g_strdup(params[1]);
-       tcp->port    = g_strdup(params[2]);
-       tcp->socket  = -1;
+       tcp->port = g_strdup(params[2]);
+       tcp->socket = -1;
 
        return SR_OK;
 }
 
-static int scpi_tcp_open(void *priv)
+static int scpi_tcp_open(struct sr_scpi_dev_inst *scpi)
 {
-       struct scpi_tcp *tcp = priv;
+       struct scpi_tcp *tcp = scpi->priv;
        struct addrinfo hints;
        struct addrinfo *results, *res;
        int err;
@@ -85,7 +87,7 @@ static int scpi_tcp_open(void *priv)
        err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
 
        if (err) {
-               sr_err("Address lookup failed: %s:%d: %s", tcp->address, tcp->port,
+               sr_err("Address lookup failed: %s:%s: %s", tcp->address, tcp->port,
                        gai_strerror(err));
                return SR_ERR;
        }
@@ -106,13 +108,24 @@ static int scpi_tcp_open(void *priv)
 
        if (tcp->socket < 0) {
                sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
-                               strerror(errno));
+                               g_strerror(errno));
                return SR_ERR;
        }
 
        return SR_OK;
 }
 
+static int scpi_tcp_connection_id(struct sr_scpi_dev_inst *scpi,
+               char **connection_id)
+{
+       struct scpi_tcp *tcp = scpi->priv;
+
+       *connection_id = g_strdup_printf("%s/%s:%s",
+               scpi->prefix, tcp->address, tcp->port);
+
+       return SR_OK;
+}
+
 static int scpi_tcp_source_add(struct sr_session *session, void *priv,
                int events, int timeout, sr_receive_data_callback cb, void *cb_data)
 {
@@ -133,15 +146,12 @@ static int scpi_tcp_send(void *priv, const char *command)
 {
        struct scpi_tcp *tcp = priv;
        int len, out;
-       char *terminated_command;
 
-       terminated_command = g_strdup_printf("%s\r\n", command);
-       len = strlen(terminated_command);
-       out = send(tcp->socket, terminated_command, len, 0);
-       g_free(terminated_command);
+       len = strlen(command);
+       out = send(tcp->socket, command, len, 0);
 
        if (out < 0) {
-               sr_err("Send error: %s", strerror(errno));
+               sr_err("Send error: %s", g_strerror(errno));
                return SR_ERR;
        }
 
@@ -173,7 +183,7 @@ static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
        len = recv(tcp->socket, buf, maxlen, 0);
 
        if (len < 0) {
-               sr_err("Receive error: %s", strerror(errno));
+               sr_err("Receive error: %s", g_strerror(errno));
                return SR_ERR;
        }
 
@@ -184,6 +194,21 @@ static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
        return len;
 }
 
+static int scpi_tcp_raw_write_data(void *priv, char *buf, int len)
+{
+       struct scpi_tcp *tcp = priv;
+       int sentlen;
+
+       sentlen = send(tcp->socket, buf, len, 0);
+
+       if (sentlen < 0) {
+               sr_err("Send error: %s.", g_strerror(errno));
+               return SR_ERR;
+       }
+
+       return sentlen;
+}
+
 static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
 {
        struct scpi_tcp *tcp = priv;
@@ -193,7 +218,7 @@ static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
                len = recv(tcp->socket, tcp->length_buf + tcp->length_bytes_read,
                                LENGTH_BYTES - tcp->length_bytes_read, 0);
                if (len < 0) {
-                       sr_err("Receive error: %s", strerror(errno));
+                       sr_err("Receive error: %s", g_strerror(errno));
                        return SR_ERR;
                }
 
@@ -211,7 +236,7 @@ static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
        len = recv(tcp->socket, buf, maxlen, 0);
 
        if (len < 0) {
-               sr_err("Receive error: %s", strerror(errno));
+               sr_err("Receive error: %s", g_strerror(errno));
                return SR_ERR;
        }
 
@@ -228,9 +253,9 @@ static int scpi_tcp_read_complete(void *priv)
                        tcp->response_bytes_read >= tcp->response_length);
 }
 
-static int scpi_tcp_close(void *priv)
+static int scpi_tcp_close(struct sr_scpi_dev_inst *scpi)
 {
-       struct scpi_tcp *tcp = priv;
+       struct scpi_tcp *tcp = scpi->priv;
 
        if (close(tcp->socket) < 0)
                return SR_ERR;
@@ -249,14 +274,17 @@ static void scpi_tcp_free(void *priv)
 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
        .name          = "RAW TCP",
        .prefix        = "tcp-raw",
+       .transport     = SCPI_TRANSPORT_RAW_TCP,
        .priv_size     = sizeof(struct scpi_tcp),
        .dev_inst_new  = scpi_tcp_dev_inst_new,
        .open          = scpi_tcp_open,
+       .connection_id = scpi_tcp_connection_id,
        .source_add    = scpi_tcp_source_add,
        .source_remove = scpi_tcp_source_remove,
        .send          = scpi_tcp_send,
        .read_begin    = scpi_tcp_read_begin,
        .read_data     = scpi_tcp_raw_read_data,
+       .write_data    = scpi_tcp_raw_write_data,
        .read_complete = scpi_tcp_read_complete,
        .close         = scpi_tcp_close,
        .free          = scpi_tcp_free,
@@ -265,9 +293,11 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
 SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
        .name          = "RIGOL TCP",
        .prefix        = "tcp-rigol",
+       .transport     = SCPI_TRANSPORT_RIGOL_TCP,
        .priv_size     = sizeof(struct scpi_tcp),
        .dev_inst_new  = scpi_tcp_dev_inst_new,
        .open          = scpi_tcp_open,
+       .connection_id = scpi_tcp_connection_id,
        .source_add    = scpi_tcp_source_add,
        .source_remove = scpi_tcp_source_remove,
        .send          = scpi_tcp_send,