*/
static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
+/*
+ * Internal function to setup & cleanup PDM to performing provisioning.
+ *
+ * @param[in] selectedDevice selected device information to performing provisioning.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice);
+
/**
* Function to update owner transfer mode
*
newDevDoxm->owned = false;
otmCtx->attemptCnt++;
+ RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
+ otmCtx->selectedDeviceInfo->securePort);
+
// In order to re-start ownership transfer, device information should be deleted from PDM.
OCStackResult res = PDMDeleteDevice(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
if (OC_STACK_OK != res)
return res;
}
-static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
+static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice)
{
- OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ char* strUuid = NULL;
OCStackResult res = OC_STACK_INVALID_PARAM;
VERIFY_NOT_NULL(TAG, selectedDevice, ERROR);
VERIFY_NOT_NULL(TAG, selectedDevice->doxm, ERROR);
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- otmCtx->selectedDeviceInfo = selectedDevice;
+ PdmDeviceState_t pdmState = PDM_DEVICE_UNKNOWN;
+ res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &pdmState);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMGetDeviceState : %d", res);
+ return res;
+ }
- //Checking duplication of Device ID.
- bool isDuplicate = true;
- res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
+ bool removeCredReq = false;
+ res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
if (OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Internal error in PDMIsDuplicateDevice");
+ OIC_LOG(WARNING, TAG, "Failed to covert uuid to string");
return res;
}
- if (isDuplicate)
+
+ if (PDM_DEVICE_UNKNOWN == pdmState && !selectedDevice->doxm->owned)
+ {
+ removeCredReq = true;
+ }
+ else if (PDM_DEVICE_ACTIVE == pdmState && !selectedDevice->doxm->owned)
{
- PdmDeviceState_t state = PDM_DEVICE_UNKNOWN;
- res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &state);
+ OIC_LOG_V(WARNING, TAG, "Unowned device[%s] dectected from PDM.", strUuid);
+ OIC_LOG_V(WARNING, TAG, "[%s] will be removed from PDM.", strUuid);
+ res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
if(OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Internal error in PDMGetDeviceState");
- SetResult(otmCtx, res);
- return res;
+ OIC_LOG_V(ERROR, TAG, "Failed to remove [%s] information from PDM.", strUuid);
+ goto exit;
}
- char* strUuid = NULL;
- res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
- if(OC_STACK_OK != res)
+ removeCredReq = true;
+ }
+
+ if (removeCredReq)
+ {
+ OIC_LOG_V(WARNING, TAG, "[%s]'s credential will be removed.", strUuid);
+ res = RemoveCredential(&selectedDevice->doxm->deviceID);
+ if (OC_STACK_RESOURCE_DELETED != res)
{
- OIC_LOG(ERROR, TAG, "Failed to convert UUID to str");
- SetResult(otmCtx, res);
- return res;
+ OIC_LOG_V(WARNING, TAG, "Can not find [%s]'s credential.", strUuid);
}
+ }
- if(PDM_DEVICE_STALE == state)
+ //Checking duplication of Device ID.
+ bool isDuplicate = true;
+ res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMIsDuplicateDevice : %d", res);
+ goto exit;
+ }
+
+ if (isDuplicate)
+ {
+ if (PDM_DEVICE_STALE == pdmState)
{
OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
"device status will revert back to initial status.");
res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
- if(OC_STACK_OK != res)
+ if (OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Internal error in PDMSetDeviceState");
- OICFree(strUuid);
- SetResult(otmCtx, res);
- return res;
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMSetDeviceState : %d", res);
+ goto exit;
}
}
- else if(PDM_DEVICE_INIT == state)
+ else if (PDM_DEVICE_INIT == pdmState)
{
OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
- OICFree(strUuid);
- SetResult(otmCtx, OC_STACK_DUPLICATE_REQUEST);
- return OC_STACK_OK;
+ res = OC_STACK_DUPLICATE_REQUEST;
+ goto exit;
}
else
{
OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
- OICFree(strUuid);
- SetResult(otmCtx, OC_STACK_ERROR);
- return OC_STACK_ERROR;
+ res = OC_STACK_ERROR;
+ goto exit;
}
}
else
{
res = PDMAddDevice(&selectedDevice->doxm->deviceID);
- if(OC_STACK_OK != res)
+ if (OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Internal error in PDMAddDevice");
- SetResult(otmCtx, res);
- return res;
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMAddDevice : %d", res);
+ goto exit;
}
}
+exit:
+ OICFree(strUuid);
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+ return res;
+}
+
+static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
+{
+ OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
+ OCStackResult res = OC_STACK_INVALID_PARAM;
+
+ VERIFY_NOT_NULL(TAG, selectedDevice, ERROR);
+ VERIFY_NOT_NULL(TAG, selectedDevice->doxm, ERROR);
+
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+ otmCtx->selectedDeviceInfo = selectedDevice;
+
+ //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
+ res = SetupPDM(selectedDevice);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
+ SetResult(otmCtx, res);
+ return res;
+ }
+
//Select the OxM to performing ownership transfer
res = OTMSelectOwnershipTransferMethod(selectedDevice->doxm->oxm,
selectedDevice->doxm->oxmLen,
SUPER_OWNER);
if(OC_STACK_OK != res)
{
- OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
+ OIC_LOG_V(ERROR, TAG, "Failed to select the provisioning method : %d", res);
SetResult(otmCtx, res);
return res;
}
res = PostOwnerTransferModeToResource(otmCtx);
if(OC_STACK_OK != res)
{
- OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
+ OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
SetResult(otmCtx, res);
return res;
}
#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_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_UPDATE_LINK_SIZE (int)sizeof(PDM_SQLITE_UPDATE_LINK)
PDM_VERIFY_STATEMENT_SIZE(PDM_SQLITE_UPDATE_LINK);
*/
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);
OIC_LOG(INFO, TAG, "Created T_DEVICE_LINK_STATE");
gInit = true;
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+
return OC_STACK_OK;
}
(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))
return createDB(dbPath);
}
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)
{
return OC_STACK_ERROR;
}
sqlite3_finalize(stmt);
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
return OC_STACK_OK;
}
*/
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, PDM_SQLITE_GET_ID_SIZE,
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);
*/
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)
sqlite3_finalize(stmt);
*result = retValue;
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
return OC_STACK_OK;
}
*/
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,
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)
{
}
ASCENDING_ORDER(id1, id2);
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
return addlink(id1, id2);
}
*/
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,
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)
{
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,
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)
{
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,
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)
{
return OC_STACK_INVALID_PARAM;
}
ASCENDING_ORDER(id1, id2);
+ 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)
{
}
*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,
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 == UUID || NULL == numOfDevices || !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)
{
}
*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;
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;
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)
{
}
sqlite3_finalize(stmt);
*result = ret;
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
return OC_STACK_OK;
}
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,
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 ;
return OC_STACK_ERROR;
}
sqlite3_finalize(stmt);
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
return OC_STACK_OK;
}
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);
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");
*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,
+ (int)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;
+}