AWS IoT MQTT Topic subscription in Runtime

I want to set a MQTT Topic for AWS IOT in Runtime with the new SDK 2.7.0.

My Legacy code that works in 2.0.0

static int awsTopicSubscribe(void) {
    int err;
    err = awsCpuIdSave(bufClientId_2, sizeof(bufClientId_2));
    if (err) {
        LOG_ERR("[MODEM] Client ID Error, error: %d", err);
    }
    // LOG_INF("client_id1: %s", bufClientId_2);
    static char custom_topic[75];
    static char custom_topic_2[75];
    sprintf(custom_topic, "$aws/things/%s/shadow/get/accepted2", bufClientId_2);
    sprintf(custom_topic_2, "$aws/things/%s/shadow/update/accepted2", bufClientId_2);

    const struct aws_iot_topic_data topics_list[2] = {[0].str = custom_topic,
                                                      [0].len = strlen(custom_topic),
                                                      [1].str = custom_topic_2,
                                                      [1].len = strlen(custom_topic_2)};

    err = aws_iot_subscription_topics_add(topics_list, ARRAY_SIZE(topics_list));
    if (err) {
        LOG_ERR("[AWS] aws_iot_subscription_topics_add, error: %d", err);
    }

    return err;
}

The new approach seems to be:

https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/libraries/networking/aws_iot.html

My Code:

static int awsTopicSubscribe(void) {
    int err;
    err = awsCpuIdSave(bufClientId_2, sizeof(bufClientId_2));
    if (err) {
        LOG_ERR("[MODEM] Client ID Error, error: %d", err);
    }
    // LOG_INF("client_id1: %s", bufClientId_2);
    static uint8_t custom_topic[75];
    static uint8_t custom_topic_2[75];
    sprintf(custom_topic, "$aws/things/%s/shadow/get/accepted2", bufClientId_2);
    sprintf(custom_topic_2, "$aws/things/%s/shadow/update/accepted2", bufClientId_2);

    static const struct mqtt_topic topic_list[] = {
        {
            .topic.utf8 = custom_topic,
            .topic.size = strlen(custom_topic),
            .qos = MQTT_QOS_1_AT_LEAST_ONCE,
        },
        {
            .topic.utf8 = custom_topic_2,
            .topic.size = strlen(custom_topic_2),
            .qos = MQTT_QOS_1_AT_LEAST_ONCE,
        }};

    err = aws_iot_application_topics_set(topic_list, ARRAY_SIZE(topic_list));
    if (err) {
        LOG_ERR("aws_iot_application_topics_set, error: %d", err);
    }

    return err;
}


The compiler Errors:

C:/WSL/epaper-nrf-firmware/src/main.c:719:27: error: initializer element is not constant
  719 |             .topic.size = strlen(custom_topic),
      |                           ^~~~~~

C:/WSL/epaper-nrf-firmware/src/main.c:724:27: error: initializer element is not constant
  724 |             .topic.size = strlen(custom_topic_2),
      |       

So do I missunderstand something or is there no way anymore to set a dynamic topic in runtime? So only predefined topics will work?

Best regards

Related