libexplain  1.4.D001
libexplain/iocontrol/vidioc_s_ext_ctrls.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2011, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/errno.h>
00021 #include <libexplain/ac/linux/videodev2.h>
00022 #include <libexplain/ac/stdio.h>
00023 #include <libexplain/ac/string.h>
00024 #include <libexplain/ac/sys/ioctl.h>
00025 
00026 #include <libexplain/buffer/einval.h>
00027 #include <libexplain/buffer/is_the_null_pointer.h>
00028 #include <libexplain/buffer/v4l2_ext_controls.h>
00029 #include <libexplain/iocontrol/generic.h>
00030 #include <libexplain/iocontrol/vidioc_s_ext_ctrls.h>
00031 #include <libexplain/is_efault.h>
00032 
00033 #ifdef VIDIOC_S_EXT_CTRLS
00034 
00035 
00036 static void
00037 print_data(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00038     int errnum, int fildes, int request, const void *data)
00039 {
00040     (void)p;
00041     (void)errnum;
00042     (void)request;
00043     explain_buffer_v4l2_ext_controls(sb, data, 1, fildes);
00044 }
00045 
00046 
00047 static void
00048 print_explanation(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00049     int errnum, int fildes, int request, const void *data)
00050 {
00051     switch (errnum)
00052     {
00053     case EINVAL:
00054         if (!data)
00055         {
00056             explain_buffer_is_the_null_pointer(sb, "data");
00057             return;
00058         }
00059 
00060         {
00061             const struct v4l2_ext_controls *arg;
00062             struct v4l2_ext_controls ctrls;
00063             unsigned        j;
00064 
00065             /*
00066              * Note: fuzz testing could lead to improbably large item
00067              * counts, and this next test should catch most of them.
00068              */
00069             arg = data;
00070             if (explain_is_efault_pointer(arg, sizeof(*arg)))
00071                 goto generic;
00072             if (arg->count)
00073             {
00074                 size_t          nbytes;
00075 
00076                 nbytes = arg->count * sizeof(arg->controls[0]);
00077                 if(explain_is_efault_pointer(arg->controls, nbytes))
00078                     goto generic;
00079             }
00080 
00081             /*
00082              * The control class may be the problem.
00083              */
00084             if (arg->count == 0)
00085                 goto bad_ctrl_class;
00086             memset(&ctrls, 0, sizeof(ctrls));
00087             ctrls.ctrl_class = arg->ctrl_class;
00088             if
00089             (
00090                 ioctl(fildes, VIDIOC_TRY_EXT_CTRLS, &ctrls) < 0
00091             &&
00092                 errno == EINVAL
00093             )
00094             {
00095                 bad_ctrl_class:
00096                 explain_buffer_einval_vague(sb, "data->ctrl_class");
00097                 return;
00098             }
00099 
00100             /*
00101              * One of the controls may be the problem.
00102              */
00103             for (j = 0; j < arg->count; ++j)
00104             {
00105                 struct v4l2_ext_control ctrl;
00106 
00107                 /* not risking changing caller's data */
00108                 ctrl = arg->controls[j];
00109 
00110                 memset(&ctrls, 0, sizeof(ctrls));
00111                 ctrls.ctrl_class = arg->ctrl_class;
00112                 ctrls.controls = &ctrl;
00113                 ctrls.count = 1;
00114 
00115                 if
00116                 (
00117                     ioctl(fildes, VIDIOC_TRY_EXT_CTRLS, &ctrls) < 0
00118                 &&
00119                     errno == EINVAL
00120                 )
00121                 {
00122                     char            name[64];
00123 
00124                     /*
00125                      * Check the the control id.
00126                      */
00127                     ctrl = arg->controls[j];
00128                     memset(&ctrls, 0, sizeof(ctrl));
00129                     ctrls.ctrl_class = arg->ctrl_class;
00130                     ctrls.controls = &ctrl;
00131                     ctrls.count = 1;
00132 
00133                     if
00134                     (
00135                         ioctl(fildes, VIDIOC_G_EXT_CTRLS, &ctrls) < 0
00136                     &&
00137                         errno == EINVAL
00138                     )
00139                     {
00140                         snprintf(name, sizeof(name),
00141                             "data->controls[%d].id", j);
00142                     }
00143                     else
00144                     {
00145                         /*
00146                          * Must be the value.
00147                          */
00148                         snprintf(name, sizeof(name),
00149                             "data->controls[%d].value", j);
00150                     }
00151                     explain_buffer_einval_vague(sb, name);
00152                     return;
00153                 }
00154             }
00155         }
00156 
00157         /* No idea. */
00158         generic:
00159         explain_buffer_einval_vague(sb, "data");
00160         break;
00161 
00162     default:
00163         explain_iocontrol_generic_print_explanation
00164         (
00165             p,
00166             sb,
00167             errnum,
00168             fildes,
00169             request,
00170             data
00171         );
00172         break;
00173     }
00174 }
00175 
00176 
00177 static void
00178 print_data_returned(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00179     int errnum, int fildes, int request, const void *data)
00180 {
00181     (void)p;
00182     (void)errnum;
00183     (void)request;
00184     explain_buffer_v4l2_ext_controls(sb, data, 1, fildes);
00185 }
00186 
00187 
00188 const explain_iocontrol_t explain_iocontrol_vidioc_s_ext_ctrls =
00189 {
00190     "VIDIOC_S_EXT_CTRLS", /* name */
00191     VIDIOC_S_EXT_CTRLS, /* value */
00192     0, /* disambiguate */
00193     0, /* print_name */
00194     print_data,
00195     print_explanation,
00196     print_data_returned,
00197     sizeof(struct v4l2_ext_controls), /* data_size */
00198     "struct v4l2_ext_controls *", /* data_type */
00199     0, /* flags */
00200     __FILE__,
00201     __LINE__,
00202 };
00203 
00204 const explain_iocontrol_t explain_iocontrol_vidioc_try_ext_ctrls =
00205 {
00206     "VIDIOC_TRY_EXT_CTRLS", /* name */
00207     VIDIOC_TRY_EXT_CTRLS, /* value */
00208     0, /* disambiguate */
00209     0, /* print_name */
00210     print_data,
00211     print_explanation,
00212     print_data_returned,
00213     sizeof(struct v4l2_ext_controls), /* data_size */
00214     "struct v4l2_ext_controls *", /* data_type */
00215     0, /* flags */
00216     __FILE__,
00217     __LINE__,
00218 };
00219 
00220 #else /* ndef VIDIOC_S_EXT_CTRLS */
00221 
00222 const explain_iocontrol_t explain_iocontrol_vidioc_s_ext_ctrls =
00223 {
00224     0, /* name */
00225     0, /* value */
00226     0, /* disambiguate */
00227     0, /* print_name */
00228     0, /* print_data */
00229     0, /* print_explanation */
00230     0, /* print_data_returned */
00231     0, /* data_size */
00232     0, /* data_type */
00233     0, /* flags */
00234     __FILE__,
00235     __LINE__,
00236 };
00237 
00238 const explain_iocontrol_t explain_iocontrol_vidioc_try_ext_ctrls =
00239 {
00240     0, /* name */
00241     0, /* value */
00242     0, /* disambiguate */
00243     0, /* print_name */
00244     0, /* print_data */
00245     0, /* print_explanation */
00246     0, /* print_data_returned */
00247     0, /* data_size */
00248     0, /* data_type */
00249     0, /* flags */
00250     __FILE__,
00251     __LINE__,
00252 };
00253 
00254 #endif /* VIDIOC_S_EXT_CTRLS */
00255 
00256 /* vim: set ts=8 sw=4 et : */