Commit graph

53 commits

Author SHA1 Message Date
Jordan Yates 23ca74b756 convert: use k_uptime_seconds
Replace usage of `k_uptime_get() / MSEC_PER_SEC` and
`k_uptime_get() / 1000` with dedicated function.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-05-14 09:32:01 +02:00
Aurelien Jarno a6fbfc0612 lorawan: make possible to send empty frames
Empty frames are allowed by the LoRaWAN protocol and are actually useful
to open new RX slots or flush MAC commands in stack. Therefore allow the
data pointer to be NULL if len is 0.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2024-05-12 11:01:18 -04:00
Jeferson Fernando c0a17a9a5e lorawan: add channels mask configuration.
Add channels mask configuration for LoRaWAN, creating a request for
 LoRaMAC. This change is needed because using LoRaWAN with ABP
activation without configuring channels mask will provide no acceptable
 channels on data transmit, causing data loss.

Signed-off-by: Jeferson Fernando <jeferson.santos@edge.ufal.br>
2024-05-03 09:51:46 +02:00
Martin Jäger f3e41c6a25 lorawan: services: add Fragmented Data Block Transport
This service receives fragmented data (usually firmware images) and
stores them in the slot1_partition in the flash.

Also update CMakeLists.txt in loramac-node module to be able to use
FragDecoder.c

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-05-02 16:54:43 +02:00
Aurelien Jarno ea995f6378 lorawan/nvm: do not call settings_save() after writing individual settings
The lorawan subsystem do not use settings handler, and instead uses
individual calls to settings_save_one(). With this strategy there is no
need to call settings_save() at the end.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2024-05-01 22:56:34 +01:00
Martin Jäger bfcd887903 lorawan: services: clock_sync: apply changed periodicity immediately
The work item for re-synchronization should be rescheduled immediately
after the periodicity is changed.

Calculation of the periodicity incl. jitter is moved to a dedicated
function so that it can be re-used.

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-03-01 17:35:04 +01:00
Martin Jäger 776c850896 lorawan: services: clock_sync: fix resync with multiple transmissions
Only the first resync transmission was sent out immediately because
the work item was rescheduled to the usual periodicity in the work
handler immediately after sending out the first transmission.

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-03-01 17:35:04 +01:00
Martin Jäger 8bb9bb67db lorawan: add emulator for unit testing
The emulator can be used for unit testing of LoRaWAN services.

It provides interfaces to send arbitrary messages to the LoRaWAN
stack and receive the response through callbacks without using
actual LoRa hardware.

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-03-01 17:35:04 +01:00
Martin Jäger d833ab746b lorawan: services: add Remote Multicast Setup
This service is responsible for multicast session key exchange and
setting up a class C session.

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-01-30 14:14:05 +00:00
Martin Jäger a4c13fc584 lorawan: services: add Class C session handling
The new functions are required for Multicast Class C session setup.

Signed-off-by: Martin Jäger <martin@libre.solar>
2024-01-30 14:14:05 +00:00
Martin Jäger c72b9f5048 lorawan: use callback function signature typedefs
This avoids duplication of the function signature in several places and
makes the API documentation more clean.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-11-13 09:53:32 +01:00
Martin Jäger 8b7c8b09ec lorawan: rename lorawan_set_battery_level_callback and make it void
Rename the lorawan_set_battery_level_callback to
lorawan_register_battery_level_callback to make it consistent with other
functions for downlink and data rate changed callbacks.

Also making the function void for consistency. The get_battery_level
already checks if the callback is NULL before invoking it.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-11-13 09:53:32 +01:00
Flavio Ceolin e7bd10ae71 random: Rename random header
rand32.h does not make much sense, since the random subsystem
provides more APIs than just getting a random 32 bits value.

Rename it to random.h and get consistently with other
subsystems.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-10 14:23:50 +03:00
Lucas Dietrich a59666754e lorawan: Add LORAWAN_PUBLIC_NETWORK configuration choice
Provides a toggle between public and private network selections.

