Example #1
0
void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
{
    printf("##########Received response from remote device #############\n");
    if (CA_ADAPTER_IP == object->adapter)
    {
        printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
               object->port, object->flags & CA_SECURE);
    }
    else
    {
        printf("Remote Address: %s \n", object->addr);
    }
    printf("response result : %d\n", responseInfo->result);
    printf("Data: %s\n", responseInfo->info.payload);
    printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
    printf("Token: %s\n", responseInfo->info.token);
    printf("Resource URI: %s \n", responseInfo->info.resourceUri);

    if (responseInfo->info.options)
    {
        uint32_t len = responseInfo->info.numOptions;
        uint32_t i;
        for (i = 0; i < len; i++)
        {
            printf("Option %d\n", i + 1);
            printf("ID : %d\n", responseInfo->info.options[i].optionID);
            printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
                   responseInfo->info.options[i].optionData);
        }
    }
    printf("############################################################\n");
    g_received = 1;

    //Check if this has secure communication information
    if (responseInfo->info.payload)
    {
        int securePort = get_secure_information(responseInfo->info.payload);
        if (0 < securePort) //Set the remote endpoint secure details and send response
        {
            printf("This is secure resource...\n");
        }
    }

#ifdef WITH_BWT
    // if received message is bulk data, create output file
    if ((responseInfo->info.payload) &&
            (responseInfo->info.payloadSize > BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE)))
    {
        create_file(responseInfo->info.payload, responseInfo->info.payloadSize);
    }
