]> sigrok.org Git - libsigrok.git/commitdiff
Add/use std_hw_dev_acquisition_stop_serial().
authorUwe Hermann <redacted>
Fri, 1 Feb 2013 22:45:32 +0000 (23:45 +0100)
committerUwe Hermann <redacted>
Fri, 8 Feb 2013 22:32:45 +0000 (23:32 +0100)
hardware/agilent-dmm/api.c
hardware/brymen-dmm/api.c
hardware/colead-slm/api.c
hardware/fluke-dmm/api.c
hardware/mic-985xx/api.c
hardware/serial-dmm/api.c
hardware/tondaj-sl-814/api.c
libsigrok-internal.h
std.c

index 6818bc8f902e269123c12172462b831319c24c42..39dab3b4d01c0ab44f36e6bbd9c905d90e8023ef 100644 (file)
@@ -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 = {
index 1bd3c2e0f7b382014cfe861cea097784db53a237..d50d5be4e2318030eccb1e0197a191c9b8349e5a 100644 (file)
@@ -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 = {
index 7cb5140eff500b4a70b2a613eabf95c3580f5429..19dcd945585f55f2659ac8953366e749dab35b10 100644 (file)
@@ -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 = {
index 438918d1645541ff387255b2de021eb563f29960..2d6a2036560e415db9ca185c64ffb8a30cb15dd3 100644 (file)
@@ -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 = {
index bfb942ed118fdcf49fc9eafa557592f4ab793a68..2920ca8240512da7e757224f50a429a96b77f98a 100644 (file)
@@ -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 */
index a3a44eb47c833378c034985238a492721390d279..3ab19494d52d9b9ea7396db1d06236b8bc6a3a27 100644 (file)
@@ -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 */
index e282f4c7a4836c3787aff814164d1421d2176453..3a3ea9662e9772c259c8e95f4b6f2c8c2b6cab50 100644 (file)
@@ -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 = {
index 048ed74f398f6e2efa60d203d8b1e8ecbaf5b418..aeb96ac854915380c6f0d346bc46a5fb52dc67a4 100644 (file)
@@ -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 d436436cf6b77e03be31b4c0a5f8dc0298e2731c..5eb2e57d8e9808f19c1e13588484f92ead7592d4 100644 (file)
--- 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;
+}