Signed-off-by: Lucas Dietrich <lucas.dietrich@socomec.com>
2023-09-20 08:57:20 +02:00
Gerard Marull-Paretas 6ca6cff9c8 lorawan: services: add missing init.h
File was using SYS_INIT, from init.h.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-08-30 11:37:53 +02:00
Daniel Leung 5395ddfeb2 lorawan: rename shadow variables
Rename shadow variables found by -Wshadow.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-08-22 11:39:58 +02:00
Jordan Yates bf3872a05c lorawan: initialise data structures earlier
Now that `lorawan_init` only initialises data structures and does not
start the stack or communicate with a physical device, run the init as
soon as possible. This allows `lorawan_register_downlink_callback` to
be called much earlier by other init functions.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2023-06-01 13:44:16 -04:00
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Martin Jäger 3b9ba15096 lorawan: services: add Application Layer Clock Synchronization
This service allows to synchronize the clock with the application
server.

Synchronization requests are initiated by the device in a regular
interval, configurable via Kconfig.

The implementation only supports TS003-2.0.0, as the previous revision
TS003-1.0.0 requested to temporarily disable ADR and and set nb_trans
to 1. This causes issues on the server side and is not recommended
anymore.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-01-23 10:05:49 +00:00
Martin Jäger a9dc566a18 lorawan: add common backend for services
This is a prepartion for adding actual services needed for firmware
upgrade over the air (FUOTA).

The services run in a dedicated work queue.

This commit introduces code that initializes the work queue and
provides functions to schedule uplink messages after a given timeout.

Signed-off-by: Martin Jäger <martin@libre.solar>
2023-01-23 10:05:49 +00:00
Martin Jäger 22c8a67d4a lorawan: enforce larger system work queue stack size via Kconfig
The LoRaWAN subsystem uses the system work queue internally and needs
more than the default stack size of 1024 bytes. This is considered
in the sample application, but may be forgotten by out of tree users.

Enforcing it via Kconfig prevents users from accidentally getting
hard-to-debug stack overflows.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-11-28 10:49:18 +01:00
Benjamin Lindqvist 85c6de4335 lorawan: enable run-time config of region/freq
This commit adds support for compiling in support for several different
regions/frequencies and dynamically choosing which to use in run-time.
This commit introduces no API breakages - if a prj.conf contains only a
single region Kconfig, the new function lorawan_set_region() does not
need to be called.

Signed-off-by: Benjamin Lindqvist <benjamin@eub.se>
2022-11-14 11:16:16 +00:00
Francois Ramu f37ebf48ec include: add missing kernel.h for lorawan subsys
Header lora.h and lorawan.c files make use of types defined
in kernel.h without including it.
The types.h is no more relevant with inclusion of kernel.h
Explicitely including <stdint.h>, even if the kernel.h includes
the stdint.h, this is an implementation detail. "If Kernel
decides one day to drop usage of stdint.h (unlikely),
lora.h users be in trouble."

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2022-10-17 18:10:10 +09:00
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Martin Jäger 2590a7aa98 lorawan: upgrade to LoRaMAC-node v4.6.0
This release contains some bug fixes and improvements.

See below link for details:
https://github.com/Lora-net/LoRaMac-node/releases/tag/v4.6.0

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-08-03 05:02:57 +01:00
Jordan Yates f12d36a51e lorawan: move REQUIRES_FULL_LIBC dependency
Move the `REQUIRES_FULL_LIBC` dependency from `config LORA` to
`config LORAWAN`. The commit that added the `select` (f590d4fa) mentions
that this is required by `loramac-node`, which is only used by LoRaWAN,
not the base LoRa code.

This results in small FLASH savings when compiling the samples, but can
result in larger savings in more complex applications:
```
// With REQUIRES_FULL_LIBC
west build -b 96b_wistrio zephyr/samples/drivers/lora/send/
[162/162] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       37708 B       128 KB     28.77%
            SRAM:        8832 B        32 KB     26.95%
        IDT_LIST:          0 GB         2 KB      0.00%

// Without REQUIRES_FULL_LIBC
[181/181] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       37444 B       128 KB     28.57%
            SRAM:        8832 B        32 KB     26.95%
        IDT_LIST:          0 GB         2 KB      0.00%
```

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-07-02 14:16:18 +02:00
Krzysztof Chruscinski 041f0e5379 all: logging: Remove log_strdup function
Logging v1 has been removed and log_strdup wrapper function is no
longer needed. Removing the function and its use in the tree.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-23 13:42:23 +02:00
Martin Jäger 65eede8194 lorawan: add LoRaWAN Class C support
The functions for Class C mode are already provided by LoRaMAC layer.

