]> sigrok.org Git - libsigrok.git/blobdiff - hardware/serial-dmm/api.c
serial-dmm: Cosmetics, documentation fixes.
[libsigrok.git] / hardware / serial-dmm / api.c
index 23cb9b356363cc2a504159e4f426965447e9bdcb..49e68a218c4d277eec0dfc6da4827ff07b4b5ebb 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <glib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
+#include <glib.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 #include "protocol.h"
@@ -59,9 +59,6 @@ SR_PRIV struct sr_dev_driver pce_pce_dm32_driver_info;
 SR_PRIV struct sr_dev_driver radioshack_22_168_driver_info;
 SR_PRIV struct sr_dev_driver radioshack_22_812_driver_info;
 
-/* After hw_init() this will point to a device-specific entry (see above). */
-static struct sr_dev_driver *di = NULL;
-
 SR_PRIV struct dmm_info dmms[] = {
        {
                "Digitek", "DT4000ZC", "2400/8n1", 2400,
@@ -143,16 +140,15 @@ SR_PRIV struct dmm_info dmms[] = {
 };
 
 /* Properly close and free all devices. */
-static int clear_instances(void)
+static int clear_instances(int dmm)
 {
        struct sr_dev_inst *sdi;
        struct drv_context *drvc;
        struct dev_context *devc;
        GSList *l;
+       struct sr_dev_driver *di;
 
-       /* di is not necessarily initialized */
-       if (!di)
-               return SR_OK;
+       di = dmms[dmm].di;
 
        if (!(drvc = di->priv))
                return SR_OK;
@@ -181,31 +177,14 @@ static int hw_init(struct sr_context *sr_ctx, int dmm)
                return SR_ERR_MALLOC;
        }
 
-       di = dmms[dmm].di;
        sr_dbg("Selected '%s' subdriver.", dmms[dmm].di->name);
 
        drvc->sr_ctx = sr_ctx;
-       //// di->priv = drvc;
        dmms[dmm].di->priv = drvc;
 
        return SR_OK;
 }
 
-/* Driver-specific hw_init() function wrappers */
-#define HW_INIT(X) static int hw_init_##X(struct sr_context *sr_ctx) \
-                               { return hw_init(sr_ctx, X); }
-HW_INIT(DIGITEK_DT4000ZC)
-HW_INIT(TEKPOWER_TP4000ZC)
-HW_INIT(METEX_ME31)
-HW_INIT(PEAKTECH_3410)
-HW_INIT(MASTECH_MAS345)
-HW_INIT(VA_VA18B)
-HW_INIT(METEX_M3640D)
-HW_INIT(PEAKTECH_4370)
-HW_INIT(PCE_PCE_DM32)
-HW_INIT(RADIOSHACK_22_168)
-HW_INIT(RADIOSHACK_22_812)
-
 static GSList *scan(const char *conn, const char *serialcomm, int dmm)
 {
        struct sr_dev_inst *sdi;
@@ -224,22 +203,12 @@ static GSList *scan(const char *conn, const char *serialcomm, int dmm)
        if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
-       sr_info("Probing port %s.", conn);
+       sr_info("Probing serial port %s.", conn);
 
-       //// drvc = di->priv;
        drvc = dmms[dmm].di->priv;
        devices = NULL;
        serial_flush(serial);
 
-       /*
-        * There's no way to get an ID from the multimeter. It just sends data
-        * periodically, so the best we can do is check if the packets match
-        * the expected format.
-        */
-
-       /* Let's get a bit of data and see if we can find a packet. */
-       len = sizeof(buf);
-
        /* Request a packet if the DMM requires this. */
        if (dmms[dmm].packet_request) {
                if ((ret = dmms[dmm].packet_request(serial)) < 0) {
@@ -248,6 +217,14 @@ static GSList *scan(const char *conn, const char *serialcomm, int dmm)
                }
        }
 
+       /*
+        * There's no way to get an ID from the multimeter. It just sends data
+        * periodically (or upon request), so the best we can do is check if
+        * the packets match the expected format.
+        */
+
+       /* Let's get a bit of data and see if we can find a packet. */
+       len = sizeof(buf);
        ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
                                   dmms[dmm].packet_valid, 1000,
                                   dmms[dmm].baudrate);
@@ -258,8 +235,8 @@ static GSList *scan(const char *conn, const char *serialcomm, int dmm)
         * If we dropped more than two packets worth of data, something is
         * wrong. We shouldn't quit however, since the dropped bytes might be
         * just zeroes at the beginning of the stream. Those can occur as a
-        * combination of the nonstandard cable that ships with this device and
-        * the serial port or USB to serial adapter.
+        * combination of the nonstandard cable that ships with some devices
+        * and the serial port or USB to serial adapter.
         */
        dropped = len - dmms[dmm].packet_size;
        if (dropped > 2 * dmms[dmm].packet_size)
@@ -277,6 +254,7 @@ static GSList *scan(const char *conn, const char *serialcomm, int dmm)
        }
 
        devc->serial = serial;
