From: Uwe Hermann Date: Wed, 6 Feb 2013 18:57:32 +0000 (+0100) Subject: Add and use std_session_send_df_header(). X-Git-Tag: dsupstream~277 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4afdfd4628e9955af02a3ea619ecdfe469f9a9e2;p=libsigrok.git Add and use std_session_send_df_header(). 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. --- diff --git a/hardware/agilent-dmm/api.c b/hardware/agilent-dmm/api.c index 64f6275a..c0b10e07 100644 --- a/hardware/agilent-dmm/api.c +++ b/hardware/agilent-dmm/api.c @@ -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); diff --git a/hardware/alsa/api.c b/hardware/alsa/api.c index c546b400..e2efbf46 100644 --- a/hardware/alsa/api.c +++ b/hardware/alsa/api.c @@ -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, diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index acd3acb9..fc7b68e6 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -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; diff --git a/hardware/brymen-dmm/api.c b/hardware/brymen-dmm/api.c index 7d60fd03..f3152e26 100644 --- a/hardware/brymen-dmm/api.c +++ b/hardware/brymen-dmm/api.c @@ -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, diff --git a/hardware/chronovu-la8/api.c b/hardware/chronovu-la8/api.c index bf80fc6b..66cbdb6e 100644 --- a/hardware/chronovu-la8/api.c +++ b/hardware/chronovu-la8/api.c @@ -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) diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index ccd5f602..33c3038d 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -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, diff --git a/hardware/demo/demo.c b/hardware/demo/demo.c index 248ee11e..8a4bc9a0 100644 --- a/hardware/demo/demo.c +++ b/hardware/demo/demo.c @@ -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; } diff --git a/hardware/fluke-dmm/api.c b/hardware/fluke-dmm/api.c index c8280d4a..0b69010d 100644 --- a/hardware/fluke-dmm/api.c +++ b/hardware/fluke-dmm/api.c @@ -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); diff --git a/hardware/fx2lafw/fx2lafw.c b/hardware/fx2lafw/fx2lafw.c index e3260e55..1bbb4547 100644 --- a/hardware/fx2lafw/fx2lafw.c +++ b/hardware/fx2lafw/fx2lafw.c @@ -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) { diff --git a/hardware/hantek-dso/api.c b/hardware/hantek-dso/api.c index 4b0dfe37..ebcc0f25 100644 --- a/hardware/hantek-dso/api.c +++ b/hardware/hantek-dso/api.c @@ -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; } diff --git a/hardware/lascar-el-usb/api.c b/hardware/lascar-el-usb/api.c index 2b442d6d..4cef9f96 100644 --- a/hardware/lascar-el-usb/api.c +++ b/hardware/lascar-el-usb/api.c @@ -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. */ diff --git a/hardware/link-mso19/api.c b/hardware/link-mso19/api.c index 4a71863d..0bc02576 100644 --- a/hardware/link-mso19/api.c +++ b/hardware/link-mso19/api.c @@ -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; } diff --git a/hardware/mic-985xx/api.c b/hardware/mic-985xx/api.c index ccc21f73..bfb942ed 100644 --- a/hardware/mic-985xx/api.c +++ b/hardware/mic-985xx/api.c @@ -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, diff --git a/hardware/openbench-logic-sniffer/api.c b/hardware/openbench-logic-sniffer/api.c index 6511e678..56176cc7 100644 --- a/hardware/openbench-logic-sniffer/api.c +++ b/hardware/openbench-logic-sniffer/api.c @@ -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; } diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index fe2ca5d2..2fd375fb 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -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); diff --git a/hardware/serial-dmm/api.c b/hardware/serial-dmm/api.c index 2d991dce..b6920819 100644 --- a/hardware/serial-dmm/api.c +++ b/hardware/serial-dmm/api.c @@ -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, diff --git a/hardware/tondaj-sl-814/api.c b/hardware/tondaj-sl-814/api.c index 36ec5c26..b6d38cd1 100644 --- a/hardware/tondaj-sl-814/api.c +++ b/hardware/tondaj-sl-814/api.c @@ -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, diff --git a/hardware/uni-t-dmm/api.c b/hardware/uni-t-dmm/api.c index 9870130a..7662fb07 100644 --- a/hardware/uni-t-dmm/api.c +++ b/hardware/uni-t-dmm/api.c @@ -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 */, diff --git a/hardware/victor-dmm/api.c b/hardware/victor-dmm/api.c index bfe57c43..871c5de0 100644 --- a/hardware/victor-dmm/api.c +++ b/hardware/victor-dmm/api.c @@ -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++) { diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 32bf0268..4d3df029 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -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."); diff --git a/input/binary.c b/input/binary.c index d8e47b52..1c786bae 100644 --- a/input/binary.c +++ b/input/binary.c @@ -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; diff --git a/input/chronovu_la8.c b/input/chronovu_la8.c index d64899d3..d83b547f 100644 --- a/input/chronovu_la8.c +++ b/input/chronovu_la8.c @@ -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; diff --git a/input/vcd.c b/input/vcd.c index 5bb1f677..a9963f7e 100644 --- a/input/vcd.c +++ b/input/vcd.c @@ -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; diff --git a/libsigrok-internal.h b/libsigrok-internal.h index ac430a38..048ed74f 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -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 { diff --git a/session_driver.c b/session_driver.c index f3cd4b6c..463fa189 100644 --- a/session_driver.c +++ b/session_driver.c @@ -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 a9cbec0e..d436436c 100644 --- 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; +}