MCPcopy Create free account
hub / github.com/chirpstack/chirpstack / encode_freq

Function encode_freq

lrwn/src/helpers.rs:3–22  ·  view source on GitHub ↗
(freq: u32)

Source from the content-addressed store, hash-verified

1use anyhow::Result;
2
3pub fn encode_freq(freq: u32) -> Result<[u8; 3]> {
4 let mut freq = freq;
5 // Support LoRaWAN 2.4GHz, in which case the stepping is 200Hz:
6 // See Frequency Encoding in MAC Commands
7 // https://lora-developers.semtech.com/documentation/tech-papers-and-guides/physical-layer-proposal-2.4ghz/
8 if freq >= 2400000000 {
9 freq /= 2;
10 }
11
12 if freq / 100 >= (1 << 24) {
13 return Err(anyhow!("max freq value is 2^24 - 1"));
14 }
15 if freq % 100 != 0 {
16 return Err(anyhow!("freq must be multiple of 100"));
17 }
18
19 let mut b = [0; 3];
20 b[0..3].copy_from_slice(&(freq / 100).to_le_bytes()[0..3]);
21 Ok(b)
22}
23
24pub fn decode_freq(b: &[u8]) -> Result<u32> {
25 if b.len() != 3 {

Callers 4

encodeMethod · 0.85
to_vecMethod · 0.85
encodeMethod · 0.85
encodeMethod · 0.85

Calls 1

to_le_bytesMethod · 0.45

Tested by

no test coverage detected