* @internal
*/
+#include <config.h>
#include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
+#include "scpi.h"
#define LOG_PREFIX "std"
* It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and
* then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed.
*
- * @param sr_ctx The libsigrok context to assign.
* @param di The driver instance to use.
- * @param[in] prefix A driver-specific prefix string used for log messages.
+ * @param sr_ctx The libsigrok context to assign.
*
- * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
- * SR_ERR_MALLOC upon memory allocation errors.
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
*/
-SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
- const char *prefix)
+SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
{
struct drv_context *drvc;
- if (!di) {
- sr_err("%s: Invalid driver, cannot initialize.", prefix);
- return SR_ERR_ARG;
- }
-
- if (!(drvc = g_try_malloc(sizeof(struct drv_context)))) {
- sr_err("%s: Driver context malloc failed.", prefix);
- return SR_ERR_MALLOC;
- }
-
+ drvc = g_malloc0(sizeof(struct drv_context));
drvc->sr_ctx = sr_ctx;
drvc->instances = NULL;
- di->priv = drvc;
+ di->context = drvc;
return SR_OK;
}
+/**
+ * Standard driver cleanup() callback API helper
+ *
+ * @param di The driver instance to use.
+ *
+ * Frees all device instances by calling sr_dev_clear() and then releases any
+ * resources allocated by std_init().
+ *
+ * @retval SR_OK Success
+ * @retval SR_ERR_ARG Invalid driver
+ *
+*/
+SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
+{
+ int ret;
+
+ ret = sr_dev_clear(di);
+ g_free(di->context);
+
+ return ret;
+}
+
/**
* Standard API helper for sending an SR_DF_HEADER packet.
*
return SR_OK;
}
+/**
+ * Standard API helper for sending an SR_DF_END packet.
+ *
+ * @param sdi The device instance to use. 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_session_send_df_end(const struct sr_dev_inst *sdi,
+ const char *prefix)
+{
+ int ret;
+ struct sr_datafeed_packet packet;
+
+ if (!sdi || !prefix)
+ return SR_ERR_ARG;
+
+ sr_dbg("%s: Sending SR_DF_END packet.", prefix);
+
+ packet.type = SR_DF_END;
+ packet.payload = NULL;
+
+ if ((ret = sr_session_send(sdi, &packet)) < 0) {
+ sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
+ return ret;
+ }
+
+ return SR_OK;
+}
+
#ifdef HAVE_LIBSERIALPORT
/**
*
* This function can be used to implement the dev_open() driver API
* callback in drivers that use a serial port. The port is opened
- * with the SERIAL_RDWR and SERIAL_NONBLOCK flags.
+ * with the SERIAL_RDWR flag.
*
* If the open succeeded, the status field of the given sdi is set
* to SR_ST_ACTIVE.
struct sr_serial_dev_inst *serial;
serial = sdi->conn;
- if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+ if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
* @retval SR_ERR Other errors.
*/
SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi,
- void *cb_data, dev_close_callback dev_close_fn,
+ dev_close_callback 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 ret;
}
- /* Send SR_DF_END packet to the session bus. */
- sr_dbg("%s: Sending SR_DF_END packet.", prefix);
- packet.type = SR_DF_END;
- packet.payload = NULL;
- if ((ret = sr_session_send(cb_data, &packet)) < 0) {
- sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
- return ret;
- }
+ std_session_send_df_end(sdi, prefix);
return SR_OK;
}
GSList *l;
int ret;
- if (!(drvc = driver->priv))
+ if (!(drvc = driver->context))
/* Driver was never initialized, nothing to do. */
return SR_OK;
#endif
if (sdi->inst_type == SR_INST_SCPI)
sr_scpi_free(sdi->conn);
+ if (sdi->inst_type == SR_INST_MODBUS)
+ sr_modbus_free(sdi->conn);
}
if (clear_private)
+ /* The helper function is responsible for freeing
+ * its own sdi->priv! */
clear_private(sdi->priv);
else
g_free(sdi->priv);
+
sr_dev_inst_free(sdi);
}
return ret;
}
+
+/**
+ * Standard implementation for the driver dev_list() callback
+ *
+ * This function can be used as the dev_list callback by most drivers that use
+ * the standard helper functions. It returns the devices contained in the driver
+ * context instances list.
+ *
+ * @param di The driver instance to use.
+ *
+ * @return The list of devices/instances of this driver, or NULL upon errors
+ * or if the list is empty.
+ */
+SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
+{
+ struct drv_context *drvc = di->context;
+
+ return drvc->instances;
+}