Exemplo n.º 1
0
TEST_F(CATests, SendResponseTestWithInvalidCode)
{
    EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));

    addr = (char *) ADDRESS;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);

    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.type = CA_MSG_RESET;
    responseData.messageId = 1;
    responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));

    EXPECT_TRUE(responseData.payload != NULL);

    if (responseData.payload)
    {
        CAGenerateToken(&tempToken, tokenLength);
        requestData.token = tempToken;
        requestData.tokenLength = tokenLength;

        memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
        responseInfo.result = CA_CONTENT;
        responseInfo.info = responseData;

        EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));

        CADestroyToken(tempToken);
        CADestroyEndpoint(tempRep);
        free(responseData.payload);
        tempRep = NULL;
    }
}
Exemplo n.º 2
0
TEST_F(CABlockTransferTests, CAUpdatePayloadToCADataWithResponse)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

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

    CAInfo_t responseData;
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.token = tempToken;
    responseData.tokenLength = CA_MAX_TOKEN_LEN;
    responseData.options = NULL;
    responseData.payload = NULL;
    responseData.payloadSize = 0;

    CAResponseInfo_t responseInfo;
    responseInfo.result = CA_VALID;
    responseInfo.info = responseData;

    CAData_t cadata;
    cadata.type = SEND_TYPE_UNICAST;
    cadata.remoteEndpoint = tempRep;
    cadata.responseInfo = &responseInfo;
    cadata.dataType = CA_RESPONSE_DATA;

    CAPayload_t payload = (CAPayload_t) "responsePayload";
    size_t payloadLen = strlen((const char*) payload) + 1;

    EXPECT_EQ(CA_STATUS_OK, CAUpdatePayloadToCAData(&cadata, payload, payloadLen));

    EXPECT_STREQ((const char*) payload, (const char*) cadata.responseInfo->info.payload);
}
Exemplo n.º 3
0
// CASendRequest TC
// check return value
TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
{
    addr = (char *) ADDRESS;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);

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

    int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
    requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
    if(!requestData.payload)
    {
        CADestroyToken(tempToken);
        FAIL() << "requestData.payload allocation failed";
    }
    snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
    requestData.payloadSize = length + 1;
    requestData.type = CA_MSG_NONCONFIRM;

    memset(&requestInfo, 0, sizeof(CARequestInfo_t));
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;

    EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
    tempRep = NULL;
}
Exemplo n.º 4
0
TEST_F(CABlockTransferTests, CAGetBlockSizeOptionFromPduTest)
{
    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.type = CA_MSG_NONCONFIRM;

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

    size_t totalPayloadLen = 0;
    EXPECT_FALSE(CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1,
                                                           &totalPayloadLen));

    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
