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

Last change on this file was c69d6aa, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 12, 2016 at 2:49:59 PM

libblock: Fix for RTEMS_DEBUG

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSMedia
5 *
6 * @brief Media implementation.
7 */
8
9/*
10 * Copyright (c) 2009, 2010 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#include <string.h>
24#include <stdlib.h>
25#include <assert.h>
26
27#include <rtems.h>
28#include <rtems/chain.h>
29#include <rtems/media.h>
30
31#define EVENT RTEMS_EVENT_13
32
33typedef struct {
34  rtems_chain_node node;
35  rtems_media_event event;
36  const char *src;
37  rtems_media_worker worker;
38  void *worker_arg;
39} message;
40
41static RTEMS_CHAIN_DEFINE_EMPTY(message_chain);
42
43static rtems_id server_id = RTEMS_ID_NONE;
44
45static void media_server(rtems_task_argument arg RTEMS_UNUSED)
46{
47  rtems_status_code sc = RTEMS_SUCCESSFUL;
48
49  while (true) {
50    message *msg = NULL;
51
52    sc = rtems_chain_get_with_wait(
53      &message_chain,
54      EVENT,
55      RTEMS_NO_TIMEOUT,
56      (rtems_chain_node **) &msg
57    );
58    assert(sc == RTEMS_SUCCESSFUL);
59    assert(msg != NULL);
60
61    rtems_media_post_event(
62      msg->event,
63      msg->src,
64      NULL,
65      msg->worker,
66      msg->worker_arg
67    );
68
69    free(msg);
70  }
71}
72
73rtems_status_code rtems_media_server_initialize(
74  rtems_task_priority priority,
75  size_t stack_size,
76  rtems_mode modes,
77  rtems_attribute attributes
78)
79{
80  rtems_status_code sc = RTEMS_SUCCESSFUL;
81
82  if (server_id == RTEMS_ID_NONE) {
83    sc = rtems_media_initialize();
84    if (sc != RTEMS_SUCCESSFUL) {
85      goto error;
86    }
87
88    sc = rtems_task_create(
89      rtems_build_name('M', 'D', 'I', 'A'),
90      priority,
91      stack_size,
92      modes,
93      attributes,
94      &server_id
95    );
96    if (sc != RTEMS_SUCCESSFUL) {
97      goto error;
98    }
99
100    sc = rtems_task_start(server_id, media_server, 0);
101    if (sc != RTEMS_SUCCESSFUL) {
102      goto error;
103    }
104  }
105 
106  return RTEMS_SUCCESSFUL;
107
108error:
109
110  if (server_id != RTEMS_ID_NONE) {
111    rtems_task_delete(server_id);
112  }
113
114  return RTEMS_NO_MEMORY;
115}
116
117rtems_status_code rtems_media_server_post_event(
118  rtems_media_event event,
119  const char *src,
120  rtems_media_worker worker,
121  void *worker_arg
122)
123{
124  rtems_status_code sc = RTEMS_SUCCESSFUL;
125  size_t src_size = strlen(src) + 1;
126  message *msg = malloc(sizeof(*msg) + src_size);
127
128  if (msg != NULL) {
129    char *s = (char *) msg + sizeof(*msg);
130
131    memcpy(s, src, src_size);
132
133    msg->event = event;
134    msg->src = s;
135    msg->worker = worker;
136    msg->worker_arg = worker_arg;
137    rtems_chain_initialize_node(&msg->node);
138
139    sc = rtems_chain_append_with_notification(
140      &message_chain,
141      &msg->node,
142      server_id,
143      EVENT
144    );
145    if (sc != RTEMS_SUCCESSFUL) {
146      sc = RTEMS_NOT_CONFIGURED;
147    }
148  } else {
149    sc = RTEMS_NO_MEMORY;
150  }
151
152  return sc;
153}
Note: See TracBrowser for help on using the repository browser.