IOT-1583: Removing /W3 warning from resource/csdk/security.
[iotivity.git] / resource / csdk / security / provisioning / src / provisioningdatabasemanager.c
index 80b3eb2..51e0b4d 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <stdlib.h>
+#include <limits.h>
 
 #include "sqlite3.h"
 #include "logger.h"
 
 #define DB_FILE "PDM.db"
 
-#define TAG "PDM"
-
-#define PDM_STALE_STATE 1
-#define PDM_ACTIVE_STATE 0
+#define TAG "OIC_PDM"
 
 #define PDM_FIRST_INDEX 0
 #define PDM_SECOND_INDEX 1
 #define PDM_SQLITE_TRANSACTION_BEGIN "BEGIN TRANSACTION;"
 #define PDM_SQLITE_TRANSACTION_COMMIT "COMMIT;"
 #define PDM_SQLITE_TRANSACTION_ROLLBACK "ROLLBACK;"
+
+#ifdef __GNUC__
+#if ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 6))
+#define static_assert(value, message) _Static_assert((value) ? 1 : 0, message)
+#else
+#define static_assert(value, message)
+#endif
+#endif
+#define PDM_VERIFY_STATEMENT_SIZE(stmt) \
+    static_assert(sizeof(stmt) < INT_MAX, #stmt " must be shorter than INT_MAX.")
+
 #define PDM_SQLITE_GET_STALE_INFO "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE STATE = ?"
+#define PDM_SQLITE_GET_STALE_INFO_SIZE (int)sizeof(PDM_SQLITE_GET_STALE_INFO)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_STALE_INFO);
+
 #define PDM_SQLITE_INSERT_T_DEVICE_LIST "INSERT INTO T_DEVICE_LIST VALUES(?,?,?)"
+#define PDM_SQLITE_INSERT_T_DEVICE_LIST_SIZE (int)sizeof(PDM_SQLITE_INSERT_T_DEVICE_LIST)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_INSERT_T_DEVICE_LIST);
+
 #define PDM_SQLITE_GET_ID "SELECT ID FROM T_DEVICE_LIST WHERE UUID like ?"
+#define PDM_SQLITE_GET_ID_SIZE (int)sizeof(PDM_SQLITE_GET_ID)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_ID);
+
 #define PDM_SQLITE_INSERT_LINK_DATA "INSERT INTO T_DEVICE_LINK_STATE VALUES(?,?,?)"
+#define PDM_SQLITE_INSERT_LINK_DATA_SIZE (int)sizeof(PDM_SQLITE_INSERT_LINK_DATA)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_INSERT_LINK_DATA);
+
 #define PDM_SQLITE_DELETE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? and ID2 = ?"
-#define PDM_SQLITE_DELETE_DEVICE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? or ID2 = ?"
+#define PDM_SQLITE_DELETE_LINK_SIZE (int)sizeof(PDM_SQLITE_DELETE_LINK)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_DELETE_LINK);
+
 #define PDM_SQLITE_DELETE_DEVICE "DELETE FROM T_DEVICE_LIST  WHERE ID = ?"
+#define PDM_SQLITE_DELETE_DEVICE_SIZE (int)sizeof(PDM_SQLITE_DELETE_DEVICE)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_DELETE_DEVICE);
+
 #define PDM_SQLITE_UPDATE_LINK "UPDATE T_DEVICE_LINK_STATE SET STATE = ?  WHERE ID = ? and ID2 = ?"
+#define PDM_SQLITE_UPDATE_LINK_SIZE (int)sizeof(PDM_SQLITE_UPDATE_LINK)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_UPDATE_LINK);
+
 #define PDM_SQLITE_LIST_ALL_UUID "SELECT UUID FROM T_DEVICE_LIST WHERE STATE = 0"
+#define PDM_SQLITE_LIST_ALL_UUID_SIZE (int)sizeof(PDM_SQLITE_LIST_ALL_UUID)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_LIST_ALL_UUID);
+
 #define PDM_SQLITE_GET_UUID "SELECT UUID,STATE FROM T_DEVICE_LIST WHERE ID = ?"
+#define PDM_SQLITE_GET_UUID_SIZE (int)sizeof(PDM_SQLITE_GET_UUID)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_UUID);
+
 #define PDM_SQLITE_GET_LINKED_DEVICES "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \
                                            (ID = ? or ID2 = ?) and state = 0"
+#define PDM_SQLITE_GET_LINKED_DEVICES_SIZE (int)sizeof(PDM_SQLITE_GET_LINKED_DEVICES)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_LINKED_DEVICES);
+
 #define PDM_SQLITE_GET_DEVICE_LINKS "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \
                                           ID = ? and ID2 = ? and state = 0"
+#define PDM_SQLITE_GET_DEVICE_LINKS_SIZE (int)sizeof(PDM_SQLITE_GET_DEVICE_LINKS)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_DEVICE_LINKS);
+
 #define PDM_SQLITE_UPDATE_DEVICE "UPDATE T_DEVICE_LIST SET STATE = ?  WHERE UUID like ?"
+#define PDM_SQLITE_UPDATE_DEVICE_SIZE (int)sizeof(PDM_SQLITE_UPDATE_DEVICE)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_UPDATE_DEVICE);
+
 #define PDM_SQLITE_GET_DEVICE_STATUS "SELECT STATE FROM T_DEVICE_LIST WHERE UUID like ?"
+#define PDM_SQLITE_GET_DEVICE_STATUS_SIZE (int)sizeof(PDM_SQLITE_GET_DEVICE_STATUS)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_GET_DEVICE_STATUS);
+
 #define PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE "UPDATE T_DEVICE_LINK_STATE SET STATE = 1\
                                                           WHERE ID = ? or ID2 = ?"
+#define PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE_SIZE (int)sizeof(PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE)
+PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE);
+
 
 #define ASCENDING_ORDER(id1, id2) do{if( (id1) > (id2) )\
   { int temp; temp = id1; id1 = id2; id2 = temp; }}while(0)
