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

Add option to build LoRaMac sources as a static library #1368

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
aaaf457
Create directory for CMakeLists and add stub README
dmeehan1968 Oct 13, 2022
580737a
Allow CMake subdirectory to be switched based on LORAMAC_AS_LIB option
dmeehan1968 Oct 13, 2022
e0c0504
Add subdirectories to mirror src directory organisation
dmeehan1968 Oct 13, 2022
ccba4fd
Add board sources to board target
dmeehan1968 Oct 13, 2022
9bad232
Add system sources to system target
dmeehan1968 Oct 13, 2022
a176e32
Improve formatting
dmeehan1968 Oct 13, 2022
045328e
Add radio sources to radio target
dmeehan1968 Oct 13, 2022
2589f94
Add peripheral sources to peripheral target
dmeehan1968 Oct 13, 2022
7f8b18c
Fix spelling of board target
dmeehan1968 Oct 13, 2022
02a420b
Add mac sources to mac target
dmeehan1968 Oct 13, 2022
c36dc12
Include region sources based on option flags
dmeehan1968 Oct 13, 2022
a3e69ca
Remove debug message
dmeehan1968 Oct 13, 2022
5f3aa15
Add support for all secure elements
dmeehan1968 Oct 13, 2022
43c36d1
Set LORAMAC_SUFFIX as cache variable if not already set
dmeehan1968 Oct 13, 2022
e73d7fe
Make LORAMAC_SECURE_ELEMENT a cache variable and set property strings…
dmeehan1968 Oct 13, 2022
09211a7
Make AES_DEC_PREKEYED only relative to SOFT_SE
dmeehan1968 Oct 13, 2022
e199890
Add options and explanations to README
dmeehan1968 Oct 13, 2022
d1f801a
Add support for all radio drivers
dmeehan1968 Oct 13, 2022
5bbd643
Use file GLOB to include aetcc608a-tnglora-se cryptoauthlib
dmeehan1968 Oct 14, 2022
b6aa97a
Update TODOs
dmeehan1968 Oct 14, 2022
b020abe
Remove LmHandler, not part of LoRaMac API
dmeehan1968 Oct 14, 2022
b9f7c59
Expand README with guidance on building with FetchContent
dmeehan1968 Oct 15, 2022
2e90b60
Renove LORAMAC_SOFT_SE_AES_DEC_PREKEYED as not exposed by `src` tree
dmeehan1968 Oct 15, 2022
7aa0fc5
Clarify that ExternalProject is not currently supported
dmeehan1968 Oct 15, 2022
7b27312
Expand README on region support, rename region variables to have LORA…
dmeehan1968 Oct 15, 2022
526f05b
Fix lack of LORAMAC_ prefix when passing region options
dmeehan1968 Oct 15, 2022
a7a07e8
Update README to note that LORAMAC_SUFFIX must use FORCE override
dmeehan1968 Oct 15, 2022
065b25d
Add RegionCN470B20.c/h when building for CN470
dmeehan1968 Apr 27, 2023
be38c56
Add RegionCN470{X}2{X}.c/h all variants when building for CN470
dmeehan1968 Apr 27, 2023
6cdda80
Remove unnecessary OR when including CN470
dmeehan1968 Apr 27, 2023
28840ec
Fix incorrect use of BOOL when including CN470
dmeehan1968 Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
project(loramac-node)
cmake_minimum_required(VERSION 3.6)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
option(LORAMAC_AS_LIB "If enabled, will configure LoRaMac-node to be built as a static library, otherwise will build example projects" OFF)

if (LORAMAC_AS_LIB)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/as-lib)
else()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
endif()
9 changes: 9 additions & 0 deletions as-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.23)

cmake_path(SET LORAMAC_SOURCE_DIR NORMALIZE ${CMAKE_CURRENT_SOURCE_DIR}/../src)

add_subdirectory(board)
add_subdirectory(system)
add_subdirectory(radio)
add_subdirectory(peripheral)
add_subdirectory(mac)
124 changes: 124 additions & 0 deletions as-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# LoRaMac-node 'as-lib'

**WIP**

## Purpose

This directory is provided to allow LoRaMac-node to be built as a static library for inclusion in other projects.

This directory contains a set of CMakeLists.txt files that shadow the directory structure under `src`. A backwards
compatible change is made to the project root CMakeLists.txt which, when provided with the option `LORAMAC_AS_LIB`,
will direct CMake to reference the as-lib directory instead of src. The option default is OFF so that the past
build behaviour is preserved as the default.

CMake configuration options are provided that mirror equivalent options in the `src` configuration tree, but a
standard `LORAMAC_` prefix is applied to all options to aid interoperability with other project sources.

## Supported CMake Configuration Options

- LORAMAC_AS_LIB:BOOL Whether to build the project as a static library (when ON), or to build the example applications
- LORAMAC_SUFFIX:STRING Defaults to empty, but can be set to any string to allow for multiple static libraries to
be build (for example with different region support)
- LORAMAC_SECURE_ELEMENT:STRING Name of the secure element, defaults to SOFT_SE
- LORAMAC_SECURE_ELEMENT_PRE_PROVISIONED:BOOL Whether the secure element is pre-provisioned (default ON)
- LORAMAC_RADIO:STRING Name of the radio driver, defaults to sx1272
- LORAMAC_USE_RADIO_DEBUG:BOOL Enable Radio Debug GPIO's (default OFF)

