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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#![allow(dead_code)]
mod status;
pub mod sys;

use {
    anyhow::{Context, Result},
    nix::{ioctl_read, ioctl_write_int_bad, ioctl_write_ptr, request_code_none},
    std::{
        ffi::CStr,
        fmt,
        fs::{File, OpenOptions},
        ops::Range,
        os::unix::{
            fs::{FileTypeExt, OpenOptionsExt},
            io::{AsRawFd, RawFd},
        },
    },
    sys::*,
};

pub use status::FeStatus;

/// A reference to the frontend device and device information
#[derive(Debug)]
pub struct FeDevice {
    adapter: u32,
    device: u32,

    file: File,

    api_version: u16,

    name: String,
    delivery_system_list: Vec<fe_delivery_system>,
    frequency_range: Range<u32>,
    symbolrate_range: Range<u32>,
    caps: fe_caps,
}

impl fmt::Display for FeDevice {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "DVB API: {}.{}",
            self.api_version >> 8,
            self.api_version & 0xFF
        )?;
        writeln!(f, "Frontend: {}", self.name)?;

        write!(f, "Delivery system:")?;
        for v in &self.delivery_system_list {
            write!(f, " {}", v)?;
        }
        writeln!(f, "")?;

        writeln!(
            f,
            "Frequency range: {} .. {}",
            self.frequency_range.start / 1000,
            self.frequency_range.end / 1000
        )?;

        writeln!(
            f,
            "Symbolrate range: {} .. {}",
            self.symbolrate_range.start / 1000,
            self.symbolrate_range.end / 1000
        )?;

        write!(f, "Frontend capabilities: 0x{:08x}", self.caps)?;

        Ok(())
    }
}

impl AsRawFd for FeDevice {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.file.as_raw_fd()
    }
}

#[macro_export]
macro_rules! get_dtv_properties {
    ( $device:expr, $( $property:ident ),+ ) => { (|| -> ::anyhow::Result<_> {
        let mut input = [ $( $property(DtvPropertyRequest::default()), )* ];
        $device.get_properties(&mut input).context("Error fetching properties")?;
        let mut iterator = input.iter();
        Ok((
            $(
                match iterator.next() {
                    Some($property(d)) => d.get(),
                    _ => ::anyhow::Result::Err(anyhow!("Missing value")),
                }.with_context(|| format!("Error unpacking {}", stringify!($property)))?,
            )*
        ))
    })()}
}

#[macro_export]
macro_rules! set_dtv_properties {
    ( $device:expr, $( $property:ident($data:expr) ),+ ) => {
        $device.set_properties(&[
            $(  $property(DtvPropertyRequest::new($data)), )*
        ])
    };
}

impl FeDevice {
    /// Clears frontend settings and event queue
    pub fn clear(&self) -> Result<()> {
        set_dtv_properties!(
            self,
            DTV_VOLTAGE(SEC_VOLTAGE_OFF),
            DTV_TONE(SEC_TONE_OFF),
            DTV_CLEAR(())
        )
        .context("FE: clear")?;

        let mut event = FeEvent::default();

        for _ in 0..FE_MAX_EVENT {
            if self.get_event(&mut event).is_err() {
                break;
            }
        }

        Ok(())
    }

