From 3348ccf67d81f36bbf76395613ac2b39a6aa0710 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Wed, 3 Aug 2022 15:18:31 +0100 Subject: [PATCH] Fix async receiver for passive-roaming HomeNSReq + add debug logs. --- backend/src/lib.rs | 39 +++++++++++++++++++++++++++---- chirpstack/src/api/backend/mod.rs | 4 +++- chirpstack/src/uplink/join_fns.rs | 15 +++++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/backend/src/lib.rs b/backend/src/lib.rs index f1571bc5..3abe4505 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -12,7 +12,7 @@ use reqwest::header::{HeaderMap, AUTHORIZATION, CONTENT_TYPE}; use reqwest::{Certificate, Identity}; use serde::{Deserialize, Serialize}; use tokio::sync::oneshot::Receiver; -use tracing::{error, info, trace}; +use tracing::{debug, error, info, trace}; const PROTOCOL_VERSION: &str = "1.0"; @@ -279,13 +279,15 @@ impl Client { }; let bp = pl.base_payload(); + let body = serde_json::to_string(&pl)?; info!(receiver_id = %hex::encode(&bp.base.receiver_id), transaction_id = bp.base.transaction_id, message_type = ?bp.base.message_type, server = %server, "Making request"); + debug!("JSON: {}", body); self.client .post(&server) .headers(self.headers.clone()) - .json(pl) + .body(body) .send() .await? .error_for_status()?; @@ -315,14 +317,16 @@ impl Client { }; let bp = pl.base_payload().clone(); + let body = serde_json::to_string(&pl)?; info!(receiver_id = %hex::encode(&bp.receiver_id), transaction_id = bp.transaction_id, message_type = ?bp.message_type, server = %server, async_interface = %async_resp.is_some(), "Making request"); + debug!("JSON: {}", body); let res = self .client .post(&server) .headers(self.headers.clone()) - .json(pl) + .body(body) .send() .await? .error_for_status()?; @@ -344,6 +348,7 @@ impl Client { None => res.text().await?, }; + debug!("JSON: {}", resp_json); let base: BasePayloadResult = serde_json::from_str(&resp_json)?; if base.result.result_code != ResultCode::Success { error!(result_code = ?base.result.result_code, description = %base.result.description, receiver_id = %hex::encode(&bp.receiver_id), transaction_id = bp.transaction_id, message_type = ?bp.message_type, "Response error"); @@ -974,7 +979,12 @@ pub struct GWInfoElement { pub id: Vec, #[serde(rename = "FineRecvTime")] pub fine_recv_time: Option, - #[serde(default, rename = "RFRegion", skip_serializing_if = "String::is_empty")] + #[serde( + default, + rename = "RFRegion", + with = "rf_region_encode", + skip_serializing_if = "String::is_empty" + )] pub rf_region: String, #[serde(rename = "RSSI")] pub rssi: Option, @@ -1079,6 +1089,27 @@ pub struct DLMetaData { pub hi_priority_flag: bool, } +mod rf_region_encode { + use serde::{Deserializer, Serializer}; + + pub fn serialize(s: &str, serializer: S) -> std::result::Result + where + S: Serializer, + { + serializer.serialize_str(&s.replace('_', "-")) + } + + pub fn deserialize<'a, D>(deserializer: D) -> Result + where + D: Deserializer<'a>, + { + let s: &str = serde::de::Deserialize::deserialize(deserializer)?; + + // Some implementations use lowercase. + Ok(s.to_uppercase()) + } +} + mod hex_encode { use serde::{Deserializer, Serializer}; diff --git a/chirpstack/src/api/backend/mod.rs b/chirpstack/src/api/backend/mod.rs index 53183e52..f991b40c 100644 --- a/chirpstack/src/api/backend/mod.rs +++ b/chirpstack/src/api/backend/mod.rs @@ -8,7 +8,7 @@ use anyhow::Result; use redis::streams::StreamReadReply; use tokio::sync::oneshot; use tokio::task; -use tracing::{error, info, warn}; +use tracing::{debug, error, info, warn}; use uuid::Uuid; use warp::{http::StatusCode, Filter, Reply}; @@ -69,6 +69,8 @@ pub async fn handle_request(mut body: impl warp::Buf) -> http::Response v, Err(e) => { diff --git a/chirpstack/src/uplink/join_fns.rs b/chirpstack/src/uplink/join_fns.rs index 1fd25f10..d259ecd0 100644 --- a/chirpstack/src/uplink/join_fns.rs +++ b/chirpstack/src/uplink/join_fns.rs @@ -70,8 +70,21 @@ impl JoinRequest { home_ns_req.base.transaction_id = 1234; } + let async_receiver = match js_client.is_async() { + false => None, + true => Some( + get_async_receiver( + home_ns_req.base.transaction_id, + js_client.get_async_timeout(), + ) + .await?, + ), + }; + trace!("Requesting home netid"); - let home_ns_ans = js_client.home_ns_req(&mut home_ns_req, None).await?; + let home_ns_ans = js_client + .home_ns_req(&mut home_ns_req, async_receiver) + .await?; self.home_net_id = Some(NetID::from_slice(&home_ns_ans.h_net_id)?); Ok(())