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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>