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