]> sigrok.org Git - libsigrok.git/commitdiff
fluke-dmm: reduce indentation in 18x/190/28x dispatch logic
authorGerhard Sittig <redacted>
Fri, 17 Mar 2023 14:13:15 +0000 (15:13 +0100)
committerGerhard Sittig <redacted>
Sun, 19 Mar 2023 10:42:03 +0000 (11:42 +0100)
Address several style nits in the fluke-dmm driver. Rephrase a lengthy
if-else-if sequence by means of switch-case, so that conditions are
easier to review. Use glib to get the vector length. Handle the trivial
case first, which reduces indentation for the remaining complex logic.
Fixup comment style, break long text lines.

src/hardware/fluke-dmm/protocol.c

index de565e49265728e1731844d5d419d551d4ca1737..20487f087da177fd339c0302510d769578f121ae 100644 (file)
@@ -449,6 +449,7 @@ static void handle_line(const struct sr_dev_inst *sdi)
        struct sr_serial_dev_inst *serial;
        int num_tokens, n, i;
        char cmd[16], **tokens;
+       int ret;
 
        devc = sdi->priv;
        serial = sdi->conn;
@@ -466,35 +467,48 @@ static void handle_line(const struct sr_dev_inst *sdi)
 
        tokens = g_strsplit(devc->buf, ",", 0);
        if (tokens[0]) {
-               if (devc->profile->model == FLUKE_87 || devc->profile->model == FLUKE_89 ||
-                               devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
+               switch (devc->profile->model) {
+               case FLUKE_87:
+               case FLUKE_89:
+               case FLUKE_187:
+               case FLUKE_189:
                        devc->expect_response = FALSE;
                        handle_qm_18x(sdi, tokens);
-               } else if (devc->profile->model == FLUKE_287 || devc->profile->model == FLUKE_289) {
+                       break;
+               case FLUKE_287:
+               case FLUKE_289:
                        devc->expect_response = FALSE;
                        handle_qm_28x(sdi, tokens);
-               } else if (devc->profile->model == FLUKE_190) {
+                       break;
+               case FLUKE_190:
                        devc->expect_response = FALSE;
-                       for (num_tokens = 0; tokens[num_tokens]; num_tokens++);
-                       if (num_tokens >= 7) {
-                               /* Response to QM: this is a comma-separated list of
-                                * fields with metadata about the measurement. This
-                                * format can return multiple sets of metadata,
-                                * split into sets of 7 tokens each. */
-                               devc->meas_type = 0;
-                               for (i = 0; i < num_tokens; i += 7)
-                                       handle_qm_19x_meta(sdi, tokens + i);
-                               if (devc->meas_type) {
-                                       /* Slip the request in now, before the main
-                                        * timer loop asks for metadata again. */
-                                       n = sprintf(cmd, "QM %d\r", devc->meas_type);
-                                       if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
-                                               sr_err("Unable to send QM (measurement).");
-                               }
-                       } else {
+                       num_tokens = g_strv_length(tokens);
+                       if (num_tokens < 7) {
                                /* Response to QM <n> measurement request. */
                                handle_qm_19x_data(sdi, tokens);
+                               break;
+                       }
+                       /*
+                        * Response to QM: This is a comma-separated list of
+                        * fields with metadata about the measurement. This
+                        * format can return multiple sets of metadata,
+                        * split into sets of 7 tokens each.
+                        */
+                       devc->meas_type = 0;
+                       for (i = 0; i < num_tokens; i += 7)
+                               handle_qm_19x_meta(sdi, tokens + i);
+                       if (devc->meas_type) {
+                               /*
+                                * Slip the request in now, before the main
+                                * timer loop asks for metadata again.
+                                */
+                               n = sprintf(cmd, "QM %d\r", devc->meas_type);
+                               ret = serial_write_blocking(serial,
+                                       cmd, n, SERIAL_WRITE_TIMEOUT_MS);
+                               if (ret < 0)
+                                       sr_err("Cannot send QM (measurement).");
                        }
+                       break;
                }
        }
        g_strfreev(tokens);