@@ -197,13 +247,13 @@ OCStackResult PDMAddDevice(const OicUuid_t *UUID)
     sqlite3_stmt *stmt = 0;
     int res =0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_INSERT_T_DEVICE_LIST,
-                              strlen(PDM_SQLITE_INSERT_T_DEVICE_LIST) + 1, &stmt, NULL);
+                              PDM_SQLITE_INSERT_T_DEVICE_LIST_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_SECOND, UUID, UUID_LENGTH, SQLITE_STATIC);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
-    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_THIRD, PDM_ACTIVE_STATE);
+    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_THIRD, PDM_DEVICE_INIT);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_step(stmt);
@@ -224,42 +274,6 @@ OCStackResult PDMAddDevice(const OicUuid_t *UUID)
     return OC_STACK_OK;
 }
 
-/**
- * Function to check whether device is stale or not
- */
-static OCStackResult isDeviceStale(const OicUuid_t *uuid, bool *result)
-{
-    if (NULL == uuid || NULL == result)
-    {
-        OIC_LOG(ERROR, TAG, "UUID or result is NULL");
-        return OC_STACK_INVALID_PARAM;
-    }
-
-    sqlite3_stmt *stmt = 0;
-    int res = 0;
-    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_DEVICE_STATUS, strlen(PDM_SQLITE_GET_DEVICE_STATUS) + 1,
-                              &stmt, NULL);
-    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
-
-    res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, uuid, UUID_LENGTH, SQLITE_STATIC);
-    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
-
-    bool retValue = false;
-    while(SQLITE_ROW == sqlite3_step(stmt))
-    {
-        int tempStaleStateFromDb = sqlite3_column_int(stmt, PDM_FIRST_INDEX);
-        OIC_LOG_V(DEBUG, TAG, "Stale state is %d", tempStaleStateFromDb);
-        if (PDM_STALE_STATE == tempStaleStateFromDb)
-        {
-            OIC_LOG(INFO, TAG, "Device is stale");
-            retValue = true;
-        }
-    }
-    *result = retValue;
-    sqlite3_finalize(stmt);
-    return OC_STACK_OK;
-}
-
 /**
  *function to get Id for given UUID
  */
