From 2fba7530fa285a2ace4fbc11c3f93c11fe50b3a7 Mon Sep 17 00:00:00 2001 From: "koushik.girijala" Date: Tue, 23 Jun 2015 16:05:49 +0530 Subject: [PATCH] [CA] Fixed sample app code for Tizen and Arduino Fixed build errors and run time errors w.r.to IPv6 and endpoint api changes Change-Id: Ia2fc13cf2b0e60ee06e451725b1b283db99daa14 Signed-off-by: koushik.girijala Reviewed-on: https://gerrit.iotivity.org/gerrit/1401 Tested-by: jenkins-iotivity Reviewed-by: Abhishek Sharma Reviewed-by: Erich Keane --- .../csdk/connectivity/samples/arduino/casample.cpp | 67 +-- .../csdk/connectivity/samples/tizen/casample.c | 502 +++++++++++---------- .../connectivity/src/bt_le_adapter/caleadapter.c | 2 +- .../src/bt_le_adapter/caleadapter_singlethread.c | 17 +- .../src/bt_le_adapter/tizen/cableclient.h | 2 +- .../src/bt_le_adapter/tizen/cableserver.h | 2 +- .../src/cainterfacecontroller_singlethread.c | 4 +- .../src/camessagehandler_singlethread.c | 2 +- 8 files changed, 325 insertions(+), 273 deletions(-) diff --git a/resource/csdk/connectivity/samples/arduino/casample.cpp b/resource/csdk/connectivity/samples/arduino/casample.cpp index 13d18c1..b2e0e74 100644 --- a/resource/csdk/connectivity/samples/arduino/casample.cpp +++ b/resource/csdk/connectivity/samples/arduino/casample.cpp @@ -39,6 +39,7 @@ #define MAX_BUF_LEN 100 //1024 #define MAX_OPT_LEN 16 +#define PORT_LENGTH 5 static bool g_isLeSelected = false; @@ -94,7 +95,7 @@ void GetData(char *readInput, size_t bufferLength, size_t *dataLength) (*dataLength) = len; } -bool ParseData(char *buf, char *url, char *resourceUri) +bool ParseData(char *buf, char *url, char *port, char *resourceUri) { char *slash = strchr(buf, '/'); if (!slash) @@ -108,6 +109,12 @@ bool ParseData(char *buf, char *url, char *resourceUri) if (dot && dot < slash) { char *colon = strchr(buf, ':'); + + if (colon) + { + strncpy(port, colon, slash - colon); + memmove(port, port+1, strlen(port)); + } if (colon && colon < slash) { strncpy(url, buf, colon - buf); @@ -124,8 +131,8 @@ CATransportAdapter_t GetConnectivityType() char type[2] = {0}; Serial.println("Select network"); Serial.println("IP: 0"); - Serial.println("RFCOMM (EDR): 1"); - Serial.println("GATT (BLE): 2"); + Serial.println("GATT (BLE): 1"); + Serial.println("RFCOMM (EDR): 2"); size_t typeLen = 0; GetData(type, sizeof(type), &typeLen); @@ -139,9 +146,9 @@ CATransportAdapter_t GetConnectivityType() case '0': return CA_ADAPTER_IP; case '1': - return CA_ADAPTER_RFCOMM_BTEDR; - case '2': return CA_ADAPTER_GATT_BTLE; + case '2': + return CA_ADAPTER_RFCOMM_BTEDR; } return CA_ADAPTER_IP; } @@ -190,13 +197,10 @@ void loop() StartDiscoveryServer(); break; - case 'F': // find resource - SendRequestAll(); - break; - case 'R': // send request SendRequest(); break; + case 'E': //send request to all SendRequestAll(); break; @@ -274,6 +278,7 @@ 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(); @@ -290,7 +295,7 @@ void SendRequest() return; } - if (!ParseData(buf, address, resourceUri)) + if (!ParseData(buf, address, port, resourceUri)) { Serial.println("bad uri"); return; @@ -298,7 +303,8 @@ void SendRequest() // create remote endpoint CAEndpoint_t *endpoint = NULL; - CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, 0, &endpoint); + CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, atoi(port), + &endpoint); if (res != CA_STATUS_OK) { Serial.println("Out of memory"); @@ -348,6 +354,7 @@ void SendRequest() requestInfo.method = CA_GET; requestInfo.isMulticast = false; requestInfo.info = requestData; + requestInfo.isMulticast = false; // send request CASendRequest(endpoint, &requestInfo); @@ -370,15 +377,14 @@ 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("========="); - Serial.println("10.11.12.13:4545/resource_uri ( for IP )"); - Serial.println("10:11:12:13:45:45/resource_uri ( for BT )"); - Serial.println("/resource_uri (any adapter)"); - Serial.println("uri : "); + Serial.println("\n=============================================\n"); + Serial.println("ex) /a/light\n"); + Serial.println("resource uri : "); size_t len = 0; GetData(buf, sizeof(buf), &len); @@ -388,7 +394,7 @@ void SendRequestAll() return; } - if (!ParseData(buf, address, resourceUri)) + if (!ParseData(buf, address, port, resourceUri)) { Serial.println("bad uri"); return; @@ -396,7 +402,8 @@ void SendRequestAll() // create remote endpoint CAEndpoint_t *endpoint = NULL; - CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, 0, &endpoint); + CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, atoi(port), + &endpoint); if (res != CA_STATUS_OK) { @@ -432,7 +439,6 @@ void SendRequestAll() requestInfo.info = requestData; // send request - // CASendRequest(endpoint, &requestInfo); CASendRequest(endpoint, &requestInfo); if (NULL != token) @@ -454,6 +460,7 @@ 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(); @@ -470,7 +477,7 @@ void SendNotification() return; } - if (!ParseData(buf, address, resourceUri)) + if (!ParseData(buf, address, port, resourceUri)) { Serial.println("bad uri"); return; @@ -478,7 +485,8 @@ void SendNotification() // create remote endpoint CAEndpoint_t *endpoint = NULL; - CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, 0, &endpoint); + CAResult_t res = CACreateEndpoint(CA_DEFAULT_FLAGS, selectedNetwork, address, atoi(port), + &endpoint); if (CA_STATUS_OK != res) { Serial.println("Out of memory"); @@ -527,8 +535,8 @@ void SelectNetwork() Serial.println("============"); Serial.println("Select network"); Serial.println("IP: 0"); - Serial.println("EDR: 1"); - Serial.println("LE: 2\n"); + Serial.println("LE: 1"); + Serial.println("EDR: 2\n"); size_t len = 0; GetData(buf, sizeof(buf), &len); @@ -575,12 +583,12 @@ void SelectNetwork() #endif } break; + case 1: + g_isLeSelected = true; + break; case 2: // Nothing TBD here break; - case 3: - g_isLeSelected = true; - break; } CASelectNetwork(CATransportAdapter_t(1<(messageType); responseData.messageId = (info != NULL) ? info->messageId : 0; + responseData.resourceUri = (info != NULL) ? info->resourceUri : 0; if(messageType != 3) { responseData.token = (info != NULL) ? info->token : NULL; diff --git a/resource/csdk/connectivity/samples/tizen/casample.c b/resource/csdk/connectivity/samples/tizen/casample.c index a38af20..c2ed61d 100644 --- a/resource/csdk/connectivity/samples/tizen/casample.c +++ b/resource/csdk/connectivity/samples/tizen/casample.c @@ -57,6 +57,11 @@ #define RESOURCE_URI_LENGTH 14 +#define COAP_PREFIX "coap://" +#define COAP_PREFIX_LEN 7 +#define COAPS_PREFIX "coaps://" +#define COAPS_PREFIX_LEN 8 + /** * @def RS_IDENTITY * @brief @@ -72,9 +77,15 @@ pthread_t thread; int g_received; uint16_t g_local_secure_port = SECURE_DEFAULT_PORT; -CATransportType_t g_selected_nw_type = CA_IPV4; +CATransportAdapter_t g_selected_nw_type = CA_ADAPTER_IP; const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"}; +typedef struct +{ + char ipAddress[CA_IPADDR_SIZE]; + uint16_t port; +} addressSet_t; + char get_menu(); void process(); CAResult_t get_network_type(); @@ -82,23 +93,23 @@ CAResult_t get_input_data(char *buf, int32_t length); void start_listening_server(); void start_discovery_server(); -void find_resource(); void send_request(); void send_request_all(); void send_notification(); void select_network(); void unselect_network(); void handle_request_response(); -void find_fixed_resource(); void get_network_info(); +void send_secure_request(); -void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo); -void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo); -void error_handler(const CARemoteEndpoint_t *object, const CAErrorInfo_t* errorInfo); - -void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info); +void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo); +void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo); +void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo); +void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info); void get_resource_uri(char *URI, char *resourceURI, int length); int get_secure_information(CAPayload_t payLoad); +int get_address_set(const char *uri, addressSet_t* outAddress); +void parse_coap_uri(const char* uri, addressSet_t* address); static CAToken_t g_last_request_token = NULL; static const char SECURE_COAPS_PREFIX[] = "coaps://"; @@ -310,11 +321,6 @@ void process() start_discovery_server(); break; - case 'f': // find resource - case 'F': - find_resource(); - break; - case 'r': // send request case 'R': send_request(); @@ -340,26 +346,11 @@ void process() handle_request_response(); break; - case 'y': - case 'Y': - while (1) - { - g_received = 0; - find_fixed_resource(); - while (g_received == 0) - { - sleep(1); - handle_request_response(); - - } - } - break; - case 'w': case 'W': g_received = 0; start_discovery_server(); - //send_secure_request(); + send_secure_request(); while (g_received == 0) { sleep(1); @@ -420,81 +411,6 @@ void start_discovery_server() } } -void find_fixed_resource() -{ - // create token - CAToken_t token = NULL; - uint8_t tokenLength = CA_MAX_TOKEN_LEN; - - CAResult_t res = CAGenerateToken(&token, tokenLength); - if ((CA_STATUS_OK != res) || (!token)) - { - printf("Token generate error!!"); - return; - } - - printf("Generated token %s\n", token); - - char buf[MAX_BUF_LEN] = { 0 }; - strcpy(buf, "/a/light"); - - res = CAFindResource(buf, token, tokenLength); - if (CA_STATUS_OK != res) - { - printf("Find resource error : %d\n", res); - } - else - { - printf("Find resource to %s URI\n", buf); - } - - // delete token - CADestroyToken(token); - - printf("=============================================\n"); -} - -void find_resource() -{ - printf("\n=============================================\n"); - printf("ex) /a/light\n"); - printf("reference uri : "); - - char buf[MAX_BUF_LEN] = { 0 }; - if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) - { - return; - } - - // create token - CAToken_t token = NULL; - uint8_t tokenLength = CA_MAX_TOKEN_LEN; - - CAResult_t res = CAGenerateToken(&token, tokenLength); - if ((CA_STATUS_OK != res) || (!token)) - { - printf("Token generate error!!\n"); - return; - } - - printf("Generated token %s\n", token); - - res = CAFindResource(buf, token, tokenLength); - if (CA_STATUS_OK != res) - { - printf("Find resource error : %d\n", res); - CADestroyToken(token); - } - else - { - printf("Find resource to %s URI\n", buf); - CADestroyToken(g_last_request_token); - g_last_request_token = token; - } - - printf("=============================================\n"); -} - void send_request() { CAResult_t res = get_network_type(); @@ -535,8 +451,14 @@ void send_request() } // create remote endpoint - CARemoteEndpoint_t *endpoint = NULL; - res = CACreateRemoteEndpoint(uri, g_selected_nw_type, &endpoint); + CAEndpoint_t *endpoint = NULL; + + printf("URI : %s\n", uri); + addressSet_t address = {}; + parse_coap_uri(uri, &address); + + res = CACreateEndpoint(CA_DEFAULT_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); @@ -550,7 +472,7 @@ void send_request() char buf[MAX_BUF_LEN] = { 0 }; if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) { - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); return; } @@ -564,31 +486,31 @@ void send_request() if ((CA_STATUS_OK != res) || (!token)) { printf("Token generate error, error code : %d\n", res); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); return; } printf("Generated token %s\n", token); // extract relative resourceuri from give uri - printf("URI : %s\n", uri); - - char resourceURI[15] = {0}; + 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) { - uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI); + 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"); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); CADestroyToken(token); return; } @@ -596,12 +518,12 @@ void send_request() } else { - uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(resourceURI); + 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"); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); CADestroyToken(token); return; } @@ -612,6 +534,7 @@ void send_request() CARequestInfo_t requestInfo = { 0 }; requestInfo.method = CA_GET; requestInfo.info = requestData; + requestInfo.isMulticast = false; // send request res = CASendRequest(endpoint, &requestInfo); @@ -623,13 +546,71 @@ void send_request() //destroy token CADestroyToken(token); // destroy remote endpoint - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); free(requestData.payload); printf("=============================================\n"); } +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; + + 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"); +} + + void send_request_all() { CAResult_t res = get_network_type(); @@ -642,30 +623,29 @@ void send_request_all() printf("ex) /a/light\n"); printf("resource uri : "); - char buf[MAX_BUF_LEN] = { 0 }; - if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) + char resourceURI[MAX_BUF_LEN] = { 0 }; + if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN)) { return; } // create remote endpoint - CARemoteEndpoint_t *endpoint = NULL; - res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint); + CAEndpoint_t *endpoint = NULL; + res = CACreateEndpoint(0, g_selected_nw_type, NULL, 0, &endpoint); if (CA_STATUS_OK != res) { printf("Create remote endpoint error, error code: %d\n", res); return; } - CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t)); + CAEndpoint_t *group = (CAEndpoint_t *) malloc(sizeof(CAEndpoint_t)); if (NULL == group) { printf("Memory allocation failed!\n"); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); return; } - group->transportType = endpoint->transportType; - group->resourceUri = endpoint->resourceUri; + group->adapter = endpoint->adapter; // create token CAToken_t token = NULL; @@ -675,25 +655,27 @@ void send_request_all() if ((CA_STATUS_OK != res) || (!token)) { printf("Token generate error!!\n"); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); free(group); return; } printf("generated token %s\n", token); - CAInfo_t requestData = {CA_MSG_RESET}; + CAInfo_t requestData = { 0 }; requestData.token = token; requestData.tokenLength = tokenLength; requestData.payload = "Temp Json Payload"; requestData.type = CA_MSG_NONCONFIRM; + requestData.resourceUri = (CAURI_t)resourceURI; - CARequestInfo_t requestInfo = {CA_GET, {CA_MSG_RESET}}; + CARequestInfo_t requestInfo = { 0 }; requestInfo.method = CA_GET; requestInfo.info = requestData; + requestInfo.isMulticast = true; - // send request all - res = CASendRequestToAll(group, &requestInfo); + // send request + res = CASendRequest(group, &requestInfo); if (CA_STATUS_OK != res) { printf("Could not send request to all\n"); @@ -706,7 +688,7 @@ void send_request_all() } // destroy remote endpoint - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); free(group); printf("=============================================\n"); @@ -722,12 +704,12 @@ void send_notification() printf("\n=============================================\n"); printf("Enter the URI like below....\n"); - printf("10.11.12.13:4545/resource_uri ( for IP )\n"); - printf("10:11:12:13:45:45/resource_uri ( for BT )\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 buf[MAX_BUF_LEN] = { 0 }; - if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) + char uri[MAX_BUF_LEN] = { 0 }; + if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN)) { return; } @@ -736,6 +718,9 @@ void send_notification() 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 }; @@ -759,9 +744,12 @@ void send_notification() return; } + addressSet_t address = {}; + parse_coap_uri(uri, &address); + // create remote endpoint - CARemoteEndpoint_t *endpoint = NULL; - res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint); + CAEndpoint_t *endpoint = NULL; + res = CACreateEndpoint(0, g_selected_nw_type, address.ipAddress, address.port, &endpoint); if (CA_STATUS_OK != res) { printf("Create remote endpoint error, error code: %d\n", res); @@ -776,7 +764,7 @@ void send_notification() if ((CA_STATUS_OK != res) || (!token)) { printf("Token generate error!!\n"); - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); return; } @@ -787,6 +775,7 @@ void send_notification() respondData.tokenLength = tokenLength; respondData.payload = "Temp Notification Data"; respondData.type = messageType; + respondData.resourceUri = (CAURI_t)uri; CAResponseInfo_t responseInfo = { 0 }; responseInfo.result = CA_CONTENT; @@ -806,7 +795,7 @@ void send_notification() // destroy token CADestroyToken(token); // destroy remote endpoint - CADestroyRemoteEndpoint(endpoint); + CADestroyEndpoint(endpoint); printf("\n=============================================\n"); } @@ -815,9 +804,9 @@ void select_network() { printf("\n=============================================\n"); printf("\tselect network\n"); - printf("IPv4 : 0\n"); - printf("EDR : 2\n"); - printf("LE : 3\n"); + printf("IP : 0\n"); + printf("GATT : 1\n"); + printf("RFCOMM : 2\n"); printf("select : "); char buf[MAX_BUF_LEN] = { 0 }; @@ -844,7 +833,6 @@ void select_network() { printf("Select network success\n"); } - printf("=============================================\n"); } @@ -852,9 +840,9 @@ void unselect_network() { printf("\n=============================================\n"); printf("\tunselect enabled network\n"); - printf("IPv4 : 0\n"); - printf("EDR : 2\n"); - printf("LE : 3\n"); + printf("IP : 0\n"); + printf("GATT : 1\n"); + printf("RFCOMM : 2\n"); printf("select : "); char buf[MAX_BUF_LEN] = { 0 }; @@ -890,16 +878,13 @@ char get_menu() printf("\t\tMenu\n"); printf("\ts : start server\n"); printf("\tc : start client\n"); - printf("\tf : find resource\n"); printf("\tr : send request\n"); printf("\tt : send request to all\n"); - printf("\ta : advertise resource\n"); printf("\tb : send notification\n"); printf("\tn : select network\n"); printf("\tx : unselect network\n"); printf("\tg : get network information\n"); printf("\th : handle request response\n"); - printf("\ty : run static client\n"); printf("\tz : run static server\n"); printf("\tw : send secure request\n"); printf("\tq : quit\n"); @@ -932,7 +917,7 @@ void handle_request_response() void get_network_info() { - CALocalConnectivity_t *tempInfo = NULL; + CAEndpoint_t *tempInfo = NULL; uint32_t tempSize = 0; CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize); @@ -949,21 +934,22 @@ void get_network_info() int index; for (index = 0; index < tempSize; index++) { - printf("Type: %d\n", tempInfo[index].type); - if (CA_IPV4 == tempInfo[index].type) + printf("Type: %d\n", tempInfo[index].adapter); + if (CA_ADAPTER_IP == tempInfo[index].adapter) { - printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress); - printf("Port: %d\n", tempInfo[index].addressInfo.IP.port); + printf("Address: %s\n", tempInfo[index].addr); + printf("Port: %d\n", tempInfo[index].port); } - else if (CA_EDR == tempInfo[index].type) + else { - printf("Address: %s\n", tempInfo[index].addressInfo.BT.btMacAddress); + printf("Address: %s\n", tempInfo[index].addr); } - printf("Secured: %d\n\n", tempInfo[index].isSecured); - if (tempInfo[index].isSecured) + printf("Secured: %s\n\n", (tempInfo[index].flags & CA_SECURE) ? "true" : "false"); + + if (tempInfo[index].flags & CA_SECURE) { - g_local_secure_port = tempInfo[index].addressInfo.IP.port; + g_local_secure_port = tempInfo[index].port; printf("Secured: in global %d\n\n", g_local_secure_port); } } @@ -972,7 +958,7 @@ void get_network_info() printf("##############################################################"); } -void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo) +void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo) { if (NULL == object || NULL == requestInfo) { @@ -989,15 +975,14 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re } printf("##########received request from remote device #############\n"); - printf("Uri: %s\n", object->resourceUri); - if (CA_IPV4 == object->transportType) + if (CA_ADAPTER_IP == object->adapter) { - printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress, - object->addressInfo.IP.port, object->isSecured); + printf("Remote Address: %s Port: %d secured:%d\n", object->addr, + object->port, object->flags & CA_SECURE); } - else if (CA_EDR == object->transportType) + else { - printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress); + printf("Remote Address: %s \n", object->addr); } printf("Data: %s\n", requestInfo->info.payload); printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]); @@ -1018,7 +1003,7 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re //Check if this has secure communication information if (requestInfo->info.payload && - (CA_IPV4 == object->transportType)) + (CA_ADAPTER_IP == object->adapter)) { int securePort = get_secure_information(requestInfo->info.payload); if (0 < securePort) //Set the remote endpoint secure details and send response @@ -1026,31 +1011,24 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re printf("This is secure resource...\n"); //length of "coaps://" - int length = sizeof(SECURE_COAPS_PREFIX) - 1; + size_t length = sizeof(SECURE_COAPS_PREFIX) - 1; // length of "ipaddress:port" - length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH; - length += strlen(object->resourceUri) + 1; + length += strlen(object->addr) + PORT_LENGTH; + length += 1; - char *uri = calloc(1, sizeof(char) * length); - if (!uri) - { - printf("Failed to create new uri\n"); - return; - } - sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress, - object->addressInfo.IP.port, object->resourceUri); + printf("%s%s:%d/", SECURE_COAPS_PREFIX, object->addr, + object->port); - CARemoteEndpoint_t *endpoint = NULL; - if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint)) + CAEndpoint_t *endpoint = NULL; + if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr, + object->port, &endpoint)) { printf("Failed to create duplicate of remote endpoint!\n"); return; } - endpoint->isSecured = true; + endpoint->flags = CA_SECURE; object = endpoint; - - free(uri); } } @@ -1060,19 +1038,20 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re g_received = 1; } -void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo) +void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo) { printf("##########Received response from remote device #############\n"); - printf("Uri: %s\n", object->resourceUri); - if (CA_IPV4 == object->transportType) + if (CA_ADAPTER_IP == object->adapter) { - printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress, - object->addressInfo.IP.port, object->isSecured); + printf("Remote Address: %s Port: %d secured:%d\n", object->addr, + object->port, object->flags & CA_SECURE); } - else if (CA_EDR == object->transportType) + else { - printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress); + printf("Remote Address: %s \n", object->addr); } + + printf("resource uri : %s\n", responseInfo->info.resourceUri); printf("response result : %d\n", responseInfo->result); printf("Data: %s\n", responseInfo->info.payload); printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]); @@ -1103,19 +1082,10 @@ void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t * } } -void error_handler(const CARemoteEndpoint_t *rep, const CAErrorInfo_t* errorInfo) +void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo) { printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n"); - if(rep && rep->resourceUri ) - { - printf("Error Handler, RemoteEndpoint Info resourceUri : %s\n", rep->resourceUri); - } - else - { - printf("Error Handler, RemoteEndpoint is NULL"); - } - if(errorInfo) { const CAInfo_t *info = &errorInfo->info; @@ -1124,6 +1094,7 @@ void error_handler(const CARemoteEndpoint_t *rep, const CAErrorInfo_t* errorInfo printf("Error Handler token : %s\n", info->token); printf("Error Handler messageId : %d\n", (uint16_t) info->messageId); printf("Error Handler type : %d\n", info->type); + printf("Error Handler resourceUri : %s\n", info->resourceUri); printf("Error Handler payload : %s\n", info->payload); if(CA_ADAPTER_NOT_ENABLED == errorInfo->result) @@ -1152,7 +1123,7 @@ void error_handler(const CARemoteEndpoint_t *rep, const CAErrorInfo_t* errorInfo return; } -void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info) +void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info) { printf("entering send_response\n"); @@ -1199,39 +1170,41 @@ void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info) } CAInfo_t responseData = { 0 }; responseData.type = messageType; - responseData.messageId = (info != NULL) ? info->messageId : 0; + responseData.resourceUri = (info != NULL) ? info->resourceUri : 0; + if(CA_MSG_RESET != messageType) { responseData.token = (info != NULL) ? info->token : NULL; responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; - if (endpoint->isSecured) + if (endpoint->flags & CA_SECURE) { printf("Sending response on secure communication\n"); - uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(endpoint->resourceUri); + uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri) + + sizeof(g_local_secure_port); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } - snprintf(responseData.payload, length, SECURE_INFO_DATA, endpoint->resourceUri, + snprintf(responseData.payload, length, SECURE_INFO_DATA, responseData.resourceUri, g_local_secure_port); } else { printf("Sending response on non-secure communication\n"); - uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(endpoint->resourceUri); + uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } - snprintf(responseData.payload, length, NORMAL_INFO_DATA, endpoint->resourceUri); + snprintf(responseData.payload, length, NORMAL_INFO_DATA, responseData.resourceUri); } } @@ -1290,12 +1263,6 @@ int get_secure_information(CAPayload_t payLoad) return -1; } - if(((endPos - 1) - startPos) > 4) - { - printf("port length is not proper.Exceeding length 4\n"); - return -1; - } - char portStr[4] = {0}; memcpy(portStr, startPos + 1, (endPos - 1) - startPos); @@ -1336,9 +1303,9 @@ CAResult_t get_network_type() { printf("\n=============================================\n"); printf("\tselect network type\n"); - printf("IPv4 : 0\n"); - printf("BT : 2\n"); - printf("LE : 3\n"); + printf("IP : 0\n"); + printf("GATT : 1\n"); + printf("RFCOMM : 2\n"); printf("select : "); char buf[MAX_BUF_LEN] = { 0 }; @@ -1350,29 +1317,23 @@ CAResult_t get_network_type() int number = buf[0] - '0'; number = (number < 0 || number > 3) ? 0 : 1 << number; - if (!(number & 0xf)) + if (number == 1) { - return CA_NOT_SUPPORTED; - } - if (number & CA_IPV4) - { - g_selected_nw_type = CA_IPV4; + g_selected_nw_type = CA_ADAPTER_IP; return CA_STATUS_OK; } - if (number & CA_EDR) + if (number == 2) { - g_selected_nw_type = CA_EDR; + g_selected_nw_type = CA_ADAPTER_GATT_BTLE; return CA_STATUS_OK; } - if (number & CA_LE) + if (number == 3) { - g_selected_nw_type = CA_LE; + g_selected_nw_type = CA_ADAPTER_RFCOMM_BTEDR; return CA_STATUS_OK; } - printf("\n=============================================\n"); - - return CA_STATUS_FAILED; + return CA_NOT_SUPPORTED; } CAResult_t get_input_data(char *buf, int32_t length) @@ -1391,3 +1352,88 @@ CAResult_t get_input_data(char *buf, int32_t length) return CA_STATUS_OK; } + + +void parse_coap_uri(const char* uri, addressSet_t* address) +{ + if (NULL == uri) + { + printf("parameter is null\n"); + return; + } + + // parse uri + // #1. check prefix + uint8_t startIndex = 0; + if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0) + { + printf("uri has '%s' prefix\n", COAPS_PREFIX); + startIndex = COAPS_PREFIX_LEN; + } + else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0) + { + printf("uri has '%s' prefix\n", COAP_PREFIX); + startIndex = COAP_PREFIX_LEN; + } + + // #2. copy uri for parse + int32_t len = strlen(uri) - startIndex; + + if (len <= 0) + { + printf("uri length is 0!\n"); + return; + } + + int res = get_address_set(uri + startIndex, address); + if (res == -1) + { + printf("address parse error\n"); + return; + } + + return; +} + +int get_address_set(const char *uri, addressSet_t* outAddress) +{ + if (NULL == uri || NULL == outAddress) + { + printf("parameter is null !\n"); + return -1; + } + + int32_t len = strlen(uri); + if (len <= 0) + { + printf("uri length is 0!\n"); + return -1; + } + + int32_t isIp = 0; + int32_t ipLen = 0; + for (int i = 0; i < len; i++) + { + if (uri[i] == '.') + { + isIp = 1; + } + + // found port number start index + if (isIp && uri[i] == ':') + { + ipLen = i; + outAddress->port = atoi(uri + ipLen + 1); + break; + } + + if (uri[i] == '/') + { + break; + } + + outAddress->ipAddress[i] = uri[i]; + } + + return isIp; +} diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 3528ac8..e5e8385 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -47,7 +47,7 @@ static CANetworkChangeCallback g_networkCallback = NULL; * @var g_localBLEAddress * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later. */ -static char g_localBLEAddress[16] = {0}; +static char g_localBLEAddress[18] = {0}; /** * @var g_isServer diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c index c5b90c9..de6d2c8 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter_singlethread.c @@ -100,7 +100,7 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, connHandler.readData = CAReadLEData; connHandler.stopAdapter = CAStopLE; connHandler.terminate = CATerminateLE; - registerCallback(connHandler, CA_LE); + registerCallback(connHandler, CA_ADAPTER_GATT_BTLE); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } @@ -149,7 +149,7 @@ CAResult_t CAStartLENotifyServer() return CA_STATUS_OK; } -uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *data, +uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen) { OIC_LOG(DEBUG, TAG, "IN"); @@ -157,7 +157,7 @@ uint32_t CASendLENotification(const CARemoteEndpoint_t *endpoint, const void *da return 1; } -int32_t CASendLEUnicastData(const CARemoteEndpoint_t *remoteEndpoint, const void *data, +int32_t CASendLEUnicastData(const CAEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen) { OIC_LOG(DEBUG, TAG, "IN"); @@ -183,7 +183,7 @@ int32_t CASendLEMulticastData(const void *data, uint32_t dataLen) return CASendLEData(data, dataLen); } -CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size) +CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size) { OIC_LOG(DEBUG, TAG, "IN"); @@ -212,7 +212,7 @@ CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t **info, uint32_t *s /** * Create local endpoint using util function */ - (*info) = CAAdapterCreateLocalEndpoint(CA_LE, leAddress); + (*info) = CAAdapterCreateLocalEndpoint(CA_ADAPTER_GATT_BTLE, leAddress); if (NULL == (*info)) { OIC_LOG(ERROR, TAG, "malloc fail"); @@ -294,11 +294,10 @@ void CANotifyCallback(const void *data, int32_t dataLen, const char *senderAdrs, { /* Cannot get Address as of now */ - CARemoteEndpoint_t endPoint; - endPoint.resourceUri = ""; // will be filled by upper layer - endPoint.transportType= CA_LE; + CAEndpoint_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_IPV4, CA_ADAPTER_GATT_BTLE, + senderAdrs, senderPort); - g_respCallback(&endPoint, data, dataLen); + g_respCallback(localEndpoint, data, dataLen); } OIC_LOG(DEBUG, TAG, "OUT"); } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableclient.h b/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableclient.h index 9985153..b220a01 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableclient.h @@ -372,7 +372,7 @@ CAResult_t CASetCharacteristicDescriptorValue * @retval #CA_STATUS_FAILED Operation failed */ CAResult_t CABleClientSenderQueueEnqueueMessage - (const CARemoteEndpoint_t *remoteEndpoint, + (const CAEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen); /** diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableserver.h b/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableserver.h index fc087ae..9d83d93 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableserver.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/cableserver.h @@ -212,7 +212,7 @@ CAResult_t CALEReadDataFromLEServer(); * @retval #CA_STATUS_FAILED Operation failed */ CAResult_t CABleServerSenderQueueEnqueueMessage - (const CARemoteEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen); + (const CAEndpoint_t *remoteEndpoint, const void *data, uint32_t dataLen); /** * @brief This is the thread which will be used for processing receiver queue. diff --git a/resource/csdk/connectivity/src/cainterfacecontroller_singlethread.c b/resource/csdk/connectivity/src/cainterfacecontroller_singlethread.c index b72f23b..1818df3 100644 --- a/resource/csdk/connectivity/src/cainterfacecontroller_singlethread.c +++ b/resource/csdk/connectivity/src/cainterfacecontroller_singlethread.c @@ -39,7 +39,7 @@ #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\ goto memory_error_exit;} } -#define CA_CONNECTIVITY_TYPE_NUM 4 +#define CA_CONNECTIVITY_TYPE_NUM 3 static CAConnectivityHandler_t g_adapterHandler[CA_CONNECTIVITY_TYPE_NUM]; @@ -61,7 +61,7 @@ static int CAGetAdapterIndex(CATransportAdapter_t cType) return 2; } - OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4"); + OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 3"); return -1; } diff --git a/resource/csdk/connectivity/src/camessagehandler_singlethread.c b/resource/csdk/connectivity/src/camessagehandler_singlethread.c index d687d45..c42d252 100644 --- a/resource/csdk/connectivity/src/camessagehandler_singlethread.c +++ b/resource/csdk/connectivity/src/camessagehandler_singlethread.c @@ -343,7 +343,7 @@ CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInf CA_MEMORY_ALLOC_CHECK(data); // save data - data->type = SEND_TYPE_UNICAST; + data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST; data->remoteEndpoint = object; data->requestInfo = request; data->responseInfo = NULL; -- 2.7.4