Lightweight M2M (LWM2M)

Overview

Lightweight Machine to Machine (LwM2M) is an application layer protocol designed with device management, data reporting and device actuation in mind. Based on CoAP/UDP, LwM2M is a standard defined by the Open Mobile Alliance and suitable for constrained devices by its use of CoAP packet-size optimization and a simple, stateless flow that supports a REST API.

One of the key differences between LwM2M and CoAP is that an LwM2M client initiates the connection to an LwM2M server. The server can then use the REST API to manage various interfaces with the client.

LwM2M uses a simple resource model with the core set of objects and resources defined in the specification.

Example LwM2M object and resources: Device

Object definition

Object ID

Name

Instance

Mandatory

3

Device

Single

Mandatory

Resource definitions

* R=Read, W=Write, E=Execute

ID

Name

OP*

Instance

Mandatory

Type

0

Manufacturer

R

Single

Optional

String

1

Model

R

Single

Optional

String

2

Serial number

R

Single

Optional

String

3

Firmware version

R

Single

Optional

String

4

Reboot

E

Single

Mandatory

5

Factory Reset

E

Single

Optional

6

Available Power Sources

R

Multiple

Optional

Integer 0-7

7

Power Source Voltage (mV)

R

Multiple

Optional

Integer

8

Power Source Current (mA)

R

Multiple

Optional

Integer

9

Battery Level %

R

Single

Optional

Integer

10

Memory Free (Kb)

R

Single

Optional

Integer

11

Error Code

R

Multiple

Optional

Integer 0-8

12

Reset Error

E

Single

Optional

13

Current Time

RW

Single

Optional

Time

14

UTC Offset

RW

Single

Optional

String

15

Timezone

RW

Single

Optional

String

16

Supported Binding

R

Single

Mandatory

String

17

Device Type

R

Single

Optional

String

18

Hardware Version

R

Single

Optional

String

19

Software Version

R

Single

Optional

String

20

Battery Status

R

Single

Optional

Integer 0-6

21

Memory Total (Kb)

R

Single

Optional

Integer

22

ExtDevInfo

R

Multiple

Optional

ObjLnk

The server could query the Manufacturer resource for Device object instance 0 (the default and only instance) by sending a READ 3/0/0 operation to the client.

The full list of registered objects and resource IDs can be found in the LwM2M registry.

Zephyr’s LwM2M library lives in the subsys/net/lib/lwm2m, with a client sample in samples/net/lwm2m_client. For more information about the provided sample see: LwM2M client The sample can be configured to use normal unsecure network sockets or sockets secured via DTLS.

The Zephyr LwM2M library implements the following items:

  • engine to process networking events and core functions

  • RD client which performs BOOTSTRAP and REGISTRATION functions

  • TLV, JSON, and plain text formatting functions

  • LwM2M Technical Specification Enabler objects such as Security, Server, Device, Firmware Update, etc.

  • Extended IPSO objects such as Light Control, Temperature Sensor, and Timer

The library currently implements up to LwM2M specification 1.0.2.

For more information about LwM2M visit OMA Specworks LwM2M.

Sample usage

To use the LwM2M library, start by creating an LwM2M client context lwm2m_ctx structure:

/* LwM2M client context */
static struct lwm2m_ctx client;

Create callback functions for LwM2M resources that you wish to have actions for:

static int device_reboot_cb(uint16_t obj_inst_id)
{
        LOG_INF("Device rebooting.");
        LOG_PANIC();
        sys_reboot(0);
        return 0; /* wont reach this */
}

The LwM2M RD client can send events back to the sample. To receive those events, setup a callback function:

static void rd_client_event(struct lwm2m_ctx *client,
                            enum lwm2m_rd_client_event client_event)
{
        switch (client_event) {

        case LWM2M_RD_CLIENT_EVENT_NONE:
                /* do nothing */
                break;

        case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE:
                LOG_DBG("Bootstrap registration failure!");
                break;

        case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE:
                LOG_DBG("Bootstrap registration complete");
                break;

        case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_TRANSFER_COMPLETE:
                LOG_DBG("Bootstrap transfer complete");
                break;

        case LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE:
                LOG_DBG("Registration failure!");
                break;

        case LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE:
                LOG_DBG("Registration complete");
                break;

        case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE:
                LOG_DBG("Registration update failure!");
                break;

        case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE:
                LOG_DBG("Registration update complete");
                break;

        case LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE:
                LOG_DBG("Deregister failure!");
                break;

        case LWM2M_RD_CLIENT_EVENT_DISCONNECT:
                LOG_DBG("Disconnected");
                break;

        }
}

