Module: xenomai-2.5
Branch: master
Commit: 6469e07b0a6069ccb91de0fa79b28e8f094ab1c6
URL:    
http://git.xenomai.org/?p=xenomai-2.5.git;a=commit;h=6469e07b0a6069ccb91de0fa79b28e8f094ab1c6

Author: Alexis Berlemont <alexis.berlem...@gmail.com>
Date:   Thu Feb 11 01:02:08 2010 +0100

analogy: fix a potential missing initialization of the subd descriptor

---

 src/utils/analogy/insn_bits.c  |   20 +++++++++++++++
 src/utils/analogy/insn_read.c  |   50 ++++++++++++++++++++------------------
 src/utils/analogy/insn_write.c |   52 ++++++++++++++++++++-------------------
 3 files changed, 73 insertions(+), 49 deletions(-)

diff --git a/src/utils/analogy/insn_bits.c b/src/utils/analogy/insn_bits.c
index d7a8e1b..9e672c4 100644
--- a/src/utils/analogy/insn_bits.c
+++ b/src/utils/analogy/insn_bits.c
@@ -152,6 +152,26 @@ int main(int argc, char *argv[])
        if (verbose != 0)
                printf("insn_bits: selected subdevice index = %d\n", idx_subd);
 
+       /* We must check that the subdevice is really a digital one
+          (in case, the subdevice index was set with the option -s) */
+       err = a4l_get_subdinfo(&dsc, idx_subd, &sbinfo);
+       if (err < 0) {
+               fprintf(stderr, 
+                       "insn_bits: get_sbinfo(%d) failed (err = %d)\n",
+                       idx_subd, err);
+               err = -EINVAL;
+               goto out_insn_bits;
+       }
+
+       if ((sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_DIO &&
+           (sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_DI &&
+           (sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_DO) {
+               fprintf(stderr, 
+                       "insn_bits: selected subdevice is not digital\n");
+               err = -EINVAL;
+               goto out_insn_bits;             
+       }
+       
        /* Set the data size to read / write */
        scan_size = a4l_sizeof_subd(sbinfo);
 
diff --git a/src/utils/analogy/insn_read.c b/src/utils/analogy/insn_read.c
index 47be76d..b3501ac 100644
--- a/src/utils/analogy/insn_read.c
+++ b/src/utils/analogy/insn_read.c
@@ -185,7 +185,7 @@ out:
 
 int main(int argc, char *argv[])
 {
-       int i = 0, err = 0;
+       int err = 0;
        unsigned int cnt = 0;
        a4l_desc_t dsc = { .sbdata = NULL };
        a4l_sbinfo_t *sbinfo;
@@ -244,13 +244,6 @@ int main(int argc, char *argv[])
                return err;
        }
 
-       /* Check there is an input subdevice */
-       if (dsc.idx_read_subd < 0) {
-               err = -ENOENT;
-               fprintf(stderr, "insn_read: no input subdevice available\n");
-               goto out_insn_read;
-       }
-
        if (verbose != 0) {
                printf("insn_read: device %s opened (fd=%d)\n", filename,
                       dsc.fd);
@@ -280,22 +273,9 @@ int main(int argc, char *argv[])
                printf("insn_read: complex descriptor retrieved\n");
 
        /* If no subdevice index was set, look for an analog input
-          subdevice (the first found will be selected) */
-       while (idx_subd == -1 && i < dsc.nb_subd) {
-               
-               err = a4l_get_subdinfo(&dsc, i, &sbinfo);
-               if (err < 0) {
-                       fprintf(stderr, 
-                               "insn_read: get_sbinfo(%d) failed (err = %d)\n",
-                               i, err);
-                       goto out_insn_read;
-               }
-               
-               if ((sbinfo->flags & A4L_SUBD_TYPES) == A4L_SUBD_AI)
-                       idx_subd = i;
-
-               i++;
-       }
+          subdevice */
+       if (idx_subd == -1)
+               idx_subd = dsc.idx_read_subd;
 
        if (idx_subd == -1) {
                fprintf(stderr, 
@@ -304,6 +284,28 @@ int main(int argc, char *argv[])
                goto  out_insn_read;
        }
 
+       if (verbose != 0)
+               printf("insn_read: selected subdevice index = %d\n", idx_subd);
+
+       /* We must check that the subdevice is really an AI one
+          (in case, the subdevice index was set with the option -s) */
+       err = a4l_get_subdinfo(&dsc, idx_subd, &sbinfo);
+       if (err < 0) {
+               fprintf(stderr, 
+                       "insn_read: get_sbinfo(%d) failed (err = %d)\n",
+                       idx_subd, err);
+               err = -EINVAL;
+               goto out_insn_read;
+       }
+
+       if ((sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_AI) {
+               fprintf(stderr, 
+                       "insn_read: wrong subdevice selected "
+                       "(not an analog input)\n");
+               err = -EINVAL;
+               goto out_insn_read;             
+       }
+
        if (idx_rng >= 0) {
 
                err = a4l_get_rnginfo(&dsc, 
diff --git a/src/utils/analogy/insn_write.c b/src/utils/analogy/insn_write.c
index 829b7d7..4039b90 100644
--- a/src/utils/analogy/insn_write.c
+++ b/src/utils/analogy/insn_write.c
@@ -70,7 +70,7 @@ void do_print_usage(void)
 
 int main(int argc, char *argv[])
 {
-       int i = 0, err = 0;
+       int err = 0;
        a4l_desc_t dsc = { .sbdata = NULL };
        a4l_sbinfo_t *sbinfo;
        a4l_chinfo_t *chinfo;
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
        /* Compute arguments */
        while ((err = getopt_long(argc,
                                  argv,
-                                 "vrd:s:c:R:V:h", insn_write_opts,
+                                 "vd:s:c:R:V:h", insn_write_opts,
                                  NULL)) >= 0) {
                switch (err) {
                case 'v':
@@ -134,13 +134,6 @@ int main(int argc, char *argv[])
                return err;
        }
 
-       /* Check there is an input subdevice */
-       if (dsc.idx_write_subd < 0) {
-               err = -ENOENT;
-               fprintf(stderr, "insn_write: no output subdevice available\n");
-               goto out_insn_write;
-       }
-
        if (verbose != 0) {
                printf("insn_write: device %s opened (fd=%d)\n", filename,
                       dsc.fd);
@@ -170,22 +163,9 @@ int main(int argc, char *argv[])
                printf("insn_write: complex descriptor retrieved\n");
 
        /* If no subdevice index was set, look for an analog output
-          subdevice (the first found will be selected) */
-       while (idx_subd == -1 && i < dsc.nb_subd) {
-               
-               err = a4l_get_subdinfo(&dsc, i, &sbinfo);
-               if (err < 0) {
-                       fprintf(stderr, 
-                               "insn_write: "
-                               "get_sbinfo(%d) failed (err = %d)\n", i, err);
-                       goto out_insn_write;
-               }
-               
-               if ((sbinfo->flags & A4L_SUBD_TYPES) == A4L_SUBD_AO)
-                       idx_subd = i;
-
-               i++;
-       }
+          subdevice */
+       if (idx_subd == -1)
+               idx_subd = dsc.idx_write_subd;
 
        if (idx_subd == -1) {
                fprintf(stderr, 
@@ -194,6 +174,28 @@ int main(int argc, char *argv[])
                goto  out_insn_write;
        }
 
+       if (verbose != 0)
+               printf("insn_write: selected subdevice index = %d\n", idx_subd);
+
+       /* We must check that the subdevice is really an AO one
+          (in case, the subdevice index was set with the option -s) */
+       err = a4l_get_subdinfo(&dsc, idx_subd, &sbinfo);
+       if (err < 0) {
+               fprintf(stderr, 
+                       "insn_write: get_sbinfo(%d) failed (err = %d)\n",
+                       idx_subd, err);
+               err = -EINVAL;
+               goto out_insn_write;
+       }
+
+       if ((sbinfo->flags & A4L_SUBD_TYPES) != A4L_SUBD_AO) {
+               fprintf(stderr, 
+                       "insn_write: wrong subdevice selected "
+                       "(not an analog output)\n");
+               err = -EINVAL;
+               goto out_insn_write;            
+       }
+
        if (idx_rng >= 0) {
 
                err = a4l_get_rnginfo(&dsc, 


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to