Remove unused code
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / ft.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <glib.h>
19 #include <dlog.h>
20 #include <service_app.h>
21 #include <app_common.h>
22 #include <sap.h>
23 #include <sap_file_transfer.h>
24 #include <sticker_provider.h>
25 #include <string.h>
26 #include <string>
27 #include <unistd.h>
28 #include <linux/limits.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <pwd.h>
32
33 #include "log.h"
34 #include "sticker_info.h"
35
36 #define ACCESSORY_SERVICE_PROFILE_ID "/sample/filetransfersender"
37
38 using namespace std;
39
40 struct priv {
41     sap_agent_h agent;
42     sap_file_transaction_h socket;
43 };
44
45 static struct priv priv_data = { 0 };
46
47 gboolean file_on_progress = 0;
48 static string incoming_file_name;
49 static string recv_filepath;
50
51 static void _on_send_completed(sap_file_transaction_h file_transaction,
52                                sap_ft_transfer_e result,
53                                const char *file_path,
54                                void *user_data)
55 {
56     if (priv_data.socket) {
57         sap_file_transfer_destroy(file_transaction);
58         priv_data.socket = NULL;
59     }
60
61     if (result == SAP_FT_TRANSFER_SUCCESS) {
62         LOGI("Transfer Completed");
63
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));
66         }
67         else {
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();
72
73                 if (unlink(recv_filepath.c_str()) == -1)
74                     LOGE("Failed to remove sticker file");
75             }
76         }
77     } else {
78         switch (result) {
79             case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
80                 LOGW("Channel IO Error.");
81                 break;
82             }
83
84             case (SAP_FT_TRANSFER_FAIL_FILE_IO): {
85                 LOGW("File IO Error.");
86                 break;
87             }
88
89             case (SAP_FT_TRANSFER_FAIL_CMD_DROPPED):
90             {
91                 LOGW("Transfer dropped.");
92                 break;
93             }
94
95             case (SAP_FT_TRANSFER_FAIL_PEER_UNRESPONSIVE):
96             {
97                 LOGW("Peer Un Responsive.");
98                 break;
99             }
100
101             case (SAP_FT_TRANSFER_FAIL_PEER_CONN_LOST):
102             {
103                 LOGW("Connection Lost.");
104                 break;
105             }
106
107             case (SAP_FT_TRANSFER_FAIL_PEER_CANCELLED):
108             {
109                 LOGW("Peer Cancelled.");
110                 break;
111             }
112
113             case (SAP_FT_TRANSFER_FAIL_SPACE_NOT_AVAILABLE):
114             {
115                 LOGW("No Space.");
116                 break;
117             }
118
119             default:
120                 LOGW("Unknown Error");
121                 break;
122         }
123     }
124
125     file_on_progress = 0;
126 }
127
128 static void _on_sending_file_in_progress(sap_file_transaction_h file_transaction,
129                                          unsigned short int percentage_progress,
130                                          void *user_data)
131 {
132     LOGI("# progress %d", percentage_progress);
133 }
134
135 static void __set_file_transfer_cb(sap_file_transaction_h file_socket)
136 {
137     LOGI("# set callbacks");
138     sap_file_transfer_set_progress_cb(file_socket, _on_sending_file_in_progress, NULL);
139
140     sap_file_transfer_set_done_cb(file_socket, _on_send_completed, NULL);
141 }
142
143 void accept_file()
144 {
145     int ret;
146     char file_path[PATH_MAX];
147     char *data_path = NULL;
148
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);
154     free(data_path);
155
156     ret = sap_file_transfer_receive(priv_data.socket, file_path);
157     switch(ret) {
158         case SAP_RESULT_PERMISSION_DENIED:
159             LOGW("permission denied");
160             break;
161         case SAP_RESULT_FAILURE:
162             LOGW("Fail");
163             break;
164         case SAP_RESULT_SUCCESS:
165             LOGI("Success");
166             break;
167     }
168
169     file_on_progress = 1;
170 }
171
172 void sap_file_transfer_get_receive_filepath(char **filepath)
173 {
174     *filepath = strdup(recv_filepath.c_str());
175 }
176
177 void reject_file()
178 {
179     int ret = sap_file_transfer_reject(priv_data.socket);
180     LOGI("ret : %d", ret);
181
182     file_on_progress = 0;
183 }
184
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,
188                                 void *user_data)
189 {
190     file_on_progress = 1;
191     priv_data.socket = socket;
192     LOGI("# incoming file request.");
193     __set_file_transfer_cb(priv_data.socket);
194
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);
198
199     LOGI("# file path : %s, incoming file name : %s", file_path, incoming_file_name.c_str());
200
201     accept_file();
202 }
203
204 void conn_terminated(sap_peer_agent_h peer_agent,
205                      sap_socket_h socket,
206                      sap_service_connection_terminated_reason_e result,
207                      void *user_data)
208 {
209     LOGI("connection terminated");
210     service_app_exit();
211 }
212
213 void
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) */
216 {
217     LOGI("received data: %s, len:%d", (char *)buffer, payload_length);
218 }
219
220 static void on_conn_req(sap_peer_agent_h peer_agent,
221                         sap_socket_h socket,
222                         sap_service_connection_result_e result,
223                         void *user_data)
224 {
225     sap_peer_agent_accept_service_connection(peer_agent);
226     sap_peer_agent_set_service_connection_terminated_cb(peer_agent, conn_terminated, NULL);
227
228     sap_socket_set_data_received_cb(socket, on_data_received, peer_agent);
229 }
230
231 static void on_agent_initialized(sap_agent_h agent,
232                                  sap_agent_initialized_result_e result,
233                                  void *user_data)
234 {
235     switch (result) {
236         case SAP_AGENT_INITIALIZED_RESULT_SUCCESS:
237             LOGD("agent is initialized");
238
239             priv_data.agent = agent;
240
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);
243
244             break;
245         case SAP_AGENT_INITIALIZED_RESULT_DUPLICATED:
246             LOGE("duplicate registration");
247             break;
248         case SAP_AGENT_INITIALIZED_RESULT_INVALID_ARGUMENTS:
249             LOGE("invalid arguments");
250             break;
251         case SAP_AGENT_INITIALIZED_RESULT_INTERNAL_ERROR:
252             LOGE("internal sap error");
253             break;
254         default:
255             LOGE("unknown status (%d)", result);
256             break;
257     }
258 }
259
260 static void on_agent_deinitialized(sap_agent_h agent,
261                                    sap_agent_deinitialized_result_e result,
262                                    void *user_data)
263 {
264     LOGD("result of deinitialize : %d", result);
265 }
266
267 static void _on_device_status_changed(sap_device_status_e status,
268                                       sap_transport_type_e transport_type,
269                                       void *user_data)
270 {
271     LOGD("%s, status :%d", __func__, status);
272
273     switch (transport_type) {
274         case SAP_TRANSPORT_TYPE_BT:
275             LOGD("transport_type (%d): bt", transport_type);
276             break;
277         case SAP_TRANSPORT_TYPE_BLE:
278             LOGD("transport_type (%d): ble", transport_type);
279             break;
280         case SAP_TRANSPORT_TYPE_TCP:
281             LOGD("transport_type (%d): tcp/ip", transport_type);
282             break;
283         case SAP_TRANSPORT_TYPE_USB:
284             LOGD("transport_type (%d): usb", transport_type);
285             break;
286         case SAP_TRANSPORT_TYPE_MOBILE:
287             LOGD("transport_type (%d): mobile", transport_type);
288             break;
289         default:
290             LOGE("unknown transport_type (%d)", transport_type);
291             break;
292     }
293
294     switch (status) {
295         case SAP_DEVICE_STATUS_DETACHED:
296             LOGD("device is not connected.");
297             break;
298         case SAP_DEVICE_STATUS_ATTACHED:
299             LOGD("Attached calling find peer now");
300             break;
301         default:
302             LOGE("unknown status (%d)", status);
303             break;
304     }
305 }
306
307 gboolean agent_initialize()
308 {
309     int result = 0;
310
311     do {
312         result = sap_agent_initialize(priv_data.agent, ACCESSORY_SERVICE_PROFILE_ID, SAP_AGENT_ROLE_CONSUMER,
313                 on_agent_initialized, NULL);
314
315         LOGD("SAP >>> getRegisteredServiceAgent() >>> %d", result);
316     } while (result != SAP_RESULT_SUCCESS);
317
318     return TRUE;
319 }
320
321 gboolean initialize_sap(void)
322 {
323     sap_agent_h agent = NULL;
324
325     sap_agent_create(&agent);
326
327     priv_data.agent = agent;
328
329     agent_initialize();
330
331     sap_set_device_status_changed_cb(_on_device_status_changed, NULL);
332
333     return TRUE;
334 }
335
336 void deinitialize_sap(void)
337 {
338     if (priv_data.agent) {
339         int ret = sap_agent_deinitialize(priv_data.agent, on_agent_deinitialized, NULL);
340         switch (ret) {
341             case SAP_RESULT_FAILURE:
342                 LOGW("Failed to deinitialize");
343                 break;
344             case SAP_RESULT_SUCCESS:
345                 LOGD("Succeed to deinitialize");
346                 break;
347             case SAP_RESULT_PERMISSION_DENIED:
348                 LOGW("permission denied: deinitialize ");
349                 break;
350             default:
351                 LOGD("deinitialize : %d", ret);
352                 break;
353         }
354
355         sap_agent_destroy(priv_data.agent);
356         priv_data.agent = NULL;
357     }
358 }