This is fairly incomplete, please see mosquitto.h for a better description of the functions.
Obtain version information about the library. If any of major, minor or revision are not NULL they will return the corresponding version numbers. The return value is an integer representation of the complete version number (e.g. 9000 for 0.9) that can be used for comparisons.
Configure the logging settings for this client. Returns 0 on success, 1 on error.
Set priorities by ORing any of the items in the following list:
Set destinations by ORing any of the items in the following list:
#include <mosquitto.h>
void my_message_callback(void *obj, struct mosquitto_message *message)
{
if(message->payloadlen){
printf("%s %s\n", message->topic, message->payload);
}else{
printf("%s (null)\n", message->topic);
}
fflush(stdout);
}
void my_connect_callback(void *obj, int result)
{
struct mosquitto *mosq = obj;
int i;
if(!result){
mosquitto_subscribe(mosq, topics[i], topic_qos);
}else{
fprintf(stderr, "Connect failed\n");
}
}
void my_subscribe_callback(void *obj, uint16_t mid, int qos_count, const uint8_t *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i<qos_count; i++){
printf(", %d", granted_qos[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
char id[30];
int i;
char *host = "localhost";
int port = 1883;
int keepalive = 60;
bool clean_session = true;
struct mosquitto *mosq = NULL;
mosq = mosquitto_new(id, NULL);
if(!mosq){
fprintf(stderr, "Error: Out of memory.\n");
return 1;
}
mosquitto_log_init(mosq, MOSQ_LOG_DEBUG | MOSQ_LOG_ERR | MOSQ_LOG_WARNING
| MOSQ_LOG_NOTICE | MOSQ_LOG_INFO, MOSQ_LOG_STDERR);
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 1;
}
while(!mosquitto_loop(mosq, -1)){
}
mosquitto_destroy(mosq);
return 0;
}