Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex Strings #234

Merged
merged 10 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lorawan-encoding/Cargo.toml
Expand Up @@ -13,6 +13,7 @@ readme = "README.md"
aes = { version = "0.8", optional = true }
cmac = { version = "0.7", optional = true }
generic-array = "0"
hex = { version = "0", default-features = false, optional = true}
defmt = { version = "0.3", optional = true }
serde = { version = "1", default-features = false, features = ["derive"], optional = true}

Expand All @@ -27,9 +28,10 @@ harness = false

[features]
default = ["full"]
full = ["with-to-string", "with-downlink", "default-crypto"]
full = ["with-to-string", "with-from-str", "with-downlink", "default-crypto"]
default-crypto = ["aes", "cmac"]
with-to-string = []
with-to-string = ["hex"]
with-from-str = ["hex"]
with-downlink = []
serde = ["dep:serde"]
defmt = ["dep:defmt"]
4 changes: 2 additions & 2 deletions lorawan-encoding/src/extra/mod.rs
Expand Up @@ -9,5 +9,5 @@
pub extern crate std;

pub mod hasher;
#[cfg(feature = "with-to-string")]
pub mod to_string;
#[cfg(any(feature = "with-to-string", feature = "with-from-str"))]
pub mod string;
162 changes: 162 additions & 0 deletions lorawan-encoding/src/extra/string.rs
@@ -0,0 +1,162 @@
// Copyright (c) 2020 Ivaylo Petrov
//
// Licensed under the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//
// author: Ivaylo Petrov <ivajloip@gmail.com>

pub extern crate std;
use crate::keys::*;
use crate::parser::*;

macro_rules! fixed_len_struct_impl_string_msb {
(
$type:ident,$size:expr;
) => {
impl std::string::ToString for $type {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
res.extend(std::iter::repeat('0').take($size * 2));
let slice = unsafe { &mut res.as_bytes_mut() };
hex::encode_to_slice(self.as_ref(), slice).unwrap();
res
}
}

impl From<&str> for $type {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
Self::from(res)
}
}
};
(
$type:ident[$size:expr];
) => {
impl<T: AsRef<[u8]>> std::string::ToString for $type<T> {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
res.extend(std::iter::repeat('0').take($size * 2));
let slice = unsafe { &mut res.as_bytes_mut() };
hex::encode_to_slice(self.as_ref(), slice).unwrap();
res
}
}

impl From<&str> for $type<[u8; $size]> {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
Self::from(res)
}
}
};
}

macro_rules! fixed_len_struct_impl_string_lsb {
(
$type:ident,$size:expr;
) => {
impl std::string::ToString for $type {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
res.extend(std::iter::repeat('0').take($size * 2));
let slice = unsafe { &mut res.as_bytes_mut() };
self.as_ref().iter().rev().enumerate().for_each(|(i, b)| {
hex::encode_to_slice(&[*b], &mut slice[i * 2..i * 2 + 2]).unwrap();
});
res
}
}

impl From<&str> for $type {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
res.reverse();
Self::from(res)
}
}
};
}

fixed_len_struct_impl_string_msb! {
EUI64[8];
}

fixed_len_struct_impl_string_msb! {
DevNonce[2];
}

fixed_len_struct_impl_string_msb! {
AppNonce[3];
}

fixed_len_struct_impl_string_msb! {
DevAddr[4];
}

fixed_len_struct_impl_string_msb! {
NwkAddr[3];
}

fixed_len_struct_impl_string_msb! {
AppKey, 16;
}

fixed_len_struct_impl_string_msb! {
NewSKey, 16;
}

fixed_len_struct_impl_string_msb! {
AppSKey, 16;
}

fixed_len_struct_impl_string_lsb! {
DevEui, 8;
}

fixed_len_struct_impl_string_lsb! {
AppEui, 8;
}

#[cfg(test)]
mod test {
use super::*;
use crate::extra::std::string::ToString;

#[test]
fn test_appskey_to_string() {
let appskey = AppSKey::from([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF,
]);
assert_eq!(appskey.to_string(), "00112233445566778899aabbccddeeff");
}

#[test]
fn test_appskey_from_str() {
let appskey = AppSKey::from("00112233445566778899aabbccddeeff");
assert_eq!(
appskey,
AppSKey::from([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF
])
);
}

#[test]
fn test_deveui_to_string() {
let deveui = DevEui::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]);
assert_eq!(deveui.to_string(), "7766554433221100");
}

#[test]
fn test_deveui_from_str() {
let deveui = DevEui::from("7766554433221100");
assert_eq!(deveui, DevEui::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]));
}
}
54 changes: 0 additions & 54 deletions lorawan-encoding/src/extra/to_string.rs

This file was deleted.

15 changes: 13 additions & 2 deletions lorawan-encoding/src/keys.rs
Expand Up @@ -31,8 +31,13 @@ macro_rules! lorawan_key {
}
}

};
}
impl AsRef<[u8]> for $type {
fn as_ref(&self) -> &[u8] {
&self.0 .0
}
}
};
}

lorawan_key!(
/// AppKey should be entered in MSB format. For example, if your LNS provides a AppKey of
Expand Down Expand Up @@ -72,6 +77,12 @@ macro_rules! lorawan_eui {
key.0
}
}

impl AsRef<[u8]> for $type {
fn as_ref(&self) -> &[u8] {
&self.0.as_ref()
}
}
};
}

Expand Down