A device in this mode is listening for incoming downlink messages almost
continuously, which results in higher energy consumption.

Class C mode is required for FUOTA.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-06-20 09:17:33 -04:00
Martin Jäger ad55227903 lorawan: make function and variable names consistent
Use Zephyr coding style for variable names and functions where possible.

The function BoardGetUniqueId has to be kept as is to match LoRaMAC
library declarations.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-06-15 09:11:34 +02:00
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00
Giuliano Franchetto 6630f7fc07 lorawan: adding settings NVM for LoRaWAN
Adding a reference implementation of the Non-Volatile Memory module
needed to join any LoRaWAN network.

This NVM is based on the SETTINGS subsys to store all the required
key to join and communicate on a LoRaWAN network.

Without proper NVM, one may experience errors when using OTAA
to join the network, as the device may violate anti-replay
protection (depending on the version of LoRaWAN).

Signed-off-by: Giuliano Franchetto <giuliano.franchetto@intellinium.com>
2022-05-02 10:56:02 +02:00
Martin Jäger 9cc32af99e lorawan: allow setting of DevNonce for OTAA re-join
Starting with LoRaWAN 1.0.4 the DevNonce sent with the OTAA join must be
monotonically increasing for each new join with the same EUI. The DevNonce
should be stored in non-volatile memory by the application.

This commit uses a simple extension of the lorawan_join_otaa struct to
allow specifying the DevNonce.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-03-19 14:29:01 -04:00
Martin Jäger b6a907dd89 lorawan: make unconfirmed message type explicit
The enum did not contain an entry for unconfirmed messages. Instead, it
was only mentioned in the comment that 0 is the default for
unconfirmed messages.

This commit adds LORAWAN_MSG_UNCONFIRMED to the enum and changes the
parameter in the lorawan_send function to enum lorawan_message_type.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-03-15 10:37:36 +01:00
Torsten Rasmussen 4790f68c84 kconfig: lorawan: experimental settings now uses select EXPERIMENTAL
With the introduction of `EXPERIMENTAL` and `WARN_EXPERIMENTAL` in
Zephyr all subsys/lorawan settings having `[EXPERIMENTAL]` in their
prompt has has been updated to include `select EXPERIMENTAL` so that
developers can enable warnings when experimental features are enabled.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-10-25 10:46:48 +02:00
Manivannan Sadhasivam 573d70bdbf lorawan: Fix setting APP_KEY using MacMibSetRequest
For setting the APP_KEY, "mib_req.Param.AppKey" field should be used.
Fix it!

Fixes: #36540

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
2021-07-22 06:11:41 -04:00
Marcin Niestroj 24a4b0d852 lorawan: add name to the region choice in kconfig
Adding name allows to modify default choice by other modules or
applications.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2021-07-20 19:59:35 -04:00
Ilya Tagunov 05957b66cb lorawan: update LoRaMac-node and move CMakeLists.txt to the main repo
Update the LoRaMac-node library to the last stable release and fix
the Zephyr glue code to match it. Move CMakeLists.txt to the main
Zephyr repository to simplify loramac-node module maintenance.

Signed-off-by: Ilya Tagunov <tagunil@gmail.com>
2021-06-18 11:22:03 +02:00
Jordan Yates bbd53dcde0 lorawan: port oriented downlink callbacks
Add downlink callbacks on a per-port basis. A single message will be
handled as many times as users have registered matching ports. Callbacks
will also be run on "meta" downlink packets on port 0, such as confirmed
uplink acknowledgements.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-04-30 13:05:10 -05:00
Jordan Yates df66cca6b6 lorawan: always wait for MAC ops to complete
Wait for MAC operations to complete when transmitting. Unconfirmed
messages still open receive windows and can cause error conditions,
which are currently dropped.

It is also possible for a second send to be requested before the first
one has finished processing, which results in `LORAMAC_STATUS_BUSY`.
Empty frames (due to insufficient payload space) now also block until
the MAC layer is ready to accept new commands.

This change means the application no longer needs to guess-and-check
when it is possible to send unconfirmed messages.

Fixes #33456.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-04-20 12:42:56 +02:00
Jordan Yates 54d13ba589 lorawan: add callback for datarate changes
Add a callback to notify applications when the datarate has changed.
This allows applications to know when payload sizes have changed without
needing to call `lorawan_get_payload_sizes` before each transmission.

