LwM2M client utils

The LwM2M client utils library enables an nRF9160-based device to connect to an LwM2M server such as Leshan Demo Server using the Lightweight Machine to Machine (LwM2M) protocol over LTE. Once the device is connected, the library supports the querying the device to retrieve location data or information about the modem.

The library provides a basic combination of LwM2M objects, which forms a common communication framework for applications that communicate with an LwM2M server. Following are the fixed set of readily initialized objects that are available to the users:

  • Firmware object

  • Connection monitoring object

  • Device object

  • Location object

  • Security object

These objects do not indicate a complete set of resources that a device is expected to support. Based on the use case, a user application can, and is expected to, define additional set of resources based on the capabilities of the device.

LwM2M client utils software stack

Configuration and implementation

By default, the library uses LTE-M for connecting and it does not utilize a bootstrap server. The library does not use LwM2M Queue mode also. To use NB-IoT, a bootstrap server, or the queue mode, follow the implementation details described in the nRF9160: LwM2M Client sample.

Configure the following parameters when using this library:

Support for the objects can be set individually but are enabled by default. Disable the CONFIG_LWM2M_CLIENT_UTILS_DEVICE_OBJ_SUPPORT Kconfig option only if you are implementing a reboot resource on your application because of a mandatory requirement.

Defining custom objects

In addition to the basic objects, you can also create custom LwM2M objects. For most of the applications, the business logic might be implemented inside custom objects, which are either proprietary to the application or following an external specification like IPSO objects. In any case, the application can extend the resource tree by defining specific objects in addition to the objects that are already defined by LwM2M.

Before defining proprietary objects, check if a similar functionality is already defined in LwM2M Object and Resource Registry and use it instead of defining custom objects.


Zephyr’s Lightweight M2M (LWM2M) library has only a limited support for some IPSO objects. To extend the functionality beyond the supported objects, you must make changes to the internal engine as well.

To define custom objects, complete the following steps:

  1. Determine the object ID of the object.

  2. Identify the resources corresponding to the object.

  3. Determine the resource ID for a resource that must be customized.

  4. Form the resource path for the resource in the object ID/instance/resource ID format.

  5. Create a structure for storing the resource value.

  6. Define a read function that responds to the read requests for the resource value from the server.

  7. Pass the resource information to the LwM2M client utils library to register callbacks for the resource and to publish the sensor data.

The following example describes how you can define an object that follows the Generic Sensor definition from IPSO. To enable the support for Generic Sensor set the Kconfig option CONFIG_LWM2M_IPSO_GENERIC_SENSOR to y.

