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; }
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[]) { 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; }
/* {{{ Mosquitto\Client::connect() */ PHP_METHOD(Mosquitto_Client, connect) { mosquitto_client_object *object; char *host = NULL, *interface = NULL; int host_len, interface_len, retval; long port = 1883; long keepalive = 0; PHP_MOSQUITTO_ERROR_HANDLING(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lls!", &host, &host_len, &port, &keepalive, &interface, &interface_len) == FAILURE) { PHP_MOSQUITTO_RESTORE_ERRORS(); return; } PHP_MOSQUITTO_RESTORE_ERRORS(); object = (mosquitto_client_object *) mosquitto_client_object_get(getThis() TSRMLS_CC); if (interface == NULL) { retval = mosquitto_connect(object->client, host, port, keepalive); } else { retval = mosquitto_connect_bind(object->client, host, port, keepalive, interface); } php_mosquitto_handle_errno(retval, errno TSRMLS_CC); RETURN_LONG(retval); }
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; }
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[]) { 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; }
static int ctx_connect(lua_State *L) { ctx_t *ctx = ctx_check(L, 1); const char *host = luaL_optstring(L, 2, "localhost"); int port = luaL_optinteger(L, 3, 1883); int keepalive = luaL_optinteger(L, 4, 60); int rc = mosquitto_connect(ctx->mosq, host, port, keepalive); return mosq__pstatus(L, 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; }
static int mosq_connect(lua_State *L) { mosq_t *ctx = mosq_get(L, 1); const char *host = luaL_optstring(L, 2, "localhost"); int port = luaL_optinteger(L, 3, 1883); int keepalive = luaL_optinteger(L, 4, 60); int rc = mosquitto_connect(ctx->mosq, host, port, keepalive); return make_mosq_status_result(L, rc); }
void QMosquitto::connectTo(const QString &remote, const int port) { this->close(); std::string s_remote = remote.toStdString(); int ret = mosquitto_connect(this->mosq, s_remote.c_str(), port, 30); if(ret == MOSQ_ERR_SUCCESS) return; else if(ret == MOSQ_ERR_ERRNO) throw strerror(errno); else if(ret == MOSQ_ERR_INVAL) throw "Illegal parameters"; else throw "Unknown error"; }
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; }
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; }
bool MosquittoHandler::connectToBroker(const char *host, int port) { if(!m_mosquittoStruct) { m_lastErrorString = "Mosquitto not initialized"; return false; } int errorNum = mosquitto_connect(m_mosquittoStruct, host, port, 60, true); if( errorNum != MOSQ_ERR_SUCCESS) { m_lastErrorString = errorByNum(errorNum); return false; } return true; }
// 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 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 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 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; }
static void srv_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen) { struct mosquitto *mosq = arg; struct ares_srv_reply *reply = NULL; if(status == ARES_SUCCESS){ status = ares_parse_srv_reply(abuf, alen, &reply); if(status == ARES_SUCCESS){ // FIXME - choose which answer to use based on rfc2782 page 3. */ mosquitto_connect(mosq, reply->host, reply->port, mosq->keepalive); } }else{ _mosquitto_log_printf(mosq, MOSQ_LOG_ERR, "Error: SRV lookup failed (%d).", status); /* FIXME - calling on_disconnect here isn't correct. */ pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_disconnect){ mosq->in_callback = true; mosq->on_disconnect(mosq, mosq->userdata, 2); mosq->in_callback = false; } pthread_mutex_unlock(&mosq->callback_mutex); } }
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; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("08-tls-psk-pub", true, NULL); rc = mosquitto_tls_psk_set(mosq, "deadbeef", "psk-id", NULL); if(rc) return rc; mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_publish_callback_set(mosq, on_publish); rc = mosquitto_connect(mosq, "localhost", 1888, 60); if(rc) return rc; while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("publish-qos2-test", true, &run); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_message_callback_set(mosq, on_message); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ mosquitto_loop(mosq, 100, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("unsubscribe-test", true, NULL); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int mosquittopp::connect(const char *host, int port, int keepalive) { return mosquitto_connect(m_mosq, host, port, keepalive); }
int main(int argc, char *argv[]) { int opt; int rc = 0; struct mosquitto *mosq = NULL; luzz_mqtt_t mqtt = { .host = "localhost", .port = 1883, .keepalive = 60, .id = NULL, .clean_session = true, .timeout = 0, .max_packets = 1, .mid = NULL, .topic = NULL, .qos = 0, .retain = 0, }; luzz_ctx_t ctx = { .mqttp = &mqtt, .panel = 0, .num_leds = 4, .fps = 2, .framep = NULL, .rt_prio = -1, .memlock = -1, }; while ((opt = getopt(argc, argv, "h:p:i:n:r:P:M")) != -1) { switch (opt) { case 'h': mqtt.host = optarg; break; case 'p': mqtt.port = atoi(optarg); break; case 'i': ctx.panel = atoi(optarg); break; case 'n': ctx.num_leds = atoi(optarg); break; case 'r': ctx.fps = atoi(optarg); break; case 'P': ctx.rt_prio = atoi(optarg); break; case 'M': ctx.memlock = 1; break; default: luzz_gen_usage(&ctx); goto finish; } } struct sched_param sched = { .sched_priority = ctx.rt_prio }; if (ctx.rt_prio > -1 && sched_setscheduler(0, SCHED_FIFO, &sched) == -1) { perror("rt_prio"); goto finish; } if (ctx.memlock > -1 && mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { perror("memlock"); goto finish; } int i = 0; bool red = true; struct timespec ts_remain; struct timespec ts_request = { .tv_sec = 0, .tv_nsec = 1e9 / ctx.fps }; ctx.framep = calloc(ctx.num_leds, sizeof(luzz_grb_t)); if (ctx.framep == NULL) { goto oom; } mosquitto_lib_init(); if (uname(&ctx.uts) < 0) { perror("uname"); goto finish; } if ((rc = asprintf(&mqtt.id, LUZZ_GEN_ID_TPL, LUZZ_VERSION, ctx.uts.nodename, getpid())) < 0) { goto oom; } mosq = mosquitto_new(mqtt.id, mqtt.clean_session, &ctx); if (!mosq) { switch (errno) { case ENOMEM: rc = 1; goto oom; case EINVAL: fprintf(stderr, "mosq_new: Invalid id and/or clean_session.\n"); rc = 1; goto finish; } } mosquitto_connect(mosq, mqtt.host, mqtt.port, mqtt.keepalive); if ((rc = asprintf(&mqtt.topic, LUZZ_TOPIC_TPL, ctx.panel)) < 0) { goto oom; } while (true) { ((luzz_rgb_t *)ctx.framep + i++)->r = red ? 0xFF : 0x00; if (i == ctx.num_leds - 1) { i = 0; red = !red; } mosquitto_publish(mosq, mqtt.mid, mqtt.topic, sizeof(luzz_rgb_t) * ctx.num_leds, (const void *)ctx.framep, mqtt.qos, mqtt.retain); while ((rc = mosquitto_loop(mosq, mqtt.timeout, mqtt.max_packets)) != MOSQ_ERR_SUCCESS) { switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_loop: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_NOMEM: goto oom; case MOSQ_ERR_PROTOCOL: fprintf(stderr, "mosq_loop: MQTT Protocol error.\n"); goto finish; case MOSQ_ERR_ERRNO: perror("mosq_loop"); goto finish; case MOSQ_ERR_NO_CONN: fprintf(stderr, "mosq_loop: No broker connection.\n"); break; case MOSQ_ERR_CONN_LOST: fprintf(stderr, "mosq_loop: Connection to broker was lost.\n"); break; } sleep(1); mosquitto_reconnect(mosq); } nanosleep(&ts_request, &ts_remain); } oom: fprintf(stderr, "error: Out of memory.\n"); finish: mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return rc; }