VSF Documented
vsf_simple_stream.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
33#ifndef __VSF_SIMPLE_STREAM_H__
34#define __VSF_SIMPLE_STREAM_H__
35
36/*============================ INCLUDES ======================================*/
37
40
41#if VSF_USE_SIMPLE_STREAM == ENABLED
42
43#if defined(__VSF_SIMPLE_STREAM_CLASS_IMPLEMENT)
44# define __VSF_CLASS_IMPLEMENT__
45#elif defined(__VSF_SIMPLE_STREAM_CLASS_INHERIT__)
46# define __VSF_CLASS_INHERIT__
47#endif
48
49#include "utilities/ooc_class.h"
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/*============================ MACROS ========================================*/
56
66#ifndef VSF_STREAM_CFG_TICKTOCK
67# define VSF_STREAM_CFG_TICKTOCK ENABLED
68#endif
69
79#ifndef VSF_STREAM_CFG_THRESHOLD
80# define VSF_STREAM_CFG_THRESHOLD ENABLED
81#endif
82
83// stream adapter
84
85#define __VSF_STREAM_ADAPTER_INIT(__STREAM_TX, __STREAM_RX, __THRESHOLD_TX, __THRESHOLD_RX)\
86 .stream_tx = (vsf_stream_t *)(__STREAM_TX), \
87 .stream_rx = (vsf_stream_t *)(__STREAM_RX), \
88 .threshold_tx = (__THRESHOLD_TX), \
89 .threshold_rx = (__THRESHOLD_RX),
90#define VSF_STREAM_ADAPTER_INIT0(__STREAM_TX, __STREAM_RX) \
91 __VSF_STREAM_ADAPTER_INIT((__STREAM_TX), (__STREAM_RX), 0, 0)
92#define VSF_STREAM_ADAPTER_INIT1(__STREAM_TX, __STREAM_RX, __THRESHOLD_TX) \
93 __VSF_STREAM_ADAPTER_INIT((__STREAM_TX), (__STREAM_RX), (__THRESHOLD_TX), 0)
94#define VSF_STREAM_ADAPTER_INIT2(__STREAM_TX, __STREAM_RX, __THRESHOLD_TX, __THRESHOLD_RX)\
95 __VSF_STREAM_ADAPTER_INIT((__STREAM_TX), (__STREAM_RX), (__THRESHOLD_TX), (__THRESHOLD_RX))
96// prototype: VSF_STREAM_ADAPTER_INIT(__STREAM_TX, __STREAM_RX, __THRESHOLD_TX = 0, __THRESHOLD_RX = 0)
97#define VSF_STREAM_ADAPTER_INIT(__STREAM_TX, __STREAM_RX, ...) \
98 __PLOOC_EVAL(VSF_STREAM_ADAPTER_INIT, __VA_ARGS__)((__STREAM_TX), (__STREAM_RX), ##__VA_ARGS__)
99
100#define __declare_stream_adapter(__name) \
101 vsf_dcl_class(vsf_stream_adapter_t)
102
103#define declare_stream_adapter(__name) \
104 __declare_stream_adapter(__name)
105#define dcl_stream_adapter(__name) \
106 declare_stream_adapter(__name)
107
108#define __describe_stream_adapter(__name, __stream_tx, __stream_rx, __threshold_tx, __threshold_rx)\
109 vsf_stream_adapter_t __name = { \
110 VSF_STREAM_ADAPTER_INIT((__stream_tx), (__stream_rx), (__threshold_tx), (__threshold_rx))\
111 };
112
113#define describe_stream_adapter0(__name, __stream_tx, __stream_rx) \
114 __describe_stream_adapter(__name, (__stream_tx), (__stream_rx), 0, 0)
115#define describe_stream_adapter1(__name, __stream_tx, __stream_rx, __threshold_tx)\
116 __describe_stream_adapter(__name, (__stream_tx), (__stream_rx), (__threshold_tx), 0)
117#define describe_stream_adapter2(__name, __stream_tx, __stream_rx, __threshold_tx, __threshold_rx) \
118 __describe_stream_adapter(__name, (__stream_tx), (__stream_rx), (__threshold_tx), __threshold_rx)
119// prototype: describe_stream_adapter(__name, __stream_tx, __stream_rx, __threshold_tx = 0, __threshold_rx = 0)
120#define describe_stream_adapter(__name, __stream_tx, __stream_rx, ...) \
121 __PLOOC_EVAL(describe_stream_adapter, __VA_ARGS__)(__name, (__stream_tx), (__stream_rx), ##__VA_ARGS__)
122
123/*============================ MACROFIED FUNCTIONS ===========================*/
124
125// VSF_STREAM_XXX in upper case is compatible with all stream classes
126#define VSF_STREAM_INIT(__stream) vsf_stream_init((vsf_stream_t *)(__stream))
127#define VSF_STREAM_FINI(__stream) vsf_stream_fini((vsf_stream_t *)(__stream))
128#define VSF_STREAM_WRITE(__stream, __buf, __size) vsf_stream_write((vsf_stream_t *)(__stream), (__buf), (__size))
129#define VSF_STREAM_READ(__stream, __buf, __size) vsf_stream_read((vsf_stream_t *)(__stream), (__buf), (__size))
130#define VSF_STREAM_GET_BUFF_SIZE(__stream) vsf_stream_get_buff_size((vsf_stream_t *)(__stream))
131#define VSF_STREAM_GET_DATA_SIZE(__stream) vsf_stream_get_data_size((vsf_stream_t *)(__stream))
132#define VSF_STREAM_GET_FREE_SIZE(__stream) vsf_stream_get_free_size((vsf_stream_t *)(__stream))
133#define VSF_STREAM_GET_WBUF(__stream, p) vsf_stream_get_wbuf((vsf_stream_t *)(__stream), (p))
134#define VSF_STREAM_GET_RBUF(__stream, p) vsf_stream_get_rbuf((vsf_stream_t *)(__stream), (p))
135#define VSF_STREAM_CONNECT_RX(__stream) vsf_stream_connect_rx((vsf_stream_t *)(__stream))
136#define VSF_STREAM_CONNECT_TX(__stream) vsf_stream_connect_tx((vsf_stream_t *)(__stream))
137#define VSF_STREAM_DISCONNECT_RX(__stream) vsf_stream_disconnect_rx((vsf_stream_t *)(__stream))
138#define VSF_STREAM_DISCONNECT_TX(__stream) vsf_stream_disconnect_tx((vsf_stream_t *)(__stream))
139#define VSF_STREAM_IS_RX_CONNECTED(__stream) vsf_stream_is_rx_connected((vsf_stream_t *)(__stream))
140#define VSF_STREAM_IS_TX_CONNECTED(__stream) vsf_stream_is_tx_connected((vsf_stream_t *)(__stream))
141#if VSF_STREAM_CFG_THRESHOLD == ENABLED
142// set threshold CAN ONLY be called after stream_init
143#define VSF_STREAM_SET_RX_THRESHOLD(__stream, __thres) vsf_stream_set_rx_threshold((vsf_stream_t *)(__stream), (__thres))
144#define VSF_STREAM_SET_TX_THRESHOLD(__stream, __thres) vsf_stream_set_tx_threshold((vsf_stream_t *)(__stream), (__thres))
145#endif
146
147/*============================ TYPES =========================================*/
148
152
153
160typedef enum vsf_stream_evt_t {
168
177 protected_member(
187 void (*init)(vsf_stream_t *stream);
188 void (*fini)(vsf_stream_t *stream);
189
202
215 uint_fast32_t (*get_buff_length)(vsf_stream_t *stream);
216 uint_fast32_t (*get_data_length)(vsf_stream_t *stream);
217 uint_fast32_t (*get_avail_length)(vsf_stream_t *stream);
218 // get consequent buffer for read/write
219 uint_fast32_t (*get_wbuf)(vsf_stream_t *stream, uint8_t **ptr);
220 uint_fast32_t (*get_rbuf)(vsf_stream_t *stream, uint8_t **ptr);
222};
223
232
233 public_member(
234 void *param;
235 void (*evthandler)(vsf_stream_t *stream, void *param, vsf_stream_evt_t evt);
237
238 private_member(
240 uint32_t threshold;
241#endif
242 bool ready;
243 bool data_notified;
244 )
245};
246
255
256 public_member(
257 vsf_stream_op_t const *op;
258#if VSF_STREAM_CFG_TICKTOCK == ENABLED
259 bool is_ticktock_read;
260 bool is_ticktock_write;
261#endif
263
264 protected_member(
265 union {
266 struct {
269 };
270 vsf_stream_terminal_t terminal[2];
271 };
273};
274
287 public_member(
288 vsf_stream_t *stream_tx;
289 vsf_stream_t *stream_rx;
290 uint32_t threshold_tx; // threshold of data_size of stream_tx
291 uint32_t threshold_rx; // threshold of free_size of stream_rx
292 // user can read data from stream_tx and write to stream_rx in on_data callback,
293 // and return the actual size write to stream_rx
294 uint_fast32_t (*on_data)(vsf_stream_adapter_t *adapter, uint_fast32_t tx_data_size, uint_fast32_t rx_free_size);
296};
297
298/*============================ GLOBAL VARIABLES ==============================*/
299/*============================ PROTOTYPES ====================================*/
300
334#if VSF_STREAM_CFG_THRESHOLD == ENABLED
335// set threshold CAN ONLY be called after stream_init
336extern void vsf_stream_set_tx_threshold(vsf_stream_t *stream, uint_fast32_t threshold);
337extern void vsf_stream_set_rx_threshold(vsf_stream_t *stream, uint_fast32_t threshold);
338#endif
344extern void vsf_stream_connect_rx(vsf_stream_t *stream);
345extern void vsf_stream_connect_tx(vsf_stream_t *stream);
346extern void vsf_stream_disconnect_rx(vsf_stream_t *stream);
347extern void vsf_stream_disconnect_tx(vsf_stream_t *stream);
348extern bool vsf_stream_is_rx_connected(vsf_stream_t *stream);
349extern bool vsf_stream_is_tx_connected(vsf_stream_t *stream);
350
351#if defined(__VSF_SIMPLE_STREAM_CLASS_IMPLEMENT) \
352 || defined(__VSF_SIMPLE_STREAM_CLASS_INHERIT__)
353extern void __vsf_stream_on_read(vsf_stream_t *stream);
354extern void __vsf_stream_on_write(vsf_stream_t *stream);
355#endif
356
357// vsf_stream_adapter_evthandler is only for classes derived from vsf_stream_adapter_t
358// IMPORTANT: vsf_stream_adapter_evthandler for stream_tx and stream_rx can not be pre-empted by each other
361
362#ifdef __cplusplus
363}
364#endif
365
366#undef __VSF_SIMPLE_STREAM_CLASS_IMPLEMENT
367#undef __VSF_SIMPLE_STREAM_CLASS_INHERIT__
368
369/*============================ INCLUDES ======================================*/
370
376
// vsf_simple_stream // vsf_service
379
380#endif // VSF_USE_SIMPLE_STREAM
381#endif // __VSF_SIMPLE_STREAM_H__
#define ENABLED
Definition __type.h:28
vsf_err_t
Definition __type.h:42
Definition vsf_simple_stream.h:286
Definition vsf_simple_stream.h:176
Definition vsf_simple_stream.h:254
Definition vsf_simple_stream.h:231
uint_fast32_t vsf_stream_get_free_size(vsf_stream_t *stream)
Definition vsf_simple_stream.c:120
uint_fast32_t vsf_stream_adapter_evthandler(vsf_stream_t *stream, void *param, vsf_stream_evt_t evt)
Definition vsf_simple_stream.c:245
void vsf_stream_adapter_init(vsf_stream_adapter_t *adapter)
Definition vsf_simple_stream.c:288
uint_fast32_t vsf_stream_get_rbuf(vsf_stream_t *stream, uint8_t **ptr)
Definition vsf_simple_stream.c:139
bool vsf_stream_is_tx_connected(vsf_stream_t *stream)
Definition vsf_simple_stream.c:188
bool vsf_stream_is_rx_connected(vsf_stream_t *stream)
Definition vsf_simple_stream.c:182
uint_fast32_t vsf_stream_read(vsf_stream_t *stream, uint8_t *buf, uint_fast32_t size)
Definition vsf_simple_stream.c:76
uint_fast32_t vsf_stream_get_wbuf(vsf_stream_t *stream, uint8_t **ptr)
Definition vsf_simple_stream.c:126
void vsf_stream_set_rx_threshold(vsf_stream_t *stream, uint_fast32_t threshold)
Definition vsf_simple_stream.c:100
#define VSF_STREAM_CFG_THRESHOLD
Definition vsf_simple_stream.h:80
uint_fast32_t vsf_stream_write(vsf_stream_t *stream, uint8_t *buf, uint_fast32_t size)
Definition vsf_simple_stream.c:84
void vsf_stream_connect_rx(vsf_stream_t *stream)
Definition vsf_simple_stream.c:170
void vsf_stream_set_tx_threshold(vsf_stream_t *stream, uint_fast32_t threshold)
Definition vsf_simple_stream.c:93
uint_fast32_t vsf_stream_get_data_size(vsf_stream_t *stream)
Definition vsf_simple_stream.c:114
vsf_stream_evt_t
Definition vsf_simple_stream.h:160
void vsf_stream_disconnect_tx(vsf_stream_t *stream)
Definition vsf_simple_stream.c:211
uint_fast32_t vsf_stream_get_buff_size(vsf_stream_t *stream)
Definition vsf_simple_stream.c:108
vsf_err_t vsf_stream_fini(vsf_stream_t *stream)
Definition vsf_simple_stream.c:230
void vsf_stream_connect_tx(vsf_stream_t *stream)
Definition vsf_simple_stream.c:176
vsf_err_t vsf_stream_init(vsf_stream_t *stream)
Definition vsf_simple_stream.c:217
void vsf_stream_disconnect_rx(vsf_stream_t *stream)
Definition vsf_simple_stream.c:205
@ VSF_STREAM_ON_DISCONNECT
Definition vsf_simple_stream.h:162
@ VSF_STREAM_ON_CONNECT
Definition vsf_simple_stream.h:161
@ VSF_STREAM_ON_TX
Definition vsf_simple_stream.h:166
@ VSF_STREAM_ON_OUT
Definition vsf_simple_stream.h:165
@ VSF_STREAM_ON_IN
Definition vsf_simple_stream.h:163
@ VSF_STREAM_ON_RX
Definition vsf_simple_stream.h:164
#define vsf_dcl_class
Definition ooc_class.h:46
#define vsf_class(__name)
Definition ooc_class.h:48
unsigned uint32_t
Definition stdint.h:9
unsigned int uint_fast32_t
Definition stdint.h:27
unsigned char uint8_t
Definition stdint.h:5
#define read
Definition unistd.h:122
#define write
Definition unistd.h:123
uint32_t size
Definition vsf_memfs.h:50
void __vsf_stream_on_write(vsf_stream_t *stream)
Definition vsf_simple_stream.c:38
void __vsf_stream_on_read(vsf_stream_t *stream)
Definition vsf_simple_stream.c:57