Fix client crash caused by observe cancellation with sequence No 1.
authorZiran Sun <ziran.sun@samsung.com>
Wed, 20 Jul 2016 15:16:52 +0000 (16:16 +0100)
committerZiran Sun <ziran.sun@samsung.com>
Tue, 26 Jul 2016 08:42:33 +0000 (08:42 +0000)
1. Added support for sequence number starting from 1.
2. When sequence number is 1, client treats it as a normal response
rather than de-registration response.
3. Correct default sequence number value to be valid.

JIRA Issue: https://jira.iotivity.org/browse/IOT-1195

Change-Id: I2ce68d70ff5f8f0cce98234a7dbda566b8085a61
Signed-off-by: Ziran Sun <ziran.sun@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9519
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Mushfiqul Islam <i.mushfiq@samsung.com>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
resource/csdk/stack/include/internal/ocstackinternal.h
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocserverrequest.c
resource/csdk/stack/src/ocstack.c
resource/examples/simpleclient.cpp
resource/src/InProcClientWrapper.cpp

index e4a2f29..7120951 100644 (file)
@@ -66,8 +66,8 @@ extern void* defaultDeviceHandlerCallbackParameter;
 /** The coap scheme */
 #define OC_COAP_SCHEME "coap://"
 
-/** the first outgoing sequence number will be 2*/
-#define OC_OFFSET_SEQUENCE_NUMBER (1)
+/** the first outgoing sequence number will be 1*/
+#define OC_OFFSET_SEQUENCE_NUMBER (0)
 
 /**
  * This structure will be created in occoap and passed up the stack on the server side.
index 4cd74f4..6479069 100755 (executable)
@@ -1029,6 +1029,9 @@ HandleResourceWithEntityHandler (OCServerRequest *request,
         {
             OIC_LOG(INFO, TAG, "Removed observer successfully");
             request->observeResult = OC_STACK_OK;
+            // There should be no observe option header for de-registration response.
+            // Set as an invalid value here so we can detect it later and remove the field in response.
+            request->observationOption = MAX_SEQUENCE_NUMBER + 1;
         }
         else
         {
index d46f889..55a83d6 100644 (file)
@@ -22,6 +22,7 @@
 #include "ocstack.h"
 #include "ocserverrequest.h"
 #include "ocresourcehandler.h"
+#include "ocobserve.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "ocpayload.h"
@@ -556,8 +557,8 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse)
     memcpy(responseInfo.info.token, serverRequest->requestToken, serverRequest->tokenLength);
     responseInfo.info.tokenLength = serverRequest->tokenLength;
 
-    // De-register observe option should not be included in the response header
-    if((serverRequest->observeResult == OC_STACK_OK) && (serverRequest->observationOption != OC_OBSERVE_DEREGISTER))
+    if((serverRequest->observeResult == OC_STACK_OK)&&
+       (serverRequest->observationOption != MAX_SEQUENCE_NUMBER + 1))
     {
         responseInfo.info.numOptions = ehResponse->numSendVendorSpecificHeaderOptions + 1;
     }
index a0bf25e..28f9645 100644 (file)
@@ -1266,7 +1266,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
 
             OCClientResponse response =
                 {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
-            response.sequenceNumber = -1;
+            response.sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
             CopyEndpointToDevAddr(endPoint, &response.devAddr);
             FixUpClientResponse(&response);
             response.resourceUri = responseInfo->info.resourceUri;
@@ -1418,6 +1418,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
 
             if (cbNode->method == OC_REST_OBSERVE &&
                 response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
+                cbNode->sequenceNumber <=  MAX_SEQUENCE_NUMBER &&
                 response.sequenceNumber <= cbNode->sequenceNumber)
             {
                 OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
index c4aff50..3301a3e 100644 (file)
@@ -37,6 +37,8 @@
 #include "OCPlatform.h"
 #include "OCApi.h"
 
+#define maxSequenceNumber 0xFFFFFF
+
 using namespace OC;
 
 static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
@@ -73,7 +75,7 @@ void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& re
 {
     try
     {
-        if(eCode == OC_STACK_OK && sequenceNumber != -1)
+        if(eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
         {
             if(sequenceNumber == OC_OBSERVE_REGISTER)
             {
index 76cce93..196c822 100644 (file)
@@ -1024,10 +1024,7 @@ namespace OC
         std::thread exec(context->callback, serverHeaderOptions, attrs,
                     result, sequenceNumber);
         exec.detach();
-        if (sequenceNumber == OC_OBSERVE_DEREGISTER)
-        {
-            return OC_STACK_DELETE_TRANSACTION;
-        }
+
         return OC_STACK_KEEP_TRANSACTION;
     }