]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi_tcp.c
build: Portability fixes.
[libsigrok.git] / hardware / common / scpi_tcp.c
index d84c7e8fa7f4dde1620d95a9a271a57bdfd7583e..ef3a6c7564e219a65e7f921a4bc72bb6f58501e1 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifdef _WIN32
+#define _WIN32_WINNT 0x0501
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
 #include <glib.h>
 #include <string.h>
 #include <unistd.h>
+#ifndef _WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#endif
 #include <errno.h>
 
-/* Message logging helpers with subsystem-specific prefix string. */
-#define LOG_PREFIX "scpi_tcp: "
-#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
-#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
-#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
-#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
-#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
+#define LOG_PREFIX "scpi_tcp"
+
+#define LENGTH_BYTES 4
 
 struct scpi_tcp {
        char *address;
        char *port;
        int socket;
+       char length_buf[LENGTH_BYTES];
+       int length_bytes_read;
+       int response_length;
+       int response_bytes_read;
 };
 
-SR_PRIV int scpi_tcp_open(void *priv)
+static int scpi_tcp_dev_inst_new(void *priv, struct drv_context *drvc,
+               const char *resource, char **params, const char *serialcomm)
+{
+       struct scpi_tcp *tcp = priv;
+
+       (void)drvc;
+       (void)resource;
+       (void)serialcomm;
+
+       if (!params || !params[1] || !params[2]) {
+               sr_err("Invalid parameters.");
+               return SR_ERR;
+       }
+
+       tcp->address = g_strdup(params[1]);
+       tcp->port    = g_strdup(params[2]);
+       tcp->socket  = -1;
+
+       return SR_OK;
+}
+
+static int scpi_tcp_open(void *priv)
 {
        struct scpi_tcp *tcp = priv;
        struct addrinfo hints;
@@ -86,28 +115,32 @@ SR_PRIV int scpi_tcp_open(void *priv)
        return SR_OK;
 }
 
-SR_PRIV int scpi_tcp_source_add(void *priv, int events, int timeout,
-                       sr_receive_data_callback_t cb, void *cb_data)
+static int scpi_tcp_source_add(struct sr_session *session, void *priv,
+               int events, int timeout, sr_receive_data_callback cb, void *cb_data)
 {
        struct scpi_tcp *tcp = priv;
 
-       return sr_source_add(tcp->socket, events, timeout, cb, cb_data);
+       return sr_session_source_add(session, tcp->socket, events, timeout,
+                       cb, cb_data);
 }
 
-SR_PRIV int scpi_tcp_source_remove(void *priv)
+static int scpi_tcp_source_remove(struct sr_session *session, void *priv)
 {
        struct scpi_tcp *tcp = priv;
 
-       return sr_source_remove(tcp->socket);
+       return sr_session_source_remove(session, tcp->socket);
 }
 
-SR_PRIV int scpi_tcp_send(void *priv, const char *command)
+static int scpi_tcp_send(void *priv, const char *command)
 {
        struct scpi_tcp *tcp = priv;
        int len, out;
+       char *terminated_command;
 
-       len = strlen(command);
-       out = send(tcp->socket, command, len, 0);
+       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);
 
        if (out < 0) {
                sr_err("Send error: %s", strerror(errno));
@@ -124,40 +157,59 @@ SR_PRIV int scpi_tcp_send(void *priv, const char *command)
        return SR_OK;
 }
 
-SR_PRIV int scpi_tcp_receive(void *priv, char **scpi_response)
+static int scpi_tcp_read_begin(void *priv)
 {
        struct scpi_tcp *tcp = priv;
-       GString *response;
-       char buf[256];
-       int len;
 
-       response = g_string_sized_new(1024);
+       tcp->response_bytes_read = 0;
+       tcp->length_bytes_read = 0;
 
-       len = recv(tcp->socket, buf, sizeof(buf), 0);
+       return SR_OK;
+}
+
+static int scpi_tcp_raw_read_data(void *priv, char *buf, int maxlen)
+{
+       struct scpi_tcp *tcp = priv;
+       int len;
+
+       len = recv(tcp->socket, buf, maxlen, 0);
 
        if (len < 0) {
                sr_err("Receive error: %s", strerror(errno));
-               g_string_free(response, TRUE);
                return SR_ERR;
        }
 
-       response = g_string_append_len(response, buf, len);
-
-       *scpi_response = response->str;
+       tcp->length_bytes_read = LENGTH_BYTES;
+       tcp->response_length = len < maxlen ? len : maxlen + 1;
+       tcp->response_bytes_read = len;
 
-       sr_dbg("SCPI response received (length %d): '%.50s'",
-              response->len, response->str);
-
-       g_string_free(response, FALSE);
-
-       return SR_OK;
+       return len;
 }
 
