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, see <http://www.gnu.org/licenses/>.
23 * Standard API helper functions.
30 #include <libsigrok/libsigrok.h>
31 #include "libsigrok-internal.h"
34 #define LOG_PREFIX "std"
37 * Standard driver init() callback API helper.
39 * This function can be used to simplify most driver's init() API callback.
41 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
42 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
44 * @param[in] di The driver instance to use. Must not be NULL.
45 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
47 * @retval SR_OK Success.
48 * @retval SR_ERR_ARG Invalid argument.
50 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
52 struct drv_context *drvc;
55 sr_err("%s: Invalid argument.", __func__);
59 drvc = g_malloc0(sizeof(struct drv_context));
60 drvc->sr_ctx = sr_ctx;
61 drvc->instances = NULL;
68 * Standard driver cleanup() callback API helper.
70 * This function can be used to simplify most driver's cleanup() API callback.
72 * Free all device instances by calling sr_dev_clear() and then release any
73 * resources allocated by std_init().
75 * @param[in] di The driver instance to use. Must not be NULL.
77 * @retval SR_OK Success.
78 * @retval SR_ERR_ARG Invalid argument.
79 * @retval other Other error.
81 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
86 sr_err("%s: Invalid argument.", __func__);
90 ret = sr_dev_clear(di);
97 * Standard API helper for sending an SR_DF_HEADER packet.
99 * This function can be used to simplify most drivers'
100 * dev_acquisition_start() API callback.
102 * @param[in] sdi The device instance to use. Must not be NULL.
104 * @retval SR_OK Success.
105 * @retval SR_ERR_ARG Invalid argument.
106 * @retval other Other error.
108 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
112 struct sr_datafeed_packet packet;
113 struct sr_datafeed_header header;
116 sr_err("%s: Invalid argument.", __func__);
120 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
122 /* Send header packet to the session bus. */
123 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
124 packet.type = SR_DF_HEADER;
125 packet.payload = (uint8_t *)&header;
126 header.feed_version = 1;
127 gettimeofday(&header.starttime, NULL);
129 if ((ret = sr_session_send(sdi, &packet)) < 0) {
130 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
138 * Standard API helper for sending an SR_DF_END packet.
140 * This function can be used to simplify most drivers'
141 * dev_acquisition_stop() API callback.
143 * @param[in] sdi The device instance to use. Must not be NULL.
145 * @retval SR_OK Success.
146 * @retval SR_ERR_ARG Invalid argument.
147 * @retval other Other error.
149 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
153 struct sr_datafeed_packet packet;
156 sr_err("%s: Invalid argument.", __func__);
160 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
162 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
164 packet.type = SR_DF_END;
165 packet.payload = NULL;
167 if ((ret = sr_session_send(sdi, &packet)) < 0) {
168 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
175 #ifdef HAVE_LIBSERIALPORT
178 * Standard serial driver dev_open() callback API helper.
180 * This function can be used to implement the dev_open() driver API
181 * callback in drivers that use a serial port. The port is opened
182 * with the SERIAL_RDWR flag.
184 * @param[in] sdi The device instance to use. Must not be NULL.
186 * @retval SR_OK Success.
187 * @retval SR_ERR_ARG Invalid argument.
188 * @retval other Serial port open failed.
190 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
192 struct sr_serial_dev_inst *serial;
195 sr_err("%s: Invalid argument.", __func__);
201 return serial_open(serial, SERIAL_RDWR);
205 * Standard serial driver dev_close() callback API helper.
207 * This function can be used to implement the dev_close() driver API
208 * callback in drivers that use a serial port.
210 * @param[in] sdi The device instance to use. Must not be NULL.
212 * @retval SR_OK Success.
213 * @retval SR_ERR_ARG Invalid argument.
214 * @retval other Serial port close failed.
216 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
218 struct sr_serial_dev_inst *serial;
221 sr_err("%s: Invalid argument.", __func__);
227 return serial_close(serial);
231 * Standard serial driver dev_acquisition_stop() callback API helper.
233 * This function can be used to simplify most (serial port based) drivers'
234 * dev_acquisition_stop() API callback.
236 * @param[in] sdi The device instance for which acquisition should stop.
239 * @retval SR_OK Success.
240 * @retval SR_ERR_ARG Invalid argument.
241 * @retval other Other error.
243 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
245 struct sr_serial_dev_inst *serial;
250 sr_err("%s: Invalid argument.", __func__);
255 prefix = sdi->driver->name;
257 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
258 sr_err("%s: Failed to remove source: %d.", prefix, ret);
262 if ((ret = sr_dev_close(sdi)) < 0) {
263 sr_err("%s: Failed to close device: %d.", prefix, ret);
267 return std_session_send_df_end(sdi);
273 * Standard driver dev_clear() callback API helper.
275 * Clear driver, this means, close all instances.
277 * This function can be used to implement the dev_clear() driver API
278 * callback. dev_close() is called before every sr_dev_inst is cleared.
280 * The only limitation is driver-specific device contexts (sdi->priv).
281 * These are freed, but any dynamic allocation within structs stored
282 * there cannot be freed.
284 * @param[in] driver The driver which will have its instances released.
286 * @param[in] clear_private If not NULL, this points to a function called
287 * with sdi->priv as argument. The function can then clear
288 * any device instance-specific resources kept there.
289 * It must also clear the struct pointed to by sdi->priv.
291 * @retval SR_OK Success.
292 * @retval SR_ERR_ARG Invalid argument.
293 * @retval SR_ERR_BUG Implementation bug.
294 * @retval other Other error.
296 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
297 std_dev_clear_callback clear_private)
299 struct drv_context *drvc;
300 struct sr_dev_inst *sdi;
305 sr_err("%s: Invalid argument.", __func__);
309 drvc = driver->context; /* Caller checked for context != NULL. */
312 for (l = drvc->instances; l; l = l->next) {
313 if (!(sdi = l->data)) {
314 sr_err("%s: Invalid device instance.", __func__);
318 if (driver->dev_close)
319 driver->dev_close(sdi);
322 #ifdef HAVE_LIBSERIALPORT
323 if (sdi->inst_type == SR_INST_SERIAL)
324 sr_serial_dev_inst_free(sdi->conn);
326 #ifdef HAVE_LIBUSB_1_0
327 if (sdi->inst_type == SR_INST_USB)
328 sr_usb_dev_inst_free(sdi->conn);
330 if (sdi->inst_type == SR_INST_SCPI)
331 sr_scpi_free(sdi->conn);
332 if (sdi->inst_type == SR_INST_MODBUS)
333 sr_modbus_free(sdi->conn);
336 /* The helper function is responsible for freeing
337 * its own sdi->priv! */
338 clear_private(sdi->priv);
342 sr_dev_inst_free(sdi);
345 g_slist_free(drvc->instances);
346 drvc->instances = NULL;
352 * Standard driver dev_list() callback API helper.
354 * This function can be used as the dev_list() callback by most drivers.
356 * Return the devices contained in the driver context instances list.
358 * @param[in] di The driver instance to use. Must not be NULL.
360 * @retval NULL Error, or the list is empty.
361 * @retval other The list of device instances of this driver.
363 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
365 struct drv_context *drvc;
368 sr_err("%s: Invalid argument.", __func__);
374 return drvc->instances;
378 * Standard driver scan() callback API helper.
380 * This function can be used to perform common tasks required by a driver's
381 * scan() callback. It will initialize the driver for each device on the list
382 * and add the devices on the list to the driver's device instance list.
383 * Usually it should be used as the last step in the scan() callback, right
386 * Note: This function can only be used if std_init() has been called
387 * previously by the driver.
391 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
393 * struct GSList *device;
394 * struct sr_dev_inst *sdi;
396 * sdi = g_new0(sr_dev_inst, 1);
399 * devices = g_slist_append(devices, sdi);
401 * return std_scan_complete(di, devices);
405 * @param[in] di The driver instance to use. Must not be NULL.
406 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
409 * @return The @p devices list.
411 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
413 struct drv_context *drvc;
417 sr_err("Invalid driver instance (di), cannot complete scan.");
423 for (l = devices; l; l = l->next) {
424 struct sr_dev_inst *sdi = l->data;
426 sr_err("Invalid device instance, cannot complete scan.");
432 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));