+OCStackResult OTMStart(const OicUuid_t *uuid, const OicUuid_t *owner)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ CHECK_PDM_INIT(TAG);
+
+ if (NULL == uuid || NULL == owner)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ sqlite3_stmt *stmt = 0;
+ int res =0;
+ char *uuidStr = NULL;
+ OCStackResult ret = OC_STACK_OK;
+
+ res = sqlite3_prepare_v2(g_db, OTM_INSERT, strlen(OTM_INSERT) + 1, &stmt, NULL);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ ConvertUuidToStr(owner, &uuidStr);
+
+ res = sqlite3_bind_text(stmt, 1, uuidStr, strlen(uuidStr), SQLITE_TRANSIENT);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ OICFree(uuidStr);
+ uuidStr = NULL;
+ ConvertUuidToStr(uuid, &uuidStr);
+
+ res = sqlite3_bind_text(stmt, 2, uuidStr, strlen(uuidStr), SQLITE_TRANSIENT);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ OICFree(uuidStr);
+
+ res = sqlite3_step(stmt);
+ if (SQLITE_DONE != res)
+ {
+ if (SQLITE_CONSTRAINT == res)
+ {
+ int state;
+ if(OC_STACK_OK == OTMGetState(uuid, &state) && 1 == state)
+ {
+ OIC_LOG_V(WARNING, TAG, "%s OTM already started", __func__);
+ ret = OC_STACK_DUPLICATE_UUID;
+ }
+ }
+ else
+ {
+ ret = OC_STACK_ERROR;
+ }
+ }
+ sqlite3_finalize(stmt);
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+ return OC_STACK_OK;
+}
+
+OCStackResult OTMSetState(const OicUuid_t *uuid, int state)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ CHECK_PDM_INIT(TAG);
+
+ if (NULL == uuid)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ sqlite3_stmt *stmt = 0;
+ int res = 0 ;
+ char *uuidStr = NULL;
+
+ res = sqlite3_prepare_v2(g_db, OTM_UPDATE, strlen(OTM_UPDATE) + 1, &stmt, NULL);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ res = sqlite3_bind_int(stmt, 1, state);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ ConvertUuidToStr(uuid, &uuidStr);
+
+ res = sqlite3_bind_text(stmt, 2, uuidStr, strlen(uuidStr), SQLITE_TRANSIENT);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ OICFree(uuidStr);
+
+ 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;
+}
+
+OCStackResult OTMStop(const OicUuid_t *uuid)
+{
+ return OTMSetState(uuid, 2);
+}
+
+OCStackResult OTMGetState(const OicUuid_t *uuid, int *state)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ CHECK_PDM_INIT(TAG);
+
+ if (NULL == uuid)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ sqlite3_stmt *stmt = 0;
+ int res = 0;
+ char *uuidStr = NULL;
+ OCStackResult ret = OC_STACK_ERROR;
+
+ res = sqlite3_prepare_v2(g_db, OTM_GET_STATE, strlen(OTM_GET_STATE) + 1, &stmt, NULL);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ ConvertUuidToStr(uuid, &uuidStr);
+ res = sqlite3_bind_text(stmt, 1, uuidStr, strlen(uuidStr), SQLITE_TRANSIENT);
+ PDM_VERIFY_SQLITE_OK(TAG, res, NULL, ERROR, OC_STACK_ERROR);
+
+ while (SQLITE_ROW == sqlite3_step(stmt))
+ {
+ *state = sqlite3_column_int(stmt, 1);
+ OIC_LOG_V(DEBUG, TAG, "%s state is %d", uuidStr, *state);
+ ret = OC_STACK_OK;
+ break;
+ }
+
+ OICFree(uuidStr);
+ sqlite3_finalize(stmt);
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+ return ret;
+}