CONPRO-1320] Cleanup observers of deleted resource.
[platform/upstream/iotivity.git] / resource / csdk / stack / include / internal / ocobserve.h
index 0a2eb3e..f5fc6c1 100644 (file)
@@ -29,9 +29,6 @@
 #ifndef OC_OBSERVE_H
 #define OC_OBSERVE_H
 
-/** Sequence number is a 24 bit field, per https://tools.ietf.org/html/draft-ietf-core-observe-16.*/
-#define MAX_SEQUENCE_NUMBER              (0xFFFFFF)
-
 /** Maximum number of observers to reach */
 
 #define MAX_OBSERVER_FAILED_COMM         (2)
 #define MAX_OBSERVER_NON_COUNT           (3)
 
 /**
+ *  MAX_OBSERVER_TTL_SECONDS sets the maximum time to live (TTL) for notification.
+ *  60 sec/min * 60 min/hr * 24 hr/day
+ */
+#define MAX_OBSERVER_TTL_SECONDS     (60 * 60 * 24)
+
+#define MILLISECONDS_PER_SECOND   (1000)
+
+/**
  * Data structure to hold informations for each registered observer.
  */
 typedef struct ResourceObserver
@@ -77,10 +82,32 @@ typedef struct ResourceObserver
     /** force the qos value to CON.*/
     uint8_t forceHighQos;
 
+    /** The TTL for this callback. TTL is set to 24 hours.
+     * A server send a notification in a confirmable message every 24 hours.
+     * This prevents a client that went away or is no logger interested
+     * from remaining in the list of observers indefinitely.*/
+    uint32_t TTL;
+
     /** next node in this list.*/
     struct ResourceObserver *next;
+
+    /** requested payload encoding format. */
+    OCPayloadFormat acceptFormat;
+
 } ResourceObserver;
 
+/**
+ *  Initialize observer list.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult InitializeObserverList();
+
+/**
+ *  Terminate observer list.
+ */
+void TerminateObserverList();
+
 #ifdef WITH_PRESENCE
 /**
  * Create an observe response and send to all observers in the observe list.
@@ -162,6 +189,7 @@ OCStackResult AddObserver (const char         *resUri,
                            uint8_t            tokenLength,
                            OCResource         *resHandle,
                            OCQualityOfService qos,
+                           OCPayloadFormat    acceptFormat,
                            const OCDevAddr    *devAddr);
 
 /**
@@ -175,6 +203,25 @@ OCStackResult AddObserver (const char         *resUri,
  */
  OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength);
 
+ /**
+  * Delete observer with device address from list of observers.
+  * Free memory that was allocated for the observer in the list.
+  *
+  * @param devAddr Device's address.
+  *
+  * @return ::OC_STACK_OK on success, some other value upon failure.
+  */
+OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr);
+
+/**
+ * Delete all observers of a resource.
+ *
+ * @param resource Handle of resource whose observers needs to be deleted.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult DeleteObserverUsingResource(OCResource *res);
+
 /**
  * Search the list of observers for the specified token.
  *
@@ -182,6 +229,7 @@ OCStackResult AddObserver (const char         *resUri,
  * @param tokenLength      Length of token.
  *
  * @return Pointer to found observer.
+ * This is copy of observer, caller must release its memory using FreeObserver().
  */
 ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLength);
 
@@ -191,10 +239,20 @@ ResourceObserver* GetObserverUsingToken (const CAToken_t token, uint8_t tokenLen
  * @param observeId        Observer ID to search for.
  *
  * @return Pointer to found observer.
+ * This is copy of observer, caller must release its memory using FreeObserver().
  */
 ResourceObserver* GetObserverUsingId (const OCObservationId observeId);
 
 /**
+ * Search the list of observers for the specified observe ID.
+ *
+ * @param observeId        Observer ID to search for.
+ *
+ * @return true if observer found matched with observer ID, otherwise false.
+ */
+bool IsObserverAvailable (const OCObservationId observeId);
+
+/**
  *  Add observe header option to a request.
  *
  * @param caHdrOpt        Target request CA header option.
@@ -229,5 +287,12 @@ GetObserveHeaderOption (uint32_t * observationOption,
                         CAHeaderOption_t *options,
                         uint8_t * numOptions);
 
+/**
+ *  Free memory associated with observer.
+ *
+ * @param obsNode        Observer to be freed.
+ */
+void FreeObserver (ResourceObserver* obsNode);
+
 #endif //OC_OBSERVE_H