Exemple #1
0
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
	struct mosq_config *cfg;
	int i;
	bool res;

	if(process_messages == false) return;

	assert(obj);
	cfg = (struct mosq_config *)obj;

	if(cfg->retained_only && !message->retain && process_messages){
		process_messages = false;
		mosquitto_disconnect(mosq);
		return;
	}

	if(message->retain && cfg->no_retain) return;
	if(cfg->filter_outs){
		for(i=0; i<cfg->filter_out_count; i++){
			mosquitto_topic_matches_sub(cfg->filter_outs[i], message->topic, &res);
			if(res) return;
		}
	}

	print_message(cfg, message);

	if(cfg->msg_count>0){
		msg_count++;
		if(cfg->msg_count == msg_count){
			process_messages = false;
			mosquitto_disconnect(mosq);
		}
	}
}
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
{
	if(mode == MSGMODE_STDIN_LINE){
		if(mid == last_mid){
			mosquitto_disconnect(mosq);
			disconnect_sent = true;
		}
	}else if(disconnect_sent == false){
		mosquitto_disconnect(mosq);
		disconnect_sent = true;
	}
}
Exemple #3
0
void MqttHal_Publish_Callback(struct mosquitto *mosq, void *obj, int mid)
{
#if 0
	last_mid_sent = mid;
	if(mode == MSGMODE_STDIN_LINE){
		if(mid == last_mid){
			mosquitto_disconnect(mosq);
			disconnect_sent = true;
		}
	}else if(disconnect_sent == false){
		mosquitto_disconnect(mosq);
		disconnect_sent = true;
	}
#endif
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
{
	if(msg->mid != 56){
		printf("Invalid mid (%d)\n", msg->mid);
		exit(1);
	}
	if(msg->qos != 2){
		printf("Invalid qos (%d)\n", msg->qos);
		exit(1);
	}
	if(strcmp(msg->topic, "len/qos2/test")){
		printf("Invalid topic (%s)\n", msg->topic);
		exit(1);
	}
	if(strcmp(msg->payload, "message")){
		printf("Invalid payload (%s)\n", (char *)msg->payload);
		exit(1);
	}
	if(msg->payloadlen != 7){
		printf("Invalid payloadlen (%d)\n", msg->payloadlen);
		exit(1);
	}
	if(msg->retain != false){
		printf("Invalid retain (%d)\n", msg->retain);
		exit(1);
	}

	mosquitto_disconnect(mosq);
}
Exemple #5
0
void my_signal_handler(int signum)
{
	if(signum == SIGALRM){
		process_messages = false;
		mosquitto_disconnect(mosq);
	}
}
static int ctx_disconnect(lua_State *L)
{
	ctx_t *ctx = ctx_check(L, 1);

	int rc = mosquitto_disconnect(ctx->mosq);
	return mosq__pstatus(L, rc);
}
Exemple #7
0
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;
}
void on_publish(struct mosquitto *mosq, void *obj, int mid, int reason_code, const mosquitto_property *properties)
{
	if(mid == 6){
		mosquitto_disconnect(mosq);
		run = 0;
	}
}
Exemple #9
0
/* Handle a message that just arrived via one of the subscriptions. */
static void on_message(struct mosquitto *m, void *udata,
                       const struct mosquitto_message *msg) {
    if (msg == NULL) { return; }
    LOG("-- got message @ %s: (%d, QoS %d, %s) '%s'\n",
        msg->topic, msg->payloadlen, msg->qos, msg->retain ? "R" : "!r",
        msg->payload);

    struct client_info *info = (struct client_info *)udata;

#if 0
    if (match(msg->topic, "tick")) {
        if (0 == strncmp(msg->payload, "tick", msg->payloadlen)) {
        } else {
            LOG("invalid 'tick' message\n");
        }
    } else if (match(msg->topic, "control/")) {
        /* This will cover both "control/all" and "control/$(PID)".
         * We won'st see "control/$(OTHER_PID)" because we won't be
         * subscribed to them.*/
        if (0 == strncmp(msg->payload, "halt", msg->payloadlen)) {
            LOG("*** halt\n");
            (void)mosquitto_disconnect(m);
        }
    }
#endif
}
void on_connect(struct mosquitto *mosq, void *obj, int rc)
{
	if(rc){
		exit(1);
	}else{
		mosquitto_disconnect(mosq);
	}
}
void mqtt_publish_callback(struct mosquitto *mosq, void *obj, int mid)
{
	struct mqtt_userdata *ud = NULL;

	assert(obj);
	ud = (struct mqtt_userdata *)obj;

	if(ud->mode == MQTT_MSGMODE_STDIN_LINE){
		if(mid == ud->last_mid){
			mosquitto_disconnect(mosq);
			ud->disconnect_sent = true;
		}
	}else if(ud->disconnect_sent == false){
		mosquitto_disconnect(mosq);
		ud->disconnect_sent = true;
	}
}
/* You can only call this if you _setup() succeeded all the way!. */
void mosq_cleanup(struct _squash *st)
{
	event_free(st->mosq_write);
	event_free(st->mosq_readidle);
	mosquitto_disconnect(st->mosq);
	mosquitto_destroy(st->mosq);
	mosquitto_lib_cleanup();
}
Exemple #13
0
void emit_close()
{
	mosquitto_disconnect(config.mosq);
	mosquitto_loop_stop(config.mosq, false);

	mosquitto_destroy(config.mosq);
	mosquitto_lib_cleanup();
}
Exemple #14
0
void fatal(void)
{
	if (m) {
		mosquitto_disconnect(m);
		mosquitto_loop_stop(m, false);
		mosquitto_lib_cleanup();
	}
	exit(1);
}
Exemple #15
0
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
{
	message_count++;
	//printf("%d ", message_count);
	if(message_count == MESSAGE_COUNT){
		gettimeofday(&stop, NULL);
		mosquitto_disconnect((struct mosquitto *)obj);
	}
}
Exemple #16
0
static
int
mosq_disconnect(lua_State *L)
{
	mosq_t *ctx = mosq_get(L, 1);

	int rc = mosquitto_disconnect(ctx->mosq);
	return make_mosq_status_result(L, rc);
}
void on_publish(struct mosquitto *mosq, void *obj, int mid)
{
	if(mid == sent_mid){
		mosquitto_disconnect(mosq);
		run = 0;
	}else{
		exit(1);
	}
}
Exemple #18
0
void my_publish_callback(struct mosquitto *mosq, void *userdata, int usernumber)
{
	/* We've published so lets countdown the messages and exit nicely */
	printf("Published message %d \n",messagesToSend);
	messagesToSend--;
	printf("Messages left to send %d \n",messagesToSend);
	if(messagesToSend==0) {
		mosquitto_disconnect(mosq);
	}
}
Exemple #19
0
void catcher(int sig)
{
	fprintf(stderr, "Going down on signal %d\n", sig);

	if (m) {
		mosquitto_disconnect(m);
		mosquitto_loop_stop(m, false);
		mosquitto_lib_cleanup();
	}
	exit(1);
}
Exemple #20
0
static int mqtt_close(void* userdata) {
    mqtt_t inst = (mqtt_t) userdata;
    int rval;

    info("Stopping MQTT service");

    rval = mosquitto_disconnect(inst->mosq);
    if (rval != MOSQ_ERR_SUCCESS) {
        error("Cannot disconnect mosquitto: %s", mosquitto_strerror(rval));
    }

    return rval;
}
/*
 * ##########################
 * Callback Functions
 * ##########################
 */
