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

4.11
Last change on this file since d91e3a7 was d91e3a7, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 13, 2010 at 12:40:16 PM

2010-08-13 Sebastian Huber <sebastian.huber@…>

  • libblock/include/rtems/media.h, libblock/src/media-desc.c, libblock/src/media-dev-ident.c, libblock/src/media-path.c, libblock/src/media-server.c, libblock/src/media.c: New files.
  • Makefile.am, preinstall.am, libblock/Makefile.am: Reflect changes above.
  • Property mode set to 100644
File size: 2.9 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.com/license/LICENSE.
21 */
22
23#include <string.h>
24#include <stdio.h>
25#include <stdlib.h>
26
27#include <rtems.h>
28#include <rtems/chain.h>
29
30#include <rtems/media.h>
31
32typedef struct {
33  rtems_media_event event;
34  char *src;
35  rtems_media_worker worker;
36  void *arg;
37} media_server_event;
38
39static rtems_id message_queue_id = RTEMS_ID_NONE;
40
41static void rtems_media_server(rtems_task_argument arg __attribute__((unused)))
42{
43  rtems_status_code sc = RTEMS_SUCCESSFUL;
44
45  while (true) {
46    media_server_event msg;
47    size_t msg_size = sizeof(msg);
48
49    sc = rtems_message_queue_receive(
50      message_queue_id,
51      &msg,
52      &msg_size,
53      RTEMS_WAIT,
54      RTEMS_NO_TIMEOUT
55    );
56    if (sc != RTEMS_SUCCESSFUL) {
57      break;
58    }
59
60    rtems_media_post_event(msg.event, msg.src, NULL, msg.worker, msg.arg);
61
62    free(msg.src);
63  }
64
65  rtems_task_delete(RTEMS_SELF);
66}
67
68rtems_status_code rtems_media_server_initialize(
69  rtems_task_priority priority,
70  size_t stack_size,
71  rtems_mode modes,
72  rtems_attribute attributes,
73  uint32_t message_count
74)
75{
76  rtems_status_code sc = RTEMS_SUCCESSFUL;
77  rtems_id task_id = RTEMS_ID_NONE;
78
79  if (message_queue_id == RTEMS_ID_NONE) {
80    sc = rtems_media_initialize();
81    if (sc != RTEMS_SUCCESSFUL) {
82      goto error;
83    }
84
85    sc = rtems_message_queue_create(
86      rtems_build_name('M', 'D', 'I', 'A'),
87      message_count,
88      sizeof(media_server_event),
89      RTEMS_DEFAULT_ATTRIBUTES,
90      &message_queue_id
91    );
92    if (sc != RTEMS_SUCCESSFUL) {
93      goto error;
94    }
95
96    sc = rtems_task_create(
97      rtems_build_name('M', 'D', 'I', 'A'),
98      priority,
99      stack_size,
100      modes,
101      attributes,
102      &task_id
103    );
104    if (sc != RTEMS_SUCCESSFUL) {
105      goto error;
106    }
107
108    sc = rtems_task_start(task_id, rtems_media_server, 0);
109    if (sc != RTEMS_SUCCESSFUL) {
110      goto error;
111    }
112  }
113 
114  return RTEMS_SUCCESSFUL;
115
116error:
117
118  if (task_id != RTEMS_ID_NONE) {
119    rtems_task_delete(task_id);
120  }
121
122  rtems_message_queue_delete(message_queue_id);
123
124  return RTEMS_NO_MEMORY;
125}
126
127rtems_status_code rtems_media_server_post_event(
128  rtems_media_event event,
129  const char *src,
130  rtems_media_worker worker,
131  void *arg
132)
133{
134  rtems_status_code sc = RTEMS_SUCCESSFUL;
135  media_server_event msg = {
136    .event = event,
137    .src = strdup(src),
138    .worker = worker,
139    .arg = arg
140  };
141
142  if (msg.src == NULL) {
143    return RTEMS_NO_MEMORY;
144  }
145
146  sc = rtems_message_queue_send(
147    message_queue_id,
148    &msg,
149    sizeof(msg)
150  );
151  if (sc != RTEMS_SUCCESSFUL) {
152    free(msg.src);
153
154    return RTEMS_UNSATISFIED;
155  }
156
157  return RTEMS_SUCCESSFUL;
158}
Note: See TracBrowser for help on using the repository browser.