Next we assign Security resource values to let the client know where and how to connect as well as set the Manufacturer and Reboot resources in the Device object with some data and the callback we defined above:

/*
 * Server URL of default Security object = 0/0/0
 * Use leshan.eclipse.org server IP (5.39.83.206) for connection
 */
lwm2m_engine_set_string("0/0/0", "coap://5.39.83.206");

/*
 * Security Mode of default Security object = 0/0/2
 * 3 = NoSec mode (no security beware!)
 */
lwm2m_engine_set_u8("0/0/2", 3);

#define CLIENT_MANUFACTURER "Zephyr Manufacturer"

/*
 * Manufacturer resource of Device object = 3/0/0
 * We use lwm2m_engine_set_res_data() function to set a pointer to the
 * CLIENT_MANUFACTURER string.
 * Note the LWM2M_RES_DATA_FLAG_RO flag which stops the engine from
 * trying to assign a new value to the buffer.
 */
lwm2m_engine_set_res_data("3/0/0", CLIENT_MANUFACTURER,
                          sizeof(CLIENT_MANUFACTURER),
                          LWM2M_RES_DATA_FLAG_RO);

/* Reboot resource of Device object = 3/0/4 */
lwm2m_engine_register_exec_callback("3/0/4", device_reboot_cb);

Lastly, we start the LwM2M RD client (which in turn starts the LwM2M engine). The second parameter of lwm2m_rd_client_start() is the client endpoint name. This is important as it needs to be unique per LwM2M server:

(void)memset(&client, 0x0, sizeof(client));
lwm2m_rd_client_start(&client, "unique-endpoint-name", 0, rd_client_event);

Using LwM2M library with DTLS

The Zephyr LwM2M library can be used with DTLS transport for secure communication by selecting CONFIG_LWM2M_DTLS_SUPPORT. In the client initialization we need to create a PSK and identity. These need to match the security information loaded onto the LwM2M server. Normally, the endpoint name is used to lookup the related security information:

/* "000102030405060708090a0b0c0d0e0f" */
static unsigned char client_psk[] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

static const char client_identity[] = "Client_identity";

Next we alter the Security object resources to include DTLS security information. The server URL should begin with coaps:// to indicate security is required. Assign a 0 value (Pre-shared Key mode) to the Security Mode resource. Lastly, set the client identity and PSK resources.

/* Use coaps:// for server URL protocol */
lwm2m_engine_set_string("0/0/0", "coaps://5.39.83.206");
/* 0 = Pre-Shared Key mode */
lwm2m_engine_set_u8("0/0/2", 0);
/* Set the client identity */
lwm2m_engine_set_string("0/0/3", (char *)client_identity);
/* Set the client pre-shared key (PSK) */
lwm2m_engine_set_opaque("0/0/5", (void *)client_psk, sizeof(client_psk));

Before calling lwm2m_rd_client_start() assign the tls_tag # where the LwM2M library should store the DTLS information prior to connection (normally a value of 1 is ok here).

(void)memset(&client, 0x0, sizeof(client));
client.tls_tag = 1; /* <---- */
lwm2m_rd_client_start(&client, "endpoint-name", 0, rd_client_event);

For a more detailed LwM2M client sample see: LwM2M client.

API Reference

group lwm2m_api

LwM2M high-level API.

LwM2M high-level interface is defined in this header.

Note

The implementation assumes UDP module is enabled.

Note

LwM2M 1.0.x is currently the only supported version.

Defines

LWM2M_OBJECT_SECURITY_ID

LwM2M Objects managed by OMA for LwM2M tech specification. Objects in this range have IDs from 0 to 1023. For more information refer to Technical Specification OMA-TS-LightweightM2M-V1_0_2-20180209-A.

LWM2M_OBJECT_SERVER_ID
LWM2M_OBJECT_ACCESS_CONTROL_ID
LWM2M_OBJECT_DEVICE_ID
LWM2M_OBJECT_CONNECTIVITY_MONITORING_ID
LWM2M_OBJECT_FIRMWARE_ID
LWM2M_OBJECT_LOCATION_ID
LWM2M_OBJECT_CONNECTIVITY_STATISTICS_ID
IPSO_OBJECT_GENERIC_SENSOR_ID

LwM2M Objects produced by 3rd party Standards Development Organizations. Objects in this range have IDs from 2048 to 10240 Refer to the OMA LightweightM2M (LwM2M) Object and Resource Registry: http://www.openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html.

