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
/// Contains the Server type with which we connect to the server.
///
/// This module also holds the `ClientMessage` and `ServerMessage` types that
/// are sent to and received from the server.
use bitflags::bitflags;
use framous::{FramedRead, FramedWrite, FramedWriter};
use mac_address::{get_mac_address, MacAddress};
pub const SLIM_PORT: u16 = 3483;

use crate::{codec::SlimCodec, status::StatusData, Capabilities, Capability};

use std::{
    collections::HashMap,
    io::{self, BufReader, BufWriter},
    net::{Ipv4Addr, SocketAddrV4, TcpStream},
    time::Duration,
};

/// An enum which describes the various [TLV](https://en.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value)
/// values with which the server can respond.
#[derive(Debug)]
pub enum ServerTlv {
    Name(String),
    Version(String),
    Address(Ipv4Addr),
    Port(u16),
}

/// A hashmap to hold all TLVs from the server
pub(crate) type ServerTlvMap = HashMap<String, ServerTlv>;

/// A Server struct to hold the connection details
pub struct Server {
    pub socket: SocketAddrV4,
    pub tlv_map: Option<ServerTlvMap>,
    pub sync_group_id: Option<String>,
}

/// Allow to clone the server.
/// We'll lose the TLV map but it's not needed for connecting to the server
impl Clone for Server {
    fn clone(&self) -> Self {
        Self {
            socket: self.socket,
            tlv_map: None,
            sync_group_id: self.sync_group_id.as_ref().map(String::from),
        }
    }
}

/// Useful for conversions from a Serv message
impl From<(Ipv4Addr, Option<String>)> for Server {
    fn from(value: (Ipv4Addr, Option<String>)) -> Self {
        Self {
            socket: SocketAddrV4::new(value.0, SLIM_PORT),
            tlv_map: None,
            sync_group_id: value.1,
        }
    }
}

impl From<SocketAddrV4> for Server {
    fn from(value: SocketAddrV4) -> Self {
        Self {
            socket: value,
            tlv_map: None,
            sync_group_id: None,
        }
    }
}

impl Default for Server {
    fn default() -> Self {
        Self {
            socket: SocketAddrV4::new([0, 0, 0, 0].into(), 9000),
            tlv_map: None,
            sync_group_id: None,
        }
    }
}

/// A prepared server struct is one that has capabilities and is ready
/// for connection to the Slim server
pub struct PreparedServer {
    server: Server,
    caps: Capabilities,
}

impl Server {
    pub fn prepare(&self, mut caps: Capabilities) -> PreparedServer {
        if let Some(sgid) = &self.sync_group_id {
            caps.add(Capability::Syncgroupid(sgid.to_owned()));
        }
        PreparedServer {
            server: self.clone(),
            caps,
        }
    }
}

impl PreparedServer {
    pub fn connect(
        self,
    ) -> io::Result<(
        FramedRead<BufReader<TcpStream>, SlimCodec>,
        FramedWrite<BufWriter<TcpStream>, SlimCodec>,
    )> {
        let cx = TcpStream::connect(self.server.socket)?;

        let helo = ClientMessage::Helo {
            device_id: 12,
            revision: 0,
            mac: match get_mac_address() {
                Ok(Some(mac)) => mac,
                _ => MacAddress::new([1, 2, 3, 4, 5, 6]),
            },
            uuid: [0u8; 16],
            wlan_channel_list: 0,
            bytes_received: 0,
            language: ['e', 'n'],
            capabilities: self.caps.to_string(),
        };

        let rx = FramedRead::new(BufReader::new(cx.try_clone()?), SlimCodec);
        let mut tx = FramedWrite::new(BufWriter::new(cx), SlimCodec);

        tx.framed_write(helo)?;
        Ok((rx, tx))
    }
}

/// A type that describes all messages that are sent from the client to
/// the server.
#[derive(Debug)]
pub enum ClientMessage {
    Helo {
        device_id: u8,
        revision: u8,
        mac: MacAddress,
        uuid: [u8; 16],
        wlan_channel_list: u16,
        bytes_received: u64,
        language: [char; 2],
        capabilities: String,
    },
    Stat {
        event_code: String,
        stat_data: StatusData,
    },
    Bye(u8),
    Name(String),
}

#[derive(Debug, PartialEq)]
pub enum AutoStart {
    None,
    Auto,
    Direct,
    AutoDirect,
}

#[derive(Debug, PartialEq)]
pub enum Format {
    Pcm,
    Mp3,
    Flac,
    Wma,
    Ogg,
    Aac,
    Alac,
}
#[derive(Debug, PartialEq)]
pub enum PcmSampleSize {
    Eight,
    Sixteen,
    Twenty,
    ThirtyTwo,
    SelfDescribing,
}

#[derive(Debug, PartialEq)]
pub enum PcmSampleRate {
    Rate(u32),
    SelfDescribing,
}

#[derive(Debug, PartialEq)]
pub enum PcmChannels {
    Mono,
    Stereo,
    SelfDescribing,
}

#[derive(Debug, PartialEq)]
pub enum PcmEndian {
    Big,
    Little,
    SelfDescribing,
}

#[derive(Debug, PartialEq)]
pub enum SpdifEnable {
    Auto,
    On,
    Off,
}

#[derive(Debug, PartialEq)]
pub enum TransType {
    None,
    Crossfade,
    FadeIn,
    FadeOut,
    FadeInOut,
}

bitflags! {
    #[derive(Debug, Default, PartialEq)]
    pub struct StreamFlags: u8 {
        const INF_LOOP = 0b1000_0000;
        const NO_RESTART_DECODER = 0b0100_0000;
        const INVERT_POLARITY_LEFT = 0b0000_0001;
        const INVERT_POLARITY_RIGHT = 0b0000_0010;
    }
}

/// A type that describes all messages that are sent from the server to
/// the client.
#[derive(Debug)]
pub enum ServerMessage {
    Serv {
        ip_address: Ipv4Addr,
        sync_group_id: Option<String>,
    },
    Status(Duration),
    Stream {
        autostart: AutoStart,
        format: Format,
        pcmsamplesize: PcmSampleSize,
        pcmsamplerate: PcmSampleRate,
        pcmchannels: PcmChannels,
        pcmendian: PcmEndian,
        threshold: u32,
        spdif_enable: SpdifEnable,
        trans_period: Duration,
        trans_type: TransType,
        flags: StreamFlags,
        output_threshold: Duration,
        replay_gain: f64,
        server_port: u16,
        server_ip: Ipv4Addr,
        http_headers: Option<String>,
    },
    Gain(f64, f64),
    Enable(bool, bool),
    Flush,
    Stop,
    Pause(Duration),
    Unpause(Duration),
    Queryname,
    Setname(String),
    DisableDac,
    Skip(Duration),
    Unrecognised(String),
    Error,
}