void mqtt_connect_callback(struct mosquitto *mosq, void *obj, int result)
{
	int rc = MOSQ_ERR_SUCCESS;
	struct mqtt_userdata *ud = NULL;

	assert(obj);
	ud = (struct mqtt_userdata *)obj;

	if(!result){
		switch(ud->mode){
			case MQTT_MSGMODE_CMD:
			case MQTT_MSGMODE_FILE:
			case MQTT_MSGMODE_STDIN_FILE:
				rc = mosquitto_publish(mosq, &ud->mid_sent, ud->topic, ud->msglen, ud->message, ud->qos, ud->retain);
				break;
			case MQTT_MSGMODE_NULL:
				rc = mosquitto_publish(mosq, &ud->mid_sent, ud->topic, 0, NULL, ud->qos, ud->retain);
				break;
			case MQTT_MSGMODE_STDIN_LINE:
				ud->status = MQTT_STATUS_CONNACK_RECVD;
				break;
		}
		if(rc){
			if(!ud->quiet){
				switch(rc){
					case MOSQ_ERR_INVAL:
						fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n");
						break;
					case MOSQ_ERR_NOMEM:
						fprintf(stderr, "Error: Out of memory when trying to publish message.\n");
						break;
					case MOSQ_ERR_NO_CONN:
						fprintf(stderr, "Error: Client not connected when trying to publish.\n");
						break;
					case MOSQ_ERR_PROTOCOL:
						fprintf(stderr, "Error: Protocol error when communicating with broker.\n");
						break;
					case MOSQ_ERR_PAYLOAD_SIZE:
						fprintf(stderr, "Error: Message payload is too large.\n");
						break;
				}
			}
			mosquitto_disconnect(mosq);
		}
	}else{
		if(result && !ud->quiet){
			fprintf(stderr, "%s\n", mosquitto_connack_string(result));
		}
	}
}
Exemple #22
0
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
	struct mosq_config *cfg;
	int i;
	bool res;

	if(process_messages == false) return;

	assert(obj);
	cfg = (struct mosq_config *)obj;

	if(message->retain && cfg->no_retain) return;
	if(cfg->filter_outs){
		for(i=0; i<cfg->filter_out_count; i++){
			mosquitto_topic_matches_sub(cfg->filter_outs[i], message->topic, &res);
			if(res) return;
		}
	}

	if(cfg->verbose){
		if(message->payloadlen){
			printf("%s ", message->topic);
			fwrite(message->payload, 1, message->payloadlen, stdout);
			if(cfg->eol){
				printf("\n");
			}
		}else{
			if(cfg->eol){
				printf("%s (null)\n", message->topic);
			}
		}
		fflush(stdout);
	}else{
		if(message->payloadlen){
			fwrite(message->payload, 1, message->payloadlen, stdout);
			if(cfg->eol){
				printf("\n");
			}
			fflush(stdout);
		}
	}
	if(cfg->msg_count>0){
		msg_count++;
		if(cfg->msg_count == msg_count){
			process_messages = false;
			mosquitto_disconnect(mosq);
		}
	}
}
Exemple #23
0
/**
 * Handles the signal from ctrl+C. Simple close the connection and close the file.
 */
