Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

passthrough.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "mmal.h"
  26. #include "core/mmal_component_private.h"
  27. #include "core/mmal_port_private.h"
  28. #include "mmal_logging.h"
  29. #define PASSTHROUGH_PORTS_NUM 1
  30. /*****************************************************************************/
  31. typedef struct MMAL_COMPONENT_MODULE_T
  32. {
  33. MMAL_BOOL_T error; /**< Error state */
  34. } MMAL_COMPONENT_MODULE_T;
  35. typedef struct MMAL_PORT_MODULE_T
  36. {
  37. MMAL_QUEUE_T *queue; /**< queue for the buffers sent to the ports */
  38. } MMAL_PORT_MODULE_T;
  39. /*****************************************************************************/
  40. /** Destroy a previously created component */
  41. static MMAL_STATUS_T passthrough_component_destroy(MMAL_COMPONENT_T *component)
  42. {
  43. unsigned int i;
  44. for(i = 0; i < component->input_num; i++)
  45. if(component->input[i]->priv->module->queue)
  46. mmal_queue_destroy(component->input[i]->priv->module->queue);
  47. if(component->input_num)
  48. mmal_ports_free(component->input, component->input_num);
  49. for(i = 0; i < component->output_num; i++)
  50. if(component->output[i]->priv->module->queue)
  51. mmal_queue_destroy(component->output[i]->priv->module->queue);
  52. if(component->output_num)
  53. mmal_ports_free(component->output, component->output_num);
  54. vcos_free(component->priv->module);
  55. return MMAL_SUCCESS;
  56. }
  57. /** Enable processing on a port */
  58. static MMAL_STATUS_T passthrough_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  59. {
  60. MMAL_PARAM_UNUSED(port);
  61. MMAL_PARAM_UNUSED(cb);
  62. return MMAL_SUCCESS;
  63. }
  64. /** Flush a port */
  65. static MMAL_STATUS_T passthrough_port_flush(MMAL_PORT_T *port)
  66. {
  67. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  68. MMAL_BUFFER_HEADER_T *buffer;
  69. /* Flush buffers that our component is holding on to */
  70. buffer = mmal_queue_get(port_module->queue);
  71. while(buffer)
  72. {
  73. mmal_port_buffer_header_callback(port, buffer);
  74. buffer = mmal_queue_get(port_module->queue);
  75. }
  76. return MMAL_SUCCESS;
  77. }
  78. /** Disable processing on a port */
  79. static MMAL_STATUS_T passthrough_port_disable(MMAL_PORT_T *port)
  80. {
  81. /* We just need to flush our internal queue */
  82. return passthrough_port_flush(port);
  83. }
  84. /** Send a buffer header to a port */
  85. static MMAL_STATUS_T passthrough_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  86. {
  87. MMAL_COMPONENT_T *component = port->component;
  88. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  89. MMAL_PORT_T **other_port, *in_port, *out_port;
  90. MMAL_BUFFER_HEADER_T **other_buffer, *in = 0, *out = 0;
  91. MMAL_STATUS_T status;
  92. if (module->error)
  93. {
  94. mmal_queue_put(port->priv->module->queue, buffer);
  95. return MMAL_SUCCESS; /* Just do nothing */
  96. }
  97. in_port = port->component->input[port->index];
  98. out_port = port->component->output[port->index];
  99. if (port->type == MMAL_PORT_TYPE_INPUT)
  100. {
  101. other_port = &out_port;
  102. other_buffer = &out;
  103. in = buffer;
  104. }
  105. else
  106. {
  107. other_port = &in_port;
  108. other_buffer = &in;
  109. out = buffer;
  110. }
  111. /* Get a buffer header from the matching port */
  112. *other_buffer = mmal_queue_get((*other_port)->priv->module->queue);
  113. if (!*other_buffer)
  114. {
  115. /* None available. Just queue the buffer header for now. */
  116. mmal_queue_put(port->priv->module->queue, buffer);
  117. return MMAL_SUCCESS;
  118. }
  119. /* Copy our input buffer header */
  120. status = mmal_buffer_header_replicate(out, in);
  121. if (status != MMAL_SUCCESS)
  122. goto error;
  123. /* Consume the input buffer */
  124. in->length = 0;
  125. /* Send buffers back */
  126. mmal_port_buffer_header_callback(in_port, in);
  127. mmal_port_buffer_header_callback(out_port, out);
  128. return MMAL_SUCCESS;
  129. error:
  130. mmal_queue_put(in_port->priv->module->queue, in);
  131. mmal_queue_put(out_port->priv->module->queue, out);
  132. status = mmal_event_error_send(port->component, status);
  133. if (status != MMAL_SUCCESS)
  134. {
  135. LOG_ERROR("unable to send an error event buffer (%i)", (int)status);
  136. return MMAL_SUCCESS;
  137. }
  138. module->error = 1;
  139. return MMAL_SUCCESS;
  140. }
  141. /** Set format on a port */
  142. static MMAL_STATUS_T passthrough_port_format_commit(MMAL_PORT_T *port)
  143. {
  144. /* Sanity check */
  145. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  146. {
  147. LOG_ERROR("output port is read-only");
  148. return MMAL_EINVAL;
  149. }
  150. return mmal_format_full_copy(port->component->output[port->index]->format, port->format);
  151. }
  152. static MMAL_STATUS_T passthrough_port_parameter_set(MMAL_PORT_T *port, const MMAL_PARAMETER_HEADER_T *param)
  153. {
  154. MMAL_COMPONENT_T *component = port->component;
  155. MMAL_PORT_T *in = component->input[port->index], *out = component->input[port->index];
  156. switch (param->id)
  157. {
  158. case MMAL_PARAMETER_BUFFER_REQUIREMENTS:
  159. {
  160. /* Propagate the requirements to the matching input and output the ports */
  161. const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *req = (const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *)param;
  162. uint32_t buffer_num_min = MMAL_MAX(port->buffer_num_min, req->buffer_num_min);
  163. uint32_t buffer_num_recommended = MMAL_MAX(port->buffer_num_recommended, req->buffer_num_recommended);
  164. uint32_t buffer_size_min = MMAL_MAX(port->buffer_size_min, req->buffer_size_min);
  165. uint32_t buffer_size_recommended = MMAL_MAX(port->buffer_size_recommended, req->buffer_size_recommended);
  166. in->buffer_num_min = buffer_num_min;
  167. in->buffer_num_recommended = buffer_num_recommended;
  168. in->buffer_size_min = buffer_size_min;
  169. in->buffer_size_recommended = buffer_size_recommended;
  170. out->buffer_num_min = buffer_num_min;
  171. out->buffer_num_recommended = buffer_num_recommended;
  172. out->buffer_size_min = buffer_size_min;
  173. out->buffer_size_recommended = buffer_size_recommended;
  174. }
  175. return MMAL_SUCCESS;
  176. default:
  177. return MMAL_ENOSYS;
  178. }
  179. }
  180. /** Create an instance of a component */
  181. static MMAL_STATUS_T mmal_component_create_passthrough(const char *name, MMAL_COMPONENT_T *component)
  182. {
  183. MMAL_COMPONENT_MODULE_T *module;
  184. MMAL_STATUS_T status = MMAL_ENOMEM;
  185. unsigned int i;
  186. MMAL_PARAM_UNUSED(name);
  187. /* Allocate the context for our module */
  188. component->priv->module = module = vcos_malloc(sizeof(*module), "mmal module");
  189. if (!module)
  190. return MMAL_ENOMEM;
  191. memset(module, 0, sizeof(*module));
  192. component->priv->pf_destroy = passthrough_component_destroy;
  193. /* Allocate and initialise all the ports for this component */
  194. component->input = mmal_ports_alloc(component, PASSTHROUGH_PORTS_NUM,
  195. MMAL_PORT_TYPE_INPUT, sizeof(MMAL_PORT_MODULE_T));
  196. if(!component->input)
  197. goto error;
  198. component->input_num = PASSTHROUGH_PORTS_NUM;
  199. for(i = 0; i < component->input_num; i++)
  200. {
  201. component->input[i]->priv->pf_enable = passthrough_port_enable;
  202. component->input[i]->priv->pf_disable = passthrough_port_disable;
  203. component->input[i]->priv->pf_flush = passthrough_port_flush;
  204. component->input[i]->priv->pf_send = passthrough_port_send;
  205. component->input[i]->priv->pf_set_format = passthrough_port_format_commit;
  206. component->input[i]->priv->pf_parameter_set = passthrough_port_parameter_set;
  207. component->input[i]->buffer_num_min = 1;
  208. component->input[i]->buffer_num_recommended = 0;
  209. component->input[i]->priv->module->queue = mmal_queue_create();
  210. if(!component->input[i]->priv->module->queue)
  211. goto error;
  212. }
  213. component->output = mmal_ports_alloc(component, PASSTHROUGH_PORTS_NUM,
  214. MMAL_PORT_TYPE_OUTPUT, sizeof(MMAL_PORT_MODULE_T));
  215. if(!component->output)
  216. goto error;
  217. component->output_num = PASSTHROUGH_PORTS_NUM;
  218. for(i = 0; i < component->output_num; i++)
  219. {
  220. component->output[i]->priv->pf_enable = passthrough_port_enable;
  221. component->output[i]->priv->pf_disable = passthrough_port_disable;
  222. component->output[i]->priv->pf_flush = passthrough_port_flush;
  223. component->output[i]->priv->pf_send = passthrough_port_send;
  224. component->output[i]->priv->pf_set_format = passthrough_port_format_commit;
  225. component->output[i]->priv->pf_parameter_set = passthrough_port_parameter_set;
  226. component->output[i]->buffer_num_min = 1;
  227. component->output[i]->buffer_num_recommended = 0;
  228. component->output[i]->capabilities = MMAL_PORT_CAPABILITY_PASSTHROUGH;
  229. component->output[i]->priv->module->queue = mmal_queue_create();
  230. if(!component->output[i]->priv->module->queue)
  231. goto error;
  232. }
  233. return MMAL_SUCCESS;
  234. error:
  235. passthrough_component_destroy(component);
  236. return status;
  237. }
  238. MMAL_CONSTRUCTOR(mmal_register_component_passthrough);
  239. void mmal_register_component_passthrough(void)
  240. {
  241. mmal_component_supplier_register("passthrough", mmal_component_create_passthrough);
  242. }