Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSMedia
0007  *
0008  * @brief Media implementation.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2009, 2010 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <string.h>
0037 #include <stdlib.h>
0038 #include <assert.h>
0039 
0040 #include <rtems.h>
0041 #include <rtems/chain.h>
0042 #include <rtems/media.h>
0043 
0044 #define EVENT RTEMS_EVENT_13
0045 
0046 typedef struct {
0047   rtems_chain_node node;
0048   rtems_media_event event;
0049   const char *src;
0050   rtems_media_worker worker;
0051   void *worker_arg;
0052 } message;
0053 
0054 static RTEMS_CHAIN_DEFINE_EMPTY(message_chain);
0055 
0056 static rtems_id server_id = RTEMS_ID_NONE;
0057 
0058 static void media_server(rtems_task_argument arg RTEMS_UNUSED)
0059 {
0060   rtems_status_code sc = RTEMS_SUCCESSFUL;
0061 
0062   while (true) {
0063     message *msg = NULL;
0064 
0065     sc = rtems_chain_get_with_wait(
0066       &message_chain,
0067       EVENT,
0068       RTEMS_NO_TIMEOUT,
0069       (rtems_chain_node **) &msg
0070     );
0071     assert(sc == RTEMS_SUCCESSFUL);
0072     assert(msg != NULL);
0073 
0074     rtems_media_post_event(
0075       msg->event,
0076       msg->src,
0077       NULL,
0078       msg->worker,
0079       msg->worker_arg
0080     );
0081 
0082     free(msg);
0083   }
0084 }
0085 
0086 rtems_status_code rtems_media_server_initialize(
0087   rtems_task_priority priority,
0088   size_t stack_size,
0089   rtems_mode modes,
0090   rtems_attribute attributes
0091 )
0092 {
0093   rtems_status_code sc = RTEMS_SUCCESSFUL;
0094 
0095   if (server_id == RTEMS_ID_NONE) {
0096     sc = rtems_media_initialize();
0097     if (sc != RTEMS_SUCCESSFUL) {
0098       goto error;
0099     }
0100 
0101     sc = rtems_task_create(
0102       rtems_build_name('M', 'D', 'I', 'A'),
0103       priority,
0104       stack_size,
0105       modes,
0106       attributes,
0107       &server_id
0108     );
0109     if (sc != RTEMS_SUCCESSFUL) {
0110       goto error;
0111     }
0112 
0113     sc = rtems_task_start(server_id, media_server, 0);
0114     if (sc != RTEMS_SUCCESSFUL) {
0115       goto error;
0116     }
0117   }
0118   
0119   return RTEMS_SUCCESSFUL;
0120 
0121 error:
0122 
0123   if (server_id != RTEMS_ID_NONE) {
0124     rtems_task_delete(server_id);
0125   }
0126 
0127   return RTEMS_NO_MEMORY;
0128 }
0129 
0130 rtems_status_code rtems_media_server_post_event(
0131   rtems_media_event event,
0132   const char *src,
0133   rtems_media_worker worker,
0134   void *worker_arg
0135 )
0136 {
0137   rtems_status_code sc = RTEMS_SUCCESSFUL;
0138   size_t src_size = strlen(src) + 1;
0139   message *msg = malloc(sizeof(*msg) + src_size);
0140 
0141   if (msg != NULL) {
0142     char *s = (char *) msg + sizeof(*msg);
0143 
0144     memcpy(s, src, src_size);
0145 
0146     msg->event = event;
0147     msg->src = s;
0148     msg->worker = worker;
0149     msg->worker_arg = worker_arg;
0150     rtems_chain_initialize_node(&msg->node);
0151 
0152     sc = rtems_chain_append_with_notification(
0153       &message_chain,
0154       &msg->node,
0155       server_id,
0156       EVENT
0157     );
0158     if (sc != RTEMS_SUCCESSFUL) {
0159       sc = RTEMS_NOT_CONFIGURED;
0160     }
0161   } else {
0162     sc = RTEMS_NO_MEMORY;
0163   }
0164 
0165   return sc;
0166 }