void sig_handler(int sig) {
  if( SIGINT != sig ) {
    CRIT("Got unexpected signal.");
  }

  mosquitto_disconnect(config.mosq);

  mosquitto_destroy(config.mosq);

  mosquitto_lib_cleanup();

  fclose(config.fd);

  exit(0);
}
Exemple #24
0
static void mqtt_free(mqtt_client_conf_t *conf) {
    if (conf == NULL)
        return;

    if (conf->connected)
        (void)mosquitto_disconnect(conf->mosq);
    conf->connected = 0;
    (void)mosquitto_destroy(conf->mosq);

    sfree(conf->host);
    sfree(conf->username);
    sfree(conf->password);
    sfree(conf->client_id);
    sfree(conf->topic_prefix);
    sfree(conf);
}
/* {{{ Mosquitto\Client::disconnect() */
PHP_METHOD(Mosquitto_Client, disconnect)
{
	mosquitto_client_object *object;
	int retval;

	PHP_MOSQUITTO_ERROR_HANDLING();
	if (zend_parse_parameters_none() == FAILURE) {
		PHP_MOSQUITTO_RESTORE_ERRORS();
		return;
	}
	PHP_MOSQUITTO_RESTORE_ERRORS();

	object = (mosquitto_client_object *) mosquitto_client_object_get(getThis() TSRMLS_CC);

	retval = mosquitto_disconnect(object->client);
	php_mosquitto_exit_loop(object);

	php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
Exemple #26
0
void ZnpActorProcess(PACTOROPTION option)
{
	mosquitto_lib_init();
	ZnpActorCreat(option->guid, option->psw, option->host, option->port);
	if (pZnpActor == NULL)
	{
		mosquitto_lib_cleanup();
		return;
	}
	while(1)
	{
		ActorProcessEvent(pZnpActor);
		mosquitto_loop(pZnpActor->client, 0, 1);
		usleep(10000);
	}
	mosquitto_disconnect(pZnpActor->client);
	mosquitto_destroy(pZnpActor->client);
	mosquitto_lib_cleanup();
}
Exemple #27
0
static void LedActorStart(PACTOROPTION option)
{
	mosquitto_lib_init();
	LedActorCreate(option->guid, option->psw, option->host, option->port);
	if (pLedActor == NULL)
	{
		mosquitto_lib_cleanup();
		return;
	}
	while(1)
	{
		ActorProcessEvent(pLedActor);
		LedTransition();
		mosquitto_loop(pLedActor->client, 0, 1);
		usleep(10000);
	}
	mosquitto_disconnect(pLedActor->client);
	mosquitto_destroy(pLedActor->client);
	mosquitto_lib_cleanup();
}
Exemple #28
0
void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags)
{
	int i;
	struct mosq_config *cfg;

	assert(obj);
	cfg = (struct mosq_config *)obj;

	if(!result){
		for(i=0; i<cfg->topic_count; i++){
			mosquitto_subscribe(mosq, NULL, cfg->topics[i], cfg->qos);
		}
		for(i=0; i<cfg->unsub_topic_count; i++){
			mosquitto_unsubscribe(mosq, NULL, cfg->unsub_topics[i]);
		}
	}else{
		if(result && !cfg->quiet){
			fprintf(stderr, "%s\n", mosquitto_connack_string(result));
		}
		mosquitto_disconnect(mosq);
	}
}
Exemple #29
0
int mosquittopp::disconnect()
{
	return mosquitto_disconnect(m_mosq);
}
Exemple #30
0
/**
 * Main!
 */
