lora/lorawan: implement minimal Join() function, and supporting functions

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-01-08 19:42:19 +01:00
committed by Ron Evans
parent 36dfdf568e
commit a62fc01d81
4 changed files with 160 additions and 44 deletions
+14 -28
View File
@@ -1,37 +1,23 @@
package lorawan
// revertByteArray inverts de order of a given byte slice
func revertByteArray(s []byte) []byte {
import "crypto/rand"
// reverseBytes reverses order of a given byte slice
func reverseBytes(s []byte) []byte {
result := make([]byte, len(s))
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
result[i], result[j] = s[j], s[i]
}
return s
return result
}
// byteToHex return string hex representation of byte
func byteToHex(b byte) string {
bb := (b >> 4) & 0x0F
ret := ""
if bb < 10 {
ret += string(rune('0' + bb))
} else {
ret += string(rune('A' + (bb - 10)))
// GetRand16 returns 2 random bytes
func GetRand16() ([2]uint8, error) {
randomBytes := make([]byte, 2)
_, err := rand.Read(randomBytes)
if err != nil {
return [2]uint8{}, err
}
bb = (b) & 0xF
if bb < 10 {
ret += string(rune('0' + bb))
} else {
ret += string(rune('A' + (bb - 10)))
}
return ret
}
// BytesToHexString converts byte slice to hex string representation
func bytesToHexString(data []byte) string {
s := ""
for i := 0; i < len(data); i++ {
s += byteToHex(data[i])
}
return s
return [2]uint8{randomBytes[0], randomBytes[1]}, nil
}