This also enables:
 * Monitoring of network conditions on the device
 * Determining if a network connection has been lost

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-28 16:36:57 -05:00
Jordan Yates bdf13bcc1d lorawan: store current datarate for ADR
Keep track of the current network datarate when ADR is enabled.

The datarate can change in two circumstances. Firstly, the datarate may
change as a result of a command from the LoRaWAN server as the link
budget varies. Secondly, the datarate may be changed due to not
receiving an expected ADRACKReq response. This necessitates querying the
datarate on both packet reception and transmission to provide timely
notifications to the application.

Due to querying the default region datarate at startup and validating
manual datarate parameters, when ADR is not enabled the datarate will
never be different from the value provided to `lorawan_set_datarate`.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-28 16:36:57 -05:00
Jordan Yates 8c1a45b90d lorawan: queriable minimum datarate
Add a function to query the minimum possible datarate on the network.
This value may change over the lifetime of the connection as a result
of commands from the network server.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-28 16:36:57 -05:00
Jordan Yates fa09d0d612 lorawan: queriable payload sizes
Add the ability for applications to query the maximum size of packets
that can be sent. This must be dynamically queried as the sizes change
with datarate, region, and as MAC commands are added by the stack.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-28 16:36:57 -05:00
Kuba Sanak a17f19907b lorawan: Add userspace-defined battery level callback API
Provide the LoRaWAN stack with an optional callback to be
called whenever the battery level needs to be read.

In the case of callback not being provided, the LoRaWAN
stack will report 255 (battery level unavailable) as per
the LoRaWAN spec.

Signed-off-by: Kuba Sanak <contact@kuba.fyi>
2021-02-15 08:19:03 -05:00
Jordan Yates ddc595b662 lorawan: move public network request
Move the public network request command from the end of `lorawan_start`
to the start of `lorawan_join`. This MAC command interacts with the
radio driver, bringing it out of sleep mode, and doesn't put it back.

The public network setting is only needed when joining a network, and
`lorawan_start` can be called well before `lorawan_join`. By moving the
command we save ~600uA before joining (for sx1262).

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-10 08:55:15 -05:00
Jordan Yates b5b9f2c458 lorawan: restore datarate after join
Restore the user requested datarate upon a successful join. Several
regions overwrite the configured datarate through `RegionAlternateDr`,
which means that for these regions, `LoRaMacQueryTxPossible` will not
be evaluating the requested datarate for the first transmission after
joining.

Fixes #31551.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-03 10:45:41 -05:00
Jordan Yates f553007e06 lorawan: validate requested datarates
Validate datarates requested by users. As a side effect of this change
the MAC layer is immediately aware of updated datarates. Therefore
`LoRaMacQueryTxPossible` in `lorawan_send` will be using the same
datarate to evaluate the payload length as `LoRaMacMcpsRequest` is
requesting.

Fixes #31551.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-03 10:45:41 -05:00
Jordan Yates 83f43c727f lorawan: query default region datarate
Query the default datarate for the configured region, instead of
assuming that the default is DR_0.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-02-03 10:45:41 -05:00
Jordan Yates 875f6a5302 lorawan: fix premature return in lorawan_send
Fixes an issue where `lorawan_send` would return prematurely when
`LORAWAN_MSG_CONFIRMED` is mixed with unconfirmed messages.

All calls to `LoRaMacMcpsRequest` result in `McpsConfirm` being run,
where the semaphore `mcps_confirm_sem` is given. However this semaphore
is only taken when `LORAWAN_MSG_CONFIRMED` is set.

Therefore if an unconfirmed message is sent, any following confirmed
messages will return from `lorawan_send` immediately as the semaphore
will be available from the previous send. The return value would also
be wrong for the same reasons.

Fixed by only giving the semaphore in situations when it is being
blocked on.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-01-19 09:02:43 -05:00
Andreas Sandberg cfb6257327 lorawan: Cleanup error handling code
The error handling code currently has a couple of issues:

 * It relies on ordered lists and upstream not changing any constants.

 * Converted messages are not stored in constant memory which means
   that log_strdup is needed whenever they are printed.

This change also factors out error handling to a separate file,
lw_priv.{c,h}, to facilitate reuse in a future secure element and
state storage implementation.

Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
2020-10-08 12:15:38 +02:00