int main(int argc, char **argv) {
  struct sigaction sigact;
  char buf[BUF_SIZE];
  int qos, retain, len;
  struct timeval recv_time, now;
  char topic[CONF_MAX_LENGTH_MQTT_TOPIC];
  int i, ret;
  struct mqtt_player_status_msg status;

  if( config_init() ) {
    CRIT("Faild to initialize config.");
  }
  
  parse_args(argc, argv);

  if( !strlen(config.log_file) ) {
    fprintf(stderr, "ERROR: You have to provide a logfile.\n");
    print_usage(*argv);
    exit(1);
  }

  config.fd = fopen(config.log_file, "r");
  if( NULL == config.fd ) {
    CRIT("Could not open log file.");
  }

  memset(&sigact, 0, sizeof(struct sigaction));
  sigact.sa_handler = sig_handler;
  if( sigaction(SIGINT, &sigact, NULL) ) {
    CRIT("Could not initialize signal handler.");
  }
  
  mosquitto_lib_init();

  config.mosq = mosquitto_new(config.mqtt_client_id, config.mqtt_clean_session, NULL);
  if( NULL == config.mosq ) {
    CRIT("Could not create a mosquitto object.");
  }

  if( mosquitto_connect(config.mosq, config.mqtt_broker, config.mqtt_port, config.mqtt_keepalive) ) {
    CRIT("Could not connect MQTT broker.");
  }

  mosquitto_loop_start(config.mosq);

  do {

    if( gettimeofday(&config.start, NULL) ) {
      CRIT("Could not get time().");
    }

    if( 0 != fseek(config.fd, SEEK_SET, 0) ) {
      CRIT("lseek faild.");
    }

    // read file config
    while(1) {
      if( fscanf(config.fd, "cnf time: %ld.%ld\n", &config.record_start_time.tv_sec, &config.record_start_time.tv_usec) ) {
        if( config.verbose ) {
          printf("record time: %3ld", config.record_start_time.tv_sec);
          printf(".%06ld\n", config.record_start_time.tv_usec);
	}
      } else {
        break;
      }
    }

    if( config.verbose ) {
      printf("-- start playing --\n");
    }

    status.status = MQTT_PLAYER_BEGIN_PLAY;
    status.sec = hton64(config.record_start_time.tv_sec);
    status.usec = hton64(config.record_start_time.tv_usec);
    // post status
    mosquitto_publish(config.mosq, NULL, config.mqtt_topic, sizeof(struct mqtt_player_status_msg), &status, 2, 0);

    // read data
    while(1) {
      if( feof(config.fd) ) {
        break;
      }
  
      ret = fscanf(config.fd, "msg %ld.%ld %d %d %d %s\n", &recv_time.tv_sec, &recv_time.tv_usec, &qos, &retain, &len, topic);
      if( 0 == ret ) {
        break;
      }
  
      if( config.verbose ) {
        printf("time: %3ld", recv_time.tv_sec);
        printf(".%06ld ", recv_time.tv_usec);
        printf("qos: %d ", qos);
        printf("retain: %d ", retain);
        printf("len: %d ", len);
        printf("topic: %s\n", topic);
      }
  
      if( 0 > qos && 2 < qos ) {
        CRIT("Format error in '%s'.", config.log_file);
      }
  
      if( 0 > retain && 1 < retain ) {
        CRIT("Format error in '%s'.", config.log_file);
      }
  
      if( 0 < len ) {
        ret == fscanf(config.fd, "%02hhx", &buf[0]);
        if( 0 == ret ) {
          break;
        }
      }
  
      for( i = 1; i < len; i++ ) {
        ret == fscanf(config.fd, " %02hhx", &buf[i]);
        if( 0 == ret ) {
          break;
        }
      }
      ret == fscanf(config.fd, "\n");
      if( 0 == ret ) {
        break;
      }
  
      if( !config.ignore_timing ) {
        timeradd(&recv_time, &config.start, &recv_time);
  
        if( gettimeofday(&now, NULL) ) {
          CRIT("Could not get time.");
        }
  
        if( timercmp(&now, &recv_time, <) ) {
          timersub(&recv_time, &now, &recv_time);
          usleep(recv_time.tv_sec * 1000000 + recv_time.tv_usec);
        }
      }
  
      mosquitto_publish(config.mosq, NULL, topic, len, buf, qos, retain);
    }
    
  }while( config.repeat && feof(config.fd) );

  mosquitto_disconnect(config.mosq);
 
  mosquitto_loop_stop(config.mosq, false);

  mosquitto_destroy(config.mosq);

  mosquitto_lib_cleanup();

  fclose(config.fd);

  return 0;
}