IPSO_OBJECT_TEMP_SENSOR_ID
IPSO_OBJECT_HUMIDITY_SENSOR_ID
IPSO_OBJECT_LIGHT_CONTROL_ID
IPSO_OBJECT_ACCELEROMETER_ID
IPSO_OBJECT_PRESSURE_ID
IPSO_OBJECT_BUZZER_ID
IPSO_OBJECT_TIMER_ID
IPSO_OBJECT_ONOFF_SWITCH_ID
IPSO_OBJECT_PUSH_BUTTON_ID
LWM2M_DEVICE_PWR_SRC_TYPE_DC_POWER

Power source types used for the “Available Power Sources” resource of the LwM2M Device object.

LWM2M_DEVICE_PWR_SRC_TYPE_BAT_INT
LWM2M_DEVICE_PWR_SRC_TYPE_BAT_EXT
LWM2M_DEVICE_PWR_SRC_TYPE_UNUSED
LWM2M_DEVICE_PWR_SRC_TYPE_PWR_OVER_ETH
LWM2M_DEVICE_PWR_SRC_TYPE_USB
LWM2M_DEVICE_PWR_SRC_TYPE_AC_POWER
LWM2M_DEVICE_PWR_SRC_TYPE_SOLAR
LWM2M_DEVICE_PWR_SRC_TYPE_MAX
LWM2M_DEVICE_ERROR_NONE

Error codes used for the “Error Code” resource of the LwM2M Device object. An LwM2M client can register one of the following error codes via the lwm2m_device_add_err() function.

LWM2M_DEVICE_ERROR_LOW_POWER
LWM2M_DEVICE_ERROR_EXT_POWER_SUPPLY_OFF
LWM2M_DEVICE_ERROR_GPS_FAILURE
LWM2M_DEVICE_ERROR_LOW_SIGNAL_STRENGTH
LWM2M_DEVICE_ERROR_OUT_OF_MEMORY
LWM2M_DEVICE_ERROR_SMS_FAILURE
LWM2M_DEVICE_ERROR_NETWORK_FAILURE
LWM2M_DEVICE_ERROR_PERIPHERAL_FAILURE
LWM2M_DEVICE_BATTERY_STATUS_NORMAL

Battery status codes used for the “Battery Status” resource (3/0/20) of the LwM2M Device object. As the battery status changes, an LwM2M client can set one of the following codes via: lwm2m_engine_set_u8(“3/0/20”, [battery status])

LWM2M_DEVICE_BATTERY_STATUS_CHARGING
LWM2M_DEVICE_BATTERY_STATUS_CHARGE_COMP
LWM2M_DEVICE_BATTERY_STATUS_DAMAGED
LWM2M_DEVICE_BATTERY_STATUS_LOW
LWM2M_DEVICE_BATTERY_STATUS_NOT_INST
LWM2M_DEVICE_BATTERY_STATUS_UNKNOWN
STATE_IDLE

LWM2M Firmware Update object states.

An LwM2M client or the LwM2M Firmware Update object use the following codes to represent the LwM2M Firmware Update state (5/0/3).

STATE_DOWNLOADING
STATE_DOWNLOADED
STATE_UPDATING
RESULT_DEFAULT

LWM2M Firmware Update object result codes.

After processing a firmware update, the client sets the result via one of the following codes via lwm2m_engine_set_u8(“5/0/5”, [result code])

RESULT_SUCCESS
RESULT_NO_STORAGE
RESULT_OUT_OF_MEM
RESULT_CONNECTION_LOST
RESULT_INTEGRITY_FAILED
RESULT_UNSUP_FW
RESULT_INVALID_URI
RESULT_UPDATE_FAILED
RESULT_UNSUP_PROTO
LWM2M_FLOAT32_DEC_MAX

Data structure used to represent the LwM2M float type: val1 is the whole number portion of the decimal val2 is the decimal portion *1000000 for 32bit, *1000000000 for 64bit Example: 123.456 == val1: 123, val2:456000 Example: 123.000456 = val1: 123, val2:456.

Maximum precision value for 32-bit LwM2M float val2

LWM2M_FLOAT64_DEC_MAX

Maximum precision value for 64-bit LwM2M float val2.

LWM2M_OBJLNK_MAX_ID

Maximum value for ObjLnk resource fields.

LWM2M_RES_DATA_READ_ONLY

Resource read-only value bit.

