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>
37 #include "sticker_info.h"
39 #include "sync_alarm.h"
41 #define ACCESSORY_SERVICE_PROFILE_ID "/sample/filetransfersender"
42 #define ACCESSORY_SERVICE_CHANNEL_ID 107
44 #define STICKER_SYNC_FEATURE_REQ "sticker-sync-feature-req"
45 #define STICKER_SYNC_FEATURE_RSP "sticker-sync-feature-rsp"
46 #define STICKER_SYNC_START_REQ "sticker-sync-start-req"
47 #define STICKER_SYNC_START_RSP "sticker-sync-start-rsp"
48 #define STICKER_SEND_START_REQ "sticker-send-start-req"
49 #define STICKER_SEND_START_RSP "sticker-send-start-rsp"
50 #define STICKER_SEND_FACE_DATA "sticker-send-face-data"
51 #define STICKER_SEND_STOP_REQ "sticker-send-stop-req"
52 #define STICKER_SEND_STOP_RSP "sticker-send-stop-rsp"
54 #ifndef VCONFKEY_STICKER_SUPPORTED_FEATURE
55 #define VCONFKEY_STICKER_SUPPORTED_FEATURE "db/sticker/supported_feature"
58 VCONFKEY_STICKER_FEATURE_NONE = 0x00,
59 VCONFKEY_STICKER_FEATURE_AREMOJI = 0x01,
60 VCONFKEY_STICKER_FEATURE_BITMOJI = 0x02
68 REQUEST_TYPE_FEATURE_REQ
72 SYNC_START_RSP_SUCCESS = 1000,
73 SYNC_START_RSP_NO_STICKER = 1001
79 sap_peer_agent_h peer_agent;
80 sap_file_transaction_h file_socket;
84 request_type req_type;
90 static void _reset_sticker_data();
101 _reset_sticker_data();
105 static struct sap_info_s priv_data = { 0 };
106 static struct sticker_info sticker_data;
107 static struct request pending_request, current_request;
109 gboolean file_on_progress = 0;
110 static string incoming_file_name;
112 static int rec_file_cnt = 0;
113 static int total_file_count = 0;
115 static gboolean _send_json_data(JsonObject *obj)
117 JsonGenerator *j_generator = NULL;
118 JsonNode *j_node = NULL;
123 j_generator = json_generator_new();
124 if (j_generator == NULL) {
125 LOGE("Unable to json_generator_new");
129 j_node = json_node_new(JSON_NODE_OBJECT);
130 if (j_node == NULL) {
131 LOGE("Unable to json_node_new");
135 json_node_set_object(j_node, obj);
136 json_generator_set_root(j_generator, j_node);
138 data = json_generator_to_data(j_generator, &size);
140 LOGE("Unable to json_generator_to_data");
144 if (priv_data.socket) {
145 LOGD("Send JSON data : %s", data);
146 result = sap_socket_send_data(priv_data.socket, ACCESSORY_SERVICE_CHANNEL_ID, strlen(data), (void *)data);
154 json_node_free(j_node);
157 g_object_unref(j_generator);
159 return result ? FALSE : TRUE;
162 static void notify_sync_progress(unsigned int file_progress)
164 if (total_file_count == 0)
167 double total_progress = (((double)rec_file_cnt / (double)total_file_count) + (1.0/(double)total_file_count*file_progress/100))*100;
169 LOGI("(%2d / %2d), file progress : %3u%%, total progress : %3u%%", rec_file_cnt, total_file_count, file_progress, (unsigned int)total_progress);
170 char progress_str[32];
171 snprintf(progress_str, sizeof(progress_str), "%u", (unsigned int)total_progress);
172 send_message("sync_progress", progress_str);
175 static void _on_send_completed(sap_file_transaction_h file_transaction,
176 sap_ft_transfer_e result,
177 const char *file_path,
180 if (priv_data.file_socket) {
181 sap_file_transfer_destroy(file_transaction);
182 priv_data.file_socket = NULL;
185 if (result == SAP_FT_TRANSFER_SUCCESS) {
186 LOGI("Transfer Completed");
188 if (chmod(sticker_data.file_path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
189 LOGE("Failed to change permission : %s. error : %s", sticker_data.file_path.c_str(), strerror(errno));
192 LOGI("Succeed to change permission : %s", sticker_data.file_path.c_str());
193 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
194 insert_sticker_data(sticker_data.file_path.c_str(), sticker_data.keyword.c_str(), sticker_data.group.c_str(), sticker_data.description.c_str());
195 destroy_sticker_provider_handle();
197 if (unlink(sticker_data.file_path.c_str()) == -1)
198 LOGE("Failed to remove sticker file");
205 case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
206 LOGW("Channel IO Error.");
210 case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
211 LOGW("File IO Error.");
215 case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED):
217 LOGW("Transfer dropped.");
221 case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE):
223 LOGW("Peer Un Responsive.");
227 case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST):
229 LOGW("Connection Lost.");
233 case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED):
235 LOGW("Peer Cancelled.");
239 case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE):
246 LOGW("Unknown Error");
251 file_on_progress = 0;
254 static void _on_sending_file_in_progress(sap_file_transaction_h file_transaction,
255 unsigned short int percentage_progress,
258 notify_sync_progress(percentage_progress);
261 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
263 LOGI("# set callbacks");
264 sap_file_transfer_set_progress_cb(file_socket, _on_sending_file_in_progress, NULL);
266 sap_file_transfer_set_done_cb(file_socket, _on_send_completed, NULL);
272 char file_path[PATH_MAX];
273 char *data_path = NULL;
275 data_path = app_get_shared_data_path();
276 LOGI("Path : %s", data_path);
277 sprintf(file_path, "%s/%s", data_path, incoming_file_name.c_str());
278 LOGI("Receive filepath : %s", file_path);
279 sticker_data.file_path = string(file_path);
282 ret = sap_file_transfer_receive(priv_data.file_socket, file_path);
284 case SAP_RESULT_PERMISSION_DENIED:
285 LOGW("permission denied");
287 case SAP_RESULT_FAILURE:
290 case SAP_RESULT_SUCCESS:
295 file_on_progress = 1;
298 void request_sticker_data(const char *mode, const char *category, const char *type)
300 JsonObject *j_object = json_object_new();
301 if (j_object == NULL) {
302 LOGE("json object create error");
306 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_START_REQ);
307 json_object_set_int_member(j_object, "tID", ++t_id);
308 json_object_set_string_member(j_object, "mode", mode);
309 json_object_set_string_member(j_object, "category", category);
310 json_object_set_string_member(j_object, "type", type);
312 if (_send_json_data(j_object) == FALSE) {
313 pending_request.req_type = REQUEST_TYPE_SYNC;
314 pending_request.mode = string(mode ? mode : "manual");
315 pending_request.category = string(category? category : "arsticker");
316 pending_request.type = string(type ? type : "input");
318 LOGI("Push sync request");
321 current_request.req_type = REQUEST_TYPE_SYNC;
322 current_request.mode = string(mode ? mode : "manual");
323 current_request.category = string(category? category : "arsticker");
324 current_request.type = string(type ? type : "input");
327 json_object_unref(j_object);
330 void request_sticker_feature()
332 JsonObject *j_object = json_object_new();
333 if (j_object == NULL) {
334 LOGE("json object create error");
338 json_object_set_string_member(j_object, "msgId", STICKER_SYNC_FEATURE_REQ);
339 json_object_set_int_member(j_object, "tID", ++t_id);
341 if (_send_json_data(j_object) == FALSE) {
342 pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
343 LOGE("Failed to send STICKER_SYNC_FEATURE_REQ");
346 json_object_unref(j_object);
351 int ret = sap_file_transfer_reject(priv_data.file_socket);
352 LOGI("ret : %d", ret);
354 file_on_progress = 0;
357 static void _on_receive_file_cb(sap_peer_agent_h peer_agent_h,
358 sap_file_transaction_h socket,
359 const char *file_path,
362 file_on_progress = 1;
363 priv_data.file_socket = socket;
364 LOGI("# incoming file request.");
365 __set_file_transfer_cb(priv_data.file_socket);
367 incoming_file_name = file_path;
368 std::size_t found = incoming_file_name.find_last_of("/");
369 incoming_file_name = incoming_file_name.substr(found+1);
371 LOGI("# file path : %s, incoming file name : %s", file_path, incoming_file_name.c_str());
376 void conn_terminated(sap_peer_agent_h peer_agent,
378 sap_service_connection_terminated_reason_e result,
381 sap_info_s *priv = NULL;
382 priv = (sap_info_s *)user_data;
386 case SAP_CONNECTION_TERMINATED_REASON_PEER_DISCONNECTED:
387 LOGW("Peer Disconnected");
389 case SAP_CONNECTION_TERMINATED_REASON_DEVICE_DETACHED:
390 LOGW("Disconnected Device Detached");
392 case SAP_CONNECTION_TERMINATED_REASON_UNKNOWN:
393 LOGW("Disconnected Unknown Reason");
396 LOGW("connection terminated. reason : %d", result);
400 sap_socket_destroy(priv->socket);
406 static void _reset_sticker_data()
408 sticker_data.file_path.clear();
409 sticker_data.group.clear();
410 sticker_data.keyword.clear();
411 sticker_data.disp_type.clear();
412 sticker_data.description.clear();
416 on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
417 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
419 unsigned int buf_len = strlen((char *)buffer);
420 LOGI("received data: %s, len: %d, buffer len : %d", (char *)buffer, payload_length, buf_len);
422 JsonParser *parser = json_parser_new();
423 GError *err_msg = NULL;
424 JsonNode *root = NULL;
425 JsonObject *root_obj = NULL;
427 const char *json_msgid = NULL;
429 json_parser_load_from_data(parser, (char *)buffer, payload_length, &err_msg);
431 LOGE("failed to load json file. error message: %s", err_msg->message);
435 root = json_parser_get_root(parser);
437 LOGE("failed to get root");
441 root_obj = json_node_get_object(root);
442 if (root_obj == NULL) {
443 LOGE("failed to get object");
447 json_msgid = json_object_get_string_member(root_obj, "msgId");
448 msg_id = string(json_msgid ? json_msgid : "");
449 if (msg_id == STICKER_SYNC_FEATURE_RSP) {
450 LOGD("msg : %s", msg_id.c_str());
451 const char *json_aremoji = json_object_get_string_member(root_obj, "arEmoji");
452 const char *json_bitmoji = json_object_get_string_member(root_obj, "bitmoji");
453 string support_aremoji = string(json_aremoji ? json_aremoji : "");
454 string support_bitmoji = string(json_bitmoji ? json_bitmoji : "");
455 int supported_feature = VCONFKEY_STICKER_FEATURE_NONE;
457 if (support_aremoji == "support")
458 supported_feature |= VCONFKEY_STICKER_FEATURE_AREMOJI;
460 if (support_bitmoji == "support")
461 supported_feature |= VCONFKEY_STICKER_FEATURE_BITMOJI;
463 if (vconf_set_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, supported_feature) == 0)
464 LOGD("Succeed to set supported feature");
466 LOGW("Fail to set supported feature");
469 } else if (msg_id == STICKER_SYNC_START_RSP) {
470 LOGD("msg : %s", msg_id.c_str());
471 const char *json_result = json_object_get_string_member(root_obj, "result");
472 string result = string(json_result ? json_result : "");
473 int result_code = json_object_get_int_member(root_obj, "resultCode");
474 LOGD("result : %s, resultCode : %d", result.c_str(), result_code);
476 string response_to_app;
477 switch (result_code) {
478 case SYNC_START_RSP_SUCCESS:
479 response_to_app = "success";
481 case SYNC_START_RSP_NO_STICKER:
482 response_to_app = "no_sticker";
485 response_to_app = "unknown_error";
489 send_message("sync_start_response", response_to_app.c_str());
491 if (result_code != SYNC_START_RSP_SUCCESS)
493 } else if (msg_id == STICKER_SEND_START_REQ) {
494 LOGD("msg : %s", msg_id.c_str());
495 total_file_count = 0;
497 t_id = json_object_get_int_member(root_obj, "tID");
498 JsonArray *file_list = json_object_get_array_member(root_obj, "list");
500 int arr_len = json_array_get_length(file_list);
501 for (int i = 0; i < arr_len; i++) {
502 JsonObject *file_obj = json_array_get_object_element(file_list, i);
503 int file_len = json_object_get_int_member(file_obj, "size");
504 const char *json_filename = json_object_get_string_member(file_obj, "fileName");
505 string file_name = string(json_filename ? json_filename : "");
508 LOGD("Add file : %s, len : %d", file_name.c_str(), file_len);
511 LOGD("Delete file : %s, len : %d", file_name.c_str(), file_len);
512 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
513 delete_sticker_data(file_name.c_str());
514 destroy_sticker_provider_handle();
520 JsonObject *j_object = json_object_new();
521 if (j_object == NULL) {
522 LOGE("Unable to json_object_new");
526 json_object_set_string_member(j_object, "msgId", STICKER_SEND_START_RSP);
527 json_object_set_int_member(j_object, "tID", t_id);
528 json_object_set_string_member(j_object, "result", "success");
530 if (_send_json_data(j_object) == FALSE)
531 LOGE("Failed to send message");
533 json_object_unref(j_object);
534 } else if (msg_id == STICKER_SEND_FACE_DATA) {
535 LOGD("msg : %s", msg_id.c_str());
536 _reset_sticker_data();
538 const char *type_data = json_object_get_string_member(root_obj, "type");
540 sticker_data.disp_type = string(type_data);
542 const char *category = json_object_get_string_member(root_obj, "category");
544 sticker_data.group = string(category);
545 sticker_data.keyword = string(category);
547 } else if (msg_id == STICKER_SEND_STOP_REQ) {
548 LOGD("msg : %s", msg_id.c_str());
549 const char *json_reason = json_object_get_string_member(root_obj, "reason");
550 string reason = string(json_reason ? json_reason : "");
551 int file_len = json_object_get_int_member(root_obj, "count");
553 JsonObject *j_object = json_object_new();
554 if (j_object == NULL) {
555 LOGE("Unable to json_object_new");
559 json_object_set_string_member(j_object, "msgId", STICKER_SEND_STOP_RSP);
560 json_object_set_int_member(j_object, "tID", t_id);
562 if (reason == "complete" && rec_file_cnt != file_len)
563 json_object_set_string_member(j_object, "result", "failure");
565 int complete_flags = 0;
566 if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &complete_flags) == 0) {
567 if (sticker_data.group == string("arsticker"))
568 complete_flags |= 0x1;
569 else if (sticker_data.group == string("bitmoji"))
570 complete_flags |= 0x2;
572 LOGD("sync complete flag : %x", complete_flags);
574 if (vconf_set_int(VCONFKEY_STICKER_SYNC_COMPLETE, complete_flags) == 0)
575 LOGD("Succeed to set sync complete");
577 LOGW("Fail to set sync complete");
580 LOGW("Failed to get sticker sync flags");
583 json_object_set_string_member(j_object, "result", "success");
585 if (current_request.mode == string("manual")) {
586 if (!sync_alarm_exist())
587 sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
591 if (_send_json_data(j_object) == FALSE)
592 LOGE("Failed to send message");
594 send_message("sync_stop_result", reason.c_str());
596 json_object_unref(j_object);
598 current_request.mode.clear();
599 current_request.category.clear();
600 current_request.type.clear();
604 LOGW("unknown msg id : %s", msg_id.c_str());
608 g_error_free(err_msg);
611 g_object_unref(parser);
614 static void on_conn_req(sap_peer_agent_h peer_agent,
616 sap_service_connection_result_e result,
621 case SAP_CONNECTION_SUCCESS:
622 LOGI("Connection success");
623 priv_data.socket = socket;
624 sap_peer_agent_accept_service_connection(peer_agent);
625 sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, &priv_data);
626 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
628 case SAP_CONNECTION_ALREADY_EXIST:
629 priv_data.socket = socket;
630 LOGI("Connection Already Exist");
632 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
633 LOGW("Connection failure device unreachable");
635 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
636 LOGW("Connection failure invalid peer agent");
638 case SAP_CONNECTION_FAILURE_NETWORK:
639 LOGW("Connection failure network");
641 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
642 LOGW("Connection failure peer agent no response");
644 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
645 LOGW("Connection failure peer agent rejected");
647 case SAP_CONNECTION_FAILURE_UNKNOWN:
648 LOGW("Connection failure unknown");
651 LOGW("Connection failure. error code : %d", result);
657 _on_service_connection_created(sap_peer_agent_h peer_agent,
659 sap_service_connection_result_e result,
662 sap_info_s *priv = (sap_info_s *)user_data;
666 case SAP_CONNECTION_SUCCESS:
667 sap_peer_agent_set_service_connection_terminated_cb(priv->peer_agent,
671 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
672 priv->socket = socket;
673 LOGI("Connection Established");
675 LOGD("pending_request : %d", pending_request.req_type);
677 if (pending_request.req_type == REQUEST_TYPE_FEATURE_REQ) {
678 LOGD("[Request feature exchange]");
679 request_sticker_feature();
681 else if (pending_request.req_type == REQUEST_TYPE_SYNC) {
682 if (!pending_request.mode.empty()) {
683 LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", pending_request.mode.c_str(),
684 pending_request.category.c_str(),
685 pending_request.type.c_str());
687 request_sticker_data(pending_request.mode.c_str(), pending_request.category.c_str(), pending_request.type.c_str());
689 pending_request.mode.clear();
690 pending_request.category.clear();
691 pending_request.type.clear();
697 case SAP_CONNECTION_ALREADY_EXIST:
698 priv->socket = socket;
699 LOGI("Connection Already Exist");
702 case SAP_CONNECTION_FAILURE_DEVICE_UNREACHABLE:
703 LOGW("Connection Failure device unreachable");
706 case SAP_CONNECTION_FAILURE_INVALID_PEERAGENT:
707 LOGW("Connection Failure invalid peer agent");
710 case SAP_CONNECTION_FAILURE_NETWORK:
711 LOGW("Connection Failure network");
714 case SAP_CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE:
715 LOGW("Connection Failure peer agent no response");
718 case SAP_CONNECTION_FAILURE_PEERAGENT_REJECTED:
719 LOGW("Connection Failure peer agent rejected");
722 case SAP_CONNECTION_FAILURE_UNKNOWN:
723 LOGW("Connection Failure peer agent unknown");
726 case SAP_CONNECTION_IN_PROGRESS:
727 LOGW("Connection in progress");
730 case SAP_CONNECTION_PEER_AGENT_NOT_SUPPORTED:
731 LOGW("Connection peer agent not supported");
737 _create_service_connection(gpointer user_data)
739 sap_info_s *priv = (sap_info_s *)user_data;
740 int result = sap_agent_request_service_connection(priv->agent,
742 _on_service_connection_created,
745 LOGD("request connection result : %d", result);
751 _on_peer_agent_updated(sap_peer_agent_h peer_agent,
752 sap_peer_agent_status_e peer_status,
753 sap_peer_agent_found_result_e result,
756 sap_info_s *priv = (sap_info_s *)user_data;
760 case SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED:
761 LOGW("SAP_PEER_AGENT_FOUND_RESULT_DEVICE_NOT_CONNECTED");
764 case SAP_PEER_AGENT_FOUND_RESULT_FOUND:
765 if (peer_status == SAP_PEER_AGENT_STATUS_AVAILABLE)
767 LOGD("SAP_PEER_AGENT_FOUND_RESULT_FOUND");
768 priv->peer_agent = peer_agent;
769 g_idle_add(_create_service_connection, priv);
773 sap_peer_agent_destroy(peer_agent);
777 case SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND:
778 LOGW("SAP_PEER_AGENT_FOUND_RESULT_SERVICE_NOT_FOUND");
781 case SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT:
782 LOGW("SAP_PEER_AGENT_FOUND_RESULT_TIMEDOUT");
785 case SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR:
786 LOGW("SAP_PEER_AGENT_FOUND_RESULT_INTERNAL_ERROR");
795 _find_peer_agent(gpointer user_data)
797 sap_info_s *priv = (sap_info_s *)user_data;
798 sap_agent_find_peer_agent(priv->agent, _on_peer_agent_updated, priv);
803 static void on_agent_initialized(sap_agent_h agent,
804 sap_agent_initialized_result_e result,
808 case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
809 LOGD("agent is initialized");
811 priv_data.agent = agent;
813 sap_file_transfer_set_incoming_file_cb(agent, _on_receive_file_cb, NULL);
814 sap_agent_set_service_connection_requested_cb(agent, on_conn_req, NULL);
817 case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
818 LOGE("duplicate registration");
820 case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
821 LOGE("invalid arguments");
823 case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
824 LOGE("internal sap error");
827 LOGE("unknown status (%d)", result);
832 static void on_agent_deinitialized(sap_agent_h agent,
833 sap_agent_deinitialized_result_e result,
836 LOGD("result of deinitialize : %d", result);
839 static void _on_device_status_changed(sap_device_status_e status,
840 sap_transport_type_e transport_type,
843 LOGD("%s, status :%d", __func__, status);
845 switch (transport_type) {
846 case SAP_TRANSPORT_TYPE_BT:
847 LOGD("transport_type (%d): bt", transport_type);
849 case SAP_TRANSPORT_TYPE_BLE:
850 LOGD("transport_type (%d): ble", transport_type);
852 case SAP_TRANSPORT_TYPE_TCP:
853 LOGD("transport_type (%d): tcp/ip", transport_type);
855 case SAP_TRANSPORT_TYPE_USB:
856 LOGD("transport_type (%d): usb", transport_type);
858 case SAP_TRANSPORT_TYPE_MOBILE:
859 LOGD("transport_type (%d): mobile", transport_type);
862 LOGE("unknown transport_type (%d)", transport_type);
867 case SAP_DEVICE_STATUS_DETACHED:
868 LOGD("device is not connected.");
870 case SAP_DEVICE_STATUS_ATTACHED:
871 LOGD("Attached calling find peer now");
872 g_idle_add(_find_peer_agent, &priv_data);
875 LOGE("unknown status (%d)", status);
880 gboolean agent_initialize()
885 result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
886 on_agent_initialized, NULL);
888 LOGD("SAP >>> getRegisteredServiceAgent() >>> %d", result);
889 } while (result != SAP_RESULT_SUCCESS);
894 gboolean initialize_sap(void)
896 sap_agent_h agent = NULL;
898 sap_agent_create(&agent);
900 priv_data.agent = agent;
904 sap_set_device_status_changed_cb(_on_device_status_changed, NULL);
909 void deinitialize_sap(void)
911 if (priv_data.agent) {
912 int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
914 case SAP_RESULT_FAILURE:
915 LOGW("Failed to deinitialize");
917 case SAP_RESULT_SUCCESS:
918 LOGD("Succeed to deinitialize");
920 case SAP_RESULT_PERMISSION_DENIED:
921 LOGW("permission denied: deinitialize ");
924 LOGD("deinitialize : %d", ret);
928 sap_agent_destroy(priv_data.agent);
929 priv_data.agent = NULL;