Skip to content

Commit c954cd3

Browse files
committed
Store temp. mac-command state in device-session.
1 parent 188ef3d commit c954cd3

7 files changed

Lines changed: 147 additions & 237 deletions

File tree

api/proto/internal/internal.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ message DeviceSession {
142142

143143
// Relay state.
144144
Relay relay = 41;
145+
146+
// Pending mac-commands.
147+
map<uint32, bytes> mac_command_pending = 43;
145148
}
146149

147150
message UplinkAdrHistory {

api/rust/proto/chirpstack/internal/internal.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ message DeviceSession {
142142

143143
// Relay state.
144144
Relay relay = 41;
145+
146+
// Pending mac-commands.
147+
map<uint32, bytes> mac_command_pending = 43;
145148
}
146149

147150
message UplinkAdrHistory {

chirpstack/src/downlink/data.rs

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ impl Data {
707707
fn set_phy_payloads(&mut self) -> Result<()> {
708708
trace!("Setting downlink PHYPayloads");
709709
let mut f_pending = self.more_device_queue_items;
710-
let ds = self.device.get_device_session()?;
710+
let dev_addr = self.device.get_dev_addr()?;
711+
let ds = self.device.get_device_session_mut()?;
711712

712713
for item in self.downlink_frame_items.iter_mut() {
713714
let mut mac_size: usize = 0;
@@ -729,6 +730,8 @@ impl Data {
729730
for mac in &**mac_set {
730731
mac_commands.push(mac.clone());
731732
}
733+
734+
mac_command::set_pending(ds, mac_set)?;
732735
}
733736

734737
// LoRaWAN MHDR
@@ -740,7 +743,7 @@ impl Data {
740743
// LoRaWAN MAC payload
741744
let mut mac_pl = lrwn::MACPayload {
742745
fhdr: lrwn::FHDR {
743-
devaddr: self.device.get_dev_addr()?,
746+
devaddr: dev_addr,
744747
f_cnt: ds.n_f_cnt_down,
745748
f_ctrl: lrwn::FCtrl {
746749
adr: !self.network_conf.adr_disabled,
@@ -1196,8 +1199,6 @@ impl Data {
11961199
if let Some(block) =
11971200
maccommand::new_channel::request(3, &current_channels, &wanted_channels)
11981201
{
1199-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::NewChannelReq, &block)
1200-
.await?;
12011202
self.mac_commands.push(block);
12021203
}
12031204

@@ -1207,7 +1208,7 @@ impl Data {
12071208
// Note: this must come before ADR!
12081209
async fn _request_channel_mask_reconfiguration(&mut self) -> Result<()> {
12091210
trace!("Requesting channel-mask reconfiguration");
1210-
let ds = self.device.get_device_session()?;
1211+
let ds = self.device.get_device_session_mut()?;
12111212

12121213
let enabled_uplink_channel_indices: Vec<usize> = ds
12131214
.enabled_uplink_channel_indices
@@ -1239,7 +1240,6 @@ impl Data {
12391240
.collect(),
12401241
);
12411242

1242-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::LinkADRReq, &set).await?;
12431243
self.mac_commands.push(set);
12441244

12451245
Ok(())
@@ -1257,12 +1257,15 @@ impl Data {
12571257
.get_data_rate(self.uplink_frame_set.as_ref().unwrap().dr)?;
12581258

12591259
let ufs = self.uplink_frame_set.as_ref().unwrap();
1260-
let ds = self.device.get_device_session()?;
1260+
let dev_eui = self.device.dev_eui;
1261+
let device_variables = self.device.variables.into_hashmap();
1262+
let ds = self.device.get_device_session_mut()?;
12611263

12621264
let req = adr::Request {
1265+
dev_eui,
1266+
device_variables,
12631267
region_config_id: ufs.region_config_id.clone(),
12641268
region_common_name: ufs.region_common_name,
1265-
dev_eui: self.device.dev_eui,
12661269
mac_version: self.device_profile.mac_version,
12671270
reg_params_revision: self.device_profile.reg_params_revision,
12681271
adr: ds.adr,
@@ -1291,7 +1294,6 @@ impl Data {
12911294
max_dr: self.network_conf.max_dr,
12921295
uplink_history: ds.uplink_adr_history.clone(),
12931296
skip_f_cnt_check: ds.skip_f_cnt_check,
1294-
device_variables: self.device.variables.into_hashmap(),
12951297
};
12961298

12971299
let resp = adr::handle(&self.device_profile.adr_algorithm_id, &req).await;
@@ -1304,24 +1306,14 @@ impl Data {
13041306
{
13051307
let mut adr_set = false;
13061308
for set in self.mac_commands.iter_mut() {
1307-
let mut is_link_adr_set = false;
1308-
13091309
for mac in &mut **set {
13101310
if let lrwn::MACCommand::LinkADRReq(pl) = mac {
13111311
pl.dr = resp.dr;
13121312
pl.tx_power = resp.tx_power_index;
13131313
pl.redundancy.nb_rep = resp.nb_trans;
1314-
13151314
adr_set = true;
1316-
is_link_adr_set = true;
13171315
}
13181316
}
1319-
1320-
if is_link_adr_set {
1321-
// We need to update the pending mac-command.
1322-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::LinkADRReq, set)
1323-
.await?;
1324-
}
13251317
}
13261318

13271319
// There was no existing LinkADRReq to be sent, we need to construct a new one.
@@ -1358,7 +1350,6 @@ impl Data {
13581350
},
13591351
)]);
13601352

1361-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::LinkADRReq, &set).await?;
13621353
self.mac_commands.push(set);
13631354
}
13641355
}
@@ -1406,7 +1397,7 @@ impl Data {
14061397
async fn _request_rejoin_param_setup(&mut self) -> Result<()> {
14071398
trace!("Requesting rejoin param setup");
14081399

1409-
let ds = self.device.get_device_session()?;
1400+
let ds = self.device.get_device_session_mut()?;
14101401

14111402
// Rejoin-request is disabled or device does not support LoRaWAN 1.1.
14121403
if !self.network_conf.rejoin_request.enabled
@@ -1423,8 +1414,6 @@ impl Data {
14231414
self.network_conf.rejoin_request.max_time_n,
14241415
self.network_conf.rejoin_request.max_count_n,
14251416
);
1426-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::RejoinParamSetupReq, &set)
1427-
.await?;
14281417
self.mac_commands.push(set);
14291418
}
14301419

@@ -1434,7 +1423,7 @@ impl Data {
14341423
async fn _set_ping_slot_parameters(&mut self) -> Result<()> {
14351424
trace!("Setting ping-slot parameters");
14361425

1437-
let ds = self.device.get_device_session()?;
1426+
let ds = self.device.get_device_session_mut()?;
14381427

14391428
if !self.device_profile.supports_class_b {
14401429
return Ok(());
@@ -1447,8 +1436,6 @@ impl Data {
14471436
self.network_conf.class_b.ping_slot_dr,
14481437
self.network_conf.class_b.ping_slot_frequency,
14491438
);
1450-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::PingSlotChannelReq, &set)
1451-
.await?;
14521439
self.mac_commands.push(set);
14531440
}
14541441

@@ -1457,7 +1444,7 @@ impl Data {
14571444

14581445
async fn _set_rx_parameters(&mut self) -> Result<()> {
14591446
trace!("Setting rx parameters");
1460-
let ds = self.device.get_device_session()?;
1447+
let ds = self.device.get_device_session_mut()?;
14611448

14621449
if ds.rx2_frequency != self.network_conf.rx2_frequency
14631450
|| ds.rx2_dr as u8 != self.network_conf.rx2_dr
@@ -1468,8 +1455,6 @@ impl Data {
14681455
self.network_conf.rx2_frequency,
14691456
self.network_conf.rx2_dr,
14701457
);
1471-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::RxParamSetupReq, &set)
1472-
.await?;
14731458
self.mac_commands.push(set);
14741459
}
14751460

@@ -1481,8 +1466,6 @@ impl Data {
14811466

14821467
if dev_rx1_delay != req_rx1_delay {
14831468
let set = maccommand::rx_timing_setup::request(req_rx1_delay);
1484-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::RxTimingSetupReq, &set)
1485-
.await?;
14861469
self.mac_commands.push(set);
14871470
}
14881471

@@ -1491,7 +1474,7 @@ impl Data {
14911474

14921475
async fn _set_tx_parameters(&mut self) -> Result<()> {
14931476
trace!("Setting tx parameters");
1494-
let ds = self.device.get_device_session()?;
1477+
let ds = self.device.get_device_session_mut()?;
14951478

14961479
if !self
14971480
.region_conf
@@ -1512,8 +1495,6 @@ impl Data {
15121495
self.network_conf.downlink_dwell_time_400ms,
15131496
uplink_eirp_index,
15141497
);
1515-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::TxParamSetupReq, &set)
1516-
.await?;
15171498
self.mac_commands.push(set);
15181499
}
15191500

@@ -1567,8 +1548,8 @@ impl Data {
15671548
|| rd.uplink_limit_reload_rate
15681549
!= device.relay_ed_uplink_limit_reload_rate as u32
15691550
{
1570-
let d = device::get(&device.dev_eui).await?;
1571-
let ds = match d.get_device_session() {
1551+
let mut d = device::get(&device.dev_eui).await?;
1552+
let ds = match d.get_device_session_mut() {
15721553
Ok(v) => v,
15731554
Err(_) => {
15741555
// It is valid that the device is no longer activated.
@@ -1595,13 +1576,17 @@ impl Data {
15951576
},
15961577
),
15971578
]);
1598-
mac_command::set_pending(
1599-
&dev_eui,
1600-
lrwn::CID::UpdateUplinkListReq,
1601-
&set,
1579+
self.mac_commands.push(set);
1580+
1581+
// Update device-session of device.
1582+
device::partial_update(
1583+
d.dev_eui,
1584+
&device::DeviceChangeset {
1585+
device_session: Some(d.device_session.clone()),
1586+
..Default::default()
1587+
},
16021588
)
16031589
.await?;
1604-
self.mac_commands.push(set);
16051590

16061591
rd.dev_addr = dev_addr.to_vec();
16071592
rd.root_wor_s_key = root_wor_s_key.to_vec();
@@ -1651,8 +1636,6 @@ impl Data {
16511636
root_wor_s_key,
16521637
},
16531638
)]);
1654-
mac_command::set_pending(&dev_eui, lrwn::CID::UpdateUplinkListReq, &set)
1655-
.await?;
16561639
self.mac_commands.push(set);
16571640

16581641
ds.relay
@@ -1788,8 +1771,6 @@ impl Data {
17881771

17891772
if !commands.is_empty() {
17901773
let set = lrwn::MACCommandSet::new(commands);
1791-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::CtrlUplinkListReq, &set)
1792-
.await?;
17931774
self.mac_commands.push(set);
17941775
}
17951776

@@ -1799,7 +1780,6 @@ impl Data {
17991780
async fn _configure_fwd_limit_req(&mut self) -> Result<()> {
18001781
trace!("Configuring Relay Fwd Limit");
18011782

1802-
let dev_eui = self.device.dev_eui;
18031783
let ds = self.device.get_device_session_mut()?;
18041784
let relay_params = self.device_profile.relay_params.clone().unwrap_or_default();
18051785

@@ -1843,7 +1823,6 @@ impl Data {
18431823
},
18441824
},
18451825
)]);
1846-
mac_command::set_pending(&dev_eui, lrwn::CID::ConfigureFwdLimitReq, &set).await?;
18471826
self.mac_commands.push(set);
18481827
}
18491828

@@ -1915,7 +1894,6 @@ impl Data {
19151894
}
19161895

19171896
let set = lrwn::MACCommandSet::new(commands);
1918-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::FilterListReq, &set).await?;
19191897
self.mac_commands.push(set);
19201898

19211899
// The deletes needs to be processed before we can add new entries.
@@ -1944,8 +1922,6 @@ impl Data {
19441922
filter_list_eui: vec![],
19451923
},
19461924
)]);
1947-
mac_command::set_pending(&self.device.dev_eui, lrwn::CID::FilterListReq, &set)
1948-
.await?;
19491925
self.mac_commands.push(set);
19501926

19511927
// Return because we can't add multiple sets and if we would combine
@@ -1977,7 +1953,6 @@ impl Data {
19771953
filter_list_eui: eui,
19781954
},
19791955
)]);
1980-
mac_command::set_pending(&dev_eui, lrwn::CID::FilterListReq, &set).await?;
19811956
self.mac_commands.push(set);
19821957

