diff --git a/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/down.sql b/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/down.sql index 61ed3b9b..a205e644 100644 --- a/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/down.sql +++ b/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/down.sql @@ -86,5 +86,5 @@ alter table device_profile drop column abp_params, drop column class_b_params, drop column class_c_params, - drop column relay_params; - + drop column relay_params, + drop column app_layer_params; diff --git a/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/up.sql b/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/up.sql index fc637cde..65213704 100644 --- a/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/up.sql +++ b/chirpstack/migrations_postgres/2025-01-13-152218_refactor_device_profile_fields/up.sql @@ -2,7 +2,11 @@ alter table device_profile add column abp_params jsonb null, add column class_b_params jsonb null, add column class_c_params jsonb null, - add column relay_params jsonb null; + add column relay_params jsonb null, + add column app_layer_params jsonb not null default '{}'; + +alter table device_profile + alter column app_layer_params drop default; update device_profile set abp_params = json_build_object( @@ -46,7 +50,7 @@ update device_profile 'relay_notify_limit_reload_rate', relay_notify_limit_reload_rate, 'relay_global_uplink_limit_reload_rate', relay_global_uplink_limit_reload_rate, 'relay_overall_limit_reload_rate', relay_overall_limit_reload_rate, - 'relay_notify_limit_bucket_size', relay_join_req_limit_bucket_size, + 'relay_join_req_limit_bucket_size', relay_join_req_limit_bucket_size, 'relay_notify_limit_bucket_size', relay_notify_limit_bucket_size, 'relay_global_uplink_limit_bucket_size', relay_global_uplink_limit_bucket_size, 'relay_overall_limit_bucket_size', relay_overall_limit_bucket_size) diff --git a/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/down.sql b/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/down.sql index 3c3f9667..d84eb90e 100644 --- a/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/down.sql +++ b/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/down.sql @@ -85,4 +85,5 @@ alter table device_profile drop column abp_params; alter table device_profile drop column class_b_params; alter table device_profile drop column class_c_params; alter table device_profile drop column relay_params; +alter table device_profile drop column app_layer_params; diff --git a/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/up.sql b/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/up.sql index c6402c5b..7ca6794f 100644 --- a/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/up.sql +++ b/chirpstack/migrations_sqlite/2025-01-13-163304_refactor_device_profile_fields/up.sql @@ -2,6 +2,7 @@ alter table device_profile add column abp_params text null; alter table device_profile add column class_b_params text null; alter table device_profile add column class_c_params text null; alter table device_profile add column relay_params text null; +alter table device_profile add column app_layer_params text not null default '{}'; update device_profile set abp_params = json_object( @@ -44,7 +45,7 @@ update device_profile 'relay_notify_limit_reload_rate', relay_notify_limit_reload_rate, 'relay_global_uplink_limit_reload_rate', relay_global_uplink_limit_reload_rate, 'relay_overall_limit_reload_rate', relay_overall_limit_reload_rate, - 'relay_notify_limit_bucket_size', relay_join_req_limit_bucket_size, + 'relay_join_req_limit_bucket_size', relay_join_req_limit_bucket_size, 'relay_notify_limit_bucket_size', relay_notify_limit_bucket_size, 'relay_global_uplink_limit_bucket_size', relay_global_uplink_limit_bucket_size, 'relay_overall_limit_bucket_size', relay_overall_limit_bucket_size) diff --git a/chirpstack/src/storage/device_profile.rs b/chirpstack/src/storage/device_profile.rs index 06dbcb9b..ad4598c6 100644 --- a/chirpstack/src/storage/device_profile.rs +++ b/chirpstack/src/storage/device_profile.rs @@ -47,6 +47,7 @@ pub struct DeviceProfile { pub class_b_params: Option, pub class_c_params: Option, pub relay_params: Option, + pub app_layer_params: fields::AppLayerParams, } impl DeviceProfile { @@ -96,6 +97,7 @@ impl Default for DeviceProfile { class_b_params: None, class_c_params: None, relay_params: None, + app_layer_params: fields::AppLayerParams::default(), } } } @@ -208,6 +210,7 @@ pub async fn update(dp: DeviceProfile) -> Result { device_profile::class_b_params.eq(&dp.class_b_params), device_profile::class_c_params.eq(&dp.class_c_params), device_profile::relay_params.eq(&dp.relay_params), + device_profile::app_layer_params.eq(&dp.app_layer_params), )) .get_result(&mut get_async_db_conn().await?) .await diff --git a/chirpstack/src/storage/fields/device_profile.rs b/chirpstack/src/storage/fields/device_profile.rs index b079b114..65dc0ca2 100644 --- a/chirpstack/src/storage/fields/device_profile.rs +++ b/chirpstack/src/storage/fields/device_profile.rs @@ -234,3 +234,68 @@ mod ed_activation_mode { Ok(v) } } + +#[derive( + Default, Debug, Clone, PartialEq, Eq, Deserialize, Serialize, AsExpression, FromSqlRow, +)] +#[cfg_attr(feature = "postgres", diesel(sql_type = Jsonb))] +#[cfg_attr(feature = "sqlite", diesel(sql_type = Text))] +pub struct AppLayerParams { + pub ts003_version: Option, + pub ts004_version: Option, + pub ts005_version: Option, +} + +#[cfg(feature = "postgres")] +impl deserialize::FromSql for AppLayerParams { + fn from_sql(value: ::RawValue<'_>) -> deserialize::Result { + let value = >::from_sql(value)?; + Ok(serde_json::from_value(value)?) + } +} + +#[cfg(feature = "postgres")] +impl serialize::ToSql for AppLayerParams { + fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, Pg>) -> serialize::Result { + let value = serde_json::to_value(&self)?; + >::to_sql(&value, &mut out.reborrow()) + } +} + +#[cfg(feature = "sqlite")] +impl deserialize::FromSql for AppLayerParams +where + *const str: deserialize::FromSql, +{ + fn from_sql(value: ::RawValue<'_>) -> deserialize::Result { + let s = + <*const str as deserialize::FromSql>::from_sql(value)?; + Ok(serde_json::from_str(unsafe { &*s })?) + } +} + +#[cfg(feature = "sqlite")] +impl serialize::ToSql 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) + } +} + +#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] +pub enum Ts003Version { + #[default] + V100, +} + +#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] +pub enum Ts004Version { + #[default] + V100, +} + +#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] +pub enum Ts005Version { + #[default] + V100, +} diff --git a/chirpstack/src/storage/fields/mod.rs b/chirpstack/src/storage/fields/mod.rs index fe3f8ce9..1db3dc58 100644 --- a/chirpstack/src/storage/fields/mod.rs +++ b/chirpstack/src/storage/fields/mod.rs @@ -9,7 +9,7 @@ mod uuid; pub use big_decimal::BigDecimal; pub use dev_nonces::DevNonces; -pub use device_profile::{AbpParams, ClassBParams, ClassCParams, RelayParams}; +pub use device_profile::{AbpParams, AppLayerParams, ClassBParams, ClassCParams, RelayParams}; pub use device_session::DeviceSession; pub use key_value::KeyValue; pub use measurements::*; diff --git a/chirpstack/src/storage/schema_postgres.rs b/chirpstack/src/storage/schema_postgres.rs index 3ad43aef..e969fcc2 100644 --- a/chirpstack/src/storage/schema_postgres.rs +++ b/chirpstack/src/storage/schema_postgres.rs @@ -117,6 +117,7 @@ diesel::table! { class_b_params -> Nullable, class_c_params -> Nullable, relay_params -> Nullable, + app_layer_params -> Jsonb, } } diff --git a/chirpstack/src/storage/schema_sqlite.rs b/chirpstack/src/storage/schema_sqlite.rs index e316d0ab..4cd4b876 100644 --- a/chirpstack/src/storage/schema_sqlite.rs +++ b/chirpstack/src/storage/schema_sqlite.rs @@ -105,6 +105,7 @@ diesel::table! { class_b_params -> Nullable, class_c_params -> Nullable, relay_params -> Nullable, + app_layer_params -> Text, } }