int main(int argc, char *argv[]) { int rc; int tmp; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("prop-test", true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_publish_callback_set(mosq, on_publish); tmp = MQTT_PROTOCOL_V5; mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &tmp); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ rc = mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int port, client_id, buffer_size; int rc; struct mosquitto *mosq; void* buffer; char line[1024]; const char *name; //prepare arguments if(argc < 3) { printf("usage: %s host port [id]\n", argv[0]); return 0; } port = atoi(argv[2]); client_id = -1; if(argc > 3) { client_id = atoi(argv[3]); } pthread_mutex_init(&mutex, NULL); name = init(client_id); //start mosquitto stuff mosquitto_lib_init(); mosq = mosquitto_new(name, true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_subscribe_callback_set(mosq, on_subscribe); mosquitto_message_callback_set(mosq, on_message); rc = mosquitto_connect_async(mosq, argv[1], port, 60); mosquitto_loop_start(mosq); while(1) { fgets(line, 1024, stdin); pthread_mutex_lock(&mutex); buffer = message_entered(line, &buffer_size); pthread_mutex_unlock(&mutex); int sent_mid = -1; mosquitto_publish(mosq, &sent_mid, topic_name, buffer_size, buffer, 0, false); message_sent(buffer, buffer_size); } mosquitto_disconnect(mosq); mosquitto_loop_stop(mosq, false); mosquitto_lib_cleanup(); cleanup(); return 0; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_property *props = NULL; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("publish-qos1-test", true, &run); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_connect_v5_callback_set(mosq, on_connect); mosquitto_publish_v5_callback_set(mosq, on_publish); rc = mosquitto_connect_bind_v5(mosq, "localhost", port, 60, NULL, NULL); while(run == -1){ mosquitto_loop(mosq, 300, 1); } mosquitto_lib_cleanup(); return run; }
bool MosquittoHandler::init(std::string id) { int minor, major, revision; mosquitto_lib_version(&major, &minor, &revision); if(numOfInstances == 0) { mosquitto_lib_init(); } m_libInit = true; numOfInstances++; void* obj = (void*) this; m_mosquittoStruct = mosquitto_new(id.c_str(), obj); if(!m_mosquittoStruct) { m_lastErrorString = "Cannot create new Mosquitto instance"; return false; } mosquitto_log_init(m_mosquittoStruct, MOSQ_LOG_ERR || MOSQ_LOG_WARNING, MOSQ_LOG_STDOUT); mosquitto_connect_callback_set(m_mosquittoStruct, MosquittoHandler::onConnectWrapper); mosquitto_disconnect_callback_set(m_mosquittoStruct, MosquittoHandler::onDisconnectWrapper); mosquitto_message_callback_set(m_mosquittoStruct, MosquittoHandler::onMessageWrapper); return true; }
int main(int argc, char *argv[]) { int i; char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; sprintf(mqMsg,"%s",argv[1]); printf("And the word is >> %s <<\n", mqMsg); mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } 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_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
int mqPub(void) { int i; char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } 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_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
int main(int argc, char *argv[]) { int i; char *host = "localhost"; int port = 1883; int keepalive = 60; bool clean_session = false; // Note this, set it to false if you want to get offline message struct mosquitto *mosq = NULL; mosquitto_lib_init(); // Note this, this first argv is id, it must be set if clean_session is false mosq = mosquitto_new("1", clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } 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); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } while(!mosquitto_loop(mosq, -1, 1)){ } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
static int mosq_new(lua_State *L) { const char *id = luaL_optstring(L, 1, NULL); bool clean_session = (lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true); if (id == NULL && !clean_session) { return luaL_argerror(L, 2, "if 'id' is nil then 'clean session' must be true"); } ctx_t *ctx = (ctx_t *) lua_newuserdata(L, sizeof(ctx_t)); /* ctx will be passed as void *obj arg in the callback functions */ ctx->mosq = mosquitto_new(id, clean_session, ctx); if (ctx->mosq == NULL) { return luaL_error(L, strerror(errno)); } ctx->L = L; ctx__on_init(ctx); luaL_getmetatable(L, MOSQ_META_CTX); lua_setmetatable(L, -2); return 1; }
/* {{{ Mosquitto\Client::__construct() */ PHP_METHOD(Mosquitto_Client, __construct) { mosquitto_client_object *object; char *id = NULL; int id_len = 0; zend_bool clean_session = 1; PHP_MOSQUITTO_ERROR_HANDLING(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &id, &id_len, &clean_session) == FAILURE) { PHP_MOSQUITTO_RESTORE_ERRORS(); return; } PHP_MOSQUITTO_RESTORE_ERRORS(); object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC); object->client = mosquitto_new(id, clean_session, object); if (!object->client) { char *message = php_mosquitto_strerror_wrapper(errno); zend_throw_exception(mosquitto_ce_exception, message, 1 TSRMLS_CC); #ifndef STRERROR_R_CHAR_P if (message != NULL) { efree(message); } #endif } }
void mloop(dispatch_data *dd) { char id[30]; char *host = "130.102.128.123"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; local_dd = dd; mosq = mosquitto_new(id, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return; } mosq_instance=mosq; mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); if(mosquitto_connect(mosq, host, port, keepalive, clean_session)){ fprintf(stderr, "Unable to connect.\n"); return; } while(mosquitto_loop(mosq, -1) != -1){ } mosquitto_destroy(mosq); return; }
int main(int argc, char *argv[]) { struct mosquitto *mosq; int i; double dstart, dstop, diff; FILE *fptr; uint8_t *buf; buf = malloc(MESSAGE_SIZE*MESSAGE_COUNT); if(!buf){ printf("Error: Out of memory.\n"); return 1; } start.tv_sec = 0; start.tv_usec = 0; stop.tv_sec = 0; stop.tv_usec = 0; if(create_data()){ printf("Error: Unable to create random input data.\n"); return 1; } fptr = fopen("msgsps_pub.dat", "rb"); if(!fptr){ printf("Error: Unable to open random input data.\n"); return 1; } fread(buf, sizeof(uint8_t), MESSAGE_SIZE*MESSAGE_COUNT, fptr); fclose(fptr); mosquitto_lib_init(); mosq = mosquitto_new("perftest", true, NULL); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); mosquitto_connect(mosq, "127.0.0.1", 1884, 600); i=0; while(!mosquitto_loop(mosq, 1, 10) && run){ if(i<MESSAGE_COUNT){ mosquitto_publish(mosq, NULL, "perf/test", MESSAGE_SIZE, &buf[i*MESSAGE_SIZE], 0, false); i++; } } dstart = (double)start.tv_sec*1.0e6 + (double)start.tv_usec; dstop = (double)stop.tv_sec*1.0e6 + (double)stop.tv_usec; diff = (dstop-dstart)/1.0e6; printf("Start: %g\nStop: %g\nDiff: %g\nMessages/s: %g\n", dstart, dstop, diff, (double)MESSAGE_COUNT/diff); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
QMosquitto::QMosquitto(QObject *parent) : QObject(parent) { mosquitto_lib_init(); this->mosq = mosquitto_new(NULL, true, this); if(this->mosq == NULL) throw "Error creating mosquitto object"; // Register message callback mosquitto_message_callback_set(this->mosq, on_message); }
mosquittopp::mosquittopp(const char *id) { mosq = mosquitto_new(id, this); mosquitto_connect_callback_set(mosq, on_connect_wrapper); mosquitto_disconnect_callback_set(mosq, on_disconnect_wrapper); mosquitto_publish_callback_set(mosq, on_publish_wrapper); mosquitto_message_callback_set(mosq, on_message_wrapper); mosquitto_subscribe_callback_set(mosq, on_subscribe_wrapper); mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe_wrapper); }
int MqttHal_Init() { int rc = 0; //struct mosq_config mosq_cfg; senhub_info_t *pshinfo; g_SensorHubList = NULL; //ADV_INFO("%s: \n", __func__); rc = mqtt_client_config_load(&g_mosq_cfg, CLIENT_SUB, 1, NULL); if(rc){ mqtt_client_config_cleanup(&g_mosq_cfg); return 1; } mosquitto_lib_init(); if(mqtt_client_id_generate(&g_mosq_cfg, "advmqttcli")){ return 1; } g_mosq = mosquitto_new(g_mosq_cfg.id, g_mosq_cfg.clean_session, &g_mosq_cfg); if(!g_mosq){ switch(errno){ case ENOMEM: if(!g_mosq_cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); break; case EINVAL: if(!g_mosq_cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); break; } mosquitto_lib_cleanup(); return 1; } if(mqtt_client_opts_set(g_mosq, &g_mosq_cfg)){ return 1; } mosquitto_connect_callback_set(g_mosq, MqttHal_Connect_Callback); mosquitto_message_callback_set(g_mosq, MqttHal_Message_Callback); mosquitto_disconnect_callback_set(g_mosq, MqttHal_Disconnect_Callback); mosquitto_publish_callback_set(g_mosq, MqttHal_Publish_Callback); // Create senhub root pshinfo = malloc(sizeof(senhub_info_t)); memset(pshinfo, 0, sizeof(senhub_info_t)); sprintf(pshinfo->macAddress, "0000%s" , g_GWInfMAC); pshinfo->jsonNode = NULL; pshinfo->id = senhub_list_newId(g_SensorHubList); //printf("%s: list add id=%d\n", __func__, pshinfo->id); g_SensorHubList = SENHUB_LIST_ADD(g_SensorHubList, pshinfo); return rc; }
bool mosq_setup(struct _squash *st) { mosquitto_lib_init(); DLOG("mosquitto -> (re)connecting\n"); st->mosq = mosquitto_new(NULL, true, st); mosquitto_log_callback_set(st->mosq, mosq_logger); mosquitto_message_callback_set(st->mosq, st->msg_handler); int rc = mosquitto_connect(st->mosq, st->mq_host, 1883, 60); if (MOSQ_ERR_SUCCESS != rc) { WLOG("Failed to connect: %s\n", strerror(errno)); rc = -1; goto unwind; } int mosq_fd = mosquitto_socket(st->mosq); if (evutil_make_socket_nonblocking(mosq_fd)) { WLOG("Failed to make non-blocking: fd = %d, possibly ok\n", mosq_fd); } st->mosq_readidle = event_new(st->base, mosq_fd, EV_READ|EV_PERSIST, mosq_ev_io, st); if (st->mosq_readidle == NULL) { WLOG("Failed to create mosquitto read/idle watcher\n"); rc = -1; goto unwind_readidle; } st->mosq_write = event_new(st->base, mosq_fd, EV_WRITE, mosq_ev_io, st); if (st->mosq_write == NULL) { WLOG("Failed to create mosquitto write watcher\n"); rc = -1; goto unwind_write; } if (mosquitto_want_write(st->mosq)) { event_add(st->mosq_write, NULL); } struct timeval mosq_idle_loop_time = { 0, 100 * 1000 }; if (event_add(st->mosq_readidle, &mosq_idle_loop_time) < 0) { WLOG("Failed to activate mosquitto watcher\n"); rc = -1; goto unwind_write; } goto out; unwind_write: event_free(st->mosq_write); unwind_readidle: event_free(st->mosq_readidle); unwind: mosquitto_destroy(st->mosq); mosquitto_lib_cleanup(); out: return rc == 0; }
mosquittopp::mosquittopp(const char *id, bool clean_session) { m_mosq = mosquitto_new(id, clean_session, this); mosquitto_connect_callback_set(m_mosq, on_connect_wrapper); mosquitto_disconnect_callback_set(m_mosq, on_disconnect_wrapper); mosquitto_publish_callback_set(m_mosq, on_publish_wrapper); mosquitto_message_callback_set(m_mosq, on_message_wrapper); mosquitto_subscribe_callback_set(m_mosq, on_subscribe_wrapper); mosquitto_unsubscribe_callback_set(m_mosq, on_unsubscribe_wrapper); mosquitto_log_callback_set(m_mosq, on_log_wrapper); }
bool connect() { struct mosquitto *m = mosquitto_new(options.clientId, true, this); this->connection = m; mosquitto_connect_callback_set(m, on_connect); mosquitto_publish_callback_set(m, on_publish); mosquitto_subscribe_callback_set(m, on_subscribe); mosquitto_message_callback_set(m, on_message); const int res = mosquitto_connect(m, options.brokerHostname, options.brokerPort, options.keepaliveSeconds); return res == MOSQ_ERR_SUCCESS; }
// establish the MQTT connection int mqtt_loop(char *host, int port, char *username, char *password) { struct mosquitto *mosq; int r = 0; mosquitto_lib_init(); if ((mosq = mosquitto_new(NULL, true, NULL)) == NULL) { r = -1; printf("Failed to build the MQTT object.\n" ); goto FUNCTION_END; } // declare the callback functions mosquitto_connect_callback_set(mosq, mqtt_callback_connect); mosquitto_message_callback_set(mosq, mqtt_callback_message); mosquitto_disconnect_callback_set(mosq, mqtt_callback_disconnect); // authentication r = mosquitto_username_pw_set(mosq, username, password); if (r) { r = -2; printf("Failed to setup the MQTT authentication - %d.\n", r); goto FUNCTION_END; } // connect and wait if (mosquitto_connect(mosq, host, port, 60) == MOSQ_ERR_SUCCESS) { r = 0; mosquitto_loop_forever(mosq, -1, 1); } else { r = -3; printf("Failed to connect to MQTT broker.\n"); } mosquitto_destroy(mosq); FUNCTION_END: mosquitto_lib_cleanup(); return r; }
int main(int argc, char *argv[]) { char clientid[24]="FcBitu'"; int rc = 0; //int nr = 0; struct mosquitto *mosq; int mid; signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); mosquitto_lib_init(); mosq = mosquitto_new(clientid, true, NULL); if(mosq){ mosquitto_connect_callback_set(mosq, connect_callback); mosquitto_message_callback_set(mosq, message_callback); rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60); mosquitto_subscribe(mosq, NULL, "coords", 0); while(run){ rc = mosquitto_loop(mosq, 1, 1); if(run && rc){ sleep(2); printf ("\n run: %d rc: %d \n", run,rc); mosquitto_reconnect(mosq); } printf("robot: timestamp: %d X: %d Y: %d unghi: %d\n ",coordrob[4] .timestamp,coordrob[4] .x,coordrob[4] .y,coordrob[4] .angle); printf("0: timestamp: %d X: %d Y: %d unghi: %d\n ",coordrob[0] .timestamp,coordrob[0] .x,coordrob[0] .y,coordrob[0] .angle); quarterback_oriented(); mosquitto_publish(mosq, &mid, idfundas, sizeof(struct control), &ctr[fundas], 0, false); mosquitto_publish(mosq, &mid, idminge, sizeof(struct control), &ctr[0], 0, false); //mosquitto_publish(mosq, &mid, idportar, sizeof(struct control), &ctr[ceva_portar], 0, false); //usleep(1000); /*if (nr >= 255) nr = 0; ctr.right = nr; ctr.left = nr; nr = nr + 1; printf ("comenzi robot: ctr.right: %d ctr.left: %d timestamp: %d\n", ctr.right,ctr.left,coordrob[0].timestamp); fflush(stdout);*/ } mosquitto_destroy(mosq); } mosquitto_lib_cleanup(); return rc; }
/* Initialize a mosquitto client. */ static struct mosquitto *init(struct client_info *info) { void *udata = (void *)info; size_t buf_sz = 32; char buf[buf_sz]; if (buf_sz < snprintf(buf, buf_sz, "client_%d", info->pid)) { return NULL; /* snprintf buffer failure */ } mosquitto_lib_init(); /* Create a new mosquitto client, with the name "client_#{PID}". */ struct mosquitto *m = mosquitto_new(buf, true, udata); return m; }
int setup_mq(const char* host) { int mq_maj, mq_min, mq_rev; mosquitto_lib_version(&mq_maj, &mq_min, &mq_rev); DLOG("You've got mosquitto version major:%d, minor:%d, rev:%d\n", mq_maj, mq_min, mq_rev); mosquitto_lib_init(); pid_t pid = getpid(); char clientid[40]; snprintf(clientid, sizeof (clientid), "rme_signed_demo_%d", pid); mosq = mosquitto_new(clientid, NULL); ILOG("Connecting to %s\n", host); mosquitto_connect(mosq, host, 1883, 15, true); return 0; }
int auth_mqtt_init(void) { mosquitto_lib_init(); auth_mqtt_mosq = mosquitto_new("auth_client_XXXXXXXXXXXX", true, NULL); if (!auth_mqtt_mosq) { mosquitto_lib_cleanup(); return -1; } mosquitto_username_pw_set(auth_mqtt_mosq, "#qmsw2..5#", "@oawifi15%"); mosquitto_connect_callback_set(auth_mqtt_mosq, connect_callback); mosquitto_message_callback_set(auth_mqtt_mosq, message_callback); mosquitto_connect(auth_mqtt_mosq, "127.0.0.1", 1883, 60); return 0; }
int main(int argc, char *argv[]) { char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; /* argv[1] is the filename, argv[2] is the number of MAC addresses */ /* and argv[3] is the timestamp in secs since the epoch */ if(argc==4) { macFile=argv[1]; messagesToSend=atoi(argv[2]); if(messagesToSend==0) { return 1; } timeNow=argv[3]; } else { printf("USAGE: thingithonmq macFileName numRecords timeStamp\n Where timeStamp is in seconds, like date +\%s\n"); return 1; } /* Start up Mosquitto */ mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
int emit_init() { char *host, *buf = strdup(config.emit_option), *bp; int port, rc; if ((bp = strchr(buf, '/')) == NULL) { fprintf(stderr, "MQTT emitter option is bad (no slash)\n"); return (1); } *bp = 0; port = atoi(bp+1); host = buf; mosquitto_lib_init(); config.mosq = mosquitto_new(NULL, true, NULL); if (!config.mosq) { fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } rc = mosquitto_connect(config.mosq, host, port, 60); if (rc) { if (rc == MOSQ_ERR_ERRNO) { char err[1024]; strerror_r(errno, err, 1024); fprintf(stderr, "Error connecting to %s:%d: %s\n", host, port, err); } else { fprintf(stderr, "Unable to connect (%d).\n", rc); } mosquitto_lib_cleanup(); return rc; } mosquitto_loop_start(config.mosq); olog("[*] Connected to MQTT at %s:%d with nsid=%s (topic=%s)\n\n", host, port, config.nsid, config.emit_topic); free(buf); return (0); }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("01-unpwd-set", true, NULL); mosquitto_username_pw_set(mosq, "uname", ";'[08gn=#"); rc = mosquitto_connect(mosq, "localhost", 1888, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("publish-qos0-test", true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_publish_callback_set(mosq, on_publish); rc = mosquitto_connect(mosq, "localhost", 1888, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { char clientid[24]="Move Test"; struct mosquitto *mosq; int rc = 0; int mid; signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); mosquitto_lib_init(); mosq = mosquitto_new(clientid, true, NULL); if(mosq) { mosquitto_connect_callback_set(mosq, connect_callback); mosquitto_message_callback_set(mosq, message_callback); rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60); mosquitto_subscribe(mosq, NULL, "#", 0); ctr.left=100; ctr.right=100; ctr.time=0; mosquitto_publish(mosq, &mid, "in15", sizeof(ctr), &ctr, 2, false); while(run) { rc = mosquitto_loop(mosq, -1, 1); if(run && rc) { sleep(20); mosquitto_reconnect(mosq); } } mosquitto_destroy(mosq); } mosquitto_lib_cleanup(); return rc; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("08-ssl-connect-crt-auth-enc", true, NULL); mosquitto_tls_opts_set(mosq, 1, "tlsv1", NULL); mosquitto_tls_set(mosq, "../ssl/test-root-ca.crt", "../ssl/certs", "../ssl/client-encrypted.crt", "../ssl/client-encrypted.key", password_callback); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); rc = mosquitto_connect(mosq, "localhost", 1888, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { struct mosquitto *mosq; int fd; bool clean_session = true; int keepalive = 60; mosq = mosquitto_new("packetgen", NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } /* CONNECT */ fd = open("mqtt.connect", O_CREAT|O_WRONLY, 00644); if(fd<0){ fprintf(stderr, "Error: Unable to open mqtt.connect for writing.\n"); return 1; } mosq->core.sock = fd; printf("_mosquitto_send_connect(): %d\n", _mosquitto_send_connect(mosq, keepalive, clean_session)); printf("loop: %d\n", mosquitto_loop_write(mosq)); close(fd); /* SUBSCRIBE */ fd = open("mqtt.subscribe", O_CREAT|O_WRONLY, 00644); if(fd<0){ fprintf(stderr, "Error: Unable to open mqtt.subscribe for writing.\n"); return 1; } mosq->core.sock = fd; printf("_mosquitto_send_subscribe(): %d\n", _mosquitto_send_subscribe(mosq, NULL, false, "subscribe/topic", 2)); printf("loop: %d\n", mosquitto_loop_write(mosq)); close(fd); mosquitto_destroy(mosq); return 0; }
int mqtt_open(mqtt_t inst, dispatch_t d, const char* name, const char* host, int port, int keepalive) { int rval = -1; int fd; inst->mosq = mosquitto_new(name, true, inst); if (inst->mosq == NULL) { error("Cannot create mqtt"); } else { inst->dispatch = d; mosquitto_log_callback_set(inst->mosq, on_log); mosquitto_connect_callback_set(inst->mosq, on_connect); mosquitto_disconnect_callback_set(inst->mosq, on_disconnect); mosquitto_publish_callback_set(inst->mosq, on_publish); mosquitto_message_callback_set(inst->mosq, on_message); mosquitto_subscribe_callback_set(inst->mosq, on_subscribe); mosquitto_unsubscribe_callback_set(inst->mosq, on_unsubscribe); inst->timer = dispatch_create_timer(d, 2000000, mqtt_misc, inst); if (inst->timer == NULL) { error("Cannot create timer"); } rval = mosquitto_connect(inst->mosq, host, port, keepalive); if (rval != MOSQ_ERR_SUCCESS) { error("Cannot connect mqtt"); } else { fd = mosquitto_socket(inst->mosq); rval = dispatch_register(d, fd, mqtt_read, mqtt_write, NULL, mqtt_close, inst); if (rval != 0) { error("Cannot register mqtt"); } } } return rval; }