Функция обратного вызова не вызывается, хотя Mosquitto Connect (AWS) был успешным в C с использованием libmosquitto - PullRequest
0 голосов
/ 26 марта 2020

Я пытаюсь подключиться к AWS с помощью libmosquitto в c. Я могу опубликовать sh сообщение на AWS с помощью команды mosquitto_pub.

Проблема: теперь я пытаюсь сделать то же самое с кодом C. mosquitto_client_connect завершился успешно, но функция on_connect_callback не вызывается

Архитектура: Мы используем локального москитного брокера для связи между несколькими приложениями. Внешний посредник (AWS) для публикации данных на сервере. Тот же код работал для другого сервера, но этот сервер не использовал TLS. После внедрения TLS для AWS я столкнулся с проблемой.

Code : 
int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
{
    printf("In client_opts_set function\n");
    /*Configure will information for a mosquitto instance*/

    if(cfg->will_topic && mosquitto_will_set(mosq, cfg->will_topic,
    cfg->will_payloadlen, cfg->will_payload, cfg->will_qos,
    cfg->will_retain))
    {
        if(!cfg->quiet)
        {
            fprintf(stderr, "Error: Problem setting will.\n");
        }
        mosquitto_lib_cleanup();
        DBG_FUNCTION_END_LOG;
        return 1;
    }

    //   Configure username and password for a mosquitton instance
   /* if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password))
    {
        if(!cfg->quiet)
        {
            fprintf(stderr, "Error: Problem setting username and password.\n");
        }
        mosquitto_lib_cleanup();
        DBG_FUNCTION_END_LOG;
        return 1;
    }*/

    //Set the number of QoS 1 and 2 messages that can be “in flight” at one time
    //  printf("Setting max_inflight message settings\n");
    mosquitto_max_inflight_messages_set(mosq, cfg->max_inflight);
   if(strcmp(cfg->host,"127.0.0.1")==0)
   {
       printf("Local broker doesn't support TLS \n");
   }
   else
   {
       printf("External Broker supports TLS \n");
       int res= mosquitto_tls_set(mosq, "/etc/ssl/certs/AWS/rootCA.crt",NULL, "/etc/ssl/certs/AWS/a82d092b38-certificate.pem.crt", "a82d092b38-private.pem.key", NULL);
       printf("tls set result : %d\n",res);
   }
    //mosquitto_tls_insecure_set(mosq, 1);

    //   Used to set options for the client
    //  printf("Setting mosquitto protocol versionfor external broker\n");
    mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &(cfg->protocol_version));

    DBG_FUNCTION_END_LOG;
    return MOSQ_ERR_SUCCESS;

}

/*It initializes local broker without TLS support */
int rt_AV_mosquitto_initialization_local_broker(struct mosq_config *cfg)
{
    DBG_FUNCTION_START_LOG;

    /*Loading Default mosquitto Config Parameters*/
    rt_AV_mosquitto_client_config_load_local_broker(cfg);

    /*Mosquitto client initialisation*/
    if(rt_AV_mosquitto_client_init_local_broker(cfg) == MOSQ_FAILURE)
    {
        DBG_PRINTF(("Failure: mosquitto_client_init\n"));
        return APP_FAILURE;
    }
    DBG_PRINTF(("Success: mosquitto_client_init\n"));

    /*Connect to an MQTT broker*/
    if(rt_AV_mosquitto_client_connect(cfg) == MOSQ_FAILURE)
    {
        DBG_PRINTF(("Failure: mosquitto_client_connect\n"));
        return APP_FAILURE;
    }

    DBG_PRINTF(("Success: mosquitto_client_connect\n"));

    /* binding server and client for infinite time-out */
    mosquitto_loop_forever(mosq1, -1, 1);
   // mosquitto_loop_start(mosq1);
    DBG_FUNCTION_END_LOG;
    return APP_SUCCESS;
}
/*It initializes AWS with TLS support.*/
int rt_AV_mosquitto_initialization_external_broker(struct mosq_config *cfg)
{
    DBG_FUNCTION_START_LOG;

    /*Loading Default mosquitto Config Parameters*/
    rt_AV_mosquitto_client_config_load_external_broker(cfg);

    /*Mosquitto client initialisation*/
    if(rt_AV_mosquitto_client_init_external_broker(cfg) == MOSQ_FAILURE)
    {
        DBG_PRINTF(("Failure: mosquitto_client_init external broker\n"));
        return APP_FAILURE;
    }
    DBG_PRINTF(("Success: mosquitto_client_init external broker\n"));

    /*Connect to an MQTT broker*/
    if(rt_AV_mosquitto_client_connect(cfg) == MOSQ_FAILURE)
    {
        DBG_PRINTF(("Failure: mosquitto_client_connect external broker\n"));
        return APP_FAILURE;
    }

   DBG_PRINTF(("Success: mosquitto_client_connect external broker\n"));

   mosquitto_loop_start(mosq2);
   DBG_FUNCTION_END_LOG;
   return APP_SUCCESS;

    }

    **O/P :** 
    Success for mosquitto_pub command : 
    root@swi-mdm9x28-wp:~# mosquitto_pub -d -t "sdkTest/sub" -h a7gzjpbbjhy48-ats.iot.ap-south-1.amazonaws.com -p 8883 --cafile /etc/ssl/certs/AWS/
    rootCA.crt --cert /etc/ssl/certs/AWS/a82d092b38-certificate.pem.crt --key /etc/ssl/certs/AWS/a82d092b38-private.pem.key -m "hello"
    Client mosqpub/21238-swi-mdm9x sending CONNECT
    Client mosqpub/21238-swi-mdm9x received CONNACK
    Client mosqpub/21238-swi-mdm9x sending PUBLISH (d0, q0, r0, m1, 'sdkTest/sub', ... (5 bytes))
    Client mosqpub/21238-swi-mdm9x sending DISCONNECT
    root@swi-mdm9x28-wp:~# 

    **O/p of application :** 
    rt_AV_mosquitto_client_init_external_broker:START
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:87:FUNCTION:client_id_generate:START
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:131:FUNCTION:client_id_generate:END
    In client_opts_set function
    External Broker supports TLS 
    tls set result : 3
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:70:FUNCTION:client_opts_set:END
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/mosquitto_pub_sub_client.c:LINE:269:FUNCTION:rt_AV_mosquitto_client_init_external_broker:END
    Success: mosquitto_client_init external broker
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:154:FUNCTION:rt_AV_mosquitto_client_connect:START
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:179:FUNCTION:client_connect:START
    Client external_broker/25123-s sending CONNECT
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:207:FUNCTION:client_connect:END
    FILE:/home/amruta/dev_studio5.3/workspace/AWS_Sample/AirVantageSenderAppComponent/src/client_shared_lib.c:LINE:166:FUNCTION:rt_AV_mosquitto_client_connect:END
    Success: mosquitto_client_connect external broker
...