Add CI Adapter to support CoAP over TCP
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / linux / sample_main.c
index 0f4fc25..f432aa9 100644 (file)
@@ -44,7 +44,9 @@
 #define SYSTEM_INVOKE_ERROR 127
 #define SYSTEM_ERROR -1
 
+#ifdef WITH_BWT
 #define BLOCK_SIZE(arg) (1 << ((arg) + 4))
+#endif
 
 /**
  * @def RS_IDENTITY
@@ -87,6 +89,9 @@ void unselect_network();
 void handle_request_response();
 void get_network_info();
 void send_secure_request();
+#ifdef CI_ADAPTER
+void create_tcp_connection();
+#endif
 
 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
@@ -102,8 +107,11 @@ static CAToken_t g_last_request_token = NULL;
 
 static const char COAP_PREFIX[] =  "coap://";
 static const char COAPS_PREFIX[] = "coaps://";
+static const char COAP_TCP_PREFIX[] =  "coap+tcp://";
+
 static const uint16_t COAP_PREFIX_LEN = sizeof(COAP_PREFIX) - 1;
 static const uint16_t COAPS_PREFIX_LEN = sizeof(COAPS_PREFIX) - 1;
+static const uint16_t COAP_TCP_PREFIX_LEN = sizeof(COAP_TCP_PREFIX) - 1;
 
 static const char SECURE_INFO_DATA[] =
                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
@@ -341,6 +349,13 @@ void process()
                 get_network_info();
                 break;
 
+#ifdef CI_ADAPTER
+            case 'p': // create tcp connection
+            case 'P':
+                create_tcp_connection();
+                break;
+#endif
+
             default:
                 printf("not supported menu!!\n");
                 break;
@@ -434,6 +449,9 @@ void send_request()
         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");
+#ifdef CI_ADAPTER
+        printf("coap+tcp://10:11:12:13:45:45/resource_uri ( for CI )\n");
+#endif
     }
     else
     {
@@ -754,6 +772,9 @@ void send_notification()
     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");
+#ifdef CI_ADAPTER
+    printf("coap+tcp://10:11:12:13:45:45/resource_uri ( for CI )\n");
+#endif
     printf("uri : ");
 
     char uri[MAX_BUF_LEN] = { 0 };
@@ -848,6 +869,9 @@ void select_network()
     printf("IP     : 0\n");
     printf("GATT   : 1\n");
     printf("RFCOMM : 2\n");
+#ifdef CI_ADAPTER
+    printf("CI     : 5\n");
+#endif
     printf("select : ");
 
     char buf[MAX_BUF_LEN] = { 0 };
@@ -858,7 +882,7 @@ void select_network()
 
     int number = buf[0] - '0';
 
-    if (number < 0 || number > 3)
+    if (number < 0 || number > 5)
     {
         printf("Invalid network type\n");
         return;
@@ -884,6 +908,9 @@ void unselect_network()
     printf("IP     : 0\n");
     printf("GATT   : 1\n");
     printf("RFCOMM : 2\n");
+#ifdef CI_ADAPTER
+    printf("CI     : 5\n");
+#endif
     printf("select : ");
 
     char buf[MAX_BUF_LEN] = { 0 };
@@ -894,7 +921,7 @@ void unselect_network()
 
     int number = buf[0] - '0';
 
-    if (number < 0 || number > 3)
+    if (number < 0 || number > 5)
     {
         printf("Invalid network type\n");
         return;
@@ -928,6 +955,9 @@ char get_menu()
     printf("\th : handle request response\n");
     printf("\tz : run static server\n");
     printf("\tw : send secure request\n");
+#ifdef CI_ADAPTER
+    printf("\tp : create tcp connection\n");
+#endif
     printf("\tq : quit\n");
     printf("=============================================\n");
     printf("select : ");
@@ -1057,12 +1087,14 @@ void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestI
         }
     }
 
+#ifdef WITH_BWT
     // if received message is bulk data, create output file
     if ((requestInfo->info.payload) &&
             (requestInfo->info.payloadSize > BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE)))
     {
         create_file(requestInfo->info.payload, requestInfo->info.payloadSize);
     }
+#endif
 
     printf("Send response with URI\n");
     send_response(object, &requestInfo->info);
@@ -1111,12 +1143,14 @@ void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *respon
         }
     }
 
+#ifdef WITH_BWT
     // if received message is bulk data, create output file
     if ((responseInfo->info.payload) &&
             (responseInfo->info.payloadSize > BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE)))
     {
         create_file(responseInfo->info.payload, responseInfo->info.payloadSize);
     }
+#endif
 }
 
 void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
@@ -1404,6 +1438,9 @@ CAResult_t get_network_type()
     printf("IP     : 0\n");
     printf("GATT   : 1\n");
     printf("RFCOMM : 2\n");
+#ifdef CI_ADAPTER
+    printf("CI     : 5\n");
+#endif
     printf("select : ");
 
     if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
@@ -1413,25 +1450,21 @@ CAResult_t get_network_type()
 
     int number = buf[0] - '0';
 
-    number = (number < 0 || number > 3) ? 0 : 1 << number;
+    number = (number < 0 || number > 5) ? 0 : 1 << number;
 
-    if (number == 1)
+    switch (number)
     {
-        g_selected_nw_type = CA_ADAPTER_IP;
-        return CA_STATUS_OK;
-    }
-    if (number == 2)
-    {
-        g_selected_nw_type = CA_ADAPTER_GATT_BTLE;
-        return CA_STATUS_OK;
-    }
-    if (number == 3)
-    {
-        g_selected_nw_type = CA_ADAPTER_RFCOMM_BTEDR;
-        return CA_STATUS_OK;
+        case CA_ADAPTER_IP:
+        case CA_ADAPTER_GATT_BTLE:
+        case CA_ADAPTER_RFCOMM_BTEDR:
+#ifdef CI_ADAPTER
+        case CA_ADAPTER_CLOUD_INTERFACE:
+#endif
+            g_selected_nw_type = number;
+            return CA_STATUS_OK;
+        default:
+            return CA_NOT_SUPPORTED;
     }
-
-    return CA_NOT_SUPPORTED;
 }
 
 CAResult_t get_input_data(char *buf, int32_t length)
@@ -1537,6 +1570,14 @@ void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t
         startIndex = COAP_PREFIX_LEN;
         *flags = CA_IPV4;
     }
+#ifdef CI_ADAPTER
+    else if (strncmp(COAP_TCP_PREFIX, uri, COAP_TCP_PREFIX_LEN) == 0)
+    {
+        printf("uri has '%s' prefix\n", COAP_TCP_PREFIX);
+        startIndex = COAP_TCP_PREFIX_LEN;
+        *flags = CA_IPV4_TCP;
+    }
+#endif
 
     // #2. copy uri for parse
     int32_t len = strlen(uri) - startIndex;
@@ -1689,3 +1730,32 @@ bool read_file(const char* name, CAPayload_t* bytes, size_t* length)
 
     return true;
 }
+
+#ifdef CI_ADAPTER
+void create_tcp_connection()
+{
+    printf("Enter the CI Server information....\n");
+    printf("IP: ");
+    char address[MAX_BUF_LEN] = {'\0'};
+    if (CA_STATUS_OK != get_input_data(address, MAX_BUF_LEN))
+    {
+        return;
+    }
+    printf("Port: ");
+    char port[MAX_BUF_LEN] = {'\0'};
+    if (CA_STATUS_OK != get_input_data(port, MAX_BUF_LEN))
+    {
+        return;
+    }
+
+    int portNum = atoi(port);
+    CACIServerInfo_t serverInfo = { .u4tcp.port = portNum };
+    strncpy(serverInfo.addr, address, strlen(address));
+
+    CAResult_t res = CACreateTCPConnection(&serverInfo);
+    if (CA_STATUS_OK != res)
+    {
+        printf("Failed to create TCP Connection");
+    }
+}
+#endif