@@ -267,7 +281,8 @@ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id)
 {
     sqlite3_stmt *stmt = 0;
     int res = 0;
-    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL);
+    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, PDM_SQLITE_GET_ID_SIZE,
+                             &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC);
@@ -300,7 +315,8 @@ OCStackResult PDMIsDuplicateDevice(const OicUuid_t* UUID, bool *result)
     }
     sqlite3_stmt *stmt = 0;
     int res = 0;
-    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL);
+    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, PDM_SQLITE_GET_ID_SIZE,
+                             &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC);
@@ -327,7 +343,7 @@ static OCStackResult addlink(int id1, int id2)
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_INSERT_LINK_DATA,
-                              strlen(PDM_SQLITE_INSERT_LINK_DATA) + 1, &stmt, NULL);
+                              PDM_SQLITE_INSERT_LINK_DATA_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id1);
@@ -336,7 +352,7 @@ static OCStackResult addlink(int id1, int id2)
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_SECOND, id2);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
-    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_THIRD, PDM_ACTIVE_STATE);
+    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_THIRD, PDM_DEVICE_ACTIVE);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     if (sqlite3_step(stmt) != SQLITE_DONE)
@@ -358,26 +374,27 @@ OCStackResult PDMLinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2)
         return  OC_STACK_INVALID_PARAM;
     }
 
-    bool result = false;
-    if (OC_STACK_OK != isDeviceStale(UUID1, &result))
+    PdmDeviceState_t state = PDM_DEVICE_UNKNOWN;
+    if (OC_STACK_OK != PDMGetDeviceState(UUID1, &state))
     {
         OIC_LOG(ERROR, TAG, "Internal error occured");
         return OC_STACK_ERROR;
     }
-    if (result)
+    if (PDM_DEVICE_ACTIVE != state)
     {
-        OIC_LOG(ERROR, TAG, "UUID1:Stale device");
+        OIC_LOG_V(ERROR, TAG, "UUID1: Device state is not active : %d", state);
         return OC_STACK_INVALID_PARAM;
     }
-    result = false;
-    if (OC_STACK_OK != isDeviceStale(UUID2, &result))
+
+    state = PDM_DEVICE_UNKNOWN;
+    if (OC_STACK_OK != PDMGetDeviceState(UUID2, &state))
     {
         OIC_LOG(ERROR, TAG, "Internal error occured");
         return OC_STACK_ERROR;
     }
-    if (result)
+    if (PDM_DEVICE_ACTIVE != state)
     {
-        OIC_LOG(ERROR, TAG, "UUID2:Stale device");
+        OIC_LOG_V(ERROR, TAG, "UUID2: Device state is not active : %d", state);
         return OC_STACK_INVALID_PARAM;
     }
 
@@ -405,7 +422,8 @@ static OCStackResult removeLink(int id1, int id2)
 {
     int res = 0;
     sqlite3_stmt *stmt = 0;
-    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_LINK, strlen(PDM_SQLITE_DELETE_LINK) + 1, &stmt, NULL);
+    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_LINK,
+                             PDM_SQLITE_DELETE_LINK_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id1);
@@ -455,7 +473,7 @@ static OCStackResult removeFromDeviceList(int id)
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_DEVICE,
-                              strlen(PDM_SQLITE_DELETE_DEVICE) + 1, &stmt, NULL);
+                              PDM_SQLITE_DELETE_DEVICE_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id);
@@ -501,7 +519,7 @@ static OCStackResult updateLinkState(int id1, int id2, int state)
     sqlite3_stmt *stmt = 0;
     int res = 0 ;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_UPDATE_LINK,
-                              strlen(PDM_SQLITE_UPDATE_LINK) + 1, &stmt, NULL);
+                              PDM_SQLITE_UPDATE_LINK_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, state);
@@ -546,8 +564,7 @@ OCStackResult PDMSetLinkStale(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u
         return OC_STACK_INVALID_PARAM;
     }
     ASCENDING_ORDER(id1, id2);
-    return updateLinkState(id1, id2, PDM_STALE_STATE);
-
+    return updateLinkState(id1, id2, PDM_DEVICE_STALE);
 }
 
 OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices)
