#include #include #include #include #include "mosquitto.h" #include #include #define MQTT_CLIENTID "001392" #define MQTT_TOPIC "pm/gpb" #define KEEPALIVE 60 char text[100] = "[1543388495584,22]"; static bool connected = true; static bool disconnect_sent = false; static int mid_sent = 0; static int qos = 0; static int retain = 0; void my_connect_callback(struct mosquitto *mosq, void *obj, int result) { int rc = MOSQ_ERR_SUCCESS; if(!result){ printf("Sending message...\n"); rc = mosquitto_publish(mosq, &mid_sent, MQTT_TOPIC, strlen(text), text, qos, retain); if(rc){ switch(rc){ case MOSQ_ERR_INVAL: fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n"); break; case MOSQ_ERR_NOMEM: fprintf(stderr, "Error: Out of memory when trying to publish message.\n"); break; case MOSQ_ERR_NO_CONN: fprintf(stderr, "Error: Client not connected when trying to publish.\n"); break; case MOSQ_ERR_PROTOCOL: fprintf(stderr, "Error: Protocol error when communicating with broker.\n"); break; case MOSQ_ERR_PAYLOAD_SIZE: fprintf(stderr, "Error: Message payload is too large.\n"); break; } mosquitto_disconnect(mosq); } } else { if(result){ fprintf(stderr, "%s\n", mosquitto_connack_string(result)); } } } void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) { printf("Disconnected!\n"); connected = false; } void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) { printf("Published!\n"); if(disconnect_sent == false){ mosquitto_disconnect(mosq); disconnect_sent = true; } } int main(int argc, char *argv[]) { struct mosquitto *mosq = NULL; int port,rc,boolRead; bool insec_option = true; const char *msg1 = "{ \"data\":\"Hello\" }"; char hostName[100]; char cafile[500] = ""; char capath[500] = ""; char certfile[500] = ""; char keyfile[500]= ""; char mqttUserName[100] = ""; char mqttPassword[100]= ""; int re=0,yesnoInput = 1; printf("Sample Mosquitto mqtt Client Starting -Compiled against mosquitto 2.0.12 \n"); printf("Input Port value: 1883 / 8883 \n"); scanf("%d",&port); printf("Port entered is %d \n",port); printf("Input host - ip addr \n"); scanf("%s",hostName); printf("Input Mqtt User Name \n"); scanf("%s",mqttUserName); printf("Input Mqtt Password \n"); scanf("%s",mqttPassword); printf("Entered host name is %s\n",hostName); if( port == 8883 ) { printf(" Enter mosquitto_insecure_set SSL option : 1 / 0 \n"); scanf("%d",&boolRead); if( boolRead == 1 ) insec_option = true; if( boolRead == 0 ) insec_option = false; printf("Want to Input cafile and capath ? 1/0 \n"); scanf("%d",&yesnoInput); if( yesnoInput == 1) { printf("Input cafile \n"); scanf("%s",cafile); printf("Input capath \n"); scanf("%s",capath); } printf("Want to input CertFile and Keyfile ? 1/0 \n"); scanf("%d",&yesnoInput); if( yesnoInput == 1) { printf("Input CertFile \n"); scanf("%s",certfile); printf("Input Keyfile \n"); scanf("%s",keyfile); } } mosquitto_lib_init(); printf("After mosquitto_lib_init \n"); mosq = mosquitto_new(MQTT_CLIENTID, true, NULL); if(!mosq){ printf("Cant initiallize mosquitto library\n"); exit(-1); } printf("Setting callbacks \n"); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); printf("setting username %sand psw %s\n",mqttUserName,mqttPassword); int out = mosquitto_username_pw_set(mosq, mqttUserName, mqttPassword); printf("Return value of mosquitto_username_pw_set is %d\n",out); if( port == 8883) { printf("Port entered is %d Calling TLS functions \n",port); int se = mosquitto_tls_opts_set(mosq, 1, "tlsv1.1", NULL); //1 is SSL_VERIFY_PEER tlsv1.1 printf("mosquitto_tls_opts_set is returned %d\n",se); printf("setting TLS opt \n"); printf("Value of cafile is %s\n",cafile); printf("Value of capath is %s\n",capath); if( yesnoInput == 1 ) { printf("Value of certfile is %s\n",certfile); printf("Value of keyfile is %s\n",keyfile); re = mosquitto_tls_set(mosq,cafile, capath, certfile, keyfile, NULL); } else { printf("Value of certfile is NULL\n"); printf("Value of keyfile is NULL\n"); re = mosquitto_tls_set(mosq,cafile, capath, NULL, NULL, NULL); } printf("Retrun value of tls set is %d\n",re); printf("mosquitto_tls_insecure_set calling with %d \n",insec_option); rc = mosquitto_tls_insecure_set(mosq, insec_option); //This should be false ideally printf("mosquitto_tls_insecure_set return value is %d \n",rc); } printf("Calling mosq connect \n"); int ret = mosquitto_connect(mosq, hostName, port, KEEPALIVE); if(ret){ printf("Cant connect to mosquitto server ,return value is %d\n",ret); exit(-1); } printf(" mosq connect success \n"); printf("Calling mos loopstart \n"); rc = mosquitto_loop_start(mosq); printf(" mosquitoo loop start ret %d \n",rc); int count=0; do { int err = mosquitto_publish(mosq,NULL,"AP22mqttTest",strlen(msg1),msg1,1,true); printf("Return Value of publish : %d",err); printf("\n description=%s\n", mosquitto_strerror(err)); count++; } while (count < 10 ); printf("mosquitto loop stop \n"); rc = mosquitto_loop_stop(mosq,true); // Force stop printf("mosquitto destroy \n"); mosquitto_destroy(mosq); return 0; }