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
use crate::{
commands::{CommandTag, GenericCommand},
macros::{derive_free, wrap},
mem::heap_remove,
};
use servicepoint::{Header, Packet, UdpSocketExt};
use std::{
ffi::{c_char, CStr},
net::{Ipv4Addr, SocketAddrV4, UdpSocket},
ptr::NonNull,
};
derive_free!(UdpSocket);
wrap! {
UdpSocket {
functions:
/// Creates a new instance of [UdpSocket].
///
/// returns: NULL if connection fails, or connected instance
///
/// # Examples
///
/// ```C
/// UdpSocket connection = sp_udp_open("172.23.42.29:2342");
/// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear());
/// ```
fn open(host: val NonNull<c_char>) -> move_ok *mut UdpSocket {
let host = unsafe { CStr::from_ptr(host.as_ptr()) }
.to_str()
.expect("Bad encoding");
UdpSocket::bind_connect(host)
};
/// Creates a new instance of [UdpSocket].
///
/// returns: NULL if connection fails, or connected instance
///
/// # Examples
///
/// ```C
/// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342);
/// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear());
/// ```
fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket {
let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
UdpSocket::bind_connect(addr)
};
methods:
/// Sends a [Packet] to the display using the [UdpSocket].
///
/// The passed `packet` gets consumed.
///
/// returns: true in case of success
fn send_packet(ref connection, packet: move NonNull<Packet>) -> val bool {
connection.send(&Vec::from(packet)).is_ok()
};
/// Sends a [GenericCommand] to the display using the [UdpSocket].
///
/// The passed `command` gets consumed.
///
/// returns: true in case of success
///
/// # Examples
///
/// ```C
/// sp_udp_send_command(connection, sp_command_brightness(5));
/// ```
fn send_command(ref connection, command: mut NonNull<GenericCommand>) -> val bool {
unsafe {
let result = match command.tag {
CommandTag::Invalid => return false,
CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)),
CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)),
CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)),
CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)),
CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)),
CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)),
CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)),
CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)),
CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)),
CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)),
}.is_some();
*command = GenericCommand::INVALID;
result
}
};
/// Sends a [Header] to the display using the [UdpSocket].
///
/// returns: true in case of success
///
/// # Examples
///
/// ```C
/// sp_udp_send_header(connection, sp_command_brightness(5));
/// ```
fn send_header(ref udp_connection, header: val Header) -> val bool {
let packet = Packet {
header,
payload: None,
};
udp_connection.send(&Vec::from(packet)).is_ok()
};
}
}
mod _hidden {
/// This is a type only used by cbindgen to have a type for pointers.
///
/// See [servicepoint::UdpSocketExt].
#[allow(unused)]
pub struct UdpSocket;
}