diff options
Diffstat (limited to 'apps/plugins/pdbox/pdbox-net.c')
-rw-r--r-- | apps/plugins/pdbox/pdbox-net.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/pdbox-net.c b/apps/plugins/pdbox/pdbox-net.c new file mode 100644 index 0000000000..5767226de3 --- /dev/null +++ b/apps/plugins/pdbox/pdbox-net.c | |||
@@ -0,0 +1,238 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2009 Wincent Balin | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "plugin.h" | ||
23 | #include "pdbox.h" | ||
24 | |||
25 | /* Datagram pool will contains 16 datagrams. */ | ||
26 | #define MAX_DATAGRAMS 16 | ||
27 | |||
28 | /* Datagram pool. */ | ||
29 | struct datagram datagrams[MAX_DATAGRAMS]; | ||
30 | |||
31 | /* UDP message queues. */ | ||
32 | struct event_queue gui_to_core; | ||
33 | struct event_queue core_to_gui; | ||
34 | |||
35 | /* Initialize net infrastructure. */ | ||
36 | void net_init(void) | ||
37 | { | ||
38 | unsigned int i; | ||
39 | |||
40 | /* Initialize message pool. */ | ||
41 | for(i = 0; i < MAX_DATAGRAMS; i++) | ||
42 | datagrams[i].used = false; | ||
43 | |||
44 | /* Initialize and register message queues. */ | ||
45 | rb->queue_init(&gui_to_core, true); | ||
46 | rb->queue_init(&core_to_gui, true); | ||
47 | } | ||
48 | |||
49 | /* Send datagram. */ | ||
50 | bool send_datagram(struct event_queue* route, | ||
51 | int port, | ||
52 | char* data, | ||
53 | size_t size) | ||
54 | { | ||
55 | unsigned int i; | ||
56 | |||
57 | /* If datagram too long, abort. */ | ||
58 | if(size > MAX_DATAGRAM_SIZE) | ||
59 | return false; | ||
60 | |||
61 | /* Find free datagram buffer. */ | ||
62 | for(i = 0; i < MAX_DATAGRAMS; i++) | ||
63 | if(!datagrams[i].used) | ||
64 | break; | ||
65 | |||
66 | /* If no free buffer found, abort. */ | ||
67 | if(i == MAX_DATAGRAMS) | ||
68 | return false; | ||
69 | |||
70 | /* Copy datagram to the buffer. */ | ||
71 | rb->memcpy(datagrams[i].data, data, size); | ||
72 | datagrams[i].size = size; | ||
73 | |||
74 | /* Mark datagram buffer as used. */ | ||
75 | datagrams[i].used = true; | ||
76 | |||
77 | /* Send event via route. */ | ||
78 | rb->queue_post(route, port, (intptr_t) &datagrams[i]); | ||
79 | |||
80 | /* Everything went ok. */ | ||
81 | return true; | ||
82 | } | ||
83 | |||
84 | /* Receive datagram. */ | ||
85 | bool receive_datagram(struct event_queue* route, | ||
86 | int port, | ||
87 | struct datagram* buffer) | ||
88 | { | ||
89 | struct queue_event event; | ||
90 | |||
91 | /* If route queue empty, abort. */ | ||
92 | if(rb->queue_empty(route)) | ||
93 | return false; | ||
94 | |||
95 | /* Receive event. */ | ||
96 | rb->queue_wait(route, &event); | ||
97 | |||
98 | /* If wrong port, abort. | ||
99 | NOTE: Event is removed from the queue in any case! */ | ||
100 | if(event.id != port) | ||
101 | return false; | ||
102 | |||
103 | /* Copy datagram. */ | ||
104 | rb->memcpy(buffer, (struct datagram*) event.data, sizeof(struct datagram)); | ||
105 | |||
106 | /* Free datagram buffer. */ | ||
107 | ((struct datagram*) event.data)->used = false; | ||
108 | |||
109 | /* Everything went ok. */ | ||
110 | return true; | ||
111 | } | ||
112 | |||
113 | /* Destroy net infrastructure. */ | ||
114 | void net_destroy(void) | ||
115 | { | ||
116 | /* Remove message queues. */ | ||
117 | rb->queue_delete(&gui_to_core); | ||
118 | rb->queue_delete(&core_to_gui); | ||
119 | } | ||
120 | /*************************************************************************** | ||
121 | * __________ __ ___. | ||
122 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
123 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
124 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
125 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
126 | * \/ \/ \/ \/ \/ | ||
127 | * $Id$ | ||
128 | * | ||
129 | * Copyright (C) 2009 Wincent Balin | ||
130 | * | ||
131 | * This program is free software; you can redistribute it and/or | ||
132 | * modify it under the terms of the GNU General Public License | ||
133 | * as published by the Free Software Foundation; either version 2 | ||
134 | * of the License, or (at your option) any later version. | ||
135 | * | ||
136 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
137 | * KIND, either express or implied. | ||
138 | * | ||
139 | ****************************************************************************/ | ||
140 | |||
141 | #include "plugin.h" | ||
142 | #include "pdbox.h" | ||
143 | |||
144 | /* Datagram pool will contains 16 datagrams. */ | ||
145 | #define MAX_DATAGRAMS 16 | ||
146 | |||
147 | /* Datagram pool. */ | ||
148 | struct datagram datagrams[MAX_DATAGRAMS]; | ||
149 | |||
150 | /* UDP message queues. */ | ||
151 | struct event_queue gui_to_core; | ||
152 | struct event_queue core_to_gui; | ||
153 | |||
154 | /* Initialize net infrastructure. */ | ||
155 | void net_init(void) | ||
156 | { | ||
157 | unsigned int i; | ||
158 | |||
159 | /* Initialize message pool. */ | ||
160 | for(i = 0; i < MAX_DATAGRAMS; i++) | ||
161 | datagrams[i].used = false; | ||
162 | |||
163 | /* Initialize and register message queues. */ | ||
164 | rb->queue_init(&gui_to_core, true); | ||
165 | rb->queue_init(&core_to_gui, true); | ||
166 | } | ||
167 | |||
168 | /* Send datagram. */ | ||
169 | bool send_datagram(struct event_queue* route, | ||
170 | int port, | ||
171 | char* data, | ||
172 | size_t size) | ||
173 | { | ||
174 | unsigned int i; | ||
175 | |||
176 | /* If datagram too long, abort. */ | ||
177 | if(size > MAX_DATAGRAM_SIZE) | ||
178 | return false; | ||
179 | |||
180 | /* Find free datagram buffer. */ | ||
181 | for(i = 0; i < MAX_DATAGRAMS; i++) | ||
182 | if(!datagrams[i].used) | ||
183 | break; | ||
184 | |||
185 | /* If no free buffer found, abort. */ | ||
186 | if(i == MAX_DATAGRAMS) | ||
187 | return false; | ||
188 | |||
189 | /* Copy datagram to the buffer. */ | ||
190 | rb->memcpy(datagrams[i].data, data, size); | ||
191 | datagrams[i].size = size; | ||
192 | |||
193 | /* Mark datagram buffer as used. */ | ||
194 | datagrams[i].used = true; | ||
195 | |||
196 | /* Send event via route. */ | ||
197 | rb->queue_post(route, port, (intptr_t) &datagrams[i]); | ||
198 | |||
199 | /* Everything went ok. */ | ||
200 | return true; | ||
201 | } | ||
202 | |||
203 | /* Receive datagram. */ | ||
204 | bool receive_datagram(struct event_queue* route, | ||
205 | int port, | ||
206 | struct datagram* buffer) | ||
207 | { | ||
208 | struct queue_event event; | ||
209 | |||
210 | /* If route queue empty, abort. */ | ||
211 | if(rb->queue_empty(route)) | ||
212 | return false; | ||
213 | |||
214 | /* Receive event. */ | ||
215 | rb->queue_wait(route, &event); | ||
216 | |||
217 | /* If wrong port, abort. | ||
218 | NOTE: Event is removed from the queue in any case! */ | ||
219 | if(event.id != port) | ||
220 | return false; | ||
221 | |||
222 | /* Copy datagram. */ | ||
223 | rb->memcpy(buffer, (struct datagram*) event.data, sizeof(struct datagram)); | ||
224 | |||
225 | /* Free datagram buffer. */ | ||
226 | ((struct datagram*) event.data)->used = false; | ||
227 | |||
228 | /* Everything went ok. */ | ||
229 | return true; | ||
230 | } | ||
231 | |||
232 | /* Destroy net infrastructure. */ | ||
233 | void net_destroy(void) | ||
234 | { | ||
235 | /* Remove message queues. */ | ||
236 | rb->queue_delete(&gui_to_core); | ||
237 | rb->queue_delete(&core_to_gui); | ||
238 | } | ||