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 <app_common.h>
22 #include <sap_file_transfer.h>
23 #include <sticker_provider.h>
27 #include <sys/types.h>
31 #define ACCESSORY_SERVICE_PROFILE_ID "/sample/filetransfersender"
33 #include "sticker_info.h"
39 sap_file_transaction_h socket;
42 static struct priv priv_data = { 0 };
44 gboolean file_on_progress = 0;
45 static string incoming_file_name;
46 static string recv_filepath;
48 static void _on_send_completed(sap_file_transaction_h file_transaction,
49 sap_ft_transfer_e result,
50 const char *file_path,
53 char error_message[100];
55 dlog_print(DLOG_INFO, TAG, "# transfer completed");
57 if (priv_data.socket) {
58 sap_file_transfer_destroy(file_transaction);
59 priv_data.socket = NULL;
62 if (result == SAP_FT_TRANSFER_SUCCESS) {
63 sprintf(error_message, "Transfer Completed");
64 dlog_print(DLOG_INFO, TAG, "Transfer Completed");
66 if (chmod(recv_filepath.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
67 dlog_print(DLOG_ERROR, TAG, "Failed to change permission : %s. error : %s", recv_filepath.c_str(), strerror(errno));
70 dlog_print(DLOG_INFO, TAG, "Succeed to change permission : %s", recv_filepath.c_str());
71 create_sticker_provider_handle();
72 insert_sticker_data(recv_filepath.c_str(), "keyword", "group", "test icon");
73 destroy_sticker_provider_handle();
77 case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
78 sprintf(error_message, "Channel IO Error.");
79 dlog_print(DLOG_WARN, TAG, "Channel IO Error.");
83 case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
84 sprintf(error_message, "File IO Error.");
85 dlog_print(DLOG_WARN, TAG, "File IO Error.");
89 case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED): {
90 sprintf(error_message, "Transfer dropped.");
91 dlog_print(DLOG_WARN, TAG, "Transfer dropped.");
95 case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE): {
96 sprintf(error_message, "Peer Un Responsive.");
97 dlog_print(DLOG_WARN, TAG, "Peer Un Responsive.");
101 case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST): {
102 sprintf(error_message, "Connection Lost.");
103 dlog_print(DLOG_WARN, TAG, "Connection Lost.");
107 case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED): {
108 sprintf(error_message, "Peer Cancelled.");
109 dlog_print(DLOG_WARN, TAG, "Peer Cancelled.");
113 case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE): {
114 sprintf(error_message, "No Space.");
115 dlog_print(DLOG_WARN, TAG, "No Space.");
120 sprintf(error_message, "Unknown Error");
121 dlog_print(DLOG_WARN, TAG, "Unknown Error");
125 file_on_progress = 0;
128 static void _on_sending_file_in_progress(sap_file_transaction_h file_transaction,
129 unsigned short int percentage_progress,
132 dlog_print(DLOG_INFO, TAG, "# progress %d", percentage_progress);
135 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
137 dlog_print(DLOG_INFO, TAG, "# set callbacks");
138 sap_file_transfer_set_progress_cb(file_socket, _on_sending_file_in_progress, NULL);
140 sap_file_transfer_set_done_cb(file_socket, _on_send_completed, NULL);
147 char *data_path = NULL;
149 data_path = app_get_shared_data_path();
150 dlog_print(DLOG_INFO, TAG, "Path : %s", data_path);
151 sprintf(file_path, "%s/%s", data_path, incoming_file_name.c_str());
152 dlog_print(DLOG_INFO, TAG, "Receive filepath : %s", file_path);
153 recv_filepath = string(file_path);
156 ret = sap_file_transfer_receive(priv_data.socket, file_path);
158 case SAP_RESULT_PERMISSION_DENIED:
159 dlog_print(DLOG_WARN, TAG, "permission denied");
161 case SAP_RESULT_FAILURE:
162 dlog_print(DLOG_WARN, TAG, "Fail");
164 case SAP_RESULT_SUCCESS:
165 dlog_print(DLOG_INFO, TAG, "Success");
169 file_on_progress = 1;
172 void sap_file_transfer_get_receive_filepath(char **filepath)
174 *filepath = strdup(recv_filepath.c_str());
179 int ret = sap_file_transfer_reject(priv_data.socket);
180 dlog_print(DLOG_INFO, TAG, "ret : %d", ret);
182 file_on_progress = 0;
185 static void _on_receive_file_cb(sap_peer_agent_h peer_agent_h,
186 sap_file_transaction_h socket,
187 const char *file_path,
190 file_on_progress = 1;
191 priv_data.socket = socket;
192 dlog_print(DLOG_INFO, TAG, "# incoming file request.");
193 __set_file_transfer_cb(priv_data.socket);
195 incoming_file_name = file_path;
196 std::size_t found = incoming_file_name.find_last_of("/");
197 incoming_file_name = incoming_file_name.substr(found+1);
199 dlog_print(DLOG_INFO, TAG, "# file path : %s, incoming file name : %s", file_path, incoming_file_name.c_str());
204 void conn_terminated(sap_peer_agent_h peer_agent,
206 sap_service_connection_terminated_reason_e result,
209 dlog_print(DLOG_INFO, TAG, "connection terminated");
213 on_message_received(sap_peer_agent_h peer_agent, unsigned int payload_length, void *buffer,
214 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
217 sap_peer_agent_get_app_name(peer_agent, &peer_app);
218 dlog_print(DLOG_INFO, TAG, "received data: %s, len:%d from %s", (char *)buffer, payload_length, peer_app);
223 on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
224 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
226 dlog_print(DLOG_INFO, TAG, "received data: %s, len:%d", (char *)buffer, payload_length);
229 static void on_conn_req(sap_peer_agent_h peer_agent,
231 sap_service_connection_result_e result,
234 sap_peer_agent_accept_service_connection(peer_agent);
235 sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, NULL);
237 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
240 static void on_agent_initialized(sap_agent_h agent,
241 sap_agent_initialized_result_e result,
245 case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
247 dlog_print(DLOG_DEBUG, TAG, "agent is initialized");
249 priv_data.agent = agent;
251 sap_file_transfer_set_incoming_file_cb(agent, _on_receive_file_cb, NULL);
252 sap_agent_set_service_connection_requested_cb(agent, on_conn_req, NULL);
256 case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
257 dlog_print(DLOG_ERROR, TAG, "duplicate registration");
261 case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
262 dlog_print(DLOG_ERROR, TAG, "invalid arguments");
266 case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
267 dlog_print(DLOG_ERROR, TAG, "internal sap error");
272 dlog_print(DLOG_ERROR, TAG, "unknown status (%d)", result);
278 static void _on_device_status_changed(sap_device_status_e status,
279 sap_transport_type_e transport_type,
282 dlog_print(DLOG_DEBUG, TAG, "%s, status :%d", __func__, status);
284 switch (transport_type) {
285 case SAP_TRANSPORT_TYPE_BT:
286 dlog_print(DLOG_DEBUG, TAG, "transport_type (%d): bt", transport_type);
289 case SAP_TRANSPORT_TYPE_BLE:
290 dlog_print(DLOG_DEBUG, TAG, "transport_type (%d): ble", transport_type);
293 case SAP_TRANSPORT_TYPE_TCP:
294 dlog_print(DLOG_DEBUG, TAG, "transport_type (%d): tcp/ip", transport_type);
297 case SAP_TRANSPORT_TYPE_USB:
298 dlog_print(DLOG_DEBUG, TAG, "transport_type (%d): usb", transport_type);
301 case SAP_TRANSPORT_TYPE_MOBILE:
302 dlog_print(DLOG_DEBUG, TAG, "transport_type (%d): mobile", transport_type);
306 dlog_print(DLOG_ERROR, TAG, "unknown transport_type (%d)", transport_type);
311 case SAP_DEVICE_STATUS_DETACHED:
312 dlog_print(DLOG_DEBUG, TAG, "device is not connected.");
315 case SAP_DEVICE_STATUS_ATTACHED:
316 dlog_print(DLOG_DEBUG, TAG, "Attached calling find peer now");
320 dlog_print(DLOG_ERROR, TAG, "unknown status (%d)", status);
325 gboolean agent_initialize()
330 result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
331 on_agent_initialized, NULL);
333 dlog_print(DLOG_DEBUG, TAG, "SAP >>> getRegisteredServiceAgent() >>> %d", result);
334 } while (result != SAP_RESULT_SUCCESS);
339 gboolean initialize_sap(void)
341 sap_agent_h agent = NULL;
343 sap_agent_create(&agent);
345 priv_data.agent = agent;
349 sap_set_device_status_changed_cb(_on_device_status_changed, NULL);