Skip to content

Commit 59b0717

Browse files
committed
Pass all qualification tests in CI
1 parent bb3e567 commit 59b0717

File tree

10 files changed

+121
-29
lines changed

10 files changed

+121
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.gdb_history
44
Cargo.lock
55
target/
6+
device_advisor_integration.log

mqttrust_core/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ defmt = { version = "^0.3", optional = true }
4040
[dev-dependencies]
4141
native-tls = { version = "^0.2" }
4242
dns-lookup = "1.0.3"
43-
env_logger = "0.9.0"
43+
env_logger = "0.11"
44+
static_cell = "2.1"
4445

4546
[features]
4647
default = []
4748

4849
std = []
4950

50-
defmt-impl = ["defmt", "mqttrust/defmt-impl", "heapless/defmt-impl", "fugit/defmt"]
51+
log = ["dep:log", "mqttrust/log"]
52+
53+
defmt-impl = ["dep:defmt", "mqttrust/defmt-impl", "heapless/defmt-impl", "fugit/defmt"]

mqttrust_core/examples/aws_device_advisor.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,49 @@ use mqttrust_core::{EventLoop, MqttOptions, Notification};
88
use common::clock::SysClock;
99
use common::network::Network;
1010
use native_tls::TlsConnector;
11+
use static_cell::StaticCell;
12+
use std::sync::atomic::AtomicBool;
1113
use std::sync::Arc;
1214
use std::thread;
1315

1416
use crate::common::credentials;
1517

16-
static mut Q: BBBuffer<{ 1024 * 6 }> = BBBuffer::new();
18+
static mut Q: BBBuffer<{ 1024 * 60 }> = BBBuffer::new();
1719

1820
fn main() {
1921
env_logger::init();
2022

2123
let (p, c) = unsafe { Q.try_split_framed().unwrap() };
2224

23-
let hostname = credentials::HOSTNAME.unwrap();
25+
static HOSTNAME: StaticCell<String> = StaticCell::new();
26+
let hostname = HOSTNAME.init(credentials::hostname());
27+
28+
log::info!(
29+
"Starting device advisor test on endpoint {}",
30+
hostname.as_str()
31+
);
2432

2533
let connector = TlsConnector::builder()
2634
.identity(credentials::identity())
2735
.add_root_certificate(credentials::root_ca())
2836
.build()
2937
.unwrap();
3038

31-
let mut network = Network::new_tls(connector, String::from(hostname));
39+
let mut network = Network::new_tls(connector, hostname.clone());
3240

3341
let thing_name = "mqttrust";
3442

3543
let mut mqtt_eventloop = EventLoop::new(
3644
c,
3745
SysClock::new(),
38-
MqttOptions::new(thing_name, hostname.into(), 8883),
46+
MqttOptions::new(thing_name, hostname.as_str().into(), 8883),
3947
);
4048

4149
let mqtt_client = mqttrust_core::Client::new(p, thing_name);
4250

51+
let connected = Arc::new(AtomicBool::new(false));
52+
let con = connected.clone();
53+
4354
thread::Builder::new()
4455
.name("eventloop".to_string())
4556
.spawn(move || loop {
@@ -49,12 +60,18 @@ fn main() {
4960
session_present,
5061
code: ConnectReturnCode::Accepted,
5162
}))) => {
52-
log::info!("Successfully connected to broker");
63+
log::info!(
64+
"Successfully connected to broker. session_present: {}",
65+
session_present
66+
);
67+
con.store(true, std::sync::atomic::Ordering::Release);
68+
}
69+
Ok(n) => {
70+
log::info!("Received {:?} during connect", n);
5371
}
54-
Ok(_) => {}
5572
}
5673

