Skip to content

Commit

Permalink
Add ability to encode USP Records of type MQTT Connect
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Egger <daniel.egger@axiros.com>
  • Loading branch information
therealprof committed Nov 20, 2023
1 parent bbd1a92 commit a9b1f2f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ enum RuspAction {
/// Output filename of file to encode USP protobuf record to
filename: Option<PathBuf>,
},
/// Encode a USP Record of type MQTT Connect
#[clap(name = "create_mqtt_connect_record")]
CreateMQTTConnectRecord {
#[clap(long = "version", default_value = "1.3")]
/// USP specification version
version: String,
#[clap(long = "from", default_value = "doc::from")]
/// Sender Id
from: String,
#[clap(long = "to", default_value = "doc::to")]
/// Recipient Id
to: String,
/// Indicate that we're using MQTT v3.11 instead of the default MQTT 5
#[clap(short = '4', long = "mqtt311")]
mqtt311: bool,
/// The subscribed topic the MQTT client is expecting to receive the messages for
#[clap(short = 's')]
subscribed_topic: String,
/// Filename (will output to standard output if omitted)
#[clap(short = 'f', long = "file")]
/// Output filename of file to encode USP protobuf record to
filename: Option<PathBuf>,
},
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -870,6 +893,32 @@ fn encode_session_record(
write_record(record, out, &format)
}

fn create_mqtt_connect_record(
version: String,
from: String,
to: String,
filename: Option<PathBuf>,
mqtt311: bool,
subscribed_topic: String,
format: OutputFormat,
) -> Result<()> {
let record = usp_generator::usp_mqtt_connect_record(
&version,
&to,
&from,
PayloadSecurity::PLAINTEXT,
&[],
&[],
mqtt311,
&subscribed_topic,
);

// Open output stream
let out = get_out_stream(filename)?;

write_record(record, out, &format)
}

fn main() -> Result<()> {
let Rusp {
action,
Expand Down Expand Up @@ -971,6 +1020,22 @@ fn main() -> Result<()> {
filename,
format,
),
RuspAction::CreateMQTTConnectRecord {
version,
from,
to,
mqtt311,
subscribed_topic,
filename,
} => create_mqtt_connect_record(
version,
from,
to,
filename,
mqtt311,
subscribed_topic,
format,
),
}?;

Ok(())
Expand Down
55 changes: 55 additions & 0 deletions src/usp_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,61 @@ pub fn usp_session_context_record<'a>(
}
}

/// Create a USP record of type MQTT Connect
///
/// # Arguments
///
/// * `version` - The USP version of the record
/// * `to_id` - The USP Endpoint ID of the receiver
/// * `from_id` - The USP Endpoint ID of the sender
/// * `payload_security` - An enumeration of type `PayloadSecurity`
/// * `mac_signature` - Message authentication code or signature used to ensure the integrity of the
/// non-payload fields, when integrity protection of non-payload fields is performed
/// * `sender_cert` - PEM encoded certificate used to provide the signature in the `mac_signature`
/// field, when the payload security mechanism does not provide the mechanism to do so
/// * `mqtt311` - true if we want to use MQTT v3.11, false for MQTT v5
/// * `subscribed_topic` - The topic the sender can receive responses on
///
/// # Examples
///
/// ```
/// use rusp::usp_generator::usp_mqtt_connect_record;
/// use rusp::usp_types::PayloadSecurity;
///
/// let newrecord = usp_mqtt_connect_record("1.3", "doc::to", "doc::from", PayloadSecurity::PLAINTEXT, &[], &[], false, "/topic/doc::from");
/// ```
pub fn usp_mqtt_connect_record<'a>(
version: &'a str,
to_id: &'a str,
from_id: &'a str,
payload_security: PayloadSecurity,
mac_signature: &'a [u8],
sender_cert: &'a [u8],
mqtt311: bool,
subscribed_topic: &'a str,
) -> Record<'a> {
use crate::usp_record::mod_MQTTConnectRecord::MQTTVersion;
use crate::usp_record::mod_Record::OneOfrecord_type::mqtt_connect;
use crate::usp_record::MQTTConnectRecord;

Record {
version: version.into(),
to_id: to_id.into(),
from_id: from_id.into(),
sender_cert: Cow::Borrowed(sender_cert),
mac_signature: Cow::Borrowed(mac_signature),
payload_security,
record_type: mqtt_connect(MQTTConnectRecord {
version: if mqtt311 {
MQTTVersion::V3_1_1
} else {
MQTTVersion::V5
},
subscribed_topic: subscribed_topic.into(),
}),
}
}

/// Creates a body for a USP Msg with a USP NotifyResp response
///
/// # Arguments
Expand Down

0 comments on commit a9b1f2f

Please sign in to comment.