]> sigrok.org Git - libsigrok.git/commitdiff
ols: move sigrok channel creation from protocol.c to api.c
authorGerhard Sittig <redacted>
Sun, 14 Aug 2022 19:07:07 +0000 (21:07 +0200)
committerGerhard Sittig <redacted>
Wed, 24 Aug 2022 18:05:25 +0000 (20:05 +0200)
It's unexpected that several code paths redundantly create sigrok
channels, and that most of them reside in the protocol.c source file.
Concentrate channel creation in api.c instead in the scan() routine,
to remain aware during maintenance.

Fallback to the maximum channel count for SUMP compatibles, prefer
metadata gathered from the device when available, including quirks for
models which are known to deviate. Just setup the number during probe,
and create the channels at the end of scan() in common code.

Change the channel count struct member to unsigned while we are here.
Shorten a long comment line to not have to break it.

src/hardware/openbench-logic-sniffer/api.c
src/hardware/openbench-logic-sniffer/protocol.c
src/hardware/openbench-logic-sniffer/protocol.h

index 35f6be7e24582f7e65b834e752201c6570afbb16..6e06f7e5b98acc74b961b31357d52c5bbd67a3be 100644 (file)
@@ -103,6 +103,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        const char *conn, *serialcomm;
        char buf[4] = { 0, 0, 0, 0 };
        struct dev_context *devc;
+       size_t ch_max;
 
        conn = serialcomm = NULL;
        for (l = options; l; l = l->next) {
@@ -185,7 +186,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
 
        /*
         * Definitely using the OLS protocol, check if it supports
-        * the metadata command.
+        * the metadata command. Otherwise assign generic values.
+        * Create as many sigrok channels as was determined when
+        * the device was probed.
         */
        send_shortcommand(serial, CMD_METADATA);
        g_usleep(RESPONSE_DELAY_US);
@@ -193,14 +196,19 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                /* Got metadata. */
                (void)ols_get_metadata(sdi);
        } else {
-               /* Not an OLS -- some other board that uses the sump protocol. */
+               /* Not an OLS -- some other board using the SUMP protocol. */
                sr_info("Device does not support metadata.");
                sdi->vendor = g_strdup("Sump");
                sdi->model = g_strdup("Logic Analyzer");
                sdi->version = g_strdup("v1.0");
-               for (i = 0; i < ARRAY_SIZE(ols_channel_names); i++)
-                       sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
-                                      ols_channel_names[i]);
+               devc->max_channels = ARRAY_SIZE(ols_channel_names);
+       }
+       ch_max = ARRAY_SIZE(ols_channel_names);
+       if (devc->max_channels && ch_max > devc->max_channels)
+               ch_max = devc->max_channels;
+       for (i = 0; i < ch_max; i++) {
+               sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
+                       ols_channel_names[i]);
        }
        /* Configure samplerate and divider. */
        if (ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK)
index 2bcf9f411335a795a2e47155a9bfe70a16ad39cd..528b05ce0ed6ef70a08b7769500d805327b7dadb 100644 (file)
@@ -139,18 +139,6 @@ static int convert_trigger(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan)
-{
-       struct dev_context *devc = sdi->priv;
-       int i;
-
-       for (i = 0; i < num_chan; i++)
-               sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
-                              ols_channel_names[i]);
-
-       devc->max_channels = num_chan;
-}
-
 static void ols_metadata_quirks(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
@@ -165,7 +153,7 @@ static void ols_metadata_quirks(struct sr_dev_inst *sdi)
        is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0;
        if (is_shrimp) {
                if (!devc->max_channels)
-                       ols_channel_new(sdi, 4);
+                       devc->max_channels = 4;
                if (!devc->max_samples)
                        devc->max_samples = 256 * 1024;
                if (!devc->max_samplerate)
@@ -252,7 +240,7 @@ SR_PRIV int ols_get_metadata(struct sr_dev_inst *sdi)
                        switch (key) {
                        case METADATA_TOKEN_NUM_PROBES_LONG:
                                /* Number of usable channels */
-                               ols_channel_new(sdi, tmp_int);
+                               devc->max_channels = tmp_int;
                                break;
                        case METADATA_TOKEN_SAMPLE_MEMORY_BYTES:
                                /* Amount of sample memory available (bytes) */
@@ -286,7 +274,7 @@ SR_PRIV int ols_get_metadata(struct sr_dev_inst *sdi)
                        switch (key) {
                        case METADATA_TOKEN_NUM_PROBES_SHORT:
                                /* Number of usable channels */
-                               ols_channel_new(sdi, tmp_c);
+                               devc->max_channels = tmp_c;
                                break;
                        case METADATA_TOKEN_PROTOCOL_VERSION_SHORT:
                                /* protocol version */
index 2506c4a95d4bbab2f9aafd832acbebde1d5b1a43..96917743f72aabc951aa0c5957d64cfe1d7e6e7b 100644 (file)
 
 struct dev_context {
        /* constant device properties: */
-       int max_channels;
+       size_t max_channels;
        uint32_t max_samples;
        uint32_t max_samplerate;
        uint32_t protocol_version;