| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "mmal_util_params.h" |
| 29 | |
| 30 | /** Helper function to set the value of a boolean parameter */ |
| 31 | MMAL_STATUS_T mmal_port_parameter_set_boolean(MMAL_PORT_T *port, uint32_t id, MMAL_BOOL_T value) |
| 32 | { |
| 33 | MMAL_PARAMETER_BOOLEAN_T param = {{id, sizeof(param)}, value}; |
| 34 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 35 | } |
| 36 | |
| 37 | /** Helper function to get the value of a boolean parameter */ |
| 38 | MMAL_STATUS_T mmal_port_parameter_get_boolean(MMAL_PORT_T *port, uint32_t id, MMAL_BOOL_T *value) |
| 39 | { |
| 40 | MMAL_PARAMETER_BOOLEAN_T param = {{id, sizeof(param)}, 0}; |
| 41 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 42 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 43 | if (status == MMAL_SUCCESS) |
| 44 | *value = param.enable; |
| 45 | return status; |
| 46 | } |
| 47 | |
| 48 | /** Helper function to set the value of a 64 bits unsigned integer parameter */ |
| 49 | MMAL_STATUS_T mmal_port_parameter_set_uint64(MMAL_PORT_T *port, uint32_t id, uint64_t value) |
| 50 | { |
| 51 | MMAL_PARAMETER_UINT64_T param = {{id, sizeof(param)}, value}; |
| 52 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 53 | } |
| 54 | |
| 55 | /** Helper function to get the value of a 64 bits unsigned integer parameter */ |
| 56 | MMAL_STATUS_T mmal_port_parameter_get_uint64(MMAL_PORT_T *port, uint32_t id, uint64_t *value) |
| 57 | { |
| 58 | MMAL_PARAMETER_UINT64_T param = {{id, sizeof(param)}, 0LL}; |
| 59 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 60 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 61 | if (status == MMAL_SUCCESS) |
| 62 | *value = param.value; |
| 63 | return status; |
| 64 | } |
| 65 | |
| 66 | /** Helper function to set the value of a 64 bits signed integer parameter */ |
| 67 | MMAL_STATUS_T mmal_port_parameter_set_int64(MMAL_PORT_T *port, uint32_t id, int64_t value) |
| 68 | { |
| 69 | MMAL_PARAMETER_INT64_T param = {{id, sizeof(param)}, value}; |
| 70 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 71 | } |
| 72 | |
| 73 | /** Helper function to get the value of a 64 bits signed integer parameter */ |
| 74 | MMAL_STATUS_T mmal_port_parameter_get_int64(MMAL_PORT_T *port, uint32_t id, int64_t *value) |
| 75 | { |
| 76 | MMAL_PARAMETER_INT64_T param = {{id, sizeof(param)}, 0LL}; |
| 77 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 78 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 79 | if (status == MMAL_SUCCESS) |
| 80 | *value = param.value; |
| 81 | return status; |
| 82 | } |
| 83 | |
| 84 | /** Helper function to set the value of a 32 bits unsigned integer parameter */ |
| 85 | MMAL_STATUS_T mmal_port_parameter_set_uint32(MMAL_PORT_T *port, uint32_t id, uint32_t value) |
| 86 | { |
| 87 | MMAL_PARAMETER_UINT32_T param = {{id, sizeof(param)}, value}; |
| 88 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 89 | } |
| 90 | |
| 91 | /** Helper function to get the value of a 32 bits unsigned integer parameter */ |
| 92 | MMAL_STATUS_T mmal_port_parameter_get_uint32(MMAL_PORT_T *port, uint32_t id, uint32_t *value) |
| 93 | { |
| 94 | MMAL_PARAMETER_UINT32_T param = {{id, sizeof(param)}, 0}; |
| 95 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 96 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 97 | if (status == MMAL_SUCCESS) |
| 98 | *value = param.value; |
| 99 | return status; |
| 100 | } |
| 101 | |
| 102 | /** Helper function to set the value of a 32 bits signed integer parameter */ |
| 103 | MMAL_STATUS_T mmal_port_parameter_set_int32(MMAL_PORT_T *port, uint32_t id, int32_t value) |
| 104 | { |
| 105 | MMAL_PARAMETER_INT32_T param = {{id, sizeof(param)}, value}; |
| 106 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 107 | } |
| 108 | |
| 109 | /** Helper function to get the value of a 32 bits signed integer parameter */ |
| 110 | MMAL_STATUS_T mmal_port_parameter_get_int32(MMAL_PORT_T *port, uint32_t id, int32_t *value) |
| 111 | { |
| 112 | MMAL_PARAMETER_INT32_T param = {{id, sizeof(param)}, 0}; |
| 113 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 114 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 115 | if (status == MMAL_SUCCESS) |
| 116 | *value = param.value; |
| 117 | return status; |
| 118 | } |
| 119 | |
| 120 | /** Helper function to set the value of a rational parameter */ |
| 121 | MMAL_STATUS_T mmal_port_parameter_set_rational(MMAL_PORT_T *port, uint32_t id, MMAL_RATIONAL_T value) |
| 122 | { |
| 123 | MMAL_PARAMETER_RATIONAL_T param = {{id, sizeof(param)}, {value.num, value.den}}; |
| 124 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 125 | } |
| 126 | |
| 127 | /** Helper function to get the value of a rational parameter */ |
| 128 | MMAL_STATUS_T mmal_port_parameter_get_rational(MMAL_PORT_T *port, uint32_t id, MMAL_RATIONAL_T *value) |
| 129 | { |
| 130 | MMAL_PARAMETER_RATIONAL_T param = {{id, sizeof(param)}, {0,0}}; |
| 131 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 132 | MMAL_STATUS_T status = mmal_port_parameter_get(port, ¶m.hdr); |
| 133 | if (status == MMAL_SUCCESS) |
| 134 | *value = param.value; |
| 135 | return status; |
| 136 | } |
| 137 | |
| 138 | /** Helper function to set the value of a string parameter */ |
| 139 | MMAL_STATUS_T mmal_port_parameter_set_string(MMAL_PORT_T *port, uint32_t id, const char *value) |
| 140 | { |
| 141 | MMAL_PARAMETER_STRING_T *param = 0; |
| 142 | MMAL_STATUS_T status; |
| 143 | size_t param_size = sizeof(param->hdr) + strlen(value) + 1; |
| 144 | |
| 145 | param = calloc(1, param_size); |
| 146 | if (!param) |
| 147 | return MMAL_ENOMEM; |
| 148 | |
| 149 | param->hdr.id = id; |
| 150 | param->hdr.size = param_size; |
| 151 | memcpy(param->str, value, strlen(value)+1); |
| 152 | status = mmal_port_parameter_set(port, ¶m->hdr); |
| 153 | free(param); |
| 154 | return status; |
| 155 | } |
| 156 | |
| 157 | /** Helper function to set a MMAL_PARAMETER_URI_T parameter on a port */ |
| 158 | MMAL_STATUS_T mmal_util_port_set_uri(MMAL_PORT_T *port, const char *uri) |
| 159 | { |
| 160 | return mmal_port_parameter_set_string(port, MMAL_PARAMETER_URI, uri); |
| 161 | } |
| 162 | |
| 163 | /** Helper function to set the value of an array of bytes parameter */ |
| 164 | MMAL_STATUS_T mmal_port_parameter_set_bytes(MMAL_PORT_T *port, uint32_t id, |
| 165 | const uint8_t *data, unsigned int size) |
| 166 | { |
| 167 | MMAL_PARAMETER_BYTES_T *param = 0; |
| 168 | MMAL_STATUS_T status; |
| 169 | size_t param_size = sizeof(param->hdr) + size; |
| 170 | |
| 171 | param = calloc(1, param_size); |
| 172 | if (!param) |
| 173 | return MMAL_ENOMEM; |
| 174 | |
| 175 | param->hdr.id = id; |
| 176 | param->hdr.size = param_size; |
| 177 | memcpy(param->data, data, size); |
| 178 | status = mmal_port_parameter_set(port, ¶m->hdr); |
| 179 | free(param); |
| 180 | return status; |
| 181 | } |
| 182 | |
| 183 | /** Set the display region. |
| 184 | * @param port port to configure |
| 185 | * @param region region |
| 186 | * |
| 187 | * @return MMAL_SUCCESS or error |
| 188 | */ |
| 189 | |
| 190 | MMAL_STATUS_T mmal_util_set_display_region(MMAL_PORT_T *port, |
| 191 | MMAL_DISPLAYREGION_T *region) |
| 192 | { |
| 193 | region->hdr.id = MMAL_PARAMETER_DISPLAYREGION; |
| 194 | region->hdr.size = sizeof(*region); |
| 195 | return mmal_port_parameter_set(port, ®ion->hdr); |
| 196 | } |
| 197 | |
| 198 | MMAL_STATUS_T mmal_util_camera_use_stc_timestamp(MMAL_PORT_T *port, MMAL_CAMERA_STC_MODE_T mode) |
| 199 | { |
| 200 | MMAL_PARAMETER_CAMERA_STC_MODE_T param = |
| 201 | {{MMAL_PARAMETER_USE_STC, sizeof(MMAL_PARAMETER_CAMERA_STC_MODE_T)},mode}; |
| 202 | return mmal_port_parameter_set(port, ¶m.hdr); |
| 203 | } |
| 204 | |
| 205 | MMAL_STATUS_T mmal_util_get_core_port_stats(MMAL_PORT_T *port, |
| 206 | MMAL_CORE_STATS_DIR dir, |
| 207 | MMAL_BOOL_T reset, |
| 208 | MMAL_CORE_STATISTICS_T *stats) |
| 209 | { |
| 210 | MMAL_PARAMETER_CORE_STATISTICS_T param; |
| 211 | MMAL_STATUS_T ret; |
| 212 | |
| 213 | memset(¶m, 0, sizeof(param)); |
| 214 | param.hdr.id = MMAL_PARAMETER_CORE_STATISTICS; |
| 215 | param.hdr.size = sizeof(param); |
| 216 | param.dir = dir; |
| 217 | param.reset = reset; |
| 218 | // coverity[overrun-buffer-val] Structure accessed correctly via size field |
| 219 | ret = mmal_port_parameter_get(port, ¶m.hdr); |
| 220 | if (ret == MMAL_SUCCESS) |
| 221 | *stats = param.stats; |
| 222 | return ret; |
| 223 | } |
| 224 | |