agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
msg_queue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 ykat UG (haftungsbeschraenkt) - All Rights Reserved
3  *
4  * Permission for non-commercial use is hereby granted,
5  * free of charge, without warranty of any kind.
6  */
7 #ifndef ZRTOS_MSG_QUEUE_H
8 #define ZRTOS_MSG_QUEUE_H
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 
14 #include <zrtos/types.h>
15 #include <zrtos/cbuffer.h>
16 
17 
19  size_t length;
20 }__attribute__((packed))zrtos_msg_queue_header_t;
21 
22 typedef struct _zrtos_msg_queue_t{
24  size_t msg_count;
27 
30  size_t msg_count;
32 
34  zrtos_msg_queue_t *thiz
36 ){
38  &thiz->cbuffer
39  ,&txn->txn
40  );
41  txn->msg_count = thiz->msg_count;
42 }
43 
45  zrtos_msg_queue_t *thiz
47 ){
49  &thiz->cbuffer
50  ,&txn->txn
51  );
52  thiz->msg_count = txn->msg_count;
53 }
54 
56  return thiz->msg_count == 0 && thiz->header.length == 0;
57 }
58 
60  zrtos_msg_queue_t *thiz
61  ,size_t len
62 ){
63  zrtos_msg_queue_header_t header = {
64  .length = len
65  };
66  size_t outlen;
68  &thiz->cbuffer
69  ,1
70  ,&outlen
71  ,&header
72  ,sizeof(header)
73  );
74  return zrtos_error__is_success(ret);
75 }
76 
78  zrtos_msg_queue_t *thiz
79  ,void *data
80  ,size_t len
81 ){
82  size_t outlen;
84  &thiz->cbuffer
85  ,1
86  ,&outlen
87  ,data
88  ,len
89  );
90  return ret;
91 }
92 
94  zrtos_msg_queue_t *thiz
95 ){
96  thiz->msg_count++;
97  return ZRTOS_ERROR__SUCCESS;
98 }
99 
101  zrtos_msg_queue_t *thiz
102  ,zrtos_cbuffer_t *data
103  ,size_t length
104 ){
106  &thiz->cbuffer
107  ,data
108  ,length
109  );
110  if(zrtos_error__is_success(ret)){
111  thiz->msg_count++;
112  }
113  return ret;
114 }
115 
117  zrtos_msg_queue_t *thiz
118  ,void *data
119  ,size_t len
120 ){
121  zrtos_msg_queue_header_t header = {
122  .length = len
123  };
124  size_t outlen;
126  &thiz->cbuffer
127  ,2
128  ,&outlen
129  ,&header
130  ,sizeof(header)
131  ,data
132  ,len
133  );
134  if(zrtos_error__is_success(ret)){
135  thiz->msg_count++;
136  }
137  return ret;
138 }
139 
141  zrtos_msg_queue_t *thiz
142  ,void *data
143  ,size_t len
144  ,size_t *outlen
145 ){
146  zrtos_error_t ret;
147  if(thiz->header.length){
148 L_READ_MESSAGE_BODY:
149  ret = zrtos_cbuffer__get_ex(
150  &thiz->cbuffer
151  ,data
152  ,ZRTOS_TYPES__MIN(len,thiz->header.length)
153  ,outlen
154  );
155  thiz->header.length -= *outlen;
156  if(thiz->header.length == 0){
157  thiz->msg_count--;
158  }else{
159  ret = ZRTOS_ERROR__AGAIN;
160  }
161  }else if(thiz->msg_count && zrtos_cbuffer__can_read_length(
162  &thiz->cbuffer
163  ,sizeof(thiz->header)
164  )){
165  ret = zrtos_cbuffer__get_ex(
166  &thiz->cbuffer
167  ,&thiz->header
168  ,sizeof(thiz->header)
169  ,outlen
170  );
171  if(zrtos_error__is_success(ret)){
172  goto L_READ_MESSAGE_BODY;
173  }
174  }else{
175  ret = ZRTOS_ERROR__NODATA;
176  }
177  return ret;
178 }
179 
181  zrtos_msg_queue_t *thiz
182  ,zrtos_cbuffer_t *dest
183  ,size_t len
184  ,size_t *outlen
185 ){
186  zrtos_error_t ret;
187  if(thiz->header.length){
188 L_READ_MESSAGE_BODY:
189  ret = zrtos_cbuffer__pipe(
190  &thiz->cbuffer
191  ,dest
192  ,ZRTOS_TYPES__MIN(len,thiz->header.length)
193  ,outlen
194  );
195  thiz->header.length -= *outlen;
196  if(thiz->header.length == 0){
197  thiz->msg_count--;
198  }else{
199  ret = ZRTOS_ERROR__AGAIN;
200  }
201  }else if(thiz->msg_count && zrtos_cbuffer__can_read_length(
202  &thiz->cbuffer
203  ,sizeof(thiz->header)
204  )){
205  ret = zrtos_cbuffer__get_ex(
206  &thiz->cbuffer
207  ,&thiz->header
208  ,sizeof(thiz->header)
209  ,outlen
210  );
211  if(zrtos_error__is_success(ret)){
212  goto L_READ_MESSAGE_BODY;
213  }
214  }else{
215  ret = ZRTOS_ERROR__NODATA;
216  }
217  return ret;
218 }
219 
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 #endif
No data available.
Definition: error.h:85
void zrtos_msg_queue__start_write_transaction(zrtos_msg_queue_t *thiz, zrtos_msg_queue_write_transaction_t *txn)
Definition: msg_queue.h:33
zrtos_error_t zrtos_msg_queue__pipe_next_message_to_cbuffer(zrtos_msg_queue_t *thiz, zrtos_cbuffer_t *dest, size_t len, size_t *outlen)
Definition: msg_queue.h:180
zrtos_error_t zrtos_msg_queue__put_length(zrtos_msg_queue_t *thiz, size_t len)
Definition: msg_queue.h:59
Try again.
Definition: error.h:35
void zrtos_cbuffer__rollback_write_transaction(zrtos_cbuffer_t *thiz, zrtos_cbuffer_write_transaction_t *txn)
Definition: cbuffer.h:154
zrtos_error_t zrtos_cbuffer__get_ex(zrtos_cbuffer_t *thiz, void *data, size_t len, size_t *outlen)
Definition: cbuffer.h:336
zrtos_cbuffer_write_transaction_t txn
Definition: msg_queue.h:29
zrtos_error_t zrtos_msg_queue__put_data(zrtos_msg_queue_t *thiz, void *data, size_t len)
Definition: msg_queue.h:77
zrtos_msg_queue_header_t header
Definition: msg_queue.h:25
struct _zrtos_msg_queue_header_t zrtos_msg_queue_header_t
struct _zrtos_msg_queue_t zrtos_msg_queue_t
bool zrtos_msg_queue__is_empty(zrtos_msg_queue_t *thiz)
Definition: msg_queue.h:55
void zrtos_cbuffer__start_write_transaction(zrtos_cbuffer_t *thiz, zrtos_cbuffer_write_transaction_t *txn)
Definition: cbuffer.h:146
bool zrtos_error__is_success(zrtos_error_t thiz)
Definition: error.h:152
#define ZRTOS_TYPES__MIN(a, b)
Definition: types.h:78
Successful program execution status.
Definition: error.h:22
struct _zrtos_msg_queue_write_transaction_t zrtos_msg_queue_write_transaction_t
void zrtos_msg_queue__rollback_write_transaction(zrtos_msg_queue_t *thiz, zrtos_msg_queue_write_transaction_t *txn)
Definition: msg_queue.h:44
zrtos_error_t zrtos_msg_queue__put(zrtos_msg_queue_t *thiz, void *data, size_t len)
Definition: msg_queue.h:116
bool zrtos_cbuffer__can_read_length(zrtos_cbuffer_t *thiz, size_t length)
Definition: cbuffer.h:382
zrtos_error_t zrtos_msg_queue__put_cbuffer_data(zrtos_msg_queue_t *thiz, zrtos_cbuffer_t *data, size_t length)
Definition: msg_queue.h:100
zrtos_cbuffer_t cbuffer
Definition: msg_queue.h:23
zrtos_error_t zrtos_cbuffer__put_ex(zrtos_cbuffer_t *thiz, size_t len, size_t *outlen,...)
Definition: cbuffer.h:278
zrtos_error_t zrtos_msg_queue__put_end(zrtos_msg_queue_t *thiz)
Definition: msg_queue.h:93
zrtos_error_t
Definition: error.h:20
zrtos_error_t zrtos_msg_queue__get(zrtos_msg_queue_t *thiz, void *data, size_t len, size_t *outlen)
Definition: msg_queue.h:140
zrtos_error_t zrtos_cbuffer__pipe(zrtos_cbuffer_t *thiz, zrtos_cbuffer_t *src, size_t length, size_t *outlen)
Definition: cbuffer.h:421