X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fdevice.c;h=6eff16d16573e45c78f0e836a168ff72ff15d31f;hb=093e1cba6b7bf14cfb77fa36f59b0c16e6fca7cc;hp=ace11dd86204b178a63ef903b7006c889ddd8661;hpb=2ecc745ccb837fd990f3ef83e4aca74cffa948cc;p=libsigrok.git diff --git a/src/device.c b/src/device.c index ace11dd8..6eff16d1 100644 --- a/src/device.c +++ b/src/device.c @@ -136,7 +136,9 @@ SR_API int sr_dev_channel_enable(struct sr_channel *channel, gboolean state) } /* Returns the next enabled channel, wrapping around if necessary. */ +/** @private */ SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi, + struct sr_channel *cur_channel) { struct sr_channel *next_channel; @@ -339,7 +341,7 @@ SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type, /** * Free device instance struct created by sr_dev_inst(). * - * @param sdi Device instance to free. Must not be NULL. + * @param sdi Device instance to free. If NULL, the function will do nothing. * * @private */ @@ -349,6 +351,9 @@ SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) struct sr_channel_group *cg; GSList *l; + if (!sdi) + return; + for (l = sdi->channels; l; l = l->next) { ch = l->data; g_free(ch->name); @@ -406,7 +411,8 @@ SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, /** * Free struct sr_usb_dev_inst * allocated by sr_usb_dev_inst(). * - * @param usb The struct sr_usb_dev_inst * to free. Must not be NULL. + * @param usb The struct sr_usb_dev_inst * to free. If NULL, this + * function does nothing. * * @private */ @@ -454,12 +460,16 @@ SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port, /** * Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst(). * - * @param serial The struct sr_serial_dev_inst * to free. Must not be NULL. + * @param serial The struct sr_serial_dev_inst * to free. If NULL, this + * function will do nothing. * * @private */ SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) { + if (!serial) + return; + g_free(serial->port); g_free(serial->serialcomm); g_free(serial); @@ -481,6 +491,9 @@ SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device) /** @private */ SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc) { + if (!usbtmc) + return; + g_free(usbtmc->device); g_free(usbtmc); } @@ -568,6 +581,14 @@ SR_API int sr_dev_close(struct sr_dev_inst *sdi) if (!sdi || !sdi->driver || !sdi->driver->dev_close) return SR_ERR; + if (sdi->status != SR_ST_ACTIVE) { + sr_err("%s: Device instance not active, can't close.", + sdi->driver->name); + return SR_ERR_DEV_CLOSED; + } + + sr_dbg("%s: Closing device.", sdi->driver->name) + ret = sdi->driver->dev_close(sdi); return ret;