Commit graph

35 commits

Author SHA1 Message Date
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 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
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
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
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
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
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
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
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
Kuba Sanak 9c976a2cad lorawan: Rework error handling code
For consistency with other parts of Zephyr, the public APIs available
in lorawan subsystem now returns error codes from the set defined in
errno.h.

Signed-off-by: Kuba Sanak <contact@kuba.fyi>
[mani: reworked the code and commit a bit for upstream]
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Kuba Sanak b44dcf88d9 lorawan: Add support for SystemMaxRxError
SystemMaxRxError is used to negotiate overall timing error for Rx
in the loramac-node library. Hence, add support for configuring this
parameter from Kconfig.

Signed-off-by: Kuba Sanak <contact@kuba.fyi>
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00
Manivannan Sadhasivam 3ce8540f3a lorawan: Add initial support for LoRaWAN
Add initial support for LoRaWAN based on Semtech's loramac-node
library. Current implementation only supports OTAA config and
sending data to LoRaWAN server like ThingsNetwork.

While at it, this commit also moves the "loramac-node" library
definition from drivers/lora to subsys/lorawan. This is required
because, subsys/lorawan gets processed before drivers/lora and
that creates issue while building.

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
2020-10-08 12:15:38 +02:00