Module: xenomai-3
Branch: master
Commit: 522dc5d6cf8f58fee5f489f5c2736ff1ddb84703
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=522dc5d6cf8f58fee5f489f5c2736ff1ddb84703

Author: Jorge Ramirez-Ortiz <j...@xenomai.org>
Date:   Fri Aug  7 09:34:41 2015 -0400

lib/analogy: check for errors in the calibration file

Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-or...@linaro.org>

---

 lib/analogy/calibration.c |   29 +++++++++++++++++++++--------
 utils/analogy/insn_read.c |    2 +-
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/lib/analogy/calibration.c b/lib/analogy/calibration.c
index 14ca454..b50cb2f 100644
--- a/lib/analogy/calibration.c
+++ b/lib/analogy/calibration.c
@@ -30,6 +30,12 @@
 #include "boilerplate/list.h"
 #include "calibration.h"
 
+#define CHK(func, ...)                                                         
\
+do {                                                                           
\
+       int rc = func(__VA_ARGS__);                                             
\
+       if (rc < 0)                                                             
\
+               return -1;                                                      
\
+} while (0)
 
 #define ARRAY_LEN(a)  (sizeof(a) / sizeof((a)[0]))
 
@@ -212,12 +218,19 @@ int a4l_read_calibration_file(char *name, struct 
a4l_calibration_data *data)
        if (d == NULL)
                return -1;
 
-       read_str(&data->driver_name, d, PLATFORM_STR, DRIVER_STR);
-       read_str(&data->board_name, d, PLATFORM_STR, BOARD_STR);
+       CHK(read_str, &data->driver_name, d, PLATFORM_STR, DRIVER_STR);
+       CHK(read_str, &data->board_name, d, PLATFORM_STR, BOARD_STR);
 
        for (k = 0; k < ARRAY_LEN(subdevice); k++) {
                read_int(&nb_elements, d, subdevice[k], -1, ELEMENTS_STR);
-               read_int(&index, d, subdevice[k], -1, INDEX_STR);
+               if (nb_elements < 0 ) {
+                       /* AO is optional */
+                       if (!strncmp(subdevice[k], AO_SUBD_STR, 
sizeof(AO_SUBD_STR)))
+                            break;
+                       return -1;
+               }
+
+               CHK(read_int, &index, d, subdevice[k], -1, INDEX_STR);
 
                if (strncmp(subdevice[k], AI_SUBD_STR,
                            strlen(AI_SUBD_STR)) == 0) {
@@ -236,19 +249,19 @@ int a4l_read_calibration_file(char *name, struct 
a4l_calibration_data *data)
                }
 
                for (i = 0; i < nb_elements; i++) {
-                       read_int(&p->expansion, d, subdevice[k], i,
+                       CHK(read_int, &p->expansion, d, subdevice[k], i,
                                 EXPANSION_STR);
-                       read_int(&p->nb_coeff, d, subdevice[k], i,
+                       CHK(read_int, &p->nb_coeff, d, subdevice[k], i,
                                 NBCOEFF_STR);
-                       read_int(&p->channel, d, subdevice[k], i,
+                       CHK(read_int, &p->channel, d, subdevice[k], i,
                                 CHANNEL_STR);
-                       read_int(&p->range, d, subdevice[k], i,
+                       CHK(read_int, &p->range, d, subdevice[k], i,
                                 RANGE_STR);
 
                        p->coeff = malloc(p->nb_coeff * sizeof(double));
 
                        for (j = 0; j < p->nb_coeff; j++) {
-                               read_dbl(&p->coeff[j], d, subdevice[k], i,
+                               CHK(read_dbl,&p->coeff[j], d, subdevice[k], i,
                                         COEFF_STR, j);
                        }
 
diff --git a/utils/analogy/insn_read.c b/utils/analogy/insn_read.c
index 8d457ed..8aa0740 100644
--- a/utils/analogy/insn_read.c
+++ b/utils/analogy/insn_read.c
@@ -223,7 +223,7 @@ static int dump_calibrated(a4l_desc_t *dsc, unsigned char 
*buf, int size)
        err = a4l_read_calibration_file(calibration_file, &cal_info);
        if (err < 0) {
                fprintf(stderr,
-                       "insn_read: failed to read /tmp/calibration.txt \n");
+                       "insn_read: error reading the calibration file \n");
                goto out;
        }
 


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://xenomai.org/mailman/listinfo/xenomai-git

Reply via email to