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 di The driver instance to use.
43 * @param sr_ctx The libsigrok context to assign.
45 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
47 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
49 struct drv_context *drvc;
51 drvc = g_malloc0(sizeof(struct drv_context));
52 drvc->sr_ctx = sr_ctx;
53 drvc->instances = NULL;
60 * Standard driver cleanup() callback API helper
62 * @param di The driver instance to use.
64 * Frees all device instances by calling sr_dev_clear() and then releases any
65 * resources allocated by std_init().
67 * @retval SR_OK Success
68 * @retval SR_ERR_ARG Invalid driver
71 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
75 ret = sr_dev_clear(di);
82 * Standard API helper for sending an SR_DF_HEADER packet.
84 * This function can be used to simplify most driver's
85 * dev_acquisition_start() API callback.
87 * @param sdi The device instance to use.
88 * @param prefix A driver-specific prefix string used for log messages.
89 * Must not be NULL. An empty string is allowed.
91 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
92 * SR_ERR upon other errors.
94 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
98 struct sr_datafeed_packet packet;
99 struct sr_datafeed_header header;
102 sr_err("Invalid prefix.");
106 sr_dbg("%s: Starting acquisition.", prefix);
108 /* Send header packet to the session bus. */
109 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
110 packet.type = SR_DF_HEADER;
111 packet.payload = (uint8_t *)&header;
112 header.feed_version = 1;
113 gettimeofday(&header.starttime, NULL);
115 if ((ret = sr_session_send(sdi, &packet)) < 0) {
116 sr_err("%s: Failed to send header packet: %d.", prefix, ret);
124 * Standard API helper for sending an SR_DF_END packet.
126 * @param sdi The device instance to use. Must not be NULL.
127 * @param prefix A driver-specific prefix string used for log messages.
128 * Must not be NULL. An empty string is allowed.
130 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
131 * SR_ERR upon other errors.
133 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi,
137 struct sr_datafeed_packet packet;
142 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
144 packet.type = SR_DF_END;
145 packet.payload = NULL;
147 if ((ret = sr_session_send(sdi, &packet)) < 0) {
148 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
155 #ifdef HAVE_LIBSERIALPORT
158 * Standard serial driver dev_open() helper.
160 * This function can be used to implement the dev_open() driver API
161 * callback in drivers that use a serial port. The port is opened
162 * with the SERIAL_RDWR flag.
164 * If the open succeeded, the status field of the given sdi is set
167 * @retval SR_OK Success.
168 * @retval SR_ERR Serial port open failed.
170 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
172 struct sr_serial_dev_inst *serial;
175 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
178 sdi->status = SR_ST_ACTIVE;
184 * Standard serial driver dev_close() helper.
186 * This function can be used to implement the dev_close() driver API
187 * callback in drivers that use a serial port.
189 * After closing the port, the status field of the given sdi is set
192 * @retval SR_OK Success.
194 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
196 struct sr_serial_dev_inst *serial;
199 if (serial && sdi->status == SR_ST_ACTIVE) {
200 serial_close(serial);
201 sdi->status = SR_ST_INACTIVE;
208 * Standard sr_session_stop() API helper.
210 * This function can be used to simplify most (serial port based) driver's
211 * dev_acquisition_stop() API callback.
213 * @param sdi The device instance for which acquisition should stop.
215 * @param cb_data Opaque 'cb_data' pointer. Must not be NULL.
216 * @param dev_close_fn Function pointer to the driver's dev_close().
218 * @param serial The serial device instance (struct serial_dev_inst *).
220 * @param[in] prefix A driver-specific prefix string used for log messages.
221 * Must not be NULL. An empty string is allowed.
223 * @retval SR_OK Success.
224 * @retval SR_ERR_ARG Invalid arguments.
225 * @retval SR_ERR_DEV_CLOSED Device is closed.
226 * @retval SR_ERR Other errors.
228 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi,
229 dev_close_callback dev_close_fn,
230 struct sr_serial_dev_inst *serial, const char *prefix)
235 sr_err("Invalid prefix.");
239 if (sdi->status != SR_ST_ACTIVE) {
240 sr_err("%s: Device inactive, can't stop acquisition.", prefix);
241 return SR_ERR_DEV_CLOSED;
244 sr_dbg("%s: Stopping acquisition.", prefix);
246 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
247 sr_err("%s: Failed to remove source: %d.", prefix, ret);
251 if ((ret = dev_close_fn(sdi)) < 0) {
252 sr_err("%s: Failed to close device: %d.", prefix, ret);
256 std_session_send_df_end(sdi, prefix);
264 * Standard driver dev_clear() helper.
266 * Clear driver, this means, close all instances.
268 * This function can be used to implement the dev_clear() driver API
269 * callback. dev_close() is called before every sr_dev_inst is cleared.
271 * The only limitation is driver-specific device contexts (sdi->priv).
272 * These are freed, but any dynamic allocation within structs stored
273 * there cannot be freed.
275 * @param driver The driver which will have its instances released.
276 * @param clear_private If not NULL, this points to a function called
277 * with sdi->priv as argument. The function can then clear any device
278 * instance-specific resources kept there. It must also clear the struct
279 * pointed to by sdi->priv.
281 * @return SR_OK on success.
283 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
284 std_dev_clear_callback clear_private)
286 struct drv_context *drvc;
287 struct sr_dev_inst *sdi;
291 if (!(drvc = driver->context))
292 /* Driver was never initialized, nothing to do. */
296 for (l = drvc->instances; l; l = l->next) {
297 if (!(sdi = l->data)) {
301 if (driver->dev_close)
302 driver->dev_close(sdi);
305 #ifdef HAVE_LIBSERIALPORT
306 if (sdi->inst_type == SR_INST_SERIAL)
307 sr_serial_dev_inst_free(sdi->conn);
309 #ifdef HAVE_LIBUSB_1_0
310 if (sdi->inst_type == SR_INST_USB)
311 sr_usb_dev_inst_free(sdi->conn);
313 if (sdi->inst_type == SR_INST_SCPI)
314 sr_scpi_free(sdi->conn);
315 if (sdi->inst_type == SR_INST_MODBUS)
316 sr_modbus_free(sdi->conn);
319 /* The helper function is responsible for freeing
320 * its own sdi->priv! */
321 clear_private(sdi->priv);
325 sr_dev_inst_free(sdi);
328 g_slist_free(drvc->instances);
329 drvc->instances = NULL;
335 * Standard implementation for the driver dev_list() callback
337 * This function can be used as the dev_list callback by most drivers that use
338 * the standard helper functions. It returns the devices contained in the driver
339 * context instances list.
341 * @param di The driver instance to use.
343 * @return The list of devices/instances of this driver, or NULL upon errors
344 * or if the list is empty.
346 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
348 struct drv_context *drvc = di->context;
350 return drvc->instances;