From c2cf7c80183478d574f9d1be4a04c7fbf3943502 Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Tue, 8 Nov 2016 14:15:35 +0000 Subject: [PATCH] demo/cmd: check subdevice exists and supports 'read' commands Check the subdevice actually exists and that it claims to support 'read' commands (SDF_CMD_READ subdevice flag set), bailing out early if not. --- demo/cmd.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/demo/cmd.c b/demo/cmd.c index f15cfe7..d024f6a 100644 --- a/demo/cmd.c +++ b/demo/cmd.c @@ -96,6 +96,20 @@ int main(int argc, char *argv[]) fprintf(stderr, "defaulted to subdevice %d\n", options.subdevice); } + /* Check subdevice exists. */ + ret = comedi_get_n_subdevices(dev); + if(ret <= options.subdevice){ + fprintf(stderr, "subdevice %d does not exist\n", options.subdevice); + exit(1); + } + + /* Check subdevice supports 'read' commands. */ + ret = comedi_get_subdevice_flags(dev, options.subdevice); + if(ret < 0 || !(ret & SDF_CMD_READ)) { + fprintf(stderr, "subdevice %d does not support 'read' commands\n", options.subdevice); + exit(1); + } + // Print numbers for clipped inputs comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);