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>
34 #include "sticker_info.h"
36 #define ACCESSORY_SERVICE_PROFILE_ID "/sample/filetransfersender"
42 sap_file_transaction_h socket;
45 static struct priv priv_data = { 0 };
47 gboolean file_on_progress = 0;
48 static string incoming_file_name;
49 static string recv_filepath;
51 static void _on_send_completed(sap_file_transaction_h file_transaction,
52 sap_ft_transfer_e result,
53 const char *file_path,
56 if (priv_data.socket) {
57 sap_file_transfer_destroy(file_transaction);
58 priv_data.socket = NULL;
61 if (result == SAP_FT_TRANSFER_SUCCESS) {
62 LOGI("Transfer Completed");
64 if (chmod(recv_filepath.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
65 LOGE("Failed to change permission : %s. error : %s", recv_filepath.c_str(), strerror(errno));
68 LOGI("Succeed to change permission : %s", recv_filepath.c_str());
69 if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
70 insert_sticker_data(recv_filepath.c_str(), "keyword", "group", "test icon");
71 destroy_sticker_provider_handle();
73 if (unlink(recv_filepath.c_str()) == -1)
74 LOGE("Failed to remove sticker file");
79 case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
80 LOGW("Channel IO Error.");
84 case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
85 LOGW("File IO Error.");
89 case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED):
91 LOGW("Transfer dropped.");
95 case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE):
97 LOGW("Peer Un Responsive.");
101 case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST):
103 LOGW("Connection Lost.");
107 case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED):
109 LOGW("Peer Cancelled.");
113 case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE):
120 LOGW("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 LOGI("# progress %d", percentage_progress);
135 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
137 LOGI("# 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);
146 char file_path[PATH_MAX];
147 char *data_path = NULL;
149 data_path = app_get_shared_data_path();
150 LOGI("Path : %s", data_path);
151 sprintf(file_path, "%s/%s", data_path, incoming_file_name.c_str());
152 LOGI("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 LOGW("permission denied");
161 case SAP_RESULT_FAILURE:
164 case SAP_RESULT_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 LOGI("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 LOGI("# 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 LOGI("# 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 LOGI("connection terminated");
214 on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
215 void *user_data) /* message exchange on_receive callback (sap_agent_data_received_cb) */
217 LOGI("received data: %s, len:%d", (char *)buffer, payload_length);
220 static void on_conn_req(sap_peer_agent_h peer_agent,
222 sap_service_connection_result_e result,
225 sap_peer_agent_accept_service_connection(peer_agent);
226 sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, NULL);
228 sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
231 static void on_agent_initialized(sap_agent_h agent,
232 sap_agent_initialized_result_e result,
236 case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
237 LOGD("agent is initialized");
239 priv_data.agent = agent;
241 sap_file_transfer_set_incoming_file_cb(agent, _on_receive_file_cb, NULL);
242 sap_agent_set_service_connection_requested_cb(agent, on_conn_req, NULL);
245 case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
246 LOGE("duplicate registration");
248 case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
249 LOGE("invalid arguments");
251 case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
252 LOGE("internal sap error");
255 LOGE("unknown status (%d)", result);
260 static void on_agent_deinitialized(sap_agent_h agent,
261 sap_agent_deinitialized_result_e result,
264 LOGD("result of deinitialize : %d", result);
267 static void _on_device_status_changed(sap_device_status_e status,
268 sap_transport_type_e transport_type,
271 LOGD("%s, status :%d", __func__, status);
273 switch (transport_type) {
274 case SAP_TRANSPORT_TYPE_BT:
275 LOGD("transport_type (%d): bt", transport_type);
277 case SAP_TRANSPORT_TYPE_BLE:
278 LOGD("transport_type (%d): ble", transport_type);
280 case SAP_TRANSPORT_TYPE_TCP:
281 LOGD("transport_type (%d): tcp/ip", transport_type);
283 case SAP_TRANSPORT_TYPE_USB:
284 LOGD("transport_type (%d): usb", transport_type);
286 case SAP_TRANSPORT_TYPE_MOBILE:
287 LOGD("transport_type (%d): mobile", transport_type);
290 LOGE("unknown transport_type (%d)", transport_type);
295 case SAP_DEVICE_STATUS_DETACHED:
296 LOGD("device is not connected.");
298 case SAP_DEVICE_STATUS_ATTACHED:
299 LOGD("Attached calling find peer now");
302 LOGE("unknown status (%d)", status);
307 gboolean agent_initialize()
312 result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
313 on_agent_initialized, NULL);
315 LOGD("SAP >>> getRegisteredServiceAgent() >>> %d", result);
316 } while (result != SAP_RESULT_SUCCESS);
321 gboolean initialize_sap(void)
323 sap_agent_h agent = NULL;
325 sap_agent_create(&agent);
327 priv_data.agent = agent;
331 sap_set_device_status_changed_cb(_on_device_status_changed, NULL);
336 void deinitialize_sap(void)
338 if (priv_data.agent) {
339 int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
341 case SAP_RESULT_FAILURE:
342 LOGW("Failed to deinitialize");
344 case SAP_RESULT_SUCCESS:
345 LOGD("Succeed to deinitialize");
347 case SAP_RESULT_PERMISSION_DENIED:
348 LOGW("permission denied: deinitialize ");
351 LOGD("deinitialize : %d", ret);
355 sap_agent_destroy(priv_data.agent);
356 priv_data.agent = NULL;