Application integration

This section describes the needed interaction between the LwM2M carrier library and the user application.

Interaction between the LwM2M carrier library and the user application

Interaction between the LwM2M carrier library and the user application

The LwM2M carrier library has an OS abstraction layer. This abstraction layer makes the LwM2M carrier library independent of the nRF Connect SDK modules and underlying implementation of primitives such as timers, non-volatile storage, and heap allocation. For more information, see lwm2m_os.h.

LwM2M library OS abstraction overview

LwM2M library OS abstraction overview

It provides an abstraction of the following modules:

The OS abstraction layer is fully implemented for the nRF Connect SDK, and it needs to be ported if used with other RTOS or on other systems.

When the LwM2M carrier library is enabled in your application, it includes the file nrf/lib/bin/lwm2m_carrier/os/lwm2m_carrier.c. This automatically runs the library’s main function (lwm2m_carrier_main()).

Configuration

To run the library in an application, you must implement the application with the API of the library. Enable the library by setting the CONFIG_LWM2M_CARRIER Kconfig option to y.

The Cellular: LwM2M carrier sample project configuration (nrf/samples/cellular/lwm2m_carrier/prj.conf) contains all the configurations that are needed by the LwM2M carrier library.

To overwrite the carrier default settings, you can provide the initialization parameter lwm2m_carrier_config_t with the Kconfig options specified in the following sections. You can also use the provided LwM2M carrier shell configuration to quickly get started and experiment with the API.

General options

Following are some of the general Kconfig options that you can configure:

Server options

Following are some of the server Kconfig options that you can configure:

The server settings can put the LwM2M carrier library either in the normal mode where it connects to the applicable carriers, or in the generic mode where it can connect to any server.

Device options

These values are reported in the Device Object and are not expected to change during run time. These configurations can be left empty unless otherwise stated by your operator. The library will automatically set the values according to the detected operator.

Following are the device Kconfig options:

LwM2M carrier library events

The lwm2m_carrier_event_handler() function may be implemented by your application. This is shown in the Cellular: LwM2M carrier sample. A __weak implementation is included in nrf/lib/bin/lwm2m_carrier/os/lwm2m_carrier.c.

Following are the various LwM2M carrier library events that are also listed in nrf/lib/bin/lwm2m_carrier/include/lwm2m_carrier.h.

  • LWM2M_CARRIER_EVENT_LTE_LINK_DOWN:

    • This event indicates that the device must disconnect from the LTE network.

    • It occurs during the bootstrapping process and FOTA. It can also be triggered when the application calls lwm2m_carrier_request().

  • LWM2M_CARRIER_EVENT_LTE_LINK_UP:

    • This event indicates that the device must connect to the LTE network.

    • It occurs during the bootstrapping process and FOTA. It can also be triggered when the application calls lwm2m_carrier_request().

  • LWM2M_CARRIER_EVENT_BOOTSTRAPPED:

    • This event indicates that the bootstrap sequence is complete, and that the device is ready to be registered.

    • This event is typically seen during the first boot-up.

  • LWM2M_CARRIER_EVENT_REGISTERED:

    • This event indicates that the device has registered successfully to the carrier’s device management servers.

  • LWM2M_CARRIER_EVENT_DEFERRED:

  • LWM2M_CARRIER_EVENT_FOTA_START:

  • LWM2M_CARRIER_EVENT_FOTA_SUCCESS

    • This event indicates that the FOTA procedure is successful.

  • LWM2M_CARRIER_EVENT_REBOOT:

    • This event indicates that the LwM2M carrier library will reboot the device.

    • If the application is not ready to reboot, it must return non-zero and then reboot at the earliest convenient time.

  • LWM2M_CARRIER_EVENT_MODEM_INIT:

    • This event indicates that the application must initialize the modem for the LwM2M carrier library to proceed.

    • This event is indicated during FOTA procedures to reinitialize the Modem library integration layer.

  • LWM2M_CARRIER_EVENT_MODEM_SHUTDOWN:

    • This event indicates that the application must shut down the modem for the LwM2M carrier library to proceed.

    • This event is indicated during FOTA procedures to reinitialize the Modem library integration layer.

  • LWM2M_CARRIER_EVENT_ERROR:

    • This event indicates an error.

    • The event data struct lwm2m_carrier_event_error_t contains the information about the error (type and value).

    • Following are the valid error types:

      • LWM2M_CARRIER_ERROR_LTE_LINK_UP_FAIL - This error is generated if the request to connect to the LTE network has failed. It indicates possible problems with the SIM card, or insufficient network coverage. See value field of the event.

      • LWM2M_CARRIER_ERROR_LTE_LINK_DOWN_FAIL - This error is generated if the request to disconnect from the LTE network has failed.

      • LWM2M_CARRIER_ERROR_BOOTSTRAP - This error is generated during the bootstrap procedure.

        Errors

        More information

        Recovery

        Retry limit for connecting to the bootstrap server has been reached (-ETIMEDOUT).

        Common reason for this failure can be incorrect URI or PSK, or the server is unavailable (for example, temporary network issues). If this error persists, contact your carrier.

        Library retries after next device reboot.

        Failure to provision the PSK needed for the bootstrap procedure (-EACCES).

        The LTE link was up while the modem attempted to write keys to the modem. Verify that the application prioritizes the LWM2M_CARRIER_EVENT_LTE_LINK_UP and LWM2M_CARRIER_EVENT_LTE_LINK_DOWN events.

        Library retries after 24 hours.

        Failure to read MSISDN or ICCID values (-EFAULT).

        ICCID is fetched from SIM, while MSISDN is received from the network for some carriers. If it has not been issued yet, the bootstrap process cannot proceed.

        Library retries upon next network connection.

      • LWM2M_CARRIER_ERROR_FOTA_FAIL - This error indicates a failure to update the device. If this error persists, create a ticket in DevZone with the modem trace. The following error values may apply:

        • -EPERM - No valid security tag found. The security tag contains the certificate needed to secure the connection to the repository server. Check with your operator which certificates are needed for the firmware update, and make sure that you have provisioned these to the device.

        • -ENOMEM - Too many open connections to connect to the firmware repository. Pay attention to LWM2M_CARRIER_EVENT_FOTA_START, which prompts you to close any TLS socket.

        • -EBADF - Incorrect firmware update version. The Firmware could not be applied to the device. Check that you are providing the correct FOTA image and that it is compatible with the current firmware.

      • LWM2M_CARRIER_ERROR_CONFIGURATION - This error indicates that an illegal object configuration has been detected.

      • LWM2M_CARRIER_ERROR_INIT - This error indicates that the LwM2M carrier library has failed to initialize.

      • LWM2M_CARRIER_ERROR_RUN - This error indicates that the library configuration is invalid. Ensure that the lwm2m_carrier_config_t structure is configured correctly.

