Accessing server version info at client callback. 43/21743/8
authorZiran Sun <ziran.sun@samsung.com>
Thu, 3 Aug 2017 15:47:06 +0000 (16:47 +0100)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 6 Sep 2017 12:50:39 +0000 (12:50 +0000)
Change-Id: I526a15ce50599782d45f8b0e50b5c4ae2d292b88
Signed-off-by: Ziran Sun <ziran.sun@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21743
Reviewed-by: Abhishek Pandey <abhi.siso@samsung.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
java/common/src/main/java/org/iotivity/base/OcHeaderOption.java
resource/csdk/connectivity/src/caprotocolmessage.c
resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp
resource/csdk/stack/src/ocserverrequest.c
resource/include/OCHeaderOption.h
resource/unittests/OCHeaderOptionTest.cpp

index a8db8e6..1f359af 100644 (file)
@@ -45,6 +45,7 @@ public class OcHeaderOption {
     public static final int LOCATION_PATH_OPTION_ID = 8;
     public static final int LOCATION_QUERY_OPTION_ID = 20;
     public static final int ACCEPT_OPTION_ID = 17;
+    public static final int CONTENT_TYPE_OPTION_ID = 12;
 
     private int mOptionId;
     private String mOptionData;
@@ -55,7 +56,8 @@ public class OcHeaderOption {
                 && optionId != IF_NONE_MATCH_OPTION_ID
                 && optionId != LOCATION_PATH_OPTION_ID
                 && optionId != LOCATION_QUERY_OPTION_ID
-                && optionId != ACCEPT_OPTION_ID) {
+                && optionId != ACCEPT_OPTION_ID
+                && optionId != CONTENT_TYPE_OPTION_ID) {
             throw new InvalidParameterException("Option ID range is invalid");
         }
 
index 1a1890f..9ab4609 100644 (file)
@@ -718,8 +718,6 @@ CAResult_t CAGetOptionCount(coap_opt_iterator_t opt_iter, uint8_t *optionCount)
         if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
             && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
             && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
-            && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
-            && CA_OPTION_CONTENT_VERSION != opt_iter.type
             && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type
             && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type
             && COAP_OPTION_PROXY_SCHEME != opt_iter.type)
@@ -912,39 +910,6 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
             {
                 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
             }
-            else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
-            {
-                if (1 == COAP_OPT_LENGTH(option))
-                {
-                    outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
-                }
-                else if (2 == COAP_OPT_LENGTH(option))
-                {
-                    unsigned int decodedFormat = coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
-                    assert(decodedFormat <= UINT16_MAX);
-                    outInfo->payloadFormat = CAConvertFormat((uint16_t)decodedFormat);
-                }
-                else
-                {
-                    outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
-                    OIC_LOG(DEBUG, TAG, "option has an unsupported format");
-                }
-            }
-            else if (CA_OPTION_CONTENT_VERSION == opt_iter.type)
-            {
-                if (2 == COAP_OPT_LENGTH(option))
-                {
-                    unsigned int decodedVersion = coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
-                    assert(decodedVersion <= UINT16_MAX);
-                    outInfo->payloadVersion = (uint16_t)decodedVersion;
-                }
-                else
-                {
-                    OIC_LOG(DEBUG, TAG, "unsupported content version");
-                    outInfo->payloadVersion = DEFAULT_VERSION_VALUE;
-
-                }
-            }
             else if (COAP_OPTION_URI_PORT == opt_iter.type ||
                     COAP_OPTION_URI_HOST == opt_iter.type ||
                     COAP_OPTION_ETAG == opt_iter.type ||
@@ -994,6 +959,41 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                         OIC_LOG(DEBUG, TAG, "option has an unsupported accept format");
                     }
                 }
