mot-agent: add OCUnlinkDevices
authorsaerome.kim <saerome.kim@samsung.com>
Mon, 15 Jan 2018 05:04:24 +0000 (14:04 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:40 +0000 (19:38 +0900)
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
oic_svr_db_server_justworks.dat [new file with mode: 0644]
src/mot-agent/ma-db.c
src/mot-agent/ma-db.h
src/mot-agent/ma-subowner.c

diff --git a/oic_svr_db_server_justworks.dat b/oic_svr_db_server_justworks.dat
new file mode 100644 (file)
index 0000000..987ca60
Binary files /dev/null and b/oic_svr_db_server_justworks.dat differ
index 26b8b0a1bf05fffa111d514c6999b32822470bcf..edf9ff938517cca51b46833faabef421654f0d26 100644 (file)
@@ -92,6 +92,9 @@ typedef enum PdmDeviceState {
 #define PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE "UPDATE T_DEVICE_LINK_STATE SET STATE = 1\
                                                           WHERE ID = ? or ID2 = ?"
 
+#define ASCENDING_ORDER(id1, id2) do{if( (id1) > (id2) )\
+  { int temp; temp = id1; id1 = id2; id2 = temp; }}while(0)
+
 #define CHECK_PDM_INIT() do{if(true != gInit)\
   { MA_LOGE("PDB is not initialized"); \
     return OC_STACK_PDM_IS_NOT_INITIALIZED; }}while(0)
@@ -101,10 +104,10 @@ static bool gInit = false;  /* Only if we can open sqlite db successfully, gInit
 
 static OCStackResult begin()
 {
-    int res = 0;
-    res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_BEGIN, NULL, NULL, NULL);
-    PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR);
-    return OC_STACK_OK;
+       int res = 0;
+       res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_BEGIN, NULL, NULL, NULL);
+       PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR);
+       return OC_STACK_OK;
 }
 
 static OCStackResult commit()
@@ -232,7 +235,7 @@ static OCStackResult removeFromDeviceList(int id)
        return OC_STACK_OK;
 }
 
-int delete_mowned_device_db(const OicUuid_t *UUID)
+int PDMDeleteDevice(const OicUuid_t *UUID)
 {
        CHECK_PDM_INIT();
        if (NULL == UUID)
@@ -255,3 +258,52 @@ int delete_mowned_device_db(const OicUuid_t *UUID)
        commit();
        return OC_STACK_OK;
 }
+
+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);
+       PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR);
+
+       res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id1);
+       PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR);
+
+       res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_SECOND, id2);
+       PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR);
+
+       if (SQLITE_DONE != sqlite3_step(stmt))
+       {
+               MA_LOGE("Error message: %s", sqlite3_errmsg(g_db));
+               sqlite3_finalize(stmt);
+               return OC_STACK_ERROR;
+       }
+       sqlite3_finalize(stmt);
+       return OC_STACK_OK;
+}
+
+int PDMUnlinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2)
+{
+       CHECK_PDM_INIT();
+       if (NULL == UUID1 || NULL == UUID2)
+       {
+               MA_LOGE("Invalid PARAM");
+               return  OC_STACK_INVALID_PARAM;
+       }
+
+       int id1 = 0;
+       if (OC_STACK_OK != getIdForUUID(UUID1, &id1))
+       {
+               MA_LOGE("Requested value not found");
+               return OC_STACK_INVALID_PARAM;
+       }
+
+       int id2 = 0;
+       if (OC_STACK_OK != getIdForUUID(UUID2, &id2))
+       {
+               MA_LOGE("Requested value not found");
+               return OC_STACK_INVALID_PARAM;
+       }
+       ASCENDING_ORDER(id1, id2);
+       return removeLink(id1, id2);
+}
\ No newline at end of file
index 83ea83dd6aaade26cb8d4e37e743f7587893f1b3..4eb8a6c3219de9abe90a5d222d51f402bff86a1e 100644 (file)
@@ -25,7 +25,8 @@ extern "C" {
 
 int openDB();
 int closeDB();
-int delete_mowned_device_db(const OicUuid_t *UUID);
+int PDMDeleteDevice(const OicUuid_t *UUID);
+int PDMUnlinkDevices(const OicUuid_t *UUID1, const OicUuid_t *UUID2);
 
 #ifdef __cplusplus
 }
index a4c185b56177fbd7f2419581053b0040c96b6dd7..999d9049dfb7505a1f23a9635ccd37980dba19a6 100644 (file)
@@ -915,7 +915,7 @@ static void _mot_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has_error
 
                for ( ; num > i; ++i) {
                        if (OC_STACK_OK !=arr[i].res) {
-                               ret = delete_mowned_device_db((const OicUuid_t*) &arr[i].deviceId);
+                               ret = PDMDeleteDevice((const OicUuid_t*) &arr[i].deviceId);
                        }
                }
                ret = closeDB();
@@ -1037,6 +1037,7 @@ static void _remove_mo_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has
                _print_result_list((const OCProvisionResult_t*) arr, num);
        }
        client->g_doneCB = true;
+       OCTerminatePM();
 }
 
 static gpointer _remove_mo_func(gpointer data)
@@ -1071,7 +1072,7 @@ static gpointer _remove_mo_func(gpointer data)
        ret = openDB();
        if (OC_STACK_OK  != ret)
                MA_LOGE( "openDB: ret = %d (%s)", ret, _error_to_string(ret));
-       ret = delete_mowned_device_db((const OicUuid_t*) uuid_target);
+       ret = PDMDeleteDevice((const OicUuid_t*) uuid_target);
        if (OC_STACK_OK  != ret)
                MA_LOGE( "delete_mowned_device_db: ret = %d (%s)", ret, _error_to_string(ret));
        ret = closeDB();