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.

splitter.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 SPLITTER_OUTPUT_PORTS_NUM 4 /* 4 should do for now */
  30. /*****************************************************************************/
  31. typedef struct MMAL_COMPONENT_MODULE_T
  32. {
  33. uint32_t enabled_flags; /**< Flags indicating which output port is enabled */
  34. uint32_t sent_flags; /**< Flags indicating which output port we've already sent data to */
  35. MMAL_BOOL_T error; /**< Error state */
  36. } MMAL_COMPONENT_MODULE_T;
  37. typedef struct MMAL_PORT_MODULE_T
  38. {
  39. MMAL_QUEUE_T *queue; /**< queue for the buffers sent to the ports */
  40. } MMAL_PORT_MODULE_T;
  41. /*****************************************************************************/
  42. /** Destroy a previously created component */
  43. static MMAL_STATUS_T splitter_component_destroy(MMAL_COMPONENT_T *component)
  44. {
  45. unsigned int i;
  46. for(i = 0; i < component->input_num; i++)
  47. if(component->input[i]->priv->module->queue)
  48. mmal_queue_destroy(component->input[i]->priv->module->queue);
  49. if(component->input_num)
  50. mmal_ports_free(component->input, component->input_num);
  51. for(i = 0; i < component->output_num; i++)
  52. if(component->output[i]->priv->module->queue)
  53. mmal_queue_destroy(component->output[i]->priv->module->queue);
  54. if(component->output_num)
  55. mmal_ports_free(component->output, component->output_num);
  56. vcos_free(component->priv->module);
  57. return MMAL_SUCCESS;
  58. }
  59. /** Enable processing on a port */
  60. static MMAL_STATUS_T splitter_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  61. {
  62. #if 0
  63. MMAL_COMPONENT_T *component = port->component;
  64. uint32_t buffer_num, buffer_size;
  65. unsigned int i;
  66. /* Find the max and apply that to all ports */
  67. buffer_num = component->input[0]->buffer_num;
  68. buffer_size = component->input[0]->buffer_size;
  69. for (i = 0; i < component->output_num; i++)
  70. {
  71. buffer_num = MMAL_MAX(buffer_num, component->output[i]->buffer_num);
  72. buffer_size = MMAL_MAX(buffer_num, component->output[i]->buffer_size);
  73. }
  74. component->input[0]->buffer_num = buffer_num;
  75. component->input[0]->buffer_size = buffer_size;
  76. for (i = 0; i < component->output_num; i++)
  77. {
  78. component->output[i]->buffer_num = buffer_num;
  79. component->output[i]->buffer_size = buffer_num;
  80. }
  81. #endif
  82. MMAL_PARAM_UNUSED(cb);
  83. if (port->buffer_size)
  84. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  85. port->component->priv->module->enabled_flags |= (1<<port->index);
  86. return MMAL_SUCCESS;
  87. }
  88. /** Flush a port */
  89. static MMAL_STATUS_T splitter_port_flush(MMAL_PORT_T *port)
  90. {
  91. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  92. MMAL_BUFFER_HEADER_T *buffer;
  93. /* Flush buffers that our component is holding on to */
  94. buffer = mmal_queue_get(port_module->queue);
  95. while(buffer)
  96. {
  97. mmal_port_buffer_header_callback(port, buffer);
  98. buffer = mmal_queue_get(port_module->queue);
  99. }
  100. if (port->type == MMAL_PORT_TYPE_INPUT)
  101. port->component->priv->module->sent_flags = 0;
  102. return MMAL_SUCCESS;
  103. }
  104. /** Disable processing on a port */
  105. static MMAL_STATUS_T splitter_port_disable(MMAL_PORT_T *port)
  106. {
  107. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  108. port->component->priv->module->enabled_flags &= ~(1<<port->index);
  109. /* We just need to flush our internal queue */
  110. return splitter_port_flush(port);
  111. }
  112. /** Send a buffer header to a port */
  113. static MMAL_STATUS_T splitter_send_output(MMAL_BUFFER_HEADER_T *buffer, MMAL_PORT_T *out_port)
  114. {
  115. MMAL_BUFFER_HEADER_T *out;
  116. MMAL_STATUS_T status;
  117. /* Get a buffer header from output port */
  118. out = mmal_queue_get(out_port->priv->module->queue);
  119. if (!out)
  120. return MMAL_EAGAIN;
  121. /* Copy our input buffer header */
  122. status = mmal_buffer_header_replicate(out, buffer);
  123. if (status != MMAL_SUCCESS)
  124. goto error;
  125. /* Send buffer back */
  126. mmal_port_buffer_header_callback(out_port, out);
  127. return MMAL_SUCCESS;
  128. error:
  129. mmal_queue_put_back(out_port->priv->module->queue, out);
  130. return status;
  131. }
  132. /** Send a buffer header to a port */
  133. static MMAL_STATUS_T splitter_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  134. {
  135. MMAL_COMPONENT_T *component = port->component;
  136. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  137. MMAL_PORT_T *in_port, *out_port;
  138. MMAL_BUFFER_HEADER_T *in;
  139. MMAL_STATUS_T status;
  140. unsigned int i;
  141. mmal_queue_put(port->priv->module->queue, buffer);
  142. if (module->error)
  143. return MMAL_SUCCESS; /* Just do nothing */
  144. /* Get input buffer header */
  145. in_port = component->input[0];
  146. in = mmal_queue_get(in_port->priv->module->queue);
  147. if (!in)
  148. return MMAL_SUCCESS; /* Nothing to do */
  149. for (i = 0; i < component->output_num; i++)
  150. {
  151. out_port = component->output[i];
  152. status = splitter_send_output(in, out_port);
  153. if (status != MMAL_SUCCESS && status != MMAL_EAGAIN)
  154. goto error;
  155. if (status == MMAL_SUCCESS)
  156. module->sent_flags |= (1<<i);
  157. }
  158. /* Check if we're done with the input buffer */
  159. if ((module->sent_flags & module->enabled_flags) == module->enabled_flags)
  160. {
  161. in->length = 0; /* Consume the input buffer */
  162. mmal_port_buffer_header_callback(in_port, in);
  163. module->sent_flags = 0;
  164. return MMAL_SUCCESS;
  165. }
  166. /* We're not done yet so put the buffer back in the queue */
  167. mmal_queue_put(in_port->priv->module->queue, in);
  168. return MMAL_SUCCESS;
  169. error:
  170. mmal_queue_put(in_port->priv->module->queue, in);
  171. status = mmal_event_error_send(port->component, status);
  172. if (status != MMAL_SUCCESS)
  173. {
  174. LOG_ERROR("unable to send an error event buffer (%i)", (int)status);
  175. return MMAL_SUCCESS;
  176. }
  177. module->error = 1;
  178. return MMAL_SUCCESS;
  179. }
  180. /** Set format on a port */
  181. static MMAL_STATUS_T splitter_port_format_commit(MMAL_PORT_T *port)
  182. {
  183. MMAL_COMPONENT_T *component = port->component;
  184. MMAL_STATUS_T status;
  185. unsigned int i;
  186. /* Sanity check */
  187. if (port->type == MMAL_PORT_TYPE_OUTPUT)
  188. {
  189. LOG_ERROR("output port is read-only");
  190. return MMAL_EINVAL;
  191. }
  192. /* Commit the format on all output ports */
  193. for (i = 0; i < component->output_num; i++)
  194. {
  195. status = mmal_format_full_copy(component->output[i]->format, port->format);
  196. if (status != MMAL_SUCCESS)
  197. return status;
  198. }
  199. return MMAL_SUCCESS;
  200. }
  201. static MMAL_STATUS_T splitter_port_parameter_set(MMAL_PORT_T *port, const MMAL_PARAMETER_HEADER_T *param)
  202. {
  203. MMAL_COMPONENT_T *component = port->component;
  204. unsigned int i;
  205. switch (param->id)
  206. {
  207. case MMAL_PARAMETER_BUFFER_REQUIREMENTS:
  208. {
  209. /* Propagate the requirements to all the ports */
  210. const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *req = (const MMAL_PARAMETER_BUFFER_REQUIREMENTS_T *)param;
  211. uint32_t buffer_num_min = MMAL_MAX(port->buffer_num_min, req->buffer_num_min);
  212. uint32_t buffer_num_recommended = MMAL_MAX(port->buffer_num_recommended, req->buffer_num_recommended);
  213. uint32_t buffer_size_min = MMAL_MAX(port->buffer_size_min, req->buffer_size_min);
  214. uint32_t buffer_size_recommended = MMAL_MAX(port->buffer_size_recommended, req->buffer_size_recommended);
  215. component->input[0]->buffer_num_min = buffer_num_min;
  216. component->input[0]->buffer_num_recommended = buffer_num_recommended;
  217. component->input[0]->buffer_size_min = buffer_size_min;
  218. component->input[0]->buffer_size_recommended = buffer_size_recommended;
  219. for (i = 0; i < component->output_num; i++)
  220. {
  221. component->output[i]->buffer_num_min = buffer_num_min;
  222. component->output[i]->buffer_num_recommended = buffer_num_recommended;
  223. component->output[i]->buffer_size_min = buffer_size_min;
  224. component->output[i]->buffer_size_recommended = buffer_size_recommended;
  225. }
  226. }
  227. return MMAL_SUCCESS;
  228. default:
  229. return MMAL_ENOSYS;
  230. }
  231. }
  232. /** Create an instance of a component */
  233. static MMAL_STATUS_T mmal_component_create_splitter(const char *name, MMAL_COMPONENT_T *component)
  234. {
  235. MMAL_COMPONENT_MODULE_T *module;
  236. MMAL_STATUS_T status = MMAL_ENOMEM;
  237. unsigned int i;
  238. MMAL_PARAM_UNUSED(name);
  239. /* Allocate the context for our module */
  240. component->priv->module = module = vcos_malloc(sizeof(*module), "mmal module");
  241. if (!module)
  242. return MMAL_ENOMEM;
  243. memset(module, 0, sizeof(*module));
  244. component->priv->pf_destroy = splitter_component_destroy;
  245. /* Allocate and initialise all the ports for this component */
  246. component->input = mmal_ports_alloc(component, 1, MMAL_PORT_TYPE_INPUT, sizeof(MMAL_PORT_MODULE_T));
  247. if(!component->input)
  248. goto error;
  249. component->input_num = 1;
  250. component->input[0]->priv->pf_enable = splitter_port_enable;
  251. component->input[0]->priv->pf_disable = splitter_port_disable;
  252. component->input[0]->priv->pf_flush = splitter_port_flush;
  253. component->input[0]->priv->pf_send = splitter_port_send;
  254. component->input[0]->priv->pf_set_format = splitter_port_format_commit;
  255. component->input[0]->priv->pf_parameter_set = splitter_port_parameter_set;
  256. component->input[0]->buffer_num_min = 1;
  257. component->input[0]->buffer_num_recommended = 0;
  258. component->input[0]->priv->module->queue = mmal_queue_create();
  259. if(!component->input[0]->priv->module->queue)
  260. goto error;
  261. component->output = mmal_ports_alloc(component, SPLITTER_OUTPUT_PORTS_NUM,
  262. MMAL_PORT_TYPE_OUTPUT, sizeof(MMAL_PORT_MODULE_T));
  263. if(!component->output)
  264. goto error;
  265. component->output_num = SPLITTER_OUTPUT_PORTS_NUM;
  266. for(i = 0; i < component->output_num; i++)
  267. {
  268. component->output[i]->priv->pf_enable = splitter_port_enable;
  269. component->output[i]->priv->pf_disable = splitter_port_disable;
  270. component->output[i]->priv->pf_flush = splitter_port_flush;
  271. component->output[i]->priv->pf_send = splitter_port_send;
  272. component->output[i]->priv->pf_set_format = splitter_port_format_commit;
  273. component->output[i]->priv->pf_parameter_set = splitter_port_parameter_set;
  274. component->output[i]->buffer_num_min = 1;
  275. component->output[i]->buffer_num_recommended = 0;
  276. component->output[i]->capabilities = MMAL_PORT_CAPABILITY_PASSTHROUGH;
  277. component->output[i]->priv->module->queue = mmal_queue_create();
  278. if(!component->output[i]->priv->module->queue)
  279. goto error;
  280. }
  281. return MMAL_SUCCESS;
  282. error:
  283. splitter_component_destroy(component);
  284. return status;
  285. }
  286. MMAL_CONSTRUCTOR(mmal_register_component_splitter);
  287. void mmal_register_component_splitter(void)
  288. {
  289. mmal_component_supplier_register("splitter", mmal_component_create_splitter);
  290. }