// request and block option1
TEST_F(CABlockTransferTests, CAAddBlockOptionTest)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t requestData;
    memset(&requestData, 0, sizeof(CAInfo_t));
    requestData.token = tempToken;
    requestData.tokenLength = CA_MAX_TOKEN_LEN;

    requestData.payload = (CAPayload_t) calloc(LARGE_PAYLOAD_LENGTH, sizeof(char));
    if(!requestData.payload)
    {
        CADestroyToken(tempToken);
        FAIL() << "requestData.payload allocation failed";
    }
    memset(requestData.payload, '1', sizeof(requestData.payload) - 1);
    requestData.payloadSize = sizeof(requestData.payload);
    requestData.type = CA_MSG_NONCONFIRM;

    pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport);

    EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption(&pdu, &requestData, tempRep, &options));

    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
Beispiel #2
0
static CAResult_t CAProcessSendData(const CAData_t *data)
{
    VERIFY_NON_NULL(data, TAG, "data");
    VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");

    CAResult_t res = CA_STATUS_FAILED;

    CASendDataType_t type = data->type;

    coap_pdu_t *pdu = NULL;
    CAInfo_t *info = NULL;
    coap_list_t *options = NULL;
    coap_transport_type transport;

    if (SEND_TYPE_UNICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Unicast message");
#ifdef ROUTING_GATEWAY
        /*
         * When forwarding a packet, do not attempt retransmission as its the responsibility of
         * packet originator node
         */
        bool skipRetransmission = false;
#endif

        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
#ifdef ROUTING_GATEWAY
            skipRetransmission = data->requestInfo->info.skipRetransmission;
#endif
            pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
                                &options, &transport);
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
#ifdef ROUTING_GATEWAY
            skipRetransmission = data->responseInfo->info.skipRetransmission;
#endif
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
                                &options, &transport);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
            return CA_STATUS_INVALID_PARAM;
        }

        // interface controller function call.
        if (NULL != pdu)
        {
#ifdef WITH_BWT
            if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                    && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                    )
            {
                // Blockwise transfer
                if (NULL != info)
                {
                    CAResult_t res = CAAddBlockOption(&pdu, info,
                                                      data->remoteEndpoint,
                                                      &options);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(INFO, TAG, "to write block option has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_list(options);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
            }
#endif
            CALogPDUInfo(pdu, data->remoteEndpoint);

            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                coap_delete_list(options);
                coap_delete_pdu(pdu);
                return res;
            }

#ifdef TCP_ADAPTER
            if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
            {
                OIC_LOG(INFO, TAG, "retransmission will be not worked");
            }
            else
#endif
#ifdef ROUTING_GATEWAY
            if(!skipRetransmission)
#endif
            {
                // for retransmission
                res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
                                               pdu->hdr, pdu->length);
                if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
                {
                    //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
                    OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
                    coap_delete_list(options);
                    coap_delete_pdu(pdu);
                    return res;
                }
            }

            coap_delete_list(options);
            coap_delete_pdu(pdu);
        }
        else
        {
            OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
            CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
            return CA_SEND_FAILED;
        }
    }
    else if (SEND_TYPE_MULTICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Multicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);

            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                        && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                        )
                {
                    // Blockwise transfer
                    CAResult_t res = CAAddBlockOption(&pdu, &data->requestInfo->info,
                                                      data->remoteEndpoint,
                                                      &options);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_list(options);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
#endif
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
                                &options, &transport);

            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                        && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                        )
                {
                    // Blockwise transfer
                    if (NULL != info)
                    {
                        CAResult_t res = CAAddBlockOption(&pdu, info,
                                                          data->remoteEndpoint,
                                                          &options);
                        if (CA_STATUS_OK != res)
                        {
                            OIC_LOG(INFO, TAG, "to write block option has failed");
                            CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                            coap_delete_list(options);
                            coap_delete_pdu(pdu);
                            return res;
                        }
                    }
                }
#endif
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else
        {
            OIC_LOG(ERROR, TAG, "request or response info is empty");
            return CA_SEND_FAILED;
        }

        CALogPDUInfo(pdu, data->remoteEndpoint);

        OIC_LOG(DEBUG, TAG, "pdu to send :");
        OIC_LOG_BUFFER(DEBUG, TAG,  (uint8_t*)pdu->hdr, pdu->length);

        res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
            CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
            coap_delete_list(options);
            coap_delete_pdu(pdu);
            return res;
        }

        coap_delete_list(options);
        coap_delete_pdu(pdu);
    }

    return CA_STATUS_OK;
}
Beispiel #3
0
static CAResult_t CAProcessSendData(const CAData_t *data)
{
    OIC_LOG(DEBUG, TAG, "IN");
    VERIFY_NON_NULL(data, TAG, "data");
    VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");

    CAResult_t res = CA_STATUS_FAILED;

    CASendDataType_t type = data->type;

    coap_pdu_t *pdu = NULL;
    CAInfo_t *info = NULL;

    if (SEND_TYPE_UNICAST == type)
    {

        OIC_LOG(DEBUG,TAG,"Unicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
            return CA_STATUS_INVALID_PARAM;
        }

        // interface controller function call.
        if (NULL != pdu)
        {
#ifdef WITH_BWT
            if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
            {
                // Blockwise transfer
                if (NULL != info)
                {
                    CAResult_t res = CAAddBlockOption(&pdu, *info,
                                                      data->remoteEndpoint);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(INFO, TAG, "to write block option has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
            }
#endif
            CALogPDUInfo(pdu);

            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                coap_delete_pdu(pdu);
                return res;
            }
            // for retransmission
            res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
                                           pdu->length);
            if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
            {
                //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
                OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
                coap_delete_pdu(pdu);
                return res;
            }

            coap_delete_pdu(pdu);
        }
        else
        {
            OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
            CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
            return CA_SEND_FAILED;
        }
    }
    else if (SEND_TYPE_MULTICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Multicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
                {
                    // Blockwise transfer
                    CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
                                                      data->remoteEndpoint);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
#endif
                CALogPDUInfo(pdu);

                res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
                if (CA_STATUS_OK != res)
                {
                    OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                    CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                    coap_delete_pdu(pdu);
                    return res;
                }

                coap_delete_pdu(pdu);
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else
        {
            OIC_LOG(ERROR, TAG, "request info is empty");
            return CA_SEND_FAILED;
        }
    }

    OIC_LOG(DEBUG, TAG, "OUT");
    return CA_STATUS_OK;
}