BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file StreamPassInterface.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Interface allowing a stream sender to pass stream data to a stream receiver.
32 *
33 * Note that this interface behaves exactly the same and has the same code as
34 * {@link StreamRecvInterface} if names and its external semantics are disregarded.
35 * If you modify this file, you should probably modify {@link StreamRecvInterface}
36 * too.
37 */
38  
39 #ifndef BADVPN_FLOW_STREAMPASSINTERFACE_H
40 #define BADVPN_FLOW_STREAMPASSINTERFACE_H
41  
42 #include <stdint.h>
43 #include <stddef.h>
44  
45 #include <misc/debug.h>
46 #include <base/DebugObject.h>
47 #include <base/BPending.h>
48  
49 #define SPI_STATE_NONE 1
50 #define SPI_STATE_OPERATION_PENDING 2
51 #define SPI_STATE_BUSY 3
52 #define SPI_STATE_DONE_PENDING 4
53  
54 typedef void (*StreamPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
55  
56 typedef void (*StreamPassInterface_handler_done) (void *user, int data_len);
57  
58 typedef struct {
59 // provider data
60 StreamPassInterface_handler_send handler_operation;
61 void *user_provider;
62  
63 // user data
64 StreamPassInterface_handler_done handler_done;
65 void *user_user;
66  
67 // operation job
68 BPending job_operation;
69 uint8_t *job_operation_data;
70 int job_operation_len;
71  
72 // done job
73 BPending job_done;
74 int job_done_len;
75  
76 // state
77 int state;
78  
79 DebugObject d_obj;
80 } StreamPassInterface;
81  
82 static void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg);
83  
84 static void StreamPassInterface_Free (StreamPassInterface *i);
85  
86 static void StreamPassInterface_Done (StreamPassInterface *i, int data_len);
87  
88 static void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user);
89  
90 static void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len);
91  
92 void _StreamPassInterface_job_operation (StreamPassInterface *i);
93 void _StreamPassInterface_job_done (StreamPassInterface *i);
94  
95 void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
96 {
97 // init arguments
98 i->handler_operation = handler_operation;
99 i->user_provider = user;
100  
101 // set no user
102 i->handler_done = NULL;
103  
104 // init jobs
105 BPending_Init(&i->job_operation, pg, (BPending_handler)_StreamPassInterface_job_operation, i);
106 BPending_Init(&i->job_done, pg, (BPending_handler)_StreamPassInterface_job_done, i);
107  
108 // set state
109 i->state = SPI_STATE_NONE;
110  
111 DebugObject_Init(&i->d_obj);
112 }
113  
114 void StreamPassInterface_Free (StreamPassInterface *i)
115 {
116 DebugObject_Free(&i->d_obj);
117  
118 // free jobs
119 BPending_Free(&i->job_done);
120 BPending_Free(&i->job_operation);
121 }
122  
123 void StreamPassInterface_Done (StreamPassInterface *i, int data_len)
124 {
125 ASSERT(i->state == SPI_STATE_BUSY)
126 ASSERT(data_len > 0)
127 ASSERT(data_len <= i->job_operation_len)
128 DebugObject_Access(&i->d_obj);
129  
130 // schedule done
131 i->job_done_len = data_len;
132 BPending_Set(&i->job_done);
133  
134 // set state
135 i->state = SPI_STATE_DONE_PENDING;
136 }
137  
138 void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user)
139 {
140 ASSERT(handler_done)
141 ASSERT(!i->handler_done)
142 DebugObject_Access(&i->d_obj);
143  
144 i->handler_done = handler_done;
145 i->user_user = user;
146 }
147  
148 void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len)
149 {
150 ASSERT(data_len > 0)
151 ASSERT(data)
152 ASSERT(i->state == SPI_STATE_NONE)
153 ASSERT(i->handler_done)
154 DebugObject_Access(&i->d_obj);
155  
156 // schedule operation
157 i->job_operation_data = data;
158 i->job_operation_len = data_len;
159 BPending_Set(&i->job_operation);
160  
161 // set state
162 i->state = SPI_STATE_OPERATION_PENDING;
163 }
164  
165 #endif