+                else if (CA_OPTION_CONTENT_VERSION == opt_iter.type)
+                {
+                    if (2 == COAP_OPT_LENGTH(option))
+                    {
+                        unsigned int decodedVersion = coap_decode_var_bytes(COAP_OPT_VALUE(option),
+                                COAP_OPT_LENGTH(option));
+                        assert(decodedVersion <= UINT16_MAX);
+                        outInfo->payloadVersion = (uint16_t) decodedVersion;
+                    }
+                    else
+                    {
+                        OIC_LOG(DEBUG, TAG, "unsupported payload version");
+                        outInfo->payloadVersion = DEFAULT_VERSION_VALUE;
+                    }
+                }
+                else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
+                {
+                    if (1 == COAP_OPT_LENGTH(option))
+                    {
+                        outInfo->payloadFormat = CAConvertFormat((uint8_t) buf[0]);
+                    }
+                    else if (2 == COAP_OPT_LENGTH(option))
+                    {
+                        unsigned int decodedFormat = coap_decode_var_bytes(COAP_OPT_VALUE(option),
+                                COAP_OPT_LENGTH(option));
+                        assert(decodedFormat <= UINT16_MAX);
+                        outInfo->payloadFormat = CAConvertFormat((uint16_t) decodedFormat);
+                    }
+                    else
+                    {
+                        outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
+                        OIC_LOG(DEBUG, TAG, "option has an unsupported accept format");
+                    }
+                }
+
                 if (idx < count)
                 {
                     if (bufLength <= sizeof(outInfo->options[0].optionData))
index 01c4b50..db7bc32 100644 (file)
@@ -323,7 +323,18 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
             {
                 OIC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
                         ((OCHeaderOption)rcvdOptions[i]).optionID );
-
+               if (COAP_OPTION_CONTENT_VERSION == ((OCHeaderOption)rcvdOptions[i]).optionID)
+               {
+                    uint16_t versionValue = rcvdOptions[i].optionData[0] * 256
+                            + rcvdOptions[i].optionData[1];
+                    OIC_LOG_V(INFO, TAG, "Received version value of %u", versionValue);
+               }
+               if (COAP_OPTION_CONTENT_FORMAT == ((OCHeaderOption)rcvdOptions[i]).optionID)
+               {
+                    uint16_t formatValue = rcvdOptions[i].optionData[0] * 256
+                            + rcvdOptions[i].optionData[1];
+                   OIC_LOG_V(INFO, TAG, "Received format value of %u", formatValue);
+               }
                 OIC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
                     MAX_HEADER_OPTION_DATA_LENGTH);
             }
index cc28bdd..e542052 100644 (file)
@@ -588,7 +588,57 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
         responseInfo.info.numOptions++;
     }
 
