]> sigrok.org Git - libsigrok.git/commitdiff
Add and use std_session_send_df_header().
authorUwe Hermann <redacted>
Wed, 6 Feb 2013 18:57:32 +0000 (19:57 +0100)
committerUwe Hermann <redacted>
Thu, 7 Feb 2013 09:46:28 +0000 (10:46 +0100)
This is a small helper function which sends the SR_DF_HEADER packet that
drivers usually emit in their hw_dev_acquisition_start() API callback.
It simplifies and shortens the hw_dev_acquisition_start() functions
quite a bit.

It also simplifies the input modules which send an SR_DF_HEADER packet, too.

This patch also automatically removes some unneeded malloc/free in some
drivers for the 'packet' and 'header' structs used for SR_DF_HEADER.

26 files changed:
hardware/agilent-dmm/api.c
hardware/alsa/api.c
hardware/asix-sigma/asix-sigma.c
hardware/brymen-dmm/api.c
hardware/chronovu-la8/api.c
hardware/colead-slm/api.c
hardware/demo/demo.c
hardware/fluke-dmm/api.c
hardware/fx2lafw/fx2lafw.c
hardware/hantek-dso/api.c
hardware/lascar-el-usb/api.c
hardware/link-mso19/api.c
hardware/mic-985xx/api.c
hardware/openbench-logic-sniffer/api.c
hardware/rigol-ds1xx2/api.c
hardware/serial-dmm/api.c
hardware/tondaj-sl-814/api.c
hardware/uni-t-dmm/api.c
hardware/victor-dmm/api.c
hardware/zeroplus-logic-cube/zeroplus.c
input/binary.c
input/chronovu_la8.c
input/vcd.c
libsigrok-internal.h
session_driver.c
std.c

index 64f6275ada5da29d0f9b341db036a596311a62f6..c0b10e0701d131a414ce99e451fafcd31d43ed4f 100644 (file)
@@ -289,8 +289,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
@@ -298,17 +296,10 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR_BUG;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 100ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
index c546b40036cfa204fbd230b7d2a45c6bcd3cced6..e2efbf468dc944b21f56f7ac7b91808c4552d39f 100644 (file)
@@ -191,8 +191,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        int count, ret;
        char *endianness;
@@ -276,12 +274,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        }
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER packet.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 10ms, or whenever some data comes in. */
        sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
index acd3acb9db733153bd4ee79257fbd355c65f56c1..fc7b68e680b2e558e50cc2a7ff0006f840fb24c5 100644 (file)
@@ -1266,8 +1266,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_datafeed_packet *packet;
-       struct sr_datafeed_header *header;
        struct clockselect_50 clockselect;
        int frac, triggerpin, ret;
        uint8_t triggerselect = 0;
@@ -1361,29 +1359,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 
        devc->session_dev_id = cb_data;
 
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("%s: packet malloc failed.", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("%s: header malloc failed.", __func__);
-               return SR_ERR_MALLOC;
-       }
-
        /* Send header packet to the session bus. */
