align with custom mqtt client
authorYoungjae Shin <yj99.shin@samsung.com>
Sun, 28 Aug 2022 22:55:25 +0000 (07:55 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 15 Sep 2022 05:36:31 +0000 (14:36 +0900)
 - add AITT_CONNET_FAILED
 - allow non IP string for connecting broker

common/AittUtil.h [deleted file]
common/MosquittoMQ.cc
common/MosquittoMQ.h
include/AittTypes.h
include/AittUtil.h [new file with mode: 0644]
include/aitt_c.h
src/aitt_c.cc

diff --git a/common/AittUtil.h b/common/AittUtil.h
deleted file mode 100644 (file)
index 4859fd6..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd 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.
- */
-#pragma once
-
-#include <string>
-
-namespace aitt {
-
-class AittUtil {
-  public:
-    AittUtil() = default;
-    ~AittUtil() = default;
-
-    static bool CompareTopic(const std::string &left, const std::string &right);
-};
-
-}  // namespace aitt
index f10532eec196f93c5bd83625b89e961a672458a5..1eb0d175eb9ae321996cbebc4b3094f7b9010e45 100644 (file)
@@ -115,7 +115,7 @@ void MosquittoMQ::ConnectCallback(struct mosquitto *mosq, void *obj, int rc, int
 
     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,
@@ -189,7 +189,7 @@ void MosquittoMQ::MessageCallback(mosquitto *handle, void *obj, const mosquitto_
 
         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++;
@@ -202,8 +202,11 @@ void MosquittoMQ::MessageCallback(mosquitto *handle, void *obj, const mosquitto_
     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) {
@@ -248,8 +251,7 @@ void MosquittoMQ::InvokeCallback(const mosquitto_message *msg, const mosquitto_p
         }
     }
 
-    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,
index 6c9380974e5021da94d9310a51119a4b003e0b45..5a55cfc6e85e9bd845a309977da6e941b2cf3c63 100644 (file)
@@ -63,7 +63,8 @@ class MosquittoMQ : public MQ {
           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;
index 1770922ce2e8df545076ac3726109f348ffec23a..37cbf312f751919674a42d50b60da01538484225 100644 (file)
@@ -34,8 +34,9 @@ enum AittQoS {
 };
 
 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
diff --git a/include/AittUtil.h b/include/AittUtil.h
new file mode 100644 (file)
index 0000000..4859fd6
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd 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.
+ */
+#pragma once
+
+#include <string>
+
+namespace aitt {
+
+class AittUtil {
+  public:
+    AittUtil() = default;
+    ~AittUtil() = default;
+
+    static bool CompareTopic(const std::string &left, const std::string &right);
+};
+
+}  // namespace aitt
index 26d03662c4e666f0b3e06b78bf476a6e45fc33cd..043d370541b0ba3d97abde11d564c3a7896b0575 100644 (file)
@@ -188,15 +188,15 @@ int aitt_will_set(aitt_h handle, const char *topic, const void *msg, const size_
  * @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.
index dc33baf3933967a9a18e43b1c1b89e10ca6c5877..d2f68dbb205f8555aa27d260db97710b2f909079 100644 (file)
@@ -27,6 +27,7 @@ struct aitt_handle {
     aitt_handle() : aitt(nullptr) {}
     AITT *aitt;
     bool connected;
+    bool custom_broker;
 };
 
 struct aitt_option {
@@ -42,6 +43,7 @@ API aitt_h aitt_new(const char *id, aitt_option_h option)
         std::string valid_id;
         std::string valid_ip;
         AittOption aitt_option;
+        bool custom_broker = false;
 
         if (id)
             valid_id = id;
@@ -51,11 +53,13 @@ API aitt_h aitt_new(const char *id, aitt_option_h option)
                 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;
@@ -191,23 +195,25 @@ static bool is_valid_ip(const char *ip)
     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;
     }