LWM2M_RES_DATA_FLAG_RO

Resource read-only flag.

LWM2M_HAS_RES_FLAG(res, f)

Read resource flags helper macro.

LWM2M_RD_CLIENT_FLAG_BOOTSTRAP

Run bootstrap procedure in current session.

Typedefs

typedef void (*lwm2m_socket_fault_cb_t)(int error)
typedef void *(*lwm2m_engine_get_data_cb_t)(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, size_t *data_len)

Asynchronous callback to get a resource buffer and length.

Prior to accessing the data buffer of a resource, the engine can use this callback to get the buffer pointer and length instead of using the resource’s data buffer.

The client or LwM2M objects can register a function of this type via: lwm2m_engine_register_read_callback() lwm2m_engine_register_pre_write_callback()

Return

Callback returns a pointer to the data buffer or NULL for failure.

Parameters
  • [in] obj_inst_id: Object instance ID generating the callback.

  • [in] res_id: Resource ID generating the callback.

  • [in] res_inst_id: Resource instance ID generating the callback (typically 0 for non-multi instance resources).

  • [out] data_len: Length of the data buffer.

typedef int (*lwm2m_engine_set_data_cb_t)(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, uint8_t *data, uint16_t data_len, bool last_block, size_t total_size)

Asynchronous callback when data has been set to a resource buffer.

After changing the data of a resource buffer, the LwM2M engine can make use of this callback to pass the data back to the client or LwM2M objects.

A function of this type can be registered via: lwm2m_engine_register_post_write_callback()

Return

Callback returns a negative error code (errno.h) indicating reason of failure or 0 for success.

Parameters
  • [in] obj_inst_id: Object instance ID generating the callback.

  • [in] res_id: Resource ID generating the callback.

  • [in] res_inst_id: Resource instance ID generating the callback (typically 0 for non-multi instance resources).

  • [in] data: Pointer to data.

  • [in] data_len: Length of the data.

  • [in] last_block: Flag used during block transfer to indicate the last block of data. For non-block transfers this is always false.

  • [in] total_size: Expected total size of data for a block transfer. For non-block transfers this is 0.

typedef int (*lwm2m_engine_user_cb_t)(uint16_t obj_inst_id)

Asynchronous event notification callback.

Various object instance and resource-based events in the LwM2M engine can trigger a callback of this function type: object instance create, object instance delete and resource execute.

Register a function of this type via: lwm2m_engine_register_exec_callback() lwm2m_engine_register_create_callback() lwm2m_engine_register_delete_callback()

Return

Callback returns a negative error code (errno.h) indicating reason of failure or 0 for success.

Parameters
  • [in] obj_inst_id: Object instance ID generating the callback.

typedef struct float32_value float32_value_t

32-bit variant of the LwM2M float structure

typedef struct float64_value float64_value_t

32-bit variant of the LwM2M float structure

typedef void (*lwm2m_ctx_event_cb_t)(struct lwm2m_ctx *ctx, enum lwm2m_rd_client_event event)

Asynchronous RD client event callback.

Parameters
  • [in] ctx: LwM2M context generating the event

  • [in] event: LwM2M RD client event code

Enums

enum lwm2m_rd_client_event

LwM2M RD client events.

LwM2M client events are passed back to the event_cb function in lwm2m_rd_client_start()

Values:

enumerator LWM2M_RD_CLIENT_EVENT_NONE
enumerator LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE
enumerator LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE
enumerator LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_TRANSFER_COMPLETE
enumerator LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE
enumerator LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE
enumerator LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE
enumerator LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE
enumerator LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE
enumerator LWM2M_RD_CLIENT_EVENT_DISCONNECT
enumerator LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF
enumerator LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR

Functions

int lwm2m_device_add_err(uint8_t error_code)

Register a new error code with LwM2M Device object.

Return

0 for success or negative in case of error.

Parameters
  • [in] error_code: New error code.

int lwm2m_engine_create_obj_inst(char *pathstr)

Create an LwM2M object instance.

LwM2M clients use this function to create non-default LwM2M objects: Example to create first temperature sensor object: lwm2m_engine_create_obj_inst(“3303/0”);

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst”

int lwm2m_engine_set_opaque(char *pathstr, char *data_ptr, uint16_t data_len)

Set resource (instance) value (opaque buffer)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] data_ptr: Data buffer

  • [in] data_len: Length of buffer

int lwm2m_engine_set_string(char *pathstr, char *data_ptr)

