1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 /* Datagram pool will contains 16 datagrams. */
26 #define MAX_DATAGRAMS 16
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. */
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);
50 bool send_datagram(struct event_queue
* route
,
57 /* If datagram too long, abort. */
58 if(size
> MAX_DATAGRAM_SIZE
)
61 /* Find free datagram buffer. */
62 for(i
= 0; i
< MAX_DATAGRAMS
; i
++)
63 if(!datagrams
[i
].used
)
66 /* If no free buffer found, abort. */
67 if(i
== MAX_DATAGRAMS
)
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. */
84 /* Receive datagram. */
85 bool receive_datagram(struct event_queue
* route
,
87 struct datagram
* buffer
)
89 struct queue_event event
;
91 /* If route queue empty, abort. */
92 if(rb
->queue_empty(route
))
96 rb
->queue_wait(route
, &event
);
98 /* If wrong port, abort.
99 NOTE: Event is removed from the queue in any case! */
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. */
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
);