Patch from Wincent Balin: Remove some duplicate code from the PDa plugin
[kugel-rb.git] / apps / plugins / pdbox / pdbox-net.c
blob83d9bd3b7d7d40d64be6f54d985dd1de6a9d0039
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Wincent Balin
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "pdbox.h"
25 /* Datagram pool will contains 16 datagrams. */
26 #define MAX_DATAGRAMS 16
28 /* Datagram pool. */
29 struct datagram datagrams[MAX_DATAGRAMS];
31 /* UDP message queues. */
32 struct event_queue gui_to_core;
33 struct event_queue core_to_gui;
35 /* Initialize net infrastructure. */
36 void net_init(void)
38 unsigned int i;
40 /* Initialize message pool. */
41 for(i = 0; i < MAX_DATAGRAMS; i++)
42 datagrams[i].used = false;
44 /* Initialize and register message queues. */
45 rb->queue_init(&gui_to_core, true);
46 rb->queue_init(&core_to_gui, true);
49 /* Send datagram. */
50 bool send_datagram(struct event_queue* route,
51 int port,
52 char* data,
53 size_t size)
55 unsigned int i;
57 /* If datagram too long, abort. */
58 if(size > MAX_DATAGRAM_SIZE)
59 return false;
61 /* Find free datagram buffer. */
62 for(i = 0; i < MAX_DATAGRAMS; i++)
63 if(!datagrams[i].used)
64 break;
66 /* If no free buffer found, abort. */
67 if(i == MAX_DATAGRAMS)
68 return false;
70 /* Copy datagram to the buffer. */
71 rb->memcpy(datagrams[i].data, data, size);
72 datagrams[i].size = size;
74 /* Mark datagram buffer as used. */
75 datagrams[i].used = true;
77 /* Send event via route. */
78 rb->queue_post(route, port, (intptr_t) &datagrams[i]);
80 /* Everything went ok. */
81 return true;
84 /* Receive datagram. */
85 bool receive_datagram(struct event_queue* route,
86 int port,
87 struct datagram* buffer)
89 struct queue_event event;
91 /* If route queue empty, abort. */
92 if(rb->queue_empty(route))
93 return false;
95 /* Receive event. */
96 rb->queue_wait(route, &event);
98 /* If wrong port, abort.
99 NOTE: Event is removed from the queue in any case! */
100 if(event.id != port)
101 return false;
103 /* Copy datagram. */
104 rb->memcpy(buffer, (struct datagram*) event.data, sizeof(struct datagram));
106 /* Free datagram buffer. */
107 ((struct datagram*) event.data)->used = false;
109 /* Everything went ok. */
110 return true;
113 /* Destroy net infrastructure. */
114 void net_destroy(void)
116 /* Remove message queues. */
117 rb->queue_delete(&gui_to_core);
118 rb->queue_delete(&core_to_gui);