Anjay Lite
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
core.h File Reference
#include <anj/anj_config.h>
#include <anj/compat/net/anj_net_api.h>
#include <anj/defs.h>
#include <anj/dm/core.h>
#include <anj_internal/exchange.h>
#include <anj_internal/bootstrap.h>
#include <anj/lwm2m_send.h>
#include <anj_internal/core.h>

Go to the source code of this file.

Data Structures

struct  anjay_configuration_struct
 

Macros

#define ANJ_INTERNAL_INCLUDE_EXCHANGE
 
#define ANJ_INTERNAL_INCLUDE_BOOTSTRAP
 
#define ANJ_SUPPORTED_BINDING_MODES   "UT"
 
#define ANJ_INTERNAL_INCLUDE_CORE
 

Typedefs

typedef void anj_connection_status_callback_t(void *arg, anj_t *anj, anj_conn_status_t conn_status)
 
typedef struct anjay_configuration_struct anj_configuration_t
 

Enumerations

enum  anj_conn_status_t {
  ANJ_CONN_STATUS_INITIAL , ANJ_CONN_STATUS_INVALID , ANJ_CONN_STATUS_FAILURE , ANJ_CONN_STATUS_BOOTSTRAPPING ,
  ANJ_CONN_STATUS_BOOTSTRAPPED , ANJ_CONN_STATUS_REGISTERING , ANJ_CONN_STATUS_REGISTERED , ANJ_CONN_STATUS_SUSPENDED ,
  ANJ_CONN_STATUS_ENTERING_QUEUE_MODE , ANJ_CONN_STATUS_QUEUE_MODE
}
 
enum  anj_core_change_type_t { ANJ_CORE_CHANGE_TYPE_VALUE_CHANGED = 0 , ANJ_CORE_CHANGE_TYPE_ADDED = 1 , ANJ_CORE_CHANGE_TYPE_DELETED = 2 }
 

Functions

int anj_core_init (anj_t *anj, const anj_configuration_t *config)
 
void anj_core_step (anj_t *anj)
 
uint64_t anj_core_next_step_time (anj_t *anj)
 
bool anj_core_ongoing_operation (anj_t *anj)
 
void anj_core_server_obj_disable_executed (anj_t *anj, uint32_t timeout)
 
void anj_core_server_obj_registration_update_trigger_executed (anj_t *anj)
 
void anj_core_server_obj_bootstrap_request_trigger_executed (anj_t *anj)
 
void anj_core_data_model_changed (anj_t *anj, const anj_uri_path_t *path, anj_core_change_type_t change_type)
 
void anj_core_disable_server (anj_t *anj, uint64_t timeout_ms)
 
void anj_core_request_bootstrap (anj_t *anj)
 
void anj_core_restart (anj_t *anj)
 
void anj_core_request_update (anj_t *anj)
 
int anj_core_shutdown (anj_t *anj)
 

Macro Definition Documentation

◆ ANJ_INTERNAL_INCLUDE_BOOTSTRAP

#define ANJ_INTERNAL_INCLUDE_BOOTSTRAP

◆ ANJ_INTERNAL_INCLUDE_CORE

#define ANJ_INTERNAL_INCLUDE_CORE

◆ ANJ_INTERNAL_INCLUDE_EXCHANGE

#define ANJ_INTERNAL_INCLUDE_EXCHANGE

◆ ANJ_SUPPORTED_BINDING_MODES

#define ANJ_SUPPORTED_BINDING_MODES   "UT"

Typedef Documentation

◆ anj_configuration_t

Anjay Lite configuration. Provided in anj_core_init() function.

◆ anj_connection_status_callback_t

typedef void anj_connection_status_callback_t(void *arg, anj_t *anj, anj_conn_status_t conn_status)

Callback type for connection status change notifications.

This function is called whenever the connection state of the LwM2M client changes — e.g., transitioning from bootstrapping to registered, or entering queue mode.

Users may use this callback to monitor client connectivity and act accordingly (e.g., update UI, manage power states, or trigger other logic).

Parameters
argOpaque argument passed to the callback.
anjAnjay object reporting the status change.
conn_statusNew connection status value; see anj_conn_status_t.

Enumeration Type Documentation

◆ anj_conn_status_t

This enum represents the possible states of a server connection.

Enumerator
ANJ_CONN_STATUS_INITIAL 

Initial state of the client after startup.

Anjay Lite will automatically attempt to transition to either ANJ_CONN_STATUS_BOOTSTRAPPING or ANJ_CONN_STATUS_REGISTERING, depending on the configuration.

If the provided configuration is invalid or incomplete, the client will immediately transition to ANJ_CONN_STATUS_INVALID.

ANJ_CONN_STATUS_INVALID 

Provided configuration is invalid and a connection cannot be established.

This state is transient — the client will immediately transition to ANJ_CONN_STATUS_FAILURE to indicate a permanent failure.

ANJ_CONN_STATUS_FAILURE 

Indicates that bootstrap or registration has permanently failed (i.e., all configured retry attempts have been exhausted).

In this case, reinitialization of the Anjay Lite client is required to attempt a new connection cycle. This can be done by calling anj_core_restart.

