std::lock_guard<std::recursive_mutex> lock_from_here(mq->callback_lock);
if (mq->connect_cb)
- mq->connect_cb(AITT_CONNECTED);
+ mq->connect_cb((rc == CONNACK_ACCEPTED) ? AITT_CONNECTED : AITT_CONNECT_FAILED);
}
void MosquittoMQ::DisconnectCallback(struct mosquitto *mosq, void *obj, int rc,
bool result = AittUtil::CompareTopic(subscribe_data->topic.c_str(), msg->topic);
if (result)
- mq->InvokeCallback(msg, props);
+ mq->InvokeCallback(*mq->subscriber_iterator, msg, props);
if (!mq->subscriber_iterator_updated)
mq->subscriber_iterator++;
mq->new_subscribers.clear();
}
-void MosquittoMQ::InvokeCallback(const mosquitto_message *msg, const mosquitto_property *props)
+void MosquittoMQ::InvokeCallback(SubscribeData *subscriber, const mosquitto_message *msg,
+ const mosquitto_property *props)
{
+ RET_IF(nullptr == subscriber);
+
MSG mq_msg;
mq_msg.SetTopic(msg->topic);
if (props) {
}
}
- SubscribeData *cb_info = *subscriber_iterator;
- cb_info->cb(&mq_msg, msg->topic, msg->payload, msg->payloadlen, cb_info->user_data);
+ subscriber->cb(&mq_msg, msg->topic, msg->payload, msg->payloadlen, subscriber->user_data);
}
void MosquittoMQ::Publish(const std::string &topic, const void *data, const size_t datalen, int qos,
const mosquitto_property *props);
static void MessageCallback(mosquitto *, void *, const mosquitto_message *,
const mosquitto_property *);
- void InvokeCallback(const mosquitto_message *msg, const mosquitto_property *props);
+ void InvokeCallback(SubscribeData *subscriber, const mosquitto_message *msg,
+ const mosquitto_property *props);
static const std::string REPLY_SEQUENCE_NUM_KEY;
static const std::string REPLY_IS_END_SEQUENCE_KEY;
};
enum AittConnectionState {
- AITT_DISCONNECTED = 0, // The connection is disconnected.
- AITT_CONNECTED = 1, // A connection was successfully established to the mqtt broker.
+ AITT_DISCONNECTED = 0, // The connection is disconnected.
+ AITT_CONNECTED = 1, // A connection was successfully established to the mqtt broker.
+ AITT_CONNECT_FAILED = 2, // Failed to connect to the mqtt broker.
};
#ifdef TIZEN
* @brief Connect to mqtt broker.
* @privlevel public
* @param[in] handle Handle of AITT service
- * @param[in] broker_ip IP address of the broker to connect to
- * @param[in] port the network port to connect to. Usually 1883.
+ * @param[in] host Identifier of the broker usually it's IP address of the broker to connect to
+ * @param[in] port the network port to connect to. Usually 1883. Ignored if not required.
* @return @c 0 on success
* otherwise a negative error value
* @retval #AITT_ERROR_NONE Success
* @retval #AITT_ERROR_INVALID_PARAMETER Invalid parameter
* @retval #AITT_ERROR_SYSTEM System errors
*/
-int aitt_connect(aitt_h handle, const char *broker_ip, int port);
+int aitt_connect(aitt_h handle, const char *host, int port);
/**
* @brief Connect to mqtt broker as aitt_connect(), but takes username and password.
aitt_handle() : aitt(nullptr) {}
AITT *aitt;
bool connected;
+ bool custom_broker;
};
struct aitt_option {
std::string valid_id;
std::string valid_ip;
AittOption aitt_option;
+ bool custom_broker = false;
if (id)
valid_id = id;
valid_ip = option->my_ip;
aitt_option.SetClearSession(option->clear_session);
aitt_option.SetUseCustomMqttBroker(option->custom_broker);
+ custom_broker = option->custom_broker;
}
handle = new aitt_handle();
handle->aitt = new AITT(valid_id, valid_ip, aitt_option);
handle->connected = false;
+ handle->custom_broker = custom_broker;
} catch (std::exception &e) {
ERR("new() Fail(%s)", e.what());
return nullptr;
return true;
}
-API int aitt_connect(aitt_h handle, const char *broker_ip, int port)
+API int aitt_connect(aitt_h handle, const char *host, int port)
{
- return aitt_connect_full(handle, broker_ip, port, NULL, NULL);
+ return aitt_connect_full(handle, host, port, NULL, NULL);
}
-API int aitt_connect_full(aitt_h handle, const char *broker_ip, int port, const char *username,
+API int aitt_connect_full(aitt_h handle, const char *host, int port, const char *username,
const char *password)
{
RETV_IF(handle == nullptr, AITT_ERROR_INVALID_PARAMETER);
- RETVM_IF(is_valid_ip(broker_ip) == false, AITT_ERROR_INVALID_PARAMETER, "Invalid IP(%s)",
- broker_ip);
+ if (handle->custom_broker)
+ RETV_IF(host == nullptr, AITT_ERROR_INVALID_PARAMETER);
+ else
+ RETVM_IF(!is_valid_ip(host), AITT_ERROR_INVALID_PARAMETER, "Invalid IP(%s)", host);
try {
- handle->aitt->Connect(broker_ip, port, username ? username : std::string(),
+ handle->aitt->Connect(host, port, username ? username : std::string(),
password ? password : std::string());
} catch (std::exception &e) {
- ERR("Connect(%s, %d) Fail(%s)", broker_ip, port, e.what());
+ ERR("Connect(%s, %d) Fail(%s)", host, port, e.what());
return AITT_ERROR_SYSTEM;
}