Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
/src/samples/mqtt/mqtt-publish.c
/*
* This file is part of the Soletta (TM) Project
*
* Copyright (C) 2015 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <sol-log.h>
#include <sol-mainloop.h>
#include <sol-mqtt.h>
static struct sol_timeout *timeout;
static char *topic;
static struct sol_mqtt_message message;
static bool
publish_callback(void *data)
{
static int id = 0;
struct sol_mqtt *mqtt = data;
SOL_NULL_CHECK(mqtt, false);
SOL_INF("%d: Sending Message.", id++);
if (sol_mqtt_publish(mqtt, &message)) {
SOL_WRN("Unable to publish message");
return false;
}
return true;
}
static bool
try_reconnect(void *data)
{
return sol_mqtt_reconnect((struct sol_mqtt *)data) != 0;
}
static void
on_connect(void *data, struct sol_mqtt *mqtt)
{
SOL_WRN("Unable to connect, retrying...");
return;
}
if (!publish_callback(mqtt))
return;
if (!timeout)
SOL_WRN("Unable to setup callback");
}
static void
on_disconnect(void *data, struct sol_mqtt *mqtt)
{
SOL_INF("Reconnecting...");
}
int
main(int argc, char *argv[])
{
struct sol_mqtt *mqtt;
struct sol_buffer payload;
struct sol_mqtt_config config = {
.clean_session = true,
.keep_alive = 60,
.handlers = {
.connect = on_connect,
.disconnect = on_disconnect,
},
};
if (argc < 5) {
SOL_INF("Usage: %s <ip> <port> <topic> <message>", argv[0]);
return 0;
}
config.port = atoi(argv[2]);
config.host = argv[1];
topic = argv[3];
payload = SOL_BUFFER_INIT_CONST(argv[4], strlen(argv[4]));
.topic = topic,
.payload = &payload,
.retain = false,
};
mqtt = sol_mqtt_connect(&config);
if (!mqtt) {
SOL_WRN("Unable to create MQTT session");
return -1;
}
if (timeout)
return 0;
}