ANJ_CONN_STATUS_BOOTSTRAPPING 

Bootstrap process is ongoing.

ANJ_CONN_STATUS_BOOTSTRAPPED 

Bootstrapping process has finished successfully.

ANJ_CONN_STATUS_REGISTERING 

Registering process is ongoing.

ANJ_CONN_STATUS_REGISTERED 

Registering/Updating process has finished successfully.

ANJ_CONN_STATUS_SUSPENDED 

Connection is suspended. If the suspension was initiated by the server the client will remain suspended until the Disable Timeout (resource 1/x/5) expires. If the suspension was initiated by the client application no action is taken until user decides to resume or timeout occurs.

ANJ_CONN_STATUS_ENTERING_QUEUE_MODE 

Client is entering queue mode.

ANJ_CONN_STATUS_QUEUE_MODE 

Client is in queue mode: new requests still can be sent to the server, but no new messages are received.

◆ anj_core_change_type_t

Contains information about the type of changes of the data model. Used in anj_core_data_model_changed function.

Enumerator
ANJ_CORE_CHANGE_TYPE_VALUE_CHANGED 

Resource or Resource Instance value changed.

ANJ_CORE_CHANGE_TYPE_ADDED 

Object Instance or Resource Instance added.

ANJ_CORE_CHANGE_TYPE_DELETED 

Object Instance or Resource Instance deleted.

Function Documentation

◆ anj_core_data_model_changed()

void anj_core_data_model_changed ( anj_t anj,
const anj_uri_path_t path,
anj_core_change_type_t  change_type 
)

Notifies the library that data model changed. Depending on the change_type there are three possible scenarios:

  • ANJ_CORE_CHANGE_TYPE_VALUE_CHANGED - Resource or Resource Instance value changed. It may trigger preparation of a LwM2M Notify message.
  • ANJ_CORE_CHANGE_TYPE_ADDED - Object Instance or Resource Instance has been added. It may trigger preparation of a LwM2M Notify message and for new Object Instance also registration update.
  • ANJ_CORE_CHANGE_TYPE_DELETED - Object Instance or Resource Instance has been deleted. The consequence will be the removal of all observations and attributes associated with this Instance. For removed Object Instance it will also trigger registration update.

For correct handling of notifications, and registration updates, this function must be called on any change in the data model.

IMPORTANT: This function is to be used only for changes in the data model that are caused by user actions. Changes caused by the LwM2M Server are handled internally.

Parameters
anjAnjay object to operate on.t.
pathPointer to the path of the Resource that changed, or the Instance that was added/removed.
change_typeType of change; anj_core_change_type_t.

◆ anj_core_disable_server()

void anj_core_disable_server ( anj_t anj,
uint64_t  timeout_ms 
)

Disables the LwM2M Server connection for a specified timeout. If the server is already disabled, this call will only update the timeout value.

Note
If the client is currently registered, a De-Register message will be sent to the server before the connection is closed.
To disable the server for infinite time, set timeout_ms to ANJ_TIME_UNDEFINED.
Parameters
anjAnjay object to operate on.
timeout_msTimeout for server disable, i.e. time for which a server should be disabled before it's automatically enabled again, in miliseconds.

◆ anj_core_init()

int anj_core_init ( anj_t anj,
const anj_configuration_t config 
)

Initializes the core of the Anjay Lite library. The anj object must be created and allocated by the user before calling this function.

Note
It is recommended to ensure that the system time is set correctly before calling this function. Many internal operations — such as registration lifetimes, scheduling, and retransmissions — rely on time-based logic. If the system time is significantly adjusted (e.g., via NTP or manual update) after initialization, it may affect incorrect library behavior.
Parameters
anjAnjay object to operate on.
configConfiguration structure.
Returns
0 on success, or a negative value in case of error.

◆ anj_core_next_step_time()

uint64_t anj_core_next_step_time ( anj_t anj)

Returns the time (in milliseconds) until the next call to anj_core_step is required.

In most cases, the returned value will be 0, indicating that the main loop should call anj_core_step immediately.

However, in certain low-activity states, this function may return a positive value:

This function is useful for optimizing power consumption or sleeping, allowing the main loop to wait the appropriate amount of time before calling anj_core_step again.

Note
For example, if the device is in queue mode and the next Update message is scheduled to be sent in 20 seconds and there is no active observations, this function will return 20*1000.
Returned value might become outdated if anj_core_change_type_t is called after this function returns. In such case, the application should call anj_core_next_step_time again to get an updated value.

This function allows the integration layer to optimize power consumption or sleep scheduling by delaying calls to anj_core_step until necessary.

Parameters
anjAnjay object to operate on.
Returns
Time in milliseconds until the next anj_core_step is required.

◆ anj_core_ongoing_operation()

bool anj_core_ongoing_operation ( anj_t anj)

Checks if there is an ongoing exchange between the client and the server. User must not process operations on the data model if this function returns true.

Parameters
anjAnjay object to operate on.
Returns
true if there is an ongoing operation, false otherwise.

◆ anj_core_request_bootstrap()

void anj_core_request_bootstrap ( anj_t anj)