+       devc->subdriver = dmm;
 
        sdi->priv = devc;
        sdi->driver = dmms[dmm].di;
@@ -324,21 +302,6 @@ static GSList *hw_scan(GSList *options, int dmm)
        return devices;
 }
 
-/* Driver-specific hw_scan() function wrappers */
-#define HW_SCAN(X) static GSList *hw_scan_##X(GSList *options) \
-                               { return hw_scan(options, X); }
-HW_SCAN(DIGITEK_DT4000ZC)
-HW_SCAN(TEKPOWER_TP4000ZC)
-HW_SCAN(METEX_ME31)
-HW_SCAN(PEAKTECH_3410)
-HW_SCAN(MASTECH_MAS345)
-HW_SCAN(VA_VA18B)
-HW_SCAN(METEX_M3640D)
-HW_SCAN(PEAKTECH_4370)
-HW_SCAN(PCE_PCE_DM32)
-HW_SCAN(RADIOSHACK_22_168)
-HW_SCAN(RADIOSHACK_22_812)
-
 static GSList *hw_dev_list(int dmm)
 {
        struct drv_context *drvc;
@@ -348,21 +311,6 @@ static GSList *hw_dev_list(int dmm)
        return drvc->instances;
 }
 
-/* Driver-specific hw_dev_list() function wrappers */
-#define HW_DEV_LIST(X) static GSList *hw_dev_list_##X(void) \
-                               { return hw_dev_list(X); }
-HW_DEV_LIST(DIGITEK_DT4000ZC)
-HW_DEV_LIST(TEKPOWER_TP4000ZC)
-HW_DEV_LIST(METEX_ME31)
-HW_DEV_LIST(PEAKTECH_3410)
-HW_DEV_LIST(MASTECH_MAS345)
-HW_DEV_LIST(VA_VA18B)
-HW_DEV_LIST(METEX_M3640D)
-HW_DEV_LIST(PEAKTECH_4370)
-HW_DEV_LIST(PCE_PCE_DM32)
-HW_DEV_LIST(RADIOSHACK_22_168)
-HW_DEV_LIST(RADIOSHACK_22_812)
-
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
@@ -397,9 +345,9 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int hw_cleanup(void)
+static int hw_cleanup(int dmm)
 {
-       clear_instances();
+       clear_instances(dmm);
 
        return SR_OK;
 }
@@ -498,7 +446,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 
        /* Poll every 50ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 50,
-                     dmms[sdi->driver->subdriver].receive_data, (void *)sdi);
+                     dmms[devc->subdriver].receive_data, (void *)sdi);
 
        return SR_OK;
 }
@@ -529,17 +477,34 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        return SR_OK;
 }
 
-/* Driver structs */
+/* Driver-specific API function wrappers */
+#define HW_INIT(X) \
+static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); }
+#define HW_CLEANUP(X) \
+static int hw_cleanup_##X(void) { return hw_cleanup(X); }
+#define HW_SCAN(X) \
+static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); }
+#define HW_DEV_LIST(X) \
+static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); }
+#define CLEAR_INSTANCES(X) \
+static int clear_instances_##X(void) { return clear_instances(X); }
+
+/* Driver structs and API function wrappers */
 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
+HW_INIT(ID_UPPER) \
+HW_CLEANUP(ID_UPPER) \
+HW_SCAN(ID_UPPER) \
+HW_DEV_LIST(ID_UPPER) \
+CLEAR_INSTANCES(ID_UPPER) \
 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
        .name = NAME, \
        .longname = LONGNAME, \
        .api_version = 1, \
        .init = hw_init_##ID_UPPER, \
-       .cleanup = hw_cleanup, \
+       .cleanup = hw_cleanup_##ID_UPPER, \
        .scan = hw_scan_##ID_UPPER, \
        .dev_list = hw_dev_list_##ID_UPPER, \
-       .dev_clear = clear_instances, \
+       .dev_clear = clear_instances_##ID_UPPER, \
        .dev_open = hw_dev_open, \
        .dev_close = hw_dev_close, \
        .info_get = hw_info_get, \
@@ -547,8 +512,8 @@ SR_PRIV struct sr_dev_driver ID##_driver_info = { \
        .dev_acquisition_start = hw_dev_acquisition_start, \
        .dev_acquisition_stop = hw_dev_acquisition_stop, \
        .priv = NULL, \
-       .subdriver = ID_UPPER, \
 };
+
 DRV(digitek_dt4000zc, DIGITEK_DT4000ZC, "digitek-dt4000zc", "Digitek DT4000ZC")
 DRV(tekpower_tp4000zc, TEKPOWER_TP4000ZC, "tekpower-tp4000zc", "TekPower TP4000ZC")
 DRV(metex_me31, METEX_ME31, "metex-me31", "Metex ME-31")