2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Standard API helper functions.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
32 #define LOG_PREFIX "std"
35 * Standard sr_driver_init() API helper.
37 * This function can be used to simplify most driver's init() API callback.
39 * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and
40 * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed.
42 * @param sr_ctx The libsigrok context to assign.
43 * @param di The driver instance to use.
44 * @param[in] prefix A driver-specific prefix string used for log messages.
46 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
48 SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
51 struct drv_context *drvc;
54 sr_err("%s: Invalid driver, cannot initialize.", prefix);
58 drvc = g_malloc0(sizeof(struct drv_context));
59 drvc->sr_ctx = sr_ctx;
60 drvc->instances = NULL;
67 * Standard driver cleanup() callback API helper
69 * @param di The driver instance to use.
71 * Frees all device instances by calling sr_dev_clear() and then releases any
72 * resources allocated by std_init().
74 * @retval SR_OK Success
75 * @retval SR_ERR_ARG Invalid driver
78 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
82 ret = sr_dev_clear(di);
89 * Standard API helper for sending an SR_DF_HEADER packet.
91 * This function can be used to simplify most driver's
92 * dev_acquisition_start() API callback.
94 * @param sdi The device instance to use.
95 * @param prefix A driver-specific prefix string used for log messages.
96 * Must not be NULL. An empty string is allowed.
98 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
99 * SR_ERR upon other errors.
101 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
105 struct sr_datafeed_packet packet;
106 struct sr_datafeed_header header;
109 sr_err("Invalid prefix.");
113 sr_dbg("%s: Starting acquisition.", prefix);
115 /* Send header packet to the session bus. */
116 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
117 packet.type = SR_DF_HEADER;
118 packet.payload = (uint8_t *)&header;
119 header.feed_version = 1;
120 gettimeofday(&header.starttime, NULL);
122 if ((ret = sr_session_send(sdi, &packet)) < 0) {
123 sr_err("%s: Failed to send header packet: %d.", prefix, ret);
131 * Standard API helper for sending an SR_DF_END packet.
133 * @param sdi The device instance to use. Must not be NULL.
134 * @param prefix A driver-specific prefix string used for log messages.
135 * Must not be NULL. An empty string is allowed.
137 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
138 * SR_ERR upon other errors.
140 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi,
144 struct sr_datafeed_packet packet;
149 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
151 packet.type = SR_DF_END;
152 packet.payload = NULL;
154 if ((ret = sr_session_send(sdi, &packet)) < 0) {
155 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
162 #ifdef HAVE_LIBSERIALPORT
165 * Standard serial driver dev_open() helper.
167 * This function can be used to implement the dev_open() driver API
168 * callback in drivers that use a serial port. The port is opened
169 * with the SERIAL_RDWR flag.
171 * If the open succeeded, the status field of the given sdi is set
174 * @retval SR_OK Success.
175 * @retval SR_ERR Serial port open failed.
177 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
179 struct sr_serial_dev_inst *serial;
182 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
185 sdi->status = SR_ST_ACTIVE;
191 * Standard serial driver dev_close() helper.
193 * This function can be used to implement the dev_close() driver API
194 * callback in drivers that use a serial port.
196 * After closing the port, the status field of the given sdi is set
199 * @retval SR_OK Success.
201 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
203 struct sr_serial_dev_inst *serial;
206 if (serial && sdi->status == SR_ST_ACTIVE) {
207 serial_close(serial);
208 sdi->status = SR_ST_INACTIVE;
215 * Standard sr_session_stop() API helper.
217 * This function can be used to simplify most (serial port based) driver's
218 * dev_acquisition_stop() API callback.
220 * @param sdi The device instance for which acquisition should stop.
222 * @param cb_data Opaque 'cb_data' pointer. Must not be NULL.
223 * @param dev_close_fn Function pointer to the driver's dev_close().
225 * @param serial The serial device instance (struct serial_dev_inst *).
227 * @param[in] prefix A driver-specific prefix string used for log messages.
228 * Must not be NULL. An empty string is allowed.
230 * @retval SR_OK Success.
231 * @retval SR_ERR_ARG Invalid arguments.
232 * @retval SR_ERR_DEV_CLOSED Device is closed.
233 * @retval SR_ERR Other errors.
235 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi,
236 dev_close_callback dev_close_fn,
237 struct sr_serial_dev_inst *serial, const char *prefix)
242 sr_err("Invalid prefix.");
246 if (sdi->status != SR_ST_ACTIVE) {
247 sr_err("%s: Device inactive, can't stop acquisition.", prefix);
248 return SR_ERR_DEV_CLOSED;
251 sr_dbg("%s: Stopping acquisition.", prefix);
253 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
254 sr_err("%s: Failed to remove source: %d.", prefix, ret);
258 if ((ret = dev_close_fn(sdi)) < 0) {
259 sr_err("%s: Failed to close device: %d.", prefix, ret);
263 std_session_send_df_end(sdi, prefix);
271 * Standard driver dev_clear() helper.
273 * Clear driver, this means, close all instances.
275 * This function can be used to implement the dev_clear() driver API
276 * callback. dev_close() is called before every sr_dev_inst is cleared.
278 * The only limitation is driver-specific device contexts (sdi->priv).
279 * These are freed, but any dynamic allocation within structs stored
280 * there cannot be freed.
282 * @param driver The driver which will have its instances released.
283 * @param clear_private If not NULL, this points to a function called
284 * with sdi->priv as argument. The function can then clear any device
285 * instance-specific resources kept there. It must also clear the struct
286 * pointed to by sdi->priv.
288 * @return SR_OK on success.
290 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
291 std_dev_clear_callback clear_private)
293 struct drv_context *drvc;
294 struct sr_dev_inst *sdi;
298 if (!(drvc = driver->context))
299 /* Driver was never initialized, nothing to do. */
303 for (l = drvc->instances; l; l = l->next) {
304 if (!(sdi = l->data)) {
308 if (driver->dev_close)
309 driver->dev_close(sdi);
312 #ifdef HAVE_LIBSERIALPORT
313 if (sdi->inst_type == SR_INST_SERIAL)
314 sr_serial_dev_inst_free(sdi->conn);
316 #ifdef HAVE_LIBUSB_1_0
317 if (sdi->inst_type == SR_INST_USB)
318 sr_usb_dev_inst_free(sdi->conn);
320 if (sdi->inst_type == SR_INST_SCPI)
321 sr_scpi_free(sdi->conn);
322 if (sdi->inst_type == SR_INST_MODBUS)
323 sr_modbus_free(sdi->conn);
326 /* The helper function is responsible for freeing
327 * its own sdi->priv! */
328 clear_private(sdi->priv);
332 sr_dev_inst_free(sdi);
335 g_slist_free(drvc->instances);
336 drvc->instances = NULL;
342 * Standard implementation for the driver dev_list() callback
344 * This function can be used as the dev_list callback by most drivers that use
345 * the standard helper functions. It returns the devices contained in the driver
346 * context instances list.
348 * @param di The driver instance to use.
350 * @return The list of devices/instances of this driver, or NULL upon errors
351 * or if the list is empty.
353 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
355 struct drv_context *drvc = di->context;
357 return drvc->instances;