-       packet->type = SR_DF_HEADER;
-       packet->payload = header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(devc->session_dev_id, packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Add capture source. */
        sr_source_add(0, G_IO_IN, 10, receive_data, (void *)sdi);
 
-       g_free(header);
-       g_free(packet);
-
        devc->state.state = SIGMA_CAPTURE;
 
        return SR_OK;
index 7d60fd031bbf553e1f87c5f8c0fba7e7da2b0300..f3152e267761aaa8dfaa2b60c7936a3afa84e29c 100644 (file)
@@ -268,8 +268,6 @@ static int hw_dev_config_set(int id, const void *value,
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
@@ -277,8 +275,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR_BUG;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /*
@@ -290,12 +286,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        devc->starttime = g_get_monotonic_time();
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 50ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 50,
index bf80fc6bbcb72b27ae4d262ed87c711947fbc5b1..66cbdb6ecdfa83393d409b55dc222f945ccdd5e3 100644 (file)
@@ -406,8 +406,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        uint8_t buf[4];
        int bytes_written;
 
@@ -432,8 +430,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
-       sr_dbg("Starting acquisition.");
-
        /* Fill acquisition parameters into buf[]. */
        buf[0] = devc->divcount;
        buf[1] = 0xff; /* This byte must always be 0xff. */
@@ -451,17 +447,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
-       sr_dbg("Acquisition started successfully.");
+       sr_dbg("Hardware acquisition started successfully.");
 
        devc->session_dev_id = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->session_dev_id, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Time when we should be done (for detecting trigger timeouts). */
        devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
index ccd5f6029b7ed08b232eb920f8ad099bc73149c7..33c3038d6f1cb64a40497279e71901b910360038 100644 (file)
@@ -234,8 +234,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
@@ -243,17 +241,10 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR_BUG;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 150ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
index 248ee11eabb53756dd3c697ff221f080448b7ce7..8a4bc9a0593c7b15ae5cfdfb44e3c2306716104b 100644 (file)
@@ -387,14 +387,10 @@ static int receive_data(int fd, int revents, void *cb_data)
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       struct sr_datafeed_packet *packet;
-       struct sr_datafeed_header *header;
        struct dev_context *devc;
 
        (void)sdi;
 
-       sr_dbg("Starting acquisition.");
-
        /* TODO: 'devc' is never g_free()'d? */
        if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
                sr_err("%s: devc malloc failed", __func__);
@@ -434,28 +430,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_session_source_add_channel(devc->channels[0], G_IO_IN | G_IO_ERR,
                    40, receive_data, devc);
 
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("%s: packet malloc failed", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("%s: header malloc failed", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       packet->type = SR_DF_HEADER;
-       packet->payload = header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(devc->session_dev_id, packet);
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* We use this timestamp to decide how many more samples to send. */
        devc->starttime = g_get_monotonic_time();
 
-       g_free(header);
-       g_free(packet);
-
        return SR_OK;
 }
 
index c8280d4a5c4e3f1cadd9504f7df48fa7cf63a110..0b69010d4dc0762523f4d54f0d185df7be0a0016 100644 (file)
@@ -323,8 +323,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
@@ -332,17 +330,10 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR_BUG;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 100ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
index e3260e55127eee5f071418cd1ecad76c6887d28d..1bbb45470bbbd5fb567cd38eab971752ff2f6079 100644 (file)
@@ -923,8 +923,6 @@ static unsigned int get_timeout(struct dev_context *devc)
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        struct drv_context *drvc;
        struct libusb_transfer *transfer;
@@ -988,11 +986,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                              timeout, receive_data, NULL);
        free(lupfd); /* NOT g_free()! */
 
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(cb_data, &packet);
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        if ((ret = command_start_acquisition(devc->usb->devhdl,
                devc->cur_samplerate, devc->sample_wide)) != SR_OK) {
index 4b0dfe37708819b3e061c3111bc588d0331c8c8b..ebcc0f253fbc23016f70b506725fd30047c4d8e3 100644 (file)
@@ -830,8 +830,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
        const struct libusb_pollfd **lupfd;
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        struct drv_context *drvc = di->priv;
        int i;
@@ -861,11 +859,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        free(lupfd);
 
        /* Send header packet to the session bus. */
-       packet.type = SR_DF_HEADER;
-       packet.payload = (unsigned char *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        return SR_OK;
 }
index 2b442d6dc101e90585bd98a0f30af93416d35148..4cef9f9698df9c0af2611166b457144ef77500a7 100644 (file)
@@ -313,7 +313,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        struct drv_context *drvc = di->priv;
        struct libusb_transfer *xfer_in, *xfer_out;
@@ -336,11 +335,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_dbg("Starting log retrieval.");
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        if (devc->logged_samples == 0) {
                /* This ensures the frontend knows the session is done. */
index 4a71863dc1de0b4b7bb3d19a918934c14d9ade35..0bc02576d70c389bec90eeebdfa23c4498c3d7fc 100644 (file)
@@ -393,8 +393,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet *packet;
-       struct sr_datafeed_header *header;
        struct dev_context *devc;
        int ret = SR_ERR;
 
@@ -445,27 +443,10 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        if (ret != SR_OK)
                return ret;
 
-       sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
-
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("Datafeed packet malloc failed.");
-               return SR_ERR_MALLOC;
-       }
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("Datafeed header malloc failed.");
-               g_free(packet);
-               return SR_ERR_MALLOC;
-       }
-
-       packet->type = SR_DF_HEADER;
-       packet->payload = (unsigned char *)header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(cb_data, packet);
-
-       g_free(header);
-       g_free(packet);
+       sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
 
        return SR_OK;
 }
index ccc21f7395fa1b89dee10670a83df62d1df5a729..bfb942ed118fdcf49fc9eafa557592f4ab793a68 100644 (file)
@@ -256,26 +256,17 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data, int idx)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        devc = sdi->priv;
 
        devc->cb_data = cb_data;
 
