Decoding PHYPayload with /brocaar/lorawan repository on GitHub

Hi @brocaar!

I’m trying to decode PHYPayload inside PRStartReq messages captured with Wireshark, and thus be able to check some packets, but I’m not experienced with go so I can’t run the example (Lorawan10Decode) properly.
I’m still getting PHYPayload, AES18{} and LoRaWAN_10 as not defined.

Without providing any steps to reproduce your issue (code or commands), it is hard for me or anybody else to provide any help :slight_smile:

1 Like

I’m sorry, thought I had edited this a few hours ago. With go installed, I got the GitHub repository with go get command.
I have tried many things, as I saw different ways to use a function outside its package, but this is what I have now:

First thing I tried (as I said, not experienced) was copying the content of func ExamplePHYPayload_lorawan10Decode() in a ´main.go´ archive with imports in phypayload_test.go and main package. At that point, log reported non-declaration statement outside function body. Then I was told by a colleague to do as follows:

  1. Execute go init inside $PATH folder which is /home/go/bin
  2. Then go tidy.
  3. Execute main.go file, which right now looks like this:
package main

import (

	. ""
func ExamplePHYPayload_lorawan10Decode() {

	nwkSKey := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	appSKey := [16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

	var phy PHYPayload
	// use use UnmarshalBinary when decoding a byte-slice
	if err := phy.UnmarshalText([]byte("gAQDAgEDAAAGcwcK4mTU9+EX0sA=")); err != nil {

	ok, err := phy.ValidateUplinkDataMIC(LoRaWAN1_0, 0, 0, 0, nwkSKey, AES128Key{})
	if err != nil {
	if !ok {
		panic("invalid mic")

	if err := phy.DecodeFOptsToMACCommands(); err != nil {

	phyJSON, err := phy.MarshalJSON()
	if err != nil {

	if err := phy.DecryptFRMPayload(appSKey); err != nil {
	macPL, ok := phy.MACPayload.(*MACPayload)
	if !ok {
		panic("*MACPayload expected")

	pl, ok := macPL.FRMPayload[0].(*DataPayload)
	if !ok {
		panic("*DataPayload expected")


	// Output:
	// {"mhdr":{"mType":"ConfirmedDataUp","major":"LoRaWANR1"},"macPayload":{"fhdr":{"devAddr":"01020304","fCtrl":{"adr":false,"adrAckReq":false,"ack":false,"fPending":false,"classB":false},"fCnt":0,"fOpts":[{"cid":"DevStatusReq","payload":{"battery":115,"margin":7}}]},"fPort":10,"frmPayload":[{"bytes":"4mTU9w=="}]},"mic":"e117d2c0"}
	// [1 2 3 4]

func main(){

I get undefined PHYPayload, LoRaWAN1_0 and AES128Key and I don’t know how to declare those variables, or If we need to do any change on the code, which I don’t think so because of other examples I’ve been reading.
Also, I see imported but not used for the imports, but I don’t know If that’s relevant.
I thing something related to GOBIN or GOPATH could be happening.
Again, sorry for my lack of experience, both my colleague and I are new in this whole project and also with Go language.

The proper way is to import the code into your code. Then you can use structs and variables like lorawan.PHYPayload

Please see How to Write Go Code - The Go Programming Language for more details.