Exemplo n.º 5
0
// check return value when address is NULL as multicast
TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
{
    addr = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);

    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.messageId = 1;
    responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
    EXPECT_TRUE(responseData.payload != NULL);

    if(!responseData.payload)
    {
        CADestroyEndpoint(tempRep);
        return;
    }

    memcpy(responseData.payload, "response payload", sizeof("response payload"));
    responseData.payloadSize = sizeof("response payload");

    CAGenerateToken(&tempToken, tokenLength);
    requestData.token = tempToken;
    requestData.tokenLength = tokenLength;

    memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
    responseInfo.result = CA_CONTENT;
    responseInfo.info = responseData;

    EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free (responseData.payload);
    tempRep = NULL;
}
Exemplo n.º 6
0
void send_secure_request()
{
    char ipv4addr[CA_IPADDR_SIZE];

    printf("\n=============================================\n");
    printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");

    if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
    {
        return;
    }
    printf("%s%s:5684/a/light", SECURE_COAPS_PREFIX, ipv4addr);

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
    if (CA_STATUS_OK != res)
    {
        printf("Failed to create remote endpoint, error code: %d\n", res);
        goto exit;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error, error code : %d\n", res);
        goto exit;
    }

    printf("Generated token %s\n", token);

    // create request data
    CAMessageType_t msgType = CA_MSG_NONCONFIRM;
    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.type = msgType;
    requestData.payload = "Temp Json Payload";
    requestData.payloadSize = strlen(requestData.payload)+1;

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;
    requestInfo.isMulticast = false;

    // send request
    CASendRequest(endpoint, &requestInfo);

exit:
    // cleanup
    CADestroyToken(token);
    CADestroyEndpoint(endpoint);
    printf("=============================================\n");
}
Exemplo n.º 7
0
// CACreateRemoteEndpoint TC
// check return value
TEST_F(CATests, CreateRemoteEndpointTestGood)
{
    addr = (char *) ADDRESS;

    EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
                                             PORT, &tempRep));

    CADestroyEndpoint(tempRep);
    tempRep = NULL;
}
Exemplo n.º 8
0
// check return value NULL is passed instead of a valid CAResponseInfo_t address
TEST_F(CATests, SendResponseTest)
{
    addr = (char *) ADDRESS;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);

    EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));

    CADestroyEndpoint(tempRep);
    tempRep = NULL;
}
Exemplo n.º 9
0
// check remoteEndpoint and values of remoteEndpoint
TEST_F(CATests, CreateRemoteEndpointTestValues)
{
    addr = (char *) ADDRESS;

    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);

    EXPECT_TRUE(tempRep != NULL);

    CADestroyEndpoint(tempRep);
    tempRep = NULL;
}
Exemplo n.º 10
0
// response and block option2
TEST_F(CABlockTransferTests, CAAddBlockOption2InResponse)
{
    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 responseData;
    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.token = tempToken;
    responseData.tokenLength = CA_MAX_TOKEN_LEN;
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.messageId = 1;

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

    pdu = CAGeneratePDU(CA_CREATED, &responseData, tempRep, &options, &transport);

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    if (currData)
    {
        EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId,
                                                        COAP_OPTION_BLOCK2));

        EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &responseData,
                                                  responseData.payloadSize,
                                                  currData->blockDataId, &options));
    }

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(responseData.payload);
}
Exemplo n.º 11
0
// check return value when a NULL is passed instead of a valid CARequestInfo_t address
TEST_F(CATests, SendRequestTestWithNullAddr)
{
    addr = (char *) ADDRESS;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);

    EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));

    if (tempRep != NULL)
    {
        CADestroyEndpoint(tempRep);
        tempRep = NULL;
    }
}
Exemplo n.º 12
0
TEST_F(CABlockTransferTests, CAGetPayloadFromBlockDataListTest)
{
    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.type = CA_MSG_NONCONFIRM;

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

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    if (currData)
    {
        size_t fullPayload = 0;
        CAPayload_t payload = CAGetPayloadFromBlockDataList(currData->blockDataId,
                                                            &fullPayload);

        size_t payloadLen = (payload != NULL) ? strlen((const char*) payload) : 0;
        EXPECT_TRUE(fullPayload == payloadLen);

        CARemoveBlockDataFromList(currData->blockDataId);
    }

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
Exemplo n.º 13
0
// 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);
}
Exemplo n.º 14
0
TEST_F(CABlockTransferTests, CASetNextBlockOption2WithResponse)
{
    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 responseData;
    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.token = tempToken;
    responseData.tokenLength = CA_MAX_TOKEN_LEN;
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.messageId = 1;

    pdu = CAGeneratePDU(CA_CREATED, &responseData, tempRep, &options, &transport);

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    coap_block_t block = {0, 0, 0};
    EXPECT_EQ(CA_STATUS_OK, CASetNextBlockOption2(pdu, tempRep, cadata, block, pdu->length));

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(responseData.payload);
}
Exemplo n.º 15
0
TEST_F(CATests, SendRequestTestWithInvalidAddress)
{
    CARegisterHandler(request_handler, response_handler, error_handler);

    EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep);

    memset(&requestData, 0, sizeof(CAInfo_t));
    CAGenerateToken(&tempToken, tokenLength);
    requestData.token = tempToken;
    requestData.tokenLength = tokenLength;
    requestData.type = CA_MSG_CONFIRM;

    memset(&requestInfo, 0, sizeof(CARequestInfo_t));
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;

    EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
    tempRep = NULL;
}
Exemplo n.º 16
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
                                                       jstring payload, jint selectedNetwork,
                                                       jint isSecured, jint msgType,
                                                       jboolean isBigData)
{
    LOGI("selectedNetwork - %d", selectedNetwork);
    if (!env || !obj)
    {
        LOGI("Invalid input parameter");
        return;
    }

    if (!uri)
    {
        LOGE("Invalid input parameter : uri");
        return;
    }

    CAResult_t res = get_network_type(selectedNetwork);
    if (CA_STATUS_OK != res)
    {
        return;
    }

    const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
    LOGI("RMSendRequest - %s", strUri);

    CATransportFlags_t flags;
    addressSet_t address = {{0}, 0};
    parsing_coap_uri(strUri, &address, &flags);

    //create remote endpoint
    CAEndpoint_t* endpoint = NULL;
    res = CACreateEndpoint(flags, g_selectedNwType, (const char*)(address.ipAddress),
                           address.port, &endpoint);
    if (CA_STATUS_OK != res)
    {
        LOGE("Could not create remote end point");
        (*env)->ReleaseStringUTFChars(env, uri, strUri);
        return;
    }

    CAMessageType_t messageType = msgType;

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        LOGE("token generate error!!");
        // destroy remote endpoint
        CADestroyEndpoint(endpoint);
        (*env)->ReleaseStringUTFChars(env, uri, strUri);
        return;
    }

    char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };

    get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
    (*env)->ReleaseStringUTFChars(env, uri, strUri);

    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;

    size_t payloadLength = 0;
    if (isBigData)
    {
        const char* path = NULL;
        if (payload)
        {
            path = (*env)->GetStringUTFChars(env, payload, NULL);
            if(path)
            {
                char* bigData = NULL;
                bool result = read_file(path, &bigData, &payloadLength);
                if (!result)
                {
                    LOGE("read has failed");
                    (*env)->ReleaseStringUTFChars(env, payload, path);
                    CADestroyToken(token);
                    CADestroyEndpoint(endpoint);
                    return;
                }
                (*env)->ReleaseStringUTFChars(env, payload, path);

                requestData.payload = (CAPayload_t) malloc(payloadLength);
                if (NULL == requestData.payload)
                {
                    LOGE("Memory allocation failed!");
                    // destroy token
                    CADestroyToken(token);
                    // destroy remote endpoint
                    CADestroyEndpoint(endpoint);
                    return;
                }
                memcpy(requestData.payload, bigData, payloadLength);
                requestData.payloadSize = payloadLength;
            }
        }
    }
    else
    {
        if (isSecured)
        {
            payloadLength = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
            requestData.payload = (CAPayload_t) malloc(payloadLength);
            if (NULL == requestData.payload)
            {
                LOGE("Memory allocation failed!");
                // destroy token
                CADestroyToken(token);
                // destroy remote endpoint
                CADestroyEndpoint(endpoint);
                return;
            }
            snprintf((char *) requestData.payload, payloadLength, SECURE_INFO_DATA,
                     resourceURI, g_localSecurePort);
            requestData.payloadSize = payloadLength;
        }
        else
        {
            payloadLength = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
            requestData.payload = (CAPayload_t) malloc(payloadLength);
            if (NULL == requestData.payload)
            {
                LOGE("Memory allocation failed!");
                // destroy token
                CADestroyToken(token);
                // destroy remote endpoint
                CADestroyEndpoint(endpoint);
                return;
            }
            snprintf((char *) requestData.payload, payloadLength, NORMAL_INFO_DATA, resourceURI);
            requestData.payloadSize = payloadLength;
        }
    }

    requestData.type = messageType;
    requestData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
    if (NULL == requestData.resourceUri)
    {
        LOGE("Memory allocation failed!");
        // destroy token
        CADestroyToken(token);
        // destroy remote endpoint
        CADestroyEndpoint(endpoint);
        free(requestData.payload);
        return;
    }
    memcpy(requestData.resourceUri, resourceURI, sizeof(resourceURI));

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.isMulticast = false;
    requestInfo.info = requestData;

    // send request
    if (CA_STATUS_OK != CASendRequest(endpoint, &requestInfo))
    {
        LOGE("Could not send request");
    }

    // destroy token
    CADestroyToken(token);

    // destroy remote endpoint
    CADestroyEndpoint(endpoint);

    free(requestData.payload);
    free(requestData.resourceUri);
}
Exemplo n.º 17
0
void send_request()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("Do you want to send secure request ?.... enter (0/1): ");

    char secureRequest[MAX_BUF_LEN] = {0};
    if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
    {
        return;
    }

    if (strcmp(secureRequest, "1") == 0)
    {
        printf("Enter the URI like below....\n");
        printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
    }
    else if (strcmp(secureRequest, "0") == 0)
    {
        printf("Enter the URI like below....\n");
        printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
        printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
        printf("coap+tcp://10:11:12:13:45:45/resource_uri ( for TCP )\n");
    }
    else
    {
        printf("Input data is wrong value\n");
        return;
    }

    char uri[MAX_BUF_LEN] = {'\0'};
    if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
    {
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CATransportFlags_t flags;

    printf("URI : %s\n", uri);
    addressSet_t address = {};
    parse_coap_uri(uri, &address, &flags);

    res = CACreateEndpoint(flags, g_selected_nw_type,
                           (const char*)address.ipAddress, address.port, &endpoint);
    if (CA_STATUS_OK != res || !endpoint)
    {
        printf("Failed to create remote endpoint, error code : %d\n", res);
        return;
    }

    printf("\n=============================================\n");
    printf("0:CON, 1:NON\n");
    printf("select message type : ");

    char buf[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
    {
        CADestroyEndpoint(endpoint);
        return;
    }

    CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error, error code : %d\n", res);
        CADestroyEndpoint(endpoint);
        return;
    }

    printf("Generated token %s\n", token);

    // extract relative resourceuri from give uri
    char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
    get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
    printf("resourceURI : %s\n", resourceURI);

    // create request data
    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.resourceUri = (CAURI_t)resourceURI;

    if (strcmp(secureRequest, "1") == 0)
    {
        size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
        requestData.payload = (CAPayload_t) calloc(length,  sizeof(char));
        if (NULL == requestData.payload)
        {
            printf("Memory allocation fail\n");
            CADestroyEndpoint(endpoint);
            CADestroyToken(token);
            return;
        }
        snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
                 (const char *) resourceURI, g_local_secure_port);
    }
    else
    {
        bool useBigPayload = select_payload();
        if (useBigPayload)
        {
            requestData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char));
            if (NULL == requestData.payload)
            {
                printf("Memory allocation fail\n");
                CADestroyEndpoint(endpoint);
                CADestroyToken(token);
                return;
            }
            populate_binary_payload(requestData.payload, BIG_PAYLOAD_SIZE);
        }
        else
        {
            size_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
            requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
            if (NULL == requestData.payload)
            {
                printf("Memory allocation fail\n");
                CADestroyEndpoint(endpoint);
                CADestroyToken(token);
                return;
            }
            snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
                     (const char *) resourceURI);
        }
    }
    requestData.payloadSize = strlen((char *)requestData.payload)+1;
    requestData.type = msgType;

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;
    requestInfo.isMulticast = false;

    // send request
    res = CASendRequest(endpoint, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Could not send request : %d\n", res);
    }

    //destroy token
    CADestroyToken(token);
    // destroy remote endpoint
    CADestroyEndpoint(endpoint);
    free(requestData.payload);


    printf("=============================================\n");
}
Exemplo n.º 18
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;
        }
    }
}
Exemplo n.º 19
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
                                                            jstring payload, jint selectedNetwork,
                                                            jint isSecured, jint msgType,
                                                            jint responseValue)
{
    LOGI("selectedNetwork - %d", selectedNetwork);
    if (!env || !obj)
    {
        LOGI("Invalid input parameter");
        return;
    }

    if (!payload)
    {
        LOGE("payload is NULL");
    }

    if (!uri)
    {
        LOGE("Invalid input parameter : uri");
        return;
    }

    CAResult_t res = get_network_type(selectedNetwork);
    if (CA_STATUS_OK != res)
    {
        LOGE("Not supported network type");
        return;
    }

    const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
    LOGI("RMSendNotification - %s", strUri);

    CATransportFlags_t flags;
    addressSet_t address = {{0}, 0};
    parsing_coap_uri(strUri, &address, &flags);

    //create remote endpoint
    CAEndpoint_t* endpoint = NULL;
    if (CA_STATUS_OK != CACreateEndpoint(flags, g_selectedNwType,
                                         (const char*)address.ipAddress,
                                         address.port, &endpoint))
    {
        //ReleaseStringUTFChars for strUri
        (*env)->ReleaseStringUTFChars(env, uri, strUri);
        LOGE("Could not create remote end point");
        return;
    }

    char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
    get_resource_uri(strUri, resourceURI, RESOURCE_URI_LENGTH);

    //ReleaseStringUTFChars for strUri
    (*env)->ReleaseStringUTFChars(env, uri, strUri);

    CAMessageType_t messageType = msgType;

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        LOGE("token generate error!");
        CADestroyEndpoint(endpoint);
        return;
    }

    CAInfo_t responseData = { 0 };
    responseData.token = token;
    responseData.tokenLength = tokenLength;
    responseData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
    if (NULL == responseData.resourceUri)
    {
        LOGE("Memory allocation failed!");
        // destroy token
        CADestroyToken(token);
        // destroy remote endpoint
        CADestroyEndpoint(endpoint);
        return;
    }
    memcpy(responseData.resourceUri, resourceURI, sizeof(resourceURI));

    if (1 == isSecured)
    {
        uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
        responseData.payload = (CAPayload_t) malloc(length);
        if (NULL == responseData.payload)
        {
            LOGE("Memory allocation failed!");
            // destroy token
            CADestroyToken(token);
            // destroy remote endpoint
            CADestroyEndpoint(endpoint);

            free(responseData.resourceUri);
            return;
        }
        snprintf((char *) responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
        responseData.payloadSize = length;
    }
    else
    {
        uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
        responseData.payload = (CAPayload_t) malloc(length);
        if (NULL == responseData.payload)
        {
            LOGE("Memory allocation failed!");
            // destroy token
            CADestroyToken(token);
            // destroy remote endpoint
            CADestroyEndpoint(endpoint);

            free(responseData.resourceUri);
            return;
        }
        snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
        responseData.payloadSize = length;
    }

    responseData.type = messageType;

    CAResponseInfo_t responseInfo = { 0 };
    responseInfo.result = responseValue;
    responseInfo.info = responseData;

    // send notification
    if (CA_STATUS_OK != CASendNotification(endpoint, &responseInfo))
    {
        LOGE("Could not send notification");
    }

    LOGI("Send Notification");

    // destroy token
    CADestroyToken(token);

    // destroy remote endpoint
    CADestroyEndpoint(endpoint);

    free(responseData.payload);
    free(responseData.resourceUri);
}
Exemplo n.º 20
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
                                                           jint selectedNetwork)
{
    LOGI("selectedNetwork - %d", selectedNetwork);
    if (!env || !obj)
    {
        LOGI("Invalid input parameter");
        return;
    }

    CAResult_t res = get_network_type(selectedNetwork);
    if (CA_STATUS_OK != res)
    {
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    res = CACreateEndpoint(CA_IPV4, g_selectedNwType, NULL, 0, &endpoint);

    if (CA_STATUS_OK != res)
    {
        LOGE("create remote endpoint error, error code: %d", res);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        LOGE("token generate error!!");
        // destroy remote endpoint
        CADestroyEndpoint(endpoint);
        return;
    }

    LOGI("generated token %s", token);

    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.payload = (CAPayload_t) "TempJsonPayload";
    requestData.payloadSize = strlen((const char *) requestData.payload);
    requestData.type = CA_MSG_NONCONFIRM;

    const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
    LOGI("resourceUri - %s", strUri);
    requestData.resourceUri = (CAURI_t)strUri;

    uint8_t optionNum = 2;
    CAHeaderOption_t *headerOpt = (CAHeaderOption_t*) calloc(1,
                                                             sizeof(CAHeaderOption_t) * optionNum);
    if (NULL == headerOpt)
    {
        LOGE("Memory allocation failed");
        // destroy remote endpoint
        CADestroyEndpoint(endpoint);
        return;
    }

    char* FirstOptionData = "Hello";
    headerOpt[0].optionID = 3000;
    memcpy(headerOpt[0].optionData, FirstOptionData, strlen(FirstOptionData));
    headerOpt[0].optionLength = (uint16_t) strlen(FirstOptionData);

    char* SecondOptionData2 = "World";
    headerOpt[1].optionID = 3001;
    memcpy(headerOpt[1].optionData, SecondOptionData2, strlen(SecondOptionData2));
    headerOpt[1].optionLength = (uint16_t) strlen(SecondOptionData2);

    requestData.numOptions = optionNum;
    requestData.options = headerOpt;

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.isMulticast = true;
    requestInfo.info = requestData;

    // send request to all
    res = CASendRequest(endpoint, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        LOGE("Could not send request to all");
        //destroy token
        CADestroyToken(token);
    }
    else
    {
        CADestroyToken(g_lastRequestToken);
        g_lastRequestToken = token;
        g_lastRequestTokenLength = tokenLength;
    }

    //ReleaseStringUTFChars for strUri
    (*env)->ReleaseStringUTFChars(env, uri, strUri);

    free(headerOpt);

    // destroy remote endpoint
    CADestroyEndpoint(endpoint);
}
Exemplo n.º 21
0
void send_request_all()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("\n=============================================\n");
    printf("ex) /a/light\n");
    printf("resource uri : ");

    char resourceURI[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
    {
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    res = CACreateEndpoint(CA_IPV4, g_selected_nw_type, NULL, 0, &endpoint);
    if (CA_STATUS_OK != res)
    {
        printf("Create remote endpoint error, error code: %d\n", res);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error!!\n");
        CADestroyEndpoint(endpoint);
        return;
    }

    printf("generated token %s\n", token);

    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.payload = (CAPayload_t) "TempJsonPayload";
    requestData.payloadSize = strlen((const char *) requestData.payload);
    requestData.type = CA_MSG_NONCONFIRM;
    requestData.resourceUri = (CAURI_t)resourceURI;

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;
    requestInfo.isMulticast = true;

    // send request
    res = CASendRequest(endpoint, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Could not send request to all\n");
    }
    else
    {
        CADestroyToken(g_last_request_token);
        g_last_request_token = token;
    }

    // destroy remote endpoint
    CADestroyEndpoint(endpoint);

    printf("=============================================\n");
}
Exemplo n.º 22
0
void SendRequestAll()
{
    char buf[MAX_BUF_LEN] = {0};
    char address[MAX_BUF_LEN] = {0};
    char resourceUri[MAX_BUF_LEN] = {0};
    char port[PORT_LENGTH] = {0};

    CATransportAdapter_t selectedNetwork;
    selectedNetwork = GetConnectivityType();

    Serial.println("\n=============================================\n");
    Serial.println("ex) /a/light\n");
    Serial.println("resource uri : ");

    size_t len = 0;
    GetData(buf, sizeof(buf), &len);
    if (0 >= len)
    {
        Serial.println("i/p err");
        return;
    }

    if (!ParseData(buf, address, port, resourceUri))
    {
        Serial.println("bad uri");
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CAResult_t res = CACreateEndpoint(CA_IPV4, selectedNetwork, address, atoi(port),
                                        &endpoint);

    if (res != CA_STATUS_OK)
    {
        Serial.println("create remote endpoint error");
        CADestroyEndpoint(endpoint);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if (res != CA_STATUS_OK || (!token))
    {
        Serial.println("token error");
        return;
    }

    Serial.println(token);

    CAInfo_t requestData = {CA_MSG_RESET};
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.payload = (CAPayload_t)"Temp Json Payload";
    requestData.payloadSize = strlen((const char *) requestData.payload);
    requestData.type = CA_MSG_NONCONFIRM;
    requestData.resourceUri = (char *)OICMalloc(strlen(resourceUri) + 1);
    strcpy(requestData.resourceUri, resourceUri);

    CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
    requestInfo.method = CA_GET;
    requestInfo.isMulticast = true;
    requestInfo.info = requestData;

    // send request
    CASendRequest(endpoint, &requestInfo);

    if (NULL != token)
    {
        CADestroyToken(token);
    }

    // destroy remote endpoint
    if (endpoint != NULL)
    {
        CADestroyEndpoint(endpoint);
    }

    Serial.println("==========");
}
Exemplo n.º 23
0
void send_notification()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("\n=============================================\n");
    printf("Enter the URI like below....\n");
    printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
    printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
    printf("uri : ");

    char uri[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
    {
        return;
    }

    printf("\n=============================================\n");
    printf("\tselect message type\n");
    printf("CON     : 0\n");
    printf("NON     : 1\n");
    printf("ACK     : 2\n");
    printf("RESET   : 3\n");

    printf("select : ");

    char messageTypeBuf[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
    {
        return;
    }

    int messageType = messageTypeBuf[0] - '0';

    switch(messageType)
    {
        case 0:
                printf("CONFIRM messagetype is selected\n");
                break;
        case 1:
                printf("NONCONFIRM messagetype is selected\n");
                break;
        default:
                printf("Invalid Selection\n");
                return;
    }

    CATransportFlags_t flags;
    addressSet_t address = {};
    parse_coap_uri(uri, &address, &flags);

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
    if (CA_STATUS_OK != res)
    {
        printf("Create remote endpoint error, error code: %d\n", res);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error!!\n");
        CADestroyEndpoint(endpoint);
        return;
    }

    printf("Generated token %s\n", token);

    CAInfo_t requestData = { 0 };
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.payload = (CAPayload_t) "TempNotificationData";
    requestData.payloadSize = strlen((const char *) requestData.payload);
    requestData.type = messageType;
    requestData.resourceUri = (CAURI_t)uri;

    CARequestInfo_t requestInfo = { 0 };
    requestInfo.method = CA_GET;
    requestInfo.info = requestData;

    // send notification
    res = CASendRequest(endpoint, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Send notification error, error code: %d\n", res);
    }
    else
    {
        printf("Send notification success\n");
    }

    // destroy token
    CADestroyToken(token);
    // destroy remote endpoint
    CADestroyEndpoint(endpoint);

    printf("\n=============================================\n");
}
Exemplo n.º 24
0
void SendRequest()
{
    char buf[MAX_BUF_LEN] = {0};
    char address[MAX_BUF_LEN] = {0};
    char resourceUri[MAX_BUF_LEN] = {0};
    char port[PORT_LENGTH] = {0};
    CATransportAdapter_t selectedNetwork;
    selectedNetwork = GetConnectivityType();

    Serial.println("============");
    Serial.println("10.11.12.13:4545/res_uri (for IP)");
    Serial.println("10:11:12:13:45:45/res_uri (for BT)");
    Serial.println("uri: ");

    size_t len = 0;
    GetData(buf, sizeof(buf), &len);
    if (0 >= len)
    {
        Serial.println("i/p err");
        return;
    }

    if (!ParseData(buf, address, port, resourceUri))
    {
        Serial.println("bad uri");
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, atoi(port),
                                      &endpoint);
    if (res != CA_STATUS_OK)
    {
        Serial.println("Out of memory");
        CADestroyEndpoint(endpoint);
        return;
    }

    memset(buf, 0, sizeof(buf));

    Serial.println("\n=============================================\n");
    Serial.println("0:CON, 1:NON\n");
    Serial.println("select message type : ");
    GetData(buf, sizeof(buf), &len);
    CAMessageType_t msgType = CA_MSG_CONFIRM;

    if (0 >= len)
    {
        Serial.println("i/p err,default: 0");
    }
    else if(buf[0] == '1')
    {
        msgType = CA_MSG_NONCONFIRM;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if (res != CA_STATUS_OK || (!token))
    {
        Serial.println("token error");
        return;
    }

    Serial.println(token);
    CAInfo_t requestData = {CA_MSG_RESET};
    requestData.token = token;
    requestData.tokenLength = tokenLength;
    requestData.payload = (CAPayload_t)"Json Payload";
    requestData.payloadSize = strlen((const char *) requestData.payload);

    requestData.type = msgType;
    requestData.resourceUri = (char *)OICMalloc(strlen(resourceUri) + 1);
    strcpy(requestData.resourceUri, resourceUri);

    CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}};
    requestInfo.method = CA_GET;
    requestInfo.isMulticast = false;
    requestInfo.info = requestData;
    requestInfo.isMulticast = false;

    // send request
    CASendRequest(endpoint, &requestInfo);
    if (NULL != token)
    {
        CADestroyToken(token);
    }

    // destroy remote endpoint
    if (endpoint != NULL)
    {
        CADestroyEndpoint(endpoint);
    }

    Serial.println("============");
}
Exemplo n.º 25
0
void send_request()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("Do you want to send secure request ?.... enter (0/1): ");

    char secureRequest[MAX_BUF_LEN] = {0};
    if (CA_STATUS_OK != get_input_data(secureRequest, MAX_BUF_LEN))
    {
        return;
    }

    if (strcmp(secureRequest, "1") == 0)
    {
        printf("Enter the URI like below....\n");
        printf("coaps://10.11.12.13:4545/resource_uri ( for IP secure)\n");
    }
    else if (strcmp(secureRequest, "0") == 0)
    {
        printf("Enter the URI like below....\n");
        printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
        printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
    }
    else
    {
        printf("Input data is wrong value\n");
        return;
    }

    char uri[MAX_BUF_LEN] = {'\0'};
    if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
    {
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CATransportFlags_t flags;

    printf("URI : %s\n", uri);
    addressSet_t address = {{}, 0};
    parsing_coap_uri(uri, &address, &flags);

    res = CACreateEndpoint(flags, g_selected_nw_type,
                           (const char*)address.ipAddress, address.port, &endpoint);
    if (CA_STATUS_OK != res || !endpoint)
    {
        printf("Failed to create remote endpoint, error code : %d\n", res);
        return;
    }

    printf("\n=============================================\n");
    printf("0:CON, 1:NON\n");
    printf("select message type : ");

    char buf[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
    {
        CADestroyEndpoint(endpoint);
        return;
    }

    CAMessageType_t msgType = (buf[0] == '1') ? 1 : 0;

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error, error code : %d\n", res);
        CADestroyEndpoint(endpoint);
        return;
    }

    printf("Generated token %s\n", token);

    // extract relative resourceuri from give uri
    char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
    get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
    printf("resourceURI : %s\n", resourceURI);

    // create request data
    CAInfo_t requestData = { .type = msgType,
                             .messageId = 0,
                             .token = token,
                             .tokenLength = tokenLength,
                             .options = NULL,
                             .numOptions = 0,
                             .payload = NULL,
                             .payloadSize = 0,
                             .resourceUri = (CAURI_t)resourceURI };

    if (strcmp(secureRequest, "1") == 0)
    {
        size_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI);
        requestData.payload = (CAPayload_t) calloc(length,  sizeof(char));
        if (NULL == requestData.payload)
        {
            printf("Memory allocation fail\n");
            CADestroyEndpoint(endpoint);
            CADestroyToken(token);
            return;
        }
        snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
                 (const char *) resourceURI, g_local_secure_port);
        requestData.payloadSize = length;
    }
    else
    {
        bool useBigPayload = select_payload_type();
        if (useBigPayload)
        {
            size_t payloadLength = 0;
            CAPayload_t binaryPayload = get_binary_payload(&payloadLength);
            if (!binaryPayload)
            {
                free(binaryPayload);
                CADestroyToken(token);
                CADestroyEndpoint(endpoint);
                return;
            }

            requestData.payload = (CAPayload_t) malloc(payloadLength);
            if (NULL == requestData.payload)
            {
                printf("Memory allocation failed!");
                free(binaryPayload);
                CADestroyToken(token);
                CADestroyEndpoint(endpoint);
                return;
            }
            memcpy(requestData.payload, binaryPayload, payloadLength);
            requestData.payloadSize = payloadLength;

            // memory free
            free(binaryPayload);
        }
        else
        {
            size_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI);
            requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
            if (NULL == requestData.payload)
            {
                printf("Memory allocation fail\n");
                CADestroyEndpoint(endpoint);
                CADestroyToken(token);
                return;
            }
            snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
                     (const char *) resourceURI);
            requestData.payloadSize = length;
        }
    }

    CAHeaderOption_t* headerOpt = get_option_data(&requestData);

    CARequestInfo_t requestInfo = { .method = CA_GET,
                                    .info = requestData,
                                    .isMulticast = false };

    // send request
    res = CASendRequest(endpoint, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Could not send request : %d\n", res);
    }

    if (headerOpt)
    {
        free(headerOpt);
    }

    //destroy token
    CADestroyToken(token);
    // destroy remote endpoint
    CADestroyEndpoint(endpoint);
    free(requestData.payload);

    printf("=============================================\n");
}

