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