asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
stream_proto.h
1
11#ifndef ASRT_STREAM_PROTO_H
12#define ASRT_STREAM_PROTO_H
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18#include "./chann.h"
19#include "./status.h"
20#include "./util.h"
21
23enum asrt_strm_message_id_e
24{
25 ASRT_STRM_MSG_DEFINE = 0x01, // reactor -> controller
26 ASRT_STRM_MSG_DATA = 0x02, // reactor -> controller
27 ASRT_STRM_MSG_ERROR = 0x03, // controller -> reactor
28};
29typedef uint8_t asrt_strm_message_id;
30
32enum asrt_strm_field_type_e
33{
34 ASRT_STRM_FIELD_U8 = 0x01,
35 ASRT_STRM_FIELD_U16 = 0x02,
36 ASRT_STRM_FIELD_U32 = 0x03,
37 ASRT_STRM_FIELD_I8 = 0x04,
38 ASRT_STRM_FIELD_I16 = 0x05,
39 ASRT_STRM_FIELD_I32 = 0x06,
40 ASRT_STRM_FIELD_FLOAT = 0x07,
41 ASRT_STRM_FIELD_BOOL = 0x08,
42 ASRT_STRM_FIELD_LBRACKET = 0x09,
43 ASRT_STRM_FIELD_RBRACKET = 0x0A,
44};
45typedef uint8_t asrt_strm_field_type;
46
48#define ASRT_STRM_FIELD_TAG_MAX 0x0A
49
51enum asrt_strm_err_e
52{
53 ASRT_STRM_ERR_SUCCESS = 0x00,
54 ASRT_STRM_ERR_UNKNOWN_SCHEMA = 0x01,
55 ASRT_STRM_ERR_SIZE_MISMATCH = 0x02,
56 ASRT_STRM_ERR_ALLOC_FAILURE = 0x03,
57 ASRT_STRM_ERR_DUPLICATE_SCHEMA = 0x04,
58 ASRT_STRM_ERR_INVALID_DEFINE = 0x05,
59};
60
62static inline uint8_t asrt_strm_field_valid( uint8_t type_tag )
63{
64 return type_tag >= 0x01 && type_tag <= ASRT_STRM_FIELD_TAG_MAX;
65}
66
69static inline uint8_t asrt_strm_field_size( asrt_strm_field_type type_tag )
70{
71 switch ( type_tag ) {
72 case ASRT_STRM_FIELD_U8:
73 case ASRT_STRM_FIELD_I8:
74 case ASRT_STRM_FIELD_BOOL:
75 return 1;
76 case ASRT_STRM_FIELD_U16:
77 case ASRT_STRM_FIELD_I16:
78 return 2;
79 case ASRT_STRM_FIELD_U32:
80 case ASRT_STRM_FIELD_I32:
81 case ASRT_STRM_FIELD_FLOAT:
82 return 4;
83 case ASRT_STRM_FIELD_LBRACKET:
84 case ASRT_STRM_FIELD_RBRACKET:
85 default:
86 return 0;
87 }
88}
89
91static inline char const* asrt_strm_field_type_to_str( enum asrt_strm_field_type_e ft )
92{
93 switch ( ft ) {
94 case ASRT_STRM_FIELD_U8:
95 return "u8";
96 case ASRT_STRM_FIELD_U16:
97 return "u16";
98 case ASRT_STRM_FIELD_U32:
99 return "u32";
100 case ASRT_STRM_FIELD_I8:
101 return "i8";
102 case ASRT_STRM_FIELD_I16:
103 return "i16";
104 case ASRT_STRM_FIELD_I32:
105 return "i32";
106 case ASRT_STRM_FIELD_FLOAT:
107 return "float";
108 case ASRT_STRM_FIELD_BOOL:
109 return "bool";
110 case ASRT_STRM_FIELD_LBRACKET:
111 return "[";
112 case ASRT_STRM_FIELD_RBRACKET:
113 return "]";
114 default:
115 return "?";
116 }
117}
118
122{
123 struct asrt_span field_span;
124 uint8_t fields[255];
125 uint8_t hdr[3];
126 struct asrt_send_req req;
127};
128
129static inline struct asrt_send_req* asrt_msg_rtoc_strm_define(
130 struct asrt_strm_define_msg* msg,
131 uint8_t schema_id,
132 enum asrt_strm_field_type_e const* fields,
133 uint16_t field_count )
134{
135 ASRT_ASSERT( field_count <= 255 );
136 msg->hdr[0] = ASRT_STRM_MSG_DEFINE;
137 msg->hdr[1] = schema_id;
138 msg->hdr[2] = (uint8_t) field_count;
139 for ( uint16_t i = 0; i < field_count; i++ )
140 msg->fields[i] = (uint8_t) fields[i];
141 msg->field_span = ( struct asrt_span ){ .b = msg->fields, .e = msg->fields + field_count };
142 msg->req.buff = ( struct asrt_span_span ){
143 .b = msg->hdr,
144 .e = msg->hdr + sizeof msg->hdr,
145 .rest = &msg->field_span,
146 .rest_count = 1,
147 };
148 return &msg->req;
149}
150
154{
155 struct asrt_span data_span;
156 uint8_t hdr[2];
157 struct asrt_send_req req;
158};
159
160static inline struct asrt_send_req* asrt_msg_rtoc_strm_data(
161 struct asrt_strm_data_msg* msg,
162 uint8_t schema_id,
163 uint8_t const* data,
164 uint16_t data_size )
165{
166 msg->hdr[0] = ASRT_STRM_MSG_DATA;
167 msg->hdr[1] = schema_id;
168 msg->data_span =
169 ( struct asrt_span ){ .b = (uint8_t*) data, .e = (uint8_t*) data + data_size };
170 msg->req.buff = ( struct asrt_span_span ){
171 .b = msg->hdr,
172 .e = msg->hdr + sizeof msg->hdr,
173 .rest = &msg->data_span,
174 .rest_count = 1,
175 };
176 return &msg->req;
177}
178
180static inline struct asrt_send_req* asrt_msg_ctor_strm_error(
181 struct asrt_u8d2msg* msg,
182 enum asrt_strm_err_e error_code )
183{
184 msg->buff[0] = ASRT_STRM_MSG_ERROR;
185 msg->buff[1] = error_code;
186 msg->req.buff = ( struct asrt_span_span ){
187 .b = msg->buff,
188 .e = msg->buff + 2,
189 .rest = NULL,
190 .rest_count = 0,
191 };
192 return &msg->req;
193}
194
195#ifdef __cplusplus
196}
197#endif
198
199#endif // ASRT_STREAM_PROTO_H
An outgoing message request placed in a module's send queue.
Definition: chann.h:64
struct asrt_span_span buff
Message payload (scatter-gather).
Definition: chann.h:65
Scatter-gather buffer: a primary byte range [b, e) followed by rest_count additional spans.
Definition: span.h:37
uint8_t * b
Primary buffer start.
Definition: span.h:38
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: span.h:23
uint8_t * b
Pointer to the first byte.
Definition: span.h:24
uint8_t * e
One-past-the-end pointer.
Definition: span.h:25
Send DATA message from reactor to controller.
Definition: stream_proto.h:154
Send DEFINE message from reactor to controller.
Definition: stream_proto.h:122
Definition: chann.h:196