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
use std::fs::File;

use futures::channel::mpsc::{Receiver, Sender};

use crate::control_pipe_runtime::ControlPipeRuntime;

/// Interface toolbar Control commands
#[derive(Debug)]
pub enum ControlCmd {
    /// commandControlInitialized
    Initialized,
    /// commandControlSet
    Set,
    /// commandControlAdd
    Add,
    /// commandControlRemove
    Remove,
    /// commandControlEnable
    Enable,
    /// commandControlDisable
    Disable,
    /// commandStatusMessage
    StatusbarMessage,
    /// commandInformationMessage
    InformationMessage,
    /// commandWarningMessage
    WarningMessage,
    /// commandErrorMessage
    ErrorMessage,
    /// Unknown
    Unknown(u8),
}

impl From<u8> for ControlCmd {
    fn from(val: u8) -> Self {
        match val {
            0 => ControlCmd::Initialized,
            1 => ControlCmd::Set,
            2 => ControlCmd::Add,
            3 => ControlCmd::Remove,
            4 => ControlCmd::Enable,
            5 => ControlCmd::Disable,
            6 => ControlCmd::StatusbarMessage,
            7 => ControlCmd::InformationMessage,
            8 => ControlCmd::WarningMessage,
            9 => ControlCmd::ErrorMessage,
            v => ControlCmd::Unknown(v),
        }
    }
}

impl From<&ControlCmd> for u8 {
    fn from(val: &ControlCmd) -> Self {
        match val {
            ControlCmd::Initialized => 0,
            ControlCmd::Set => 1,
            ControlCmd::Add => 2,
            ControlCmd::Remove => 3,
            ControlCmd::Enable => 4,
            ControlCmd::Disable => 5,
            ControlCmd::StatusbarMessage => 6,
            ControlCmd::InformationMessage => 7,
            ControlCmd::WarningMessage => 8,
            ControlCmd::ErrorMessage => 9,
            ControlCmd::Unknown(v) => *v,
        }
    }
}

/// Control protocol message
#[derive(Debug)]
pub struct ControlMsg {
    ctrl_num: u8,
    command: ControlCmd,
    data: Vec<u8>,
}

impl ControlMsg {
    /// Creates a new instance of `ControlMsg`
    pub fn new(ctrl_num: u8, command: ControlCmd, data: &[u8]) -> Self {
        Self {
            ctrl_num,
            command,
            data: data.into(),
        }
    }

    /// Get the Control number
    pub fn get_ctrl_num(&self) -> u8 {
        self.ctrl_num
    }

    /// Get the Control command type
    pub fn get_command(&self) -> &ControlCmd {
        &self.command
    }

    /// Get the data
    pub fn get_data(&self) -> &[u8] {
        &self.data
    }
}

/// Control pipes
pub type CtrlPipes = (Receiver<ControlMsg>, Sender<ControlMsg>);

pub struct ControlPipe {
    runtime: ControlPipeRuntime,
}

impl ControlPipe {
    pub(crate) fn new(pipe_in: File, pipe_out: File) -> Self {
        Self {
            runtime: ControlPipeRuntime::new(pipe_in, pipe_out),
        }
    }

    pub(crate) fn start(&mut self) -> CtrlPipes {
        self.runtime.start()
    }

    pub(crate) fn stop(self) {
        self.runtime.stop();
    }
}