int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg) { int rc; 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(); return 1; } 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(); return 1; } #ifdef WITH_TLS if((cfg->cafile || cfg->capath) && mosquitto_tls_set(mosq, cfg->cafile, cfg->capath, cfg->certfile, cfg->keyfile, NULL)) { if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; } if(cfg->insecure && mosquitto_tls_insecure_set(mosq, true)) { if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS insecure option.\n"); mosquitto_lib_cleanup(); return 1; } # ifdef WITH_TLS_PSK if(cfg->psk && mosquitto_tls_psk_set(mosq, cfg->psk, cfg->psk_identity, NULL)) { if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); mosquitto_lib_cleanup(); return 1; } # endif if(cfg->tls_version && mosquitto_tls_opts_set(mosq, 1, cfg->tls_version, cfg->ciphers)) { if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; } #endif mosquitto_max_inflight_messages_set(mosq, cfg->max_inflight); #ifdef WITH_SOCKS if(cfg->socks5_host) { rc = mosquitto_socks5_set(mosq, cfg->socks5_host, cfg->socks5_port, cfg->socks5_username, cfg->socks5_password); if(rc) { mosquitto_lib_cleanup(); return rc; } } #endif mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &(cfg->protocol_version)); return MOSQ_ERR_SUCCESS; }
static int ctx_will_set(lua_State *L) { ctx_t *ctx = ctx_check(L, 1); const char *topic = luaL_checkstring(L, 2); size_t payloadlen = 0; const void *payload = NULL; if (!lua_isnil(L, 3)) { payload = lua_tolstring(L, 3, &payloadlen); }; int qos = luaL_optinteger(L, 4, 0); bool retain = lua_toboolean(L, 5); int rc = mosquitto_will_set(ctx->mosq, topic, payloadlen, payload, qos, retain); return mosq__pstatus(L, rc); }
/* {{{ Mosquitto\Client::setWill() */ PHP_METHOD(Mosquitto_Client, setWill) { mosquitto_client_object *object; int topic_len, payload_len, retval; long qos; zend_bool retain; char *topic, *payload; PHP_MOSQUITTO_ERROR_HANDLING(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sslb", &topic, &topic_len, &payload, &payload_len, &qos, &retain) == FAILURE) { PHP_MOSQUITTO_RESTORE_ERRORS(); return; } PHP_MOSQUITTO_RESTORE_ERRORS(); object = (mosquitto_client_object *) mosquitto_client_object_get(getThis() TSRMLS_CC); retval = mosquitto_will_set(object->client, topic, payload_len, (void *) payload, qos, retain); php_mosquitto_handle_errno(retval, errno TSRMLS_CC); }
int mosquittopp::will_set(const char *topic, int payloadlen, const void *payload, int qos, bool retain) { return mosquitto_will_set(m_mosq, topic, payloadlen, payload, qos, retain); }
int main(int argc, char *argv[]) { char *id = NULL; char *id_prefix = NULL; int i; char *host = "localhost"; int port = 1883; int keepalive = 60; char buf[1024]; bool debug = false; struct mosquitto *mosq = NULL; int rc; int rc2; char hostname[256]; char err[1024]; int len; char *will_payload = NULL; long will_payloadlen = 0; int will_qos = 0; bool will_retain = false; char *will_topic = NULL; char *cafile = NULL; char *capath = NULL; char *certfile = NULL; char *keyfile = NULL; char *psk = NULL; char *psk_identity = NULL; for(i=1; i<argc; i++){ if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i==argc-1){ fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); print_usage(); return 1; }else{ port = atoi(argv[i+1]); if(port<1 || port>65535){ fprintf(stderr, "Error: Invalid port given: %d\n", port); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--cafile")){ if(i==argc-1){ fprintf(stderr, "Error: --cafile argument given but no file specified.\n\n"); print_usage(); return 1; }else{ cafile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--capath")){ if(i==argc-1){ fprintf(stderr, "Error: --capath argument given but no directory specified.\n\n"); print_usage(); return 1; }else{ capath = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--cert")){ if(i==argc-1){ fprintf(stderr, "Error: --cert argument given but no file specified.\n\n"); print_usage(); return 1; }else{ certfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){ debug = true; }else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -f argument given but no file specified.\n\n"); print_usage(); return 1; }else{ if(load_file(argv[i+1])) return 1; } i++; }else if(!strcmp(argv[i], "--help")){ print_usage(); return 0; }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){ if(i==argc-1){ fprintf(stderr, "Error: -h argument given but no host specified.\n\n"); print_usage(); return 1; }else{ host = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){ if(id_prefix){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -i argument given but no id specified.\n\n"); print_usage(); return 1; }else{ id = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){ if(id){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n"); print_usage(); return 1; }else{ id_prefix = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--key")){ if(i==argc-1){ fprintf(stderr, "Error: --key argument given but no file specified.\n\n"); print_usage(); return 1; }else{ keyfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-l") || !strcmp(argv[i], "--stdin-line")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ mode = MSGMODE_STDIN_LINE; } }else if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--message")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -m argument given but no message specified.\n\n"); print_usage(); return 1; }else{ message = argv[i+1]; msglen = strlen(message); mode = MSGMODE_CMD; } i++; }else if(!strcmp(argv[i], "-n") || !strcmp(argv[i], "--null-message")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ mode = MSGMODE_NULL; } }else if(!strcmp(argv[i], "--psk")){ if(i==argc-1){ fprintf(stderr, "Error: --psk argument given but no key specified.\n\n"); print_usage(); return 1; }else{ psk = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--psk-identity")){ if(i==argc-1){ fprintf(stderr, "Error: --psk-identity argument given but no identity specified.\n\n"); print_usage(); return 1; }else{ psk_identity = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){ if(i==argc-1){ fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n"); print_usage(); return 1; }else{ qos = atoi(argv[i+1]); if(qos<0 || qos>2){ fprintf(stderr, "Error: Invalid QoS given: %d\n", qos); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--quiet")){ quiet = true; }else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--retain")){ retain = 1; }else if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--stdin-file")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ if(load_stdin()) return 1; } }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); print_usage(); return 1; }else{ topic = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){ if(i==argc-1){ fprintf(stderr, "Error: -u argument given but no username specified.\n\n"); print_usage(); return 1; }else{ username = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); print_usage(); return 1; }else{ password = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); print_usage(); return 1; }else{ will_payload = argv[i+1]; will_payloadlen = strlen(will_payload); } i++; }else if(!strcmp(argv[i], "--will-qos")){ if(i==argc-1){ fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n"); print_usage(); return 1; }else{ will_qos = atoi(argv[i+1]); if(will_qos < 0 || will_qos > 2){ fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos); return 1; } } i++; }else if(!strcmp(argv[i], "--will-retain")){ will_retain = true; }else if(!strcmp(argv[i], "--will-topic")){ if(i==argc-1){ fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n"); print_usage(); return 1; }else{ will_topic = argv[i+1]; } i++; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); print_usage(); return 1; } } if(!topic || mode == MSGMODE_NONE){ fprintf(stderr, "Error: Both topic and message must be supplied.\n"); print_usage(); return 1; } if(will_payload && !will_topic){ fprintf(stderr, "Error: Will payload given, but no will topic given.\n"); print_usage(); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "Error: Will retain given, but no will topic given.\n"); print_usage(); return 1; } if(password && !username){ if(!quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); } if((certfile && !keyfile) || (keyfile && !certfile)){ fprintf(stderr, "Error: Both certfile and keyfile must be provided if one of them is.\n"); print_usage(); return 1; } if((cafile || capath) && psk){ if(!quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); return 1; } if(psk && !psk_identity){ if(!quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); return 1; } mosquitto_lib_init(); if(id_prefix){ id = malloc(strlen(id_prefix)+10); if(!id){ if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid()); }else if(!id){ hostname[0] = '\0'; gethostname(hostname, 256); hostname[255] = '\0'; len = strlen("mosqpub/-") + 6 + strlen(hostname); id = malloc(len); if(!id){ if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, len, "mosqpub/%d-%s", getpid(), hostname); if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } } mosq = mosquitto_new(id, true, NULL); if(!mosq){ switch(errno){ case ENOMEM: if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); break; case EINVAL: if(!quiet) fprintf(stderr, "Error: Invalid id.\n"); break; } mosquitto_lib_cleanup(); return 1; } if(debug){ mosquitto_log_callback_set(mosq, my_log_callback); } if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ if(!quiet) fprintf(stderr, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } if(username && mosquitto_username_pw_set(mosq, username, password)){ if(!quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } if((cafile || capath) && mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL)){ if(!quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; } if(psk && mosquitto_tls_psk_set(mosq, psk, psk_identity, NULL)){ if(!quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); rc = mosquitto_connect(mosq, host, port, keepalive); if(rc){ if(!quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 strerror_r(errno, err, 1024); #else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); #endif fprintf(stderr, "Error: %s\n", err); }else{ fprintf(stderr, "Unable to connect (%d).\n", rc); } } mosquitto_lib_cleanup(); return rc; } if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_start(mosq); } do{ if(mode == MSGMODE_STDIN_LINE){ if(status == STATUS_CONNACK_RECVD){ if(fgets(buf, 1024, stdin)){ buf[strlen(buf)-1] = '\0'; rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, retain); if(rc2){ if(!quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); mosquitto_disconnect(mosq); } }else if(feof(stdin)){ last_mid = mid_sent; status = STATUS_WAITING; } }else if(status == STATUS_WAITING){ #ifdef WIN32 Sleep(1000); #else usleep(1000000); #endif } rc = MOSQ_ERR_SUCCESS; }else{ rc = mosquitto_loop(mosq, -1, 1); } }while(rc == MOSQ_ERR_SUCCESS && connected); if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_stop(mosq, false); } if(message && mode == MSGMODE_FILE){ free(message); } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); if(rc){ if(rc == MOSQ_ERR_ERRNO){ fprintf(stderr, "Error: %s\n", strerror(errno)); }else{ fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); } } return rc; }
void /*PORT_NAME*/_setup() { char *id = NULL; char *id_prefix = NULL; int i; char *host = "/*HOST_ADDRESS*/"; int port = /*PORT_NUMBER*/; int keepalive = 60; bool clean_session = true; bool debug = false; int rc, rc2; char hostname[21]; char err[1024]; uint8_t *will_payload = NULL; long will_payloadlen = 0; int will_qos = 0; bool will_retain = false; char *will_topic = NULL; /*MULTI_TOPIC_INIT*/ /*TRACE_LEVEL_1*/printf("[/*PORT_NAME*/] Initialization MQTT at %s:%i\n", host, port); if(clean_session == false && (id_prefix || !id)){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: You must provide a client id if you are using the -c option.\n"); return 1; } if(id_prefix){ id = malloc(strlen(id_prefix)+10); if(!id){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Out of memory.\n"); return 1; } snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid()); }else if(!id){ id = malloc(30); if(!id){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Out of memory.\n"); return 1; } memset(hostname, 0, 21); gethostname(hostname, 20); snprintf(id, 23, "mosq_sub_%d_%s", getpid(), hostname); } if(will_payload && !will_topic){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Will payload given, but no will topic given.\n"); return 1; } if(will_retain && !will_topic){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Will retain given, but no will topic given.\n"); return 1; } if(/*PORT_NAME*/_password && !/*PORT_NAME*/_username){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Warning: Not using password since username not set.\n"); } mosquitto_lib_init(); /*PORT_NAME*/_mosq = mosquitto_new(id, NULL); if(!/*PORT_NAME*/_mosq){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Out of memory.\n"); return 1; } if(will_topic && mosquitto_will_set(/*PORT_NAME*/_mosq, true, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Problem setting will.\n"); return 1; } if(/*PORT_NAME*/_username && mosquitto_username_pw_set(/*PORT_NAME*/_mosq, /*PORT_NAME*/_username, /*PORT_NAME*/_password)){ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: Problem setting username and password.\n"); return 1; } mosquitto_connect_callback_set(/*PORT_NAME*/_mosq, /*PORT_NAME*/_connect_callback); mosquitto_message_callback_set(/*PORT_NAME*/_mosq, /*PORT_NAME*/_message_callback); /*TRACE_LEVEL_1*/mosquitto_subscribe_callback_set(/*PORT_NAME*/_mosq, /*PORT_NAME*/_subscribe_callback); rc = mosquitto_connect(/*PORT_NAME*/_mosq, host, port, keepalive, clean_session); if(rc){ /*TRACE_LEVEL_1*/if(rc == MOSQ_ERR_ERRNO){ /*TRACE_LEVEL_1*/strerror_r(errno, err, 1024); /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Error: %s\n", err); /*TRACE_LEVEL_1*/}else{ /*TRACE_LEVEL_1*/fprintf(stderr, "[/*PORT_NAME*/] Unable to connect (%d).\n", rc); /*TRACE_LEVEL_1*/} //return rc; } }
/* * ########################## * Main Function * ########################## */ int main(int argc, char *argv[]) { /* program setup variable */ struct mosquitto *mosq = NULL; struct mqtt_userdata *ud = NULL; unsigned int max_inflight = 20; bool debug = false; char buf[MQTT_BUFSIZE]; char err[MQTT_ERR_BUFSIZE]; /* client id */ char id[MQTT_ID_LEN]; char id_prefix[MQTT_ID_LEN]; char hostname[MQTT_HOSTNAME_BUFSIZE]; /* broker variable */ char host[MQTT_IP_LEN] = "127.0.0.1"; int port = 1883; int keepalive = 60; /* will information */ char *will_topic = NULL; long will_payloadlen = 0; char *will_payload = NULL; int will_qos = 0; bool will_retain = false; /* temp variable */ int i; int rc; int rc2; /* initialized program and user data structure */ ud = malloc(sizeof(struct mqtt_userdata)); memset(ud, 0, sizeof(struct mqtt_userdata)); ud->last_mid = -1; ud->connected = true; memset(id, '\0', sizeof(id)); memset(id_prefix, '\0', sizeof(id_prefix)); ud->qos = 2; /* get option */ for(i=1; i<argc; i++){ if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i==argc-1){ fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); mqtt_print_usage(); return 1; }else{ port = atoi(argv[i+1]); if(port<1 || port>65535){ fprintf(stderr, "Error: Invalid port given: %d\n", port); mqtt_print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){ debug = true; }else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")){ if(ud->mode != MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); mqtt_print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -f argument given but no file specified.\n\n"); mqtt_print_usage(); return 1; }else{ if(mqtt_load_file(argv[i+1], ud)) return 1; } i++; }else if(!strcmp(argv[i], "--help")){ mqtt_print_usage(); return 0; }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){ if(i==argc-1){ fprintf(stderr, "Error: -h argument given but no host specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MQTT_IP_LEN) { fprintf(stderr, "Error: max length of ip is %d.\n\n", MQTT_IP_LEN); mqtt_print_usage(); } else { memset(host, '\0', sizeof(host)); strcpy(host, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){ if(strlen(id_prefix) != 0){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); mqtt_print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -i argument given but no id specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MOSQ_MQTT_ID_MAX_LENGTH) { fprintf(stderr, "Error: max length of client id is %d.\n\n", MOSQ_MQTT_ID_MAX_LENGTH); mqtt_print_usage(); } else { strcpy(id, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){ if(strlen(id) != 0){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); mqtt_print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MOSQ_MQTT_ID_MAX_LENGTH) { fprintf(stderr, "Error: max length of client id is %d.\n\n", MOSQ_MQTT_ID_MAX_LENGTH); mqtt_print_usage(); } else { strcpy(id_prefix, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-l") || !strcmp(argv[i], "--stdin-line")){ if(ud->mode != MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); mqtt_print_usage(); return 1; }else{ ud->mode = MQTT_MSGMODE_STDIN_LINE; } }else if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--message")){ if(ud->mode != MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); mqtt_print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -m argument given but no message specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->message = argv[i+1]; ud->msglen = strlen(ud->message); ud->mode = MQTT_MSGMODE_CMD; } i++; }else if(!strcmp(argv[i], "-M")){ if(i==argc-1){ fprintf(stderr, "Error: -M argument given but max_inflight not specified.\n\n"); mqtt_print_usage(); return 1; }else{ max_inflight = atoi(argv[i+1]); } i++; }else if(!strcmp(argv[i], "-n") || !strcmp(argv[i], "--null-message")){ if(ud->mode != MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); mqtt_print_usage(); return 1; }else{ ud->mode = MQTT_MSGMODE_NULL; } }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){ if(i==argc-1){ fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->qos = atoi(argv[i+1]); if(ud->qos<0 || ud->qos>2){ fprintf(stderr, "Error: Invalid QoS given: %d\n", ud->qos); mqtt_print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--quiet")){ ud->quiet = true; }else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--retain")){ ud->retain = 1; }else if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--stdin-file")){ if(ud->mode != MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); mqtt_print_usage(); return 1; }else{ if(mqtt_load_stdin(ud)) return 1; } }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->topic = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){ if(i==argc-1){ fprintf(stderr, "Error: -u argument given but no username specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->username = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->password = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_payload = argv[i+1]; will_payloadlen = strlen(will_payload); } i++; }else if(!strcmp(argv[i], "--will-qos")){ if(i==argc-1){ fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_qos = atoi(argv[i+1]); if(will_qos < 0 || will_qos > 2){ fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos); return 1; } } i++; }else if(!strcmp(argv[i], "--will-retain")){ will_retain = true; }else if(!strcmp(argv[i], "--will-topic")){ if(i==argc-1){ fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_topic = argv[i+1]; } i++; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); mqtt_print_usage(); return 1; } } /* verify necessary variable */ if(!ud->topic || ud->mode == MQTT_MSGMODE_NONE){ fprintf(stderr, "Error: Both topic and message must be supplied.\n"); mqtt_print_usage(); return 1; } if(will_payload && !will_topic){ fprintf(stderr, "Error: Will payload given, but no will topic given.\n"); mqtt_print_usage(); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "Error: Will retain given, but no will topic given.\n"); mqtt_print_usage(); return 1; } if(ud->password && !ud->username){ if(!ud->quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); } /* init mosquitto library */ mosquitto_lib_init(); /* setup client id */ if(strlen(id_prefix) != 0){ snprintf(id, sizeof(id), "%s%d", id_prefix, getpid()); }else if(strlen(id) == 0){ memset(hostname, '\0', sizeof(hostname)); gethostname(hostname, sizeof(hostname)); snprintf(id, sizeof(id), "mosqub/%d-%s", getpid(), hostname); } if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } /* start mosquitto */ mosq = mosquitto_new(id, true, ud); if(!mosq){ if(!ud->quiet) fprintf(stderr, "Error: %s\n", strerror(errno)); mosquitto_lib_cleanup(); return 1; } /* setup mosquitto */ if(debug){ mosquitto_log_callback_set(mosq, mqtt_log_callback); } if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ if(!ud->quiet) fprintf(stderr, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } if(ud->username && mosquitto_username_pw_set(mosq, ud->username, ud->password)){ if(!ud->quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_max_inflight_messages_set(mosq, max_inflight); mosquitto_connect_callback_set(mosq, mqtt_connect_callback); mosquitto_disconnect_callback_set(mosq, mqtt_disconnect_callback); mosquitto_publish_callback_set(mosq, mqtt_publish_callback); /* connect mosquitto */ rc = mosquitto_connect(mosq, host, port, keepalive); if(rc){ if(!ud->quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 strerror_r(errno, err, sizeof(err)); #else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, sizeof(err), NULL); #endif fprintf(stderr, "Error: %s\n", err); }else{ fprintf(stderr, "Unable to connect (%d: %s).\n", rc, mosquitto_strerror(rc)); } } mosquitto_lib_cleanup(); return rc; } /* publish mosquitto mqtt message BASED ON DIFFERENT message mode */ if(ud->mode == MQTT_MSGMODE_STDIN_LINE){ mosquitto_loop_start(mosq); } do{ if(ud->mode == MQTT_MSGMODE_STDIN_LINE){ if(ud->status == MQTT_STATUS_CONNACK_RECVD){ if(fgets(buf, sizeof(buf), stdin)){ buf[strlen(buf)-1] = '\0'; rc2 = mosquitto_publish(mosq, &ud->mid_sent, ud->topic, strlen(buf), buf, ud->qos, ud->retain); if(rc2){ if(!ud->quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); mosquitto_disconnect(mosq); } }else if(feof(stdin)){ ud->last_mid = ud->mid_sent; ud->status = MQTT_STATUS_WAITING; } }else if(ud->status == MQTT_STATUS_WAITING){ #ifdef WIN32 Sleep(1000); #else usleep(1000000); #endif } rc = MOSQ_ERR_SUCCESS; }else{ rc = mosquitto_loop(mosq, -1, 1); } }while(rc == MOSQ_ERR_SUCCESS && ud->connected); if(ud->mode == MQTT_MSGMODE_STDIN_LINE){ mosquitto_loop_stop(mosq, false); } /* free mosquitto */ mosquitto_destroy(mosq); mosquitto_lib_cleanup(); mqtt_userdata_free(ud); return rc; }
int main(int argc, char *argv[]) { char *id = NULL; char *id_prefix = NULL; int i; char *host = "localhost"; int port = 1883; int keepalive = 60; bool clean_session = true; bool debug = false; struct mosquitto *mosq = NULL; int rc; char hostname[256]; char err[1024]; struct userdata ud; int len; char *will_payload = NULL; long will_payloadlen = 0; int will_qos = 0; bool will_retain = false; char *will_topic = NULL; char *cafile = NULL; char *capath = NULL; char *certfile = NULL; char *keyfile = NULL; char *psk = NULL; char *psk_identity = NULL; memset(&ud, 0, sizeof(struct userdata)); for(i=1; i<argc; i++){ if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i==argc-1){ fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); print_usage(); return 1; }else{ port = atoi(argv[i+1]); if(port<1 || port>65535){ fprintf(stderr, "Error: Invalid port given: %d\n", port); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--disable-clean-session")){ clean_session = false; }else if(!strcmp(argv[i], "--cafile")){ if(i==argc-1){ fprintf(stderr, "Error: --cafile argument given but no file specified.\n\n"); print_usage(); return 1; }else{ cafile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--capath")){ if(i==argc-1){ fprintf(stderr, "Error: --capath argument given but no directory specified.\n\n"); print_usage(); return 1; }else{ capath = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--cert")){ if(i==argc-1){ fprintf(stderr, "Error: --cert argument given but no file specified.\n\n"); print_usage(); return 1; }else{ certfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){ debug = true; }else if(!strcmp(argv[i], "--help")){ print_usage(); return 0; }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){ if(i==argc-1){ fprintf(stderr, "Error: -h argument given but no host specified.\n\n"); print_usage(); return 1; }else{ host = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){ if(id_prefix){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -i argument given but no id specified.\n\n"); print_usage(); return 1; }else{ id = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){ if(id){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n"); print_usage(); return 1; }else{ id_prefix = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-k") || !strcmp(argv[i], "--keepalive")){ if(i==argc-1){ fprintf(stderr, "Error: -k argument given but no keepalive specified.\n\n"); print_usage(); return 1; }else{ keepalive = atoi(argv[i+1]); if(keepalive>65535){ fprintf(stderr, "Error: Invalid keepalive given: %d\n", keepalive); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--key")){ if(i==argc-1){ fprintf(stderr, "Error: --key argument given but no file specified.\n\n"); print_usage(); return 1; }else{ keyfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--psk")){ if(i==argc-1){ fprintf(stderr, "Error: --psk argument given but no key specified.\n\n"); print_usage(); return 1; }else{ psk = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--psk-identity")){ if(i==argc-1){ fprintf(stderr, "Error: --psk-identity argument given but no identity specified.\n\n"); print_usage(); return 1; }else{ psk_identity = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){ if(i==argc-1){ fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n"); print_usage(); return 1; }else{ ud.topic_qos = atoi(argv[i+1]); if(ud.topic_qos<0 || ud.topic_qos>2){ fprintf(stderr, "Error: Invalid QoS given: %d\n", ud.topic_qos); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--quiet")){ ud.quiet = true; }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); print_usage(); return 1; }else{ ud.topic_count++; ud.topics = realloc(ud.topics, ud.topic_count*sizeof(char *)); ud.topics[ud.topic_count-1] = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){ if(i==argc-1){ fprintf(stderr, "Error: -u argument given but no username specified.\n\n"); print_usage(); return 1; }else{ ud.username = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")){ ud.verbose = 1; }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); print_usage(); return 1; }else{ ud.password = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); print_usage(); return 1; }else{ will_payload = argv[i+1]; will_payloadlen = strlen(will_payload); } i++; }else if(!strcmp(argv[i], "--will-qos")){ if(i==argc-1){ fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n"); print_usage(); return 1; }else{ will_qos = atoi(argv[i+1]); if(will_qos < 0 || will_qos > 2){ fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos); return 1; } } i++; }else if(!strcmp(argv[i], "--will-retain")){ will_retain = true; }else if(!strcmp(argv[i], "--will-topic")){ if(i==argc-1){ fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n"); print_usage(); return 1; }else{ will_topic = argv[i+1]; } i++; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); print_usage(); return 1; } } if(clean_session == false && (id_prefix || !id)){ if(!ud.quiet) fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n"); return 1; } if(ud.topic_count == 0){ fprintf(stderr, "Error: You must specify a topic to subscribe to.\n"); print_usage(); return 1; } if(will_payload && !will_topic){ fprintf(stderr, "Error: Will payload given, but no will topic given.\n"); print_usage(); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "Error: Will retain given, but no will topic given.\n"); print_usage(); return 1; } if(ud.password && !ud.username){ if(!ud.quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); } if((certfile && !keyfile) || (keyfile && !certfile)){ fprintf(stderr, "Error: Both certfile and keyfile must be provided if one of them is.\n"); print_usage(); return 1; } if((cafile || capath) && psk){ if(!ud.quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); return 1; } if(psk && !psk_identity){ if(!ud.quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); return 1; } mosquitto_lib_init(); if(id_prefix){ id = malloc(strlen(id_prefix)+10); if(!id){ if(!ud.quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid()); }else if(!id){ hostname[0] = '\0'; gethostname(hostname, 256); hostname[255] = '\0'; len = strlen("mosqsub/-") + 6 + strlen(hostname); id = malloc(len); if(!id){ if(!ud.quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, len, "mosqsub/%d-%s", getpid(), hostname); if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } } mosq = mosquitto_new(id, clean_session, &ud); if(!mosq){ switch(errno){ case ENOMEM: if(!ud.quiet) fprintf(stderr, "Error: Out of memory.\n"); break; case EINVAL: if(!ud.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); break; } mosquitto_lib_cleanup(); return 1; } if(debug){ mosquitto_log_callback_set(mosq, my_log_callback); } if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ if(!ud.quiet) fprintf(stderr, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } if(ud.username && mosquitto_username_pw_set(mosq, ud.username, ud.password)){ if(!ud.quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } if((cafile || capath) && mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL)){ if(!ud.quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; } if(psk && mosquitto_tls_psk_set(mosq, psk, psk_identity, NULL)){ if(!ud.quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); if(debug){ mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); } rc = mosquitto_connect(mosq, host, port, keepalive); if(rc){ if(!ud.quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 strerror_r(errno, err, 1024); #else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); #endif fprintf(stderr, "Error: %s\n", err); }else{ fprintf(stderr, "Unable to connect (%d).\n", rc); } } return rc; mosquitto_lib_cleanup(); } rc = mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); if(rc){ if(rc == MOSQ_ERR_ERRNO){ fprintf(stderr, "Error: %s\n", strerror(errno)); }else{ fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); } } return rc; }
void MQTT_setup() { printf("[MQTT] setup\n"); char *id = NULL; char *id_prefix = NULL; int i; char *host = "192.168.1.127"; int port = 1883; int keepalive = 60; bool clean_session = true; bool debug = false; int rc, rc2; char hostname[21]; char err[1024]; uint8_t *will_payload = NULL; long will_payloadlen = 0; int will_qos = 0; bool will_retain = false; char *will_topic = NULL; printf("[MQTT] Initialization MQTT at %s:%i\n", host, port); if(clean_session == false && (id_prefix || !id)){ fprintf(stderr, "[MQTT] Error: You must provide a client id if you are using the -c option.\n"); return 1; } if(id_prefix){ id = malloc(strlen(id_prefix)+10); if(!id){ fprintf(stderr, "[MQTT] Error: Out of memory.\n"); return 1; } snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid()); }else if(!id){ id = malloc(30); if(!id){ fprintf(stderr, "[MQTT] Error: Out of memory.\n"); return 1; } memset(hostname, 0, 21); gethostname(hostname, 20); snprintf(id, 23, "mosq_sub_%d_%s", getpid(), hostname); } if(will_payload && !will_topic){ fprintf(stderr, "[MQTT] Error: Will payload given, but no will topic given.\n"); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "[MQTT] Error: Will retain given, but no will topic given.\n"); return 1; } if(MQTT_password && !MQTT_username){ fprintf(stderr, "[MQTT] Warning: Not using password since username not set.\n"); } mosquitto_lib_init(); MQTT_mosq = mosquitto_new(id, NULL); if(!MQTT_mosq){ fprintf(stderr, "[MQTT] Error: Out of memory.\n"); return 1; } if(will_topic && mosquitto_will_set(MQTT_mosq, true, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ fprintf(stderr, "[MQTT] Error: Problem setting will.\n"); return 1; } if(MQTT_username && mosquitto_username_pw_set(MQTT_mosq, MQTT_username, MQTT_password)){ fprintf(stderr, "[MQTT] Error: Problem setting username and password.\n"); return 1; } mosquitto_connect_callback_set(MQTT_mosq, MQTT_connect_callback); mosquitto_message_callback_set(MQTT_mosq, MQTT_message_callback); mosquitto_subscribe_callback_set(MQTT_mosq, MQTT_subscribe_callback); rc = mosquitto_connect(MQTT_mosq, host, port, keepalive, clean_session); if(rc){ if(rc == MOSQ_ERR_ERRNO){ strerror_r(errno, err, 1024); fprintf(stderr, "[MQTT] Error: %s\n", err); }else{ fprintf(stderr, "[MQTT] Unable to connect (%d).\n", rc); } //return rc; } }
int main(int argc, char *argv[]) { int sd = -1; char *conf_file = NULL; struct mosquitto *mosq; struct module *md; struct device *dev; int rc; int i; gbuf[0] = 0; if (!quiet) printf("Version: %s\n", version); signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); signal(SIGUSR1, handle_signal); signal(SIGUSR2, handle_signal); signal(SIGALRM, each_sec); for (i=1; i<argc; i++) { if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")){ if(i==argc-1){ fprintf(stderr, "Error: -c argument given but no file specified.\n\n"); print_usage(argv[0]); return 1; }else{ conf_file = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--quiet")){ quiet = true; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); print_usage(argv[0]); return 1; } } if (!conf_file) { fprintf(stderr, "Error: No config file given.\n"); return 1; } memset(&config, 0, sizeof(struct bridge_config)); if (config_parse(conf_file, &config)) return 1; if (quiet) config.debug = 0; if (config.debug != 0) printf("Debug: %d\n", config.debug); rc = bridge_init(&bridge, config.id, MODULES_BRIDGE_ID); if (rc) { if (config.debug) printf("Error: Failed to initialize bridge: %d\n", rc); return 1; } mosquitto_lib_init(); mosq = mosquitto_new(config.id, true, NULL); if(!mosq){ fprintf(stderr, "Error creating mqtt instance.\n"); switch(errno){ case ENOMEM: fprintf(stderr, " out of memory.\n"); break; case EINVAL: fprintf(stderr, " invalid id.\n"); break; } return 1; } snprintf(gbuf, GBUF_SIZE, "%d", PROTOCOL_TIMEOUT); mosquitto_will_set(mosq, bridge.bridge_dev->status_topic, strlen(gbuf), gbuf, config.mqtt_qos, MQTT_RETAIN); mosquitto_connect_callback_set(mosq, on_mqtt_connect); mosquitto_disconnect_callback_set(mosq, on_mqtt_disconnect); mosquitto_message_callback_set(mosq, on_mqtt_message); mosquitto_user_data_set(mosq, &sd); md = bridge_add_module(bridge.bridge_dev, MODULES_MQTT_ID, true); if (!md) { fprintf(stderr, "Failed to add MQTT module.\n"); return 1; } if (config.scripts_folder) { if (access(config.scripts_folder, R_OK )) { fprintf(stderr, "Couldn't open scripts folder: %s\n", config.scripts_folder); return 1; } md = bridge_add_module(bridge.bridge_dev, MODULES_SCRIPT_ID, true); if (!md) { fprintf(stderr, "Failed to add script module.\n"); return 1; } } if (config.interface) { //TODO: check if interface exists if (access("/proc/net/dev", R_OK )) { fprintf(stderr, "Couldn't open /proc/net/dev\n"); return 1; } md = bridge_add_module(bridge.bridge_dev, MODULES_BANDWIDTH_ID, true); if (!md) { fprintf(stderr, "Failed to add bandwidth module.\n"); return 1; } bandwidth = true; } if (config.serial.port) { sd = serialport_init(config.serial.port, config.serial.baudrate); if( sd == -1 ) { fprintf(stderr, "Couldn't open serial port.\n"); return 1; } else { md = bridge_add_module(bridge.bridge_dev, MODULES_SERIAL_ID, true); if (!md) { fprintf(stderr, "Failed to add serial module.\n"); return 1; } serialport_flush(sd); bridge.serial_ready = true; if (config.debug) printf("Serial ready.\n"); } } if (config.debug > 2) bridge_print_modules(bridge.bridge_dev); rc = mosquitto_connect(mosq, config.mqtt_host, config.mqtt_port, 60); if (rc) { fprintf(stderr, "ERROR: %s\n", mosquitto_strerror(rc)); return -1; } alarm(1); while (run) { if (bridge.serial_ready) { rc = serial_in(sd, mosq, MODULES_SERIAL_ID); if (rc == -1) { serial_hang(mosq); } else if (rc > 0) { bridge.serial_alive = DEVICE_ALIVE_MAX; } } if (user_signal) { if (config.debug > 2) printf("Signal - SIGUSR: %d\n", user_signal); signal_usr(sd, mosq); } rc = mosquitto_loop(mosq, 100, 1); if (run && rc) { if (config.debug > 2) printf("MQTT loop: %s\n", mosquitto_strerror(rc)); usleep(100000); // wait 100 msec mosquitto_reconnect(mosq); } usleep(20); if (every1s) { every1s = false; for (dev = bridge.dev_list; dev != NULL; dev = dev->next) { if (dev->alive) { dev->alive--; if (!dev->alive) { if (connected) { snprintf(gbuf, GBUF_SIZE, "%d", PROTOCOL_TIMEOUT); mqtt_publish(mosq, dev->status_topic, gbuf); rc = mosquitto_unsubscribe(mosq, NULL, dev->config_topic); if (rc) fprintf(stderr, "Error: MQTT unsubscribe returned: %s\n", mosquitto_strerror(rc)); } if (config.debug) printf("Device: %s - Timeout.\n", dev->id); bridge_remove_device(&bridge, dev->id); } } } } if (every30s) { every30s = false; if (connected) { send_alive(mosq); if (bandwidth) { md = bridge_get_module(bridge.bridge_dev, MODULES_BANDWIDTH_ID); if (md) { snprintf(gbuf, GBUF_SIZE, "%.0f,%.0f", upspeed, downspeed); mqtt_publish(mosq, md->topic, gbuf); if (config.debug > 2) printf("down: %f - up: %f\n", downspeed, upspeed); } } } else { if (config.debug) printf("MQTT Offline.\n"); } if (bridge.serial_alive) { bridge.serial_alive--; if (!bridge.serial_alive) { if (config.debug > 1) printf("Serial timeout.\n"); serial_hang(mosq); } } else { if (config.serial.port && !bridge.serial_ready) { if (config.debug > 1) printf("Trying to reconnect serial port.\n"); serialport_close(sd); sd = serialport_init(config.serial.port, config.serial.baudrate); if( sd == -1 ) { fprintf(stderr, "Couldn't open serial port.\n"); } else { serialport_flush(sd); bridge.serial_ready = true; snprintf(gbuf, GBUF_SIZE, "%d", MODULES_SERIAL_OPEN); mqtt_publish(mosq, md->topic, gbuf); if (config.debug) printf("Serial reopened.\n"); } } } } } if (bridge.serial_ready) { serialport_close(sd); } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); config_cleanup(&config); printf("Exiting..\n\n"); return 0; }
int main(int argc, char *argv[]) { int sd = -1; char *conf_file = NULL; struct mosquitto *mosq; struct module *md; struct device *dev; int rc; int i; if (!quiet) printf("Version: %s\n", version); signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); signal(SIGUSR1, handle_signal); signal(SIGUSR2, handle_signal); signal(SIGALRM, each_sec); for (i=1; i<argc; i++) { if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")){ if(i==argc-1){ fprintf(stderr, "Error: -c argument given but no file specified.\n\n"); print_usage(argv[0]); return 1; }else{ conf_file = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--quiet")){ quiet = true; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); print_usage(argv[0]); return 1; } } if(!conf_file) { fprintf(stderr, "Error: No config file given.\n"); return 1; } memset(&config, 0, sizeof(struct bridge_config)); if(config_parse(conf_file, &config)) return 1; if (quiet) config.debug = 0; if (config.debug != 0) printf("Debug: %d\n", config.debug); if (!device_isValid_id(config.id)) { fprintf(stderr, "Invalid id.\n"); return -1; } if (device_init(&bridge, config.id) == -1) return 1; mosquitto_lib_init(); mosq = mosquitto_new(config.id, true, NULL); if(!mosq){ fprintf(stderr, "Error creating mqtt instance.\n"); switch(errno){ case ENOMEM: fprintf(stderr, " out of memory.\n"); break; case EINVAL: fprintf(stderr, " invalid id.\n"); break; } return 1; } snprintf(gbuf, GBUF_SIZE, "%d", PROTO_ST_TIMEOUT); mosquitto_will_set(mosq, bridge.status_topic, strlen(gbuf), gbuf, config.mqtt_qos, MQTT_RETAIN); mosquitto_connect_callback_set(mosq, on_mqtt_connect); mosquitto_disconnect_callback_set(mosq, on_mqtt_disconnect); mosquitto_message_callback_set(mosq, on_mqtt_message); mosquitto_user_data_set(mosq, &sd); if (config.debug > 1) printf("Subscribe topic: %s\n", bridge.config_topic); rc = device_add_module(&bridge, MODULE_MQTT_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add mqtt module.\n"); return 1; } if (config.scripts_folder) { if (access(config.scripts_folder, R_OK )) { fprintf(stderr, "Couldn't open scripts folder: %s\n", config.scripts_folder); return 1; } rc = device_add_module(&bridge, MODULE_SCRIPT_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add script module.\n"); return 1; } } if (config.interface) { //TODO: check if interface exists if (access("/proc/net/dev", R_OK )) { fprintf(stderr, "Couldn't open /proc/net/dev\n"); return 1; } rc = device_add_module(&bridge, MODULE_BANDWIDTH_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add bandwidth module.\n"); return 1; } bandwidth = true; } if (config.serial.port) { sd = serialport_init(config.serial.port, config.serial.baudrate); if( sd == -1 ) { fprintf(stderr, "Couldn't open serial port.\n"); return 1; } else { rc = device_add_module(&bridge, MODULE_SERIAL_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add serial module.\n"); return 1; } serialport_flush(sd); bridge.serial_ready = true; if (config.debug) printf("Serial ready.\n"); } } rc = device_add_module(&bridge, MODULE_SIGUSR1_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add sigusr1 module.\n"); return 1; } rc = device_add_module(&bridge, MODULE_SIGUSR2_ID, bridge.id); //TODO: autogen id? if (rc) { fprintf(stderr, "Failed to add sigusr2 module.\n"); return 1; } device_print_modules(&bridge); rc = mosquitto_connect(mosq, config.mqtt_host, config.mqtt_port, 60); if (rc) { fprintf(stderr, "Wrong MQTT parameters. Check your config.\n"); return -1; } alarm(1); while (run) { if (bridge.serial_ready) { rc = serial_in(sd, mosq, MODULE_SERIAL_ID); if (rc == -1) { serial_hang(mosq); } else if (rc > 0) { bridge.serial_alive = ALIVE_CNT; } } if (user_signal) { if (config.debug > 1) printf("Signal - SIGUSR: %d\n", user_signal); signal_usr(sd, mosq); } rc = mosquitto_loop(mosq, -1, 1); if (run && rc) { if (config.debug > 2) printf("MQTT loop: %s\n", mosquitto_strerror(rc)); usleep(100000); // wait 100 msec mosquitto_reconnect(mosq); } if (every30s) { every30s = false; bridge.controller = false; for (i = 0; i < bridge.devices_len; i++) { dev = &bridge.devices[i]; if (dev->alive) { dev->alive--; if (!dev->alive) { snprintf(gbuf, GBUF_SIZE, "%d,%s", PROTO_ST_TIMEOUT, dev->id); mqtt_publish(mosq, bridge.status_topic, gbuf); if (dev->md_deps->type == MODULE_MQTT && dev->type == DEVICE_TYPE_NODE) { snprintf(gbuf, GBUF_SIZE, "status/%s", dev->id); rc = mosquitto_unsubscribe(mosq, NULL, gbuf); if (rc) fprintf(stderr, "Error: MQTT unsubscribe returned: %s\n", mosquitto_strerror(rc)); } if (config.debug) printf("Device timeout - id: %s\n", dev->id); } else { if (dev->type == DEVICE_TYPE_CONTROLLER) bridge.controller = true; } } } if (!bridge.controller) bridge.modules_update = false; if (connected) { snprintf(gbuf, GBUF_SIZE, "%d,%d", PROTO_ST_ALIVE, bridge.modules_len); mqtt_publish(mosq, bridge.status_topic, gbuf); if (bridge.modules_update) { snprintf(gbuf, GBUF_SIZE, "%d", PROTO_ST_MODULES_UP); if (mqtt_publish(mosq, bridge.status_topic, gbuf)) bridge.modules_update = false; } if (bandwidth) { md = device_get_module(&bridge, MODULE_BANDWIDTH_ID); if (md) { if (mqtt_publish_bandwidth(mosq, md->topic) == -1) break; } } } else { if (config.debug != 0) printf("MQTT Offline.\n"); } if (bridge.serial_alive) { bridge.serial_alive--; if (!bridge.serial_alive) { if (config.debug > 1) printf("Serial timeout.\n"); serial_hang(mosq); } } else { if (config.serial.port && !bridge.serial_ready) { if (config.debug > 1) printf("Trying to reconnect serial port.\n"); serialport_close(sd); sd = serialport_init(config.serial.port, config.serial.baudrate); if( sd == -1 ) fprintf(stderr, "Couldn't open serial port.\n"); else { serialport_flush(sd); bridge.serial_ready = true; if (config.debug) printf("Serial reopened.\n"); } } } } usleep(20); } if (bridge.serial_ready) { serialport_close(sd); } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); config_cleanup(&config); printf("Exiting..\n\n"); return 0; }
int mosquittopp::will_set(bool will, const char *topic, uint32_t payloadlen, const uint8_t *payload, int qos, bool retain) { return mosquitto_will_set(mosq, will, topic, payloadlen, payload, qos, retain); }
/* * ########################## * Main Function * ########################## */ int main(int argc, char *argv[]) { /* program setup variable */ struct mosquitto *mosq = NULL; struct mqtt_userdata *ud = NULL; bool clean_session = true; bool debug = false; char buf[MQTT_BUFSIZE]; char err[MQTT_ERR_BUFSIZE]; /* client id */ char id[MQTT_ID_LEN]; char id_prefix[MQTT_ID_LEN]; char hostname[MQTT_HOSTNAME_BUFSIZE]; /* broker variable */ char host[MQTT_IP_LEN] = "127.0.0.1"; int port = 1883; int keepalive = 3600; /* will information */ char *will_topic = NULL; long will_payloadlen = 0; char *will_payload = NULL; int will_qos = 0; bool will_retain = false; /* temp variable */ int i; int rc; /* initialized program and user data structure */ ud = malloc(sizeof(struct mqtt_userdata)); memset(ud, 0, sizeof(struct mqtt_userdata)); memset(id, '\0', sizeof(id)); memset(id_prefix, '\0', sizeof(id_prefix)); ud->topic_qos = 2; /* get option */ for(i=1; i<argc; i++){ if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i==argc-1){ fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); mqtt_print_usage(); return 1; }else{ port = atoi(argv[i+1]); if(port<1 || port>65535){ fprintf(stderr, "Error: Invalid port given: %d\n", port); mqtt_print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--disable-clean-session")){ clean_session = false; }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){ debug = true; }else if(!strcmp(argv[i], "--help")){ mqtt_print_usage(); return 0; }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){ if(i==argc-1){ fprintf(stderr, "Error: -h argument given but no host specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MQTT_IP_LEN) { fprintf(stderr, "Error: max length of ip is %d.\n\n", MQTT_IP_LEN); mqtt_print_usage(); } else { memset(host, '\0', sizeof(host)); strcpy(host, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){ if(strlen(id_prefix) != 0){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); mqtt_print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -i argument given but no id specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MOSQ_MQTT_ID_MAX_LENGTH) { fprintf(stderr, "Error: max length of client id is %d.\n\n", MOSQ_MQTT_ID_MAX_LENGTH); mqtt_print_usage(); } else { strcpy(id, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){ if(strlen(id) != 0){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); mqtt_print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n"); mqtt_print_usage(); return 1; }else{ if (strlen(argv[i+1]) >= MOSQ_MQTT_ID_MAX_LENGTH) { fprintf(stderr, "Error: max length of client id is %d.\n\n", MOSQ_MQTT_ID_MAX_LENGTH); mqtt_print_usage(); } else { strcpy(id_prefix, argv[i+1]); } } i++; }else if(!strcmp(argv[i], "-k") || !strcmp(argv[i], "--keepalive")){ if(i==argc-1){ fprintf(stderr, "Error: -k argument given but no keepalive specified.\n\n"); mqtt_print_usage(); return 1; }else{ keepalive = atoi(argv[i+1]); if(keepalive>65535){ fprintf(stderr, "Error: Invalid keepalive given: %d\n", keepalive); mqtt_print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){ if(i==argc-1){ fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->topic_qos = atoi(argv[i+1]); if(ud->topic_qos<0 || ud->topic_qos>2){ fprintf(stderr, "Error: Invalid QoS given: %d\n", ud->topic_qos); mqtt_print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--quiet")){ ud->quiet = true; }else if(!strcmp(argv[i], "-R")){ ud->no_retain = true; }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->topic_count++; ud->topics = realloc(ud->topics, ud->topic_count*sizeof(char *)); ud->topics[ud->topic_count-1] = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){ if(i==argc-1){ fprintf(stderr, "Error: -u argument given but no username specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->username = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")){ ud->verbose = 1; }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); mqtt_print_usage(); return 1; }else{ ud->password = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_payload = argv[i+1]; will_payloadlen = strlen(will_payload); } i++; }else if(!strcmp(argv[i], "--will-qos")){ if(i==argc-1){ fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_qos = atoi(argv[i+1]); if(will_qos < 0 || will_qos > 2){ fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos); return 1; } } i++; }else if(!strcmp(argv[i], "--will-retain")){ will_retain = true; }else if(!strcmp(argv[i], "--will-topic")){ if(i==argc-1){ fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n"); mqtt_print_usage(); return 1; }else{ will_topic = argv[i+1]; } i++; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); mqtt_print_usage(); return 1; } } /* verify necessary variable */ if(clean_session == false && ((strlen(id_prefix) != 0) || (strlen(id) == 0))){ if(!ud->quiet) fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n"); return 1; } if(ud->topic_count == 0){ fprintf(stderr, "Error: You must specify a topic to subscribe to.\n"); mqtt_print_usage(); return 1; } if(will_payload && !will_topic){ fprintf(stderr, "Error: Will payload given, but no will topic given.\n"); mqtt_print_usage(); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "Error: Will retain given, but no will topic given.\n"); mqtt_print_usage(); return 1; } if(ud->password && !ud->username){ if(!ud->quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); } /* setup signal handler */ signal(SIGINT, mqtt_signal_handler); signal(SIGTERM, mqtt_signal_handler); /* init mosquitto library */ mosquitto_lib_init(); /* setup client id */ if(strlen(id_prefix) != 0){ snprintf(id, sizeof(id), "%s%d", id_prefix, getpid()); }else if(strlen(id) == 0){ memset(hostname, '\0', sizeof(hostname)); gethostname(hostname, sizeof(hostname)); snprintf(id, sizeof(id), "mosqsub/%d-%s", getpid(), hostname); } if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } /* start mosquitto */ mosq = mosquitto_new(id, clean_session, ud); if(!mosq){ if(!ud->quiet) fprintf(stderr, "Error: %s\n", strerror(errno)); mosquitto_lib_cleanup(); return 1; } /* setup mosquitto */ if(debug){ mosquitto_log_callback_set(mosq, mqtt_log_callback); } if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ if(!ud->quiet) fprintf(stderr, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } if(ud->username && mosquitto_username_pw_set(mosq, ud->username, ud->password)){ if(!ud->quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_connect_callback_set(mosq, mqtt_connect_callback); mosquitto_message_callback_set(mosq, mqtt_message_callback); if(debug){ mosquitto_subscribe_callback_set(mosq, mqtt_subscribe_callback); } /* connect mosquitto */ rc = mosquitto_connect(mosq, host, port, keepalive); if(rc){ if(!ud->quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 strerror_r(errno, err, sizeof(err)); #else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, sizeof(err), NULL); #endif fprintf(stderr, "Error: %s\n", err); }else{ fprintf(stderr, "Unable to connect (%d: %s).\n", rc, mosquitto_strerror(rc)); } } mosquitto_lib_cleanup(); return rc; } mosquitto_loop_start(mosq); /* * loop mosquitto, * it use select() to call back the callback-function which defined before. */ do{ if (fgets(buf, sizeof(buf), stdin)) { buf[strlen(buf)-1] = '\0'; rc = mosquitto_publish(mosq, &ud->mid_sent, "simon", strlen(buf), buf, 0, 0); if (rc) { if (!ud->quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc); mosquitto_disconnect(mosq); } } } while (rc == MOSQ_ERR_SUCCESS); mosquitto_loop_stop(mosq, false); /* free mosquitto */ mosquitto_destroy(mosq); mosquitto_lib_cleanup(); mqtt_userdata_free(ud); return 0; }