void send_secure_request()
{
    char uri[MAX_BUF_LEN];
    char ipv4addr[CA_IPADDR_SIZE];

    printf("\n=============================================\n");
    printf("Enter IPv4 address of the source hosting secure resource (Ex: 11.12.13.14)\n");

    if (CA_STATUS_OK != get_input_data(ipv4addr, CA_IPADDR_SIZE))
    {
        return;
    }
    snprintf(uri, MAX_BUF_LEN, "%s%s:5684/a/light", COAPS_PREFIX, ipv4addr);

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
    if (CA_STATUS_OK != res)
    {
        printf("Failed to create remote endpoint, error code: %d\n", res);
        goto exit;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if (CA_STATUS_OK != res)
    {
        printf("Token generate error, error code : %d\n", res);
        goto exit;
    }

    printf("Generated token %s\n", token);

    // create request data
    CAInfo_t requestData = { .type = CA_MSG_NONCONFIRM,
                             .messageId = 0,
                             .token = token,
                             .tokenLength = tokenLength,
                             .options = NULL,
                             .numOptions = 0,
                             .payload = NULL,
                             .payloadSize = 0,
                             .resourceUri = NULL };

    CARequestInfo_t requestInfo = { .method = CA_GET,
                                    .info = requestData,
                                    .isMulticast = false };

    // send request
    CASendRequest(endpoint, &requestInfo);

exit:
    // cleanup
    CADestroyToken(token);
    CADestroyEndpoint(endpoint);
    printf("=============================================\n");
}


void send_request_all()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("\n=============================================\n");
    printf("ex) /a/light\n");
    printf("resource uri : ");

    char resourceURI[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
    {
        return;
    }

    // create remote endpoint
    CAEndpoint_t *group = NULL;
    res = CACreateEndpoint(CA_IPV4, g_selected_nw_type, NULL, 0, &group);
    if (CA_STATUS_OK != res)
    {
        printf("Create remote endpoint error, error code: %d\n", res);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error!!\n");
        CADestroyEndpoint(group);
        return;
    }

    printf("generated token %s\n", token);

    // create request data
    CAPayload_t payload = (CAPayload_t) "TempJsonPayload";
    size_t payloadSize = strlen((const char *) payload);

    CAInfo_t requestData = { .type = CA_MSG_NONCONFIRM,
                             .messageId = 0,
                             .token = token,
                             .tokenLength = tokenLength,
                             .options = NULL,
                             .numOptions = 0,
                             .payload = payload,
                             .payloadSize = payloadSize,
                             .resourceUri = (CAURI_t) resourceURI };

    CARequestInfo_t requestInfo = { .method = CA_GET,
                                    .info = requestData,
                                    .isMulticast = true };

    CAHeaderOption_t* headerOpt = get_option_data(&requestData);

    // send request
    res = CASendRequest(group, &requestInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Could not send request to all\n");
        CADestroyToken(token);
    }
    else
    {
        CADestroyToken(g_last_request_token);
        g_last_request_token = token;
    }

    if (headerOpt)
    {
        free(headerOpt);
    }

    // destroy remote endpoint
    CADestroyEndpoint(group);

    printf("=============================================\n");
}

void send_notification()
{
    CAResult_t res = get_network_type();
    if (CA_STATUS_OK != res)
    {
        return;
    }

    printf("\n=============================================\n");
    printf("Enter the URI like below....\n");
    printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
    printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
    printf("uri : ");

    char uri[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
    {
        return;
    }

    printf("\n=============================================\n");
    printf("\tselect message type\n");
    printf("CON     : 0\n");
    printf("NON     : 1\n");
    printf("ACK     : 2\n");
    printf("RESET   : 3\n");
    printf("select : ");

    char messageTypeBuf[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(messageTypeBuf, MAX_BUF_LEN))
    {
        return;
    }

    int messageType = messageTypeBuf[0] - '0';

    CATransportFlags_t flags;
    addressSet_t address = {{}, 0};
    parsing_coap_uri(uri, &address, &flags);

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
    if (CA_STATUS_OK != res)
    {
        printf("Create remote endpoint error, error code: %d\n", res);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if ((CA_STATUS_OK != res) || (!token))
    {
        printf("Token generate error!!\n");
        CADestroyEndpoint(endpoint);
        return;
    }

    printf("Generated token %s\n", token);

    // create response data
    CAPayload_t payload = (CAPayload_t) "TempNotificationData";
    size_t payloadSize = strlen((const char *) payload);

    CAInfo_t respondData = { .type = messageType,
                             .messageId = 0,
                             .token = token,
                             .tokenLength = tokenLength,
                             .options = NULL,
                             .numOptions = 0,
                             .payload = payload,
                             .payloadSize = payloadSize,
                             .resourceUri = (CAURI_t)uri };

    CAResponseInfo_t responseInfo = { .result = CA_CONTENT,
                                      .info = respondData };

    // send request
    res = CASendNotification(endpoint, &responseInfo);
    if (CA_STATUS_OK != res)
    {
        printf("Send notification error, error code: %d\n", res);
    }
    else
    {
        printf("Send notification success\n");
    }

    // destroy token
    CADestroyToken(token);
    // destroy remote endpoint
    CADestroyEndpoint(endpoint);

    printf("\n=============================================\n");
}

void select_network()
{
    printf("\n=============================================\n");
    printf("\tselect network\n");
    printf("IP     : 0\n");
    printf("GATT   : 1\n");
    printf("RFCOMM : 2\n");
    printf("select : ");

    char buf[MAX_BUF_LEN] = { 0 };
    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
    {
        return;
    }

    int number = buf[0] - '0';

    if (number < 0 || number > 3)
    {
        printf("Invalid network type\n");
        return;
    }

    CAResult_t res = CASelectNetwork(1 << number);
    if (CA_STATUS_OK != res)
    {
        printf("Select network error\n");
    }
    else
    {
        printf("Select network success\n");
    }

    printf("=============================================\n");
}
Exemplo n.º 26
0
void SendNotification()
{
    char buf[MAX_BUF_LEN] = {0};
    char address[MAX_BUF_LEN] = {0};
    char resourceUri[MAX_BUF_LEN] = {0};
    char port[PORT_LENGTH] = {0};
    CATransportAdapter_t selectedNetwork;
    selectedNetwork = GetConnectivityType();

    Serial.println("============");
    Serial.println("10.11.12.13:4545/res_uri (for IP)");
    Serial.println("10:11:12:13:45:45/res_uri (for BT)");
    Serial.println("uri: ");

    size_t len = 0;
    GetData(buf, sizeof(buf), &len);
    if (0 >= len)
    {
        Serial.println("i/p err");
        return;
    }

    if (!ParseData(buf, address, port, resourceUri))
    {
        Serial.println("bad uri");
        return;
    }

    // create remote endpoint
    CAEndpoint_t *endpoint = NULL;
    CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, atoi(port),
                                      &endpoint);
    if (CA_STATUS_OK != res)
    {
        Serial.println("Out of memory");
        CADestroyEndpoint(endpoint);
        return;
    }

    // create token
    CAToken_t token = NULL;
    uint8_t tokenLength = CA_MAX_TOKEN_LEN;

    res = CAGenerateToken(&token, tokenLength);
    if (res != CA_STATUS_OK || (!token))
    {
        Serial.println("token error");
        return;
    }

    CAInfo_t respondData = {CA_MSG_NONCONFIRM};
    respondData.token = token;
    respondData.tokenLength = tokenLength;
    respondData.payload = (CAPayload_t)"Notification Data";
    respondData.payloadSize = strlen((const char *) respondData.payload);
    respondData.resourceUri = (char *)OICMalloc(strlen(resourceUri) + 1);
    strcpy(respondData.resourceUri, resourceUri);

    CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
    responseInfo.result = CA_CONTENT;
    responseInfo.info = respondData;

    // send request
    CASendNotification(endpoint, &responseInfo);
    // destroy remote endpoint
    if (NULL != endpoint)
    {
        CADestroyEndpoint(endpoint);
    }

    CADestroyToken(token);
    Serial.println("============");
}
Exemplo n.º 27
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;
}