Implementation of connectivity abstraction feature Release v0.61
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / casample.c
index 22961ff..60edefd 100644 (file)
@@ -54,8 +54,8 @@ void select_network();
 void unselect_network();
 void handle_request_response();
 
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo);
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo);
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
+void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
 void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
 void terminate();
 
@@ -66,7 +66,6 @@ void pthread_func()
 
 int main()
 {
-    system("clear");
 
     printf("=============================================\n");
     printf("\t\tsample main\n");
@@ -92,6 +91,7 @@ void process()
 
             case 'I': // Initialize interface
                 initialize();
+                break;
 
             case 'S': // start server
                 start_listening_server();
@@ -140,7 +140,7 @@ void process()
 void initialize()
 {
     mainloop = g_main_loop_new(NULL, FALSE);
-    pthread_create(&thread, NULL, (void *) &pthread_func, NULL);
+    pthread_create (&thread, NULL, (void *) &pthread_func, NULL);
 
     CAInitialize();
 
@@ -179,7 +179,18 @@ void find_resource()
 
     gets(buf);
 
-    CAResult_t res = CAFindResource(buf);
+    // create token
+    CAToken_t token = NULL;
+    CAResult_t res = CAGenerateToken(&token);
+    if (res != CA_STATUS_OK)
+    {
+        printf("token generate error!!\n");
+        token = NULL;
+    }
+
+    printf("generated token %s\n", (token != NULL) ? token : "");
+
+    res = CAFindResource(buf, token);
 
     if (res != CA_STATUS_OK)
     {
@@ -286,7 +297,7 @@ void advertise_resource()
     }
 
     int i;
-    for (i = 0; i < optionNum; i++)
+    for (i = 0 ; i < optionNum ; i++)
     {
         int optionID = 0;
         printf("[%d] Option ID : ", i + 1);
@@ -297,61 +308,27 @@ void advertise_resource()
         printf("[%d] Option Data : ", i + 1);
         scanf("%s", optionData);
         memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
-        printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
+        printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
 
-        headerOpt[i].optionLength = (uint16_t) strlen(optionData);
+        headerOpt[i].optionLength = (uint16_t)strlen(optionData);
     }
     printf("\n=============================================\n");
 
-    CAAdvertiseResource(buf, headerOpt, (uint8_t) optionNum);
-
-    free(headerOpt);
-
-}
-
-void send_notification()
-{
-    char buf[MAX_BUF_LEN];
-
-    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
-
-    printf("\n=============================================\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("uri : ");
-
-    gets(buf);
-
-    // create remote endpoint
-    CARemoteEndpoint_t *endpoint = NULL;
-    CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
-
+    // create token
+    CAToken_t token = NULL;
+    CAResult_t res = CAGenerateToken(&token);
     if (res != CA_STATUS_OK)
     {
-        printf("create remote endpoint error!!");
-        CADestroyRemoteEndpoint(endpoint);
-        return;
+        printf("token generate error!!\n");
+        token = NULL;
     }
 
-    CAInfo_t respondeData;
-    memset(&respondeData, 0, sizeof(CAInfo_t));
-    respondeData.token = "client token";
-    respondeData.payload = "Temp Notification Data";
+    printf("generated token %s\n", (token != NULL) ? token : "");
 
-    CAResponseInfo_t responseInfo;
-    memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
-    responseInfo.result = CA_CONTENT;
-    responseInfo.info = respondeData;
+    CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
 
-    // send request
-    CASendNotification(endpoint, &responseInfo);
+    free(headerOpt);
 
-    // destroy remote endpoint
-    if (endpoint != NULL)
-    {
-        CADestroyRemoteEndpoint(endpoint);
-    }
-    printf("\n=============================================\n");
 }
 
 void select_network()
@@ -437,28 +414,26 @@ void handle_request_response()
     CAHandleRequestResponse();
 }
 
-void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* requestInfo)
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
 {
 
     printf("[CALLBACK] request_handler, uri : %s, data : %s\n",
-            (object != NULL) ? object->resourceUri : "",
-            (requestInfo != NULL) ? requestInfo->info.payload : "");
+           (object != NULL) ? object->resourceUri : "",
+           (requestInfo != NULL) ? requestInfo->info.payload : "");
 
     printf("[CALLBACK] request_handler, address : %s\n",
-            (object != NULL) ? object->addressInfo.IP.ipAddress : "");
+           (object != NULL) ? object->addressInfo.IP.ipAddress : "");
 
     if (requestInfo->info.options)
     {
-        uint32_t len = requestInfo->info.numOptions;
+        uint32_t len =  requestInfo->info.numOptions;
         uint32_t i;
-        for (i = 0; i < len; i++)
+        for (i = 0 ; i < len ; i++)
         {
-            printf("[CALLBACK] request_handler, option ID : %d\n",
-                    requestInfo->info.options[i].optionID);
+            printf("[CALLBACK] request_handler, option ID : %d\n", requestInfo->info.options[i].optionID);
             printf("[CALLBACK] request_handler, options data length : %d\n",
-                    requestInfo->info.options[i].optionLength);
-            printf("[CALLBACK] request_handler, options data : %s\n",
-                    requestInfo->info.options[i].optionData);
+                   requestInfo->info.options[i].optionLength);
+            printf("[CALLBACK] request_handler, options data : %s\n", requestInfo->info.options[i].optionData );
         }
     }
 
@@ -467,31 +442,32 @@ void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* re
 
 }
 
-void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
+void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
 {
 
     printf("[CALLBACK] response_handler, uri : %s, data : %s\n",
-            (object != NULL) ? object->resourceUri : "",
-            (responseInfo != NULL) ? responseInfo->info.payload : "");
+           (object != NULL) ? object->resourceUri : "",
+           (responseInfo != NULL) ? responseInfo->info.payload : "");
 
     printf("[CALLBACK] response_handler, address : %s\n",
-            (object != NULL) ? object->addressInfo.IP.ipAddress : "");
+           (object != NULL) ? object->addressInfo.IP.ipAddress : "");
 
     if (responseInfo->info.options)
     {
-        uint32_t len = responseInfo->info.numOptions;
+        uint32_t len =  responseInfo->info.numOptions;
         uint32_t i;
-        for (i = 0; i < len; i++)
+        for (i = 0 ; i < len ; i++)
         {
-            printf("[CALLBACK] response_handler, option ID : %d\n",
-                    responseInfo->info.options[i].optionID);
+            printf("[CALLBACK] response_handler, option ID : %d\n", responseInfo->info.options[i].optionID);
             printf("[CALLBACK] response_handler, options data length : %d\n",
-                    responseInfo->info.options[i].optionLength);
+                   responseInfo->info.options[i].optionLength);
             printf("[CALLBACK] response_handler, options data : %s\n",
-                    responseInfo->info.options[i].optionData);
+                   responseInfo->info.options[i].optionData );
         }
     }
 
+    //printf("send request with URI\n");
+    //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
 }
 
 void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)