source: rtems/cpukit/libblock/src/media-server.c

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSMedia
7 *
8 * @brief Media implementation.
9 */
10
11/*
12 * Copyright (C) 2009, 2010 embedded brains GmbH & Co. KG
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <string.h>
37#include <stdlib.h>
38#include <assert.h>
39
40#include <rtems.h>
41#include <rtems/chain.h>
42#include <rtems/media.h>
43
44#define EVENT RTEMS_EVENT_13
45
46typedef struct {
47  rtems_chain_node node;
48  rtems_media_event event;
49  const char *src;
50  rtems_media_worker worker;
51  void *worker_arg;
52} message;
53
54static RTEMS_CHAIN_DEFINE_EMPTY(message_chain);
55
56static rtems_id server_id = RTEMS_ID_NONE;
57
58static void media_server(rtems_task_argument arg RTEMS_UNUSED)
59{
60  rtems_status_code sc = RTEMS_SUCCESSFUL;
61
62  while (true) {
63    message *msg = NULL;
64
65    sc = rtems_chain_get_with_wait(
66      &message_chain,
67      EVENT,
68      RTEMS_NO_TIMEOUT,
69      (rtems_chain_node **) &msg
70    );
71    assert(sc == RTEMS_SUCCESSFUL);
72    assert(msg != NULL);
73
74    rtems_media_post_event(
75      msg->event,
76      msg->src,
77      NULL,
78      msg->worker,
79      msg->worker_arg
80    );
81
82    free(msg);
83  }
84}
85
86rtems_status_code rtems_media_server_initialize(
87  rtems_task_priority priority,
88  size_t stack_size,
89  rtems_mode modes,
90  rtems_attribute attributes
91)
92{
93  rtems_status_code sc = RTEMS_SUCCESSFUL;
94
95  if (server_id == RTEMS_ID_NONE) {
96    sc = rtems_media_initialize();
97    if (sc != RTEMS_SUCCESSFUL) {
98      goto error;
99    }
100
101    sc = rtems_task_create(
102      rtems_build_name('M', 'D', 'I', 'A'),
103      priority,
104      stack_size,
105      modes,
106      attributes,
107      &server_id
108    );
109    if (sc != RTEMS_SUCCESSFUL) {
110      goto error;
111    }
112
113    sc = rtems_task_start(server_id, media_server, 0);
114    if (sc != RTEMS_SUCCESSFUL) {
115      goto error;
116    }
117  }
118 
119  return RTEMS_SUCCESSFUL;
120
121error:
122
123  if (server_id != RTEMS_ID_NONE) {
124    rtems_task_delete(server_id);
125  }
126
127  return RTEMS_NO_MEMORY;
128}
129
130rtems_status_code rtems_media_server_post_event(
131  rtems_media_event event,
132  const char *src,
133  rtems_media_worker worker,
134  void *worker_arg
135)
136{
137  rtems_status_code sc = RTEMS_SUCCESSFUL;
138  size_t src_size = strlen(src) + 1;
139  message *msg = malloc(sizeof(*msg) + src_size);
140
141  if (msg != NULL) {
142    char *s = (char *) msg + sizeof(*msg);
143
144    memcpy(s, src, src_size);
145
146    msg->event = event;
147    msg->src = s;
148    msg->worker = worker;
149    msg->worker_arg = worker_arg;
150    rtems_chain_initialize_node(&msg->node);
151
152    sc = rtems_chain_append_with_notification(
153      &message_chain,
154      &msg->node,
155      server_id,
156      EVENT
157    );
158    if (sc != RTEMS_SUCCESSFUL) {
159      sc = RTEMS_NOT_CONFIGURED;
160    }
161  } else {
162    sc = RTEMS_NO_MEMORY;
163  }
164
165  return sc;
166}
Note: See TracBrowser for help on using the repository browser.