## Region support

Note that unlike the `src` build, the supported regions are not configured as CMake cache options. This is to
support easier override when building multiple regions (where cache FORCE would be needed to override which).

At least one region must be enabled, and there are no regions enabled by default. A fatal CMake configure error
will be generated if no regions are supported.

- LORAMAC_REGION_EU868:BOOL Enable support for EU868
- LORAMAC_REGION_US915:BOOL Enable support for US915
- LORAMAC_REGION_CN779:BOOL Enable support for CN779
- LORAMAC_REGION_EU433:BOOL Enable support for EU433
- LORAMAC_REGION_AU915:BOOL Enable support for AU915
- LORAMAC_REGION_AS923:BOOL Enable support for AS923
- LORAMAC_REGION_CN470:BOOL Enable support for CN470
- LORAMAC_REGION_KR920:BOOL Enable support for KR920
- LORAMAC_REGION_IN865:BOOL Enable support for IN865
- LORAMAC_REGION_RU864:BOOL Enable support for RU864

## Preparation for loading and building

You must establish your toolchain prior to your first CMake `project()` call (which triggers toolchain detection). It
is beyond the scope of this document to describe how to do that for your platform.

You will need to provide a board implementation that suits your project. A number of standard boards are provided
in the LoRaMac-node project which can be copied after the first CMake configure pass. Typically you will copy one of these
(from the `/src/boards` directory) into your own project sources and make any necessary changes.

NB: Pre-built board implementation targets are not provided due to the complexity of providing included or project
specific HAL libraries. `CMakeLists.txt` files are included in the board implementation directories that can be used
as a starting point for your own project.

Depending on your platform, you may need to obtain Hardware Abstraction Libraries that are used by the board
implementation that you choose. LoRaMac-node includes some of these for the provided board implementations (in the
`/src/board/mcu` directory), but you may wish provide your own version (perhaps via FetchContent from the official
sources).

## Configuring via FetchContent to create a single library

```
FetchContent_Declare(
loramac
GIT_REPOSITORY https://github.com/Lora-net/LoRaMac-node
GIT_TAG master # branch or version tag, such a v4.7.0
)

set(LORAMAC_AS_LIB ON)
set(LORAMAC_RADIO sx1276)
set(REGION_EU868 ON)
FetchContent_MakeAvailable(loramac)

# add your own target

add_executable(MyProject C)
target_sources(MyProject main.c)
target_link_libraries(MyProject loramac)
```

## Configuring via FetchContent to create multiple libraries

FetchContent should be used to load the project at CMake configure time (rather than build time using ExternalProject).

`ExternalProject_Add` is not supported at this time.

NB: If building multiple static libraries for regional variants, ensure that you set the previous passes region to OFF

```
FetchContent_Declare(
loramac
GIT_REPOSITORY https://github.com/Lora-net/LoRaMac-node
GIT_TAG master # branch or version tag, such a v4.7.0
)

FetchContent_GetProperties(loramac)
if (NOT loramac_POPULATED)
FetchContent_Populate(loramac)
endif()

set(LORAMAC_AS_LIB ON)
set(LORAMAC_RADIO sx1276)
set(LORAMAC_SUFFIX -Europe CACHE STRING "" FORCE) # must force override
set(REGION_EU868 ON)
add_subdirectory(loramac_SOURCE_DIR loramac${LORAMAC_SUFFIX})

set(REGION_EU868 OFF) # NB: Override last pass
set(REGION_US915 ON)
set(LORAMAC_SUFFIX -US CACHE STRING "" FORCE) # must force override
add_subdirectory(loramac_SOURCE_DIR loramac${LORAMAC_SUFFIX})

# You now have targets loramac-Europe and loramac-US to link to your own targets

add_executable(MyProject C)
target_sources(MyProject main.c)
target_link_libraries(MyProject loramac-Europe)
```

## TODO

43 changes: 43 additions & 0 deletions as-lib/board/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
cmake_minimum_required(VERSION 3.23)

project(loramac-board C)

if (NOT TARGET ${PROJECT_NAME})

add_library(${PROJECT_NAME} STATIC)

