// 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;
}
// 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);
}
// 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;
}
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);
}
Beispiel #5
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;
    }
}
// CADestroyToken TC
// check destroyed token
TEST_F(CATests, DestroyTokenTest)
{
    CAGenerateToken(&tempToken, tokenLength);
    CADestroyToken(tempToken);

    char * check = (char *) "destroy success";
    EXPECT_STREQ(check, "destroy success");
}
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");
}
Beispiel #8
0
int main()
{
    int ret = system("clear");
    // shell invoke error: 127, others: -1
    if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
    {
        printf("Terminal Clear Error: %d\n", ret);
        return -1;
    }

    printf("=============================================\n");
    printf("\t\tsample main\n");
    printf("=============================================\n");

    CAResult_t res = CAInitialize();
    if (CA_STATUS_OK != res)
    {
        printf("CAInitialize fail\n");
        return -1;
    }

    /*
    * Read DTLS PSK credentials from persistent storage and
    * set in the OC stack.
    */
#ifdef __WITH_DTLS__
    res = SetCredentials();
    if (CA_STATUS_OK != res)
    {
        printf("SetCredentials failed\n");
        return -1;
    }

    res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
    if (CA_STATUS_OK != res)
    {
        printf("Set credential handler fail\n");
        return -1;
    }
#endif

    // set handler.
    CARegisterHandler(request_handler, response_handler, error_handler);

    process();

    CADestroyToken(g_last_request_token);

    g_last_request_token = NULL;

    CATerminate();
#ifdef __WITH_DTLS__
    clearDtlsCredentialInfo();
#endif
    return 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);
}
Beispiel #10
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMTerminate(JNIEnv *env, jobject obj)
{
    LOGI("RMTerminate");
    if (!env || !obj)
    {
        LOGI("Invalid input parameter");
        return;
    }

    CADestroyToken(g_lastRequestToken);
    CATerminate();
    delete_global_references(env, obj);
}
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);
}
Beispiel #12
0
int main()
{
    int ret = system("clear");
    // shell invoke error: 127, others: -1
    if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
    {
        printf("Terminal Clear Error: %d\n", ret);
        return -1;
    }

    printf("=============================================\n");
    printf("\t\tsample main\n");
    printf("=============================================\n");

    CAResult_t res = CAInitialize();
    if (CA_STATUS_OK != res)
    {
        printf("CAInitialize fail\n");
        return -1;
    }

    // Set the PSK Credentials callback handler.
#ifdef __WITH_DTLS__
    res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
    if (CA_STATUS_OK != res)
    {
        printf("Register credential handler fail\n");
        return -1;
    }
#endif

    // set handler.
    CARegisterHandler(request_handler, response_handler, error_handler);

    process();

    CADestroyToken(g_last_request_token);

    g_last_request_token = NULL;

    CATerminate();
    return 0;
}
Beispiel #13
0
void DeleteClientCB(ClientCB * cbNode)
{
    if(cbNode)
    {
        LL_DELETE(cbList, cbNode);
        OC_LOG (INFO, TAG, "Deleting token");
        OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
        CADestroyToken (cbNode->token);
        OICFree(cbNode->devAddr);
        OICFree(cbNode->handle);
        OC_LOG_V (INFO, TAG, "Deleting callback with uri %s", cbNode->requestUri);
        OICFree(cbNode->requestUri);
        if(cbNode->deleteCallback)
        {
            cbNode->deleteCallback(cbNode->context);
        }

        #ifdef WITH_PRESENCE
        if(cbNode->presence)
        {
            OICFree(cbNode->presence->timeOut);
            OICFree(cbNode->presence);
        }
        if(cbNode->method == OC_REST_PRESENCE)
        {
            OCResourceType * pointer = cbNode->filterResourceType;
            OCResourceType * next = NULL;
            while(pointer)
            {
                next = pointer->next;
                OICFree(pointer->resourcetypename);
                OICFree(pointer);
                pointer = next;
            }
        }
        #endif // WITH_PRESENCE
        OICFree(cbNode);
        cbNode = NULL;
    }
}
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);
}
Beispiel #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;
}
Beispiel #16
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("============");
}
Beispiel #17
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("============");
}
Beispiel #18
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("==========");
}
Beispiel #19
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");
}
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);
}
// CAGerateToken TC
// check return value
TEST_F(CATests, GenerateTokenTestGood)
{
    EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));

    CADestroyToken(tempToken);
}
Beispiel #22
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");
}
Beispiel #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");
}
Beispiel #24
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);
}
Beispiel #25
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
                                                        jint selectedNetwork,
                                                        jint isSecured, jint msgType,
                                                        jint responseValue)
{
    LOGI("RMSendResponse");
    if (!env || !obj)
    {
        LOGI("Invalid input parameter");
        return;
    }

    LOGI("selectedNetwork - %d", selectedNetwork);

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

    if (NULL == g_clientEndpoint)
    {
        LOGE("No Request received");
        return;
    }

    CAMessageType_t messageType = msgType;

    CAInfo_t responseData = { 0 };
    responseData.type = messageType;
    responseData.messageId = g_clientMsgId;
    responseData.resourceUri = (CAURI_t)g_resourceUri;

    CAResponseInfo_t responseInfo = { 0 };

    if (msgType != CA_MSG_RESET)
    {
        responseData.token = g_clientToken;
        responseData.tokenLength = g_clientTokenLength;
        responseInfo.result = responseValue;

        if (1 == isSecured)
        {
            uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_resourceUri) + 1;
            responseData.payload = (CAPayload_t) malloc(length);
            sprintf((char *) responseData.payload, SECURE_INFO_DATA, g_resourceUri,
                    g_localSecurePort);
            responseData.payloadSize = length;
        }
        else
        {
            uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_resourceUri) + 1;
            responseData.payload = (CAPayload_t) malloc(length);
            sprintf((char *) responseData.payload, NORMAL_INFO_DATA, g_resourceUri);
            responseData.payloadSize = length;
        }
    }
    //msgType is RESET
    else
    {
        responseInfo.result = CA_EMPTY;
    }

    responseInfo.info = responseData;

    // send response
    res = CASendResponse(g_clientEndpoint, &responseInfo);
    if (CA_STATUS_OK != res)
    {
        LOGE("Could not send response");
    }

    // destroy token
    CADestroyToken(g_clientToken);
    g_clientToken = NULL;
    g_clientTokenLength = 0;

    // destroy remote endpoint
    CADestroyEndpoint(g_clientEndpoint);
    g_clientEndpoint = NULL;
}
Beispiel #26
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);
}
Beispiel #27
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");
}