57-
match mqtt_eventloop.yield_event(&mut network) {
74+
match nb::block!(mqtt_eventloop.yield_event(&mut network)) {
5875
Ok(Notification::Publish(_)) => {}
5976
Ok(n) => {
6077
log::trace!("{:?}", n);
@@ -66,19 +83,21 @@ fn main() {
6683

6784
loop {
6885
thread::sleep(std::time::Duration::from_millis(5000));
69-
mqtt_client
70-
.subscribe(&[SubscribeTopic {
71-
topic_path: format!("plc/output/{}", thing_name).as_str(),
72-
qos: QoS::AtLeastOnce,
73-
}])
74-
.unwrap();
75-
76-
mqtt_client
77-
.publish(
78-
format!("plc/input/{}", thing_name).as_str(),
79-
format!("Hello from {}", thing_name).as_bytes(),
80-
QoS::AtLeastOnce,
81-
)
82-
.unwrap();
86+
if connected.load(std::sync::atomic::Ordering::Acquire) {
87+
mqtt_client
88+
.subscribe(&[SubscribeTopic {
89+
topic_path: format!("plc/output/{}", thing_name).as_str(),
90+
qos: QoS::AtLeastOnce,
91+
}])
92+
.unwrap();
93+
94+
mqtt_client
95+
.publish(
96+
format!("plc/input/{}", thing_name).as_str(),
97+
format!("Hello from {}", thing_name).as_bytes(),
98+
QoS::AtLeastOnce,
99+
)
100+
.unwrap();
101+
}
83102
}
84103
}

mqttrust_core/examples/common/clock.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ use std::{
55
};
66
pub struct SysClock {
77
start_time: u32,
8-
countdown_end: Option<u32>,
98
}
109

1110
impl SysClock {
1211
pub fn new() -> Self {
1312
Self {
1413
start_time: Self::epoch(),
15-
countdown_end: None,
1614
}
1715
}
1816

mqttrust_core/examples/common/credentials.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ pub fn root_ca() -> Certificate {
1111
Certificate::from_pem(include_bytes!("../secrets/root-ca.pem")).unwrap()
1212
}
1313

14-
pub const HOSTNAME: Option<&'static str> = option_env!("AWS_HOSTNAME");
14+
pub fn hostname() -> String {
15+
env::var("AWS_HOSTNAME").unwrap()
16+
}
0 Bytes
Binary file not shown.

mqttrust_core/src/eventloop.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ where
219219
}
220220

221221
fn backoff(&self) -> TimerDurationU32<TIMER_HZ> {
222-
let base_time_ms: u32 = 1000;
222+
let base_time_ms: u32 = 200;
223223
let backoff = base_time_ms.saturating_mul(u32::pow(2, self.connect_counter as u32));
224224

225225
core::cmp::min(50.secs(), backoff.millis())
@@ -265,14 +265,15 @@ where
265265
MqttConnectionStatus::Handshake => {
266266
let now = self.last_outgoing_timer.now();
267267

268-
let backoff_time = core::cmp::max(50.secs(), self.backoff());
268+
let backoff_time = self.backoff();
269269

270270
if self
271271
.state
272272
.last_ping_entry()
273273
.or_insert(now)
274274
.has_elapsed(&now, backoff_time)
275275
{
276+
warn!("Timed out waiting for connect packet!");
276277
return Err(nb::Error::Other(EventError::Timeout));
277278
}
278279

@@ -562,6 +563,7 @@ impl<'a> PacketDecoder<'a> {
562563
Err(EventError::Encoding(e).into())
563564
}
564565
Ok(Some(packet)) => {
566+
warn!("Got packet! {:?}", packet);
565567
self.is_err.replace(false);
566568
state
567569
.handle_incoming_packet(packet)

scripts/da_monitor.sh

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ function report_status {
4444
done
4545
}
4646

47+
function check_pid {
48+
if [ -n "$pid" ]; then
49+
ps -p $pid > /dev/null;
50+
return $?
51+
else
52+
return 0
53+
fi
54+
}
55+
4756
while test ${IN_PROGRESS} == 1; do
4857
# Fetch the current status and stash in /tmp so we can use it throughout the status fetch process.
4958

@@ -68,13 +77,16 @@ while test ${IN_PROGRESS} == 1; do
6877
elif test x"${overall_status}" == x${STATUS_PASS}; then
6978
MONITOR_STATUS=0
7079
IN_PROGRESS=0
80+
elif test x"${overall_status}" == x${STATUS_PASS_WITH_WARNINGS}; then
81+
MONITOR_STATUS=0
82+
IN_PROGRESS=0
7183
elif test x"${overall_status}" == x${STATUS_STOPPING}; then
7284
MONITOR_STATUS=1
7385
IN_PROGRESS=0
7486
elif test x"${overall_status}" == x${STATUS_STOPPED}; then
7587
MONITOR_STATUS=1
7688
IN_PROGRESS=0
77-
elif { ps -p $pid > /dev/null; }; [ "$?" = 1 ]; then
89+
elif check_pid; [ "$?" = 1 ]; then
7890
echo Binary is not running any more?
7991

8092
MONITOR_STATUS=1

scripts/da_run_test.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ -z "$DEVICE_ADVISOR_PASSWORD" ]]; then
4+
echo "DEVICE_ADVISOR_PASSWORD environment variable must be set!"
5+
return 1;
6+
fi
7+
8+
set -e
9+
10+
DAEMONIZE=true
11+
12+
THING_NAME="mqttrust"
13+
SUITE_ID="1gaev57dq6i5"
14+
export RUST_LOG=debug
15+
16+
THING_ARN="arn:aws:iot:eu-west-1:411974994697:thing/$THING_NAME"
17+
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
18+
19+
20+
export AWS_HOSTNAME=$(aws iotdeviceadvisor get-endpoint --thing-arn $THING_ARN --output text --query endpoint)
21+
22+
cargo build --features=log --example aws_device_advisor --release
23+
24+
SUITE_RUN_ID=$(aws iotdeviceadvisor start-suite-run --suite-definition-id $SUITE_ID --suite-run-configuration "primaryDevice={thingArn=$THING_ARN},parallelRun=true" --output text --query suiteRunId)
25+
if $DAEMONIZE; then
26+
nohup ./target/release/examples/aws_device_advisor > device_advisor_integration.log &
27+
PID=$!
28+
else
29+
echo "You can now run 'DEVICE_ADVISOR_PASSWORD=$DEVICE_ADVISOR_PASSWORD AWS_HOSTNAME=$AWS_HOSTNAME ./target/release/examples/aws_device_advisor' in a seperate terminal"
30+
fi
31+
32+
always() {
33+
kill $PID || true
34+
cat device_advisor_integration.log
35+
}
36+
37+
on_failure() {
38+
if $DAEMONIZE; then
39+
always || true
40+
fi
41+
aws iotdeviceadvisor stop-suite-run --suite-definition-id $SUITE_ID --suite-run-id $SUITE_RUN_ID
42+
}
43+
44+
trap "on_failure" ERR INT
45+
46+
$SCRIPT_DIR/da_monitor.sh $SUITE_ID $SUITE_RUN_ID $PID
47+
48+
always
49+

scripts/rotate_secrets.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ CERT_PATH="$SECRETS_DIR/cert.pem"
1313
PRIV_KEY_PATH="$SECRETS_DIR/priv.key.pem"
1414

1515
CERT_ARN=$(aws iot create-keys-and-certificate --set-as-active --certificate-pem-outfile $CERT_PATH --private-key-outfile $PRIV_KEY_PATH | jq -r .certificateArn);
16+
for OLD_CERT in $(aws iot list-thing-principals --thing-name $THING_NAME | jq -r '.principals[]' | xargs); do
17+
CERT_ID=$(echo $OLD_CERT | cut -d "/" -f 2)
18+
aws iot detach-thing-principal --thing-name $THING_NAME --principal $OLD_CERT
19+
aws iot update-certificate --new-status INACTIVE --certificate-id $CERT_ID
20+
aws iot delete-certificate --certificate-id $CERT_ID --force-delete
21+
done
1622
aws iot attach-thing-principal --thing-name $THING_NAME --principal $CERT_ARN > /dev/null 2>&1
1723
aws iot attach-policy --policy-name Connect --target $CERT_ARN > /dev/null 2>&1
1824
aws iot attach-policy --policy-name Input --target $CERT_ARN > /dev/null 2>&1

0 commit comments

Comments
 (0)