Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] libmosquitto issues in mqtt client application

Hello Roger,

                Thank you very much for reply to my query.
As per your advise, i used the function mosquitto_threaded_set() while initializing mosquitto as followed in the Code snippet below.
But still, the mosquitto_loop_forever() function used in the thread is exiting with the same error "MOSQ_ERR_INVA".
the only possible reason can be "mosq" argument being NULL. this happening when the Mqtt broker Disconnected due to loss of INTERNET connectivity but
the the mosquitto_loop_forever() doesn't  exit every time the connection is lost. it happens if the connection to the broker is lost for a long time.

Can you think of any other possible reason why it is happening??

i would appreciate your reply on this.

Code snippet :

int fnMqttInit(void)
{
    printf("\nFunction  MQTT Initlization\n");

    int keepalive = 60;
    char clean_session = TRUE;
    int Return = 0;

    mosquitto_lib_init();
    mosq = mosquitto_new(NULL, clean_session, NULL);
    if(!mosq)
    {
        fprintf(stderr, "Error: Out of memory.\n");
        return 0;
    }
    mosquitto_log_callback_set(mosq, my_log_callback);
    mosquitto_connect_callback_set(mosq, my_connect_callback);
    mosquitto_message_callback_set(mosq, my_message_callback);
    mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
    mosquitto_publish_callback_set(mosq,my_publish_callback);
    mosquitto_disconnect_callback_set(mosq,my_disconnect_callback);

    mosquitto_threaded_set(mosq,TRUE);     <-- used here
    mosquitto_username_pw_set(mosq,Mqtt.UserName,Mqtt.Password);
    printf("\n Mqtt.UserName:%s,Mqtt.Password:%s\n",Mqtt.UserName,Mqtt.Password);
    printf("\n Mqtt.HostName:%s,Mqtt.PortNo:%d\n",Mqtt.HostName,Mqtt.PortNo);
    usleep(5000 * 1000);
    if(Return = mosquitto_connect(mosq,Mqtt.HostName,Mqtt.PortNo, keepalive))
    {
        fprintf(stderr, "fnMqttInit : (mosquitto_connect) Error: %s\n", mosquitto_strerror(Return));
        syslog(LOG_INFO, "%s",mosquitto_strerror(Return));
        fprintf(stderr, "\nUnable to connect To Broker\n");
        g_bServerStatus = 0;
        return 0;
    }
    else
    {
        g_bServerStatus = 1;
        syslog(LOG_INFO, "%s",mosquitto_strerror(Return));
        printf("\n CONNECTED TO MQTT BROKER");
    }
    return 1;
}


On Sunday 11 December 2016 10:30 PM, mosquitto-dev-request@xxxxxxxxxxx wrote:
Send mosquitto-dev mailing list submissions to
	mosquitto-dev@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
	https://dev.eclipse.org/mailman/listinfo/mosquitto-dev
or, via email, send a message with subject or body 'help' to
	mosquitto-dev-request@xxxxxxxxxxx

You can reach the person managing the list at
	mosquitto-dev-owner@xxxxxxxxxxx

When replying, please edit your Subject line so it is more specific
than "Re: Contents of mosquitto-dev digest..."


Today's Topics:

   1. Re: libmosquitto issues (Roger Light)


----------------------------------------------------------------------

Message: 1
Date: Sat, 10 Dec 2016 22:56:26 +0000
From: Roger Light <roger@xxxxxxxxxx>
To: General development discussions for the mosquitto project
	<mosquitto-dev@xxxxxxxxxxx>
Subject: Re: [mosquitto-dev] libmosquitto issues
Message-ID:
	<CAH7zdyfdg779f1LzBhcznq6zcaKrPcY6aNR3AHzZx8dDZY4uNg@xxxxxxxxxxxxxx>
Content-Type: text/plain; charset=UTF-8

On Sat, Dec 10, 2016 at 7:30 AM, Sansheel
<sansheel.kokne@xxxxxxxxxxxxxxxx> wrote:
Hello,

        I am using "libmosquitto" for my MQTT client code and i am having an
issue with mosquitto_loop_forever() API getting exited with an error.
The error says "INVALID FUNCTION ARGUMENT PROVIDED".
The only reason for mosquitto_loop_forever() to return MOSQ_ERR_INVAL
is if the function parameters were invalid - like the "mosq" argument
being NULL.

->    After initialization, a thread is created where
mosquitto_loop_forever() ran.
->    Please find the thread function MqttLoopFun() below fnMqttInit()
function.
->    After initialization and thread creation, some subscription and
publishing is done periodically.
If you're creating your own threads, please make sure you follow the
information given in the first paragraph of the documentation in
mosquitto.h:

 *  If your application uses threads you must use <mosquitto_threaded_set> to
 *  tell the library this is the case, otherwise it makes some optimisations
 *  for the single threaded case that may result in unexpected behaviour for
 *  the multi threaded case.

Regards,

Roger


------------------------------

_______________________________________________
mosquitto-dev mailing list
mosquitto-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/mosquitto-dev

End of mosquitto-dev Digest, Vol 36, Issue 7
********************************************



Back to the top