/**
 * 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)
{
    OC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");

    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)
    {
        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;
            }
        }

        //Try DTLS handshake to generate secure session
        if(g_OTMDatas[selOxm].createSecureSessionCB)
        {
            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;
            }
        }

        //Send request : PUT /oic/sec/doxm [{"Owned":"True", .. , "Owner":"PT's UUID"}]
        res = PutOwnershipInformation(otmCtx);
        if(OC_STACK_OK != res)
        {
            OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
            SetResult(otmCtx, res);
        }
    }
    else
    {
        OC_LOG(ERROR, TAG, "Error while update operation mode");
        SetResult(otmCtx, clientResponse->result);
    }

    OC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");

exit:
    return  OC_STACK_DELETE_TRANSACTION;
}
Ejemplo n.º 2
0
/**
 * 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(g_otmCtx && endpoint && info)
    {
        OC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
                 endpoint->addr, endpoint->port, info->result);

        //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;

            CARegisterDTLSHandshakeCallback(NULL);

            //In case of success, send next coaps request.
            if(CA_STATUS_OK == info->result)
            {
                //Send request : PUT /oic/sec/doxm [{"Owned":"True", .. , "Owner":"PT's UUID"}]
                res = PutOwnershipInformation(g_otmCtx);
                if(OC_STACK_OK != res)
                {
                    OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
                    SetResult(g_otmCtx, res);
                }
            }
            //In case of failure, re-start the ownership transfer in case of PIN OxM
            else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
            {
                g_otmCtx->selectedDeviceInfo->doxm->owned = false;
                g_otmCtx->attemptCnt++;

                if(g_otmCtx->selectedDeviceInfo->doxm->oxmSel == OIC_RANDOM_DEVICE_PIN)
                {
                    res = RemoveCredential(&g_otmCtx->subIdForPinOxm);
                    if(OC_STACK_RESOURCE_DELETED != res)
                    {
                        OC_LOG_V(ERROR, TAG, "Failed to remove temporal PSK : %d", res);
                        SetResult(g_otmCtx, res);
                        return;
                    }

                    if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
                    {
                        res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
                        if(OC_STACK_OK != res)
                        {
                            SetResult(g_otmCtx, res);
                            OC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
                        }
                    }
                    else
                    {
                        SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
                    }
                }
                else
                {
                    SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
                }
            }
        }
    }
}