-       sr_dbg("Starting acquisition.");
-
        devc->num_samples = 0;
        devc->starttime = g_get_monotonic_time();
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 100ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 100,
index 6511e67852d768c9eeadfb04fe6156e7e9e460c1..56176cc7eda1e3b8496545934543801aea424282 100644 (file)
@@ -334,8 +334,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       struct sr_datafeed_packet *packet;
-       struct sr_datafeed_header *header;
        struct dev_context *devc;
        uint32_t trigger_config[4];
        uint32_t data;
@@ -457,30 +455,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        if (send_shortcommand(devc->serial, CMD_RUN) != SR_OK)
                return SR_ERR;
 
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+
        sr_source_add(devc->serial->fd, G_IO_IN, -1, ols_receive_data,
                      cb_data);
 
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("Datafeed packet malloc failed.");
-               return SR_ERR_MALLOC;
-       }
-
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("Datafeed header malloc failed.");
-               g_free(packet);
-               return SR_ERR_MALLOC;
-       }
-
-       /* Send header packet to the session bus. */
-       packet->type = SR_DF_HEADER;
-       packet->payload = (unsigned char *)header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(cb_data, packet);
-
-       g_free(header);
-       g_free(packet);
-
        return SR_OK;
 }
 
index fe2ca5d271d14477e63fac26b5def09c3eab55de..2fd375fbbdba9702abd489743469d8790d492452 100644 (file)
@@ -431,8 +431,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        char buf[256];
        int len;
 
@@ -445,11 +443,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
 
        /* Send header packet to the session bus. */
