* @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"
* @param di The driver instance to use.
* @param[in] prefix A driver-specific prefix string used for log messages.
*
- * @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)
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;
}
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;
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(cb_data, 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);
}