From: Uwe Hermann Date: Fri, 1 Feb 2013 22:45:32 +0000 (+0100) Subject: Add/use std_hw_dev_acquisition_stop_serial(). X-Git-Tag: dsupstream~270 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=cd2f0fe22c35dcf3b010411ff6f123701be2a2d6;p=libsigrok.git Add/use std_hw_dev_acquisition_stop_serial(). --- diff --git a/hardware/agilent-dmm/api.c b/hardware/agilent-dmm/api.c index 6818bc8f..39dab3b4 100644 --- a/hardware/agilent-dmm/api.c +++ b/hardware/agilent-dmm/api.c @@ -309,29 +309,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR; - - if (!(devc = sdi->priv)) { - sr_err("sdi->priv was NULL."); - return SR_ERR_BUG; - } - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } SR_PRIV struct sr_dev_driver agdmm_driver_info = { diff --git a/hardware/brymen-dmm/api.c b/hardware/brymen-dmm/api.c index 1bd3c2e0..d50d5be4 100644 --- a/hardware/brymen-dmm/api.c +++ b/hardware/brymen-dmm/api.c @@ -296,30 +296,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) { - sr_err("Device inactive, can't stop acquisition."); - return SR_ERR; - } - - if (!(devc = sdi->priv)) { - sr_err("sdi->priv was NULL."); - return SR_ERR_BUG; - } - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = { diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index 7cb5140e..19dcd945 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -255,28 +255,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR; - - if (!(devc = sdi->priv)) { - sr_err("sdi->priv was NULL."); - return SR_ERR_BUG; - } - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } SR_PRIV struct sr_dev_driver colead_slm_driver_info = { diff --git a/hardware/fluke-dmm/api.c b/hardware/fluke-dmm/api.c index 438918d1..2d6a2036 100644 --- a/hardware/fluke-dmm/api.c +++ b/hardware/fluke-dmm/api.c @@ -350,28 +350,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR; - - if (!(devc = sdi->priv)) { - sr_err("sdi->priv was NULL."); - return SR_ERR_BUG; - } - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } SR_PRIV struct sr_dev_driver flukedmm_driver_info = { diff --git a/hardware/mic-985xx/api.c b/hardware/mic-985xx/api.c index bfb942ed..2920ca82 100644 --- a/hardware/mic-985xx/api.c +++ b/hardware/mic-985xx/api.c @@ -277,27 +277,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) { - sr_err("Device inactive, can't stop acquisition."); - return SR_ERR; - } - - devc = sdi->priv; - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } /* Driver-specific API function wrappers */ diff --git a/hardware/serial-dmm/api.c b/hardware/serial-dmm/api.c index a3a44eb4..3ab19494 100644 --- a/hardware/serial-dmm/api.c +++ b/hardware/serial-dmm/api.c @@ -439,28 +439,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct sr_datafeed_packet packet; - struct dev_context *devc; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR; - - if (!(devc = sdi->priv)) { - sr_err("sdi->priv was NULL."); - return SR_ERR_BUG; - } - - sr_dbg("Stopping acquisition."); - - sr_source_remove(devc->serial->fd); - hw_dev_close((struct sr_dev_inst *)sdi); - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return SR_OK; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } /* Driver-specific API function wrappers */ diff --git a/hardware/tondaj-sl-814/api.c b/hardware/tondaj-sl-814/api.c index e282f4c7..3a3ea966 100644 --- a/hardware/tondaj-sl-814/api.c +++ b/hardware/tondaj-sl-814/api.c @@ -246,35 +246,8 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - int ret, final_ret; - struct sr_datafeed_packet packet; - struct dev_context *devc; - - final_ret = SR_OK; - - if (sdi->status != SR_ST_ACTIVE) { - sr_err("Device inactive, can't stop acquisition."); - return SR_ERR; - } - - devc = sdi->priv; - - if ((ret = sr_source_remove(devc->serial->fd)) < 0) { - sr_err("Error removing source: %d.", ret); - final_ret = SR_ERR; - } - - if ((ret = hw_dev_close(sdi)) < 0) { - sr_err("Error closing device: %d.", ret); - final_ret = SR_ERR; - } - - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); - - return final_ret; + return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, + ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); } SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info = { diff --git a/libsigrok-internal.h b/libsigrok-internal.h index 048ed74f..aeb96ac8 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -125,9 +125,13 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, /*--- std.c -----------------------------------------------------------------*/ +typedef int (*dev_close_t)(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_hw_dev_acquisition_stop_serial(struct sr_dev_inst *sdi, + void *cb_data, dev_close_t hw_dev_close_fn, + struct sr_serial_dev_inst *serial, const char *prefix); SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi, const char *prefix); diff --git a/std.c b/std.c index d436436c..5eb2e57d 100644 --- a/std.c +++ b/std.c @@ -99,3 +99,63 @@ SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi, return SR_OK; } + +/* + * Standard sr_session_stop() API helper. + * + * This function can be used to simplify most (serial port based) driver's + * hw_dev_acquisition_stop() API callback. + * + * @param sdi The device instance for which acquisition should stop. + * Must not be NULL. + * @param cb_data Opaque 'cb_data' pointer. Must not be NULL. + * @param hw_dev_close_fn Function pointer to the driver's hw_dev_close(). + * Must not be NULL. + * @param serial The serial device instance (struct serial_dev_inst *). + * Must not be NULL. + * @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_hw_dev_acquisition_stop_serial(struct sr_dev_inst *sdi, + void *cb_data, dev_close_t hw_dev_close_fn, + struct sr_serial_dev_inst *serial, const char *prefix) +{ + int ret; + struct sr_datafeed_packet packet; + + if (!prefix) { + sr_err("Invalid prefix."); + return SR_ERR_ARG; + } + + if (sdi->status != SR_ST_ACTIVE) { + sr_err("%sDevice inactive, can't stop acquisition.", prefix); + return SR_ERR; + } + + sr_dbg("%sStopping acquisition.", prefix); + + if ((ret = sr_source_remove(serial->fd)) < 0) { + sr_err("%sFailed to remove source: %d.", prefix, ret); + return ret; + } + + if ((ret = hw_dev_close_fn(sdi)) < 0) { + sr_err("%sFailed to close device: %d.", prefix, ret); + return ret; + } + + /* Send SR_DF_END packet to the session bus. */ + sr_dbg("%sSending SR_DF_END packet.", prefix); + packet.type = SR_DF_END; + packet.payload = NULL; + if ((ret = sr_session_send(cb_data, &packet)) < 0) { + sr_err("%sFailed to send SR_DF_END packet: %d.", prefix, ret); + return ret; + } + + return SR_OK; +}