コード例 #1
0
ファイル: mqtt.c プロジェクト: dsmrd/dsmrd
static void on_connect(/*@unused@*/ struct mosquitto *mosq, void *obj, /*@unused@*/ int rc) {
    mqtt_t inst = (mqtt_t) obj;

    inst->connected = 1;

    info("MQTT connect: %s", mosquitto_connack_string(rc));
}
コード例 #2
0
ファイル: mqtt_bridge.c プロジェクト: darcyg/mqtt_bridge
void on_mqtt_connect(struct mosquitto *mosq, void *obj, int result)
{
	struct device *dev;
	int rc;

	if (!result) {
		connected = true;
		if(config.debug) printf("MQTT Connected.\n");

		rc = mosquitto_subscribe(mosq, NULL, bridge.bridge_dev->config_topic, config.mqtt_qos);
		if (rc) {
			fprintf(stderr, "MQTT - Subscribe ERROR: %s\n", mosquitto_strerror(rc));
			run = 0;
			return;
		}
		if (config.debug > 1) printf("Subscribe topic: %s\n", bridge.bridge_dev->config_topic);

		for (dev = bridge.dev_list; dev != NULL; dev = dev->next) {
			rc = mosquitto_subscribe(mosq, NULL, dev->config_topic, config.mqtt_qos);
			if (rc) {
				fprintf(stderr, "MQTT - Subscribe ERROR: %s\n", mosquitto_strerror(rc));
				run = 0;
				return;
			}
			if (config.debug > 1) printf("Subscribe topic: %s\n", bridge.bridge_dev->config_topic);
		}

		send_alive(mosq);
	} else {
		fprintf(stderr, "MQTT - Failed to connect: %s\n", mosquitto_connack_string(result));
    }
}
コード例 #3
0
ファイル: pub_client.c プロジェクト: eclipse/mosquitto
void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags, const mosquitto_property *properties)
{
	int rc = MOSQ_ERR_SUCCESS;

	UNUSED(obj);
	UNUSED(flags);
	UNUSED(properties);

	if(!result){
		switch(cfg.pub_mode){
			case MSGMODE_CMD:
			case MSGMODE_FILE:
			case MSGMODE_STDIN_FILE:
				rc = my_publish(mosq, &mid_sent, cfg.topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain);
				break;
			case MSGMODE_NULL:
				rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain);
				break;
			case MSGMODE_STDIN_LINE:
				status = STATUS_CONNACK_RECVD;
				break;
		}
		if(rc){
			if(!cfg.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;
					case MOSQ_ERR_QOS_NOT_SUPPORTED:
						fprintf(stderr, "Error: Message QoS not supported on broker, try a lower QoS.\n");
						break;
				}
			}
			mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
		}
	}else{
		if(result && !cfg.quiet){
			if(cfg.protocol_version == MQTT_PROTOCOL_V5){
				fprintf(stderr, "%s\n", mosquitto_reason_string(result));
			}else{
				fprintf(stderr, "%s\n", mosquitto_connack_string(result));
			}
		}
	}
}
コード例 #4
0
/*
 * ##########################
 * 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));
		}
	}
}
コード例 #5
0
ファイル: sub_client.c プロジェクト: SSSang2/mosquittoMaster
void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
{
	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);
		}
	}else{
		if(result && !cfg->quiet){
			fprintf(stderr, "%s\n", mosquitto_connack_string(result));
		}
	}
}
コード例 #6
0
void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
{
	int i;
	struct userdata *ud;

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

	if(!result){
		for(i=0; i<ud->topic_count; i++){
			mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos);
		}
	}else{
		if(result && !ud->quiet){
			fprintf(stderr, "%s\n", mosquitto_connack_string(result));
		}
	}
}
コード例 #7
0
void on_mqtt_connect(struct mosquitto *mosq, void *obj, int result)
{
	int rc;

	if (!result) {
		connected = true;
		if(config.debug != 0) printf("MQTT Connected.\n");

		rc = mosquitto_subscribe(mosq, NULL, bridge.config_topic, config.mqtt_qos);
		if (rc) {
			fprintf(stderr, "MQTT - Subscribe ERROR: %s\n", mosquitto_strerror(rc));
			run = 0;
			return;
		}
		snprintf(gbuf, GBUF_SIZE, "%d,%d", PROTO_ST_ALIVE, bridge.modules_len);
		mqtt_publish(mosq, bridge.status_topic, gbuf);
		return;
	} else {
		fprintf(stderr, "MQTT - Failed to connect: %s\n", mosquitto_connack_string(result));
    }
}
コード例 #8
0
ファイル: mosquittopp.cpp プロジェクト: PierreF/mosquitto
const char* connack_string(int connack_code)
{
	return mosquitto_connack_string(connack_code);
}