#endif
}
Example #2
0
void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo)
{

    if (!object)
    {
        LOGE("Remote endpoint is NULL!");
        return;
    }

    if (!requestInfo)
    {
        LOGE("Request info is NULL!");
        return;
    }

    if ((NULL != g_lastRequestToken) && (NULL != requestInfo->info.token) &&
            (strncmp(g_lastRequestToken, requestInfo->info.token,
                     requestInfo->info.tokenLength) == 0))
    {
        LOGI("token is same. received request of it's own. skip.. ");
        return;
    }

    CAResult_t res = get_remote_address(object->addr);
    if (CA_STATUS_OK != res)
    {
        return;
    }

    LOGI("##########received request from remote device #############");
    LOGI("Remote Address: %s", g_remoteAddress);
    LOGI("Remote Port: %d", object->port);
    LOGI("Uri: %s", requestInfo->info.resourceUri);
    LOGI("Data: %s", requestInfo->info.payload);
    LOGI("Token: %s", requestInfo->info.token);
    LOGI("Code: %d", requestInfo->method);
    LOGI("MessageType: %d", requestInfo->info.type);

    if (NULL != g_responseListenerObject)
    {
        char *cloneUri = NULL;
        uint32_t len = 0;

        if (NULL != requestInfo->info.resourceUri)
        {
            len = strlen(requestInfo->info.resourceUri);
            cloneUri = (char *)malloc(sizeof(char) * (len + 1));

            if (NULL == cloneUri)
            {
                LOGE("cloneUri Out of memory");
                free(g_remoteAddress);
                return;
            }

            memcpy(cloneUri, requestInfo->info.resourceUri, len + 1);
            callback("Uri: ", cloneUri);
        }

        len = strlen(g_remoteAddress);
        char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));

        if (NULL == cloneRemoteAddress)
        {
            LOGE("cloneRemoteAddress Out of memory");
            free(g_remoteAddress);
            free(cloneUri);
            return;
        }

        memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);

        callback("Remote Address: ", cloneRemoteAddress);
        free(cloneRemoteAddress);
        free(g_remoteAddress);

        char portInfo[PORT_LENGTH] = { 0, };
        sprintf(portInfo, "%d", object->port);
        callback("Remote Port: ", portInfo);

        //clone g_clientEndpoint
        g_clientEndpoint = (CAEndpoint_t *) malloc(sizeof(CAEndpoint_t));
        if (NULL == g_clientEndpoint)
        {
            LOGE("g_clientEndpoint Out of memory");
            free(cloneUri);
            return;
        }
        memcpy(g_clientEndpoint, object, sizeof(CAEndpoint_t));

        if (NULL != cloneUri)
        {
            len = strlen(cloneUri);
            g_resourceUri = (char *) malloc(sizeof(char) * (len + 1));
            if (NULL == g_resourceUri)
            {
                LOGE("g_clientEndpoint->resourceUri Out of memory");
                free(g_clientEndpoint);
                free(cloneUri);
                return;
            }
            memcpy(g_resourceUri, cloneUri, len + 1);
            free(cloneUri);
        }
        //clone g_clientToken
        len = requestInfo->info.tokenLength;

        g_clientToken = (char *) malloc(sizeof(char) * len);
        if (NULL == g_clientToken)
        {
            LOGE("g_clientToken Out of memory");
            free(g_clientEndpoint);
            return;
        }

        if (NULL != requestInfo->info.token)
        {
            memcpy(g_clientToken, requestInfo->info.token, len);
            g_clientTokenLength = len;

        }

        //clone g_clientMsgId
        g_clientMsgId = requestInfo->info.messageId;

        if (NULL != requestInfo->info.payload && requestInfo->info.payloadSize > 0)
        {
            len = requestInfo->info.payloadSize;
            char *clonePayload = (char *) malloc(len + 1);
            if (NULL == clonePayload)
            {
                LOGE("clonePayload Out of memory");
                free(g_clientEndpoint);
                return;
            }

            memcpy(clonePayload, requestInfo->info.payload, len);
            clonePayload[len] = '\0';

            callback("Data: ", clonePayload);
            free(clonePayload);
        }
    }

    if (requestInfo->info.options)
    {
        uint32_t len = requestInfo->info.numOptions;
        uint32_t i;

        LOGI("Option count: %d", requestInfo->info.numOptions);

        for (i = 0; i < len; i++)
        {
            LOGI("Option %d", i + 1);
            LOGI("ID : %d", requestInfo->info.options[i].optionID);
            LOGI("Data[%d]: %s", requestInfo->info.options[i].optionLength,
                 requestInfo->info.options[i].optionData);

            if (NULL != g_responseListenerObject)
            {
                char optionInfo[OPTION_INFO_LENGTH] = { 0, };
                sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
                        requestInfo->info.options[i].optionID,
                        requestInfo->info.options[i].optionLength);

                callback("Option info: ", optionInfo);

                size_t optionDataLen = strlen(requestInfo->info.options[i].optionData);
                char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
                if (NULL == cloneOptionData)
                {
                    LOGE("cloneOptionData Out of memory");
                    free(g_clientEndpoint);
                    return;
                }

                memcpy(cloneOptionData, requestInfo->info.options[i].optionData,
                       optionDataLen + 1);

                callback("Option Data: ", cloneOptionData);
                free(cloneOptionData);
            }
        }
    }
    LOGI("############################################################");

    //Check if this has secure communication information
    if (requestInfo->info.payload && CA_ADAPTER_IP == object->adapter)
    {
        uint32_t securePort = get_secure_information(requestInfo->info.payload);
        if (0 < securePort) //Set the remote endpoint secure details and send response
        {
            LOGI("This is secure resource...");

            CAEndpoint_t *endpoint = NULL;
            if (CA_STATUS_OK != CACreateEndpoint(CA_SECURE,
                        object->adapter, object->addr, securePort, &endpoint))
            {
                LOGE("Failed to create duplicate of remote endpoint!");
                return;
            }
            object = endpoint;
        }
    }
}
Example #3
0
void response_handler(const CAEndpoint_t* object, const CAResponseInfo_t* responseInfo)
{
    if (!object || !responseInfo)
    {
        LOGE("Invalid input parameter");
        return;
    }

    CAResult_t res = get_remote_address(object->addr);
    if (CA_STATUS_OK != res)
    {
        return;
    }

    LOGI("##########Received response from remote device #############");
    LOGI("Uri: %s", responseInfo->info.resourceUri);
    LOGI("Remote Address: %s", g_remoteAddress);
    LOGI("Remote Port: %d", object->port);
    LOGI("response result: %d", responseInfo->result);
    LOGI("Data: %s", responseInfo->info.payload);
    LOGI("Token: %s", responseInfo->info.token);
    LOGI("MessageType: %d", responseInfo->info.type);

    if (NULL != g_responseListenerObject)
    {
        uint32_t len = 0;

        if (NULL != responseInfo->info.resourceUri)
        {
            len = strlen(responseInfo->info.resourceUri);
            char *cloneUri = (char *) malloc(sizeof(char) * (len + 1));

            if (NULL == cloneUri)
            {
                LOGE("cloneUri Out of memory");
                free(g_remoteAddress);
                return;
            }

            memcpy(cloneUri, responseInfo->info.resourceUri, len + 1);

            callback("Uri: ", cloneUri);
            free(cloneUri);
        }

        len = strlen(g_remoteAddress);
        char *cloneRemoteAddress = (char *) malloc(sizeof(char) * (len + 1));

        if (NULL == cloneRemoteAddress)
        {
            LOGE("cloneRemoteAddress Out of memory");
            free(g_remoteAddress);
            return;
        }

        memcpy(cloneRemoteAddress, g_remoteAddress, len + 1);

        callback("Remote Address: ", cloneRemoteAddress);
        free(cloneRemoteAddress);
        free(g_remoteAddress);

        char portInfo[PORT_LENGTH] = { 0, };
        sprintf(portInfo, "%d", object->port);
        callback("Remote Port: ", portInfo);

        if (NULL != responseInfo->info.payload && responseInfo->info.payloadSize)
        {
            len = responseInfo->info.payloadSize;
            char *clonePayload = (char *) malloc(len + 1);
            if (NULL == clonePayload)
            {
                LOGE("clonePayload Out of memory");
                return;
            }

            memcpy(clonePayload, responseInfo->info.payload, len);
            clonePayload[len] = '\0';

            callback("Data: ", clonePayload);
            free(clonePayload);
        }
    }

    if (responseInfo->info.options)
    {
        uint32_t len = responseInfo->info.numOptions;
        uint32_t i;
        for (i = 0; i < len; i++)
        {
            LOGI("Option %d", i + 1);
            LOGI("ID : %d", responseInfo->info.options[i].optionID);
            LOGI("Data[%d]: %s", responseInfo->info.options[i].optionLength,
                 responseInfo->info.options[i].optionData);

            if (NULL != g_responseListenerObject)
            {
                char optionInfo[OPTION_INFO_LENGTH] = { 0, };
                sprintf(optionInfo, "Num[%d] - ID : %d, Option Length : %d", i + 1,
                        responseInfo->info.options[i].optionID,
                        responseInfo->info.options[i].optionLength);

                callback("Option info: ", optionInfo);

                size_t optionDataLen = strlen(responseInfo->info.options[i].optionData);
                char *cloneOptionData = (char *) malloc(sizeof(char) * (optionDataLen + 1));
                if (NULL == cloneOptionData)
                {
                    LOGE("cloneOptionData Out of memory");
                    return;
                }
                memcpy(cloneOptionData, responseInfo->info.options[i].optionData,
                       optionDataLen + 1);
                callback("Option Data: ", cloneOptionData);
                free(cloneOptionData);
            }
        }
    }
    LOGI("############################################################");

    //Check if this has secure communication information
    if (responseInfo->info.payload && CA_ADAPTER_IP == object->adapter)
    {
        uint32_t securePort = get_secure_information(responseInfo->info.payload);
        if (0 < securePort) //Set the remote endpoint secure details and send response
        {
            LOGI("This is secure resource...");
        }
    }
}
Example #4
0
void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
{
    if (NULL == object || NULL == requestInfo)
    {
        printf("Input parameter is NULL\n");
        return;
    }

    if ((NULL != g_last_request_token) && (NULL != requestInfo->info.token)
        && (memcmp(g_last_request_token, requestInfo->info.token,
                   CA_MAX_TOKEN_LEN) == 0))
    {
        printf("token is same. received request of it's own. skip.. \n");
        return;
    }

    printf("##########received request from remote device #############\n");
    if (CA_ADAPTER_IP == object->adapter)
    {
        printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
               object->port, object->flags & CA_SECURE);
    }
    else
    {
        printf("Remote Address: %s \n", object->addr);
    }
    printf("Data: %s\n", requestInfo->info.payload);
    printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);

    if (requestInfo->info.options)
    {
        uint32_t len = requestInfo->info.numOptions;
        uint32_t i;
        for (i = 0; i < len; i++)
        {
            printf("Option %d\n", i + 1);
            printf("ID : %d\n", requestInfo->info.options[i].optionID);
            printf("Data[%d]: %s\n", requestInfo->info.options[i].optionLength,
                   requestInfo->info.options[i].optionData);
        }
    }
    printf("############################################################\n");

    //Check if this has secure communication information
    if (requestInfo->info.payload &&
            (CA_ADAPTER_IP == object->adapter))
    {
        int securePort = get_secure_information(requestInfo->info.payload);
        if (0 < securePort) //Set the remote endpoint secure details and send response
        {
            printf("This is secure resource...\n");

            CAEndpoint_t *endpoint = NULL;
            if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr,
                                                 object->port, &endpoint))
            {
                printf("Failed to create duplicate of remote endpoint!\n");
                return;
            }
            endpoint->flags = CA_SECURE;
            object = endpoint;
        }
    }

    // if received message is bulk data, create output file
    if ((requestInfo->info.payload) &&
            (requestInfo->info.payloadSize > BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE)))
    {
        create_file(requestInfo->info.payload, requestInfo->info.payloadSize);
    }

    printf("Send response with URI\n");
    send_response(object, &requestInfo->info);

    g_received = 1;
}