libexplain  1.4.D001
libexplain/iocontrol/vidioc_g_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_g_ext_ctrls.h>
00031 #include <libexplain/is_efault.h>
00032 
00033 #ifdef VIDIOC_G_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, 0, 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_G_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_G_EXT_CTRLS, &ctrls) < 0
00118                 &&
00119                     errno == EINVAL
00120                 )
00121                 {
00122                     char            name[64];
00123 
00124                     snprintf(name, sizeof(name), "data->controls[%d].id", j);
00125                     explain_buffer_einval_vague(sb, name);
00126                     return;
00127                 }
00128             }
00129         }
00130 
00131         /* No idea. */
00132         generic:
00133         explain_buffer_einval_vague(sb, "data");
00134         break;
00135 
00136     default:
00137         explain_iocontrol_generic_print_explanation
00138         (
00139             p,
00140             sb,
00141             errnum,
00142             fildes,
00143             request,
00144             data
00145         );
00146         break;
00147     }
00148 }
00149 
00150 
00151 static void
00152 print_data_returned(const explain_iocontrol_t *p, explain_string_buffer_t *sb,
00153     int errnum, int fildes, int request, const void *data)
00154 {
00155     (void)p;
00156     (void)errnum;
00157     (void)request;
00158     explain_buffer_v4l2_ext_controls(sb, data, 1, fildes);
00159 }
00160 
00161 
00162 const explain_iocontrol_t explain_iocontrol_vidioc_g_ext_ctrls =
00163 {
00164     "VIDIOC_G_EXT_CTRLS", /* name */
00165     VIDIOC_G_EXT_CTRLS, /* value */
00166     0, /* disambiguate */
00167     0, /* print_name */
00168     print_data,
00169     print_explanation,
00170     print_data_returned,
00171     sizeof(struct v4l2_ext_controls), /* data_size */
00172     "struct v4l2_ext_controls *", /* data_type */
00173     0, /* flags */
00174     __FILE__,
00175     __LINE__,
00176 };
00177 
00178 #else /* ndef VIDIOC_G_EXT_CTRLS */
00179 
00180 const explain_iocontrol_t explain_iocontrol_vidioc_g_ext_ctrls =
00181 {
00182     0, /* name */
00183     0, /* value */
00184     0, /* disambiguate */
00185     0, /* print_name */
00186     0, /* print_data */
00187     0, /* print_explanation */
00188     0, /* print_data_returned */
00189     0, /* data_size */
00190     0, /* data_type */
00191     0, /* flags */
00192     __FILE__,
00193     __LINE__,
00194 };
00195 
00196 #endif /* VIDIOC_G_EXT_CTRLS */
00197 
00198 /* vim: set ts=8 sw=4 et : */