#define _POSIX_C_SOURCE 200809L
#endif
+#ifdef HAVE_TIME_H
#include <time.h>
+#endif
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
+#endif
#include <stdbool.h>
#include <string.h>
#include "cainterface.h"
#include "base64.h"
#include "cJSON.h"
+#include "global.h"
#include "srmresourcestrings.h"
#include "doxmresource.h"
#include "oxmjustworks.h"
#include "pmtypes.h"
#include "pmutility.h"
-
-// TODO: Not yet supported.
-//#include "oxmrandompin.h"
-
-#define DEFAULT_CONTEXT_VALUE 0x99
-#define DEFAULT_SECURE_PORT 5684
+#include "srmutility.h"
+#include "provisioningdatabasemanager.h"
+#include "oxmrandompin.h"
+#include "ocpayload.h"
+#include "payload_logging.h"
#define TAG "OTM"
-OCProvisionResultCB g_resultCallback;
-OCProvisionResult_t* g_resultArray = NULL;
-uint32_t g_resultArraySize = 0;
-bool hasError = false;
-
-/**
- * Possible states of ownership transfer manager module.
- */
-typedef enum
-{
- SP_NO_MASK = (0 ),
- SP_DISCOVERY_STARTED = (0x1 << 1),
- SP_DISCOVERY_ERROR = (0x1 << 2),
- SP_DISCOVERY_DONE = (0x1 << 3),
- SP_UP_OWN_TR_METH_STARTED = (0x1 << 4),
- SP_UP_OWN_TR_METH_ERROR = (0x1 << 5),
- SP_UP_OWN_TR_METH_DONE = (0x1 << 6),
- SP_LIST_METHODS_STARTED = (0x1 << 7),
- SP_LIST_METHODS_ERROR = (0x1 << 8),
- SP_LIST_METHODS_DONE = (0x1 << 9),
- SP_UPDATE_OP_MODE_STARTED = (0x1 << 10),
- SP_UPDATE_OP_MODE_ERROR = (0x1 << 11),
- SP_UPDATE_OP_MODE_DONE = (0x1 << 12),
- SP_UPDATE_OWNER_STARTED = (0x1 << 13),
- SP_UPDATE_OWNER_ERROR = (0x1 << 14),
- SP_UPDATE_OWNER_DONE = (0x1 << 15)
-} OTMStates;
-
-
-
/**
* Array to store the callbacks for each owner transfer method.
*/
-OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
-
-/**
- * Variable for storing provisioning tool's provisioning capabilities
- * Must be in decreasing order of preference. More prefered method should
- * have lower array index.
- */
-static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
-
-/**
- * Number of supported provisioning methods
- * current version supports only one.
- */
-static int gNumOfProvisioningMethodsPT = 1;
+static OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
/**
- * Function to getting string of ownership transfer method
+ * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
*/
-static const char* GetOxmString(OicSecOxm_t oxmType)
-{
- switch(oxmType)
- {
- case OIC_JUST_WORKS:
- return OXM_JUST_WORKS;
- case OIC_MODE_SWITCH:
- return OXM_MODE_SWITCH;
- case OIC_RANDOM_DEVICE_PIN:
- return OXM_RANDOM_DEVICE_PIN;
- case OIC_PRE_PROVISIONED_DEVICE_PIN:
- return OXM_PRE_PROVISIONED_DEVICE_PIN;
- case OIC_PRE_PROVISION_STRONG_CREDENTIAL:
- return OXM_PRE_PROVISIONED_STRONG_CREDENTIAL;
- default:
- return NULL;
- }
-}
+static OTMContext_t* g_otmCtx = NULL;
/**
* Function to select appropriate provisioning method.
*
- * @param[in] supportedMethodsList List of supported methods
+ * @param[in] supportedMethods Array of supported methods
+ * @param[in] numberOfMethods number of supported methods
* @param[out] selectedMethod Selected methods
- * @return SP_SUCCESS on success
+ * @return OC_STACK_OK on success
*/
-static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods, size_t numberOfMethods,
- OicSecOxm_t *selectedMethod)
+static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
+ size_t numberOfMethods, OicSecOxm_t *selectedMethod)
{
- int i;
- OC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
+ OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
if(numberOfMethods == 0 || !supportedMethods)
{
- OC_LOG(WARNING, TAG, "Could not find a supported OxM.");
+ OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
return OC_STACK_ERROR;
}
*selectedMethod = supportedMethods[0];
- for(i = 0; i < numberOfMethods; i++)
+ for(size_t i = 0; i < numberOfMethods; i++)
{
if(*selectedMethod < supportedMethods[i])
{
/**
* Function to select operation mode.This function will return most secure common operation mode.
*
+ * @param[in] selectedDeviceInfo selected device information to performing provisioning.
* @param[out] selectedMode selected operation mode
- * @return SP_SUCCESS on success
+ * @return OC_STACK_OK on success
*/
static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
OicSecDpom_t *selectedMode)
{
- OC_LOG(DEBUG, TAG, "IN SelectOperationMode");
-
- int i = 0;
- int j = 0;
-
- while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
- {
- if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
- {
- i++;
- }
- else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
- {
- j++;
- }
- else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
- {
- *selectedMode = gProvisioningToolCapability[j];
- break;
- }
- }
- OC_LOG(DEBUG, TAG, "OUT SelectOperationMode");
+ OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
+ *selectedMode = selectedDeviceInfo->pstat->sm[0];
+ OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
}
/**
+ * Function to start ownership transfer.
+ * This function will send the first request for provisioning,
+ * The next request message is sent from the response handler for this request.
+ *
+ * @param[in] ctx context value passed to callback from calling function.
+ * @param[in] selectedDevice selected device information to performing provisioning.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
+
+/**
* Function to update owner transfer mode
*
- * @param[in] deviceInfo Device Info.
+ * @param[in] otmCtx Context value of ownership transfer.
* @return OC_STACK_OK on success
*/
-static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
+static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
/**
* Function to send request to resource to get its pstat resource information.
*
- * @param[in] deviceInfo Selected device Info.
- * @param[in] otmCtx address of pointer variable for OTMContext_t
+ * @param[in] otmCtx Context value of ownership transfer.
* @return OC_STACK_OK on success
*/
static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
/**
- * Function to send ownerShip info. This function would update Owned as true and
- * owner as UUID for provisioning tool
+ * Function to send uuid of owner device to new device.
+ * This function would update 'owner of doxm' as UUID for provisioning tool.
*
- * @param[in] deviceInfo Selected device info
- * @param[in] otmCtx address of pointer variable for OTMContext_t
+ * @param[in] otmCtx Context value of ownership transfer.
* @return OC_STACK_OK on success
*/
-static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
+static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
/**
* Function to update the operation mode. As per the spec. Operation mode in client driven
* single service provisioning it will be updated to 0x3
*
- * @param[in] deviceInfo Selected device Info.
+ * @param[in] otmCtx Context value of ownership transfer.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
+
+/**
+ * Function to update the owner credential to new device
+ *
+ * @param[in] otmCtx Context value of ownership transfer.
* @param[in] selectedOperationMode selected operation mode
- * @param[in] otmCtx address of pointer variable for OTMContext_t
* @return OC_STACK_OK on success
*/
-static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
- OicSecDpom_t selectedOperationMode);
+static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
-static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
+/**
+ * Function to send ownerShip info.
+ * This function would update 'owned of doxm' as true.
+ *
+ * @param[in] otmCtx Context value of ownership transfer.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
-static OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx);
+/**
+ * Function to update pstat as Ready for provisioning.
+ * This function would update 'cm' from bx0000,0010 to bx0000,0000.
+ *
+ * @param[in] ctx context value passed to callback from calling function.
+ * @param[in] selectedDevice selected device information to performing provisioning.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
+/**
+ * Function to update pstat as Ready for Normal Operation.
+ * This function would update 'isop' from false to true.
+ *
+ * @param[in] ctx context value passed to callback from calling function.
+ * @param[in] selectedDevice selected device information to performing provisioning.
+ * @return OC_STACK_OK on success
+ */
+static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
-static bool IsComplete()
+static bool IsComplete(OTMContext_t* otmCtx)
{
- int i;
- for(i = 0; i < g_resultArraySize; i++)
+ for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
{
- if(OC_STACK_CONTINUE == g_resultArray[i].res)
+ if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
{
return false;
}
return true;
}
-static void SetResult(OTMContext_t* otmCtx, OCStackResult res)
+/**
+ * Function to save the result of provisioning.
+ *
+ * @param[in,out] otmCtx Context value of ownership transfer.
+ * @param[in] res result of provisioning
+ */
+static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
{
- int i;
-
- OC_LOG(DEBUG, TAG, "IN SetResult");
+ OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
if(!otmCtx)
{
- OC_LOG(WARNING, TAG, "OTMContext is NULL");
+ OIC_LOG(WARNING, TAG, "OTMContext is NULL");
+ return;
}
if(otmCtx->selectedDeviceInfo)
{
- for(i = 0; i < g_resultArraySize; i++)
+ //Revert psk_info callback and new deivce uuid in case of random PIN OxM
+ if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
+ {
+ if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
+ {
+ OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
+ }
+ OicUuid_t emptyUuid = { .id={0}};
+ SetUuidForRandomPinOxm(&emptyUuid);
+ }
+
+ for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
{
if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
- g_resultArray[i].deviceId.id, UUID_LENGTH) == 0)
+ otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
{
- g_resultArray[i].res = res;
+ otmCtx->ctxResultArray[i].res = res;
if(OC_STACK_OK != res)
{
- hasError = true;
+ otmCtx->ctxHasError = true;
}
}
}
+ g_otmCtx = NULL;
+
//If all request is completed, invoke the user callback.
- if(IsComplete())
+ if(IsComplete(otmCtx))
{
- g_resultCallback(otmCtx->userCtx, g_resultArraySize, g_resultArray, hasError);
+ otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
+ otmCtx->ctxResultArray, otmCtx->ctxHasError);
+ OICFree(otmCtx->ctxResultArray);
+ OICFree(otmCtx);
}
else
{
- if(OC_STACK_OK != StartOwnershipTransfer(otmCtx->userCtx,
+ if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
otmCtx->selectedDeviceInfo->next))
{
- OC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
+ OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
}
}
}
- OC_LOG(DEBUG, TAG, "OUT SetResult");
-
- OICFree(otmCtx);
+ OIC_LOG(DEBUG, TAG, "OUT SetResult");
}
+/**
+ * Function to handle the handshake result in OTM.
+ * This function will be invoked after DTLS handshake
+ * @param endPoint [IN] The remote endpoint.
+ * @param errorInfo [IN] Error information from the endpoint.
+ * @return NONE
+ */
+void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
+{
+ if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
+ NULL != endpoint && NULL != info)
+ {
+ OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
+ endpoint->addr, endpoint->port, info->result);
+
+ OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
+
+ if(NULL != newDevDoxm)
+ {
+ OicUuid_t emptyUuid = {.id={0}};
+
+ //Make sure the address matches.
+ if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
+ endpoint->addr,
+ sizeof(endpoint->addr)) == 0 &&
+ g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
+ {
+ OCStackResult res = OC_STACK_ERROR;
+
+ //If temporal secure sesstion established successfully
+ if(CA_STATUS_OK == info->result &&
+ false == newDevDoxm->owned &&
+ memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
+ {
+ //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
+ res = PostOwnerUuid(g_otmCtx);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
+ SetResult(g_otmCtx, res);
+ }
+ }
+ //In case of authentication failure
+ else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
+ {
+ //in case of error from owner credential
+ if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
+ true == newDevDoxm->owned)
+ {
+ OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
+
+ if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
+ {
+ OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
+ }
+ SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
+ }
+ //in case of error from wrong PIN, re-start the ownership transfer
+ else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
+ {
+ OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
+
+ memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
+ newDevDoxm->owned = false;
+ g_otmCtx->attemptCnt++;
+
+ if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
+ {
+ res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
+ if(OC_STACK_OK != res)
+ {
+ SetResult(g_otmCtx, res);
+ OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
+ SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
+ SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
+ }
+ }
+ }
+ }
+ }
+}
/**
* Function to save ownerPSK at provisioning tool end.
*
+ * @param[in] selectedDeviceInfo selected device information to performing provisioning.
* @return OC_STACK_OK on success
*/
static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
{
- OC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
+ OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
OCStackResult res = OC_STACK_ERROR;
- CAEndpoint_t endpoint = {0};
+ CAEndpoint_t endpoint;
+ memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
endpoint.port = selectedDeviceInfo->securePort;
- OicUuid_t ptDeviceID = {};
+ OicUuid_t ptDeviceID = {.id={0}};
if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
{
- OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
+ OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
return res;
}
- uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {};
+ uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
+ OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
//Generating OwnerPSK
CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
(uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
- strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)), ptDeviceID.id,
- sizeof(ptDeviceID.id), selectedDeviceInfo->doxm->deviceID.id,
- sizeof(selectedDeviceInfo->doxm->deviceID.id), ownerPSK,
- OWNER_PSK_LENGTH_128);
+ strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
+ ptDeviceID.id, sizeof(ptDeviceID.id),
+ selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
+ ownerPSK, OWNER_PSK_LENGTH_128);
if (CA_STATUS_OK == pskRet)
{
- OC_LOG(INFO, TAG,"ownerPSK dump:\n");
- OC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
+ OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
+ OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
//Generating new credential for provisioning tool
- size_t ownLen = 1;
- uint32_t outLen = 0;
+ OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
+ SYMMETRIC_PAIR_WISE_KEY, NULL,
+ &ownerKey, &ptDeviceID);
+ VERIFY_NON_NULL(TAG, cred, ERROR);
- char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
- B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
- &outLen);
- if (B64_OK == b64Ret)
+ res = AddCredential(cred);
+ if(res != OC_STACK_OK)
{
- OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
- SYMMETRIC_PAIR_WISE_KEY, NULL,
- base64Buff, ownLen, &ptDeviceID);
- if (cred)
- {
- //Update the SVR database.
- if (OC_STACK_OK == AddCredential(cred))
- {
- res = OC_STACK_OK;
- }
- else
- {
- OC_LOG(ERROR, TAG, "AddCredential failed");
- }
- }
- else
- {
- OC_LOG(ERROR, TAG, "GenerateCredential failed");
- }
- }
- else
- {
- OC_LOG(ERROR, TAG, "b64Encode failed");
+ DeleteCredList(cred);
+ return res;
}
}
else
{
- OC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
+ OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
}
- OC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
-
+ OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
+exit:
return res;
}
/**
* Callback handler for OwnerShipTransferModeHandler API.
*
- * @param[in] ctx IP of target device.
- * @param[in] handle port of remote server.
- * @param[in] clientResponse adapter type of endpoint.
- * @param[in] doxm pointer to doxm instance.
- * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
- * OC_STACK_DELETE_TRANSACTION to delete it.
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
*/
-static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse)
+static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
{
- if (clientResponse)
+ OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
+
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
+
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+ (void)UNUSED;
+ if(clientResponse->result == OC_STACK_OK)
{
- OC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
+ OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
+ //Send request : GET /oic/sec/pstat
+ OCStackResult res = GetProvisioningStatusResource(otmCtx);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(WARNING, TAG, "Failed to get pstat information");
+ SetResult(otmCtx, res);
+ }
+ }
+ else
+ {
+ OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
+ clientResponse->result);
+ SetResult(otmCtx, clientResponse->result);
+ }
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if(!otmCtx)
+ OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
+
+exit:
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+/**
+ * Callback handler for ProvisioningStatusResouceHandler API.
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
+
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
+
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+ (void)UNUSED;
+ if (OC_STACK_OK == clientResponse->result)
+ {
+ if (NULL == clientResponse->payload)
{
- OC_LOG(ERROR, TAG, "Can't find OTMContext");
+ OIC_LOG(INFO, TAG, "Skiping Null payload");
+ SetResult(otmCtx, OC_STACK_ERROR);
return OC_STACK_DELETE_TRANSACTION;
}
- if(clientResponse->result == OC_STACK_OK)
+ if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
{
- OC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
- //Send request : GET /oic/sec/pstat
- OCStackResult res = GetProvisioningStatusResource(otmCtx);
- if(OC_STACK_OK != res)
- {
-
- OC_LOG(WARNING, TAG, "Failed to getting pstat information");
- SetResult(otmCtx, res);
- return OC_STACK_DELETE_TRANSACTION;
- }
+ OIC_LOG(INFO, TAG, "Unknown payload type");
+ SetResult(otmCtx, OC_STACK_ERROR);
+ return OC_STACK_DELETE_TRANSACTION;
}
- else
+ OicSecPstat_t* pstat = NULL;
+ OCStackResult result = CBORPayloadToPstat(
+ ((OCSecurityPayload*)clientResponse->payload)->securityData,
+ ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
+ &pstat);
+ if(NULL == pstat || result != OC_STACK_OK)
{
-
- OC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
- clientResponse->result);
- SetResult(otmCtx, clientResponse->result);
+ OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
+ SetResult(otmCtx, OC_STACK_ERROR);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ if(false == (TAKE_OWNER & pstat->cm))
+ {
+ OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
+ SetResult(otmCtx, OC_STACK_ERROR);
return OC_STACK_DELETE_TRANSACTION;
}
- OC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
+ otmCtx->selectedDeviceInfo->pstat = pstat;
+
+ //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
+ SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
+
+ //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
+ OCStackResult res = PostUpdateOperationMode(otmCtx);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
+ SetResult(otmCtx, res);
+ }
+ }
+ else
+ {
+ OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
+ clientResponse->result);
+ SetResult(otmCtx, clientResponse->result);
}
+ OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
+exit:
return OC_STACK_DELETE_TRANSACTION;
}
/**
- * Callback handler for ProvisioningStatusResouceHandler API.
+ * Response handler for update owner uuid request.
*
- * @param[in] ctx IP of target device.
- * @param[in] handle port of remote server.
- * @param[in] clientResponse adapter type of endpoint.
- * @param[in] doxm pointer to doxm instance.
- * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
- * OC_STACK_DELETE_TRANSACTION to delete it.
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
*/
-static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse)
+static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
{
- if (clientResponse)
- {
- OC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if(!otmCtx)
- {
- OC_LOG(ERROR, TAG, "Can't find OTMContext");
- return OC_STACK_DELETE_TRANSACTION;
- }
+ OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
+ (void)UNUSED;
+ OCStackResult res = OC_STACK_OK;
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if (OC_STACK_OK == clientResponse->result)
+ if(OC_STACK_OK == clientResponse->result)
+ {
+ if(otmCtx && otmCtx->selectedDeviceInfo)
{
- if (NULL == clientResponse->payload)
+ res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
+ if(OC_STACK_OK != res)
{
- OC_LOG(INFO, TAG, "Skiping Null payload");
- SetResult(otmCtx, OC_STACK_ERROR);
+ OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
+ SetResult(otmCtx, res);
return OC_STACK_DELETE_TRANSACTION;
}
- if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
- {
- OC_LOG(INFO, TAG, "Unknown payload type");
- SetResult(otmCtx, OC_STACK_ERROR);
- return OC_STACK_KEEP_TRANSACTION;
- }
-
- OicSecPstat_t* pstat = JSONToPstatBin(
- ((OCSecurityPayload*)clientResponse->payload)->securityData);
- if(NULL == pstat)
+ //PUT owner credential to new device according to security spec B.
+ res = PutOwnerCredential(otmCtx);
+ if(OC_STACK_OK != res)
{
- OC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
- SetResult(otmCtx, OC_STACK_ERROR);
+ OIC_LOG(ERROR, TAG,
+ "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
+ SetResult(otmCtx, res);
return OC_STACK_DELETE_TRANSACTION;
}
- otmCtx->selectedDeviceInfo->pstat = pstat;
+ }
+ }
+ else
+ {
+ res = clientResponse->result;
+ OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
+ SetResult(otmCtx, res);
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
+
+exit:
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+/**
+ * Response handler for update operation mode.
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
- //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
- OicSecDpom_t selectedOperationMode;
- SelectOperationMode(otmCtx->selectedDeviceInfo, &selectedOperationMode);
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
- //Send request : PUT /oic/sec/pstat [{"OM":"0x11", .. }]
- OCStackResult res = PutUpdateOperationMode(otmCtx, selectedOperationMode);
- if (OC_STACK_OK != res)
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+ (void) UNUSED;
+ if (OC_STACK_OK == clientResponse->result)
+ {
+ OCStackResult res = OC_STACK_ERROR;
+ OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
+ //DTLS Handshake
+ //Load secret for temporal secure session.
+ if(g_OTMDatas[selOxm].loadSecretCB)
+ {
+ res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
+ if(OC_STACK_OK != res)
{
- OC_LOG(ERROR, TAG, "Error while updating operation mode.");
+ OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
SetResult(otmCtx, res);
-
+ return OC_STACK_DELETE_TRANSACTION;
}
}
- else
+
+ //It will be used in handshake event handler
+ g_otmCtx = otmCtx;
+
+ //Try DTLS handshake to generate secure session
+ if(g_OTMDatas[selOxm].createSecureSessionCB)
{
- OC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
- clientResponse->result);
- SetResult(otmCtx, clientResponse->result);
+ res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
+ SetResult(otmCtx, res);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
}
-
- OC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Error while update operation mode");
+ SetResult(otmCtx, clientResponse->result);
}
+ OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
+
+exit:
return OC_STACK_DELETE_TRANSACTION;
}
/**
- * Callback handler for OwnershipInformationHandler API.
+ * Response handler for update owner crendetial request.
*
- * @param[in] ctx IP of target device.
- * @param[in] handle port of remote server.
- * @param[in] clientResponse adapter type of endpoint.
- * @param[in] doxm pointer to doxm instance.
- * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
- * OC_STACK_DELETE_TRANSACTION to delete it.
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
*/
-static OCStackApplicationResult OwnershipInformationHandler(void *ctx,
- OCDoHandle handle, OCClientResponse *clientResponse)
+static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
{
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
- if (clientResponse)
- {
- OC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
+ OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
+ (void)UNUSED;
+ OCStackResult res = OC_STACK_OK;
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- OCStackResult res = OC_STACK_OK;
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if (OC_STACK_OK == clientResponse->result)
+ if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
+ {
+ if(otmCtx && otmCtx->selectedDeviceInfo)
{
- res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
- if(OC_STACK_OK != res)
+ //Close the temporal secure session to verify the owner credential
+ CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
+ endpoint->port = otmCtx->selectedDeviceInfo->securePort;
+ CAResult_t caResult = CACloseDtlsSession(endpoint);
+ if(CA_STATUS_OK != caResult)
{
- OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to owner PSK generation");
- SetResult(otmCtx, res);
+ OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
+ SetResult(otmCtx, caResult);
return OC_STACK_DELETE_TRANSACTION;
}
- CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
- endpoint->port = otmCtx->selectedDeviceInfo->securePort;
- CAResult_t closeRes = CACloseDtlsSession(endpoint);
- if(CA_STATUS_OK != closeRes)
+ /**
+ * If we select NULL cipher,
+ * client will select appropriate cipher suite according to server's cipher-suite list.
+ */
+ caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
+ if(CA_STATUS_OK != caResult)
{
- OC_LOG(ERROR, TAG, "Failed to close DTLS session");
- SetResult(otmCtx, closeRes);
+ OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
+ SetResult(otmCtx, caResult);
return OC_STACK_DELETE_TRANSACTION;
}
- // TODO: PIN based OxM is required.
- /*
- if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
+ /**
+ * in case of random PIN based OxM,
+ * revert get_psk_info callback of tinyDTLS to use owner credential.
+ */
+ if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
{
- res = RemoveCredential(otmCtx->tempCredId);
- if(OC_STACK_OK != res)
+ OicUuid_t emptyUuid = { .id={0}};
+ SetUuidForRandomPinOxm(&emptyUuid);
+
+ if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
{
- OC_LOG_V(ERROR, TAG, "Failed to remove temporal PSK : %d", res);
+ OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
+ SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
return OC_STACK_DELETE_TRANSACTION;
}
}
- */
- OC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
- OC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
- res = FinalizeProvisioning(otmCtx);
+ //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
+ res = PostOwnershipInformation(otmCtx);
if(OC_STACK_OK != res)
{
+ OIC_LOG(ERROR, TAG, "Failed to post ownership information to new device");
SetResult(otmCtx, res);
return OC_STACK_DELETE_TRANSACTION;
}
}
- else
- {
- res = clientResponse->result;
- }
-
- OC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
}
+ else
+ {
+ res = clientResponse->result;
+ OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
+ SetResult(otmCtx, res);
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
+exit:
return OC_STACK_DELETE_TRANSACTION;
}
+
/**
- * Response handler for update operation mode.
+ * Response handler for update owner information request.
*
- * @param[in] object Remote endpoint object
- * @param[in] requestInfo Datastructure containing request information.
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
*/
-static OCStackApplicationResult OperationModeUpdateHandler(void *ctx,
- OCDoHandle handle, OCClientResponse *clientResponse)
+static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
{
- if (clientResponse)
- {
- OC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
+ VERIFY_NON_NULL(TAG, clientResponse, WARNING);
+ VERIFY_NON_NULL(TAG, ctx, WARNING);
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if (OC_STACK_OK == clientResponse->result)
+ OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
+ (void)UNUSED;
+ OCStackResult res = OC_STACK_OK;
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+
+ if(OC_STACK_OK == clientResponse->result)
+ {
+ if(otmCtx && otmCtx->selectedDeviceInfo)
{
- OCStackResult res = OC_STACK_ERROR;
- OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
- //DTLS Handshake
- //Load secret for temporal secure session.
- if(g_OTMDatas[selOxm].loadSecretCB)
- {
- res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
- if(OC_STACK_OK != res)
- {
- OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
- SetResult(otmCtx, res);
- return OC_STACK_DELETE_TRANSACTION;
- }
- }
+ OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
+ OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
- //Try DTLS handshake to generate secure session
- if(g_OTMDatas[selOxm].createSecureSessionCB)
+ res = PostProvisioningStatus(otmCtx);
+ if(OC_STACK_OK != res)
{
- res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
- if(OC_STACK_OK != res)
- {
- OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
- SetResult(otmCtx, res);
- return OC_STACK_DELETE_TRANSACTION;
- }
+ OIC_LOG(ERROR, TAG, "Failed to update pstat");
+ SetResult(otmCtx, res);
}
+ }
+ }
+ else
+ {
+ res = clientResponse->result;
+ OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
+ SetResult(otmCtx, res);
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
- //Send request : PUT /oic/sec/doxm [{"Owned":"True", .. , "Owner":"PT's UUID"}]
- res = PutOwnershipInformation(otmCtx);
+exit:
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+/**
+ * Response handler of update provisioning status.
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
+
+ VERIFY_NON_NULL(TAG, clientResponse, ERROR);
+ VERIFY_NON_NULL(TAG, ctx, ERROR);
+
+ OTMContext_t* otmCtx = (OTMContext_t*) ctx;
+ (void)UNUSED;
+ OCStackResult res = OC_STACK_OK;
+
+ if(OC_STACK_OK == clientResponse->result)
+ {
+ if(otmCtx && otmCtx->selectedDeviceInfo)
+ {
+ OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
+
+ res = PostNormalOperationStatus(otmCtx);
if(OC_STACK_OK != res)
{
- OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
+ OIC_LOG(ERROR, TAG, "Failed to update pstat");
SetResult(otmCtx, res);
- return OC_STACK_DELETE_TRANSACTION;
}
}
- else
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
+ clientResponse->result);
+ SetResult(otmCtx, clientResponse->result);
+ }
+
+exit:
+ OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+/**
+ * Response handler of update provisioning status to Ready for Normal..
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
+
+ VERIFY_NON_NULL(TAG, clientResponse, ERROR);
+ VERIFY_NON_NULL(TAG, ctx, ERROR);
+
+ OTMContext_t* otmCtx = (OTMContext_t*) ctx;
+ (void)UNUSED;
+
+ if (OC_STACK_OK == clientResponse->result)
+ {
+ OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
+ OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
+ if (OC_STACK_OK == res)
+ {
+ OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
+ SetResult(otmCtx, OC_STACK_OK);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
+ }
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
+ clientResponse->result);
+ SetResult(otmCtx, clientResponse->result);
+ }
+
+exit:
+ OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
+{
+ OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
+
+ if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ {
+ OIC_LOG(ERROR, TAG, "Invalid parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+
+ if(!PMGenerateQuery(true,
+ deviceInfo->endpoint.addr, deviceInfo->securePort,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_CRED_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+ OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
+ if(!secPayload)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ //Generate owner credential for new device
+ secPayload->base.type = PAYLOAD_TYPE_SECURITY;
+ const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
+ if(!ownerCredential)
+ {
+ OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
+ return OC_STACK_NO_RESOURCE;
+ }
+
+ OicUuid_t credSubjectId = {.id={0}};
+ if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
+ {
+ OicSecCred_t newCredential;
+ memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
+ newCredential.next = NULL;
+
+ //Set subject ID as PT's ID
+ memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
+
+ //Fill private data as empty string
+ newCredential.privateData.data = NULL;
+ newCredential.privateData.len = 0;
+#ifdef __WITH_X509__
+ newCredential.publicData.data = NULL;
+ newCredential.publicData.len = 0;
+#endif
+
+ //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
+ if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData, &secPayload->payloadSize))
+ {
+ OICFree(secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG(DEBUG, TAG, "Cred Payload:");
+ OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
+
+ OCCallbackData cbData;
+ cbData.cb = &OwnerCredentialHandler;
+ cbData.context = (void *)otmCtx;
+ cbData.cd = NULL;
+ OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
+ &deviceInfo->endpoint, (OCPayload*)secPayload,
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if (res != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "Error while update operation mode");
- SetResult(otmCtx, clientResponse->result);
- return OC_STACK_DELETE_TRANSACTION;
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
+ return OC_STACK_NO_RESOURCE;
}
- return OC_STACK_DELETE_TRANSACTION;
-}
+ OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
+ return OC_STACK_OK;
+}
-static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
+static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
{
- OC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
+ OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
if(!otmCtx || !otmCtx->selectedDeviceInfo)
{
- OC_LOG(ERROR, TAG, "Invailed parameters");
+ OIC_LOG(ERROR, TAG, "Invalid parameters");
return OC_STACK_INVALID_PARAM;
}
OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
- char query[MAX_QUERY_LENGTH] = {};
- sprintf(query, "%s%s:%d%s", COAP_PREFIX,
- deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
- OIC_RSRC_DOXM_URI);
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+
+ if(!PMGenerateQuery(false,
+ deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_DOXM_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
if(!secPayload)
{
- OC_LOG(ERROR, TAG, "Failed to memory allocation");
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
- if (NULL == secPayload->securityData)
+ OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
+ &secPayload->securityData, &secPayload->payloadSize);
+ if (OC_STACK_OK != res && NULL == secPayload->securityData)
{
- OC_LOG(ERROR, TAG, "Error while converting bin to json");
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
return OC_STACK_ERROR;
}
- OC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
OCCallbackData cbData;
cbData.cb = &OwnerTransferModeHandler;
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
-
- // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
- &deviceInfo->endpoint, (OCPayload*)secPayload,
- CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_POST, query,
+ &deviceInfo->endpoint, (OCPayload *)secPayload,
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
+ OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
return res;
}
-/**
- * Function to send request to resource to get its pstat resource information.
- *
- * @param[in] timeout timeout for operation.
- * @param[in] deviceInfo Device Info.
- * @return OC_STACK_OK on success
- */
static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
{
- OC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
+ OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
if(!otmCtx || !otmCtx->selectedDeviceInfo)
{
- OC_LOG(ERROR, TAG, "Invailed parameters");
+ OIC_LOG(ERROR, TAG, "Invailed parameters");
return OC_STACK_INVALID_PARAM;
}
OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
- char query[MAX_QUERY_LENGTH] = {};
- sprintf(query, "%s%s:%d%s", COAP_PREFIX,
- deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
- OIC_RSRC_PSTAT_URI);
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(false,
+ deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
OCCallbackData cbData;
cbData.cb = &ListMethodsHandler;
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
-
- // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
- CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
+ OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
return res;
}
-
-/**
- * Function to send ownerShip info. This function would update Owned as true and
- * owner as UUID for provisioning tool
- *
- * @param[in] timeout timeout value for the operation.
- * @param[in] deviceInfo provisioning context.
- * @return OC_STACK_OK on success
- */
-static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
+static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
{
- OC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
+ OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
if(!otmCtx || !otmCtx->selectedDeviceInfo)
{
- OC_LOG(ERROR, TAG, "Invailed parameters");
+ OIC_LOG(ERROR, TAG, "Invailed parameters");
return OC_STACK_INVALID_PARAM;
}
OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
- char query[MAX_QUERY_LENGTH] = {};
- sprintf(query, "%s%s:%d%s", COAPS_PREFIX,
- deviceInfo->endpoint.addr, deviceInfo->securePort,
- OIC_RSRC_DOXM_URI);
- //OwnershipInformationHandler
- OicSecOxm_t selOxm = deviceInfo->doxm->oxmSel;
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ deviceInfo->endpoint.addr, deviceInfo->securePort,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_DOXM_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ //Post PT's uuid to new device
OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
if(!secPayload)
{
- OC_LOG(ERROR, TAG, "Failed to memory allocation");
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = g_OTMDatas[selOxm].createOwnerTransferPayloadCB(otmCtx);
- if (NULL == secPayload->securityData)
+ OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
+ otmCtx, &secPayload->securityData, &secPayload->payloadSize);
+ if (OC_STACK_OK != res && NULL == secPayload->securityData)
{
- OC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
+ return OC_STACK_INVALID_PARAM;
+ }
+ OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
+
+ OCCallbackData cbData;
+ cbData.cb = &OwnerUuidUpdateHandler;
+ cbData.context = (void *)otmCtx;
+ cbData.cd = NULL;
+
+ res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if (res != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
+
+ return res;
+}
+
+static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
+{
+ OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
+
+ if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ {
+ OIC_LOG(ERROR, TAG, "Invailed parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ deviceInfo->endpoint.addr, deviceInfo->securePort,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_DOXM_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ //OwnershipInformationHandler
+ OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
+ if (!secPayload)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ otmCtx->selectedDeviceInfo->doxm->owned = true;
+
+ secPayload->base.type = PAYLOAD_TYPE_SECURITY;
+ OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
+ &secPayload->securityData, &secPayload->payloadSize);
+ if (OC_STACK_OK != res && NULL == secPayload->securityData)
+ {
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
return OC_STACK_INVALID_PARAM;
}
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
- // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
- CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
+ OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
return res;
}
-/**
- * Function to update the operation mode. As per the spec. Operation mode in client driven
- * single service provisioning it will be updated to 0x3
- *
- * @param[in] timeout timeout for operation.
- * @param[in] deviceInfo Device Info.
- * @return OC_STACK_OK on success
- */
-static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
- OicSecDpom_t selectedOperationMode)
+static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
{
- OC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
+ OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
if(!otmCtx || !otmCtx->selectedDeviceInfo)
{
}
OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
- char query[MAX_QUERY_LENGTH] = {};
- sprintf(query, "%s%s:%d%s", COAP_PREFIX,
- deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
- OIC_RSRC_PSTAT_URI);
- deviceInfo->pstat->om = selectedOperationMode;
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(false,
+ deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
+ deviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
if(!secPayload)
{
- OC_LOG(ERROR, TAG, "Failed to memory allocation");
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
- if (NULL == secPayload->securityData)
+ OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
+ &secPayload->payloadSize);
+ if (OC_STACK_OK != res)
{
- OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
+ OCPayloadDestroy((OCPayload *)secPayload);
+ OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
return OC_STACK_INVALID_PARAM;
}
cbData.cb = &OperationModeUpdateHandler;
cbData.context = (void *)otmCtx;
cbData.cd = NULL;
-
- OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
- CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
+ res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
+ deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
if (res != OC_STACK_OK)
{
- OC_LOG(ERROR, TAG, "OCStack resource error");
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
+ OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
return res;
}
static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
{
- OC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
- OTMContext_t* otmCtx = (OTMContext_t*)OICMalloc(sizeof(OTMContext_t));
- if(!otmCtx)
- {
- OC_LOG(ERROR, TAG, "Failed to create OTM Context");
- return OC_STACK_NO_MEMORY;
- }
- otmCtx->userCtx = ctx;
+ OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
+ OTMContext_t* otmCtx = (OTMContext_t*)ctx;
otmCtx->selectedDeviceInfo = selectedDevice;
//Set to the lowest level OxM, and then find more higher level OxM.
&selectedDevice->doxm->oxmSel);
if(OC_STACK_OK != res)
{
- OC_LOG(ERROR, TAG, "Failed to select the provisioning method");
+ OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
SetResult(otmCtx, res);
return res;
}
- OC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
+ OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
- //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
- res = PutOwnerTransferModeToResource(otmCtx);
+ //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
+ res = PostOwnerTransferModeToResource(otmCtx);
if(OC_STACK_OK != res)
{
- OC_LOG(WARNING, TAG, "Failed to select the provisioning method");
+ OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
SetResult(otmCtx, res);
return res;
}
- OC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
+ //Register DTLS event handler to catch the dtls event while handshake
+ if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
+ {
+ OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
+ }
+
+ OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
return res;
OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
{
- OC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
+ OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
if(!data)
{
- OC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
+ OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
return OC_STACK_INVALID_PARAM;
}
if(oxmType >= OIC_OXM_COUNT)
{
- OC_LOG(INFO, TAG, "Unknow ownership transfer method");
+ OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
return OC_STACK_INVALID_PARAM;
}
g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
- OC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
+ OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
return OC_STACK_OK;
}
OCProvisionDev_t *selectedDevicelist,
OCProvisionResultCB resultCallback)
{
- OC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
+ OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
- if (NULL == selectedDevicelist || NULL == resultCallback )
+ if (NULL == selectedDevicelist)
{
return OC_STACK_INVALID_PARAM;
}
+ if (NULL == resultCallback)
+ {
+ return OC_STACK_INVALID_CALLBACK;
+ }
- g_resultCallback = resultCallback;
- hasError = false;
-
+ OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
+ if(!otmCtx)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
+ return OC_STACK_NO_MEMORY;
+ }
+ otmCtx->ctxResultCallback = resultCallback;
+ otmCtx->ctxHasError = false;
+ otmCtx->userCtx = ctx;
OCProvisionDev_t* pCurDev = selectedDevicelist;
//Counting number of selected devices.
- g_resultArraySize = 0;
+ otmCtx->ctxResultArraySize = 0;
while(NULL != pCurDev)
{
- g_resultArraySize++;
+ otmCtx->ctxResultArraySize++;
pCurDev = pCurDev->next;
}
- if(g_resultArray)
- {
- OICFree(g_resultArray);
- }
- g_resultArray =
- (OCProvisionResult_t*)OICMalloc(sizeof(OCProvisionResult_t) * g_resultArraySize);
- if(NULL == g_resultArray)
+ otmCtx->ctxResultArray =
+ (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
+ if(NULL == otmCtx->ctxResultArray)
{
- OC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
+ OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
+ OICFree(otmCtx);
return OC_STACK_NO_MEMORY;
}
-
- int devIdx = 0;
pCurDev = selectedDevicelist;
+
+ OCStackResult res = OC_STACK_OK;
//Fill the device UUID for result array.
- for(devIdx = 0; devIdx < g_resultArraySize; devIdx++)
+ for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
{
- memcpy(g_resultArray[devIdx].deviceId.id,
+ //Checking duplication of Device ID.
+ bool isDuplicate = true;
+ res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
+ if (OC_STACK_OK != res)
+ {
+ goto error;
+ }
+ if (isDuplicate)
+ {
+ OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
+ res = OC_STACK_INVALID_PARAM;
+ goto error;
+ }
+ memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
pCurDev->doxm->deviceID.id,
UUID_LENGTH);
- g_resultArray[devIdx].res = OC_STACK_CONTINUE;
+ otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
pCurDev = pCurDev->next;
}
- StartOwnershipTransfer(ctx, selectedDevicelist);
+ StartOwnershipTransfer(otmCtx, selectedDevicelist);
- OC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
+ OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
+ return OC_STACK_OK;
- return (hasError ? OC_STACK_ERROR : OC_STACK_OK);
+error:
+ OICFree(otmCtx->ctxResultArray);
+ OICFree(otmCtx);
+ return res;
}
-/**
- * Callback handler of SRPFinalizeProvisioning.
- *
- * @param[in] ctx ctx value passed to callback from calling function.
- * @param[in] handle handle to an invocation
- * @param[in] clientResponse Response from queries to remote servers.
- * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
- * and OC_STACK_KEEP_TRANSACTION to keep it.
- */
-static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse)
+OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
{
- if (clientResponse)
- {
- OC_LOG_V(INFO, TAG, "IN FinalizeProvisioningCB.");
- OTMContext_t* otmCtx = (OTMContext_t*)ctx;
- if(!otmCtx)
- {
- OC_LOG(ERROR, TAG, "OTMContext is NULL");
- return OC_STACK_DELETE_TRANSACTION;
- }
+ OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
- if(OC_STACK_OK == clientResponse->result)
- {
- SetResult(otmCtx, OC_STACK_OK);
- return OC_STACK_DELETE_TRANSACTION;
- }
+ if(!otmCtx || !otmCtx->selectedDeviceInfo)
+ {
+ OIC_LOG(ERROR, TAG, "OTMContext is NULL");
+ return OC_STACK_INVALID_PARAM;
}
- return OC_STACK_DELETE_TRANSACTION;
-}
-
-/**
- * Callback handler of default ACL provisioning.
- *
- * @param[in] ctx ctx value passed to callback from calling function.
- * @param[in] handle handle to an invocation
- * @param[in] clientResponse Response from queries to remote servers.
- * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
- * and OC_STACK_KEEP_TRANSACTION to keep it.
- */
-static OCStackApplicationResult ProvisionDefaultACLCB(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse)
-{
- OC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");
+ //Change the TAKE_OWNER bit of CM to 0.
+ otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
- if (clientResponse)
+ OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
+ if (!secPayload)
{
- OTMContext_t* otmCtx = (OTMContext_t*) ctx;
- if(!otmCtx)
- {
- OC_LOG(ERROR, TAG, "OTMContext is NULL");
- return OC_STACK_DELETE_TRANSACTION;
- }
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
+ return OC_STACK_NO_MEMORY;
+ }
+ secPayload->base.type = PAYLOAD_TYPE_SECURITY;
+ if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
+ &secPayload->securityData, &secPayload->payloadSize))
+ {
+ OCPayloadDestroy((OCPayload *)secPayload);
+ return OC_STACK_INVALID_JSON;
+ }
+ OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
+ OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
+
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ otmCtx->selectedDeviceInfo->endpoint.addr,
+ otmCtx->selectedDeviceInfo->securePort,
+ otmCtx->selectedDeviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
- if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
- {
- OC_LOG_V(INFO, TAG, "Staring commit hash task.");
- // TODO hash currently have fixed value 0.
- uint16_t aclHash = 0;
- otmCtx->selectedDeviceInfo->pstat->commitHash = aclHash;
- otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
- OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
- if(!secPayload)
- {
- OC_LOG(ERROR, TAG, "Failed to memory allocation");
- return OC_STACK_NO_MEMORY;
- }
- secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
- if (NULL == secPayload->securityData)
- {
- SetResult(otmCtx, OC_STACK_INVALID_JSON);
- return OC_STACK_DELETE_TRANSACTION;
- }
- OC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData);
-
- char uri[MAX_QUERY_LENGTH] = { 0 };
- size_t uriLen = sizeof(uri);
-
- snprintf(uri, uriLen - 1, COAPS_QUERY, otmCtx->selectedDeviceInfo->endpoint.addr,
- otmCtx->selectedDeviceInfo->securePort, OIC_RSRC_PSTAT_URI);
- uri[uriLen - 1] = '\0';
- OCCallbackData cbData = {0,};
- cbData.cb = &FinalizeProvisioningCB;
- cbData.context = (void*)otmCtx; // forward context to SRPFinalizeProvisioningCB
- cbData.cd = NULL;
-
- // TODO change value of CT_ADAPTER_IP with val from discovery
- OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, uri, 0, (OCPayload*)secPayload,
- CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
- OC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
- if (ret != OC_STACK_OK)
- {
- OC_LOG(ERROR, TAG, "OCStack resource error");
- SetResult(otmCtx, ret);
- return OC_STACK_DELETE_TRANSACTION;
- }
- return OC_STACK_DELETE_TRANSACTION;
- }
- else
- {
- OC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
- clientResponse->result);
- SetResult(otmCtx, clientResponse->result);
- }
+ OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
+ cbData.cb = &ProvisioningStatusHandler;
+ cbData.context = (void*)otmCtx;
+ cbData.cd = NULL;
+ OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
+ otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
+ if (ret != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- return OC_STACK_DELETE_TRANSACTION;
-}
+ OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
+ return ret;
+}
-OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx)
+OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
{
- OC_LOG(INFO, TAG, "IN FinalizeProvisioning");
+ OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
- if(!otmCtx)
- {
- OC_LOG(ERROR, TAG, "OTMContext is NULL");
- return OC_STACK_INVALID_PARAM;
- }
- if(!otmCtx->selectedDeviceInfo)
+ if(!otmCtx || !otmCtx->selectedDeviceInfo)
{
- OC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
- OICFree(otmCtx);
+ OIC_LOG(ERROR, TAG, "OTMContext is NULL");
return OC_STACK_INVALID_PARAM;
}
- // Provision Default ACL to device
- OicSecAcl_t defaultAcl =
- { {},
- 1,
- NULL,
- 0x001F,
- 0,
- NULL,
- NULL,
- 1,
- NULL,
- NULL,
- };
-
- OicUuid_t provTooldeviceID = {{0,}};
- if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
- {
- OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
- SetResult(otmCtx, OC_STACK_ERROR);
- return OC_STACK_ERROR;
- }
- OC_LOG(INFO, TAG, "Retieved deviceID");
- memcpy(defaultAcl.subject.id, provTooldeviceID.id, sizeof(defaultAcl.subject.id));
- char *wildCardResource = "*";
- defaultAcl.resources = &wildCardResource;
- defaultAcl.owners = (OicUuid_t *) OICCalloc(1, UUID_LENGTH);
- if(!defaultAcl.owners)
- {
- OC_LOG(ERROR, TAG, "Failed to memory allocation for default ACL");
- SetResult(otmCtx, OC_STACK_NO_MEMORY);
- return OC_STACK_NO_MEMORY;
- }
- memcpy(defaultAcl.owners->id, provTooldeviceID.id, UUID_LENGTH);
- OC_LOG(INFO, TAG, "Provisioning default ACL");
+ //Set isop to true.
+ otmCtx->selectedDeviceInfo->pstat->isOp = true;
- OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
- if(!secPayload)
+ OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
+ if (!secPayload)
{
- OC_LOG(ERROR, TAG, "Failed to memory allocation");
+ OIC_LOG(ERROR, TAG, "Failed to memory allocation");
return OC_STACK_NO_MEMORY;
}
secPayload->base.type = PAYLOAD_TYPE_SECURITY;
- secPayload->securityData = BinToAclJSON(&defaultAcl);
- OICFree(defaultAcl.owners);
- if(!secPayload->securityData)
+ if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
+ &secPayload->securityData, &secPayload->payloadSize))
{
- OC_LOG(INFO, TAG, "FinalizeProvisioning : Failed to BinToAclJSON");
- SetResult(otmCtx, OC_STACK_ERROR);
+ OCPayloadDestroy((OCPayload *)secPayload);
+ return OC_STACK_INVALID_JSON;
+ }
+ OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
+ OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
+
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ otmCtx->selectedDeviceInfo->endpoint.addr,
+ otmCtx->selectedDeviceInfo->securePort,
+ otmCtx->selectedDeviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
return OC_STACK_ERROR;
}
- OC_LOG_V(INFO, TAG, "Provisioning default ACL : %s",secPayload->securityData);
-
- char uri[MAX_QUERY_LENGTH] = { 0 };
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
- size_t uriLen = sizeof(uri);
- snprintf(uri, uriLen - 1, COAPS_QUERY, otmCtx->selectedDeviceInfo->endpoint.addr,
- otmCtx->selectedDeviceInfo->securePort, OIC_RSRC_ACL_URI);
- uri[uriLen - 1] = '\0';
- OC_LOG_V(INFO, TAG, "Request URI for Provisioning default ACL : %s",uri);
-
- OCCallbackData cbData = {};
- cbData.cb = &ProvisionDefaultACLCB;
- cbData.context = (void *)otmCtx;
+ OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
+ cbData.cb = &ReadyForNomalStatusHandler;
+ cbData.context = (void*)otmCtx;
cbData.cd = NULL;
-
- OCStackResult ret = OCDoResource(NULL, OC_REST_POST, uri,
- &otmCtx->selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
- CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
- if (OC_STACK_OK != ret)
+ OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
+ otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
+ if (ret != OC_STACK_OK)
{
- SetResult(otmCtx, ret);
- return ret;
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
}
- OC_LOG(INFO, TAG, "OUT FinalizeProvisioning");
+ OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");
return ret;
-
}
-