-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_streams.h
228 lines (154 loc) · 4.98 KB
/
kernel_streams.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef __KERNEL_STREAMS_H
#define __KERNEL_STREAMS_H
#include "tinyos.h"
#include "kernel_dev.h"
#define BUF_SIZE 8192 // Size for our buffer
#define MAX_PORT 1023
/**
@file kernel_streams.h
@brief Support for I/O streams.
@defgroup streams Streams.
@ingroup kernel
@brief Support for I/O streams.
The stream model of tinyos3 is similar to the Unix model.
Streams are objects that are shared between processes.
Streams are accessed by file IDs (similar to file descriptors
in Unix).
The streams of each process are held in the file table of the
PCB of the process. The system calls generally use the API
of this file to access FCBs: @ref get_fcb, @ref FCB_reserve
and @ref FCB_unreserve.
Streams are connected to devices by virtue of a @c file_operations
object, which provides pointers to device-specific implementations
for read, write and close.
@{
*/
/** @brief The file control block.
A file control block provides a uniform object to the
system calls, and contains pointers to device-specific
functions.
*/
typedef int Socket_t;
typedef struct pipe_control_block
{
char buffer[BUF_SIZE];
uint w,r ;
FCB *reader;
FCB *writer;
CondVar In_Cv, Out_Cv ; //Was empty and full at lectures
} PIPE_CB;
typedef enum {
UNBOUND, //neutral type
LISTENER,
PEER
} Socket_type;
typedef struct unbound_control_block
{
rlnode un_queue;
}UNBOUND_CB;
typedef struct peer_control_block
{
Socket_t peer;
PIPE_CB* pipe;
//PICB* receiver;
}PEER_CB;
typedef struct listener_control_block
{
rlnode queue;
CondVar req;//pending requests
int flag; //FLAG FOR CONNECTION ESTABLISHMENT
}LCB;
typedef struct file_control_block
{
uint refcount; /**< @brief Reference counter. */
void* streamobj; /**< @brief The stream object (e.g., a device) */
file_ops* streamfunc; /**< @brief The stream implementation methods */
rlnode freelist_node; /**< @brief Intrusive list node */
} FCB;
typedef struct socket_control_block
{
rlnode socket_node;
uint refcount;
FCB* sfcb;
Socket_type type;
port_t port;
/*contains all the data for listeners unbound and peers control block*/
union {
LCB* lcb;
UNBOUND_CB* ucb;
PEER_CB* peercb;
};
}SCB ;
SCB* PORT_MAP[MAX_PORT+1];
/*
---------------------------------
PIPE CONTROL BLOCK
COMMENT: Change names of CV to
costumize buffer as a ring eazily.
---------------------------------
*/
// Usefull funcs
int Initialize_Pipe();
int pipe_write();
char get_char();
void put_char(char ch, PIPE_CB* pipe);
int pipe_read();
int pipe_writer_close();
int pipe_reader_close();
/**
@brief Initialization for files and streams.
This function is called at kernel startup.
*/
void initialize_files();
/**
@brief Increase the reference count of an fcb
@param fcb the fcb whose reference count will be increased
*/
void FCB_incref(FCB* fcb);
/**
@brief Decrease the reference count of the fcb.
If the reference count drops to 0, release the FCB, calling the
Close method and returning its return value.
If the reference count is still >0, return 0.
@param fcb the fcb whose reference count is decreased
@returns if the reference count is still >0, return 0, else return the value returned by the
`Close()` operation
*/
int FCB_decref(FCB* fcb);
/** @brief Acquire a number of FCBs and corresponding fids.
Given an array of fids and an array of pointers to FCBs of
size @ num, this function will check is available resources
in the current process PCB and FCB are available, and if so
it will fill the two arrays with the appropriate values.
If not, the state is unchanged (but the array contents
may have been overwritten).
If these resources are not needed, the operation can be
reversed by calling @ref FCB_unreserve.
@param num the number of resources to reserve.
@param fid array of size at least `num` of `Fid_t`.
@param fcb array of size at least `num` of `FCB*`.
@returns 1 for success and 0 for failure.
*/
int FCB_reserve(size_t num, Fid_t *fid, FCB** fcb);
/** @brief Release a number of FCBs and corresponding fids.
Given an array of fids of size @ num, this function will
return the fids to the free pool of the current process and
release the corresponding FCBs.
This is the opposite of operation @ref FCB_reserve.
Note that this is very different from closing open fids.
No I/O operation is performed by this function.
This function does not check its arguments for correctness.
Use only with arrays filled by a call to @ref FCB_reserve.
@param num the number of resources to unreserve.
@param fid array of size at least `num` of `Fid_t`.
@param fcb array of size at least `num` of `FCB*`.
*/
void FCB_unreserve(size_t num, Fid_t *fid, FCB** fcb);
/** @brief Translate an fid to an FCB.
This routine will return NULL if the fid is not legal.
@param fid the file ID to translate to a pointer to FCB
@returns a pointer to the corresponding FCB, or NULL.
*/
FCB* get_fcb(Fid_t fid);
/** @} */
#endif