@@ -561,7 +578,7 @@ OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices)
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_LIST_ALL_UUID,
-                              strlen(PDM_SQLITE_LIST_ALL_UUID) + 1, &stmt, NULL);
+                              PDM_SQLITE_LIST_ALL_UUID_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     size_t counter  = 0;
@@ -590,7 +607,7 @@ static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result)
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_UUID,
-                              strlen(PDM_SQLITE_GET_UUID) + 1, &stmt, NULL);
+                              PDM_SQLITE_GET_UUID_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id);
@@ -602,7 +619,7 @@ static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result)
         memcpy(uid, ptr, sizeof(OicUuid_t));
 
         int temp = sqlite3_column_int(stmt, PDM_SECOND_INDEX);
-        if(PDM_STALE_STATE == temp)
+        if(PDM_DEVICE_STALE == temp)
         {
             if(result)
             {
@@ -635,16 +652,16 @@ OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST
         OIC_LOG(ERROR, TAG, "Not null list will cause memory leak");
         return OC_STACK_INVALID_PARAM;
     }
-    bool result = false;
-    OCStackResult ret = isDeviceStale(UUID, &result);
+    PdmDeviceState_t state = PDM_DEVICE_UNKNOWN;
+    OCStackResult ret = PDMGetDeviceState(UUID, &state);
     if (OC_STACK_OK != ret)
     {
         OIC_LOG(ERROR, TAG, "Internal error occured");
         return OC_STACK_ERROR;
     }
-    if (result)
+    if (PDM_DEVICE_ACTIVE != state)
     {
-        OIC_LOG(ERROR, TAG, "Device is stale");
+        OIC_LOG_V(ERROR, TAG, "Device state is not active : %d", state);
         return OC_STACK_INVALID_PARAM;
     }
     int id = 0;
@@ -658,7 +675,7 @@ OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_LINKED_DEVICES,
-                              strlen(PDM_SQLITE_GET_LINKED_DEVICES) + 1, &stmt, NULL);
+                              PDM_SQLITE_GET_LINKED_DEVICES_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id);
@@ -711,10 +728,10 @@ OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *num
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_STALE_INFO,
-                              strlen(PDM_SQLITE_GET_STALE_INFO) + 1, &stmt, NULL);
+                              PDM_SQLITE_GET_STALE_INFO_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
-    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, PDM_STALE_STATE);
+    res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, PDM_DEVICE_STALE);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     size_t counter  = 0;
@@ -801,27 +818,27 @@ OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u
         return OC_STACK_INVALID_PARAM;
     }
 
-    bool isStale = false;
-    if (OC_STACK_OK != isDeviceStale(uuidOfDevice1, &isStale))
+    PdmDeviceState_t state = PDM_DEVICE_UNKNOWN;
+    if (OC_STACK_OK != PDMGetDeviceState(uuidOfDevice1, &state))
     {
         OIC_LOG(ERROR, TAG, "uuidOfDevice1:Internal error occured");
         return OC_STACK_ERROR;
     }
-    if (isStale)
+    if (PDM_DEVICE_ACTIVE != state)
     {
-        OIC_LOG(ERROR, TAG, "uuidOfDevice1:Device is stale");
+        OIC_LOG_V(ERROR, TAG, "uuidOfDevice1:Device state is not active : %d", state);
         return OC_STACK_INVALID_PARAM;
     }
 
-    isStale = false;
-    if (OC_STACK_OK != isDeviceStale(uuidOfDevice2, &isStale))
+    state = PDM_DEVICE_UNKNOWN;
+    if (OC_STACK_OK != PDMGetDeviceState(uuidOfDevice2, &state))
     {
         OIC_LOG(ERROR, TAG, "uuidOfDevice2:Internal error occured");
         return OC_STACK_ERROR;
     }
-    if (isStale)
+    if (PDM_DEVICE_ACTIVE != state)
     {
-        OIC_LOG(ERROR, TAG, "uuidOfDevice2:Device is stale");
+        OIC_LOG_V(ERROR, TAG, "uuidOfDevice2:Device state is not active : %d", state);
         return OC_STACK_INVALID_PARAM;
     }
 