Forces the start of a Bootstrap sequence.

This function immediately initiates a client-side Bootstrap procedure, regardless of the current connection status.

Note
If a bootstrap session is already active, this function will have no effect.
Parameters
anjAnjay object to operate on.

◆ anj_core_request_update()

void anj_core_request_update ( anj_t anj)

Forces to start a Registration Update sequence.

Note
If a registration session is not active, this function will have no effect.
Parameters
anjAnjay object to operate on.

◆ anj_core_restart()

void anj_core_restart ( anj_t anj)

Restarts the Anjay Lite client connection by resetting its internal state to the initial state (ANJ_CONN_STATUS_INITIAL).

This function can be used to fully reinitialize the LwM2M connection cycle, e.g., after a configuration change, error recovery, or user-triggered reset.

The following operations are performed:

  • If the client is currently registered with a LwM2M Server, a De-Register message is sent before tearing down the connection.
  • If a Bootstrap or Registration process is in progress, it will be immediately aborted.
Note
If the client is currently in the ANJ_CONN_STATUS_SUSPENDED state due to a server-triggered Disable operation (i.e., after calling anj_core_server_obj_disable_executed), the user should not attempt to manually resume the connection (e.g., via anj_core_restart or anj_core_request_bootstrap).

In this case, the connection is suspended intentionally by the server, and the client must remain idle until the disable timeout expires.

Parameters
anjAnjay object to operate on.

◆ anj_core_server_obj_bootstrap_request_trigger_executed()

void anj_core_server_obj_bootstrap_request_trigger_executed ( anj_t anj)

Should be called when Bootstrap-Request Trigger resource of Server object (/1/x/9) is executed.

Note
The bootstrap process will be scheduled but delayed until the currently processed LwM2M request is fully completed.
De-Register message will be sent to the server before the connection with Management Server is closed and Bootstrap is started.
Parameters
anjAnjay object to operate on.

◆ anj_core_server_obj_disable_executed()

void anj_core_server_obj_disable_executed ( anj_t anj,
uint32_t  timeout 
)

Should be called when Disable resource of Server object (/1/x/4) is executed.

Note
The disable process will be scheduled but delayed until the currently processed LwM2M request is fully completed.
De-Register message will be sent to the server before the connection is closed.
Parameters
anjAnjay object to operate on.
timeoutTimeout for server disable, i.e. time for which a server should be disabled before it's automatically enabled again, in seconds. It's taken from /1/x/5 Resource and if it's not set, default value of 86400 seconds (24h) should be used.

◆ anj_core_server_obj_registration_update_trigger_executed()

void anj_core_server_obj_registration_update_trigger_executed ( anj_t anj)

Should be called when Registration Update Trigger resource of Server object (/1/x/8) is executed.

Note
The Update operation will be scheduled but delayed until the currently processed LwM2M request is fully completed.
Parameters
anjAnjay object to operate on.

◆ anj_core_shutdown()

int anj_core_shutdown ( anj_t anj)

Shuts down the Anjay Lite client instance.

This function halts all active LwM2M operations and clears the client’s internal state. Once called, the anj object becomes unusable unless it is reinitialized using anj_core_init.

The following cleanup steps are performed:

  • All queued Send operations are canceled and removed.
  • All observation and attribute storage entries are deleted.
  • Any ongoing LwM2M exchanges (e.g., registration, update, notification, Server-initiated requests) are terminated.
Note
LwM2M Objects registered via anj_dm_add_obj are user-managed. This function does not free or reset those objects; users are responsible for their manual deallocation if needed.

Internally, this function invokes a sequence of operations to tear down networking, including shutting down and closing sockets, and releasing network context state. If any of these steps return ANJ_NET_EAGAIN, indicating that the operation is not yet complete, this function will immediately return that value. In this case, the client is not yet fully shut down, and the function should be called again later to complete the process.

If any networking-related call returns an error other than ANJ_NET_EAGAIN, the client is still fully shut down internally, but some network resources (such as sockets or internal handles) may not have been properly released.

Warning
If shutdown fails due to networking errors, there is a risk that sockets or system-level resources allocated by the client will not be completely cleaned up. This is especially important in long-running processes or test environments where resource leaks could accumulate.
Note
If blocking behavior is acceptable, the shutdown can be safely handled in a loop:
int ret;
do {
ret = anj_core_shutdown(&anj);
} while (ret == ANJ_NET_EAGAIN);
#define ANJ_NET_EAGAIN
Definition anj_net_api.h:45
int anj_core_shutdown(anj_t *anj)
Parameters
anjPointer to the Anjay client instance to shut down.
Returns
0 on success, ANJ_NET_EAGAIN if shutdown is still in progress, or a different error code on failure.

◆ anj_core_step()

void anj_core_step ( anj_t anj)

Main step function of the Anjay Lite library.

This function should be called regularly in the main application loop. It drives the internal state machine and handles all scheduled operations related to LwM2M communication (e.g., bootstrap, registration, notifications, queue mode transitions, etc.).

This function is non-blocking, unless a custom network implementation introduces blocking behavior.

Parameters
anjAnjay object to operate on.