Set resource (instance) value (string)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] data_ptr: NULL terminated char buffer

int lwm2m_engine_set_u8(char *pathstr, uint8_t value)

Set resource (instance) value (u8)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: u8 value

int lwm2m_engine_set_u16(char *pathstr, uint16_t value)

Set resource (instance) value (u16)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: u16 value

int lwm2m_engine_set_u32(char *pathstr, uint32_t value)

Set resource (instance) value (u32)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: u32 value

int lwm2m_engine_set_u64(char *pathstr, uint64_t value)

Set resource (instance) value (u64)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: u64 value

int lwm2m_engine_set_s8(char *pathstr, int8_t value)

Set resource (instance) value (s8)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: s8 value

int lwm2m_engine_set_s16(char *pathstr, int16_t value)

Set resource (instance) value (s16)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: s16 value

int lwm2m_engine_set_s32(char *pathstr, int32_t value)

Set resource (instance) value (s32)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: s32 value

int lwm2m_engine_set_s64(char *pathstr, int64_t value)

Set resource (instance) value (s64)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: s64 value

int lwm2m_engine_set_bool(char *pathstr, bool value)

Set resource (instance) value (bool)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: bool value

int lwm2m_engine_set_float32(char *pathstr, float32_value_t *value)

Set resource (instance) value (32-bit float structure)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: 32-bit float value

int lwm2m_engine_set_float64(char *pathstr, float64_value_t *value)

Set resource (instance) value (64-bit float structure)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: 64-bit float value

int lwm2m_engine_set_objlnk(char *pathstr, struct lwm2m_objlnk *value)

Set resource (instance) value (ObjLnk)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] value: pointer to the lwm2m_objlnk structure

int lwm2m_engine_get_opaque(char *pathstr, void *buf, uint16_t buflen)

Get resource (instance) value (opaque buffer)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] buf: Data buffer to copy data into

  • [in] buflen: Length of buffer

int lwm2m_engine_get_string(char *pathstr, void *str, uint16_t strlen)

Get resource (instance) value (string)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] str: String buffer to copy data into

  • [in] strlen: Length of buffer

int lwm2m_engine_get_u8(char *pathstr, uint8_t *value)

Get resource (instance) value (u8)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: u8 buffer to copy data into

int lwm2m_engine_get_u16(char *pathstr, uint16_t *value)

Get resource (instance) value (u16)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: u16 buffer to copy data into

int lwm2m_engine_get_u32(char *pathstr, uint32_t *value)

Get resource (instance) value (u32)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: u32 buffer to copy data into

int lwm2m_engine_get_u64(char *pathstr, uint64_t *value)

Get resource (instance) value (u64)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: u64 buffer to copy data into

int lwm2m_engine_get_s8(char *pathstr, int8_t *value)

Get resource (instance) value (s8)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: s8 buffer to copy data into

int lwm2m_engine_get_s16(char *pathstr, int16_t *value)

Get resource (instance) value (s16)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: s16 buffer to copy data into

int lwm2m_engine_get_s32(char *pathstr, int32_t *value)

Get resource (instance) value (s32)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: s32 buffer to copy data into

int lwm2m_engine_get_s64(char *pathstr, int64_t *value)

Get resource (instance) value (s64)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: s64 buffer to copy data into

int lwm2m_engine_get_bool(char *pathstr, bool *value)

Get resource (instance) value (bool)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] value: bool buffer to copy data into

int lwm2m_engine_get_float32(char *pathstr, float32_value_t *buf)

Get resource (instance) value (32-bit float structure)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] buf: 32-bit float buffer to copy data into

int lwm2m_engine_get_float64(char *pathstr, float64_value_t *buf)

Get resource (instance) value (64-bit float structure)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] buf: 64-bit float buffer to copy data into

int lwm2m_engine_get_objlnk(char *pathstr, struct lwm2m_objlnk *buf)

Get resource (instance) value (ObjLnk)

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] buf: lwm2m_objlnk buffer to copy data into

int lwm2m_engine_register_read_callback(char *pathstr, lwm2m_engine_get_data_cb_t cb)

Set resource (instance) read callback.

LwM2M clients can use this to set the callback function for resource reads.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] cb: Read resource callback

int lwm2m_engine_register_pre_write_callback(char *pathstr, lwm2m_engine_get_data_cb_t cb)

Set resource (instance) pre-write callback.

This callback is triggered before setting the value of a resource. It can pass a special data buffer to the engine so that the actual resource value can be calculated later, etc.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] cb: Pre-write resource callback