To define an object that follows the Generic Sensor definition, complete the following steps:

  1. Determine the object ID of the object:

    Refer LwM2M Object and Resource Registry list and observe that the object ID of the Generic Sensor object is 3300.

  2. Identify the resources corresponding to the object:

    Click on the entry for the object ID(3300 in this example) in the LwM2M Object and Resource Registry list to open a raw XML file or open the user friendly LwM2M editor to determine the resources that are defined for the specific the object ID. The following table shows the resource list corresponding to the Generic Sensor object:









    Sensor Value





    Last or Current Measured Value from the Sensor.


    Sensor Units





    Measurement Units Definition.


    Min Measured Value





    The minimum value measured by the sensor since power ON or reset.


    Max Measured Value





    The maximum value measured by the sensor since power ON or reset.

  3. Determine the resource ID for a resource that must be customized:

    This example changes only the single resource that is marked Mandatory in the above table, which is Sensor Value. You can see that the resource ID for the Sensor Value resource is 5700.

  4. Form the resource path for the resource in the object ID/instance/resource ID format:

    LwM2M uses resource paths in the object ID/instance/resource ID format. The object ID in the example is 3300 and since it is the first instance of the object, the instance value is 0. Therefore, the full path for the sensor value will be 3300/0/5700. You must use this path in the LwM2M client utils library API.

  5. Create a structure for storing the resource value:

    In the example, the datatype of the resource is float. Use the following structure to store the float values associated with the resource:

    typedef struct float32_value {
       int32_t val1;
       int32_t val2;
    } float32_value_t;

    The variable val1 corresponds to the integer component of the value and val2 corresponds to the decimal component, which is multiplied by the constant LWM2M_FLOAT32_DEC_MAX that is equal to 1000000.

  6. Define a read function that responds to the read requests for the resource value from the server:

    static struct float32_value sensor_value = { 1, 0 };
     static void *read_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, size_t *data_len)
        /* Only object instance 0 is currently used */
        if (obj_inst_id != 0) {
           *data_len = 0;
        return NULL;
        /* Demo: change the sensor value */
        /* Return sensor value for the LwM2M library */
        lwm2m_engine_set_float32("3300/0/5700", &sensor_value);
        *data_len = sizeof(sensor_value);
        return &sensor_value;
  7. Pass the resource information to the LwM2M client utils library to register callbacks for the resource and to publish the sensor data:

    int init_resource(void)
       lwm2m_engine_register_read_callback("3300/0/5700", read_cb);
       return 0;

    The above code registers the object instance and passes the resource information to the library to register the read callback.

At this stage, the generic sensor is fully functional. For defining outputs, the process is very much similar but instead of read callback, write callback is defined.

Registering a read callback is optional and is recommended if you want to read the data directly from a sensor on each read operation. If the value of a readable resource is modified on an event, a read callback need not be registered. An example is the Push Button object. On receipt of an event that is triggered by button press or release, the value is updated through the lwm2m_engine with lwm2m_engine_set_bool(). When a read operation is issued by the server, the engine obtains the button value directly from the object’s internal data instead of the read callback. This causes the internal engine to allocate memory and store all the resources that are defined for the IPSO object ID.

Extending the library with new object types

If the library is not supporting the object type that you need, it is possible to extend the support by introducing completely new object types. This is currently possible only by using an internal API from the LwM2M engine.

Before creating new object types, see the existing implementation of IPSO objects from zephyr/subsys/net/lib/lwm2m directory. Select one of the existing object types, for example Push Button, and refactor it according to your need.

The following example shows how to create a new object type that follows the IPSO Digital Output definition:

  1. Define the following IDs:

    #define IPSO_DIGITAL_OUTPUT_ID        3201
    #define OUTPUT_DIGITAL_STATE_ID        5550
  2. Define the storage for the output type:

    /* resource state */
    struct output_data {
       bool state;
    static struct output_data output_data[MAX_INSTANCE_COUNT];
  3. Define the resources for the object type:

     #define N_RESOURCES        1
     static struct lwm2m_engine_obj_field fields[] = {

    The above code defines only a single resource.

  4. Define the structures that are required by the engine:

    static struct lwm2m_engine_obj output_obj;
    static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
    static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][N_RESOURCES];
    static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT][N_RESOURCES];
  5. Pass the information about how you want to create the object to the LwM2M engine:

    static struct lwm2m_engine_obj_inst *output_create(uint16_t id)
       /* Check that there is no other instance with this ID */
       /* It is assumed that the instance ID is same as the index in the array */
       if (inst[id].obj) {
          LOG_ERR("Cannot create instance - already existing: %u", id);
          return NULL;
       if (id >= MAX_INSTANCE_COUNT) {
          LOG_ERR("Cannot create instance - no more room: %u", id);
          return NULL;
       /* Set default values */
       (void)memset(&output_data[id], 0, sizeof(output_data[id]));
       (void)memset(res[id], 0, sizeof(res[id][0]) * ARRAY_SIZE(res[id]));
       init_res_instance(res_inst[id], ARRAY_SIZE(res_inst[id]));
       /* initialize instance resource data */
       int i = 0, j = 0;
             res_inst[id], j, 1, false, true,
             NULL, NULL, NULL, NULL);
       inst[id].resources = res[id];
       inst[id].resource_count = i;
       LOG_DBG("Created IPSO Output instance: %d", id);
       return &inst[id];
  6. Register the new object type with the engine:

    int ipso_output_init()
       output_obj.obj_id = IPSO_DIGITAL_OUTPUT_ID;
       output_obj.fields = fields;
       output_obj.field_count = ARRAY_SIZE(fields);
       output_obj.max_instance_count = ARRAY_SIZE(inst);
       output_obj.create_cb = output_create;
       lwm2m_engine_create_obj_inst(LWM2M_PATH(3201, 0));
       lwm2m_engine_register_post_write_callback(RESOURCE_PATH, on_off_cb);
       return 0;

    As shown in the above code, the instance is created, and a callback is attached to it. The content of the callback is similar as in the Generic Sensor example. Some details are left out in these examples and for more information, see the existing IPSO objects from the LwM2M engine.

API documentation

Header files: include/net/lwm2m_client_utils.h, include/net/lwm2m_client_utils_fota.h
Source files: subsys/net/lib/lwm2m_client_utils/fota, subsys/net/lib/lwm2m_client_utils/lwm2m
group lwm2m_client_utils

LwM2M Client utilities to build an application.

The client provides APIs for:

  • connecting to a remote server

  • setting up default resources

    • Firmware

    • Connection monitoring

    • Device

    • Location

    • Security


int lwm2m_init_security(struct lwm2m_ctx *ctx, char *endpoint)

Initialize Security object.

int lwm2m_init_device(void)

Initialize Device object.

int lwm2m_init_location(void)

Initialize Location object.

void *firmware_read_cb(uint16_t obj_inst_id, size_t *data_len)

Firmware read callback.

int lwm2m_init_firmware(void)

Verify active firmware image.

int lwm2m_init_image(void)

Initialize Image Update object.

void lwm2m_verify_modem_fw_update(void)

Verifies modem firmware update.

int lwm2m_init_connmon(void)

Initialize Connection Monitor object.

int lwm2m_update_connmon(void)

Update Connection Monitor object.

API for the LwM2M based FOTA.


enum counter_type

Counter type.



Active image’s counter


Update image’s counter


int fota_update_counter_read(struct update_counter *update_counter)

Read the update counter.

int fota_update_counter_update(enum counter_type type, uint32_t new_value)

Update the update counter.

int fota_settings_init(void)

Initialize FOTA settings.

struct update_counter
#include <lwm2m_client_utils_fota.h>

Update counter.

Public Members

int current

Active image’s state

int update

Update image’s state