target_sources(
${PROJECT_NAME}

PUBLIC
${LORAMAC_SOURCE_DIR}/boards/adc-board.h
${LORAMAC_SOURCE_DIR}/boards/board.h
${LORAMAC_SOURCE_DIR}/boards/delay-board.h
${LORAMAC_SOURCE_DIR}/boards/display-board.h
${LORAMAC_SOURCE_DIR}/boards/eeprom-board.h
${LORAMAC_SOURCE_DIR}/boards/gpio-board.h
${LORAMAC_SOURCE_DIR}/boards/gps-board.h
${LORAMAC_SOURCE_DIR}/boards/i2c-board.h
${LORAMAC_SOURCE_DIR}/boards/lpm-board.h
${LORAMAC_SOURCE_DIR}/boards/lr1110-board.h
${LORAMAC_SOURCE_DIR}/boards/pinName-board.h
${LORAMAC_SOURCE_DIR}/boards/pinName-ioe.h
${LORAMAC_SOURCE_DIR}/boards/rtc-board.h
${LORAMAC_SOURCE_DIR}/boards/sx126x-board.h
${LORAMAC_SOURCE_DIR}/boards/sx1272-board.h
${LORAMAC_SOURCE_DIR}/boards/sx1276-board.h
${LORAMAC_SOURCE_DIR}/boards/uart-board.h
${LORAMAC_SOURCE_DIR}/boards/uart-usb-board.h
${LORAMAC_SOURCE_DIR}/boards/utilities.h

PRIVATE
${LORAMAC_SOURCE_DIR}/boards/mcu/utilities.c
)

target_include_directories(
${PROJECT_NAME}
PUBLIC
${LORAMAC_SOURCE_DIR}/boards
)

endif()
114 changes: 114 additions & 0 deletions as-lib/mac/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
cmake_minimum_required(VERSION 3.23)

if (NOT DEFINED LORAMAC_SUFFIX)
set(LORAMAC_SUFFIX "" CACHE STRING "Default suffix for LoRaMac Static Library builds")
endif()

project(loramac${LORAMAC_SUFFIX} C)

if (NOT TARGET ${PROJECT_NAME})

set(LORAMAC_REGION_LIST EU868 US915 CN779 EU433 AU915 AS923 CN470 KR920 IN865 RU864)

add_library(${PROJECT_NAME} STATIC)
target_sources(
${PROJECT_NAME}
PUBLIC
${LORAMAC_SOURCE_DIR}/mac/LoRaMac.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacAdr.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacClassB.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacClassBConfig.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacClassBNvm.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacCommands.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacConfirmQueue.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacCrypto.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacCryptoNvm.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacHeaderTypes.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacMessageTypes.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacParser.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacSerializer.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacTest.h
${LORAMAC_SOURCE_DIR}/mac/LoRaMacTypes.h
${LORAMAC_SOURCE_DIR}/mac/secure-element.h
${LORAMAC_SOURCE_DIR}/mac/secure-element-nvm.h
${LORAMAC_SOURCE_DIR}/mac/region/Region.h
${LORAMAC_SOURCE_DIR}/mac/region/RegionCommon.h
$<$<OR:$<BOOL:LORAMAC_REGION_AU915>,$<BOOL:LORAMAC_REGION_US915>>:${LORAMAC_SOURCE_DIR}/mac/region/RegionBaseUS.h>
$<$<BOOL:${LORAMAC_REGION_CN470}>:
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470A20.h
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470B20.h
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470A26.h
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470B26.h
>

PRIVATE
${LORAMAC_SOURCE_DIR}/mac/LoRaMac.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacAdr.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacClassB.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacCommands.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacConfirmQueue.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacCrypto.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacParser.c
${LORAMAC_SOURCE_DIR}/mac/LoRaMacSerializer.c
${LORAMAC_SOURCE_DIR}/mac/region/Region.c
${LORAMAC_SOURCE_DIR}/mac/region/RegionCommon.c
$<$<OR:$<BOOL:${LORAMAC_REGION_AU915}>,$<BOOL:${LORAMAC_REGION_US915}>>:${LORAMAC_SOURCE_DIR}/mac/region/RegionBaseUS.c>
$<$<BOOL:${LORAMAC_REGION_CN470}>:
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470A20.c
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470B20.c
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470A26.c
${LORAMAC_SOURCE_DIR}/mac/region/RegionCN470B26.c
>
)

set(ACTIVE_REGION_COUNT 0)

foreach(REGION ${LORAMAC_REGION_LIST})
if (${LORAMAC_REGION_${REGION}})

MATH(EXPR ACTIVE_REGION_COUNT "${ACTIVE_REGION_COUNT}+1")

target_sources(
${PROJECT_NAME}
PUBLIC
${LORAMAC_SOURCE_DIR}/mac/region/Region${REGION}.h

PRIVATE
${LORAMAC_SOURCE_DIR}/mac/region/Region${REGION}.c
)

target_compile_definitions(
${PROJECT_NAME}
PUBLIC
$<$<BOOL:${LORAMAC_REGION_${REGION}}>:REGION_${REGION}>
)

endif()
endforeach()

if (${ACTIVE_REGION_COUNT} LESS 1)
message(FATAL_ERROR "No LORAMAC_REGION_xxx's specified")
endif()

target_compile_definitions(
${PROJECT_NAME}
PUBLIC
)

target_include_directories(
${PROJECT_NAME}
PUBLIC
${LORAMAC_SOURCE_DIR}/mac
${LORAMAC_SOURCE_DIR}/mac/region
)

target_link_libraries(
${PROJECT_NAME}
PUBLIC
loramac-board
loramac-system
loramac-radio
loramac-peripheral
)

endif()
Loading