@@ -830,7 +847,7 @@ OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u
     sqlite3_stmt *stmt = 0;
     int res = 0;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_DEVICE_LINKS,
-                              strlen(PDM_SQLITE_GET_DEVICE_LINKS) + 1, &stmt, NULL);
+                              PDM_SQLITE_GET_DEVICE_LINKS_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id1);
@@ -850,12 +867,12 @@ OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u
     return OC_STACK_OK;
 }
 
-static OCStackResult updateDeviceState(const OicUuid_t *uuid, int state)
+static OCStackResult updateDeviceState(const OicUuid_t *uuid, PdmDeviceState_t state)
 {
     sqlite3_stmt *stmt = 0;
     int res = 0 ;
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_UPDATE_DEVICE,
-                              strlen(PDM_SQLITE_UPDATE_DEVICE) + 1, &stmt, NULL);
+                              PDM_SQLITE_UPDATE_DEVICE_SIZE, &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
     res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, state);
@@ -887,7 +904,7 @@ static OCStackResult updateLinkForStaleDevice(const OicUuid_t *devUuid)
     }
 
     res = sqlite3_prepare_v2(g_db, PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE,
-                              strlen(PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE) + 1,
+                              PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE_SIZE,
                                &stmt, NULL);
     PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
 
@@ -907,23 +924,30 @@ static OCStackResult updateLinkForStaleDevice(const OicUuid_t *devUuid)
     return OC_STACK_OK;
 }
 
-OCStackResult PDMSetDeviceStale(const OicUuid_t* uuidOfDevice)
+OCStackResult PDMSetDeviceState(const OicUuid_t* uuid, PdmDeviceState_t state)
 {
+    OCStackResult res = OC_STACK_ERROR;
+
     CHECK_PDM_INIT(TAG);
-    if (NULL == uuidOfDevice)
+    if (NULL == uuid)
     {
         OIC_LOG(ERROR, TAG, "Invalid PARAM");
         return  OC_STACK_INVALID_PARAM;
     }
     begin();
-    OCStackResult res = updateLinkForStaleDevice(uuidOfDevice);
-    if (OC_STACK_OK != res)
+
+    if(PDM_DEVICE_STALE == state)
     {
-        rollback();
-        OIC_LOG(ERROR, TAG, "unable to update links");
-        return res;
+        res = updateLinkForStaleDevice(uuid);
+        if (OC_STACK_OK != res)
+        {
+            rollback();
+            OIC_LOG(ERROR, TAG, "unable to update links");
+            return res;
+        }
     }
-    res = updateDeviceState(uuidOfDevice, PDM_STALE_STATE);
+
+    res = updateDeviceState(uuid, state);
     if (OC_STACK_OK != res)
     {
         rollback();
@@ -933,3 +957,32 @@ OCStackResult PDMSetDeviceStale(const OicUuid_t* uuidOfDevice)
     commit();
     return OC_STACK_OK;
 }
+
+OCStackResult PDMGetDeviceState(const OicUuid_t *uuid, PdmDeviceState_t* result)
+{
+    if (NULL == uuid || NULL == result)
+    {
+        OIC_LOG(ERROR, TAG, "UUID or result is NULL");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    sqlite3_stmt *stmt = 0;
+    int res = 0;
+    res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_DEVICE_STATUS, PDM_SQLITE_GET_DEVICE_STATUS_SIZE,
+                              &stmt, NULL);
+    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
+
+    res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, uuid, UUID_LENGTH, SQLITE_STATIC);
+    PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR);
+
+    *result = PDM_DEVICE_UNKNOWN;
+    while(SQLITE_ROW == sqlite3_step(stmt))
+    {
+        int tempStaleStateFromDb = sqlite3_column_int(stmt, PDM_FIRST_INDEX);
+        OIC_LOG_V(DEBUG, TAG, "Device state is %d", tempStaleStateFromDb);
+        *result = (PdmDeviceState_t)tempStaleStateFromDb;
+    }
+    sqlite3_finalize(stmt);
+    return OC_STACK_OK;
+}
+