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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "README.md" ) ) ]
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
pub const OP_NOP: u8 = 0x00;
pub const OP_PUBLISH: u8 = 0x01;
pub const OP_SUBSCRIBE: u8 = 0x02;
pub const OP_UNSUBSCRIBE: u8 = 0x03;
pub const OP_MESSAGE: u8 = 0x12;
pub const OP_BROADCAST: u8 = 0x13;
pub const OP_ACK: u8 = 0xFE;
pub const PROTOCOL_VERSION: u16 = 0x01;
pub const RESPONSE_OK: u8 = 0x01;
pub const PING_FRAME: &[u8] = &[0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const ERR_CLIENT_NOT_REGISTERED: u8 = 0x71;
pub const ERR_DATA: u8 = 0x72;
pub const ERR_IO: u8 = 0x73;
pub const ERR_OTHER: u8 = 0x74;
pub const ERR_NOT_SUPPORTED: u8 = 0x75;
pub const ERR_BUSY: u8 = 0x76;
pub const ERR_NOT_DELIVERED: u8 = 0x77;
pub const ERR_TIMEOUT: u8 = 0x78;
pub const ERR_ACCESS: u8 = 0x79;
pub const GREETINGS: [u8; 1] = [0xEB];
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub static AUTHOR: &str = "(c) 2022 Bohemia Automation / Altertech";
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(1);
pub const DEFAULT_BUF_TTL: Duration = Duration::from_micros(10);
pub const DEFAULT_BUF_SIZE: usize = 8192;
pub const DEFAULT_QUEUE_SIZE: usize = 8192;
pub const SECONDARY_SEP: &str = "%%";
pub type OpConfirm = Option<tokio::sync::oneshot::Receiver<Result<(), Error>>>;
pub type Frame = Arc<FrameData>;
pub type EventChannel = async_channel::Receiver<Frame>;
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum ErrorKind {
NotRegistered = ERR_CLIENT_NOT_REGISTERED,
NotSupported = ERR_NOT_SUPPORTED,
Io = ERR_IO,
Timeout = ERR_TIMEOUT,
Data = ERR_DATA,
Busy = ERR_BUSY,
NotDelivered = ERR_NOT_DELIVERED,
Access = ERR_ACCESS,
Other = ERR_OTHER,
Eof = 0xff,
}
impl From<u8> for ErrorKind {
fn from(code: u8) -> Self {
match code {
ERR_CLIENT_NOT_REGISTERED => ErrorKind::NotRegistered,
ERR_NOT_SUPPORTED => ErrorKind::NotSupported,
ERR_IO => ErrorKind::Io,
ERR_DATA => ErrorKind::Data,
ERR_BUSY => ErrorKind::Busy,
ERR_NOT_DELIVERED => ErrorKind::NotDelivered,
ERR_ACCESS => ErrorKind::Access,
_ => ErrorKind::Other,
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
ErrorKind::NotRegistered => "Client not registered",
ErrorKind::NotSupported => "Feature not supported",
ErrorKind::Io => "I/O Error",
ErrorKind::Timeout => "Timeout",
ErrorKind::Data => "Data Error",
ErrorKind::Busy => "Busy",
ErrorKind::NotDelivered => "Frame not delivered",
ErrorKind::Other => "Error",
ErrorKind::Access => "Access denied",
ErrorKind::Eof => "Eof",
}
)
}
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
message: Option<String>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref message) = self.message {
write!(f, "{}: {}", self.kind, message)
} else {
write!(f, "{}", self.kind)
}
}
}
impl Error {
#[inline]
pub fn new(kind: ErrorKind, message: Option<impl fmt::Display>) -> Self {
Self {
kind,
message: message.map(|m| m.to_string()),
}
}
#[inline]
pub fn io(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Io,
message: Some(e.to_string()),
}
}
#[inline]
pub fn data(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Data,
message: Some(e.to_string()),
}
}
#[inline]
pub fn access(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Access,
message: Some(e.to_string()),
}
}
#[inline]
pub fn not_supported(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::NotSupported,
message: Some(e.to_string()),
}
}
#[inline]
pub fn not_registered() -> Self {
Self {
kind: ErrorKind::NotRegistered,
message: None,
}
}
#[inline]
pub fn not_delivered() -> Self {
Self {
kind: ErrorKind::NotDelivered,
message: None,
}
}
#[inline]
pub fn timeout() -> Self {
Self {
kind: ErrorKind::Timeout,
message: None,
}
}
#[inline]
pub fn busy(e: impl fmt::Display) -> Self {
Self {
kind: ErrorKind::Busy,
message: Some(e.to_string()),
}
}
#[inline]
pub fn kind(&self) -> ErrorKind {
self.kind
}
}
pub trait IntoBusRtResult {
fn to_busrt_result(self) -> Result<(), Error>;
}
impl IntoBusRtResult for u8 {
#[inline]
fn to_busrt_result(self) -> Result<(), Error> {
if self == RESPONSE_OK {
Ok(())
} else {
Err(Error {
kind: self.into(),
message: None,
})
}
}
}
impl From<tokio::time::error::Elapsed> for Error {
fn from(_e: tokio::time::error::Elapsed) -> Error {
Error::timeout()
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
if e.kind() == std::io::ErrorKind::UnexpectedEof
|| e.kind() == std::io::ErrorKind::BrokenPipe
|| e.kind() == std::io::ErrorKind::ConnectionReset
{
Error {
kind: ErrorKind::Eof,
message: None,
}
} else {
Error::io(e)
}
}
}
impl From<&std::io::Error> for Error {
fn from(e: &std::io::Error) -> Error {
if e.kind() == std::io::ErrorKind::UnexpectedEof
|| e.kind() == std::io::ErrorKind::BrokenPipe
|| e.kind() == std::io::ErrorKind::ConnectionReset
{
Error {
kind: ErrorKind::Eof,
message: None,
}
} else {
Error::io(e)
}
}
}
impl From<std::str::Utf8Error> for Error {
fn from(e: std::str::Utf8Error) -> Error {
Error::data(e)
}
}
impl From<std::array::TryFromSliceError> for Error {
fn from(e: std::array::TryFromSliceError) -> Error {
Error::data(e)
}
}
impl<T> From<async_channel::SendError<T>> for Error {
fn from(_e: async_channel::SendError<T>) -> Error {
Error {
kind: ErrorKind::Eof,
message: None,
}
}
}
impl From<tokio::sync::oneshot::error::RecvError> for Error {
fn from(_e: tokio::sync::oneshot::error::RecvError) -> Error {
Error {
kind: ErrorKind::Eof,
message: None,
}
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum FrameOp {
Nop = OP_NOP,
Message = OP_MESSAGE,
Broadcast = OP_BROADCAST,
PublishTopic = OP_PUBLISH,
SubscribeTopic = OP_SUBSCRIBE,
UnsubscribeTopic = OP_UNSUBSCRIBE,
}
impl TryFrom<u8> for FrameOp {
type Error = Error;
fn try_from(tp: u8) -> Result<Self, Error> {
match tp {
OP_NOP => Ok(FrameOp::Nop),
OP_MESSAGE => Ok(FrameOp::Message),
OP_BROADCAST => Ok(FrameOp::Broadcast),
OP_PUBLISH => Ok(FrameOp::PublishTopic),
OP_SUBSCRIBE => Ok(FrameOp::SubscribeTopic),
OP_UNSUBSCRIBE => Ok(FrameOp::UnsubscribeTopic),
_ => Err(Error::data(format!("Invalid frame type: {}", tp))),
}
}
}
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum QoS {
No = 0,
Processed = 1,
Realtime = 2,
RealtimeProcessed = 3,
}
impl QoS {
#[inline]
pub fn is_realtime(self) -> bool {
self as u8 & 0b10 != 0
}
#[inline]
pub fn needs_ack(self) -> bool {
self as u8 & 0b1 != 0
}
}
impl TryFrom<u8> for QoS {
type Error = Error;
fn try_from(q: u8) -> Result<Self, Error> {
match q {
0 => Ok(QoS::No),
1 => Ok(QoS::Processed),
2 => Ok(QoS::Realtime),
3 => Ok(QoS::RealtimeProcessed),
_ => Err(Error::data(format!("Invalid QoS: {}", q))),
}
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum FrameKind {
Prepared = 0xff,
Message = OP_MESSAGE,
Broadcast = OP_BROADCAST,
Publish = OP_PUBLISH,
Acknowledge = OP_ACK,
Nop = OP_NOP,
}
impl TryFrom<u8> for FrameKind {
type Error = Error;
fn try_from(code: u8) -> Result<Self, Self::Error> {
match code {
OP_MESSAGE => Ok(FrameKind::Message),
OP_BROADCAST => Ok(FrameKind::Broadcast),
OP_PUBLISH => Ok(FrameKind::Publish),
OP_ACK => Ok(FrameKind::Acknowledge),
OP_NOP => Ok(FrameKind::Nop),
_ => Err(Error::data(format!("Invalid frame type: {:x}", code))),
}
}
}
#[derive(Debug)]
pub struct FrameData {
kind: FrameKind,
sender: Option<String>,
topic: Option<String>,
header: Option<Vec<u8>>, buf: Vec<u8>,
payload_pos: usize,
realtime: bool,
}
impl FrameData {
#[inline]
pub fn new(
kind: FrameKind,
sender: Option<String>,
topic: Option<String>,
header: Option<Vec<u8>>,
buf: Vec<u8>,
payload_pos: usize,
realtime: bool,
) -> Self {
Self {
kind,
sender,
topic,
header,
buf,
payload_pos,
realtime,
}
}
#[inline]
pub fn new_nop() -> Self {
Self {
kind: FrameKind::Nop,
sender: None,
topic: None,
header: None,
buf: Vec::new(),
payload_pos: 0,
realtime: false,
}
}
#[inline]
pub fn kind(&self) -> FrameKind {
self.kind
}
#[inline]
pub fn sender(&self) -> &str {
self.sender.as_ref().unwrap()
}
#[inline]
pub fn primary_sender(&self) -> &str {
let primary_sender = self.sender.as_ref().unwrap();
if let Some(pos) = primary_sender.find(SECONDARY_SEP) {
&primary_sender[..pos]
} else {
primary_sender
}
}
#[inline]
pub fn topic(&self) -> Option<&str> {
self.topic.as_deref()
}
#[inline]
pub fn payload(&self) -> &[u8] {
&self.buf[self.payload_pos..]
}
#[inline]
pub fn header(&self) -> Option<&[u8]> {
self.header.as_deref()
}
#[inline]
pub fn is_realtime(&self) -> bool {
self.realtime
}
}
pub mod borrow;
pub mod common;
pub mod tools {
#[cfg(any(feature = "rpc", feature = "broker", feature = "ipc"))]
pub mod pubsub;
}
#[cfg(feature = "broker")]
pub mod broker;
#[cfg(feature = "ipc")]
pub mod ipc;
#[cfg(feature = "rpc")]
pub mod rpc;
#[cfg(any(feature = "rpc", feature = "broker", feature = "ipc"))]
pub mod client;
#[cfg(any(feature = "broker", feature = "ipc"))]
pub mod comm;