int lwm2m_engine_register_post_write_callback(char *pathstr, lwm2m_engine_set_data_cb_t cb)

Set resource (instance) post-write callback.

This callback is triggered after setting the value of a resource to the resource data buffer.

It allows an LwM2M client or object to post-process the value of a resource or trigger other related resource calculations.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] cb: Post-write resource callback

int lwm2m_engine_register_exec_callback(char *pathstr, lwm2m_engine_user_cb_t cb)

Set resource execute event callback.

This event is triggered when the execute method of a resource is enabled.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res”

  • [in] cb: Execute resource callback

int lwm2m_engine_register_create_callback(uint16_t obj_id, lwm2m_engine_user_cb_t cb)

Set object instance create event callback.

This event is triggered when an object instance is created.

Return

0 for success or negative in case of error.

Parameters
  • [in] obj_id: LwM2M object id

  • [in] cb: Create object instance callback

int lwm2m_engine_register_delete_callback(uint16_t obj_id, lwm2m_engine_user_cb_t cb)

Set object instance delete event callback.

This event is triggered when an object instance is deleted.

Return

0 for success or negative in case of error.

Parameters
  • [in] obj_id: LwM2M object id

  • [in] cb: Delete object instance callback

int lwm2m_engine_set_res_data(char *pathstr, void *data_ptr, uint16_t data_len, uint8_t data_flags)

Set data buffer for a resource.

Use this function to set the data buffer and flags for the specified LwM2M resource.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [in] data_ptr: Data buffer pointer

  • [in] data_len: Length of buffer

  • [in] data_flags: Data buffer flags (such as read-only, etc)

int lwm2m_engine_get_res_data(char *pathstr, void **data_ptr, uint16_t *data_len, uint8_t *data_flags)

Get data buffer for a resource.

Use this function to get the data buffer information for the specified LwM2M resource.

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res(/res-inst)”

  • [out] data_ptr: Data buffer pointer

  • [out] data_len: Length of buffer

  • [out] data_flags: Data buffer flags (such as read-only, etc)

int lwm2m_engine_create_res_inst(char *pathstr)

Create a resource instance.

LwM2M clients use this function to create multi-resource instances: Example to create 0 instance of device available power sources: lwm2m_engine_create_res_inst(“3/0/6/0”);

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res/res-inst”

int lwm2m_engine_delete_res_inst(char *pathstr)

Delete a resource instance.

Use this function to remove an existing resource instance

Return

0 for success or negative in case of error.

Parameters
  • [in] pathstr: LwM2M path string “obj/obj-inst/res/res-inst”

int lwm2m_engine_start(struct lwm2m_ctx *client_ctx)

Start the LwM2M engine.

LwM2M clients normally do not need to call this function as it is called by lwm2m_rd_client_start(). However, if the client does not use the RD client implementation, it will need to be called manually.

Return

0 for success or negative in case of error.

Parameters
  • [in] client_ctx: LwM2M context

void lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, const char *ep_name, uint32_t flags, lwm2m_ctx_event_cb_t event_cb)

Start the LwM2M RD (Registration / Discovery) Client.

The RD client sits just above the LwM2M engine and performs the necessary actions to implement the “Registration interface”. For more information see Section 5.3 “Client Registration Interface” of the LwM2M Technical Specification.

NOTE: lwm2m_engine_start() is called automatically by this function.

Parameters
  • [in] client_ctx: LwM2M context

  • [in] ep_name: Registered endpoint name

  • [in] flags: Flags used to configure current LwM2M session.

  • [in] event_cb: Client event callback function

void lwm2m_rd_client_stop(struct lwm2m_ctx *client_ctx, lwm2m_ctx_event_cb_t event_cb)

Stop the LwM2M RD (De-register) Client.

The RD client sits just above the LwM2M engine and performs the necessary actions to implement the “Registration interface”. For more information see Section 5.3 “Client Registration Interface” of the LwM2M Technical Specification.

Parameters
  • [in] client_ctx: LwM2M context

  • [in] event_cb: Client event callback function

struct lwm2m_ctx
#include <lwm2m.h>

LwM2M context structure to maintain information for a single LwM2M connection.

struct float32_value
#include <lwm2m.h>

32-bit variant of the LwM2M float structure

struct float64_value
#include <lwm2m.h>

32-bit variant of the LwM2M float structure

struct lwm2m_objlnk
#include <lwm2m.h>

LWM2M ObjLnk resource type structure.