LwM2M carrier shell configuration

The LwM2M carrier shell allows you to interact with the carrier library through the shell command line. This allows you to overwrite initialization parameters and call the different runtime APIs of the library. This can be useful for getting started and debugging. See Shell for more information.

To enable and configure the LwM2M carrier shell, set the CONFIG_LWM2M_CARRIER_SHELL Kconfig option to y. The CONFIG_LWM2M_CARRIER_SHELL Kconfig option has the following dependencies:

In the Cellular: LwM2M carrier sample, you can enable the LwM2M carrier shell by building with the overlay file overlay-shell.conf.

LwM2M carrier shell

LwM2M carrier shell

carrier_config

The initialization parameter lwm2m_carrier_config_t can be overwritten with custom settings through the LwM2M carrier shell command group carrier_config. Use the print command to display the configurations that are written with carrier_config:

> carrier_config print
Automatic startup                No

Custom carrier settings          Yes
  Carriers enabled               All
  Bootstrap from smartcard       Yes
  Session idle timeout           0
  CoAP confirmable interval      0
  APN
  PDN type                       IPv4v6
  Service code
  Device Serial Number type      0

Custom carrier server settings   No
  Is bootstrap server            No  (Not used without server URI)
  Server URI
  PSK security tag               0
  Server lifetime                0
  Server binding                 U

Custom carrier device settings   No
  Manufacturer
  Model number
  Device type
  Hardware version
  Software version

To allow time to change configurations before the library applies them, the application waits in the initialization phase (lwm2m_carrier_custom_init()) until auto_startup is set.

uart:~$ carrier_config auto_startup y
Set auto startup: Yes

The settings are applied by the function lwm2m_carrier_custom_init().

This function is implemented in nrf/lib/bin/lwm2m_carrier/os/lwm2m_settings.c that is included in the project when you enable the LwM2M carrier shell. The library thread calls the lwm2m_carrier_custom_init() function before calling the lwm2m_carrier_main() function.

carrier_api

The LwM2M carrier shell command group carrier_api allows you to access the public LwM2M API as shown in nrf/lib/bin/lwm2m_carrier/include/lwm2m_carrier.h.

For example, to indicate the battery level of the device to the carrier, the function lwm2m_carrier_battery_level_set() is used. This can also be done through the carrier_api command:

> carrier_api device battery_level 20
Battery level updated successfully

help

To display help for all available shell commands, pass the following command to shell:

> [group] help

If the optional argument is not provided, the command displays help for all command groups.

If the optional argument is provided, it displays help for subcommands of the specified command group. For example, carrier_config help displays help for all carrier_config commands.

Objects

The objects enabled depend on the carrier network. When connecting to a generic LwM2M Server, the following objects are enabled:

  • Security

  • Server

  • Access Control

  • Device

  • Connectivity Monitoring

  • Firmware Update

  • Location

  • Connectivity Statistics

  • Cellular Connectivity

  • APN Connection Profile

  • Binary App Data Container

  • Event Log

Resources

The following values that reflect the state of the device must be kept up to date by the application:

  • Available Power Sources - Defaults to 0 if not set (DC Power).

  • Power Source Voltage - Defaults to 0 if not set.

  • Power Source Current - Defaults to 0 if not set.

  • Battery Level - Defaults to 0 if not set.

  • Battery Status - Defaults to 5 if not set (Not Installed).

  • Memory Total - Defaults to 0 if not set.

  • Error Code - Defaults to 0 if not set (No Error).

  • Device Type - Defaults to Module if not set.

  • Software Version - Defaults to LwM2M <libversion>. For example, LwM2M carrier 3.4.0 for release 3.4.0.

  • Hardware Version - Default value is read from the modem. An example value is nRF9161 LACA ADA.

  • Location - Defaults to 0 if not set.

The following values are read from the modem by default but can be overwritten:

  • Manufacturer

  • Model Number

  • UTC Offset

  • Time zone

  • Current Time

For example, the carrier device management platform can observe the battery level of your device. The application uses the lwm2m_carrier_battery_level_set() function to indicate the current battery level of the device to the carrier.

DFU interface

The LwM2M carrier library makes use of the DFU target library to manage the DFU process, providing a single interface to support different types of firmware upgrades. Currently, the following types of firmware upgrades are supported:

The type of upgrade is determined when the library calls the lwm2m_os_dfu_img_type() function in the abstraction layer upon receiving a new firmware image.

If MCUboot-style upgrades are enabled, the LwM2M carrier library uses the function lwm2m_os_dfu_application_update_validate() to validate the application image update. A __weak implementation of the function is included, which checks if the currently running image is not yet confirmed as valid (which is the case after an upgrade) and marks it appropriately.