-    if(responseInfo.info.numOptions > 0)
+    // Check if version and format option exist.
+    uint16_t payloadVersion = OC_SPEC_VERSION_VALUE;
+    uint16_t payloadFormat = COAP_MEDIATYPE_APPLICATION_VND_OCF_CBOR;
+    bool IsPayloadVersionSet = false;
+    bool IsPayloadFormatSet = false;
+    if (ehResponse->payload)
+    {
+        for (uint8_t i = 0; i < responseInfo.info.numOptions; i++)
+        {
+            if (COAP_OPTION_CONTENT_VERSION == ehResponse->sendVendorSpecificHeaderOptions[i].optionID)
+            {
+                payloadVersion =
+                        (ehResponse->sendVendorSpecificHeaderOptions[i].optionData[1] << 8)
+                        + ehResponse->sendVendorSpecificHeaderOptions[i].optionData[0];
+                IsPayloadVersionSet = true;
+            }
+            else if (COAP_OPTION_CONTENT_TYPE == ehResponse->sendVendorSpecificHeaderOptions[i].optionID)
+            {
+                if (1 == ehResponse->sendVendorSpecificHeaderOptions[i].optionLength)
+                {
+                    payloadFormat = ehResponse->sendVendorSpecificHeaderOptions[i].optionData[0];
+                    IsPayloadFormatSet = true;
+                }
+                else if (2 == ehResponse->sendVendorSpecificHeaderOptions[i].optionLength)
+                {
+                    payloadFormat =
+                            (ehResponse->sendVendorSpecificHeaderOptions[i].optionData[1] << 8)
+                            + ehResponse->sendVendorSpecificHeaderOptions[i].optionData[0];
+
+                    IsPayloadFormatSet = true;
+                }
+                else
+                {
+                    payloadFormat = CA_FORMAT_UNSUPPORTED;
+                    IsPayloadFormatSet = true;
+                    OIC_LOG_V(DEBUG, TAG, "option has an unsupported format");
+                }
+            }
+        }
+        if (!IsPayloadVersionSet && !IsPayloadFormatSet)
+        {
+            responseInfo.info.numOptions = responseInfo.info.numOptions + 2;
+        }
+        else if ((IsPayloadFormatSet && CA_FORMAT_APPLICATION_VND_OCF_CBOR == payloadFormat
+                && !IsPayloadVersionSet) || (IsPayloadVersionSet && !IsPayloadFormatSet))
+        {
+            responseInfo.info.numOptions++;
+        }
+    }
+
+    if (responseInfo.info.numOptions > 0)
     {
         responseInfo.info.options = (CAHeaderOption_t *)
                                       OICCalloc(responseInfo.info.numOptions,
@@ -651,6 +701,43 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
                 sizeof(optionsPointer->optionData),
                 ehResponse->resourceUri);
             optionsPointer->optionLength = (uint16_t)strlen(optionsPointer->optionData) + 1;
+            optionsPointer += 1;
+        }
+
+        if (ehResponse->payload)
+        {
+            if (!IsPayloadVersionSet && !IsPayloadFormatSet)
+            {
+                optionsPointer->protocolID = CA_COAP_ID;
+                optionsPointer->optionID = CA_OPTION_CONTENT_VERSION;
+                memcpy(optionsPointer->optionData, &payloadVersion,
+                        sizeof(uint16_t));
+                optionsPointer->optionLength = sizeof(uint16_t);
+                optionsPointer += 1;
+
+                optionsPointer->protocolID = CA_COAP_ID;
+                optionsPointer->optionID = COAP_OPTION_CONTENT_FORMAT;
+                memcpy(optionsPointer->optionData, &payloadFormat,
+                        sizeof(uint16_t));
+                optionsPointer->optionLength = sizeof(uint16_t);
+            }
+            else if (IsPayloadFormatSet && CA_FORMAT_APPLICATION_VND_OCF_CBOR == payloadFormat
+                            && !IsPayloadVersionSet)
+            {
+                optionsPointer->protocolID = CA_COAP_ID;
+                optionsPointer->optionID = CA_OPTION_CONTENT_VERSION;
+                memcpy(optionsPointer->optionData, &payloadVersion,
+                        sizeof(uint16_t));
+                optionsPointer->optionLength = sizeof(uint16_t);
+            }
+            else if (IsPayloadVersionSet && OC_SPEC_VERSION_VALUE <= payloadVersion && !IsPayloadFormatSet)
+            {
+                optionsPointer->protocolID = CA_COAP_ID;
+                optionsPointer->optionID = COAP_OPTION_CONTENT_TYPE;
+                memcpy(optionsPointer->optionData, &payloadFormat,
+                        sizeof(uint16_t));
+                optionsPointer->optionLength = sizeof(uint16_t);
+            }
         }
     }
     else
index e9533ec..1e26a06 100644 (file)
@@ -52,6 +52,7 @@ namespace OC
         const uint16_t LOCATION_PATH_OPTION_ID = 8;
         const uint16_t LOCATION_QUERY_OPTION_ID = 20;
         const uint16_t ACCEPT_OPTION_ID = 17;
+        const uint16_t CONTENT_TYPE_OPTION_ID = 12;
 
         class OCHeaderOption
         {
@@ -72,7 +73,8 @@ namespace OC
                         && optionID != IF_NONE_MATCH_OPTION_ID
                         && optionID != LOCATION_PATH_OPTION_ID
                         && optionID != LOCATION_QUERY_OPTION_ID
-                        && optionID != ACCEPT_OPTION_ID)
+                        && optionID != ACCEPT_OPTION_ID
+                        && optionID != CONTENT_TYPE_OPTION_ID)
                 {
                     throw OCException(OC::Exception::OPTION_ID_RANGE_INVALID);
                 }
index fe10e19..2ce0143 100644 (file)
@@ -52,7 +52,8 @@ namespace OC
                             && HeaderOption::IF_NONE_MATCH_OPTION_ID != i
                             && HeaderOption::LOCATION_PATH_OPTION_ID != i
                             && HeaderOption::LOCATION_QUERY_OPTION_ID != i
-                            && HeaderOption::ACCEPT_OPTION_ID != i)
+                            && HeaderOption::ACCEPT_OPTION_ID != i
+                            && HeaderOption::CONTENT_TYPE_OPTION_ID != i)
                     {
                         ASSERT_THROW(
                                 HeaderOption::OCHeaderOption(i,""),