X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fsecurity%2Fprovisioning%2Fsrc%2Fprovisioningdatabasemanager.c;h=359b5109a993f04ed6d5efc935cf5d8d85691f6c;hb=refs%2Ftags%2Faccepted%2Ftizen%2Funified%2F20171010.063815;hp=907a6a256191fd1b5d38a3ea248d49f26e0d5e6d;hpb=2ef6dbada15be2acb96bcf124e19830bc3efe4d8;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c index 907a6a2..359b510 100644 --- a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c +++ b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c @@ -34,10 +34,7 @@ #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 @@ -69,6 +66,7 @@ #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_DEVICE "DELETE FROM T_DEVICE_LIST WHERE ID = ?" +#define PDM_SQLITE_DELETE_DEVICE_WITH_STATE "DELETE FROM T_DEVICE_LIST WHERE STATE= ?" #define PDM_SQLITE_UPDATE_LINK "UPDATE T_DEVICE_LINK_STATE SET STATE = ? WHERE ID = ? and ID2 = ?" #define PDM_SQLITE_LIST_ALL_UUID "SELECT UUID FROM T_DEVICE_LIST WHERE STATE = 0" #define PDM_SQLITE_GET_UUID "SELECT UUID,STATE FROM T_DEVICE_LIST WHERE ID = ?" @@ -96,6 +94,7 @@ static bool gInit = false; /* Only if we can open sqlite db successfully, gInit */ static OCStackResult createDB(const char* path) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); int result = 0; result = sqlite3_open_v2(path, &g_db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL); @@ -110,6 +109,9 @@ static OCStackResult createDB(const char* path) OIC_LOG(INFO, TAG, "Created T_DEVICE_LINK_STATE"); gInit = true; + + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); + return OC_STACK_OK; } @@ -155,11 +157,13 @@ void errLogCallback(void *pArg, int iErrCode, const char *zMsg) (void) pArg; (void) iErrCode; (void) zMsg; - OIC_LOG_V(DEBUG,TAG, "(%d) %s", iErrCode, zMsg); + OIC_LOG_V(DEBUG,TAG, "%s : (%d) %s", __func__, iErrCode, zMsg); } OCStackResult PDMInit(const char *path) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + int rc; const char *dbPath = NULL; if (SQLITE_OK != sqlite3_config(SQLITE_CONFIG_LOG, errLogCallback, NULL)) @@ -179,15 +183,37 @@ OCStackResult PDMInit(const char *path) if (SQLITE_OK != rc) { OIC_LOG_V(INFO, TAG, "ERROR: Can't open database: %s", sqlite3_errmsg(g_db)); - return createDB(dbPath); + sqlite3_close(g_db); + OCStackResult ret = createDB(dbPath); + if (OC_STACK_OK != ret) + { + sqlite3_close(g_db); + } + return ret; } gInit = true; + + /* + * Remove PDM_DEVICE_INIT status devices. + * PDM_DEVICE_INIT means that the OTM process is in progress. + * PDM_DEVICE_INIT state device can be existed when the program is terminated during the OTM process in progress. + * For this reason, PDM_DEVICE_INIT devices should be removed at PDM initialization time. + */ + if(OC_STACK_OK != PDMDeleteDeviceWithState(PDM_DEVICE_INIT)) + { + OIC_LOG_V(WARNING, TAG, "Failed to delete init state devices."); + } + + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); + return OC_STACK_OK; } OCStackResult PDMAddDevice(const OicUuid_t *UUID) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == UUID) { @@ -203,7 +229,7 @@ OCStackResult PDMAddDevice(const OicUuid_t *UUID) 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); @@ -221,42 +247,8 @@ OCStackResult PDMAddDevice(const OicUuid_t *UUID) return OC_STACK_ERROR; } sqlite3_finalize(stmt); - 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); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } @@ -265,6 +257,8 @@ static OCStackResult isDeviceStale(const OicUuid_t *uuid, bool *result) */ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + 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); @@ -280,6 +274,7 @@ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) OIC_LOG_V(DEBUG, TAG, "ID is %d", tempId); *id = tempId; sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } sqlite3_finalize(stmt); @@ -291,6 +286,7 @@ static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) */ OCStackResult PDMIsDuplicateDevice(const OicUuid_t* UUID, bool *result) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); CHECK_PDM_INIT(TAG); if (NULL == UUID || NULL == result) @@ -316,6 +312,8 @@ OCStackResult PDMIsDuplicateDevice(const OicUuid_t* UUID, bool *result) sqlite3_finalize(stmt); *result = retValue; + + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } @@ -324,6 +322,8 @@ OCStackResult PDMIsDuplicateDevice(const OicUuid_t* UUID, bool *result) */ static OCStackResult addlink(int id1, int id2) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0; res = sqlite3_prepare_v2(g_db, PDM_SQLITE_INSERT_LINK_DATA, @@ -336,7 +336,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) @@ -346,11 +346,14 @@ static OCStackResult addlink(int id1, int id2) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMLinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == UUID1 || NULL == UUID2) { @@ -358,26 +361,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; } @@ -395,6 +399,7 @@ OCStackResult PDMLinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) } ASCENDING_ORDER(id1, id2); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return addlink(id1, id2); } @@ -403,6 +408,8 @@ OCStackResult PDMLinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) */ static OCStackResult removeLink(int id1, int id2) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + 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); @@ -421,11 +428,14 @@ static OCStackResult removeLink(int id1, int id2) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMUnlinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == UUID1 || NULL == UUID2) { @@ -447,11 +457,14 @@ OCStackResult PDMUnlinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2) return OC_STACK_INVALID_PARAM; } ASCENDING_ORDER(id1, id2); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return removeLink(id1, id2); } static OCStackResult removeFromDeviceList(int id) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0; res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_DEVICE, @@ -468,11 +481,14 @@ static OCStackResult removeFromDeviceList(int id) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMDeleteDevice(const OicUuid_t *UUID) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == UUID) { @@ -492,12 +508,15 @@ OCStackResult PDMDeleteDevice(const OicUuid_t *UUID) return OC_STACK_ERROR; } commit(); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } static OCStackResult updateLinkState(int id1, int id2, int state) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0 ; res = sqlite3_prepare_v2(g_db, PDM_SQLITE_UPDATE_LINK, @@ -520,11 +539,14 @@ static OCStackResult updateLinkState(int id1, int id2, int state) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMSetLinkStale(const OicUuid_t* uuidOfDevice1, const OicUuid_t* uuidOfDevice2) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == uuidOfDevice1 || NULL == uuidOfDevice2) { @@ -546,12 +568,14 @@ 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); - + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); + return updateLinkState(id1, id2, PDM_DEVICE_STALE); } OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL != *uuidList) { @@ -564,7 +588,7 @@ OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices) strlen(PDM_SQLITE_LIST_ALL_UUID) + 1, &stmt, NULL); PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); - int counter = 0; + size_t counter = 0; while (SQLITE_ROW == sqlite3_step(stmt)) { const void *ptr = sqlite3_column_blob(stmt, PDM_FIRST_INDEX); @@ -582,11 +606,14 @@ OCStackResult PDMGetOwnedDevices(OCUuidList_t **uuidList, size_t *numOfDevices) } *numOfDevices = counter; sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0; res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_UUID, @@ -602,7 +629,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) { @@ -620,31 +647,34 @@ static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result) return OC_STACK_OK; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_INVALID_PARAM; } OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST, size_t *numOfDevices) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); - if (NULL != *UUIDLIST) + if (NULL == UUID || NULL == numOfDevices || !UUIDLIST) { - OIC_LOG(ERROR, TAG, "Not null list will cause memory leak"); return OC_STACK_INVALID_PARAM; } - if (NULL == UUID) + if (NULL != *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; @@ -667,7 +697,7 @@ OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_SECOND, id); PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); - int counter = 0; + size_t counter = 0; while (SQLITE_ROW == sqlite3_step(stmt)) { int i1 = sqlite3_column_int(stmt, PDM_FIRST_INDEX); @@ -696,11 +726,14 @@ OCStackResult PDMGetLinkedDevices(const OicUuid_t *UUID, OCUuidList_t **UUIDLIST } *numOfDevices = counter; sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *numOfDevices) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL != *staleDevList) { @@ -714,10 +747,10 @@ OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *num strlen(PDM_SQLITE_GET_STALE_INFO) + 1, &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); - int counter = 0; + size_t counter = 0; while (SQLITE_ROW == sqlite3_step(stmt)) { int i1 = sqlite3_column_int(stmt, PDM_FIRST_INDEX); @@ -741,20 +774,26 @@ OCStackResult PDMGetToBeUnlinkedDevices(OCPairList_t **staleDevList, size_t *num } *numOfDevices = counter; sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } OCStackResult PDMClose() { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); int res = 0; res = sqlite3_close(g_db); PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } void PDMDestoryOicUuidLinkList(OCUuidList_t* ptr) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + if(ptr) { OCUuidList_t *tmp1 = NULL,*tmp2=NULL; @@ -764,10 +803,14 @@ void PDMDestoryOicUuidLinkList(OCUuidList_t* ptr) OICFree(tmp1); } } + + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); } void PDMDestoryStaleLinkList(OCPairList_t* ptr) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + if(ptr) { OCPairList_t *tmp1 = NULL,*tmp2=NULL; @@ -777,11 +820,15 @@ void PDMDestoryStaleLinkList(OCPairList_t* ptr) OICFree(tmp1); } } + + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); } OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* uuidOfDevice2, bool* result) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + CHECK_PDM_INIT(TAG); if (NULL == uuidOfDevice1 || NULL == uuidOfDevice2 || NULL == result) { @@ -801,27 +848,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; } @@ -847,11 +894,14 @@ OCStackResult PDMIsLinkExists(const OicUuid_t* uuidOfDevice1, const OicUuid_t* u } sqlite3_finalize(stmt); *result = ret; + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } -static OCStackResult updateDeviceState(const OicUuid_t *uuid, int state) +static OCStackResult updateDeviceState(const OicUuid_t *uuid, PdmDeviceState_t state) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0 ; res = sqlite3_prepare_v2(g_db, PDM_SQLITE_UPDATE_DEVICE, @@ -871,11 +921,14 @@ static OCStackResult updateDeviceState(const OicUuid_t *uuid, int state) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } static OCStackResult updateLinkForStaleDevice(const OicUuid_t *devUuid) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + sqlite3_stmt *stmt = 0; int res = 0 ; @@ -904,26 +957,36 @@ static OCStackResult updateLinkForStaleDevice(const OicUuid_t *devUuid) return OC_STACK_ERROR; } sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; } -OCStackResult PDMSetDeviceStale(const OicUuid_t* uuidOfDevice) +OCStackResult PDMSetDeviceState(const OicUuid_t* uuid, PdmDeviceState_t state) { + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + + 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(); @@ -931,5 +994,68 @@ OCStackResult PDMSetDeviceStale(const OicUuid_t* uuidOfDevice) return res; } commit(); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); + return OC_STACK_OK; +} + +OCStackResult PDMGetDeviceState(const OicUuid_t *uuid, PdmDeviceState_t* result) +{ + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + + 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); + + *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); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); + return OC_STACK_OK; +} + +OCStackResult PDMDeleteDeviceWithState(const PdmDeviceState_t state) +{ + OIC_LOG_V(DEBUG, TAG, "IN %s", __func__); + + CHECK_PDM_INIT(TAG); + if (PDM_DEVICE_ACTIVE != state && PDM_DEVICE_STALE != state && + PDM_DEVICE_INIT != state && PDM_DEVICE_UNKNOWN != state) + { + return OC_STACK_INVALID_PARAM; + } + + sqlite3_stmt *stmt = 0; + int res =0; + res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_DEVICE_WITH_STATE, + strlen(PDM_SQLITE_DELETE_DEVICE_WITH_STATE) + 1, &stmt, NULL); + PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); + + res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, state); + PDM_VERIFY_SQLITE_OK(TAG, res, ERROR, OC_STACK_ERROR); + + if (SQLITE_DONE != sqlite3_step(stmt)) + { + OIC_LOG_V(ERROR, TAG, "Error message: %s", sqlite3_errmsg(g_db)); + sqlite3_finalize(stmt); + return OC_STACK_ERROR; + } + sqlite3_finalize(stmt); + OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__); return OC_STACK_OK; }