1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef K_BUF_QUEUE_H
6 #define K_BUF_QUEUE_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /** @addtogroup aos_rhino bitmap
13  *  buf_queue passes the specified length information.
14  *  When receiving a message, it can cause task blocking.
15  *  When sending a messages, it can't cause task blocking.
16  *  may discard the messages when the buffer is full.
17  *
18  *  @{
19  */
20 
21 /**
22  * buf_que object
23  */
24 typedef struct {
25     blk_obj_t    blk_obj;           /**< Manage blocked tasks */
26     void        *buf;               /**< ringbuf address */
27     k_ringbuf_t  ringbuf;           /**< ringbuf management */
28     size_t       max_msg_size;      /**< limited message length */
29     size_t       cur_num;           /**< msgs used */
30     size_t       peak_num;          /**< maximum msgs used */
31     size_t       min_free_buf_size; /**< minimum free size */
32 #if (RHINO_CONFIG_KOBJ_LIST > 0)
33     klist_t      buf_queue_item;    /**< kobj list for statistics */
34 #endif
35 #if (RHINO_CONFIG_USER_SPACE > 0)
36     uint32_t     key;
37 #endif
38     uint8_t      mm_alloc_flag;     /**< buffer from internal malloc or caller input */
39 } kbuf_queue_t;
40 
41 /**
42  * buf_queue infomation
43  */
44 typedef struct {
45     size_t buf_size;                /**< whole queue size */
46     size_t max_msg_size;
47     size_t cur_num;
48     size_t peak_num;
49     size_t free_buf_size;
50     size_t min_free_buf_size;
51 } kbuf_queue_info_t;
52 
53 /**
54  * Create a buf_queue.
55  *
56  * @param[in]  queue    pointer to the queue (the space is provided outside, by user)
57  * @param[in]  name     name of the queue
58  * @param[in]  buf      pointer to the buf
59  * @param[in]  size     size of the buf
60  * @param[in]  max_msg  max size of one msg
61  *
62  * @return  the operation status, RHINO_SUCCESS is OK, others is error
63  */
64 kstat_t krhino_buf_queue_create(kbuf_queue_t *queue, const name_t *name,
65                                 void *buf, size_t size, size_t max_msg);
66 
67 /**
68  * Create a fix-msg-len buf_queue.
69  *
70  * @param[in]  queue     pointer to the queue (the space is provided outside, by user)
71  * @param[in]  name      name of the queue
72  * @param[in]  buf       pointer to the buf
73  * @param[in]  msg_size  fix size of the msg
74  * @param[in]  msg_num   number of msg
75  *
76  * @return  the operation status, RHINO_SUCCESS is OK, others is error
77  */
78 kstat_t krhino_fix_buf_queue_create(kbuf_queue_t *queue, const name_t *name,
79                                     void *buf, size_t msg_size, size_t msg_num);
80 
81 /**
82  * Delete a buf_queue.
83  *
84  * @param[in]  queue  pointer to the buf_queue
85  *
86  * @return  the operation status, RHINO_SUCCESS is OK, others is error
87  */
88 kstat_t krhino_buf_queue_del(kbuf_queue_t *queue);
89 
90 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
91 
92 /**
93  * Malloc and create a variable-msg-len buf_queue.
94  * Each message consumes extra 4 bytes to save length.
95  *
96  * @param[out]  queue    pointer to the queue (the space is provided inside, from heap)
97  * @param[in]   name     pointer to the nam
98  * @param[in]   size     size of the buf
99  * @param[in]   max_msg  max size of one msg
100  *
101  * @return  the operation status, RHINO_SUCCESS is OK, others is error
102  */
103 kstat_t krhino_buf_queue_dyn_create(kbuf_queue_t **queue, const name_t *name,
104                                     size_t size, size_t max_msg);
105 
106 /**
107  * Malloc and create a fix-msg-len buf_queue.
108  *
109  * @param[out] queue     pointer to the queue (the space is provided inside, from heap)
110  * @param[in]  name      name of the queue
111  * @param[in]  buf       pointer to the buf
112  * @param[in]  msg_size  size of the msg
113  * @param[in]  msg_num   number of msg
114  *
115  * @return  the operation status, RHINO_SUCCESS is OK, others is error
116  */
117 kstat_t krhino_fix_buf_queue_dyn_create(kbuf_queue_t **queue, const name_t *name,
118                                         size_t msg_size, size_t msg_num);
119 
120 /**
121  * Delete and free a buf_queue.
122  *
123  * @param[in]  queue  pointer to the queue
124  *
125  * @return  the operation status, RHINO_SUCCESS is OK, others is error
126  */
127 kstat_t krhino_buf_queue_dyn_del(kbuf_queue_t *queue);
128 #endif
129 
130 /**
131  * This function will send a msg at the end of queue.
132  * If queue is full, task is non-blocking, return fail.
133  *
134  * @param[in]  queue  pointer to the queue
135  * @param[in]  msg    pointer to msg to be send
136  * @param[in]  size   size of the msg
137  *
138  * @return  the operation status, RHINO_SUCCESS is OK, others is error
139  */
140 kstat_t krhino_buf_queue_send(kbuf_queue_t *queue, void *msg, size_t size);
141 
142 /**
143  * This function will receive msg form aqueue.
144  * If queue is empty, task is blocking.
145  *
146  * @param[in]   queue  pointer to the queue
147  * @param[in]   ticks  ticks to wait before receiving msg
148  * @param[out]  msg    pointer to the buf to save msg
149  * @param[out]  size   size of received msg
150  *
151  * @return  the operation status, RHINO_SUCCESS is OK, others is error
152  */
153 kstat_t krhino_buf_queue_recv(kbuf_queue_t *queue, tick_t ticks, void *msg, size_t *size);
154 
155 /**
156  * Reset queue, abondon the msg.
157  *
158  * @param[in]  queue  pointer to the queue
159  *
160  * @return  the operation status, RHINO_SUCCESS is OK, others is error
161  */
162 kstat_t krhino_buf_queue_flush(kbuf_queue_t *queue);
163 
164 /**
165  * Get information of a queue.
166  *
167  * @param[in]   queue  pointer to the queue
168  * @param[out]  info   info msg of the queue buf
169  *
170  * @return  the operation status, RHINO_SUCCESS is OK, others is error
171  */
172 kstat_t krhino_buf_queue_info_get(kbuf_queue_t *queue, kbuf_queue_info_t *info);
173 
174 /** @} */
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif /* K_BUF_QUEUE_H */
181 
182