5.5. FOTA Pull with CoAPs (Certificates)

5.5.1. Overview

This tutorial extends the FOTA Pull with CoAPs (PSK) example and demonstrates how to perform firmware downloads over CoAPs (CoAP over DTLS) using certificate-based authentication instead of PSK.

The overall firmware update flow remains unchanged compared to the PSK-based CoAPs variant. The key difference lies in how the client authenticates with the firmware download server. In this version, the LwM2M Client uses X.509 certificates provisioned in the Security Object, as described in the DTLS connection using certificates tutorial.

5.5.2. Function: fu_uri_write()

The function below is the only difference between the FOTA Pull with CoAPs using PSK and Certificates variant.

static anj_dm_fw_update_result_t fu_uri_write(void *user_ptr,
                                              const char *_uri) {
    firmware_update_t *fu = (firmware_update_t *) user_ptr;
    log(L_INFO, "fu_uri_write called with URI: %s", _uri);

    anj_net_config_t net_cfg;
    memset(&net_cfg, 0x00, sizeof(net_cfg));
    net_cfg.secure_socket_config.security.mode = ANJ_NET_SECURITY_CERTIFICATE;
    if (anj_security_get_cert_info(
                fu->anj,
                false,
                &net_cfg.secure_socket_config.security.data.cert)) {
        log(L_ERROR, "Failed to get CERT credentials from Security Object");
        return ANJ_DM_FW_UPDATE_RESULT_FAILED;
    }

    int res = anj_coap_downloader_start(&coap_downloader, _uri, &net_cfg);
    if (res == ANJ_COAP_DOWNLOADER_ERR_INVALID_URI) {
        return ANJ_DM_FW_UPDATE_RESULT_INVALID_URI;
    } else if (res) {
        return ANJ_DM_FW_UPDATE_RESULT_FAILED;
    }
    return ANJ_DM_FW_UPDATE_RESULT_SUCCESS;
}

Explanation

Certificate credentials are retrieved from the Security Object using anj_security_get_cert_info() and reused for the firmware download connection. This works with Coiote DM, which allows the device to use the same key and certificate for both the management and the FOTA session, but may not apply to all servers or deployment models.