-       packet.type = SR_DF_HEADER;
-       packet.payload = (unsigned char *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Hardcoded to CH1 only. */
        devc->enabled_probes = g_slist_append(NULL, sdi->probes->data);
index 2d991dcef251ecbb0996c19285e1a325cf920fd8..b6920819870ab3830629d9a189c95b2464c34ee5 100644 (file)
@@ -410,8 +410,6 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data, int dmm)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        if (!(devc = sdi->priv)) {
@@ -419,8 +417,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR_BUG;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /*
@@ -432,12 +428,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        devc->starttime = g_get_monotonic_time();
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 50ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 50,
index 36ec5c2635d9dba3110e7c03d1dca82969710066..b6d38cd160cbe9cb7c542866377ac805e5e91595 100644 (file)
@@ -229,20 +229,13 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        devc = sdi->priv;
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        /* Poll every 500ms, or whenever some data comes in. */
        sr_source_add(devc->serial->fd, G_IO_IN, 500,
index 9870130a17132cf67f7727058c1b1c27d496edef..7662fb072fde63a85318b22f127f4c2478ecf733 100644 (file)
@@ -229,23 +229,14 @@ static int config_list(int key, const void **data, const struct sr_dev_inst *sdi
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
 
        devc = sdi->priv;
 
-       sr_dbg("Starting acquisition.");
-
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        if (!strcmp(di->name, "uni-t-ut61d")) {
                sr_source_add(0, 0, 10 /* poll_timeout */,
index bfe57c437a96f10124459d539917a44fd59b6a03..871c5de0e7257c2b977c47e092d22a7f0d926fc8 100644 (file)
@@ -363,8 +363,6 @@ static int handle_events(int fd, int revents, void *cb_data)
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        struct drv_context *drvc = di->priv;
        const struct libusb_pollfd **pfd;
@@ -377,17 +375,11 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc = sdi->priv;
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
        for (i = 0; pfd[i]; i++) {
index 32bf026884dcacef44fb3a8960ec966927fbb25b..4d3df029bbc55e34fef737fca07d0ff48d43a39c 100644 (file)
@@ -676,7 +676,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
-       struct sr_datafeed_header header;
        //uint64_t samples_read;
        int res;
        unsigned int packet_num;
@@ -710,11 +709,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_info("Trigger address = 0x%x.",
                analyzer_get_trigger_address(devc->usb->devhdl));
 
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(cb_data, &packet);
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
        if (!(buf = g_try_malloc(PACKET_SIZE))) {
                sr_err("Packet buffer malloc failed.");
index d8e47b52e56ce9b9786b3d10d2f504c56e3c31d5..1c786baebfc2c71ec6b833fa35ee7f16b476deea 100644 (file)
@@ -98,7 +98,6 @@ static int init(struct sr_input *in)
 
 static int loadfile(struct sr_input *in, const char *filename)
 {
-       struct sr_datafeed_header header;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_meta meta;
        struct sr_datafeed_logic logic;
@@ -115,11 +114,7 @@ static int loadfile(struct sr_input *in, const char *filename)
        num_probes = g_slist_length(in->sdi->probes);
 
        /* Send header packet to the session bus. */
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       sr_session_send(in->sdi, &packet);
+       std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
 
        if (ctx->samplerate) {
                packet.type = SR_DF_META;
index d64899d3c040e15f24b1420116c861e9d8448494..d83b547f280fe218b28dcd644435a8e9c4b69f09 100644 (file)
@@ -137,7 +137,6 @@ static int init(struct sr_input *in)
 
 static int loadfile(struct sr_input *in, const char *filename)
 {
-       struct sr_datafeed_header header;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_meta meta;
        struct sr_datafeed_logic logic;
@@ -166,12 +165,7 @@ static int loadfile(struct sr_input *in, const char *filename)
        sr_dbg("%s: samplerate is %" PRIu64, __func__, samplerate);
 
        /* Send header packet to the session bus. */
-       sr_dbg("%s: sending SR_DF_HEADER packet", __func__);
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(in->sdi, &packet);
+       std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
 
        /* Send metadata about the SR_DF_LOGIC packets to come. */
        packet.type = SR_DF_META;
index 5bb1f677b7c527ad0c6107960ec4b9af2e154117..a9963f7e7682dc136575f785f9471f2e439b7d61 100644 (file)
@@ -543,7 +543,6 @@ static void parse_contents(FILE *file, const struct sr_dev_inst *sdi, struct con
 
 static int loadfile(struct sr_input *in, const char *filename)
 {
-       struct sr_datafeed_header header;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_meta meta;
        struct sr_config *src;
@@ -564,11 +563,7 @@ static int loadfile(struct sr_input *in, const char *filename)
        }
 
        /* Send header packet to the session bus. */
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       packet.type = SR_DF_HEADER;
-       packet.payload = &header;
-       sr_session_send(in->sdi, &packet);
+       std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
 
        /* Send metadata about the SR_DF_LOGIC packets to come. */
        packet.type = SR_DF_META;
index ac430a3872b8f8127e363381471d5b3055bd1556..048ed74f398f6e2efa60d203d8b1e8ecbaf5b418 100644 (file)
@@ -128,6 +128,9 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
 SR_PRIV int std_hw_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
                        const char *prefix);
 
+SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
+                                      const char *prefix);
+
 /*--- hardware/common/serial.c ----------------------------------------------*/
 
 enum {
index f3cd4b6c8b57e3bff710fdb9e7b90ff590ee372e..463fa189bea34714d34a7122767c818f5d04e5a0 100644 (file)
@@ -225,8 +225,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 {
        struct zip_stat zs;
        struct session_vdev *vdev;
-       struct sr_datafeed_header *header;
-       struct sr_datafeed_packet *packet;
        int ret;
 
        vdev = sdi->priv;
@@ -252,29 +250,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+
        /* freewheeling source */
        sr_session_source_add(-1, 0, 0, receive_data, cb_data);
 
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("%s: packet malloc failed", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("%s: header malloc failed", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       /* Send header packet to the session bus. */
-       packet->type = SR_DF_HEADER;
-       packet->payload = (unsigned char *)header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(cb_data, packet);
-
-       g_free(header);
-       g_free(packet);
-
        return SR_OK;
 }
 
diff --git a/std.c b/std.c
index a9cbec0eaa367134b85b56572e7ba0b755a711cf..d436436cf6b77e03be31b4c0a5f8dc0298e2731c 100644 (file)
--- a/std.c
+++ b/std.c
@@ -57,3 +57,45 @@ SR_PRIV int std_hw_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
 
        return SR_OK;
 }
+
+/**
+ * Standard API helper for sending an SR_DF_HEADER packet.
+ *
+ * This function can be used to simplify most driver's
+ * hw_dev_acquisition_start() API callback.
+ *
+ * @param sdi The device instance to use.
+ * @param prefix A driver-specific prefix string used for log messages.
+ *              Must not be NULL. An empty string is allowed.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ *         SR_ERR upon other errors.
+ */
+SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
+                                      const char *prefix)
+{
+       int ret;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_header header;
+
+       if (!prefix) {
+               sr_err("Invalid prefix.");
+               return SR_ERR_ARG;
+       }
+
+       sr_dbg("%sStarting acquisition.", prefix);
+
+       /* Send header packet to the session bus. */
+       sr_dbg("%sSending SR_DF_HEADER packet.", prefix);
+       packet.type = SR_DF_HEADER;
+       packet.payload = (uint8_t *)&header;
+       header.feed_version = 1;
+       gettimeofday(&header.starttime, NULL);
+
+       if ((ret = sr_session_send(sdi, &packet)) < 0) {
+               sr_err("%sFailed to send header packet: %d.", prefix, ret);
+               return ret;
+       }
+
+       return SR_OK;
+}