    fn get_info(&mut self) -> Result<()> {
        let mut feinfo = FeInfo::default();

        // FE_GET_INFO
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            61,
            FeInfo
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut feinfo as *mut _) }.context("FE: get info")?;

        if let Some(len) = feinfo.name.iter().position(|&b| b == 0) {
            let name = unsafe { CStr::from_ptr(feinfo.name[..len + 1].as_ptr()) };
            if let Ok(name) = name.to_str() {
                self.name = name.to_owned();
            }
        }

        self.frequency_range = feinfo.frequency_min..feinfo.frequency_max;
        self.symbolrate_range = feinfo.symbol_rate_min..feinfo.symbol_rate_max;

        self.caps = feinfo.caps;

        // DVB v5 properties
        let (api_version, enum_delsys) =
            get_dtv_properties!(self, DTV_API_VERSION, DTV_ENUM_DELSYS)
                .context("FE: get api version (deprecated driver)")?;

        // DVB API Version
        self.api_version = api_version as u16;

        // Suppoerted delivery systems
        self.delivery_system_list = enum_delsys;

        // dev-file metadata

        let metadata = self.file.metadata().context("FE: get device metadata")?;

        ensure!(
            metadata.file_type().is_char_device(),
            "FE: path is not to char device"
        );

        Ok(())
    }

    fn open(adapter: u32, device: u32, is_write: bool) -> Result<FeDevice> {
        let path = format!("/dev/dvb/adapter{}/frontend{}", adapter, device);
        let file = OpenOptions::new()
            .read(true)
            .write(is_write)
            .custom_flags(::nix::libc::O_NONBLOCK)
            .open(&path)
            .with_context(|| format!("FE: failed to open device {}", &path))?;

        let mut fe = FeDevice {
            adapter,
            device,

            file,

            api_version: 0,

            name: String::default(),
            delivery_system_list: Vec::default(),
            frequency_range: 0..0,
            symbolrate_range: 0..0,
            caps: fe_caps::FE_IS_STUPID,
        };

        fe.get_info()?;

        Ok(fe)
    }

    /// Attempts to open frontend device in read-only mode
    #[inline]
    pub fn open_ro(adapter: u32, device: u32) -> Result<FeDevice> {
        Self::open(adapter, device, false)
    }

    /// Attempts to open frontend device in read-write mode
    #[inline]
    pub fn open_rw(adapter: u32, device: u32) -> Result<FeDevice> {
        Self::open(adapter, device, true)
    }

    fn check_properties(&self, cmdseq: &[DtvProperty]) -> Result<()> {
        for p in cmdseq {
            match p {
                DTV_FREQUENCY(d) => {
                    ensure!(
                        self.frequency_range.contains(&d.get()?),
                        "FE: frequency out of range"
                    );
                }
                DTV_SYMBOL_RATE(d) => {
                    ensure!(
                        self.symbolrate_range.contains(&d.get()?),
                        "FE: symbolrate out of range"
                    );
                }
                DTV_INVERSION(d) => {
                    if d.get()? == INVERSION_AUTO {
                        ensure!(
                            self.caps.contains(fe_caps::FE_CAN_INVERSION_AUTO),
                            "FE: auto inversion is not available"
                        );
                    }
                }
                DTV_TRANSMISSION_MODE(d) => {
                    if d.get()? == TRANSMISSION_MODE_AUTO {
                        ensure!(
                            self.caps.contains(fe_caps::FE_CAN_TRANSMISSION_MODE_AUTO),
                            "FE: no auto transmission mode"
                        );
                    }
                }
                DTV_GUARD_INTERVAL(d) => {
                    if d.get()? == GUARD_INTERVAL_AUTO {
                        ensure!(
                            self.caps.contains(fe_caps::FE_CAN_GUARD_INTERVAL_AUTO),
                            "FE: no auto guard interval"
                        );
                    }
                }
                DTV_HIERARCHY(d) => {
                    if d.get()? == HIERARCHY_AUTO {
                        ensure!(
                            self.caps.contains(fe_caps::FE_CAN_HIERARCHY_AUTO),
                            "FE: no auto hierarchy"
                        );
                    }
                }
                DTV_STREAM_ID(..) => {
                    ensure!(
                        self.caps.contains(fe_caps::FE_CAN_MULTISTREAM),
                        "FE: no multistream"
                    );
                }
                _ => {}
            }
        }

        Ok(())
    }

    /// Sets properties on frontend device
    pub fn set_properties(&self, cmdseq: &[DtvProperty]) -> Result<()> {
        self.check_properties(cmdseq)?;

        #[repr(C)]
        pub struct DtvProperties {
            num: u32,
            props: *const DtvProperty,
        }

        let cmd = DtvProperties {
            num: cmdseq.len() as u32,
            props: cmdseq.as_ptr(),
        };

        // FE_SET_PROPERTY
        ioctl_write_ptr!(
            #[inline]
            ioctl_call,
            b'o',
            82,
            DtvProperties
        );
        unsafe { ioctl_call(self.as_raw_fd(), &cmd as *const _) }.context("FE: set properties")?;

        Ok(())
    }

    /// Gets properties from frontend device
    pub fn get_properties(&self, cmdseq: &mut [DtvProperty]) -> Result<()> {
        #[repr(C)]
        pub struct DtvProperties {
            num: u32,
            props: *mut DtvProperty,
        }

        let mut cmd = DtvProperties {
            num: cmdseq.len() as u32,
            props: cmdseq.as_mut_ptr(),
        };

        // FE_GET_PROPERTY
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            83,
            DtvProperties
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut cmd as *mut _) }
            .context("FE: get properties")?;

        Ok(())
    }

    /// Returns a frontend events if available
    pub fn get_event(&self, event: &mut FeEvent) -> Result<()> {
        // FE_GET_EVENT
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            78,
            FeEvent
        );
        unsafe { ioctl_call(self.as_raw_fd(), event as *mut _) }.context("FE: get event")?;

        Ok(())
    }

    /// Returns frontend status
    /// - [`FE_NONE`]
    /// - [`FE_HAS_SIGNAL`]
    /// - [`FE_HAS_CARRIER`]
    /// - [`FE_HAS_VITERBI`]
    /// - [`FE_HAS_SYNC`]
    /// - [`FE_HAS_LOCK`]
    /// - [`FE_TIMEDOUT`]
    /// - [`FE_REINIT`]
    pub fn read_status(&self) -> Result<fe_status> {
        let mut result: u32 = 0;

        // FE_READ_STATUS
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            69,
            u32
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut result as *mut _) }
            .context("FE: read status")?;

        Ok(fe_status::from_bits(result).context("Invalid status")?)
    }

    /// Reads and returns a signal strength relative value (DVBv3 API)
    pub fn read_signal_strength(&self) -> Result<u16> {
        let mut result: u16 = 0;

        // FE_READ_SIGNAL_STRENGTH
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            71,
            u16
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut result as *mut _) }
            .context("FE: read signal strength")?;

        Ok(result)
    }

    /// Reads and returns a signal-to-noise ratio, relative value (DVBv3 API)
    pub fn read_snr(&self) -> Result<u16> {
        let mut result: u16 = 0;

        // FE_READ_SNR
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            72,
            u16
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut result as *mut _) }.context("FE: read snr")?;

        Ok(result)
    }

    /// Reads and returns a bit error counter (DVBv3 API)
    pub fn read_ber(&self) -> Result<u64> {
        let mut result: u32 = 0;

        // FE_READ_BER
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            70,
            u32
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut result as *mut _) }.context("FE: read ber")?;

        Ok(result as u64)
    }

    /// Reads and returns an uncorrected blocks counter (DVBv3 API)
    pub fn read_unc(&self) -> Result<u64> {
        let mut result: u32 = 0;

        // FE_READ_UNCORRECTED_BLOCKS
        ioctl_read!(
            #[inline]
            ioctl_call,
            b'o',
            73,
            u32
        );
        unsafe { ioctl_call(self.as_raw_fd(), &mut result as *mut _) }
            .context("FE: read uncorrected blocks")?;

        Ok(result as u64)
    }

    /// Turns on/off generation of the continuous 22kHz tone
    ///
    /// allowed `value`'s:
    ///
    /// - SEC_TONE_ON - turn 22kHz on
    /// - SEC_TONE_OFF - turn 22kHz off
    pub fn set_tone(&self, value: u32) -> Result<()> {
        // FE_SET_TONE
        ioctl_write_int_bad!(
            #[inline]
            ioctl_call,
            request_code_none!(b'o', 66)
        );

        unsafe { ioctl_call(self.as_raw_fd(), value as _) }.context("FE: set tone")?;

        Ok(())
    }

    /// Sets the DC voltage level for LNB
    ///
    /// allowed `value`'s:
    ///
    /// - SEC_VOLTAGE_13 for 13V
    /// - SEC_VOLTAGE_18 for 18V
    /// - SEC_VOLTAGE_OFF turns LNB power supply off
    ///
    /// Different power levels used to select internal antennas for different polarizations:
    ///
    /// - 13V:
    ///     - Vertical in linear LNB
    ///     - Right in circular LNB
    /// - 18V:
    ///     - Horizontal in linear LNB
    ///     - Left in circular LNB
    /// - OFF is needed with external power supply, for example
    ///   to use same LNB with several receivers.
    pub fn set_voltage(&self, value: u32) -> Result<()> {
        // FE_SET_VOLTAGE
        ioctl_write_int_bad!(
            #[inline]
            ioctl_call,
            request_code_none!(b'o', 67)
        );

        unsafe { ioctl_call(self.as_raw_fd(), value as _) }.context("FE: set voltage")?;

        Ok(())
    }

    /// Sets DiSEqC master command
    ///
    /// `msg` is a message no more 6 bytes length
    ///
    /// Example DiSEqC commited command:
    ///
    /// ```text
    /// [0xE0, 0x10, 0x38, 0xF0 | value]
    /// ```
    ///
    /// - byte 1 is a framing (master command without response)
    /// - byte 2 is an address (any LNB)
    /// - byte 3 is a command (commited)
    /// - last 4 bits of byte 4 is:
    ///     - xx00 - switch input
    ///     - 00x0 - bit is set on SEC_VOLTAGE_18
    ///     - 000x - bit is set on SEC_TONE_ON
    ///
    pub fn diseqc_master_cmd(&self, msg: &[u8]) -> Result<()> {
        let mut cmd = DiseqcMasterCmd::default();
        debug_assert!(msg.len() <= cmd.msg.len());

        cmd.msg[0..msg.len()].copy_from_slice(msg);
        cmd.len = msg.len() as u8;

        // FE_DISEQC_SEND_MASTER_CMD
        ioctl_write_ptr!(ioctl_call, b'o', 63, DiseqcMasterCmd);
        unsafe { ioctl_call(self.as_raw_fd(), &cmd as *const _) }
            .context("FE: diseqc master cmd")?;

        Ok(())
    }

    /// Returns the current API version
    /// major - first byte
    /// minor - second byte
    #[inline]
    pub fn get_api_version(&self) -> u16 {
        self.api_version
    }
}