Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / network-access / interface.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <glib-object.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include "utility/sync_util.h"
23 #include "network-access/interface.h"
24 #include "network-access/interface_internal.h"
25 #include "network-access/external.h"
26 #include "plugin/network_access_plugin.h"
27 #include "fsapi/operation.h"
28
29 #include "utility/fw_mainloop.h"
30 #include "utility/fw_mainloop_internal.h"
31
32 #include "network-access/network_status.h"
33
34 #include "network-access/session_manager.h"
35
36 #define NA_RETRY_CNT            3
37
38 #ifndef EXPORT_API
39 #define EXPORT_API __attribute__ ((visibility("default")))
40 #endif
41
42 #ifndef SYNC_AGENT_LOG
43 #undef LOG_TAG
44 #define LOG_TAG "AF_NACI"
45 #endif
46
47 sync_agent_na_result_e na_init_network_access(int na_plugin_id)
48 {
49         _EXTERN_FUNC_ENTER;
50
51         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
52
53         na_init_session_manager();
54         plugin_register_fw_mainloop_na_cb func_point_register_fw_mainloop_na = plugin_get_function_register_fw_mainloop_na(na_plugin_id);
55         if (func_point_register_fw_mainloop_na == NULL) {
56                 _DEBUG_ERROR("cannot get pFunc !!");
57                 res = SYNC_AGENT_NA_INIT_FAIL;
58         } else {
59                 util_register_callback_to_main_loop(func_point_register_fw_mainloop_na, NULL);
60         }
61
62         _EXTERN_FUNC_EXIT;
63
64         return res;
65 }
66
67 EXPORT_API sync_agent_na_result_e sync_agent_open_connection(int na_plugin_id, int timeout, unsigned int *session_id)
68 {
69         _EXTERN_FUNC_ENTER;
70
71         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
72         void *session = NULL;
73
74         int err = na_create_session_info(&session);
75         if (err != 1) {
76                 _DEBUG_ERROR("na_create_session_info() fail !!");
77                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
78         } else {
79                 _DEBUG_INFO("na_create_session_info() success !!");
80         }
81
82         err = na_provide_session_id(session_id);
83         if (err != 1) {
84                 _DEBUG_ERROR("na_provide_session_id() fail !!");
85                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
86         } else {
87                 _DEBUG_INFO("na_provide_session_id() success !!");
88                 _DEBUG_INFO("session id : %d", *session_id);
89         }
90
91         err = na_set_timeout(*session_id, timeout);
92         if (err != 1) {
93                 _DEBUG_ERROR("na_set_timeout() fail !!");
94                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
95         } else {
96                 _DEBUG_INFO("na_set_timeout() success !!");
97         }
98
99         if (plugin_use_network(na_plugin_id) == 1) {
100                 /* use http plug-in */
101                 switch (na_get_network_status()) {
102                 case SYNC_AGENT_NA_NETWORK_UNKNOWN:
103                 case SYNC_AGENT_NA_NETWORK_OFF:
104                         {
105                                 _DEBUG_ERROR("network OFF !!");
106                                 err = na_remove_session_info(*session_id);
107                                 if (err != 1) {
108                                         _DEBUG_ERROR("na_remove_session_info() fail !!");
109                                 } else {
110                                         _DEBUG_INFO("na_remove_session_info() success !!");
111                                 }
112                                 return SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
113                         }
114                         break;
115                 case SYNC_AGENT_NA_NETWORK_ON:
116                         {
117                                 _DEBUG_INFO("network ON !!");
118                                 /*FW_NETWORK_TYPE fw_net_type = get_network_type(); */
119                                 sync_agent_na_network_connection_state_e conn_state = na_get_connection_state();
120                                 /*if ((fw_net_type == NETWORK_NONE) || (fw_net_type == NETWORK_3G_OFF) || (fw_net_type == NETWORK_WIFI_OFF) || (fw_net_type == NETWORK_WIFI_ON_NOT_CONNECTED)) { */
121                                 if ((SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_NONE <= conn_state && conn_state <= SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_WIFI_DISCONNECTED) ||
122                                     (conn_state == SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_CELLULAR_OUT_OF_SERVICE) || (conn_state == SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_CELLULAR_CALL_ONLY_AVAILABLE) ||
123                                     (SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_ETHERNET_DEACTIVATED <= conn_state && conn_state <= SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_ETHERNET_DISCONNECTED)) {
124                                         _DEBUG_ERROR("network ON but  not connected !!");
125                                         err = na_remove_session_info(*session_id);
126                                         if (err != 1) {
127                                                 _DEBUG_ERROR("na_remove_session_info() fail !!");
128                                         } else {
129                                                 _DEBUG_INFO("na_remove_session_info() success !!");
130                                         }
131                                         return SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
132                                 }
133                         }
134                         break;
135                 default:
136                         break;
137                 }
138         }
139
140         plugin_open_connection_cb func_point_open_connection = plugin_get_function_connection(na_plugin_id);
141
142         if (func_point_open_connection == NULL) {
143                 _DEBUG_ERROR("cannot get func_point_open_connection !!");
144                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
145         }
146
147         err = func_point_open_connection(&session, na_get_proxy(), timeout);
148         if (err != 1) {
149                 _DEBUG_ERROR("Open_Connection() fail !!");
150                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
151         } else {
152                 _DEBUG_INFO("Open_Connection() success !!");
153         }
154
155         err = na_set_session(*session_id, session);
156         if (err != 1) {
157                 _DEBUG_ERROR("na_set_session() fail !!");
158                 return SYNC_AGENT_NA_OPEN_CONNECTION_FAIL;
159         } else {
160                 _DEBUG_INFO("na_set_session() success !!");
161         }
162
163         _EXTERN_FUNC_EXIT;
164
165         return res;
166 }
167
168 EXPORT_API sync_agent_na_result_e sync_agent_close_connection(int na_plugin_id, unsigned int session_id)
169 {
170         _EXTERN_FUNC_ENTER;
171
172         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
173         void *session = NULL;
174
175         _DEBUG_INFO("session id : %d", session_id);
176         int err = na_get_session(session_id, &session);
177         if (err != 1) {
178                 _DEBUG_INFO("get session() fail !!");
179                 return SYNC_AGENT_NA_CLOSE_CONNECTION_FAIL;
180         } else {
181                 _DEBUG_INFO("get session() success !!");
182         }
183
184         plugin_close_connection_cb func_point_close_connection = plugin_get_function_close_connection(na_plugin_id);
185
186         if (func_point_close_connection == NULL) {
187                 _DEBUG_ERROR("cannot get func_point_close_connection !!");
188                 return SYNC_AGENT_NA_CLOSE_CONNECTION_FAIL;
189         }
190
191         err = func_point_close_connection(session);
192         if (err != 1) {
193                 _DEBUG_ERROR("Close_Connection() fail !!");
194                 return SYNC_AGENT_NA_CLOSE_CONNECTION_FAIL;
195         } else {
196                 _DEBUG_INFO("Close_Connection() success !!");
197         }
198
199         err = na_remove_session_info(session_id);
200         if (err != 1) {
201                 _DEBUG_ERROR("na_remove_session_info() fail !!");
202                 return SYNC_AGENT_NA_CLOSE_CONNECTION_FAIL;
203         } else {
204                 _DEBUG_INFO("na_remove_session_info() success !!");
205         }
206
207         _EXTERN_FUNC_EXIT;
208
209         return res;
210 }
211
212 EXPORT_API sync_agent_na_result_e sync_agent_send_msg(GList * header_info, int na_plugin_id, char *send_msg, unsigned int send_msg_length, GList ** recv_header, unsigned char **recv_msg, unsigned int *recv_msg_length, sync_agent_na_send_type_e send_type,
213                                                       unsigned int session_id)
214 {
215         _EXTERN_FUNC_ENTER;
216
217         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
218         void *session = NULL;
219         void *msg = NULL;
220         int try_again_cnt = 0;
221
222         /* check current network status */
223         if (plugin_use_network(na_plugin_id) == 1) {
224                 res = sync_agent_check_network_status(0, 1);
225                 if (res == SYNC_AGENT_NA_NETWORK_UNAVAILABLE) {
226                         _DEBUG_INFO("network is unavailable !!");
227 //                      int err = na_remove_session_info(session_id, 0);
228 //                      if (err != 1) {
229 //                              _DEBUG_ERROR("na_remove_session_info() fail !!");
230 //                      } else {
231 //                              _DEBUG_INFO("na_remove_session_info() success !!");
232 //                      }
233                         return res;
234                 } else if (res == SYNC_AGENT_NA_NETWORK_CHANGING) {
235                         _DEBUG_INFO("network type is changing !!");
236                         _DEBUG_INFO("retry sync_agent_check_network_status() after 3 seconds !!");
237                         sleep(3);
238                         /* retry checking network status */
239                         res = sync_agent_check_network_status(0, 1);
240                         if (res == SYNC_AGENT_NA_NETWORK_UNAVAILABLE) {
241                                 _DEBUG_INFO("network is unavailable !!");
242                                 return res;
243                         } else {
244                                 _DEBUG_INFO("network is available !!");
245                         }
246                 } else {
247                         _DEBUG_INFO("network is available !!");
248                 }
249         }
250
251         /*
252          *      Header Binding
253          */
254         plugin_header_binding_cb func_point_header_binding = plugin_get_function_header_binding(na_plugin_id);
255
256         if (func_point_header_binding == NULL) {
257                 _DEBUG_ERROR("cannot get func_point_header_binding !!");
258                 return SYNC_AGENT_NA_HEADER_BINDING_FAIL;
259         }
260
261         int err = na_get_msg(session_id, &msg);
262         if (err != 1) {
263                 _DEBUG_ERROR("na_get_msg() fail !!");
264                 return SYNC_AGENT_NA_HEADER_BINDING_FAIL;
265         } else {
266                 _DEBUG_INFO("na_get_msg() success !!");
267         }
268
269         unsigned int recv_msg_size = 0;
270
271         err = func_point_header_binding(header_info, &msg);
272         if (err != 1) {
273                 _DEBUG_ERROR("Header_Binding() fail !!");
274                 return SYNC_AGENT_NA_HEADER_BINDING_FAIL;
275         } else {
276                 _DEBUG_INFO("Header_Binding() success !!");
277         }
278
279         /*
280          *      Send Message
281          */
282         err = na_get_session(session_id, &session);
283         if (err != 1) {
284                 _DEBUG_ERROR("na_get_session() fail !!");
285                 return SYNC_AGENT_NA_SEND_MSG_FAIL;
286         } else {
287                 _DEBUG_INFO("na_get_session() success !!");
288         }
289
290         if (send_type == SYNC_AGENT_NA_SEND_TYPE_SEND_N_RECEIVE) {
291                 plugin_send_message_cb func_point_send_message = plugin_get_function_send_message(na_plugin_id);
292
293                 if (func_point_send_message == NULL) {
294                         _DEBUG_ERROR("cannot get func_point_send_message !!");
295                         return SYNC_AGENT_NA_SEND_MSG_FAIL;
296                 }
297
298  send_message:
299                 err = func_point_send_message(session, &msg, send_msg, send_msg_length, &recv_msg_size);
300                 if (err != 1) {
301                         if (err == -408) {      /* FIXME : request time-out (temporary) */
302                                 _DEBUG_ERROR("request time-out !!");
303                                 return SYNC_AGENT_NA_TIME_OUT_SEND_MSG;
304                         } else if (err == -1) { /* FIXME : cancel message (temporary) */
305                                 _DEBUG_INFO("send message canceled !!");
306                                 return SYNC_AGENT_NA_SEND_MSG_CANCEL;
307                         } else if (err == -8) { /* try again - retry 3 times */
308                                 _DEBUG_ERROR("send message try again ( %d ) !!", try_again_cnt);
309
310                                 if (try_again_cnt < NA_RETRY_CNT) {
311                                         _DEBUG_INFO("re-make session !!");
312
313                                         try_again_cnt++;
314
315                                         session = NULL;
316
317 #if 0
318                                         plugin_open_connection_cb func_point_open_connection = plugin_get_function_connection(na_plugin_id);
319                                         if (func_point_open_connection == NULL) {
320                                                 _DEBUG_ERROR("cannot get func_point_open_connection !!");
321                                                 return SYNC_AGENT_NA_SEND_MSG_TRY_AGAIN;
322                                         }
323
324                                         int timeout = 0;
325                                         int er = na_get_timeout(session_id, &timeout);
326                                         if (er != 1) {
327                                                 _DEBUG_ERROR("na_get_timeout() fail !!");
328                                                 return SYNC_AGENT_NA_SEND_MSG_TRY_AGAIN;
329                                         }
330
331                                         err = func_point_open_connection(&session, na_get_proxy(), timeout);
332                                         if (err != 1) {
333                                                 _DEBUG_ERROR("Open_Connection() fail !!");
334                                                 return SYNC_AGENT_NA_SEND_MSG_TRY_AGAIN;
335                                         } else {
336                                                 _DEBUG_INFO("Open_Connection() success !!");
337                                         }
338
339                                         err = na_set_session(session_id, session);
340                                         if (err != 1) {
341                                                 _DEBUG_ERROR("na_set_session() fail !!");
342                                                 return SYNC_AGENT_NA_SEND_MSG_TRY_AGAIN;
343                                         } else {
344                                                 _DEBUG_INFO("na_set_session() success !!");
345                                         }
346 #endif
347                                         goto send_message;
348                                 } else {
349                                         _DEBUG_ERROR("try again count over 3 times !!");
350                                         _DEBUG_ERROR("Send_Messasge() fail !!");
351
352                                         return SYNC_AGENT_NA_SEND_MSG_TRY_AGAIN;
353                                 }
354                         } else if (err == -10) {        /* untrusted certificate */
355                                 _DEBUG_INFO("untrusted certificate !!");
356                                 return SYNC_AGENT_NA_UNTRUSTED_CERTIFICATE;
357                         } else {
358                                 _DEBUG_ERROR("Send_Message() fail !! : %d", err);
359                                 return SYNC_AGENT_NA_SEND_MSG_FAIL;
360                         }
361                 } else {
362                         _DEBUG_INFO("Send_Message success !!");
363                 }
364         } else if (send_type == SYNC_AGENT_NA_SEND_TYPE_JUST_SEND) {
365                 plugin_just_send_message_cb func_point_just_send_message = plugin_get_function_just_send_message(na_plugin_id);
366
367                 if (func_point_just_send_message == NULL) {
368                         _DEBUG_ERROR("cannot get func_point_just_send_message !!");
369                         return SYNC_AGENT_NA_JUST_SEND_MSG_FAIL;
370                 }
371
372                 err = func_point_just_send_message(session, &msg, send_msg, send_msg_length);
373                 if (err != 1) {
374                         _DEBUG_ERROR("Just_Send_Message() fail !!");
375                         return SYNC_AGENT_NA_JUST_SEND_MSG_FAIL;
376                 } else {
377                         _DEBUG_INFO("Just_Send_Message() success !!");
378                         _EXTERN_FUNC_EXIT;
379                         return res;
380                 }
381         }
382
383         /*
384          *      Header Unbinding
385          */
386         plugin_header_unbinding_cb func_point_header_unbinding = plugin_get_function_header_unbinding(na_plugin_id);
387
388         if (func_point_header_unbinding == NULL) {
389                 _DEBUG_ERROR("cannot get func_point_header_unbinding !!");
390                 return SYNC_AGENT_NA_HEADER_UNBINDING_FAIL;
391         }
392
393         /*
394          *      extract header/body information using message from server
395          */
396         err = func_point_header_unbinding(msg, recv_msg_size, recv_header, recv_msg, recv_msg_length);
397         if (err != 1) {
398                 _DEBUG_ERROR("Header_Unbinding() fail !!");
399                 return SYNC_AGENT_NA_HEADER_UNBINDING_FAIL;
400         } else {
401                 _DEBUG_INFO("Header_Unbinding() success !!");
402         }
403
404         _EXTERN_FUNC_EXIT;
405
406         return res;
407 }
408
409 EXPORT_API sync_agent_na_result_e sync_agent_cancel_msg(int na_plugin_id, unsigned int session_id)
410 {
411         _EXTERN_FUNC_ENTER;
412
413         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
414         void *session = NULL;
415
416         plugin_cancel_message_cb func_point_cancel_message = plugin_get_function_cancel_message(na_plugin_id);
417
418         if (func_point_cancel_message == NULL) {
419                 _DEBUG_ERROR("cannot get func_point_cancel_message !!");
420                 _EXTERN_FUNC_EXIT;
421                 return SYNC_AGENT_NA_FAIL;
422         }
423
424         int err = na_get_session(session_id, &session);
425         if (err != 1) {
426                 _DEBUG_ERROR("na_get_session() fail !!");
427                 return SYNC_AGENT_NA_CANCEL_MSG_FAIL;
428         } else {
429                 _DEBUG_INFO("na_get_session() success !!");
430         }
431
432         _DEBUG_INFO("session id : %d", session_id);
433         err = func_point_cancel_message(session);
434         if (err != 1) {
435                 _DEBUG_ERROR("Cancel_Message() fail !!");
436                 return SYNC_AGENT_NA_CANCEL_MSG_FAIL;
437         } else {
438                 _DEBUG_INFO("Cancel_Message() success !!");
439         }
440
441         _EXTERN_FUNC_EXIT;
442
443         return res;
444 }
445
446 EXPORT_API sync_agent_na_result_e sync_agent_destroy_netowk_access()
447 {
448         _EXTERN_FUNC_ENTER;
449
450         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
451
452         na_destroy_session_manager();
453
454         _EXTERN_FUNC_EXIT;
455
456         return res;
457 }
458
459 EXPORT_API sync_agent_na_result_e sync_agent_get_header_info(int na_plugin_id, GList * header_info, char *key, char **value)
460 {
461         _EXTERN_FUNC_ENTER;
462
463         retvm_if(header_info == NULL, SYNC_AGENT_NA_FAIL, "GList is NULL !!");
464
465         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
466
467         plugin_get_header_info_cb func_point_get_header_info = plugin_get_function_get_header_info(na_plugin_id);
468
469         if (func_point_get_header_info == NULL) {
470                 _DEBUG_ERROR("cannot get func_point_get_header_info !!");
471                 _EXTERN_FUNC_EXIT;
472                 return SYNC_AGENT_NA_FAIL;
473         }
474
475         int err = func_point_get_header_info(header_info, key, value);
476         if (err != 1) {
477                 _DEBUG_ERROR("Get_Header_Info() fail !!");
478                 return SYNC_AGENT_NA_GET_HEADER_INFO_FAIL;
479         } else {
480                 _DEBUG_INFO("Get_Header_Info() success !!");
481         }
482
483         _EXTERN_FUNC_EXIT;
484
485         return res;
486 }
487
488 sync_agent_na_result_e na_free_recv_header_info(GList * header_info)
489 {
490         _EXTERN_FUNC_ENTER;
491
492         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
493
494         if (header_info != NULL) {
495                 GList *iter = NULL;
496                 sync_agent_na_common_header_info_s *iter_data;
497
498                 for (iter = header_info; iter != NULL;) {
499                         iter_data = NULL;
500                         iter_data = ((sync_agent_na_common_header_info_s *) (iter->data));
501
502                         iter = g_list_next(iter);
503                         header_info = g_list_remove(header_info, iter_data);
504
505                         if (iter_data->key != NULL)
506                                 free(iter_data->key);
507                         if (iter_data->value != NULL)
508                                 free(iter_data->value);
509                         free(iter_data);
510                 }
511
512                 g_list_free(header_info);
513
514                 _DEBUG_INFO("header_info is free !!");
515         } else {
516                 _DEBUG_INFO("header_info is NULL !!");
517         }
518
519         _EXTERN_FUNC_EXIT;
520
521         return res;
522 }
523
524 EXPORT_API sync_agent_na_result_e sync_agent_check_network_status(int interval, int retry_cnt)
525 {
526         _EXTERN_FUNC_ENTER;
527
528         retvm_if(interval < 0, SYNC_AGENT_NA_FAIL, "interval paramter is 0 !!!");
529
530         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
531         int i = 0;
532
533         for (; i < retry_cnt; i++) {
534                 switch (na_get_network_status()) {
535                 case SYNC_AGENT_NA_NETWORK_UNKNOWN:
536                 case SYNC_AGENT_NA_NETWORK_OFF:
537                         {
538                                 _DEBUG_ERROR("network OFF !!");
539                                 res = SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
540                         }
541                         break;
542                 case SYNC_AGENT_NA_NETWORK_ON:
543                         {
544                                 _DEBUG_INFO("[%s] network ON \n", __func__);
545                                 /*FW_NETWORK_TYPE fw_net_type = get_network_type(); */
546                                 sync_agent_na_network_connection_state_e conn_state = na_get_connection_state();
547                                 /*if ((fw_net_type == NETWORK_NONE) || (fw_net_type == NETWORK_3G_OFF) || (fw_net_type == NETWORK_WIFI_OFF) || (fw_net_type == NETWORK_WIFI_ON_NOT_CONNECTED)) { */
548                                 if ((SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_NONE <= conn_state && conn_state <= SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_WIFI_DISCONNECTED) ||
549                                     (conn_state == SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_CELLULAR_OUT_OF_SERVICE) || (conn_state == SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_CELLULAR_CALL_ONLY_AVAILABLE) ||
550                                     (SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_ETHERNET_DEACTIVATED <= conn_state && conn_state <= SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_ETHERNET_DISCONNECTED)) {
551                                         _DEBUG_ERROR("network ON but  not connected !!");
552                                         res = SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
553                                 } else {
554                                         res = SYNC_AGENT_NA_SUCCESS;
555                                         _EXTERN_FUNC_EXIT;
556                                         return res;
557                                 }
558                         }
559                         break;
560                 case SYNC_AGENT_NA_NETWORK_CHANGE:
561                         {
562                                 _DEBUG_INFO("[%s] network CHANGE \n", __func__);
563                                 res = SYNC_AGENT_NA_NETWORK_CHANGING;
564                                 _EXTERN_FUNC_EXIT;
565                                 return res;
566                         }
567                         break;
568                 default:
569                         break;
570                 }
571                 sleep(interval);
572         }
573
574         _EXTERN_FUNC_EXIT;
575
576         return res;
577 }
578
579 EXPORT_API sync_agent_na_result_e sync_agent_get_connection_type(int *conn_type)
580 {
581         _EXTERN_FUNC_ENTER;
582
583         retvm_if(conn_type == NULL, SYNC_AGENT_NA_FAIL, "conn_type paramter is NULL !!!");
584
585         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
586
587         *conn_type = na_get_connection_type();
588         _DEBUG_INFO("current connection type : %d", *conn_type);
589
590         _EXTERN_FUNC_EXIT;
591
592         return res;
593 }
594
595 EXPORT_API sync_agent_na_result_e sync_agent_get_connection_state(int *conn_state)
596 {
597         _EXTERN_FUNC_ENTER;
598
599         retvm_if(conn_state == NULL, SYNC_AGENT_NA_FAIL, "conn_state paramter is NULL !!!");
600
601         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
602
603         *conn_state = na_get_connection_state();
604         _DEBUG_INFO("current connection state : %d", *conn_state);
605
606         _EXTERN_FUNC_EXIT;
607
608         return res;
609 }
610
611 sync_agent_na_result_e na_download_data(GList * header_info, int na_plugin_id, unsigned char *download_folder, unsigned int download_start_size, unsigned int download_body_size, unsigned char **out_download_path, sync_agent_na_send_type_e send_type,
612                                         unsigned int session_id)
613 {
614         _EXTERN_FUNC_ENTER;
615
616         retvm_if(header_info == NULL, SYNC_AGENT_NA_FAIL, "GList is NULL !!");
617         retvm_if(download_folder == NULL, SYNC_AGENT_NA_FAIL, "download folder path is NULL !!");
618
619         GList *recv_header_info = 0;
620         /* char *recvMsg = 0; */
621         unsigned char *recvMsg = NULL;
622         unsigned int recvMsg_size = 0;
623
624         unsigned int download_current_size = download_start_size;
625         /* unsigned int download_total_size = 0; */
626         int download_total_size = 0;
627         char *current_download_range = NULL;
628         char *download_file_name = NULL;
629
630         GList *backup_send_head_info_list = NULL;
631         GList *iter = NULL;
632
633         int count = 0;          /* for test log */
634         sync_agent_na_result_e ret = SYNC_AGENT_NA_SUCCESS;
635         while (download_total_size == 0 || download_current_size < download_total_size - 1) {
636
637                 _DEBUG_INFO("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
638                 _DEBUG_INFO("############# %d th download", ++count);
639                 _DEBUG_INFO("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
640
641                 /* back up the header list TODO */
642                 /* GSList *backup_send_head_info_list = 0; */
643                 backup_send_head_info_list = NULL;
644                 iter = NULL;
645                 for (iter = header_info; iter != NULL; iter = g_list_next(iter)) {
646                         sync_agent_na_common_header_info_s *header_item_info = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
647                         if (header_item_info == NULL) {
648                                 _DEBUG_ERROR("calloc failed !!");
649                                 ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
650                                 goto return_part;
651                         }
652
653                         if (((sync_agent_na_common_header_info_s *) (iter->data))->key != NULL) {
654                                 header_item_info->key = strdup(((sync_agent_na_common_header_info_s *) (iter->data))->key);
655
656                                 if (((sync_agent_na_common_header_info_s *) (iter->data))->value != NULL) {
657                                         header_item_info->value = strdup(((sync_agent_na_common_header_info_s *) (iter->data))->value);
658                                 } else {
659                                         _DEBUG_ERROR("key : %s, value is NULL !!", header_item_info->key);
660                                         header_item_info->value = NULL;
661                                 }
662
663                                 backup_send_head_info_list = g_list_append(backup_send_head_info_list, header_item_info);
664                         } else {
665                                 _DEBUG_ERROR("key is NULL !!");
666                                 header_item_info->key = NULL;
667                                 header_item_info->value = NULL;
668                         }
669
670                         /*backup_send_head_info_list = g_list_append(backup_send_head_info_list, header_item_info); */
671                         if (header_item_info->key != NULL) {
672                                 if (strcmp(header_item_info->key, "Range") == 0)
673                                         _DEBUG_INFO("[Before] key : %s, value : %s", header_item_info->key, header_item_info->value);
674                         }
675                 }
676
677                 /* send_msg */
678                 ret = sync_agent_send_msg(backup_send_head_info_list, na_plugin_id, 0, 0, &recv_header_info, &recvMsg, &recvMsg_size, send_type, session_id);
679                 if (ret != SYNC_AGENT_NA_SUCCESS) {
680                         _DEBUG_ERROR("sync_agent_send_msg() fail");
681                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
682                         goto return_part;
683                 }
684
685                 _DEBUG_INFO("RecvMSg_Size = %d", recvMsg_size);
686                 _DEBUG_INFO("############# sync_agent_send_msg() success !!");
687
688                 /* get download total size, download current range */
689                 plugin_get_data_download_info_cb func_point_get_data_down_info = plugin_get_function_get_data_download_info(na_plugin_id);
690                 if (func_point_get_data_down_info == NULL) {
691                         _DEBUG_ERROR("cannot get func_point_get_data_down_info !!");
692                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
693                         goto return_part;
694                 }
695
696                 ret = func_point_get_data_down_info(header_info, recv_header_info, &download_total_size, &current_download_range, &download_file_name);
697                 if (ret != SYNC_AGENT_NA_SUCCESS) {
698                         _DEBUG_ERROR("Get_Data_Down_Info() fail !!");
699                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
700                         goto return_part;
701                 }
702
703                 _DEBUG_INFO("############# Get_Data_Down_Info() success !!");
704                 if (current_download_range == NULL || download_total_size == 0 || download_file_name == NULL) {
705                         _DEBUG_ERROR("current_download_range == NULL || download_total_size == 0 || download_file_name == NULL");
706                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
707                         goto return_part;
708                 }
709
710                 _DEBUG_INFO("download_total_size = %d", download_total_size);
711                 _DEBUG_INFO("current_download_range = %s", current_download_range);
712                 _DEBUG_INFO("download_file_name = %s", download_file_name);
713
714                 /* download path setting */
715                 if (*out_download_path == NULL) {
716                         *out_download_path = (unsigned char *)g_strdup_printf("%s/%s", download_folder, download_file_name);
717                         _DEBUG_INFO("*out_download_path = %s", *out_download_path);
718                 }
719
720                 /* increment download count */
721                 char *del = "-";
722                 /* int down_size = 0; */
723                 char *ptr = strtok(current_download_range, del);
724                 if (ptr != NULL) {
725
726                         int start_size = 0;
727                         int end_size = 0;
728
729                         _DEBUG_INFO("start_size = %s", ptr);
730                         start_size = atoi(ptr);
731
732                         ptr = strtok(NULL, del);
733                         _DEBUG_INFO("end_size = %s", ptr);
734                         end_size = atoi(ptr);
735
736                         _DEBUG_INFO("prev download_current_size = %d", download_current_size);
737                         _DEBUG_INFO("Gap = %d", end_size - start_size);
738
739                         download_current_size += (end_size - start_size + 1);
740                         _DEBUG_INFO("after download_current_size = %d", download_current_size);
741                 }
742
743                 bool isFinal = false;
744                 if (download_current_size >= download_total_size - 1)
745                         isFinal = true;
746
747                 /* set file */
748                 _DEBUG_INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RecvMSg_Size = %d", recvMsg_size);
749                 ret = sync_agent_append_file((char *)(*out_download_path), (char *)recvMsg, recvMsg_size, false);
750                 if (ret != SYNC_AGENT_NA_SUCCESS) {
751                         _DEBUG_ERROR("sync_agent_write_file() fail !!");
752                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
753                         goto return_part;
754                 } else {
755                         _DEBUG_INFO("############# sync_agent_write_file() success !!");
756                 }
757
758                 /* set download total size, download current range */
759                 plugin_set_data_download_info_cb func_point_set_data_down_info = plugin_get_function_set_data_download_info(na_plugin_id);
760                 if (func_point_set_data_down_info == NULL) {
761                         _DEBUG_ERROR("cannot get func_point_set_data_down_info !!");
762                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
763                         goto return_part;
764                 }
765
766                 char *new_download_range = g_strdup_printf("%d-%d", download_current_size, download_current_size + (download_body_size - 1));
767                 ret = func_point_set_data_down_info(header_info, new_download_range);
768                 if (ret != SYNC_AGENT_NA_SUCCESS) {
769                         _DEBUG_ERROR("Set_Data_Down_Info() fail !!");
770                         ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
771                         goto return_part;
772                 }
773
774                 _DEBUG_INFO("############# Set_Data_Down_Info() success !!");
775
776         }
777
778  return_part:
779
780         if (backup_send_head_info_list != NULL) {
781                 sync_agent_na_result_e result = na_free_recv_header_info(backup_send_head_info_list);
782                 if (result != SYNC_AGENT_NA_SUCCESS)
783                         _DEBUG_ERROR("na_free_recv_header_info() fail !!");
784         }
785
786         _EXTERN_FUNC_EXIT;
787
788         return ret;
789 }
790
791 EXPORT_API sync_agent_na_result_e sync_agent_add_authentication_info(int na_plugin_id, char *id, char *password, unsigned int session_id)
792 {
793         _EXTERN_FUNC_ENTER;
794
795         retvm_if(id == NULL, SYNC_AGENT_NA_FAIL, "id is NULL !!");
796         retvm_if(password == NULL, SYNC_AGENT_NA_FAIL, "password is NULL !!");
797
798         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
799         void *session = NULL;
800
801         plugin_add_authentication_info_cb func_point_add_authentication_info = plugin_get_function_add_authentication_info(na_plugin_id);
802
803         if (func_point_add_authentication_info == NULL) {
804                 _DEBUG_ERROR("cannot get func_point_add_authentication_info !!");
805                 return SYNC_AGENT_NA_ADD_AUTH_INFO_FAIL;
806         }
807
808         int err = na_get_session(session_id, &session);
809         if (err != 1) {
810                 _DEBUG_ERROR("na_get_session() fail !!");
811                 return SYNC_AGENT_NA_ADD_AUTH_INFO_FAIL;
812         } else {
813                 _DEBUG_INFO("na_get_session() success !!");
814         }
815
816         _DEBUG_INFO("session id : %d", session_id);
817
818         err = func_point_add_authentication_info(session, id, password);
819         if (err != 1) {
820                 _DEBUG_ERROR("Add_Authentication_Info() fail !!");
821                 return SYNC_AGENT_NA_ADD_AUTH_INFO_FAIL;
822         } else {
823                 _DEBUG_INFO("Add_Authentication_Info() success !!");
824         }
825
826         _EXTERN_FUNC_EXIT;
827
828         return res;
829 }
830
831 sync_agent_na_result_e na_set_property(int na_plugin_id, unsigned int session_id, sync_agent_na_property_e property, ...)
832 {
833         _EXTERN_FUNC_ENTER;
834
835         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
836         void *session = NULL;
837         va_list va;
838
839         plugin_set_property_cb func_point_set_property = plugin_get_function_set_property(na_plugin_id);
840
841         if (func_point_set_property == NULL) {
842                 _DEBUG_ERROR("cannot get SetProperty !!");
843                 return SYNC_AGENT_NA_FAIL;
844         }
845
846         int err = na_get_session(session_id, &session);
847         if (err != 1) {
848                 _DEBUG_ERROR("na_get_session() fail !!");
849                 return SYNC_AGENT_NA_FAIL;
850         } else {
851                 _DEBUG_INFO("na_get_session() success !!");
852         }
853
854         _DEBUG_INFO("session id : %d", session_id);
855
856         va_start(va, property);
857         err = func_point_set_property(session, property, va);
858         if (err != 1) {
859                 _DEBUG_ERROR("SetProperty() fail !!");
860                 va_end(va);
861                 return SYNC_AGENT_NA_FAIL;
862         } else {
863                 _DEBUG_INFO("SetProperty() success !!");
864         }
865         va_end(va);
866
867         _EXTERN_FUNC_EXIT;
868
869         return res;
870 }
871
872 EXPORT_API sync_agent_na_result_e sync_agent_get_status(int na_plugin_id, unsigned int session_id, sync_agent_na_status_e status, ...)
873 {
874         _EXTERN_FUNC_ENTER;
875
876         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
877         void *session = NULL;
878         va_list va;
879
880         plugin_get_status_cb func_point_get_status = plugin_get_function_get_status(na_plugin_id);
881
882         if (func_point_get_status == NULL) {
883                 _DEBUG_ERROR("cannot get func_point_get_status !!");
884                 return SYNC_AGENT_NA_FAIL;
885         }
886
887         int err = na_get_session(session_id, &session);
888         if (err != 1) {
889                 _DEBUG_ERROR("na_get_session() fail !!");
890                 return SYNC_AGENT_NA_FAIL;
891         } else {
892                 _DEBUG_INFO("na_get_session() success !!");
893         }
894
895         _DEBUG_INFO("session id : %d", session_id);
896
897         va_start(va, status);
898         err = func_point_get_status(session, status, va);
899         if (err != 1) {
900                 _DEBUG_ERROR("GetStatus() fail !!");
901                 va_end(va);
902                 return SYNC_AGENT_NA_FAIL;
903         } else {
904                 _DEBUG_INFO("GetStatus() success !!");
905         }
906         va_end(va);
907
908         _EXTERN_FUNC_EXIT;
909
910         return res;
911 }
912
913 EXPORT_API void sync_agent_free_na_common_header_info(sync_agent_na_common_header_info_s * header_info)
914 {
915         _EXTERN_FUNC_ENTER;
916
917         if (header_info == NULL)
918                 return;
919
920         if (header_info->key != NULL) {
921                 free(header_info->key);
922         }
923         if (header_info->value != NULL) {
924                 free(header_info->value);
925         }
926
927         free(header_info);
928
929         _EXTERN_FUNC_EXIT;
930 }