19831958
f.join_eui = device.join_eui.to_vec();
@@ -2009,7 +1984,6 @@ impl Data {
20091984
filter_list_eui: eui,
20101985
},
20111986
)]);
2012-
mac_command::set_pending(&dev_eui, lrwn::CID::FilterListReq, &set).await?;
20131987
self.mac_commands.push(set);
20141988

20151989
ds.relay
@@ -2037,7 +2011,6 @@ impl Data {
20372011
async fn _update_relay_conf(&mut self) -> Result<()> {
20382012
trace!("Updating Relay Conf");
20392013

2040-
let dev_eui = self.device.dev_eui;
20412014
let ds = self.device.get_device_session_mut()?;
20422015
let relay_params = self.device_profile.relay_params.clone().unwrap_or_default();
20432016

@@ -2075,7 +2048,6 @@ impl Data {
20752048
second_ch_freq: relay_params.second_channel_freq,
20762049
},
20772050
)]);
2078-
mac_command::set_pending(&dev_eui, lrwn::CID::RelayConfReq, &set).await?;
20792051
self.mac_commands.push(set);
20802052
}
20812053

@@ -2087,7 +2059,6 @@ impl Data {
20872059
async fn _update_end_device_conf(&mut self) -> Result<()> {
20882060
trace!("Updating End Device Conf");
20892061

2090-
let dev_eui = self.device.dev_eui;
20912062
let ds = self.device.get_device_session_mut()?;
20922063
let relay_params = self.device_profile.relay_params.clone().unwrap_or_default();
20932064

@@ -2124,7 +2095,6 @@ impl Data {
21242095
second_ch_freq: relay_params.second_channel_freq,
21252096
},
21262097
)]);
2127-
mac_command::set_pending(&dev_eui, lrwn::CID::EndDeviceConfReq, &set).await?;
21282098
self.mac_commands.push(set);
21292099
}
21302100

chirpstack/src/maccommand/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,14 @@ pub async fn handle_uplink(
7777
);
7878

7979
// Get pending mac-command block, this could return None.
80-
let pending = match mac_command::get_pending(&dev.dev_eui, cid).await {
80+
let pending = match mac_command::get_pending(dev.get_device_session_mut()?, cid).await {
8181
Ok(v) => v,
8282
Err(e) => {
8383
error!(dev_eui = %dev.dev_eui, cid = %cid, error = %e, "Get pending mac-command block error");
8484
continue;
8585
}
8686
};
8787

88-
// Delete the pending mac-command.
89-
if pending.is_some() {
90-
if let Err(e) = mac_command::delete_pending(&dev.dev_eui, cid).await {
91-
error!(dev_eui = %dev.dev_eui, cid = %cid, error = %e, "Delete pending mac-command error");
92-
}
93-
}
94-
9588
// Handle the mac-command, which might return a block to answer the uplink mac-command
9689
// request.
9790
let res = match handle(

0 commit comments

Comments
 (0)