// 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; }
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"); }
// 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)); CADestroyEndpoint(tempRep); tempRep = NULL; }
OCStackResult SendDisconnectMessage(const KeepAliveEntry_t *entry) { VERIFY_NON_NULL(entry, FATAL, OC_STACK_INVALID_PARAM); /* * Send empty message to disconnect a connection. * If CA get the empty message from RI, CA will disconnect a connection. */ CARequestInfo_t requestInfo = { .method = CA_PUT }; return CASendRequest(&entry->remoteAddr, &requestInfo); }
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; }
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("=========="); }
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("============"); }
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); }
JNIEXPORT void JNICALL Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri, jstring payload, jint selectedNetwork, jint isSecured, jint msgType) { 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) { 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; if (1 == isSecured) { uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI); requestData.payload = (CAPayload_t) malloc(length); if (NULL == requestData.payload) { LOGE("Memory allocation failed!"); // destroy token CADestroyToken(token); // destroy remote endpoint CADestroyEndpoint(endpoint); return; } snprintf((char *) requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort); requestData.payloadSize = length; } else { uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI); requestData.payload = (CAPayload_t) malloc(length); if (NULL == requestData.payload) { LOGE("Memory allocation failed!"); // destroy token CADestroyToken(token); // destroy remote endpoint CADestroyEndpoint(endpoint); return; } snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA, resourceURI); requestData.payloadSize = length; } 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); }
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"); }
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"); }
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"); }
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"); }