]> sigrok.org Git - libsigrok.git/blobdiff - src/libsigrok-internal.h
serial: introduce local receive data buffer
[libsigrok.git] / src / libsigrok-internal.h
index 812cf3ceca11811aebe7569c6e14439375a7febb..e1623f629b0e2c675cf59386eea95d542e8bf5bb 100644 (file)
@@ -26,6 +26,8 @@
 #ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
 #define LIBSIGROK_LIBSIGROK_INTERNAL_H
 
+#include "config.h"
+
 #include <glib.h>
 #ifdef HAVE_LIBSERIALPORT
 #include <libserialport.h>
@@ -34,7 +36,9 @@
 #include <libusb.h>
 #endif
 #include <stdarg.h>
+#include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 struct zip;
 struct zip_stat;
@@ -720,20 +724,26 @@ struct sr_usb_dev_inst {
 };
 #endif
 
-#ifdef HAVE_LIBSERIALPORT
+struct sr_serial_dev_inst;
+#ifdef HAVE_SERIAL_COMM
+struct ser_lib_functions;
 struct sr_serial_dev_inst {
        /** Port name, e.g. '/dev/tty42'. */
        char *port;
        /** Comm params for serial_set_paramstr(). */
        char *serialcomm;
+       struct ser_lib_functions *lib_funcs;
        struct {
                int bit_rate;
                int data_bits;
                int parity_bits;
                int stop_bits;
        } comm_params;
+       GString *rcv_buffer;
+#ifdef HAVE_LIBSERIALPORT
        /** libserialport port handle */
        struct sp_port *sp_data;
+#endif
 };
 #endif
 
@@ -835,7 +845,31 @@ SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
 #endif
 
-#ifdef HAVE_LIBSERIALPORT
+#ifdef HAVE_SERIAL_COMM
+#ifndef HAVE_LIBSERIALPORT
+/*
+ * Some identifiers which initially got provided by libserialport are
+ * used internally within the libsigrok serial layer's implementation,
+ * while libserialport no longer is the exclusive provider of serial
+ * communication support. Declare the identifiers here so they remain
+ * available across all build configurations.
+ */
+enum libsp_parity {
+       SP_PARITY_NONE = 0,
+       SP_PARITY_ODD = 1,
+       SP_PARITY_EVEN = 2,
+       SP_PARITY_MARK = 3,
+       SP_PARITY_SPACE = 4,
+};
+
+enum libsp_flowcontrol {
+       SP_FLOWCONTROL_NONE = 0,
+       SP_FLOWCONTROL_XONXOFF = 1,
+       SP_FLOWCONTROL_RTSCTS = 2,
+       SP_FLOWCONTROL_DTRDSR = 3,
+};
+#endif
+
 /* Serial-specific instances */
 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
                const char *serialcomm);
@@ -952,7 +986,7 @@ SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
-#ifdef HAVE_LIBSERIALPORT
+#ifdef HAVE_SERIAL_COMM
 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
 #endif
@@ -1062,7 +1096,7 @@ SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf
 
 /*--- serial.c --------------------------------------------------------------*/
 
-#ifdef HAVE_LIBSERIALPORT
+#ifdef HAVE_SERIAL_COMM
 enum {
        SERIAL_RDWR = 1,
        SERIAL_RDONLY = 2,
@@ -1070,10 +1104,15 @@ enum {
 
 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
 
+typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
+               const char *desc);
+typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
+
 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
+SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
                const void *buf, size_t count, unsigned int timeout_ms);
 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
@@ -1102,6 +1141,42 @@ SR_PRIV int serial_source_remove(struct sr_session *session,
                struct sr_serial_dev_inst *serial);
 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
+
+SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
+SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
+SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
+               const uint8_t *data, size_t len);
+SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
+               uint8_t *data, size_t len);
+
+struct ser_lib_functions {
+       int (*open)(struct sr_serial_dev_inst *serial, int flags);
+       int (*close)(struct sr_serial_dev_inst *serial);
+       int (*flush)(struct sr_serial_dev_inst *serial);
+       int (*drain)(struct sr_serial_dev_inst *serial);
+       int (*write)(struct sr_serial_dev_inst *serial,
+                       const void *buf, size_t count,
+                       int nonblocking, unsigned int timeout_ms);
+       int (*read)(struct sr_serial_dev_inst *serial,
+                       void *buf, size_t count,
+                       int nonblocking, unsigned int timeout_ms);
+       int (*set_params)(struct sr_serial_dev_inst *serial,
+                       int baudrate, int bits, int parity, int stopbits,
+                       int flowcontrol, int rts, int dtr);
+       int (*setup_source_add)(struct sr_session *session,
+                       struct sr_serial_dev_inst *serial,
+                       int events, int timeout,
+                       sr_receive_data_callback cb, void *cb_data);
+       int (*setup_source_remove)(struct sr_session *session,
+                       struct sr_serial_dev_inst *serial);
+       GSList *(*list)(GSList *list, sr_ser_list_append_t append);
+       GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
+                       uint16_t vendor_id, uint16_t product_id);
+       int (*get_frame_format)(struct sr_serial_dev_inst *serial,
+                       int *baud, int *bits);
+       size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
+};
+extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
 #endif
 
 /*--- ezusb.c ---------------------------------------------------------------*/
@@ -1346,7 +1421,7 @@ struct metex14_info {
        gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
 };
 
-#ifdef HAVE_LIBSERIALPORT
+#ifdef HAVE_SERIAL_COMM
 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
 #endif
 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
@@ -1477,7 +1552,7 @@ struct asycii_info {
        gboolean is_invalid;
 };
 
-#ifdef HAVE_LIBSERIALPORT
+#ifdef HAVE_SERIAL_COMM
 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
 #endif
 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);