-SR_PRIV int scpi_tcp_read(void *priv, char *buf, int maxlen)
+static int scpi_tcp_rigol_read_data(void *priv, char *buf, int maxlen)
 {
        struct scpi_tcp *tcp = priv;
        int len;
 
+       if (tcp->length_bytes_read < LENGTH_BYTES) {
+               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));
+                       return SR_ERR;
+               }
+
+               tcp->length_bytes_read += len;
+
+               if (tcp->length_bytes_read < LENGTH_BYTES)
+                       return 0;
+               else
+                       tcp->response_length = RL32(tcp->length_buf);
+       }
+
+       if (tcp->response_bytes_read >= tcp->response_length)
+               return SR_ERR;
+
        len = recv(tcp->socket, buf, maxlen, 0);
 
        if (len < 0) {
@@ -165,10 +217,20 @@ SR_PRIV int scpi_tcp_read(void *priv, char *buf, int maxlen)
                return SR_ERR;
        }
 
+       tcp->response_bytes_read += len;
+
        return len;
 }
 
-SR_PRIV int scpi_tcp_close(void *priv)
+static int scpi_tcp_read_complete(void *priv)
+{
+       struct scpi_tcp *tcp = priv;
+
+       return (tcp->length_bytes_read == LENGTH_BYTES &&
+                       tcp->response_bytes_read >= tcp->response_length);
+}
+
+static int scpi_tcp_close(void *priv)
 {
        struct scpi_tcp *tcp = priv;
 
@@ -178,37 +240,42 @@ SR_PRIV int scpi_tcp_close(void *priv)
        return SR_OK;
 }
 
-SR_PRIV void scpi_tcp_free(void *priv)
+static void scpi_tcp_free(void *priv)
 {
        struct scpi_tcp *tcp = priv;
 
        g_free(tcp->address);
        g_free(tcp->port);
-       g_free(tcp);
 }
 
-SR_PRIV struct sr_scpi_dev_inst *scpi_tcp_dev_inst_new(const char *address,
-               const char *port)
-{
-       struct sr_scpi_dev_inst *scpi;
-       struct scpi_tcp *tcp;
-
-       scpi = g_malloc(sizeof(struct sr_scpi_dev_inst));
-       tcp = g_malloc0(sizeof(struct scpi_tcp));
-
-       tcp->address = g_strdup(address);
-       tcp->port = g_strdup(port);
-       tcp->socket = -1;
-
-       scpi->open = scpi_tcp_open;
-       scpi->source_add = scpi_tcp_source_add;
-       scpi->source_remove = scpi_tcp_source_remove;
-       scpi->send = scpi_tcp_send;
-       scpi->receive = scpi_tcp_receive;
-       scpi->read = scpi_tcp_read;
-       scpi->close = scpi_tcp_close;
-       scpi->free = scpi_tcp_free;
-       scpi->priv = tcp;
-
-       return scpi;
-}
+SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_raw_dev = {
+       .name          = "RAW TCP",
+       .prefix        = "tcp-raw",
+       .priv_size     = sizeof(struct scpi_tcp),
+       .dev_inst_new  = scpi_tcp_dev_inst_new,
+       .open          = scpi_tcp_open,
+       .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,
+       .read_complete = scpi_tcp_read_complete,
+       .close         = scpi_tcp_close,
+       .free          = scpi_tcp_free,
+};
+
+SR_PRIV const struct sr_scpi_dev_inst scpi_tcp_rigol_dev = {
+       .name          = "RIGOL TCP",
+       .prefix        = "tcp-rigol",
+       .priv_size     = sizeof(struct scpi_tcp),
+       .dev_inst_new  = scpi_tcp_dev_inst_new,
+       .open          = scpi_tcp_open,
+       .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_rigol_read_data,
+       .read_complete = scpi_tcp_read_complete,
+       .close         = scpi_tcp_close,
+       .free          = scpi_tcp_free,
+};