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

Examples: Add support for overriding lorawan keys during build via LORA_APPKEY, LORA_DEVEUI.. values #232

Merged
merged 1 commit into from Mar 29, 2024

Conversation

plaes
Copy link
Collaborator

@plaes plaes commented Mar 21, 2024

Implement lorawan key customization via environment variables (for nrf52840):
LORA_APPEUI= LORA_APPKEY= LORA_DEVEUI=1122334455667788 cargo build --release

@plaes plaes changed the title Examples override lorawan keys Examples: Add support for overriding lorawan keys during build via LORA_APPKEY, LORA_DEVEUI.. values Mar 21, 2024
lucasgranberg
lucasgranberg previously approved these changes Mar 22, 2024
@lthiery
Copy link
Collaborator

lthiery commented Mar 26, 2024

What about providing this as a util in the lorawan-device module? You could call the util from build.rs.

@lthiery
Copy link
Collaborator

lthiery commented Mar 26, 2024

Come to think of it, providing this as a util in the lorawan module will be difficult because this (and build.rs) can depend on std but lorawan can't.

We could create lorawan-util crates that has std... but I think this is also fine the way it is.

@lthiery
Copy link
Collaborator

lthiery commented Mar 26, 2024

Actually, I was wrong above. build-dependencies could have std enabled for lorawan-device which could provide this utility without having std enabled in the project itself. Then that could enable the from_str from lorawan-encoding (see review)?

lthiery
lthiery previously approved these changes Mar 26, 2024
Copy link
Collaborator

@lthiery lthiery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some non-blocking suggestions.

use std::path::PathBuf;

fn hex_to_bytes(s: &str) -> Option<Vec<u8>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hex::encode?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I initially didn't want to include extra dependency for just a single semi-trivial function (left-pad 🗡️ )..

const DEVEUI: Option<[u8; 8]> = {};\n\
const APPEUI: Option<[u8; 8]> = {};\n\
const APPKEY: Option<[u8; 16]> = {};\n",
parse_lorawan_id(option_env!("LORA_DEVEUI"), "LORA_DEVEUI", 8).unwrap_or("None".to_string()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps implement DevEui/AppEui/AppKey::from_str? Feature-gated behind std being enabled in lorawan-encoding.

@lthiery
Copy link
Collaborator

lthiery commented Mar 26, 2024

I also see that we hand-rolled the hex encoding here in lorawan-encoding. I've corrected there here.

@lthiery
Copy link
Collaborator

lthiery commented Mar 26, 2024

I went ahead and just made the PR for lorawan-encoding: #234

@lucasgranberg
Copy link
Collaborator

just for consideration the hex decoding can be done at compile time without custom build scripts by using the hex-literal crate. https://crates.io/crates/hex-literal

@plaes
Copy link
Collaborator Author

plaes commented Mar 28, 2024

So, I basically ran into a headache and current version is good enough.
I can easily use the build.rs to parse the DevEui and friends, but in the end I would need to somehow "tell" rust to use this value in some way, which means I would have to either convert it back to array syntax or find something better.
This is how it works for now:

  1. In generated file I have: const DEVEUI: Option<[u8; 8]> = None; (or Some([...]) which is easy to generate).
  2. In lorawan example code I need to match the types so I can override: deveui: DevEui::from(DEVEUI.unwrap_or(DEFAULT_DEVEUI)),

(Also, from_str() requires pulling in use core::str::FromStr; and its a lot easier to type out zeroes as array than into hex string..)

Add possibility to overriding LoRaWAN keys from env during compile.
@plaes plaes dismissed stale reviews from lthiery and lucasgranberg via 57c1c13 March 28, 2024 12:29
@plaes plaes force-pushed the examples-override-lorawan-keys branch from 964a764 to 57c1c13 Compare March 28, 2024 12:29
@lthiery lthiery merged commit 9cff2b3 into lora-rs:main Mar 29, 2024
1 check passed
@plaes plaes deleted the examples-override-lorawan-keys branch March 29, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants