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; } }
// CAGetNetworkInformation TC TEST_F (CATests, GetNetworkInformationTest) { uint32_t tempSize = 0; CAEndpoint_t *tempInfo = NULL; EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize)); free(tempInfo); }
JNIEXPORT void JNICALL Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj, jint networkType) { LOGI("RMSelectNetwork Type : %d", networkType); if (!env || !obj) { LOGI("Invalid input parameter"); return; } if (CA_STATUS_OK != CASelectNetwork(networkType)) { LOGE("Could not select network"); } }
CAResult_t checkSelectNetwork() { CAResult_t res = CASelectNetwork(CA_ADAPTER_IP); if (CA_STATUS_OK == res) { EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP)); return CA_STATUS_OK; } if (CA_NOT_SUPPORTED == res) { EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP)); return CA_STATUS_OK; } return res; }
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 select_network() { printf("\n=============================================\n"); printf("\tselect network\n"); printf("IP : 0\n"); printf("GATT : 1\n"); printf("RFCOMM : 2\n"); printf("TCP : 4\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 > 4) { printf("Invalid network type\n"); return; } CAResult_t res = CASelectNetwork(1 << number); if (CA_STATUS_OK != res) { printf("Select network error\n"); g_selected_nw_type = 1 << number; } else { printf("Select network success\n"); } printf("=============================================\n"); }
void SelectNetwork() { char buf[MAX_BUF_LEN] = {0}; Serial.println("============"); Serial.println("Select network"); Serial.println("IP: 0"); Serial.println("LE: 1"); Serial.println("EDR: 2\n"); size_t len = 0; GetData(buf, sizeof(buf), &len); int number = buf[0] - '0'; if (0 >= len || number < 0 || number > 3) { Serial.println("Wrong i/p. WIFI selected"); number = 1; } switch (number) { case 0: { #ifdef ARDUINOWIFI const char ssid[] = "SSID"; // your network SSID (name) const char pass[] = "SSID_Password"; // your network password int16_t status = WL_IDLE_STATUS; // the Wifi radio's status if (WiFi.status() == WL_NO_SHIELD) { Serial.println("ERROR:No Shield"); return; } while (status != WL_CONNECTED) { Serial.print("connecting: "); Serial.println(ssid); // WiFi.begin api is weird. ssid should also be taken as const char * // Connect to WPA/WPA2 network: status = WiFi.begin((char *)ssid, pass); } #elif defined ARDUINOETH // Note: ****Update the MAC address here with your shield's MAC address**** uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xC4, 0x05}; uint8_t error = Ethernet.begin(ETHERNET_MAC); if (error == 0) { Serial.print("Failed: "); Serial.println(error); return; } #endif } break; case 1: g_isLeSelected = true; break; case 2: // Nothing TBD here break; } CASelectNetwork(CATransportAdapter_t(1<<number)); Serial.println("============"); }
// check return value when selected network is disable TEST_F(CATests, SelectNetworkTestBad) { //Select disable network EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t) CA_TRANSPORT_ADAPTER_SCOPE)); }
// CAStartListeningServer TC // check return value TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01) { CASelectNetwork(CA_ADAPTER_IP); EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer()); }
// CAStopListeningServer TC TEST_F(CATests, CAStopListeningServerTest) { EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer()); }
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"); }