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 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
214 char thumb_path[PATH_MAX];
215 char *data_path = NULL;
216 data_path = app_get_shared_data_path();
217 snprintf(thumb_path, sizeof(thumb_path), "%s/thumbnail/%s", data_path, incoming_file_name.c_str());
218 sticker_data.thumbnail_path = string(thumb_path);
223 int ret = thumbnail_util_extract_to_file(sticker_data.file_path.c_str(), THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, sticker_data.thumbnail_path.c_str());
224 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
225 LOGE("Failed to create thumbnail. msg : %s", get_error_message(ret));
226 sticker_data.thumbnail_path.clear();
229 insert_sticker_data(sticker_data.file_path.c_str(), sticker_data.keyword.c_str(), sticker_data.group.c_str(), sticker_data.description.c_str(),
230 sticker_data.thumbnail_path.c_str(), sticker_data.disp_type.c_str());
231 destroy_sticker_provider_handle();
233 if (unlink(sticker_data.file_path.c_str()) == -1)
234 LOGE("Failed to remove sticker file");
236 if (!sticker_data.thumbnail_path.empty()) {
237 if (unlink(sticker_data.thumbnail_path.c_str()) == -1)
238 LOGE("Failed to remove sticker thumbnail");
243 rec_file_cnt_in_group++;
246 case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
247 LOGW("Channel IO Error.");
251 case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
252 LOGW("File IO Error.");
256 case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED):
258 LOGW("Transfer dropped.");
262 case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE):
264 LOGW("Peer Un Responsive.");
268 case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST):
270 LOGW("Connection Lost.");
274 case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED):
276 LOGW("Peer Cancelled.");
280 case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE):
287 LOGW("Unknown Error");
292 file_on_progress = FALSE;
293 sticker_data.reset();
296 static void _on_transfer_file_in_progress(sap_file_transaction_h file_transaction,
297 unsigned short int percentage_progress,
300 notify_sync_progress(percentage_progress);
303 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
305 LOGI("# set callbacks");
306 sap_file_transfer_set_progress_cb(file_socket, _on_transfer_file_in_progress, NULL);
308 sap_file_transfer_set_done_cb(file_socket, _on_transfer_completed, NULL);
311 bool get_job_progress()
319 char file_path[PATH_MAX];
320 char *data_path = NULL;
322 data_path = app_get_shared_data_path();
323 LOGI("Path : %s", data_path);
324 snprintf(file_path, sizeof(file_path), "%s/%s", data_path, incoming_file_name.c_str());
325 LOGI("Receive filepath : %s", file_path);
326 sticker_data.file_path = string(file_path);
331 ret = sap_file_transfer_receive(priv_data.file_socket, file_path);
333 case SAP_RESULT_PERMISSION_DENIED:
334 LOGW("permission denied");
336 case SAP_RESULT_FAILURE:
339 case SAP_RESULT_SUCCESS:
344 file_on_progress = TRUE;
347 static int _create_thumbnail_directory()
349 char thumb_path[PATH_MAX];
350 char *data_path = NULL;
351 data_path = app_get_shared_data_path();
352 snprintf(thumb_path, sizeof(thumb_path), "%s/thumbnail", data_path);
357 if (access(thumb_path, F_OK) == 0)
360 if (mkdir(thumb_path, 0755) == -1) {
361 LOGE("directory create error");
368 bool request_sticker_data(const char *mode, const char *category, const char *type)
371 JsonObject *j_object = NULL;
373 if (!priv_data.socket) {
375 StickerRequest pending_request;
376 pending_request.req_type = REQUEST_TYPE_SYNC;
377 pending_request.mode = string(mode ? mode : "manual");
378 pending_request.category = string(category ? category : "arsticker");
379 pending_request.type = string(type ? type : "input");
381 ReqQueue.push(pending_request);
382 LOGI("Push sync request");
387 j_object = json_object_new();
388 if (j_object == NULL) {
389 LOGE("json object create error");
393 if (_create_thumbnail_directory() != 0)
394 LOGE("Failed to create thumbnail directory");
396 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_START_REQ);
397 json_object_set_int_member(j_object, "tID", ++t_id);
398 json_object_set_string_member(j_object, "mode", mode);
399 json_object_set_string_member(j_object, "category", category);
400 json_object_set_string_member(j_object, "type", type);
402 if (_send_json_data(j_object) == FALSE) {
403 LOGE("Failed to send STICKER_SYNC_START_REQ");
407 current_request.req_type = REQUEST_TYPE_SYNC;
408 current_request.mode = string(mode ? mode : "manual");
409 current_request.category = string(category ? category : "arsticker");
410 current_request.type = string(type ? type : "input");
411 set_sync_progressing(TRUE);
415 json_object_unref(j_object);
420 static bool process_request_queue()
422 if (ReqQueue.empty())
425 StickerRequest request = ReqQueue.front();
427 if (request.req_type == REQUEST_TYPE_FEATURE_REQ) {
428 LOGD("[Request feature exchange]");
429 request_sticker_feature();
432 else if (request.req_type == REQUEST_TYPE_SYNC) {
433 LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", request.mode.c_str(),
434 request.category.c_str(),
435 request.type.c_str());
437 if (request_sticker_data(request.mode.c_str(), request.category.c_str(), request.type.c_str()))
440 else if (request.req_type == REQUEST_TYPE_SHOW_NOTIFICATION) {
441 LOGD("[Request to show notification]");
442 request_show_sync_notification();
449 void request_all_sticker_data(const char *mode, const char *type)
451 StickerRequest pending_request;
452 pending_request.req_type = REQUEST_TYPE_SYNC;
453 pending_request.mode = string(mode);
454 pending_request.type = string(type);
456 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
457 int feature_flag = 0;
458 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
460 if (feature_flag & VCONFKEY_STICKER_FEATURE_AREMOJI) {
461 pending_request.category = string("arsticker");
462 ReqQueue.push(pending_request);
465 if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI) {
466 pending_request.category = string("bitmoji");
467 ReqQueue.push(pending_request);
471 LOGW("Failed to get value of VCONFKEY_STICKER_SUPPORTED_FEATURE");
473 pending_request.category = string("arsticker");
474 ReqQueue.push(pending_request);
475 pending_request.category = string("bitmoji");
476 ReqQueue.push(pending_request);
479 if (priv_data.socket)
480 process_request_queue();
483 void request_sticker_feature()
485 JsonObject *j_object = NULL;
487 if (!priv_data.socket) {
489 StickerRequest pending_request;
490 pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
491 ReqQueue.push(pending_request);
492 LOGI("Push sync feature request");
496 j_object = json_object_new();
497 if (j_object == NULL) {
498 LOGE("json object create error");
502 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_FEATURE_REQ);
503 json_object_set_int_member(j_object, "tID", ++t_id);
505 if (_send_json_data(j_object) == FALSE) {
506 LOGE("Failed to send STICKER_SYNC_FEATURE_REQ");
512 json_object_unref(j_object);
514 if (_create_thumbnail_directory() != 0)
515 LOGE("Failed to create thumbnail directory");
518 void send_disconnect_message()
520 JsonObject *j_object = NULL;
521 j_object = json_object_new();
522 if (j_object == NULL) {
523 LOGE("json object create error");
527 json_object_set_string_member(j_object, "msgId", STICKER_SEND_DISCONNECT_REQ);
528 json_object_set_int_member(j_object, "tID", t_id);
530 if (_send_json_data(j_object) == FALSE) {
531 LOGE("Failed to send STICKER_SEND_DISCONNECT_REQ");
534 json_object_unref(j_object);
537 void request_show_sync_notification()
539 JsonObject *j_object = NULL;
541 if (!priv_data.socket) {
543 StickerRequest pending_request;
544 pending_request.req_type = REQUEST_TYPE_SHOW_NOTIFICATION;
545 ReqQueue.push(pending_request);
546 LOGI("Push show notification request");
550 j_object = json_object_new();
551 if (j_object == NULL) {
552 LOGE("json object create error");
556 json_object_set_string_member(j_object, "msgId", STICKER_REQUEST_NOTI_REQ);
557 json_object_set_int_member(j_object, "tID", ++t_id);
559 if (_send_json_data(j_object) == FALSE) {
560 LOGE("Failed to send STICKER_REQUEST_NOTI_REQ");
565 json_object_unref(j_object);
570 int ret = sap_file_transfer_reject(priv_data.file_socket);
571 LOGI("ret : %d", ret);
573 file_on_progress = FALSE;
576 static void _on_transfer_file_cb(sap_peer_agent_h peer_agent_h,
577 sap_file_transaction_h socket,
578 const char *file_path,
581 file_on_progress = TRUE;
582 priv_data.file_socket = socket;
583 LOGI("# incoming file request.");
584 __set_file_transfer_cb(priv_data.file_socket);
586 incoming_file_name = file_path;
587 std::size_t found = incoming_file_name.find_last_of("/");
588 incoming_file_name = incoming_file_name.substr(found+1);
590 LOGI("# file path : %s, incoming file name : %s", file_path, incoming_file_name.c_str());
595 void conn_terminated(sap_peer_agent_h peer_agent,
597 sap_service_connection_terminated_reason_e result,
600 sap_info_s *priv = NULL;
601 priv = (sap_info_s *)user_data;
605 case SAP_CONNECTION_TERMINATED_REASON_PEER_DISCONNECTED:
606 LOGW("Peer Disconnected");
608 case SAP_CONNECTION_TERMINATED_REASON_DEVICE_DETACHED:
609 LOGW("Disconnected Device Detached");
611 case SAP_CONNECTION_TERMINATED_REASON_UNKNOWN:
612 LOGW("Disconnected Unknown Reason");
615 LOGW("connection terminated. reason : %d", result);
619 sap_socket_destroy(priv->socket);
622 set_sync_progressing(FALSE);
623 send_message("sync_stop_result", "cancel");
629 job_progress = FALSE;
630 send_disconnect_message();
634 static void send_sync_start_response(int result_code)
636 int feature_flag = 0;
637 string response_to_app;
638 switch (result_code) {
639 case SYNC_START_RSP_SUCCESS:
641 response_to_app = "success";
642 if (current_request.category == string("arsticker"))
643 sync_complete_flags |= VCONFKEY_STICKER_FEATURE_AREMOJI;
644 else if (current_request.category == string("bitmoji"))
645 sync_complete_flags |= VCONFKEY_STICKER_FEATURE_BITMOJI;
648 case SYNC_START_RSP_NO_STICKER:
649 response_to_app = "no_sticker";
651 case SYNC_START_RSP_BITMOJI_ALL_DELETE:
654 if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &is_synced) != 0)
655 LOGW("Failed to read sync completion");
657 if (is_synced == VCONFKEY_STICKER_SYNC_COMPLETE_NONE)
658 response_to_app = "no_sticker";
660 response_to_app = "success";
663 case SYNC_START_RSP_AREMOJI_ALL_DELETE:
664 response_to_app = "success";
667 response_to_app = "unknown_error";
671 LOGD("result code : %d, sync complete flag : %d", result_code, sync_complete_flags);
673 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
674 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) != 0)
676 LOGW("Failed to read support feature");
680 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
681 if (feature_flag == VCONFKEY_STICKER_FEATURE_AREMOJI ||
682 feature_flag == VCONFKEY_STICKER_FEATURE_BITMOJI)
684 LOGD("only standalone sync mode");
685 send_message("sync_start_response", response_to_app.c_str());
688 if (current_request.category == string("arsticker"))
690 if (result_code == SYNC_START_RSP_SUCCESS)
691 send_message("sync_start_response", response_to_app.c_str());
693 else if (current_request.category == string("bitmoji"))
695 if (sync_complete_flags == 0 ||
696 sync_complete_flags == VCONFKEY_STICKER_FEATURE_BITMOJI)
697 send_message("sync_start_response", response_to_app.c_str());
704 on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
705 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
707 unsigned int buf_len = strlen((char *)buffer);
708 LOGI("received data: %s, len: %d, buffer len : %d", (char *)buffer, payload_length, buf_len);
710 JsonParser *parser = json_parser_new();
711 GError *err_msg = NULL;
712 JsonNode *root = NULL;
713 JsonObject *root_obj = NULL;
715 const char *json_msgid = NULL;
717 json_parser_load_from_data(parser, (char *)buffer, payload_length, &err_msg);
719 LOGE("failed to load json file. error message: %s", err_msg->message);
723 root = json_parser_get_root(parser);
725 LOGE("failed to get root");
729 root_obj = json_node_get_object(root);
730 if (root_obj == NULL) {
731 LOGE("failed to get object");
735 json_msgid = json_object_get_string_member(root_obj, "msgId");
736 msg_id = string(json_msgid ? json_msgid : "");
737 if (msg_id == STICKER_REQUEST_SYNC_REQ) {
738 request_all_sticker_data("manual", "input");
739 } else if (msg_id == STICKER_SYNC_FEATURE_RSP) {
740 LOGD("msg : %s", msg_id.c_str());
741 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
742 const char *json_aremoji = json_object_get_string_member(root_obj, "arEmoji");
743 const char *json_bitmoji = json_object_get_string_member(root_obj, "bitmoji");
744 string support_aremoji = string(json_aremoji ? json_aremoji : "");
745 string support_bitmoji = string(json_bitmoji ? json_bitmoji : "");
746 int supported_feature = VCONFKEY_STICKER_FEATURE_NONE;
748 if (support_aremoji == "support")
749 supported_feature |= VCONFKEY_STICKER_FEATURE_AREMOJI;
751 if (support_bitmoji == "support")
752 supported_feature |= VCONFKEY_STICKER_FEATURE_BITMOJI;
754 if (vconf_set_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, supported_feature) == 0)
755 LOGD("Succeed to set supported feature");
757 LOGW("Fail to set supported feature");
759 LOGW("No vconf supported feature");
762 if (!process_request_queue()) {
765 } else if (msg_id == STICKER_SYNC_START_RSP) {
766 LOGD("msg : %s", msg_id.c_str());
767 const char *json_result = json_object_get_string_member(root_obj, "result");
768 string result = string(json_result ? json_result : "");
769 int result_code = json_object_get_int_member(root_obj, "resultCode");
770 LOGD("result : %s, resultCode : %d", result.c_str(), result_code);
772 if(result_code == SYNC_START_RSP_SUCCESS) {
773 send_sync_start_response(result_code);
774 if (current_request.category == string("bitmoji")) {
775 LOGD("Delete all bitmoji stickers");
776 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
777 delete_all_stickers("bitmoji");
778 destroy_sticker_provider_handle();
782 if (result_code == SYNC_START_RSP_BITMOJI_ALL_DELETE) {
783 LOGD("Delete all bitmoji stickers");
784 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
785 delete_all_stickers("bitmoji");
786 destroy_sticker_provider_handle();
788 } else if (result_code == SYNC_START_RSP_AREMOJI_ALL_DELETE) {
789 LOGD("Delete all AR Emoji stickers");
790 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
791 delete_all_stickers("arsticker");
792 destroy_sticker_provider_handle();
796 if (!process_request_queue()) {
797 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
798 int feature_flag = 0;
800 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
802 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
803 if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
805 if (current_request.category == string("bitmoji"))
807 /* other sticker group(s) has been synchronized completely */
808 if (sync_success_cnt > 0) {
815 LOGW("Failed to read support feature");
818 LOGW("No vconf supported feature");
819 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
821 send_sync_start_response(result_code);
822 set_sync_progressing(FALSE);
823 save_last_sync_time();
828 } else if (msg_id == STICKER_SEND_START_REQ) {
829 LOGD("msg : %s", msg_id.c_str());
830 total_file_count_in_group = 0;
831 rec_file_cnt_in_group = 0;
832 t_id = json_object_get_int_member(root_obj, "tID");
833 JsonArray *file_list = json_object_get_array_member(root_obj, "list");
835 int arr_len = json_array_get_length(file_list);
836 for (int i = 0; i < arr_len; i++) {
837 JsonObject *file_obj = json_array_get_object_element(file_list, i);
838 int file_len = json_object_get_int_member(file_obj, "size");
839 const char *json_filename = json_object_get_string_member(file_obj, "fileName");
840 string file_name = string(json_filename ? json_filename : "");
843 LOGD("Add file : %s, len : %d", file_name.c_str(), file_len);
844 total_file_count_in_group++;
847 char file_path[PATH_MAX];
848 char del_path[PATH_MAX];
849 std::size_t found = file_name.find_last_of("/");
850 string del_file_name = file_name.substr(found+1);
851 char *data_path = app_get_shared_data_path();
853 snprintf(file_path, sizeof(file_path), "%s/%s", data_path, del_file_name.c_str());
854 snprintf(del_path, sizeof(del_path), "%s/%s%s",STICKER_DIRECTORY, app_id, file_path);
856 LOGD("Delete file : %s, len : %d", del_path, file_len);
857 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
858 delete_sticker_data(del_path);
859 destroy_sticker_provider_handle();
868 JsonObject *j_object = json_object_new();
869 if (j_object == NULL) {
870 LOGE("Unable to json_object_new");
874 json_object_set_string_member(j_object, "msgId", STICKER_SEND_START_RSP);
875 json_object_set_int_member(j_object, "tID", t_id);
876 json_object_set_string_member(j_object, "result", "success");
878 if (_send_json_data(j_object) == FALSE)
879 LOGE("Failed to send message");
881 json_object_unref(j_object);
882 } else if (msg_id == STICKER_SEND_FACE_DATA) {
883 LOGD("msg : %s", msg_id.c_str());
884 const char *type_data = json_object_get_string_member(root_obj, "type");
886 sticker_data.disp_type = string(type_data);
888 const char *category = json_object_get_string_member(root_obj, "category");
890 sticker_data.group = string(category);
891 sticker_data.keyword = string(category);
893 } else if (msg_id == STICKER_SEND_STOP_REQ) {
894 LOGD("msg : %s", msg_id.c_str());
895 const char *json_reason = json_object_get_string_member(root_obj, "reason");
896 string reason = string(json_reason ? json_reason : "");
897 int file_len = json_object_get_int_member(root_obj, "count");
899 JsonObject *j_object = json_object_new();
900 if (j_object == NULL) {
901 LOGE("Unable to json_object_new");
905 json_object_set_string_member(j_object, "msgId", STICKER_SEND_STOP_RSP);
906 json_object_set_int_member(j_object, "tID", t_id);
908 if (reason == "complete") {
909 if (rec_file_cnt_in_group == file_len) {
910 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
911 int feature_flag = 0;
913 if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0)
915 LOGD("feature : %d, current request category : %s", feature_flag, current_request.category.c_str());
916 if (feature_flag == VCONFKEY_STICKER_FEATURE_AREMOJI)
918 if (current_request.category == string("arsticker"))
921 set_sync_progressing(FALSE);
924 else if (feature_flag & VCONFKEY_STICKER_FEATURE_BITMOJI)
926 if (current_request.category == string("bitmoji"))
929 set_sync_progressing(FALSE);
934 LOGW("Failed to read support feature");
937 LOGW("No vconf supported feature");
938 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
940 json_object_set_string_member(j_object, "result", "success");
944 json_object_set_string_member(j_object, "result", "failure");
948 json_object_set_string_member(j_object, "result", "failure");
951 if (_send_json_data(j_object) == FALSE)
952 LOGE("Failed to send message");
954 json_object_unref(j_object);
956 send_message("sync_stop_result", reason.c_str());
958 current_request.mode.clear();
959 current_request.category.clear();
960 current_request.type.clear();
962 if (!process_request_queue()) {
963 sync_success_cnt = 0;
968 LOGW("unknown msg id : %s", msg_id.c_str());
972 g_error_free(err_msg);
975 g_object_unref(parser);
978 static void on_conn_req(sap_peer_agent_h peer_agent,
980 sap_service_connection_result_e result,
985 case SAP_CONNECTION_SUCCESS:
986 LOGI("Connection success");
987 priv_data.socket = socket;
988 sap_peer_agent_accept_service_connection(peer_agent);
989 sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, &priv_data);
990 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
992 case SAP_CONNECTION_ALREADY_EXIST:
993 priv_data.socket = socket;
994 LOGI("Connection Already Exist");
996 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
997 LOGW("Connection failure device unreachable");
999 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
1000 LOGW("Connection failure invalid peer agent");
1002 case SAP_CONNECTION_FAILURE_NETWORK:
1003 LOGW("Connection failure network");
1005 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
1006 LOGW("Connection failure peer agent no response");
1008 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
1009 LOGW("Connection failure peer agent rejected");
1011 case SAP_CONNECTION_FAILURE_UNKNOWN:
1012 LOGW("Connection failure unknown");
1015 LOGW("Connection failure. error code : %d", result);
1021 _on_service_connection_created(sap_peer_agent_h peer_agent,
1022 sap_socket_h socket,
1023 sap_service_connection_result_e result,
1026 sap_info_s *priv = (sap_info_s *)user_data;
1030 case SAP_CONNECTION_SUCCESS:
1031 sap_peer_agent_set_service_connection_terminated_cb(priv->peer_agent,
1035 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
1036 priv->socket = socket;
1037 LOGI("Connection Established");
1039 process_request_queue();
1043 case SAP_CONNECTION_ALREADY_EXIST:
1044 priv->socket = socket;
1045 LOGI("Connection Already Exist");
1048 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
1049 LOGW("Connection Failure device unreachable");
1052 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
1053 LOGW("Connection Failure invalid peer agent");
1056 case SAP_CONNECTION_FAILURE_NETWORK:
1057 LOGW("Connection Failure network");
1060 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
1061 LOGW("Connection Failure peer agent no response");
1064 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
1065 LOGW("Connection Failure peer agent rejected");
1068 case SAP_CONNECTION_FAILURE_UNKNOWN:
1069 LOGW("Connection Failure peer agent unknown");
1072 case SAP_CONNECTION_IN_PROGRESS:
1073 LOGW("Connection in progress");
1076 case SAP_CONNECTION_PEER_AGENT_NOT_SUPPORTED:
1077 LOGW("Connection peer agent not supported");
1083 _create_service_connection(gpointer user_data)
1085 sap_info_s *priv = (sap_info_s *)user_data;
1086 int result = sap_agent_request_service_connection(priv->agent,
1088 _on_service_connection_created,
1091 LOGD("request connection result : %d", result);
1097 _on_peer_agent_updated(sap_peer_agent_h peer_agent,
1098 sap_peer_agent_status_e peer_status,
1099 sap_peer_agent_found_result_e result,
1102 sap_info_s *priv = (sap_info_s *)user_data;
1106 case SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED:
1107 LOGW("SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED");
1110 case SAP_PEER_AGENT_FOUND_RESULT_FOUND:
1111 if (peer_status == SAP_PEER_AGENT_STATUS_AVAILABLE)
1113 LOGD("SAP_PEER_AGENT_FOUND_RESULT_FOUND");
1114 priv->peer_agent = peer_agent;
1115 g_idle_add(_create_service_connection, priv);
1119 sap_peer_agent_destroy(peer_agent);
1123 case SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND:
1124 LOGW("SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND");
1127 case SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT:
1128 LOGW("SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT");
1131 case SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR:
1132 LOGW("SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR");
1141 _find_peer_agent(gpointer user_data)
1143 sap_info_s *priv = (sap_info_s *)user_data;
1144 sap_agent_find_peer_agent(priv->agent, _on_peer_agent_updated, priv);
1149 static void on_agent_initialized(sap_agent_h agent,
1150 sap_agent_initialized_result_e result,
1154 case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
1155 LOGD("agent is initialized");
1157 priv_data.agent = agent;
1159 sap_file_transfer_set_incoming_file_cb(agent, _on_transfer_file_cb, NULL);
1160 sap_agent_set_service_connection_requested_cb(agent, on_conn_req, NULL);
1163 case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
1164 LOGE("duplicate registration");
1166 case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
1167 LOGE("invalid arguments");
1169 case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
1170 LOGE("internal sap error");
1173 LOGE("unknown status (%d)", result);
1178 static void on_agent_deinitialized(sap_agent_h agent,
1179 sap_agent_deinitialized_result_e result,
1182 LOGD("result of deinitialize : %d", result);
1185 static void _on_device_status_changed(sap_device_status_e status,
1186 sap_transport_type_e transport_type,
1189 LOGD("%s, status :%d", __func__, status);
1191 switch (transport_type) {
1192 case SAP_TRANSPORT_TYPE_BT:
1193 LOGD("transport_type (%d): bt", transport_type);
1195 case SAP_TRANSPORT_TYPE_BLE:
1196 LOGD("transport_type (%d): ble", transport_type);
1198 case SAP_TRANSPORT_TYPE_TCP:
1199 LOGD("transport_type (%d): tcp/ip", transport_type);
1201 case SAP_TRANSPORT_TYPE_USB:
1202 LOGD("transport_type (%d): usb", transport_type);
1204 case SAP_TRANSPORT_TYPE_MOBILE:
1205 LOGD("transport_type (%d): mobile", transport_type);
1208 LOGE("unknown transport_type (%d)", transport_type);
1213 case SAP_DEVICE_STATUS_DETACHED:
1214 LOGD("device is not connected.");
1215 send_message("sync_stop_result", "cancel");
1217 case SAP_DEVICE_STATUS_ATTACHED:
1218 LOGD("Attached calling find peer now");
1219 g_idle_add(_find_peer_agent, &priv_data);
1222 LOGE("unknown status (%d)", status);
1227 gboolean agent_initialize()
1232 result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
1233 on_agent_initialized, NULL);
1235 LOGD("SAP >>> getRegisteredServiceAgent() >>> %d", result);
1236 } while (result != SAP_RESULT_SUCCESS);
1241 gboolean initialize_sap(void)
1243 sap_agent_h agent = NULL;
1245 if (priv_data.agent) {
1246 LOGW("duplicate initialize");
1250 sap_agent_create(&agent);
1252 priv_data.agent = agent;
1256 sap_set_device_status_changed_cb(_on_device_status_changed, NULL);
1261 void deinitialize_sap(void)
1263 LOGD("deinitialize");
1264 if (priv_data.agent) {
1265 int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
1267 case SAP_RESULT_FAILURE:
1268 LOGW("Failed to deinitialize");
1270 case SAP_RESULT_SUCCESS:
1271 LOGD("Succeed to deinitialize");
1273 case SAP_RESULT_PERMISSION_DENIED:
1274 LOGW("permission denied: deinitialize ");
1277 LOGD("deinitialize : %d", ret);
1281 sap_agent_destroy(priv_data.agent);
1282 priv_data.agent = NULL;
1286 gboolean is_init_sap()
1288 return priv_data.agent ? TRUE : FALSE;