Can't get lorawan + shell working

Hi, I am exploring RIOT + LoRaWAN.

  1. I tested examples/lorawan and it worked fine
  2. Then I modified it to support shell commands, however when I do this I get a compilation error: sc_loramac.c:451: undefined reference to `loramac’

Below I share the diff of my changes in examples/lorawan, and the full log output.

Diff
diff --git a/examples/lorawan/Makefile b/examples/lorawan/Makefile
index 513490347f..8c47cd9476 100644
--- a/examples/lorawan/Makefile
+++ b/examples/lorawan/Makefile
@@ -29,6 +29,9 @@ USEMODULE += $(DRIVER)
 USEMODULE += fmt
 FEATURES_OPTIONAL += periph_rtc
 
+USEMODULE += shell
+USEMODULE += shell_commands
+
 # Comment this out to disable code in RIOT that does safety checking
 # which is not needed in a production environment but helps in the
 # development process:
diff --git a/examples/lorawan/main.c b/examples/lorawan/main.c
index 5a8a5e31b2..1a795fa119 100644
--- a/examples/lorawan/main.c
+++ b/examples/lorawan/main.c
@@ -49,6 +49,9 @@
 #include "sx126x_params.h"
 #endif
 
+#include "shell.h"
+#include "shell_commands.h"
+
 /* Messages are sent every 20s to respect the duty cycle on each channel */
 #define PERIOD_S            (20U)
 
@@ -179,5 +182,9 @@ int main(void)
     /* trigger the first send */
     msg_t msg;
     msg_send(&msg, sender_pid);
+
+    char line_buf[SHELL_DEFAULT_BUFSIZE];
+    shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
+
     return 0;
 }
Output of `make`
Building application "lorawan" for "b-l072z-lrwan1" with MCU "stm32".

"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semtech-loramac/                                                                               
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/build/pkg/semtech-loramac/src/boards/mcu -f /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semt
ech-loramac/Makefile.loramac_arch                                                                                                                            
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/build/pkg/semtech-loramac/src/system/crypto -f /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semtech-loramac/Makefile.loramac_crypto
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/build/pkg/semtech-loramac/src/mac -f /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semtech-lor
amac/Makefile.loramac_mac                                                                                                                                    
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/build/pkg/semtech-loramac/src/mac/region -f /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semt
ech-loramac/Makefile.loramac_region                                                                                                                          
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/boards/b-l072z-lrwan1
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/core                                                                                               
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/stm32
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/cortexm_common                                                                                 
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/cortexm_common/periph
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/stm32/periph
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/stm32/stmclk
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/cpu/stm32/vectors
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/drivers
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/drivers/periph_common
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/drivers/sx127x
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/pkg/semtech-loramac/contrib
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/auto_init
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/crypto
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/fmt
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/frac
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/hashes
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/iolist
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/isrpipe
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/luid
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/malloc_thread_safe
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/newlib_syscalls_default
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/pm_layered
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/random
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/rtc_utils
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/shell
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/shell/commands
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/stdio_uart
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/tsrb
"make" -C /home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/ztimer
/usr/lib/gcc/arm-none-eabi/7.3.1/../../../arm-none-eabi/bin/ld: /home/geovane/dev/stuff/explore-riot/RIOT-pulga/examples/lorawan/bin/b-l072z-lrwan1/shell_commands/sc_loramac.o: in function `_loramac_handler':
/home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/shell/commands/sc_loramac.c:209: undefined reference to `loramac'                                        
/usr/lib/gcc/arm-none-eabi/7.3.1/../../../arm-none-eabi/bin/ld: /home/geovane/dev/stuff/explore-riot/RIOT-pulga/examples/lorawan/bin/b-l072z-lrwan1/shell_commands/sc_loramac.o: in function `_loramac_tx_usage':
/home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/shell/commands/sc_loramac.c:55: undefined reference to `loramac'                                         
/usr/lib/gcc/arm-none-eabi/7.3.1/../../../arm-none-eabi/bin/ld: /home/geovane/dev/stuff/explore-riot/RIOT-pulga/examples/lorawan/bin/b-l072z-lrwan1/shell_commands/sc_loramac.o: in function `_loramac_handler':
/home/geovane/dev/stuff/explore-riot/RIOT-pulga/sys/shell/commands/sc_loramac.c:451: undefined reference to `loramac'                                        
collect2: error: ld returned 1 exit status
make: *** [/home/geovane/dev/stuff/explore-riot/RIOT-pulga/examples/lorawan/../../Makefile.include:722: /home/geovane/dev/stuff/explore-riot/RIOT-pulga/examples/lorawan/bin/b-l072z-lrwan1/lorawan.elf] Error 1

Any ideas? Thanks.

Hi @geonnave! So the examples/lorawan code is somewhat meant as a standalone example and therefore declares its own descriptor for semtech_loramac_t loramac and the LoRa device drivers as well, in your case sx127x_t sx127x, but in line 62 and 64 those are declared static and therefore not visible to other compilation units, but the loramac shell declares extern semtech_loramac_t loramac, the quickest fix is to simple remove the static part:

-static semtech_loramac_t loramac;
+semtech_loramac_t loramac;

I’ll open a PR with this as well.

Well actually not sure if this is what @aabadie would prefer, opinions?

Ugg, you beat me to it :slight_smile:

Maybe it would be better to just use auto_init_loramac and remove the static semtech_loramac_t loramac; completely.

Hmm not sure, I thought about it, but I think this example is meant to show the whole stack, with initializing the drivers and network stack, in which case it makes sense to leave the whole initialization process.

Thanks @fjmolinas, it worked after removing the static modifier :slight_smile:

Btw, I agree that showing the whole initialization in the example makes sense. One the other hand, I was not aware of auto_init_loramac. So one idea is to add a comment stating that “this part could be replaced by auto_init_loramac, for more information check the documentation”.

1 Like

Hi! I opened a PR to remove the static and a mention to auto_init_loramac.

1 Like