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
use std::io::{Read, Write};
use std::fmt;

use crate::{
    encoding::*,
    date_time::*,
    status_codes::StatusCode,
};

/// This primitive data type is a UInt32 that is used as an identifier, such as a handle.
/// All values, except for 0, are valid. IntegerId = 288,
pub type IntegerId = u32;

const MESSAGE_SECURITY_MODE_NONE: &str = "None";
const MESSAGE_SECURITY_MODE_SIGN: &str = "Sign";
const MESSAGE_SECURITY_MODE_SIGN_AND_ENCRYPT: &str = "SignAndEncrypt";

/// The MessageSecurityMode is an enumeration that specifies what security should be applied to messages exchanges during a Session.
#[derive(Debug, Clone, PartialEq, PartialOrd, Copy)]
pub enum MessageSecurityMode {
    /// The MessageSecurityMode is invalid. This value is the default value to ensure that unless
    /// explicitly set, a connection will be rejected.
    Invalid = 0,
    /// No security is applied. This assumes a plaintext security policy.
    None = 1,
    /// All messages are signed but not encrypted.
    Sign = 2,
    /// All messages are signed and encrypted.
    SignAndEncrypt = 3,
}

impl BinaryEncoder<MessageSecurityMode> for MessageSecurityMode {
    fn byte_len(&self) -> usize {
        4
    }

    fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
        // All enums are Int32
        write_i32(stream, *self as i32)
    }

    fn decode<S: Read>(stream: &mut S, _: &DecodingLimits) -> EncodingResult<Self> {
        // All enums are Int32
        let value = read_i32(stream)?;
        Ok(match value {
            0 => MessageSecurityMode::Invalid,
            1 => MessageSecurityMode::None,
            2 => MessageSecurityMode::Sign,
            3 => MessageSecurityMode::SignAndEncrypt,
            _ => {
                error!("Mode value is invalid = {}", value);
                MessageSecurityMode::Invalid
            }
        })
    }
}

impl fmt::Display for MessageSecurityMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            MessageSecurityMode::None => MESSAGE_SECURITY_MODE_NONE,
            MessageSecurityMode::Sign => MESSAGE_SECURITY_MODE_SIGN,
            MessageSecurityMode::SignAndEncrypt => MESSAGE_SECURITY_MODE_SIGN_AND_ENCRYPT,
            _ => "",
        };
        write!(f, "{}", name)
    }
}

impl From<MessageSecurityMode> for String {
    fn from(security_mode: MessageSecurityMode) -> Self {
        String::from(
            match security_mode {
                MessageSecurityMode::None => MESSAGE_SECURITY_MODE_NONE,
                MessageSecurityMode::Sign => MESSAGE_SECURITY_MODE_SIGN,
                MessageSecurityMode::SignAndEncrypt => MESSAGE_SECURITY_MODE_SIGN_AND_ENCRYPT,
                _ => "",
            }
        )
    }
}

impl<'a> From<&'a str> for MessageSecurityMode {
    fn from(str: &'a str) -> Self {
        match str {
            MESSAGE_SECURITY_MODE_NONE => MessageSecurityMode::None,
            MESSAGE_SECURITY_MODE_SIGN => MessageSecurityMode::Sign,
            MESSAGE_SECURITY_MODE_SIGN_AND_ENCRYPT => MessageSecurityMode::SignAndEncrypt,
            _ => {
                error!("Specified security mode {} is not recognized", str);
                MessageSecurityMode::Invalid
            }
        }
    }
}

/// This Simple DataType is a Double that defines an interval of time in milliseconds (fractions can
/// be used to define sub-millisecond values). Negative values are generally invalid but may have
/// special meanings where the Duration is used. Duration = 290,
pub type Duration = f64;

/// UtcTime = 294,
pub type UtcTime = DateTime;

#[derive(Debug, Clone, PartialEq, Copy, Serialize)]
pub enum MonitoringMode {
    Disabled = 0,
    Sampling = 1,
    Reporting = 2,
}

impl BinaryEncoder<MonitoringMode> for MonitoringMode {
    fn byte_len(&self) -> usize {
        4
    }

    fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
        // All enums are Int32
        write_i32(stream, *self as i32)
    }

    fn decode<S: Read>(stream: &mut S, _: &DecodingLimits) -> EncodingResult<Self> {
        // All enums are Int32
        let value = read_i32(stream)?;
        match value {
            0 => Ok(MonitoringMode::Disabled),
            1 => Ok(MonitoringMode::Sampling),
            2 => Ok(MonitoringMode::Reporting),
            _ => {
                error!("Don't know what monitoring mode {} is", value);
                Err(StatusCode::BadDecodingError)
            }
        }
    }
}