Delete Client callback when receive observe cancel response.
[platform/upstream/iotivity.git] / resource / src / OCResource.cpp
index 12a9628..ba6df4b 100644 (file)
@@ -18,6 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+#include "iotivity_config.h"
 #include "OCResource.h"
 #include "OCUtilities.h"
 
@@ -125,11 +126,11 @@ void OCResource::setHost(const std::string& host)
 {
     size_t prefix_len;
 
-    if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
+    if (host.compare(0, sizeof(COAP) - 1, COAP) == 0)
     {
         prefix_len = sizeof(COAP) - 1;
     }
-    else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
+    else if (host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
     {
         prefix_len = sizeof(COAPS) - 1;
         m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags | OC_SECURE);
@@ -155,11 +156,11 @@ void OCResource::setHost(const std::string& host)
     // remove 'coap://' or 'coaps://' or 'coap+tcp://' or 'coap+gatt://' or 'coap+rfcomm://'
     std::string host_token = host.substr(prefix_len);
 
-    if(host_token[0] == '[') // IPv6
+    if (host_token[0] == '[') // IPv6
     {
         size_t bracket = host_token.find(']');
 
-        if(bracket == std::string::npos || bracket == 0)
+        if (std::string::npos == bracket || 0 == bracket)
         {
             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
@@ -168,9 +169,15 @@ void OCResource::setHost(const std::string& host)
         std::string ip6Addr = host_token.substr(1, bracket - 1);
 
         // address validity check
+        std::string ip6AddrToValidityCheck(ip6Addr);
+        size_t percent = ip6AddrToValidityCheck.find('%');
+        if (std::string::npos != percent)
+        {
+            ip6AddrToValidityCheck.resize(percent);
+        }
         struct in6_addr buf;
-        const char *cAddr = ip6Addr.c_str();
-        if(0 == inet_pton(AF_INET6, cAddr, &buf))
+        const char *cAddr = ip6AddrToValidityCheck.c_str();
+        if (0 == inet_pton(AF_INET6, cAddr, &buf))
         {
             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
@@ -251,7 +258,7 @@ void OCResource::setHost(const std::string& host)
             // address validity check
             struct in_addr buf;
             const char *cAddr = ip4Addr.c_str();
-            if(0 == inet_pton(AF_INET, cAddr, &buf))
+            if (0 == inet_pton(AF_INET, cAddr, &buf))
             {
                 throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
                     m_interfaces.empty(), m_clientWrapper.expired(), false, false);
@@ -280,7 +287,7 @@ OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
     return checked_guard(m_clientWrapper.lock(),
                             &IClientWrapper::GetResourceRepresentation,
                             m_devAddr, m_uri,
-                            queryParametersMap, m_headerOptions,
+                            queryParametersMap, m_headerOptions, CT_DEFAULT,
                             attributeHandler, QoS);
 }
 
@@ -307,12 +314,12 @@ OCStackResult OCResource::get(const std::string& resourceType, const std::string
 {
     QueryParamsMap mapCpy(queryParametersMap);
 
-    if(!resourceType.empty())
+    if (!resourceType.empty())
     {
         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
-    if(!resourceInterface.empty())
+    if (!resourceInterface.empty())
     {
         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
     }
@@ -357,12 +364,12 @@ OCStackResult OCResource::put(const std::string& resourceType,
 {
     QueryParamsMap mapCpy(queryParametersMap);
 
-    if(!resourceType.empty())
+    if (!resourceType.empty())
     {
         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
-    if(!resourceInterface.empty())
+    if (!resourceInterface.empty())
     {
         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
     }
@@ -407,12 +414,12 @@ OCStackResult OCResource::post(const std::string& resourceType,
 {
     QueryParamsMap mapCpy(queryParametersMap);
 
-    if(!resourceType.empty())
+    if (!resourceType.empty())
     {
         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
     }
 
-    if(!resourceInterface.empty())
+    if (!resourceInterface.empty())
     {
         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
     }
@@ -423,7 +430,7 @@ OCStackResult OCResource::post(const std::string& resourceType,
 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
 {
     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
-                         m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
+                         m_devAddr, m_uri, m_headerOptions, CT_DEFAULT, deleteHandler, QoS);
 }
 
 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
@@ -438,11 +445,6 @@ OCStackResult OCResource::observe(ObserveType observeType,
         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
         QualityOfService QoS)
 {
-    if(m_observeHandle != nullptr)
-    {
-        return result_guard(OC_STACK_INVALID_PARAM);
-    }
-
     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
                          observeType, &m_observeHandle, m_devAddr,
                          m_uri, queryParametersMap, m_headerOptions,
@@ -467,7 +469,7 @@ OCStackResult OCResource::cancelObserve()
 
 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
 {
-    if(m_observeHandle == nullptr)
+    if (m_observeHandle == nullptr)
     {
         return result_guard(OC_STACK_INVALID_PARAM);
     }
@@ -476,7 +478,7 @@ OCStackResult OCResource::cancelObserve(QualityOfService QoS)
             &IClientWrapper::CancelObserveResource,
             m_observeHandle, (const char*)"", m_uri, m_headerOptions, QoS);
 
-    if(result == OC_STACK_OK)
+    if (result == OC_STACK_OK)
     {
         m_observeHandle = nullptr;
     }
@@ -582,64 +584,57 @@ std::string OCResource::sid() const
 
 #ifdef WITH_MQ
 OCStackResult OCResource::discoveryMQTopics(const QueryParamsMap& queryParametersMap,
-                                            FindCallback attributeHandler)
+                                            MQTopicCallback attributeHandler,
+                                            QualityOfService qos)
 {
-    QualityOfService defaultQos = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
     return checked_guard(m_clientWrapper.lock(),
                             &IClientWrapper::ListenForMQTopic,
                             m_devAddr, m_uri,
                             queryParametersMap, m_headerOptions,
-                            attributeHandler, defaultQos);
+                            attributeHandler, qos);
 }
 
 OCStackResult OCResource::createMQTopic(const OCRepresentation& rep,
                                         const std::string& topicUri,
                                         const QueryParamsMap& queryParametersMap,
-                                        MQCreateTopicCallback attributeHandler)
+                                        MQTopicCallback attributeHandler,
+                                        QualityOfService qos)
 {
-    QualityOfService defaultQos = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutMQTopicRepresentation,
                          m_devAddr, topicUri, rep, queryParametersMap,
-                         m_headerOptions, attributeHandler, defaultQos);
+                         m_headerOptions, attributeHandler, qos);
 }
 #endif
 #ifdef MQ_SUBSCRIBER
 OCStackResult OCResource::subscribeMQTopic(ObserveType observeType,
-        const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
+                                           const QueryParamsMap& queryParametersMap,
+                                           ObserveCallback observeHandler,
+                                           QualityOfService qos)
 {
-    QualityOfService defaultQoS = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
-
-    return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
+    return result_guard(observe(observeType, queryParametersMap, observeHandler, qos));
 }
 
-OCStackResult OCResource::unsubscribeMQTopic()
+OCStackResult OCResource::unsubscribeMQTopic(QualityOfService qos)
 {
-    QualityOfService defaultQoS = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
-    return result_guard(cancelObserve(defaultQoS));
+    return result_guard(cancelObserve(qos));
 }
 
 OCStackResult OCResource::requestMQPublish(const QueryParamsMap& queryParametersMap,
-                                           PostCallback attributeHandler)
+                                           PostCallback attributeHandler,
+                                           QualityOfService qos)
 {
     OCRepresentation rep;
     rep.setValue(std::string("req_pub"), std::string("true"));
-    QualityOfService defaultQos = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
-    return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
+    return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
 }
 #endif
 #ifdef MQ_PUBLISHER
 OCStackResult OCResource::publishMQTopic(const OCRepresentation& rep,
                                          const QueryParamsMap& queryParametersMap,
-                                         PostCallback attributeHandler)
+                                         PostCallback attributeHandler,
+                                         QualityOfService qos)
 {
-    QualityOfService defaultQos = OC::QualityOfService::NaQos;
-    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
-    return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
+    return result_guard(post(rep, queryParametersMap, attributeHandler, qos));
 }
 #endif