summaryrefslogtreecommitdiffstats
path: root/cpukit/libblock/src/media-server.c
blob: 1f20f38cfda4b17bd9306a09a5f17724edba1a65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSMedia
 *
 * @brief Media implementation.
 */

/*
 * Copyright (C) 2009, 2010 embedded brains GmbH & Co. KG
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include <rtems.h>
#include <rtems/chain.h>
#include <rtems/media.h>

#define EVENT RTEMS_EVENT_13

typedef struct {
  rtems_chain_node node;
  rtems_media_event event;
  const char *src;
  rtems_media_worker worker;
  void *worker_arg;
} message;

static RTEMS_CHAIN_DEFINE_EMPTY(message_chain);

static rtems_id server_id = RTEMS_ID_NONE;

static void media_server(rtems_task_argument arg RTEMS_UNUSED)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  while (true) {
    message *msg = NULL;

    sc = rtems_chain_get_with_wait(
      &message_chain,
      EVENT,
      RTEMS_NO_TIMEOUT,
      (rtems_chain_node **) &msg
    );
    assert(sc == RTEMS_SUCCESSFUL);
    assert(msg != NULL);

    rtems_media_post_event(
      msg->event,
      msg->src,
      NULL,
      msg->worker,
      msg->worker_arg
    );

    free(msg);
  }
}

rtems_status_code rtems_media_server_initialize(
  rtems_task_priority priority,
  size_t stack_size,
  rtems_mode modes,
  rtems_attribute attributes
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (server_id == RTEMS_ID_NONE) {
    sc = rtems_media_initialize();
    if (sc != RTEMS_SUCCESSFUL) {
      goto error;
    }

    sc = rtems_task_create(
      rtems_build_name('M', 'D', 'I', 'A'),
      priority,
      stack_size,
      modes,
      attributes,
      &server_id
    );
    if (sc != RTEMS_SUCCESSFUL) {
      goto error;
    }

    sc = rtems_task_start(server_id, media_server, 0);
    if (sc != RTEMS_SUCCESSFUL) {
      goto error;
    }
  }
  
  return RTEMS_SUCCESSFUL;

error:

  if (server_id != RTEMS_ID_NONE) {
    rtems_task_delete(server_id);
  }

  return RTEMS_NO_MEMORY;
}

rtems_status_code rtems_media_server_post_event(
  rtems_media_event event,
  const char *src,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  size_t src_size = strlen(src) + 1;
  message *msg = malloc(sizeof(*msg) + src_size);

  if (msg != NULL) {
    char *s = (char *) msg + sizeof(*msg);

    memcpy(s, src, src_size);

    msg->event = event;
    msg->src = s;
    msg->worker = worker;
    msg->worker_arg = worker_arg;
    rtems_chain_initialize_node(&msg->node);

    sc = rtems_chain_append_with_notification(
      &message_chain,
      &msg->node,
      server_id,
      EVENT
    );
    if (sc != RTEMS_SUCCESSFUL) {
      sc = RTEMS_NOT_CONFIGURED;
    }
  } else {
    sc = RTEMS_NO_MEMORY;
  }

  return sc;
}