Change way to connect in sticker-receiver 08/226808/2
authorJihoon Kim <jihoon48.kim@samsung.com>
Thu, 5 Mar 2020 11:44:50 +0000 (20:44 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Thu, 5 Mar 2020 11:46:16 +0000 (20:46 +0900)
Previously, mobile device connects to wearable.
From now on, sticker-reciever connect to mobile device to sync sticker data.

Change-Id: I7ce6b8664a88a5b0a900369fed52078f3adb3731
Signed-off-by: Jihoon Kim <jihoon48.kim@samsung.com>
receiver/src/ft.cpp
receiver/src/main.cpp

index e388ef0..b926aca 100644 (file)
 
 using namespace std;
 
-struct priv {
+struct sap_info_s {
     sap_agent_h agent;
     sap_socket_h socket;
+    sap_peer_agent_h peer_agent;
     sap_file_transaction_h file_socket;
 };
 
+struct sync_request {
+    string mode;
+    string category;
+    string type;
+};
+
 static void _reset_sticker_data();
 
 struct sticker_info {
@@ -69,8 +76,9 @@ struct sticker_info {
     }
 };
 
-static struct priv priv_data = { 0 };
+static struct sap_info_s priv_data = { 0 };
 static struct sticker_info sticker_data;
+static struct sync_request pending_sync_request;
 
 gboolean file_on_progress = 0;
 static string incoming_file_name;
@@ -266,8 +274,13 @@ void request_sticker_data(const char *mode, const char *category, const char *ty
     json_object_set_string_member(j_object, "category", category);
     json_object_set_string_member(j_object, "type", type);
 
-    if (_send_json_data(j_object) == FALSE)
-        LOGE("Failed to request sticker data");
+    if (_send_json_data(j_object) == FALSE) {
+        pending_sync_request.mode = string(mode ? mode : "manual");
+        pending_sync_request.category = string(category? category : "arsticker");
+        pending_sync_request.type = string(type ? type : "input");
+
+        LOGI("Push sync request");
+    }
 
     json_object_unref(j_object);
 }
@@ -304,6 +317,9 @@ void conn_terminated(sap_peer_agent_h peer_agent,
                      sap_service_connection_terminated_reason_e result,
                      void *user_data)
 {
+    sap_info_s *priv = NULL;
+    priv = (sap_info_s *)user_data;
+
     switch (result)
     {
     case SAP_CONNECTION_TERMINATED_REASON_PEER_DISCONNECTED:
@@ -320,6 +336,9 @@ void conn_terminated(sap_peer_agent_h peer_agent,
         break;
     }
 
+    sap_socket_destroy(priv->socket);
+    priv->socket = NULL;
+
     service_app_exit();
 }
 
@@ -382,6 +401,9 @@ on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned in
                 JsonObject *file_obj = json_array_get_object_element(file_list, i);
                 int file_len = json_object_get_int_member(file_obj, "size");
                 const char *json_filename = json_object_get_string_member(file_obj, "fileName");
+                if (!json_filename) {
+                    json_filename = json_object_get_string_member(file_obj, "filename");
+                }
                 string file_name = string(json_filename ? json_filename : "");
 
                 if (file_len > 0) {
@@ -466,7 +488,7 @@ static void on_conn_req(sap_peer_agent_h peer_agent,
         LOGI("Connection success");
         priv_data.socket = socket;
         sap_peer_agent_accept_service_connection(peer_agent);
-        sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, NULL);
+        sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, &priv_data);
         sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
         break;
     case SAP_CONNECTION_ALREADY_EXIST:
@@ -497,6 +519,144 @@ static void on_conn_req(sap_peer_agent_h peer_agent,
     }
 }
 
+static void
+_on_service_connection_created(sap_peer_agent_h peer_agent,
+                               sap_socket_h socket,
+                               sap_service_connection_result_e result,
+                               void *user_data)
+{
+    sap_info_s *priv = (sap_info_s *)user_data;
+
+    switch (result)
+    {
+    case SAP_CONNECTION_SUCCESS:
+        sap_peer_agent_set_service_connection_terminated_cb(priv->peer_agent,
+                                                            conn_terminated,
+                                                            priv);
+
+        sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
+        priv->socket = socket;
+        LOGI("Connection Established");
+
+        if (!pending_sync_request.mode.empty()) {
+            LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", pending_sync_request.mode.c_str(),
+                                                                                pending_sync_request.category.c_str(),
+                                                                                pending_sync_request.type.c_str());
+
+            request_sticker_data(pending_sync_request.mode.c_str(), pending_sync_request.category.c_str(), pending_sync_request.type.c_str());
+            pending_sync_request.mode.clear();
+            pending_sync_request.category.clear();
+            pending_sync_request.type.clear();
+        }
+
+        break;
+
+    case SAP_CONNECTION_ALREADY_EXIST:
+        priv->socket = socket;
+        LOGI("Connection Already Exist");
+        break;
+
+    case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
+        LOGW("Connection Failure device unreachable");
+        break;
+
+    case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
+        LOGW("Connection Failure invalid peer agent");
+        break;
+
+    case SAP_CONNECTION_FAILURE_NETWORK:
+        LOGW("Connection Failure network");
+        break;
+
+    case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
+        LOGW("Connection Failure peer agent no response");
+        break;
+
+    case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
+        LOGW("Connection Failure peer agent rejected");
+        break;
+
+    case SAP_CONNECTION_FAILURE_UNKNOWN:
+        LOGW("Connection Failure peer agent unknown");
+        break;
+
+    case SAP_CONNECTION_IN_PROGRESS:
+        LOGW("Connection in progress");
+        break;
+
+    case SAP_CONNECTION_PEER_AGENT_NOT_SUPPORTED:
+        LOGW("Connection peer agent not supported");
+        break;
+    }
+}
+
+static gboolean
+_create_service_connection(gpointer user_data)
+{
+    sap_info_s *priv = (sap_info_s *)user_data;
+    int result = sap_agent_request_service_connection(priv->agent,
+                                                      priv->peer_agent,
+                                                      _on_service_connection_created,
+                                                      priv);
+
+    LOGD("request connection result : %d", result);
+
+    return FALSE;
+}
+
+static void
+_on_peer_agent_updated(sap_peer_agent_h peer_agent,
+                       sap_peer_agent_status_e peer_status,
+                       sap_peer_agent_found_result_e result,
+                       void *user_data)
+{
+    sap_info_s *priv = (sap_info_s *)user_data;
+
+    switch (result)
+    {
+    case SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED:
+        LOGW("SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED");
+        break;
+
+    case SAP_PEER_AGENT_FOUND_RESULT_FOUND:
+        if (peer_status == SAP_PEER_AGENT_STATUS_AVAILABLE)
+        {
+            LOGD("SAP_PEER_AGENT_FOUND_RESULT_FOUND");
+            priv->peer_agent = peer_agent;
+            g_idle_add(_create_service_connection, priv);
+        }
+        else
+        {
+            sap_peer_agent_destroy(peer_agent);
+        }
+        break;
+
+    case SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND:
+        LOGW("SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND");
+        break;
+
+    case SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT:
+        LOGW("SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT");
+        break;
+
+    case SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR:
+        LOGW("SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static gboolean
+_find_peer_agent(gpointer user_data)
+{
+    sap_info_s *priv = (sap_info_s *)user_data;
+    sap_agent_find_peer_agent(priv->agent, _on_peer_agent_updated, priv);
+
+    return FALSE;
+}
+
 static void on_agent_initialized(sap_agent_h agent,
                                  sap_agent_initialized_result_e result,
                                  void *user_data)
@@ -566,6 +726,7 @@ static void _on_device_status_changed(sap_device_status_e status,
             break;
         case SAP_DEVICE_STATUS_ATTACHED:
             LOGD("Attached calling find peer now");
+            g_idle_add(_find_peer_agent, &priv_data);
             break;
         default:
             LOGE("unknown status (%d)", status);
index 3bf6300..6191a28 100644 (file)
@@ -1,8 +1,10 @@
+#include <app_common.h>
+#include <service_app.h>
+#include <stdlib.h>
+
 #include "main.h"
 #include "ft.h"
 #include "log.h"
-#include <app_common.h>
-#include <service_app.h>
 
 static bool app_create(void *data)
 {
@@ -19,6 +21,55 @@ static bool app_create(void *data)
 static void app_control(app_control_h app_control, void *data)
 {
     /* Handle the launch request. */
+    char* request = NULL;
+    char* mode = NULL;
+    char* category = NULL;
+    char* type = NULL;
+
+    int res;
+
+    // sync request
+    res = app_control_get_extra_data(app_control, "request", &request);
+    if (APP_CONTROL_ERROR_NONE == res && NULL != request)
+    {
+        if (strcmp(request, "sync") == 0)
+        {
+            if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE)
+            {
+                LOGE("No given mode");
+                goto cleanup;
+            }
+
+            if (app_control_get_extra_data(app_control, "category", &category) != APP_CONTROL_ERROR_NONE)
+            {
+                LOGE("No given category");
+                goto cleanup;
+            }
+
+            if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE)
+            {
+                LOGE("No given type");
+                goto cleanup;
+            }
+
+            LOGI("[sync request] mode : %s, category : %s, type : %s", mode, category, type);
+
+            if (mode && category && type)
+                request_sticker_data(mode, category, type);
+        }
+    }
+cleanup:
+    if (NULL != request)
+        free(request);
+
+    if (NULL != mode)
+        free(mode);
+
+    if (NULL != category)
+        free(category);
+
+    if (NULL != type)
+        free(type);
 }
 
 static void app_terminate(void *data)