mirror of
https://github.com/chirpstack/chirpstack.git
synced 2025-04-08 20:04:19 +00:00
Store and increase TS004 session_cnt per device.
For TS004 v2.0.0, the session_cnt must be incremented for every fragmentation-session.
This commit is contained in:
parent
c130be9dd0
commit
236b468aa4
@ -1,3 +1,6 @@
|
||||
alter table device
|
||||
drop column app_layer_params;
|
||||
|
||||
alter table device_keys
|
||||
drop column gen_app_key;
|
||||
|
||||
|
@ -73,3 +73,9 @@ alter table device_keys
|
||||
|
||||
alter table device_keys
|
||||
alter column gen_app_key drop default;
|
||||
|
||||
alter table device
|
||||
add column app_layer_params jsonb not null default '{}';
|
||||
|
||||
alter table device
|
||||
alter column app_layer_params drop default;
|
||||
|
@ -1,3 +1,6 @@
|
||||
alter table device
|
||||
drop column app_layer_params;
|
||||
|
||||
alter table device_keys
|
||||
drop column gen_app_key;
|
||||
|
||||
|
@ -70,3 +70,6 @@ create index idx_fuota_deployment_job_scheduler_run_after on fuota_deployment_jo
|
||||
|
||||
alter table device_keys
|
||||
add column gen_app_key blob not null default x'00000000000000000000000000000000';
|
||||
|
||||
alter table device
|
||||
add column all_layer_params text not null default '{}';
|
||||
|
@ -397,6 +397,20 @@ impl Flow {
|
||||
)
|
||||
.to_vec()?,
|
||||
Some(Ts004Version::V200) => {
|
||||
let dev = device::get(&fuota_dev.dev_eui).await?;
|
||||
let session_cnt = dev.app_layer_params.ts004_session_cnt[0];
|
||||
let mut app_layer_params = dev.app_layer_params.clone();
|
||||
app_layer_params.ts004_session_cnt[0] += 1;
|
||||
|
||||
device::partial_update(
|
||||
fuota_dev.dev_eui,
|
||||
&device::DeviceChangeset {
|
||||
app_layer_params: Some(app_layer_params),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let dev_keys = device_keys::get(&fuota_dev.dev_eui).await?;
|
||||
let data_block_int_key = match self.device_profile.mac_version {
|
||||
MacVersion::LORAWAN_1_0_0
|
||||
@ -412,7 +426,7 @@ impl Flow {
|
||||
};
|
||||
let mic = fragmentation::v2::calculate_mic(
|
||||
data_block_int_key,
|
||||
0,
|
||||
session_cnt,
|
||||
0,
|
||||
[0, 0, 0, 0],
|
||||
&self.fuota_deployment.payload,
|
||||
@ -434,7 +448,7 @@ impl Flow {
|
||||
},
|
||||
descriptor: [0, 0, 0, 0],
|
||||
mic,
|
||||
session_cnt: 0,
|
||||
session_cnt,
|
||||
},
|
||||
)
|
||||
.to_vec()?
|
||||
|
@ -116,6 +116,7 @@ pub struct Device {
|
||||
pub join_eui: EUI64,
|
||||
pub secondary_dev_addr: Option<DevAddr>,
|
||||
pub device_session: Option<fields::DeviceSession>,
|
||||
pub app_layer_params: fields::device::AppLayerParams,
|
||||
}
|
||||
|
||||
#[derive(AsChangeset, Debug, Clone, Default)]
|
||||
@ -133,6 +134,7 @@ pub struct DeviceChangeset {
|
||||
pub battery_level: Option<Option<fields::BigDecimal>>,
|
||||
pub scheduler_run_after: Option<Option<DateTime<Utc>>>,
|
||||
pub is_disabled: Option<bool>,
|
||||
pub app_layer_params: Option<fields::device::AppLayerParams>,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
@ -190,6 +192,7 @@ impl Default for Device {
|
||||
join_eui: EUI64::default(),
|
||||
secondary_dev_addr: None,
|
||||
device_session: None,
|
||||
app_layer_params: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -552,6 +555,7 @@ pub async fn update(d: Device) -> Result<Device, Error> {
|
||||
device::tags.eq(&d.tags),
|
||||
device::variables.eq(&d.variables),
|
||||
device::join_eui.eq(&d.join_eui),
|
||||
device::app_layer_params.eq(&d.app_layer_params),
|
||||
))
|
||||
.get_result(&mut get_async_db_conn().await?)
|
||||
.await
|
||||
|
59
chirpstack/src/storage/fields/device.rs
Normal file
59
chirpstack/src/storage/fields/device.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use diesel::backend::Backend;
|
||||
use diesel::{deserialize, serialize};
|
||||
#[cfg(feature = "postgres")]
|
||||
use diesel::{pg::Pg, sql_types::Jsonb};
|
||||
#[cfg(feature = "sqlite")]
|
||||
use diesel::{sql_types::Text, sqlite::Sqlite};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, AsExpression, FromSqlRow)]
|
||||
#[cfg_attr(feature = "postgres", diesel(sql_type = Jsonb))]
|
||||
#[cfg_attr(feature = "sqlite", diesel(sql_type = Text))]
|
||||
#[serde(default)]
|
||||
pub struct AppLayerParams {
|
||||
pub ts004_session_cnt: [u16; 4],
|
||||
}
|
||||
|
||||
impl Default for AppLayerParams {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ts004_session_cnt: [0, 0, 0, 0],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
impl deserialize::FromSql<Jsonb, Pg> for AppLayerParams {
|
||||
fn from_sql(value: <Pg as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
|
||||
let value = <serde_json::Value as deserialize::FromSql<Jsonb, Pg>>::from_sql(value)?;
|
||||
Ok(serde_json::from_value(value)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
impl serialize::ToSql<Jsonb, Pg> for AppLayerParams {
|
||||
fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, Pg>) -> serialize::Result {
|
||||
let value = serde_json::to_value(&self)?;
|
||||
<serde_json::Value as serialize::ToSql<Jsonb, Pg>>::to_sql(&value, &mut out.reborrow())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
impl deserialize::FromSql<Text, Sqlite> for AppLayerParams
|
||||
where
|
||||
*const str: deserialize::FromSql<Text, Sqlite>,
|
||||
{
|
||||
fn from_sql(value: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
|
||||
let s =
|
||||
<*const str as deserialize::FromSql<diesel::sql_types::Text, Sqlite>>::from_sql(value)?;
|
||||
Ok(serde_json::from_str(unsafe { &*s })?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
impl serialize::ToSql<Text, Sqlite> for AppLayerParams {
|
||||
fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, Sqlite>) -> serialize::Result {
|
||||
out.set_value(serde_json::to_string(&self)?);
|
||||
Ok(serialize::IsNull::No)
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
mod big_decimal;
|
||||
mod dev_nonces;
|
||||
pub mod device;
|
||||
pub mod device_profile;
|
||||
mod device_session;
|
||||
mod fuota;
|
||||
|
@ -65,6 +65,7 @@ diesel::table! {
|
||||
join_eui -> Bytea,
|
||||
secondary_dev_addr -> Nullable<Bytea>,
|
||||
device_session -> Nullable<Bytea>,
|
||||
app_layer_params -> Jsonb,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,7 @@ diesel::table! {
|
||||
join_eui -> Binary,
|
||||
secondary_dev_addr -> Nullable<Binary>,
|
||||
device_session -> Nullable<Binary>,
|
||||
all_layer_params -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user