2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <service_app.h>
21 #include <app_common.h>
23 #include <sap_file_transfer.h>
24 #include <sticker_provider.h>
28 #include <linux/limits.h>
29 #include <sys/types.h>
32 #include <json-glib/json-glib.h>
35 #include <thumbnail_util.h>
37 #include <app_preference.h>
41 #include "sticker_info.h"
42 #include "../inc/sticker_data.h"
43 #include "sticker_request.h"
46 #include "receiver_preference.h"
48 #define STICKER_SYNC_FEATURE_REQ "sticker-sync-feature-req"
49 #define STICKER_SYNC_FEATURE_RSP "sticker-sync-feature-rsp"
50 #define STICKER_REQUEST_SYNC_REQ "sticker-request-sync-req"
51 #define STICKER_REQUEST_NOTI_REQ "sticker-request-noti-req"
52 #define STICKER_SYNC_START_REQ "sticker-sync-start-req"
53 #define STICKER_SYNC_START_RSP "sticker-sync-start-rsp"
54 #define STICKER_SEND_START_REQ "sticker-send-start-req"
55 #define STICKER_SEND_START_RSP "sticker-send-start-rsp"
56 #define STICKER_SEND_FACE_DATA "sticker-send-face-data"
57 #define STICKER_SEND_STOP_REQ "sticker-send-stop-req"
58 #define STICKER_SEND_STOP_RSP "sticker-send-stop-rsp"
59 #define STICKER_SEND_DISCONNECT_REQ "sticker-send-disconnect-req"
61 #ifndef VCONFKEY_STICKER_SYNC_COMPLETE_DONE
62 #define VCONFKEY_STICKER_SYNC_COMPLETE_DONE 0x1
68 SYNC_START_RSP_SUCCESS = 1000,
69 SYNC_START_RSP_NO_STICKER = 1001,
70 SYNC_START_RSP_BITMOJI_ALL_DELETE = 1002,
71 SYNC_START_RSP_AREMOJI_ALL_DELETE = 1003
77 sap_peer_agent_h peer_agent;
78 sap_file_transaction_h file_socket;
81 static struct sap_info_s priv_data = { 0 };
82 static struct sticker_info sticker_data;
83 static queue<StickerRequest> ReqQueue;
84 static StickerRequest current_request;
86 static gboolean file_on_progress = FALSE;
87 static string incoming_file_name;
89 static int rec_file_cnt_in_group = 0;
90 static int total_file_count_in_group = 0;
91 static int sync_success_cnt = 0;
92 static gboolean job_progress = FALSE;
93 static int sync_complete_flags = 0;
95 static void save_last_sync_time()
97 // save last sync time in preference
98 double current_time = ecore_time_unix_get();
99 if (preference_set_double(LAST_SYNC_TIME, current_time) == PREFERENCE_ERROR_NONE) {
100 LOGI("Succeed to set last sync time : %f", current_time);
103 LOGW("Failed to set last sync time");
107 static void set_sync_first_complete()
109 int complete_flags = 0;
110 if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &complete_flags) == 0 && complete_flags == 0) {
112 if (vconf_set_int(VCONFKEY_STICKER_SYNC_COMPLETE, VCONFKEY_STICKER_SYNC_COMPLETE_DONE) == 0)
113 LOGD("Succeed to set sync complete");
115 LOGW("Fail to set sync complete");
119 static void set_sync_complete()
121 set_sync_first_complete();
122 save_last_sync_time();
125 static void set_sync_progressing(gboolean flag)
128 #ifdef VCONFKEY_STICKER_SYNC_STATE
129 LOGD("sync progressing : %d", flag);
130 if (vconf_set_int(VCONFKEY_STICKER_SYNC_STATE, flag ? VCONFKEY_STICKER_SYNC_STATE_IN_PROGRESS : VCONFKEY_STICKER_SYNC_STATE_WAITING) == 0)
131 LOGD("Succeed to set sync state");
133 LOGW("Fail to set sync state");
135 LOGW("No vconf sync state definition");
139 static gboolean _send_json_data(JsonObject *obj)
141 JsonGenerator *j_generator = NULL;
142 JsonNode *j_node = NULL;
147 j_generator = json_generator_new();
148 if (j_generator == NULL) {
149 LOGE("Unable to json_generator_new");
153 j_node = json_node_new(JSON_NODE_OBJECT);
154 if (j_node == NULL) {
155 LOGE("Unable to json_node_new");
159 json_node_set_object(j_node, obj);
160 json_generator_set_root(j_generator, j_node);
162 data = json_generator_to_data(j_generator, &size);
164 LOGE("Unable to json_generator_to_data");
168 if (priv_data.socket) {
169 LOGD("Send JSON data : %s", data);
170 result = sap_socket_send_data(priv_data.socket, ACCESSORY_SERVICE_CHANNEL_ID, strlen(data), (void *)data);
178 json_node_free(j_node);
181 g_object_unref(j_generator);
183 return result ? FALSE : TRUE;
186 static void notify_sync_progress(unsigned int file_progress)
188 if (total_file_count_in_group == 0)
191 LOGI("(%2d / %2d), file progress : %3u%%", rec_file_cnt_in_group+1, total_file_count_in_group, file_progress);
192 send_message("sync_progress", NULL);
195 static void _on_transfer_completed(sap_file_transaction_h file_transaction,
196 sap_ft_transfer_e result,
197 const char *file_path,
200 if (priv_data.file_socket) {
201 sap_file_transfer_destroy(file_transaction);
202 priv_data.file_socket = NULL;
205 if (result == SAP_FT_TRANSFER_SUCCESS) {
206 LOGI("Transfer Completed");
208 if (chmod(sticker_data.file_path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
209 LOGE("Failed to change permission : %s. error : %s", sticker_data.file_path.c_str(), strerror(errno));
212 LOGI("Succeed to change permission : %s", sticker_data.file_path.c_str());
213 char thumb_path[PATH_MAX];
214 char *data_path = NULL;
215 data_path = app_get_shared_data_path();
216 snprintf(thumb_path, sizeof(thumb_path), "%s/thumbnail/%s", data_path, incoming_file_name.c_str());
217 sticker_data.thumbnail_path = string(thumb_path);
222 int ret = thumbnail_util_extract_to_file(sticker_data.file_path.c_str(), THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, sticker_data.thumbnail_path.c_str());
223 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
224 LOGE("Failed to create thumbnail. msg : %s", get_error_message(ret));
225 sticker_data.thumbnail_path.clear();
228 insert_sticker_data(sticker_data.file_path.c_str(), sticker_data.keyword.c_str(), sticker_data.group.c_str(), sticker_data.description.c_str(),
229 sticker_data.thumbnail_path.c_str(), sticker_data.disp_type.c_str());
231 if (unlink(sticker_data.file_path.c_str()) == -1)
232 LOGE("Failed to remove sticker file");
234 if (!sticker_data.thumbnail_path.empty()) {
235 if (unlink(sticker_data.thumbnail_path.c_str()) == -1)
236 LOGE("Failed to remove sticker thumbnail");
240 rec_file_cnt_in_group++;
243 case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
244 LOGW("Channel IO Error.");
248 case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
249 LOGW("File IO Error.");
253 case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED):
255 LOGW("Transfer dropped.");
259 case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE):
261 LOGW("Peer Un Responsive.");
265 case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST):
267 LOGW("Connection Lost.");
271 case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED):
273 LOGW("Peer Cancelled.");
277 case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE):
284 LOGW("Unknown Error");
289 file_on_progress = FALSE;
290 sticker_data.reset();
293 static void _on_transfer_file_in_progress(sap_file_transaction_h file_transaction,
294 unsigned short int percentage_progress,
297 notify_sync_progress(percentage_progress);
300 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
302 LOGI("# set callbacks");
303 sap_file_transfer_set_progress_cb(file_socket, _on_transfer_file_in_progress, NULL);
305 sap_file_transfer_set_done_cb(file_socket, _on_transfer_completed, NULL);
308 bool get_job_progress()
316 char file_path[PATH_MAX];
317 char *data_path = NULL;
319 data_path = app_get_shared_data_path();
320 LOGI("Path : %s", data_path);
321 snprintf(file_path, sizeof(file_path), "%s/%s", data_path, incoming_file_name.c_str());
322 LOGI("Receive filepath : %s", file_path);
323 sticker_data.file_path = string(file_path);
328 ret = sap_file_transfer_receive(priv_data.file_socket, file_path);
330 case SAP_RESULT_PERMISSION_DENIED:
331 LOGW("permission denied");
333 case SAP_RESULT_FAILURE:
336 case SAP_RESULT_SUCCESS:
341 file_on_progress = TRUE;
344 static int _create_thumbnail_directory()
346 char thumb_path[PATH_MAX];
347 char *data_path = NULL;
348 data_path = app_get_shared_data_path();
349 snprintf(thumb_path, sizeof(thumb_path), "%s/thumbnail", data_path);
354 if (access(thumb_path, F_OK) == 0)
357 if (mkdir(thumb_path, 0755) == -1) {
358 LOGE("directory create error");
365 bool request_sticker_data(const char *mode, const char *category, const char *type)
368 JsonObject *j_object = NULL;
370 if (!priv_data.socket) {
372 StickerRequest pending_request;
373 pending_request.req_type = REQUEST_TYPE_SYNC;
374 pending_request.mode = string(mode ? mode : "manual");
375 pending_request.category = string(category ? category : "arsticker");
376 pending_request.type = string(type ? type : "input");
378 ReqQueue.push(pending_request);
379 LOGI("Push sync request");
384 j_object = json_object_new();
385 if (j_object == NULL) {
386 LOGE("json object create error");
390 if (_create_thumbnail_directory() != 0)
391 LOGE("Failed to create thumbnail directory");
393 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_START_REQ);
394 json_object_set_int_member(j_object, "tID", ++t_id);
395 json_object_set_string_member(j_object, "mode", mode);
396 json_object_set_string_member(j_object, "category", category);
397 json_object_set_string_member(j_object, "type", type);
399 if (_send_json_data(j_object) == FALSE) {
400 LOGE("Failed to send STICKER_SYNC_START_REQ");
404 current_request.req_type = REQUEST_TYPE_SYNC;
405 current_request.mode = string(mode ? mode : "manual");
406 current_request.category = string(category ? category : "arsticker");
407 current_request.type = string(type ? type : "input");
408 set_sync_progressing(TRUE);
412 json_object_unref(j_object);
417 static bool process_request_queue()
419 if (ReqQueue.empty())
422 StickerRequest request = ReqQueue.front();
424 if (request.req_type == REQUEST_TYPE_FEATURE_REQ) {
425 LOGD("[Request feature exchange]");
426 request_sticker_feature();
429 else if (request.req_type == REQUEST_TYPE_SYNC) {
430 LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", request.mode.c_str(),
431 request.category.c_str(),
432 request.type.c_str());
434 if (request_sticker_data(request.mode.c_str(), request.category.c_str(), request.type.c_str()))
437 else if (request.req_type == REQUEST_TYPE_SHOW_NOTIFICATION) {
438 LOGD("[Request to show notification]");
439 request_show_sync_notification();
446 void request_all_sticker_data(const char *mode, const char *type)
448 StickerRequest pending_request;
449 pending_request.req_type = REQUEST_TYPE_SYNC;
450 pending_request.mode = string(mode);
451 pending_request.type = string(type);
453 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
454 int feature_flag = 0;
455 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
457 if (feature_flag & VCONFKEY_STICKER_FEATURE_AREMOJI) {
458 pending_request.category = string("arsticker");
459 ReqQueue.push(pending_request);
462 if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI) {
463 pending_request.category = string("bitmoji");
464 ReqQueue.push(pending_request);
468 LOGW("Failed to get value of VCONFKEY_STICKER_SUPPORTED_FEATURE");
470 pending_request.category = string("arsticker");
471 ReqQueue.push(pending_request);
472 pending_request.category = string("bitmoji");
473 ReqQueue.push(pending_request);
476 if (priv_data.socket)
477 process_request_queue();
480 void request_sticker_feature()
482 JsonObject *j_object = NULL;
484 if (!priv_data.socket) {
486 StickerRequest pending_request;
487 pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
488 ReqQueue.push(pending_request);
489 LOGI("Push sync feature request");
493 j_object = json_object_new();
494 if (j_object == NULL) {
495 LOGE("json object create error");
499 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_FEATURE_REQ);
500 json_object_set_int_member(j_object, "tID", ++t_id);
502 if (_send_json_data(j_object) == FALSE) {
503 LOGE("Failed to send STICKER_SYNC_FEATURE_REQ");
509 json_object_unref(j_object);
511 if (_create_thumbnail_directory() != 0)
512 LOGE("Failed to create thumbnail directory");
515 void send_disconnect_message()
517 JsonObject *j_object = NULL;
518 j_object = json_object_new();
519 if (j_object == NULL) {
520 LOGE("json object create error");
524 json_object_set_string_member(j_object, "msgId", STICKER_SEND_DISCONNECT_REQ);
525 json_object_set_int_member(j_object, "tID", t_id);
527 if (_send_json_data(j_object) == FALSE) {
528 LOGE("Failed to send STICKER_SEND_DISCONNECT_REQ");
531 json_object_unref(j_object);
534 void request_show_sync_notification()
536 JsonObject *j_object = NULL;
538 if (!priv_data.socket) {
540 StickerRequest pending_request;
541 pending_request.req_type = REQUEST_TYPE_SHOW_NOTIFICATION;
542 ReqQueue.push(pending_request);
543 LOGI("Push show notification request");
547 j_object = json_object_new();
548 if (j_object == NULL) {
549 LOGE("json object create error");
553 json_object_set_string_member(j_object, "msgId", STICKER_REQUEST_NOTI_REQ);
554 json_object_set_int_member(j_object, "tID", ++t_id);
556 if (_send_json_data(j_object) == FALSE) {
557 LOGE("Failed to send STICKER_REQUEST_NOTI_REQ");
562 json_object_unref(j_object);
567 int ret = sap_file_transfer_reject(priv_data.file_socket);
568 LOGI("ret : %d", ret);
570 file_on_progress = FALSE;
573 static void _on_transfer_file_cb(sap_peer_agent_h peer_agent_h,
574 sap_file_transaction_h socket,
575 const char *file_path,
578 file_on_progress = TRUE;
579 priv_data.file_socket = socket;
580 LOGI("# incoming file request.");
581 __set_file_transfer_cb(priv_data.file_socket);
583 incoming_file_name = file_path;
584 std::size_t found = incoming_file_name.find_last_of("/");
585 incoming_file_name = incoming_file_name.substr(found+1);
587 LOGI("# file path : %s, incoming file name : %s", file_path, incoming_file_name.c_str());
592 void conn_terminated(sap_peer_agent_h peer_agent,
594 sap_service_connection_terminated_reason_e result,
597 sap_info_s *priv = NULL;
598 priv = (sap_info_s *)user_data;
602 case SAP_CONNECTION_TERMINATED_REASON_PEER_DISCONNECTED:
603 LOGW("Peer Disconnected");
605 case SAP_CONNECTION_TERMINATED_REASON_DEVICE_DETACHED:
606 LOGW("Disconnected Device Detached");
608 case SAP_CONNECTION_TERMINATED_REASON_UNKNOWN:
609 LOGW("Disconnected Unknown Reason");
612 LOGW("connection terminated. reason : %d", result);
616 sap_socket_destroy(priv->socket);
619 set_sync_progressing(FALSE);
620 send_message("sync_stop_result", "cancel");
626 job_progress = FALSE;
627 send_disconnect_message();
631 static void send_sync_start_response(int result_code)
633 int feature_flag = 0;
634 string response_to_app;
635 switch (result_code) {
636 case SYNC_START_RSP_SUCCESS:
638 response_to_app = "success";
639 if (current_request.category == string("arsticker"))
640 sync_complete_flags |= VCONFKEY_STICKER_FEATURE_AREMOJI;
641 else if (current_request.category == string("bitmoji"))
642 sync_complete_flags |= VCONFKEY_STICKER_FEATURE_BITMOJI;
645 case SYNC_START_RSP_NO_STICKER:
646 response_to_app = "no_sticker";
648 case SYNC_START_RSP_BITMOJI_ALL_DELETE:
651 if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &is_synced) != 0)
652 LOGW("Failed to read sync completion");
654 if (is_synced == VCONFKEY_STICKER_SYNC_COMPLETE_NONE)
655 response_to_app = "no_sticker";
657 response_to_app = "success";
660 case SYNC_START_RSP_AREMOJI_ALL_DELETE:
661 response_to_app = "success";
664 response_to_app = "unknown_error";
668 LOGD("result code : %d, sync complete flag : %d", result_code, sync_complete_flags);
670 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
671 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) != 0)
673 LOGW("Failed to read support feature");
677 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
678 if (feature_flag == VCONFKEY_STICKER_FEATURE_AREMOJI ||
679 feature_flag == VCONFKEY_STICKER_FEATURE_BITMOJI)
681 LOGD("only standalone sync mode");
682 send_message("sync_start_response", response_to_app.c_str());
685 if (current_request.category == string("arsticker"))
687 if (result_code == SYNC_START_RSP_SUCCESS)
688 send_message("sync_start_response", response_to_app.c_str());
690 else if (current_request.category == string("bitmoji"))
692 if (sync_complete_flags == 0 ||
693 sync_complete_flags == VCONFKEY_STICKER_FEATURE_BITMOJI)
694 send_message("sync_start_response", response_to_app.c_str());
701 on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
702 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
704 unsigned int buf_len = strlen((char *)buffer);
705 LOGI("received data: %s, len: %d, buffer len : %d", (char *)buffer, payload_length, buf_len);
707 JsonParser *parser = json_parser_new();
708 GError *err_msg = NULL;
709 JsonNode *root = NULL;
710 JsonObject *root_obj = NULL;
712 const char *json_msgid = NULL;
714 json_parser_load_from_data(parser, (char *)buffer, payload_length, &err_msg);
716 LOGE("failed to load json file. error message: %s", err_msg->message);
720 root = json_parser_get_root(parser);
722 LOGE("failed to get root");
726 root_obj = json_node_get_object(root);
727 if (root_obj == NULL) {
728 LOGE("failed to get object");
732 json_msgid = json_object_get_string_member(root_obj, "msgId");
733 msg_id = string(json_msgid ? json_msgid : "");
734 if (msg_id == STICKER_REQUEST_SYNC_REQ) {
735 request_all_sticker_data("manual", "input");
736 } else if (msg_id == STICKER_SYNC_FEATURE_RSP) {
737 LOGD("msg : %s", msg_id.c_str());
738 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
739 const char *json_aremoji = json_object_get_string_member(root_obj, "arEmoji");
740 const char *json_bitmoji = json_object_get_string_member(root_obj, "bitmoji");
741 string support_aremoji = string(json_aremoji ? json_aremoji : "");
742 string support_bitmoji = string(json_bitmoji ? json_bitmoji : "");
743 int supported_feature = VCONFKEY_STICKER_FEATURE_NONE;
745 if (support_aremoji == "support")
746 supported_feature |= VCONFKEY_STICKER_FEATURE_AREMOJI;
748 if (support_bitmoji == "support")
749 supported_feature |= VCONFKEY_STICKER_FEATURE_BITMOJI;
751 if (vconf_set_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, supported_feature) == 0)
752 LOGD("Succeed to set supported feature");
754 LOGW("Fail to set supported feature");
756 LOGW("No vconf supported feature");
759 if (!process_request_queue()) {
762 } else if (msg_id == STICKER_SYNC_START_RSP) {
763 LOGD("msg : %s", msg_id.c_str());
764 const char *json_result = json_object_get_string_member(root_obj, "result");
765 string result = string(json_result ? json_result : "");
766 int result_code = json_object_get_int_member(root_obj, "resultCode");
767 LOGD("result : %s, resultCode : %d", result.c_str(), result_code);
769 if(result_code == SYNC_START_RSP_SUCCESS) {
770 send_sync_start_response(result_code);
771 if (current_request.category == string("bitmoji")) {
772 LOGD("Delete all bitmoji stickers");
773 delete_all_stickers("bitmoji");
776 if (result_code == SYNC_START_RSP_BITMOJI_ALL_DELETE) {
777 LOGD("Delete all bitmoji stickers");
778 delete_all_stickers("bitmoji");
779 } else if (result_code == SYNC_START_RSP_AREMOJI_ALL_DELETE) {
780 LOGD("Delete all AR Emoji stickers");
781 delete_all_stickers("arsticker");
784 if (!process_request_queue()) {
785 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
786 int feature_flag = 0;
788 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
790 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
791 if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
793 if (current_request.category == string("bitmoji"))
795 /* other sticker group(s) has been synchronized completely */
796 if (sync_success_cnt > 0) {
803 LOGW("Failed to read support feature");
806 LOGW("No vconf supported feature");
807 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
809 send_sync_start_response(result_code);
810 set_sync_progressing(FALSE);
811 save_last_sync_time();
816 } else if (msg_id == STICKER_SEND_START_REQ) {
817 LOGD("msg : %s", msg_id.c_str());
818 total_file_count_in_group = 0;
819 rec_file_cnt_in_group = 0;
820 t_id = json_object_get_int_member(root_obj, "tID");
821 JsonArray *file_list = json_object_get_array_member(root_obj, "list");
823 int arr_len = json_array_get_length(file_list);
824 for (int i = 0; i < arr_len; i++) {
825 JsonObject *file_obj = json_array_get_object_element(file_list, i);
826 int file_len = json_object_get_int_member(file_obj, "size");
827 const char *json_filename = json_object_get_string_member(file_obj, "fileName");
828 string file_name = string(json_filename ? json_filename : "");
831 LOGD("Add file : %s, len : %d", file_name.c_str(), file_len);
832 total_file_count_in_group++;
835 char file_path[PATH_MAX];
836 char del_path[PATH_MAX];
837 std::size_t found = file_name.find_last_of("/");
838 string del_file_name = file_name.substr(found+1);
839 char *data_path = app_get_shared_data_path();
841 snprintf(file_path, sizeof(file_path), "%s/%s", data_path, del_file_name.c_str());
842 snprintf(del_path, sizeof(del_path), "%s/%s%s",STICKER_DIRECTORY, app_id, file_path);
844 LOGD("Delete file : %s, len : %d", del_path, file_len);
845 delete_sticker_data(del_path);
853 JsonObject *j_object = json_object_new();
854 if (j_object == NULL) {
855 LOGE("Unable to json_object_new");
859 json_object_set_string_member(j_object, "msgId", STICKER_SEND_START_RSP);
860 json_object_set_int_member(j_object, "tID", t_id);
861 json_object_set_string_member(j_object, "result", "success");
863 if (_send_json_data(j_object) == FALSE)
864 LOGE("Failed to send message");
866 json_object_unref(j_object);
867 } else if (msg_id == STICKER_SEND_FACE_DATA) {
868 LOGD("msg : %s", msg_id.c_str());
869 const char *type_data = json_object_get_string_member(root_obj, "type");
871 sticker_data.disp_type = string(type_data);
873 const char *category = json_object_get_string_member(root_obj, "category");
875 sticker_data.group = string(category);
876 sticker_data.keyword = string(category);
878 } else if (msg_id == STICKER_SEND_STOP_REQ) {
879 LOGD("msg : %s", msg_id.c_str());
880 const char *json_reason = json_object_get_string_member(root_obj, "reason");
881 string reason = string(json_reason ? json_reason : "");
882 int file_len = json_object_get_int_member(root_obj, "count");
884 JsonObject *j_object = json_object_new();
885 if (j_object == NULL) {
886 LOGE("Unable to json_object_new");
890 json_object_set_string_member(j_object, "msgId", STICKER_SEND_STOP_RSP);
891 json_object_set_int_member(j_object, "tID", t_id);
893 if (reason == "complete") {
894 if (rec_file_cnt_in_group == file_len) {
895 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
896 int feature_flag = 0;
898 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
900 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
901 if (feature_flag == VCONFKEY_STICKER_FEATURE_AREMOJI)
903 if (current_request.category == string("arsticker"))
906 set_sync_progressing(FALSE);
909 else if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
911 if (current_request.category == string("bitmoji"))
914 set_sync_progressing(FALSE);
919 LOGW("Failed to read support feature");
922 LOGW("No vconf supported feature");
923 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
925 json_object_set_string_member(j_object, "result", "success");
929 json_object_set_string_member(j_object, "result", "failure");
933 json_object_set_string_member(j_object, "result", "failure");
936 if (_send_json_data(j_object) == FALSE)
937 LOGE("Failed to send message");
939 json_object_unref(j_object);
941 send_message("sync_stop_result", reason.c_str());
943 current_request.mode.clear();
944 current_request.category.clear();
945 current_request.type.clear();
947 if (!process_request_queue()) {
948 sync_success_cnt = 0;
953 LOGW("unknown msg id : %s", msg_id.c_str());
957 g_error_free(err_msg);
960 g_object_unref(parser);
963 static void on_conn_req(sap_peer_agent_h peer_agent,
965 sap_service_connection_result_e result,
970 case SAP_CONNECTION_SUCCESS:
971 LOGI("Connection success");
972 priv_data.socket = socket;
973 sap_peer_agent_accept_service_connection(peer_agent);
974 sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, &priv_data);
975 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
977 case SAP_CONNECTION_ALREADY_EXIST:
978 priv_data.socket = socket;
979 LOGI("Connection Already Exist");
981 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
982 LOGW("Connection failure device unreachable");
984 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
985 LOGW("Connection failure invalid peer agent");
987 case SAP_CONNECTION_FAILURE_NETWORK:
988 LOGW("Connection failure network");
990 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
991 LOGW("Connection failure peer agent no response");
993 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
994 LOGW("Connection failure peer agent rejected");
996 case SAP_CONNECTION_FAILURE_UNKNOWN:
997 LOGW("Connection failure unknown");
1000 LOGW("Connection failure. error code : %d", result);
1006 _on_service_connection_created(sap_peer_agent_h peer_agent,
1007 sap_socket_h socket,
1008 sap_service_connection_result_e result,
1011 sap_info_s *priv = (sap_info_s *)user_data;
1015 case SAP_CONNECTION_SUCCESS:
1016 sap_peer_agent_set_service_connection_terminated_cb(priv->peer_agent,
1020 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
1021 priv->socket = socket;
1022 LOGI("Connection Established");
1024 process_request_queue();
1028 case SAP_CONNECTION_ALREADY_EXIST:
1029 priv->socket = socket;
1030 LOGI("Connection Already Exist");
1033 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
1034 LOGW("Connection Failure device unreachable");
1037 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
1038 LOGW("Connection Failure invalid peer agent");
1041 case SAP_CONNECTION_FAILURE_NETWORK:
1042 LOGW("Connection Failure network");
1045 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
1046 LOGW("Connection Failure peer agent no response");
1049 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
1050 LOGW("Connection Failure peer agent rejected");
1053 case SAP_CONNECTION_FAILURE_UNKNOWN:
1054 LOGW("Connection Failure peer agent unknown");
1057 case SAP_CONNECTION_IN_PROGRESS:
1058 LOGW("Connection in progress");
1061 case SAP_CONNECTION_PEER_AGENT_NOT_SUPPORTED:
1062 LOGW("Connection peer agent not supported");
1068 _create_service_connection(gpointer user_data)
1070 sap_info_s *priv = (sap_info_s *)user_data;
1071 int result = sap_agent_request_service_connection(priv->agent,
1073 _on_service_connection_created,
1076 LOGD("request connection result : %d", result);
1082 _on_peer_agent_updated(sap_peer_agent_h peer_agent,
1083 sap_peer_agent_status_e peer_status,
1084 sap_peer_agent_found_result_e result,
1087 sap_info_s *priv = (sap_info_s *)user_data;
1091 case SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED:
1092 LOGW("SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED");
1095 case SAP_PEER_AGENT_FOUND_RESULT_FOUND:
1096 if (peer_status == SAP_PEER_AGENT_STATUS_AVAILABLE)
1098 LOGD("SAP_PEER_AGENT_FOUND_RESULT_FOUND");
1099 priv->peer_agent = peer_agent;
1100 g_idle_add(_create_service_connection, priv);
1104 sap_peer_agent_destroy(peer_agent);
1108 case SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND:
1109 LOGW("SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND");
1112 case SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT:
1113 LOGW("SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT");
1116 case SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR:
1117 LOGW("SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR");
1126 _find_peer_agent(gpointer user_data)
1128 sap_info_s *priv = (sap_info_s *)user_data;
1129 sap_agent_find_peer_agent(priv->agent, _on_peer_agent_updated, priv);
1134 static void on_agent_initialized(sap_agent_h agent,
1135 sap_agent_initialized_result_e result,
1139 case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
1140 LOGD("agent is initialized");
1142 priv_data.agent = agent;
1144 sap_file_transfer_set_incoming_file_cb(agent, _on_transfer_file_cb, NULL);
1145 sap_agent_set_service_connection_requested_cb(agent, on_conn_req, NULL);
1148 case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
1149 LOGE("duplicate registration");
1151 case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
1152 LOGE("invalid arguments");
1154 case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
1155 LOGE("internal sap error");
1158 LOGE("unknown status (%d)", result);
1163 static void on_agent_deinitialized(sap_agent_h agent,
1164 sap_agent_deinitialized_result_e result,
1167 LOGD("result of deinitialize : %d", result);
1170 static void _on_device_status_changed(sap_device_status_e status,
1171 sap_transport_type_e transport_type,
1174 LOGD("%s, status :%d", __func__, status);
1176 switch (transport_type) {
1177 case SAP_TRANSPORT_TYPE_BT:
1178 LOGD("transport_type (%d): bt", transport_type);
1180 case SAP_TRANSPORT_TYPE_BLE:
1181 LOGD("transport_type (%d): ble", transport_type);
1183 case SAP_TRANSPORT_TYPE_TCP:
1184 LOGD("transport_type (%d): tcp/ip", transport_type);
1186 case SAP_TRANSPORT_TYPE_USB:
1187 LOGD("transport_type (%d): usb", transport_type);
1189 case SAP_TRANSPORT_TYPE_MOBILE:
1190 LOGD("transport_type (%d): mobile", transport_type);
1193 LOGE("unknown transport_type (%d)", transport_type);
1198 case SAP_DEVICE_STATUS_DETACHED:
1199 LOGD("device is not connected.");
1200 send_message("sync_stop_result", "cancel");
1202 case SAP_DEVICE_STATUS_ATTACHED:
1203 LOGD("Attached calling find peer now");
1204 g_idle_add(_find_peer_agent, &priv_data);
1207 LOGE("unknown status (%d)", status);
1212 gboolean agent_initialize()
1217 result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
1218 on_agent_initialized, NULL);
1220 LOGD("SAP >>> getRegisteredServiceAgent() >>> %d", result);
1221 } while (result != SAP_RESULT_SUCCESS);
1226 gboolean initialize_sap(void)
1228 sap_agent_h agent = NULL;
1230 if (priv_data.agent) {
1231 LOGW("duplicate initialize");
1235 sap_agent_create(&agent);
1237 priv_data.agent = agent;
1241 sap_set_device_status_changed_cb(_on_device_status_changed, NULL);
1246 void deinitialize_sap(void)
1248 LOGD("deinitialize");
1249 if (priv_data.agent) {
1250 int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
1252 case SAP_RESULT_FAILURE:
1253 LOGW("Failed to deinitialize");
1255 case SAP_RESULT_SUCCESS:
1256 LOGD("Succeed to deinitialize");
1258 case SAP_RESULT_PERMISSION_DENIED:
1259 LOGW("permission denied: deinitialize ");
1262 LOGD("deinitialize : %d", ret);
1266 sap_agent_destroy(priv_data.agent);
1267 priv_data.agent = NULL;
1271 gboolean is_init_sap()
1273 return priv_data.agent ? TRUE : FALSE;