Mistake on this page? Email us

Client initialization

Client Lite is an event-based component, which means all the operations are asynchronous, and you need to handle all your API calls and callbacks through event callbacks. Your application must instantiate an event loop and provide the event callback handler and handle all the event callback operations there.

This example shows how to do it in the reference example application. If you are creating your own application, you need to implement this logic yourself.

uint8_t interface_event_handler_id;
ns_hal_init(NULL, MBED_CLIENT_EVENT_LOOP_SIZE, NULL, NULL);
interface_event_handler_id = eventOS_event_handler_create(interface_event_handler, M2M_CLIENT_EVENT_INITIALIZED);
pdmc_connect_init(interface_event_handler_id);

The minimum size of MBED_CLIENT_EVENT_LOOP_SIZE should be 512 but you can increase it if you are also using events in your application to handle more async operations.

Once your client is initialized, you will receive callback events through your interface_event_handler which is the function pointer you pass in eventOS_event_handler_create().

Here is how you can implement the interface_event_handler:

static void interface_event_handler(arm_event_t *event)
{
    if (event->event_type == 0 && event->event_id == 0) {
        return;
    }

    if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_OBJECT_REGISTERED) {
        printf("Client registered\n");
    } else if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_OBJECT_UNREGISTERED) {
        printf("Client unregistered\n");
    } else if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_REGISTRATION_UPDATED) {
        printf("Client registration updated\n");
    } else if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_VALUE_UPDATED) {
        printf("Client resource receives new value\n");
    } else if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_ERROR) {
        pdmc_client_error_event((lwm2m_interface_error_t)event->event_type);
    } else if (event->event_id == LWM2M_INTERFACE_OBSERVER_EVENT_BOOTSTRAP_DONE) {
        printf("Client bootstrapped\n");
    } else if (event->event_id == M2M_CLIENT_EVENT_SETUP_COMPLETED) {
        pdmc_connect_register(get_network_interface());
    } else {
        printf("Unhandled event: %d\n", event->event_id);
    }
}

When the callback receives the M2M_CLIENT_EVENT_SETUP_COMPLETED event, you need to call pdmc_connect_register() and pass the get_network_interface() NetworkInterface pointer to it. The client uses the pointer to establish the connection to Device Management.

Along with the event handler, your application needs to handle various different errors indicated by Client Lite. An error is propagated through the LWM2M_INTERFACE_OBSERVER_EVENT_ERROR event originating from the event handler and the error code is indicated by the event_type of the callback event.

Here is how you can list errors coming from Client Lite:

static void pdmc_client_error_event(lwm2m_interface_error_t error_code)
{
    const char *error;
    switch (error_code) {
        case LWM2M_INTERFACE_ERROR_NONE:
            error = "ErrorNone";
            break;
        case LWM2M_INTERFACE_ERROR_ALREADY_EXISTS:
            error = "AlreadyExists";
            break;
        case LWM2M_INTERFACE_ERROR_BOOTSTRAP_FAILED:
            error = "BootstrapFailed";
            break;
        case LWM2M_INTERFACE_ERROR_INVALID_PARAMETERS:
            error = "InvalidParameters";
            break;
        case LWM2M_INTERFACE_ERROR_NOT_REGISTERED:
            error = "NotRegistered";
            break;
        case LWM2M_INTERFACE_ERROR_TIMEOUT:
            error = "Timeout";
            break;
        case LWM2M_INTERFACE_ERROR_NETWORK_ERROR:
            error = "NetworkError";
            break;
        case LWM2M_INTERFACE_ERROR_RESPONSE_PARSE_FAILED:
            error = "ResponseParseFailed";
            break;
        case LWM2M_INTERFACE_ERROR_UNKNOWN_ERROR:
            error = "UnknownError";
            break;
        case LWM2M_INTERFACE_ERROR_MEMORY_FAIL:
            error = "MemoryFail";
            break;
        case LWM2M_INTERFACE_ERROR_NOT_ALLOWED:
            error = "NotAllowed";
            break;
        case LWM2M_INTERFACE_ERROR_SECURE_CONNECTION_FAILED:
            error = "SecureConnectionFailed";
            break;
        case LWM2M_INTERFACE_ERROR_DNS_RESOLVING_FAILED:
            error = "DnsResolvingFailed";
            break;
        case LWM2M_INTERFACE_ERROR_UNREGISTRATION_FAILED:
            error = "UnregistrationFailed";
            break;
        default:
            error = "";
    }
    printf("Error occurred : %s\n", error);
    printf("Error code : %d\n", error_code);
}