Add getServerHeaderOption API in OCResource. 49/22349/1
authorZiran Sun <ziran.sun@samsung.com>
Fri, 1 Sep 2017 10:35:57 +0000 (11:35 +0100)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 6 Sep 2017 14:01:57 +0000 (14:01 +0000)
Client requests to accessing headoptions, especially for
server version information. This patch adds a new API in
OCResource to access header Options from server response.

BUG: https://jira.iotivity.org/browse/IOT-2667
Change-Id: Ie18d3396666f778e0758ef0e4609274e53226cf1
Signed-off-by: Ziran Sun <ziran.sun@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/22299
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Abhishek Pandey <abhi.siso@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
(cherry picked from commit 3c83c81cf200156a13179c80ccc419ca4957ebf7)

resource/examples/simpleclient.cpp
resource/include/OCResource.h
resource/include/OCSerialization.h
resource/src/InProcClientWrapper.cpp
resource/src/OCResource.cpp

index f17da35..0307016 100644 (file)
@@ -437,6 +437,38 @@ void foundResource(std::shared_ptr<OCResource> resource)
 
             if(resourceURI == "/a/light")
             {
+                HeaderOptions headerOptions = resource->getServerHeaderOptions();
+                if (headerOptions.size() == 0)
+                {
+                    std::cout << "No header option exists" << std::endl;
+                }
+                else
+                {
+                    for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
+                    {
+                        if (it->getOptionID() == 12)  // COAP_OPTION_CONTENT_FORMAT
+                        {
+                            size_t dataLength = it->getOptionData().length();
+                            char* optionData = new char[dataLength];
+                            strncpy(optionData, it->getOptionData().c_str(), dataLength);
+                            int format = optionData[0] * 256 + optionData[1];
+                            std::cout << "Server format in Discovery response:" << format
+                                    << std::endl;
+                            delete[] optionData;
+                        }
+                        if (it->getOptionID() == 2053) // CA_OPTION_CONTENT_VERSION
+                        {
+                            size_t dataLength = it->getOptionData().length();
+                            char* optionData = new char[dataLength];
+                            strncpy(optionData, it->getOptionData().c_str(), dataLength);
+                            int version = optionData[0] * 256;
+                            std::cout << "Server version in Discovery response:" << version
+                                    << std::endl;
+                            delete[] optionData;
+                        }
+                    }
+                }
+
                 if (resource->connectivityType() & TRANSPORT_TYPE_TO_USE)
                 {
                     curResource = resource;
index fbb324a..f36783b 100644 (file)
@@ -124,6 +124,7 @@ namespace OC
             m_interfaces(std::move(o.m_interfaces)),
             m_children(std::move(m_children)),
             m_endpoints(std::move(m_endpoints)),
+            m_serverHeaderOptions(std::move(m_serverHeaderOptions)),
             m_observeHandle(std::move(m_observeHandle)),
             m_headerOptions(std::move(m_headerOptions))
         {
@@ -491,6 +492,12 @@ namespace OC
         */
         std::vector<std::string> getAllHosts() const;
 
+        /**
+         * Function to get the header options information from response
+         * @return headerOptions HeaderOptions vector consisting of OCHeaderOption objects
+         */
+        HeaderOptions getServerHeaderOptions() const;
+
         /**
         * Function to get the URI for this resource
         * @return std::string resource URI
@@ -678,6 +685,7 @@ namespace OC
         std::vector<std::string> m_interfaces;
         std::vector<std::string> m_children;
         std::vector<std::string> m_endpoints;
+        HeaderOptions m_serverHeaderOptions;
         OCDoHandle m_observeHandle;
         HeaderOptions m_headerOptions;
 
@@ -693,7 +701,8 @@ namespace OC
                     const std::string& serverId, uint8_t property,
                     const std::vector<std::string>& resourceTypes,
                     const std::vector<std::string>& interfaces,
-                    const std::vector<std::string>& endpoints);
+                    const std::vector<std::string>& endpoints,
+                    const HeaderOptions& serverHeaderOptions);
 
         OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
                     const std::string& host, const std::string& uri,
index 2e80451..d4ed9f3 100644 (file)
@@ -82,7 +82,8 @@ namespace OC
 
         public:
             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
-                    OCDevAddr& devAddr, OCDiscoveryPayload* payload)
+                    OCDevAddr& devAddr,
+                    HeaderOptions serverHeaderOption, OCDiscoveryPayload* payload)
             {
                 OCDevAddr currentDevAddr = devAddr;
                 while (payload)
@@ -112,7 +113,8 @@ namespace OC
                                     res->bitmap,
                                     StringLLToVector(res->types),
                                     StringLLToVector(res->interfaces),
-                                    epsVector
+                                    epsVector,
+                                    serverHeaderOption
                                     )));
 
 #ifdef TCP_ADAPTER
@@ -128,7 +130,8 @@ namespace OC
                                             res->bitmap,
                                             StringLLToVector(res->types),
                                             StringLLToVector(res->interfaces),
-                                            epsVector
+                                            epsVector,
+                                            serverHeaderOption
                                             )));
                         }
 #endif
index c3a1913..9158c3d 100644 (file)
@@ -142,6 +142,32 @@ namespace OC
         return root;
     }
 
+    void parseServerHeaderOptions(OCClientResponse* clientResponse,
+                    HeaderOptions& serverHeaderOptions)
+    {
+        if (clientResponse)
+        {
+            // Parse header options from server
+            uint16_t optionID;
+            std::string optionData;
+
+            for(size_t i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
+            {
+                optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
+                optionData = reinterpret_cast<const char*>
+                                (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
+                HeaderOption::OCHeaderOption headerOption(optionID, optionData);
+                serverHeaderOptions.push_back(headerOption);
+            }
+        }
+        else
+        {
+            // clientResponse is invalid
+            // TODO check proper logging
+            std::cout << " Invalid response " << std::endl;
+        }
+    }
+
     OCStackApplicationResult listenCallback(void* ctx, OCDoHandle /*handle*/,
         OCClientResponse* clientResponse)
     {
@@ -180,7 +206,10 @@ namespace OC
 
         try
         {
+            HeaderOptions serverHeaderOptions;
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
             ListenOCContainer container(clientWrapper, clientResponse->devAddr,
+                                    serverHeaderOptions,
                                     reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
             // loop to ensure valid construction of all resources
 
@@ -230,8 +259,11 @@ namespace OC
                 return OC_STACK_KEEP_TRANSACTION;
             }
 
+            HeaderOptions serverHeaderOptions;
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
             ListenOCContainer container(clientWrapper, clientResponse->devAddr,
-                                        reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
+                    serverHeaderOptions,
+                    reinterpret_cast< OCDiscoveryPayload* >(clientResponse->payload));
             // loop to ensure valid construction of all resources
             for (auto resource : container.Resources())
             {
@@ -376,8 +408,11 @@ namespace OC
 
         try
         {
+            HeaderOptions serverHeaderOptions;
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
             ListenOCContainer container(clientWrapper, clientResponse->devAddr,
-                                    reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
+                    serverHeaderOptions,
+                    reinterpret_cast< OCDiscoveryPayload* >(clientResponse->payload));
 
             OIC_LOG_V(DEBUG, TAG, "%s: call response callback", __func__);
             std::thread exec(context->callback, container.Resources());
@@ -475,8 +510,11 @@ namespace OC
 
         try
         {
+            HeaderOptions serverHeaderOptions;
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
             ListenOCContainer container(clientWrapper, clientResponse->devAddr,
-                            reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
+                    serverHeaderOptions,
+                    reinterpret_cast< OCDiscoveryPayload* >(clientResponse->payload));
 
             OIC_LOG_V(DEBUG, TAG, "%s: call response callback", __func__);
             std::thread exec(context->callback, container.Resources());
@@ -575,8 +613,11 @@ namespace OC
 
         try
         {
+            HeaderOptions serverHeaderOptions;
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
             ListenOCContainer container(clientWrapper, clientResponse->devAddr,
-                                        (OCRepPayload *) clientResponse->payload);
+                    serverHeaderOptions,
+                    (OCRepPayload *) clientResponse->payload);
 
             // loop to ensure valid construction of all resources
             for (auto resource : container.Resources())
@@ -708,32 +749,6 @@ namespace OC
         return result;
     }
 
-    void parseServerHeaderOptions(OCClientResponse* clientResponse,
-                    HeaderOptions& serverHeaderOptions)
-    {
-        if (clientResponse)
-        {
-            // Parse header options from server
-            uint16_t optionID;
-            std::string optionData;
-
-            for(size_t i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
-            {
-                optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
-                optionData = reinterpret_cast<const char*>
-                                (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
-                HeaderOption::OCHeaderOption headerOption(optionID, optionData);
-                serverHeaderOptions.push_back(headerOption);
-            }
-        }
-        else
-        {
-            // clientResponse is invalid
-            // TODO check proper logging
-            std::cout << " Invalid response " << std::endl;
-        }
-    }
-
 #ifdef WITH_MQ
     OCStackApplicationResult createMQTopicCallback(void* ctx, OCDoHandle /*handle*/,
                     OCClientResponse* clientResponse)
index 335914e..0aae3ce 100644 (file)
@@ -82,12 +82,14 @@ OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
                         const std::string& serverId, uint8_t property,
                         const std::vector<std::string>& resourceTypes,
                         const std::vector<std::string>& interfaces,
-                        const std::vector<std::string>& endpoints)
+                        const std::vector<std::string>& endpoints,
+                        const HeaderOptions& serverHeaderOptions)
  :  m_clientWrapper(clientWrapper), m_uri(uri),
     m_resourceId(serverId, m_uri), m_devAddr(devAddr),
     m_isCollection(false), m_property(property),
     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
     m_endpoints(endpoints),
+    m_serverHeaderOptions(serverHeaderOptions),
     m_observeHandle(nullptr)
 {
     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
@@ -737,6 +739,11 @@ std::vector<std::string> OCResource::getAllHosts() const
     return m_endpoints;
 }
 
+HeaderOptions OCResource::getServerHeaderOptions() const
+{
+    return m_serverHeaderOptions;
+}
+
 std::string OCResource::uri() const
 {
     return m_uri;