summaryrefslogtreecommitdiff
path: root/src/agent/service-engine/se_sync.c
blob: a50098d84c2724d9ae55011dcf457b840a57f129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
/*
 * oma-ds-agent
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *   @SE_Sync.c
 *   @version									0.1
 *   @brief										This file is the source file of implementation of functions which process sync request, auto configure etc..
 */

#include <sys/time.h>

#include <sync_agent.h>
#include <plugin/plugin_slp_device.h>

#include "service-engine/se_sync.h"
#include "service-engine/se_storage.h"
#include "service-engine/se_common.h"
#include "service-engine/se_notification.h"
#include "service-adapter/sa_common_interface.h"
#include "common/common_util.h"
#include "common/common_define_internal.h"

#ifndef OMADS_AGENT_LOG
#undef LOG_TAG
#define LOG_TAG "OMA_DS_SE"
#endif

static int __convert_alert_to_sync_type_value(alert_type_e sync_type);
static int __convert_src_uri_value(char *src_uri);
static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error);
static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result);
static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client);
static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result);

static se_error_type_e __init_datastore_info_array(int account_id);
static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type);
static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore);
static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore);
static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore);
static se_error_type_e __get_sync_type(int account_id, alert_type_e * sync_type);

static se_error_type_e __on_synchronising_account(int account_id);
static se_error_type_e _off_synchronising_account(int account_id);

static se_error_type_e __on_resume_flag(int account_id);
static se_error_type_e _off_resume_flag(int account_id);

static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj);
static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type);
static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package);
static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type);
static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id);
static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type);
static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status);
static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
static se_error_type_e _update_sync_result(int account_id);

static command_result_e ___convert_return_status(sync_agent_da_return_e da_err);
static char *__convert_cttype_str(int itemTypeId);
static int ___convert_sync_type_value(char *sync_type_str);
static char *__convert_sync_type_str(alert_type_e sync_type);
static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status);
static char *___convert_operation_type_str(operation_type_e operation_type);
static char *_convert_sync_progress_str(sync_progress_e process);
static char *_convert_sync_error_str(sync_error_e error);
static se_error_type_e _check_low_battery();
static se_error_type_e _open_services();
static se_error_type_e _close_services();

static inline long myclock()
{
	struct timeval tv;
	gettimeofday(&tv, 0);
	return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}

static int __convert_alert_to_sync_type_value(alert_type_e sync_type)
{
	_INNER_FUNC_ENTER;

	int sync_type_value = 0;
	_DEBUG_INFO("sync_type: [%d]", sync_type);

	switch (sync_type) {
	case ALERT_TWO_WAY:
		sync_type_value = SYNC_TYPE_UPDATE_BOTH;
		break;
	case ALERT_SLOW_SYNC:
		sync_type_value = SYNC_TYPE_FULL_SYNC;
		break;
	case ALERT_ONE_WAY_FROM_CLIENT:
		sync_type_value = SYNC_TYPE_UPDATE_TO_SERVER;
		break;
	case ALERT_REFRESH_FROM_CLIENT:
		sync_type_value = SYNC_TYPE_REFRESH_FROM_PHONE;
		break;
	case ALERT_ONE_WAY_FROM_SERVER:
		sync_type_value = SYNC_TYPE_UPDATE_TO_PHONE;
		break;
	case ALERT_REFRESH_FROM_SERVER:
		sync_type_value = SYNC_TYPE_REFRESH_FROM_SERVER;
		break;
	default:
		_DEBUG_ERROR("sync_type is invalid!!");
		break;
	}

	_INNER_FUNC_EXIT;
	return sync_type_value;
}

static int __convert_src_uri_value(char *src_uri)
{
	_INNER_FUNC_ENTER;

	int src_uri_value = 0;
	_DEBUG_INFO("src_uri: [%s]", src_uri);

	if (strcmp(src_uri,DEFINE_SOURCE_CONTACT_URI) == 0) {
		src_uri_value = SRC_URI_CONTACT;
	} else if (strcmp(src_uri,DEFINE_SOURCE_CALENDAR_URI) == 0) {
		src_uri_value = SRC_URI_CALENDAR;
	} else if (strcmp(src_uri,DEFINE_SOURCE_MEMO_URI) == 0) {
		src_uri_value = SRC_URI_MEMO;
	} else if (strcmp(src_uri,DEFINE_SOURCE_CALLLOG_URI) == 0) {
		src_uri_value = SRC_URI_CALLLOG;
	} else {
		_DEBUG_ERROR("src_uri is invalid!!");
	}

	_INNER_FUNC_EXIT;
	return src_uri_value;
}

static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error)
{
	_INNER_FUNC_ENTER;

	char *profileDirName = NULL;
	se_error_type_e err = SE_INTERNAL_OK;
	bool result;

	result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
	if (result == false) {
		_DEBUG_ERROR("failed in get_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	if (profileDirName != NULL) {
		_DEBUG_INFO("profileDirName: [%s]", profileDirName);
		err = session_process(profileDirName, server_sync_type, process, error);
		if (err != SE_INTERNAL_OK) {
			_DEBUG_ERROR("failed to send noti");
			goto error;
		}
	}

 error:
	if (profileDirName != NULL) {
		free(profileDirName);
		profileDirName = NULL;
	}

	_INNER_FUNC_EXIT;

	return err;
}

static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result)
{
	_INNER_FUNC_ENTER;

	_DEBUG_VERBOSE("account_id =%d, progress_status = %d, operation_type = %d, content_type = %d, isFromServer = %d, needToSave = %d ", account_id, progress_status, operation_type, content_type, is_from_server, need_to_save);
	_DEBUG_VERBOSE("numberOfChange = %d, received_count = %d,  syncCount = %d", sync_result->number_of_change, sync_result->received_count, sync_result->add_count + sync_result->replace_count + sync_result->delete_count);

	se_error_type_e err = SE_INTERNAL_OK;
	char *profileDirName = NULL;
	char *uri = NULL;
	char *sync_type = NULL;
	char *operation = NULL;
	char *progress = NULL;
	int sync_type_value = 0;
	int uri_value = 0;

	bool result;
	result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
	if (result == false) {
		_DEBUG_ERROR("failed in get_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	/* do not need to send update noti to ui */
	if (profileDirName == NULL) {
		_DEBUG_VERBOSE("profileDirName is NULL");
		goto error;
	}

	if (need_to_save == true) {
		err = write_sync_statistics(account_id, content_type, is_from_server, sync_result);
		if (err != SE_INTERNAL_OK) {
			_DEBUG_ERROR("failed in writeSyncStatistics");
			goto error;
		}
	}

	sync_type = __convert_sync_type_str(server_sync_type);
	sync_type_value = __convert_alert_to_sync_type_value(server_sync_type);
	_DEBUG_INFO("sync_type_value: [%d]", sync_type_value);

	progress = ___convert_sync_progress_status_str(progress_status);

	operation = ___convert_operation_type_str(operation_type);

	if (sync_type == NULL || progress == NULL || operation == NULL) {
		err = SE_INTERNAL_NOT_DEFINED;
		goto error;
	}

	if (datastoreinfo_per_content_type[content_type] != NULL) {
		if (datastoreinfo_per_content_type[content_type]->source != NULL)
			uri = strdup(datastoreinfo_per_content_type[content_type]->source);
	}
	if (uri != NULL) {
		uri_value = __convert_src_uri_value(uri);
		_DEBUG_INFO("uri_value: [%d]", uri_value);
	} else {
		_DEBUG_ERROR("uri is NULL!!");
		goto error;
	}

	err = send_noti_process_update(profileDirName, sync_type_value, uri_value, progress, operation, is_from_server, 0, 0, sync_result->number_of_change, sync_result->add_count + sync_result->replace_count + sync_result->delete_count);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in send_noti_process_update");
		goto error;
	}

 error:

	if (profileDirName != NULL)
		free(profileDirName);

	if (uri != NULL)
		free(uri);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	err = write_profile_data(account_id, alert_type, sync_session_result, last_session_time, only_from_client);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in writeProfileData");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	err = write_sync_resource_info(account_id, content_type, last_session_time, only_from_client, client_sync_result, server_sync_result);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in write_sync_resource_info");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __init_datastore_info_array(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, TYPE_CONTACT);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in set_datastoreinfo_array");
		goto error;
	}

	err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, TYPE_CALENDAR);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in set_datastoreinfo_array");
		goto error;
	}

	err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, TYPE_MEMO);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in set_datastoreinfo_array");
		goto error;
	}

	err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, TYPE_CALLLOG);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in set_datastoreinfo_array");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	datastore_s *datastore_info = NULL;
	bool result;
	char *value = NULL;

	result = get_config(account_id, config_key, &value);
	if (result == true) {
		if (strcmp(value, "1") == 0) {
			err = ___generate_datastore_info(account_id, content_type, &datastore_info);
			if (err != SE_INTERNAL_OK) {
				_DEBUG_ERROR("failed to create or init datastore_info");
				datastoreinfo_per_content_type[content_type] = NULL;
				goto error;
			}
			datastoreinfo_per_content_type[content_type] = datastore_info;
		}

	} else
		datastoreinfo_per_content_type[content_type] = NULL;

 error:

	if (value != NULL)
		free(value);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore)
{
	_INNER_FUNC_ENTER;
	_DEBUG_VERBOSE("content_type=[%d]\n", content_type);

	se_error_type_e err = SE_INTERNAL_OK;
	char *clientLastAnchor = NULL;
	char *clientNextAnchor = NULL;
	char *serverLastAnchor = NULL;

	sync_agent_da_return_e da_err = SYNC_AGENT_DA_ERRORS;

	sync_result_s *pSyncResult = NULL;
	datastore_s *temp_datastore = NULL;
	err = ___set_datastore_config(account_id, content_type, &temp_datastore);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in set_datastore_config");
		goto error;
	}

	GList *anchor_list = NULL;
	sync_agent_da_get_last_anchor_list_query_s query;
	query.option = SYNC_AGENT_DA_GET_LAST_ANCHOR_LIST_OPTION_ITEM_TYPE_ID;
	query.account_id = account_id;
	query.item_type_id = temp_datastore->datastore_id;

	da_err = sync_agent_get_last_anchor_list(&query, &anchor_list);
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_get_last_anchor_list !!");
		goto error;
	}

	if (g_list_length(anchor_list) > 0) {
		GList *last_anchor_info = g_list_nth(anchor_list, g_list_length(anchor_list) - 1);
		clientLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_client;
		serverLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_server;
	} else {
		sync_agent_da_last_anchor_s lastAnchor_daci;

		lastAnchor_daci.account_id = account_id;
		lastAnchor_daci.data_store_id = temp_datastore->datastore_id;
		lastAnchor_daci.last_anchor_server = NULL;
		lastAnchor_daci.last_anchor_client = NULL;
		lastAnchor_daci.access_name = "Engine";

		da_err = sync_agent_add_last_anchor(&lastAnchor_daci);
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("da_add_last_anchor_internal is failed %d", da_err);
			err = SE_INTERNAL_DA_ERROR;
			goto error;
		}
	}

	clientNextAnchor = g_strdup_printf("%ld", time(NULL));

	_DEBUG_VERBOSE("clientLastAnchor = %s\n", clientLastAnchor);
	_DEBUG_VERBOSE("serverLastAnchor = %s\n", serverLastAnchor);
	_DEBUG_VERBOSE("clientNextAnchor = %s\n", clientNextAnchor);

	/*slow sync if managed anchor info not exist.. (first sync) */
	if (clientLastAnchor == NULL)
		set_datastore_client_anchor(temp_datastore, clientNextAnchor, clientNextAnchor);
	else
		set_datastore_client_anchor(temp_datastore, clientLastAnchor, clientNextAnchor);

	set_datastore_server_anchor(temp_datastore, serverLastAnchor, NULL);

	pSyncResult = create_sync_result();
	if (pSyncResult == NULL) {
		_DEBUG_ERROR("create_syncResult is failed");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}
	temp_datastore->client_sync_result = pSyncResult;

	pSyncResult = create_sync_result();
	if (pSyncResult == NULL) {
		_DEBUG_ERROR("create_syncResult is failed");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}
	temp_datastore->server_sync_result = pSyncResult;

	*datastore = temp_datastore;
	temp_datastore = NULL;

 error:

	if (clientNextAnchor != NULL)
		free(clientNextAnchor);

	if (temp_datastore != NULL)
		free_datastore(temp_datastore);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	char *sourceDatastore = NULL;
	char key[128];
	datastore_s *temp_datastore = NULL;
	sync_result_s *pSyncResult = NULL;

	bool result;
	switch (content_type) {
	case TYPE_CONTACT:
		snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
		break;
	case TYPE_CALENDAR:
		snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
		break;
	case TYPE_MEMO:
		snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
		break;
	case TYPE_CALLLOG:
		snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
		break;
	default:
		_DEBUG_VERBOSE("unknown content type = %d", content_type);
		goto error;
	}

	_DEBUG_ERROR("content_type = %d", content_type);
	_DEBUG_ERROR("key = %s", key);
	result = get_config(account_id, key, &sourceDatastore);
	if (result == false) {
		_DEBUG_ERROR("failed in get_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	temp_datastore = create_datastore(NULL, sourceDatastore);
	if (temp_datastore == NULL) {
		_DEBUG_ERROR("failed to create_Datastore");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}

	pSyncResult = create_sync_result();
	if (pSyncResult == NULL) {
		_DEBUG_ERROR("create_syncResult is failed");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}
	temp_datastore->client_sync_result = pSyncResult;

	pSyncResult = create_sync_result();
	if (pSyncResult == NULL) {
		_DEBUG_ERROR("create_syncResult is failed");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}
	temp_datastore->server_sync_result = pSyncResult;

	*datastore = temp_datastore;
	temp_datastore = NULL;

 error:
	if (sourceDatastore != NULL)
		free(sourceDatastore);

	if (temp_datastore != NULL)
		free_datastore(temp_datastore);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore)
{
	_INNER_FUNC_ENTER;

	datastore_s *temp_datastore = NULL;
	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	char *contentType = NULL;

	char sourceDatastore_key[128];
	char targetDatastore_key[128];
	char id_key[128];
	char pw_key[128];

	char *sourceDatastore = NULL;
	char *targetDatastore = NULL;
	char *sync_type = NULL;
	char *id = NULL;
	char *pw = NULL;

	alert_type_e alert_type = ALERT_UNKNOWN;

	GList *config_list = NULL;
	GList *iter = NULL;
	sync_agent_da_config_s *config_data = NULL;

	int folder_type_id = 0;
	switch (content_type) {
	case TYPE_CONTACT:
		{
			folder_type_id = 0;
			contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS;
		}
		break;
	case TYPE_CALENDAR:
		{
			folder_type_id = 0;
			contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR;
		}
		break;
	case TYPE_MEMO:
		{
			folder_type_id = 0;
			contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO;
		}
		break;
	case TYPE_CALLLOG:
		{
			folder_type_id = 0;
			contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG;
		}
		break;
	default:
		_DEBUG_VERBOSE("unknown content type = %d", content_type);
		err = SE_INTERNAL_ERROR;
		goto error;
	}

	snprintf(sourceDatastore_key, sizeof(sourceDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
	snprintf(targetDatastore_key, sizeof(targetDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET);
	snprintf(id_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID);
	snprintf(pw_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD);

	da_err = sync_agent_get_config_list(account_id, &config_list);
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	for (iter = config_list; iter != NULL; iter = g_list_next(iter)) {
		config_data = (sync_agent_da_config_s *) iter->data;

		if (config_data != NULL) {
			if (config_data->key != NULL) {
				if (strcmp(config_data->key, sourceDatastore_key) == 0) {
					if (config_data->value != NULL)
						sourceDatastore = strdup(config_data->value);
				}

				if (strcmp(config_data->key, targetDatastore_key) == 0) {
					if (config_data->value != NULL)
						targetDatastore = strdup(config_data->value);
				}

				if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE) == 0) {
					if (config_data->value != NULL)
						sync_type = strdup(config_data->value);
				}

				if (strcmp(config_data->key, id_key) == 0) {
					if (config_data->value != NULL)
						id = strdup(config_data->value);
				}

				if (strcmp(config_data->key, pw_key) == 0) {
					if (config_data->value != NULL)
						pw = strdup(config_data->value);
				}
			}
		}
	}

	if (sync_type != NULL) {
		alert_type = ___convert_sync_type_value(sync_type);
	} else {
		_DEBUG_ERROR("sync_type is NULL !");
		err = SE_INTERNAL_ERROR;
		goto error;
	}

	_DEBUG_VERBOSE("get_Config result  sourceDatastore= %s ", sourceDatastore);
	_DEBUG_VERBOSE("get_Config result  targetDatastore= %s ", targetDatastore);
	_DEBUG_VERBOSE("get_Config result  alert_type= %s ", sync_type);
	_DEBUG_VERBOSE("get_Config result  id= %s ", id);
	_DEBUG_VERBOSE("get_Config result  pw= %s ", pw);

	temp_datastore = create_datastore(targetDatastore, sourceDatastore);
	if (temp_datastore == NULL) {
		_DEBUG_ERROR("temp_datastore is NULL");
		err = SE_INTERNAL_ERROR;
		goto error;
	}

	set_datastore_content_type_info(temp_datastore, content_type, folder_type_id);
	set_datastore_account_info(temp_datastore, id, pw);
	set_datastore_client_sync_type(temp_datastore, alert_type);

	*datastore = temp_datastore;
	temp_datastore = NULL;

 error:

	if (sourceDatastore != NULL)
		free(sourceDatastore);

	if (targetDatastore != NULL)
		free(targetDatastore);

	if (sync_type != NULL)
		free(sync_type);

	if (id != NULL)
		free(id);

	if (pw != NULL)
		free(pw);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __get_sync_type(int account_id, alert_type_e * alert_type)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	char *sync_type = NULL;

	bool result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, &sync_type);
	if (result == false) {
		_DEBUG_ERROR("failed in get_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	*alert_type = ___convert_sync_type_value(sync_type);

 error:

	if (sync_type != NULL)
		free(sync_type);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __on_synchronising_account(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "1", "string", "SE");
	if (result == false) {
		_DEBUG_ERROR("failed in set_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _off_synchronising_account(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "string", "SE");
	if (result == false) {
		_DEBUG_ERROR("failed in set_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __on_resume_flag(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "1", "string", "SE");
	if (result == false) {
		_DEBUG_ERROR("failed in set_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _off_resume_flag(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "0", "string", "SE");
	if (result == false) {
		_DEBUG_ERROR("failed in set_Config");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj)
{
	_INNER_FUNC_ENTER;

	if (account_id < 1){
		_DEBUG_ERROR("account_id is invalid!!");
		return SE_INTERNAL_ERROR;
	} else {
		_DEBUG_INFO("account_id: [%d]", account_id);
	}

	se_error_type_e err = SE_INTERNAL_OK;
	changed_datastore_s *pChangedDatastore = NULL;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	int content_type;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
				continue;

			if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC
			    || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT_BY_SERVER) {

				if (content_type == TYPE_CALLLOG) {
					_DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service for call log");
					sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->datastore_id);
				}

				GList *item_list = NULL;
				sync_agent_da_get_item_list_query_s query;
				query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID;
				query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

				da_err = sync_agent_get_item_list(account_id, &query, &item_list);
				if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_get_item_list !!");
				}

				pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_list));

				if (pChangedDatastore == NULL) {
					sync_agent_free_item_list(item_list);
					_DEBUG_ERROR("failed in create_ChangedDatastore");
					err = SE_INTERNAL_NO_MEMORY;
					goto error;
				}

				if (g_list_length(item_list) > 0) {
					GList *iter = NULL;
					sync_agent_da_item_s *iter_data;
					for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
						iter_data = NULL;
						iter_data = (sync_agent_da_item_s *) (iter->data);
						if (iter_data != NULL) {
							char *cttype = __convert_cttype_str(iter_data->data_store_id);
							changed_item_s *pChanged = create_changed_item(CHANGE_ADD, iter_data->item_id);
							if (pChanged == NULL) {
								sync_agent_free_item_list(item_list);
								_DEBUG_ERROR("failed in create_ChangedDatastore");
								err = SE_INTERNAL_NO_MEMORY;
								goto error;
							}

							set_changed_item_content_type(pChanged, cttype);
							set_changed_item_index_of_datastore(pChanged, content_type);

							add_changed_datastore_changed_item(pChangedDatastore, pChanged);
						} else {
							_DEBUG_ERROR("iter_data is NULL !!");
							sync_agent_free_item_list(item_list);
							err = SE_INTERNAL_NO_MEMORY;
							goto error;
						}
					}

					sync_agent_free_item_list(item_list);
				}

				GList *item_id_list = NULL;

				sync_agent_da_get_item_id_list_query_s item_id_query = {0,};
				item_id_query.option = SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_ITEM_TYPE_ID;
				item_id_query.account_id = account_id;
				item_id_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
				item_id_query.operation_id = SYNC_AGENT_DA_CHANGE_OPERATION_DELETE;

				da_err = sync_agent_get_item_id_list(&item_id_query, &item_id_list);
				if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_get_item_id_list !!");
					err = SE_INTERNAL_DA_ERROR;
					goto error;
				}

				GList *item_id_iter = NULL;
				char *item_id;

				if (g_list_length(item_id_list) > 0) {
					for (item_id_iter = item_id_list; item_id_iter != NULL; item_id_iter = g_list_next(item_id_iter)) {
						item_id = NULL;
						item_id = (char *)(item_id_iter->data);

						da_err = sync_agent_delete_item(item_id, 1);
						if (da_err != SYNC_AGENT_DA_SUCCESS) {
							_DEBUG_ERROR("failed in sync_agent_delete_item !!");
							sync_agent_free_item_id_list(item_id_list);
							err = SE_INTERNAL_DA_ERROR;
							goto error;
						}
					}
				}

				sync_agent_free_item_id_list(item_id_list);

				/*delete changelog data by account id and datastore id */
				sync_agent_da_delete_changelog_query_s delete_ch_query = {0,};
				delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID;
				delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

				da_err = sync_agent_delete_changelog(account_id, &delete_ch_query);
				if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
					err = SE_INTERNAL_DA_ERROR;
					goto error;
				}
			} else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY
				   || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY_BY_SERVER
				   || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
				_DEBUG_TRACE("server sync type is ALERT_TYPE [%d]", datastoreinfo_per_content_type[content_type]->server_sync_type);

				/*on resume flag into agent DB */
				err = __on_resume_flag(account_id);
				if (err != SE_INTERNAL_OK) {
					_DEBUG_ERROR("failed in __on_resume_flag");
					goto error;
				}

				if (content_type == TYPE_CALLLOG) {
					_DEBUG_TRACE("sync_agent_construct_item_tbl_from_service for call log");
					sync_agent_construct_item_tbl_from_service(account_id, content_type);
				}

				/*from changelog */
				GList *item_info_list = NULL;
				sync_agent_da_get_changelog_list_query_s get_ch_query;
				get_ch_query.option = SYNC_AGENT_DA_GET_CHANGELOG_LIST_OPTION_ITEM_TYPE_ID;
				get_ch_query.account_id = account_id;
				get_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

				da_err = sync_agent_get_changelog_list(&get_ch_query, &item_info_list);
				if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_get_changelog_list !!");
					err = SE_INTERNAL_NO_MEMORY;
					goto error;
				}

				pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_info_list));

				if (pChangedDatastore == NULL) {
					sync_agent_free_changelog_list(item_info_list);
					_DEBUG_ERROR("failed in create_ChangedDatastore");
					err = SE_INTERNAL_NO_MEMORY;
					goto error;
				}

				/*gathering changelog info */
				if (g_list_length(item_info_list) > 0) {
					char *cttype = __convert_cttype_str(datastoreinfo_per_content_type[content_type]->datastore_id);

					GList *item_info_iter = NULL;
					sync_agent_da_item_changelog_s *iter_data;

					for (item_info_iter = item_info_list; item_info_iter != NULL; item_info_iter = g_list_next(item_info_iter)) {
						iter_data = NULL;
						iter_data = (sync_agent_da_item_changelog_s *) (item_info_iter->data);

						if (iter_data != NULL) {
							changed_item_s *pChanged = create_changed_item(iter_data->operation_id - 300, iter_data->item_id);
							if (pChanged == NULL) {
								sync_agent_free_changelog_list(item_info_list);
								_DEBUG_ERROR("failed in create_ChangedDatastore");
								err = SE_INTERNAL_NO_MEMORY;
								goto error;
							}

							set_changed_item_content_type(pChanged, cttype);
							set_changed_item_index_of_datastore(pChanged, content_type);
							add_changed_datastore_changed_item(pChangedDatastore, pChanged);
						} else {
							sync_agent_free_changelog_list(item_info_list);
							_DEBUG_ERROR("iter_data is NULL !!");
							err = SE_INTERNAL_NO_MEMORY;
							goto error;
						}
					}

					int *itemTypeIdList = (int *)calloc(1, sizeof(int));
					if (itemTypeIdList == NULL) {
						_DEBUG_ERROR("failed in da_set_item_changelog_wait_status_internal");
						err = SE_INTERNAL_NO_MEMORY;
						goto error;
					}

					itemTypeIdList[0] = datastoreinfo_per_content_type[content_type]->datastore_id;

					sync_agent_da_update_changelog_query_s query;
					query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_WAIT_STATUS;
					query.folder_id_list = NULL;
					query.folder_id_count = 0;
					query.item_type_id_list = itemTypeIdList;
					query.item_type_id_count = 1;

					da_err = sync_agent_update_changelog(account_id, &query);

					sync_agent_free_changelog_list(item_info_list);

					if (itemTypeIdList != NULL)
						free(itemTypeIdList);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {
						_DEBUG_ERROR("failed in sync_agent_update_changelog !!");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}
				}
			} else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER
				   || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER_BY_SERVER
				   || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {

				if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER) {
					/*on resume flag into agent DB */
					err = __on_resume_flag(account_id);
					if (err != SE_INTERNAL_OK) {
						_DEBUG_ERROR("failed in __on_resume_flag");
						goto error;
					}
				}

				/*MUST create empty changedDatastore.. */
				pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, 0);
				if (pChangedDatastore == NULL) {
					_DEBUG_ERROR("pChangedDatastore is NULL");
					err = SE_INTERNAL_NO_MEMORY;
					goto error;
				}

				if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {

					/*_DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service");
					sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type);*/

					/*  Delete All item (include changelog), before adapting server item data */
					_DEBUG_TRACE("sync_agent_begin_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
					sync_agent_begin_service(datastoreinfo_per_content_type[content_type]->datastore_id);
					sync_agent_begin_transaction();

					/*delete service item data */
					sync_agent_da_delete_service_item_query_s query;
					query.content_type = datastoreinfo_per_content_type[content_type]->datastore_id;
					query.account_id = account_id;

					da_err = sync_agent_query_delete_service_items(&query);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {

						sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
						sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);

						_DEBUG_ERROR("failed in sync_agent_query_delete_service_items()");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}

					_DEBUG_TRACE("da_get_item_by_account_id_internal");
					/*get all item by account id */
					GList *item_list = NULL;
					sync_agent_da_get_item_list_query_s get_item_query;
					get_item_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID;
					get_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

					da_err = sync_agent_get_item_list(account_id, &get_item_query, &item_list);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {
						_DEBUG_ERROR("failed in sync_agent_get_item_list !!");
					}
					_DEBUG_TRACE("item_cnt = %d", g_list_length(item_list));

					GList *item_iter = NULL;
					sync_agent_da_item_s *item_iter_data;
					for (item_iter = item_list; item_iter != NULL; item_iter = g_list_next(item_iter)) {
						item_iter_data = NULL;
						item_iter_data = (sync_agent_da_item_s *) (item_iter->data);

						GList *mapping_list = NULL;
						sync_agent_da_get_item_list_query_s mapping_query;
						mapping_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
						mapping_query.item_id = item_iter_data->item_id;

						da_err = sync_agent_get_item_list(account_id, &mapping_query, &mapping_list);
						if (da_err != SYNC_AGENT_DA_SUCCESS) {
							_DEBUG_ERROR("failed in sync_agent_get_item_list !!");
						}

						_DEBUG_TRACE("acc_cnt = %d", g_list_length(mapping_list));

						GList *mapping_iter = NULL;
						sync_agent_da_item_s *mapping_iter_data;

						for (mapping_iter = mapping_list; mapping_iter != NULL; mapping_iter = g_list_next(mapping_iter)) {
							mapping_iter_data = NULL;
							mapping_iter_data = (sync_agent_da_item_s *) (mapping_iter->data);
							da_err = sync_agent_delete_item(mapping_iter_data->item_id, 1);
							if (da_err != SYNC_AGENT_DA_SUCCESS) {
								_DEBUG_ERROR("failed in sync_agent_delete_item !!");
							}
						}

						sync_agent_free_item_list(mapping_list);
					}

					/*delete changelog data by account id and datastore id */
					_DEBUG_TRACE("da_delete_item_changelog_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id);

					sync_agent_da_delete_changelog_query_s delete_ch_query;
					delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID;
					delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

					da_err = sync_agent_delete_changelog(account_id, &delete_ch_query);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {

						sync_agent_free_item_list(item_list);

						sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
						sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);

						_DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}

					/*delete item from item_tbl */
					_DEBUG_TRACE("da_delete_item_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
					sync_agent_da_delete_item_query_s delete_item_query;
					delete_item_query.option = SYNC_AGENT_DA_DELETE_ITEM_OPTION_ITEM_TYPE_ID;
					delete_item_query.account_id = account_id;
					delete_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;

					da_err = sync_agent_query_delete_item(&delete_item_query);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {
						sync_agent_free_item_list(item_list);

						sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
						sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);

						_DEBUG_ERROR("failed in sync_agent_query_delete_item !!");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}

					_DEBUG_TRACE("sync_agent_end_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
					da_err = sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 1);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {

						sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
						sync_agent_free_item_list(item_list);

						_DEBUG_ERROR("failed in sync_agent_end_service !!");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}
					sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
					sync_agent_free_item_list(item_list);
				}
			}
			if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
				(*sync_obj)->changed_datastore = g_list_append((*sync_obj)->changed_datastore, pChangedDatastore);

				/* for prevent */
				if (pChangedDatastore != NULL) {
					set_number_of_change(datastoreinfo_per_content_type[content_type]->client_sync_result, pChangedDatastore->number_of_changes);
				} else {
					_DEBUG_ERROR("pChangedDatastore is NULL !");
				}

				operation_type_e operation_type;
				if (datastoreinfo_per_content_type[content_type]->client_sync_result->number_of_change == 0)
					operation_type = OPERATION_NOOP;
				else
					operation_type = OPERATION_ADD;
				err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, content_type, false, false, datastoreinfo_per_content_type[content_type]->client_sync_result);
				if (err != SE_INTERNAL_OK) {
					_DEBUG_ERROR("Failed in process_update");
					goto error;
				}
			}
		}
	}

	_INNER_FUNC_EXIT;
	return err;

 error:

	if (pChangedDatastore != NULL) {
		free_changed_datastore(pChangedDatastore);
		pChangedDatastore = NULL;
	}

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	/*set synchronising flag into agent DB */
	err = __on_synchronising_account(account_id);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __on_synchronising_account");
		goto error;
	}

	/*get sync type */
	err = __get_sync_type(account_id, sync_type);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __get_sync_type");
		goto error;
	}

	/*init datastore_info_array */
	err = __init_datastore_info_array(account_id);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __init_datastore_info_array");
		goto error;
	}

	/*set config based on sync mode */
	err = __set_config_based_on_sync_mode(account_id, sync_mode, san_package);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __set_config_based_on_sync_mode");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	int content_type;

	if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
		if (san_package != NULL) {
			int count = san_package->cnt_sync_alerts;
			_DEBUG_TRACE("count = %d", count);
			int i;
			for (i = 0; i < count; i++) {
				for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {

					if (datastoreinfo_per_content_type[content_type] != NULL) {
						_DEBUG_TRACE("datastoreinfo_per_content_type[content_type]->target = %s", datastoreinfo_per_content_type[content_type]->target);
						if (datastoreinfo_per_content_type[content_type]->target == NULL)
							continue;

						if (strcmp(san_package->sync_alerts[i].server_uri, datastoreinfo_per_content_type[content_type]->target) == 0) {
							datastoreinfo_per_content_type[content_type]->client_sync_type = san_package->sync_alerts[i].sync_type;
							_DEBUG_TRACE("san_package->syncAlerts[i].sync_type = %d", san_package->sync_alerts[i].sync_type);
						} else
							datastoreinfo_per_content_type[content_type]->client_sync_type = ALERT_UNKNOWN;
					}
				}
			}
		}
	}

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	/*if there is no alert command from server check it and goto fail_part */
	if (g_list_length(pre_sync_return_obj->datastore_info) == 0) {
		_DEBUG_TRACE("alert command list from server is empty");
		err = SE_INTERNAL_MISCONFIGURATION;
		goto error;
	}

	err = __execute_pre_sync_set_server_id(account_id, pre_sync_return_obj->dev_id);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __set_server_id");
		goto error;
	}

	err = __execute_pre_sync_datastore(account_id, session_time, pre_sync_return_obj->datastore_info, server_sync_type);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __process_datastore");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;

}

static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	bool result;
	char *value = NULL;

	if (dev_id != NULL) {
		result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &value);
		if (result == true) {
			if (value != NULL) {
				if (strcmp(value, dev_id) == 0) {
					/* OK */
				} else {
					result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE");
					if (result == false) {
						_DEBUG_ERROR("failed in set_Config");
						err = SE_INTERNAL_DA_ERROR;
						goto error;
					}
				}
			} else {
				result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE");
				if (result == false) {
					_DEBUG_ERROR("failed in set_Config");
					err = SE_INTERNAL_DA_ERROR;
					goto error;
				}
			}
		} else {
			_DEBUG_ERROR("failed in get_Config");
			err = SE_INTERNAL_DA_ERROR;
			goto error;
		}
	}
 error:

	if (value != NULL) {
		free(value);
		value = NULL;
	}

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	int result;

	bool changeSyncType = false;
	alert_type_e sync_type = ALERT_UNKNOWN;

	int content_type;
	GList *serverDatastore_iter = NULL;
	datastore_info_s *serverDatastoreInfo = NULL;
	bool existInServerDatastore;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		existInServerDatastore = false;

		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
				_DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->client_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->client_sync_type);

				for (serverDatastore_iter = datastore_info; serverDatastore_iter != NULL; serverDatastore_iter = g_list_next(serverDatastore_iter)) {
					serverDatastoreInfo = serverDatastore_iter->data;

					if (strcmp(serverDatastoreInfo->source, datastoreinfo_per_content_type[content_type]->target) == 0) {
						existInServerDatastore = true;

						if (serverDatastoreInfo->next_anchor != NULL)
							datastoreinfo_per_content_type[content_type]->next_anchor_server = strdup(serverDatastoreInfo->next_anchor);

						datastoreinfo_per_content_type[content_type]->server_sync_type = serverDatastoreInfo->sync_type;

						if (datastoreinfo_per_content_type[content_type]->client_sync_type != datastoreinfo_per_content_type[content_type]->server_sync_type) {
							changeSyncType = true;
							sync_type = datastoreinfo_per_content_type[content_type]->server_sync_type;
						}

						_DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->server_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->server_sync_type);

						if (datastoreinfo_per_content_type[content_type]->last_anchor_server) {
							/*if (strcmp(datastoreinfo_per_content_type[content_type]->lastAnchorServer, serverDatastoreInfo->lastAnchor) != 0) {
							   free(datastoreinfo_per_content_type[content_type]->lastAnchorServer);
							   datastoreinfo_per_content_type[content_type]->lastAnchorServer = NULL;
							   datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC;
							   } */
						} else {
							/* When first synchronize, lastAnchorServer is NULL...
							   if (datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_SLOW_SYNC
							   && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT
							   && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT_BY_SERVER
							   && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER
							   && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
							   _DEBUG_VERBOSE("sync_agent_refresh_item_tbl_from_service");
							   sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type);
							   } */
						}
						break;
					}
				}

				if (!existInServerDatastore) {
					/* datastore config is wrong this datastore is not going to contain sync process from now */
					err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, false, true, datastoreinfo_per_content_type[content_type]->client_sync_result);
					if (err != SE_INTERNAL_OK) {
						_DEBUG_ERROR("failed to process_update");
						goto error;
					}

					err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, true, true, datastoreinfo_per_content_type[content_type]->server_sync_result);
					if (err != SE_INTERNAL_OK) {
						_DEBUG_ERROR("failed to process_update");
						goto error;
					}

					/* set fail into result (both client and server) */
					if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
						datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED;
					if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
						datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED;

					err = ___write_sync_resource_info(account_id, content_type, session_time, 0, datastoreinfo_per_content_type[content_type]->client_sync_result, datastoreinfo_per_content_type[content_type]->server_sync_result);
					if (err != SE_INTERNAL_OK) {
						_DEBUG_ERROR("failed to write_SyncResourceInfo");
						goto error;
					}

					free_datastore(datastoreinfo_per_content_type[content_type]);
					datastoreinfo_per_content_type[content_type] = NULL;

					datastore_s *datastore = NULL;
					err = __init_datastore_info(account_id, content_type, &datastore);
					if (err != SE_INTERNAL_OK) {
						_DEBUG_ERROR("failed to init_datastore_info");
						goto error;
					}

					datastoreinfo_per_content_type[content_type] = datastore;
				}
			}
		}
	}

	if (changeSyncType) {
		*server_sync_type = sync_type;
		result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, __convert_sync_type_str(sync_type), "string", "SE");
		if (result == false) {
			_DEBUG_ERROR("failed in set_config");
			err = SE_INTERNAL_DA_ERROR;
			goto error;
		}
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	if (client_sync_type == ALERT_TWO_WAY || client_sync_type == ALERT_TWO_WAY_BY_SERVER || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {

		err = __execute_sync_arrange_changelog(account_id, client_sync_type, (*sync_return_obj)->status);
		if (err != SE_INTERNAL_OK) {
			_DEBUG_ERROR("failed to process_update");
			goto error;
		}
	}

	err = __execute_sync_status(account_id, server_sync_type, sync_obj, sync_return_obj);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __execute_sync_status");
		goto error;
	}

	err = __execute_sync_change(account_id, server_sync_type, sync_obj, sync_return_obj);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __execute_sync_change");
		goto error;
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err;

	int i = 0;
	int count = 0;
	int list_length = g_list_length(status);
	char **itemIdList = (char **)calloc(list_length, sizeof(char *));
	if (itemIdList == NULL) {
		_DEBUG_ERROR("Failed to alloc memory");
		err = SE_INTERNAL_NO_MEMORY;
		goto error;
	}

	GList *appliedStatus_iter = NULL;
	applied_status_s *pAppliedStatus = NULL;
	for (appliedStatus_iter = status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
		pAppliedStatus = appliedStatus_iter->data;

		switch (pAppliedStatus->status) {
		case 200:
		case 201:
		case 202:
		case 203:
		case 204:
		case 205:
		case 207:
		case 208:
		case 209:
		case 210:
		case 211:
			{
				/*clean up change log */
				if (pAppliedStatus->luid != NULL)
					itemIdList[count++] = strdup(pAppliedStatus->luid);

				if (sync_type == ALERT_TWO_WAY || sync_type == ALERT_TWO_WAY_BY_SERVER || sync_type == ALERT_ONE_WAY_FROM_CLIENT || sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {

					if (pAppliedStatus->change_type == CHANGE_DELETE) {
						da_err = sync_agent_delete_item(pAppliedStatus->luid, 0);
						if (da_err != SYNC_AGENT_DA_SUCCESS) {
							_DEBUG_ERROR("failed in da_delete_item_by_item_id_internal");
							err = SE_INTERNAL_DA_ERROR;
							goto error;
						}
					}
				}
				break;
			}
		case 206:
		case 420:
			break;
		case 400:
		case 500:
			{
				sync_agent_da_item_changelog_s *get_changelog = NULL;
				da_err = sync_agent_create_changelog(&get_changelog);
				if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_create_changelog !!");
					err = SE_INTERNAL_DA_ERROR;
					goto error;
				}

				da_err = sync_agent_get_changelog(account_id, pAppliedStatus->luid, &get_changelog);
				if (da_err == SYNC_AGENT_DA_ERR_NO_DATA) {
					_DEBUG_VERBOSE("%s item does not exist in ChangeLog tbl", pAppliedStatus->luid);
					sync_agent_free_changelog(get_changelog);
					break;
				} else if (da_err != SYNC_AGENT_DA_SUCCESS) {
					_DEBUG_ERROR("failed in sync_agent_get_changelog !!");
					sync_agent_free_changelog(get_changelog);
					err = SE_INTERNAL_DA_ERROR;
					goto error;
				}

				if (get_changelog->status != NULL) {
					if (strcmp(get_changelog->status, "SYNC_ERROR") == 0) {
						if (pAppliedStatus->luid != NULL)
							itemIdList[count++] = strdup(pAppliedStatus->luid);
					} else {
						sync_agent_da_item_changelog_s *set_changelog = NULL;

						da_err = sync_agent_create_changelog(&set_changelog);
						if (da_err != SYNC_AGENT_DA_SUCCESS) {
							_DEBUG_ERROR("failed in sync_agent_create_changelog !!");
							sync_agent_free_changelog(get_changelog);
							err = SE_INTERNAL_DA_ERROR;
							goto error;
						}

						set_changelog->item_id = strdup(pAppliedStatus->luid);
						set_changelog->status = strdup("SYNC_ERROR");
						set_changelog->access_name = strdup("SE");

						sync_agent_da_update_changelog_query_s update_ch_query;
						update_ch_query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_SYNC_STATUS;
						update_ch_query.update_item = set_changelog;

						da_err = sync_agent_update_changelog(account_id, &update_ch_query);
						if (da_err != SYNC_AGENT_DA_SUCCESS) {
							_DEBUG_ERROR("failed in sync_agent_update_changelog !!");
							sync_agent_free_changelog(get_changelog);
							sync_agent_free_changelog(set_changelog);
							err = SE_INTERNAL_DA_ERROR;
							goto error;
						}

						sync_agent_free_changelog(set_changelog);
					}
				}

				sync_agent_free_changelog(get_changelog);
				break;
			}
		}
	}

	if (list_length >= 1) {
		_DEBUG_TRACE("before da_delete_item_changelog_by_item_id_list_internal");
		_DEBUG_TRACE("count = %d", count);

		sync_agent_da_delete_changelog_query_s query;
		query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_ID_LIST;
		query.item_id_list = itemIdList;
		query.count = count;

		da_err = sync_agent_delete_changelog(account_id, &query);
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
			err = SE_INTERNAL_DA_ERROR;
			goto error;
		}
	}

 error:

	/*free itemIdList */
	if (itemIdList != NULL) {
		for (i = 0; i < list_length; i++) {
			if (itemIdList[i] != NULL)
				free(itemIdList[i]);
		}
		free(itemIdList);
	}

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	int content_type;
	bool existEqualItem;
	bool needToSave;
	sync_result_s *pClientSyncResult;
	GList *changedDatastore_iter = NULL;
	changed_datastore_s *pSyncChangedDatastore = NULL;
	for (changedDatastore_iter = (*sync_obj)->changed_datastore; changedDatastore_iter != NULL;) {
		pSyncChangedDatastore = (changed_datastore_s *) changedDatastore_iter->data;
		_DEBUG_VERBOSE("pChangedDatastore = %p", pSyncChangedDatastore);

		int datastoreContentType = 0;
		for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
			if (datastoreinfo_per_content_type[content_type] != NULL) {
				if (strcmp(pSyncChangedDatastore->source, datastoreinfo_per_content_type[content_type]->source) == 0) {
					datastoreContentType = content_type;
					break;
				}
			}
		}

		GList *changedItem_iter = NULL;
		changed_item_s *changedItem = NULL;
		for (changedItem_iter = pSyncChangedDatastore->sent_item; changedItem_iter != NULL;) {
			changedItem = changedItem_iter->data;

			existEqualItem = false;
			GList *appliedStatus_iter = NULL;
			applied_status_s *pAppliedStatus = NULL;
			for (appliedStatus_iter = (*sync_return_obj)->status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
				pAppliedStatus = appliedStatus_iter->data;

				if (strcmp(pAppliedStatus->luid, changedItem->luid) == 0) {
					add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);

					if (pAppliedStatus->status >= 200 && pAppliedStatus->status <= 210) {
						switch (changedItem->change_type) {
						case CHANGE_ADD:
							add_add_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
							break;
						case CHANGE_REPLACE:
							add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
							break;
						case CHANGE_DELETE:
							add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
							break;
						default:
							break;
						}
					} else {
						/* if status is not success count it is failed */
					}

					(*sync_return_obj)->status = g_list_remove((*sync_return_obj)->status, pAppliedStatus);
					free_applied_status(pAppliedStatus);
					pAppliedStatus = NULL;

					changedItem_iter = g_list_next(changedItem_iter);

					pSyncChangedDatastore->sent_item = g_list_remove(pSyncChangedDatastore->sent_item, changedItem);
					free_changed_item(changedItem);
					changedItem = NULL;

					existEqualItem = true;
					break;
				}
			}
			if (existEqualItem == false)
				changedItem_iter = g_list_next(changedItem_iter);
		}

		changedDatastore_iter = g_list_next(changedDatastore_iter);

		needToSave = false;
		pClientSyncResult = datastoreinfo_per_content_type[datastoreContentType]->client_sync_result;
		if (pClientSyncResult->number_of_change == pClientSyncResult->received_count) {
			needToSave = true;

			if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result != NULL)
				datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->session_result = SYNC_SESSION_SUCCEEDED;

			/*remove datastore from list and
			   free current ChangedDatastore it does not need anymore because all item in datastore has been sent and receive status */
			(*sync_obj)->changed_datastore = g_list_remove((*sync_obj)->changed_datastore, pSyncChangedDatastore);
			free_changed_datastore(pSyncChangedDatastore);
			pSyncChangedDatastore = NULL;
		}

		operation_type_e operation_type;
		if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->number_of_change == 0)
			operation_type = OPERATION_NOOP;
		else
			operation_type = OPERATION_ADD;
		err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, false, needToSave, datastoreinfo_per_content_type[datastoreContentType]->client_sync_result);
		if (err != SE_INTERNAL_OK) {
			_DEBUG_ERROR("failed in process_update");
			goto error;
		}
	}

 error:

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
	sync_agent_da_service_item_s *service_item = NULL;
	sync_agent_da_item_s *fw_item = NULL;
	int content_type;
	GList *f_id_list = NULL;
	sync_result_s *tempServerSyncResult = NULL;

	/* process command that from server sended to client(sync, add, replace etc...) */
	changed_datastore_s *pSyncReturnChangedDatastore = NULL;
	sending_status_s *sendingStatus = NULL;
	GList *iter = NULL;
	for (iter = (*sync_return_obj)->changed_datastore; iter != NULL;) {
		pSyncReturnChangedDatastore = (changed_datastore_s *) iter->data;

		int datastoreContentType = 0;
		for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
			if (datastoreinfo_per_content_type[content_type] != NULL) {
				if (strcmp(pSyncReturnChangedDatastore->target, datastoreinfo_per_content_type[content_type]->source) == 0) {
					datastoreContentType = content_type;
					break;
				}
			}
		}

		/*noti to UI numberOfChanges from server */
		if (pSyncReturnChangedDatastore->has_number_of_changes) {
			set_number_of_change(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, pSyncReturnChangedDatastore->number_of_changes);

			operation_type_e operation_type;
			if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0)
				operation_type = OPERATION_NOOP;
			else
				operation_type = OPERATION_ADD;
			err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, false, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
			if (err != SE_INTERNAL_OK) {
				_DEBUG_ERROR("failed in process_update");
				goto error;
			}
		}

		sendingStatus = create_sending_status(pSyncReturnChangedDatastore->target, pSyncReturnChangedDatastore->source);
		if (sendingStatus == NULL) {
			err = SE_INTERNAL_NO_MEMORY;
			_DEBUG_ERROR("failed to alloc memory");
			goto error;
		}

		/*get folderId */
		char *folderId = NULL;
		sync_agent_da_get_folder_id_list_query_s query;
		query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID;
		query.account_id = account_id;
		query.item_type_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
		query.folder_type_id = datastoreinfo_per_content_type[datastoreContentType]->folder_type_id;

		da_err = sync_agent_get_folder_id_list(&query, &f_id_list);
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!");
			goto error;
		}

		if (g_list_length(f_id_list) > 0) {
			GList *iter = g_list_nth(f_id_list, 0);
			folderId = (char *)(iter->data);
		} else {
			_DEBUG_ERROR("failed to get folderId");
			err = SE_INTERNAL_DA_ERROR;
			goto error;
		}

		int changeItemCount = g_list_length(pSyncReturnChangedDatastore->change_item);
		if (changeItemCount > 0) {
			/*begin transaction */
			_DEBUG_VERBOSE("sync_agent_begin_service with datastoreContentType = %d", datastoreContentType);
//                      sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
			sync_agent_begin_transaction();

			/*back up syncresult structure for rollback case */
			tempServerSyncResult = dup_sync_result(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
		}

		bool needToSave = false;
		sync_result_s *pServerSyncResult = NULL;
		GList *changedIter = NULL;
		changed_item_s *changedItem = NULL;
		applied_status_s *appliedStatus = NULL;

		for (changedIter = pSyncReturnChangedDatastore->change_item; changedIter != NULL; changedIter = g_list_next(changedIter)) {
			changedItem = (changedIter->data);

			_DEBUG_VERBOSE("changedItem->changeType : %d", changedItem->change_type);
			_DEBUG_VERBOSE("changedItem->luid : %s", changedItem->luid);
			_DEBUG_VERBOSE("changedItem->content_type : %s", changedItem->content_type);
			_DEBUG_VERBOSE("changedItem->indexOfDatastore : %d", changedItem->index_of_datastore);
			_DEBUG_VERBOSE("changedItem->data : %s", changedItem->data);

			add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);

			bool da_fail = false;
			command_result_e returnResult = COMMAND_RESULT_INIT;
			switch (changedItem->change_type) {
			case CHANGE_ADD:
				{
					/* add to service DB */
					if (changedItem->data != NULL) {
						char *fw_id = NULL;

						da_err = sync_agent_create_service_item(&service_item);
						if ((da_err != SYNC_AGENT_DA_SUCCESS) || (service_item == NULL)) {
							_DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
							returnResult = ___convert_return_status(da_err);
							da_fail = true;
							break;
						}

						service_item->item_id = g_strdup(changedItem->luid);
						service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
						service_item->account_id = account_id;
						service_item->folder_id = g_strdup(folderId);
						service_item->access_name = g_strdup("Engine");
						service_item->data = g_strdup(changedItem->data);
						sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);

						da_err = sync_agent_add_service_item(service_item, &fw_id, false);

						sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);

						_DEBUG_VERBOSE("da_err = %d", da_err);

						if (da_err == SYNC_AGENT_DA_SUCCESS) {
							int acc_cnt;
							bool success = true;
							char *service_id = sync_agent_get_service_item_id(fw_id);

							GList *folder_list = NULL;
							sync_agent_da_get_folder_list_query_s query;
							query.option = SYNC_AGENT_DA_GET_FOLDER_LIST_OPTION_SERVICE_ID_MAPPING;
							query.account_id = account_id;
							query.folder_id = folderId;
							da_err = sync_agent_get_folder_list(&query, &folder_list);
							if (da_err != SYNC_AGENT_DA_SUCCESS) {
								_DEBUG_ERROR("sync_agent_get_folder_list() failed !!");
								success = false;
							}

							acc_cnt = g_list_length(folder_list);

							_DEBUG_VERBOSE("acc_cnt = %d", acc_cnt);
							if (acc_cnt > 0) {
								char **fw_item_id_list = sync_agent_generate_item_luid(1, acc_cnt);
								char **id_list = (char **)calloc(acc_cnt, sizeof(char *));
								if (id_list == NULL) {
									_DEBUG_ERROR("CALLOC failed !!!");
									success = false;
									break;
								}

								if (fw_item_id_list == NULL) {
									_DEBUG_ERROR("failed in sync_agent_generate_item_luid");
									da_err = SYNC_AGENT_DA_ERRORS;
									success = false;
								} else {
									int i = 0;
									GList *iter = NULL;
									sync_agent_da_folder_s *iter_data;

									for (iter = folder_list; iter != NULL; iter = g_list_next(iter)) {
										iter_data = NULL;
										iter_data = (sync_agent_da_folder_s *) (iter->data);

										da_err = sync_agent_create_item(&fw_item);
										if ((da_err != SYNC_AGENT_DA_SUCCESS) || (fw_item == NULL)) {
											_DEBUG_ERROR("failed in sync_agent_create_item() = %d", da_err);
											success = false;
											break;
										}

										fw_item->item_id = g_strdup(fw_item_id_list[i]);
										fw_item->data_store_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
										fw_item->account_id = iter_data->account_id;
										fw_item->folder_id = g_strdup(iter_data->folder_id);
										fw_item->service_id = g_strdup(service_id);
										fw_item->access_name = g_strdup("DACI_ChangeLog");

										da_err = sync_agent_add_item(fw_item, &(id_list[i]), true);
										if (fw_item != NULL)
											sync_agent_free_item(fw_item);
										free(fw_item_id_list[i]);
										free(id_list[i]);

										i++;

										_DEBUG_VERBOSE("da_err = %d", da_err);

										if (da_err != SYNC_AGENT_DA_SUCCESS) {
											success = false;
											break;
										}
									}
									if (fw_item_id_list != NULL) {
										free(fw_item_id_list);
									}
									if (id_list != NULL) {
										free(id_list);
									}
								}
							}

							if (success == true) {
								returnResult = COMMAND_RESULT_ADDED;
								add_add_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
							} else {
								/*this case just fail do rollback and goto fail */
								returnResult = ___convert_return_status(da_err);
								da_fail = true;
							}
							sync_agent_free_folder_list(folder_list);

							if (service_id != NULL)
								free(service_id);

						} else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT || da_err == SYNC_AGENT_DA_ERR_ALREADY_EXIST) {
							/*just return error to server */
							returnResult = ___convert_return_status(da_err);
						} else {
							/*this case just fail do rollback and goto fail */
							returnResult = ___convert_return_status(da_err);
							da_fail = true;
						}

						if (service_item != NULL)
							sync_agent_free_service_item(service_item);

						if (fw_id != NULL)
							free(fw_id);
					} else
						returnResult = COMMAND_RESULT_COMMAND_FAIL;

					break;
				}
			case CHANGE_REPLACE:
				{
					if (changedItem->data != NULL) {
						/* update from service DB */
						da_err = sync_agent_create_service_item(&service_item);
						if (da_err != SYNC_AGENT_DA_SUCCESS || (service_item == NULL)) {
							_DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
							returnResult = ___convert_return_status(da_err);
							da_fail = true;
							break;
						}

						service_item->item_id = g_strdup(changedItem->luid);
						service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
						service_item->account_id = account_id;
						service_item->folder_id = g_strdup(folderId);
						service_item->access_name = g_strdup("Engine");
						service_item->data = (const void *)g_strdup(changedItem->data);

						sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);

						da_err = sync_agent_update_service_item(service_item, changedItem->luid, false);

						sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);

						_DEBUG_VERBOSE("da_err = %d", da_err);
						if (da_err == SYNC_AGENT_DA_SUCCESS) {

							GList *item_list = NULL;
							sync_agent_da_get_item_list_query_s query;
							query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
							query.item_id = changedItem->luid;

							da_err = sync_agent_get_item_list(account_id, &query, &item_list);
							if (da_err != SYNC_AGENT_DA_SUCCESS) {
								_DEBUG_ERROR("failed in sync_agent_get_item_list !!");
							}

							bool success = true;
							GList *iter = NULL;
							sync_agent_da_item_s *iter_data;

							for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
								iter_data = NULL;
								iter_data = (sync_agent_da_item_s *) (iter->data);

								da_err = sync_agent_update_item(iter_data->item_id, 1);
								if (da_err != SYNC_AGENT_DA_SUCCESS) {
									success = false;
									break;
								}
							}

							if (success == true) {
								returnResult = COMMAND_RESULT_OK;
								add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
							} else {
								/*this case just fail do rollback and goto fail */
								returnResult = ___convert_return_status(da_err);
								da_fail = true;
							}

							sync_agent_free_item_list(item_list);
						} else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) {
							/*just return error to server */
							returnResult = ___convert_return_status(da_err);
						} else {
							/*this case just fail do rollback and goto fail */
							returnResult = ___convert_return_status(da_err);
							da_fail = true;
						}

						if (service_item != NULL)
							sync_agent_free_service_item(service_item);
					} else
						returnResult = COMMAND_RESULT_COMMAND_FAIL;

					break;
				}
			case CHANGE_DELETE:
				{
					/* delete from service DB */
					GList *item_list = NULL;
					sync_agent_da_get_item_list_query_s query;
					query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
					query.item_id = changedItem->luid;

					da_err = sync_agent_get_item_list(account_id, &query, &item_list);
					if (da_err != SYNC_AGENT_DA_SUCCESS) {
						_DEBUG_ERROR("failed in sync_agent_get_item_list !!");
					}

					sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);

					da_err = sync_agent_delete_service_item(changedItem->luid, false);

					sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);

					_DEBUG_VERBOSE("[sync_agent_delete_service_item] result : %d", da_err);
					if (da_err == SYNC_AGENT_DA_SUCCESS) {
						bool success = true;
						GList *iter = NULL;
						sync_agent_da_item_s *iter_data;

						for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
							iter_data = NULL;
							iter_data = (sync_agent_da_item_s *) (iter->data);

							da_err = sync_agent_delete_item(iter_data->item_id, true);
							if (da_err != SYNC_AGENT_DA_SUCCESS) {
								success = false;
								break;
							}
						}

						if (success == true) {
							returnResult = COMMAND_RESULT_DELETE_WITHOUT_ARCHIVE;
							add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
						} else {
							/*this case just fail do rollback and goto fail */
							returnResult = ___convert_return_status(da_err);
							da_fail = true;
						}
					} else if (da_err == SYNC_AGENT_DA_ERR_NO_DATA || da_err == SYNC_AGENT_DA_ERR_SUB_DATA_EXIST || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) {
						/*just return error to server */
						returnResult = ___convert_return_status(da_err);
					} else {
						/*this case just fail do rollback and goto fail */
						returnResult = ___convert_return_status(da_err);
						da_fail = true;
					}
					sync_agent_free_item_list(item_list);
					break;
				}
			default:
				break;
			}

			if (da_fail == true) {
				/*replace syncresult when rollback happened */
				if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL)
					free(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);

				datastoreinfo_per_content_type[datastoreContentType]->server_sync_result = tempServerSyncResult;
				tempServerSyncResult = NULL;

				_DEBUG_VERBOSE("Transaction_Rollback");
				_DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
//                              sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
				sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);

				err = SE_INTERNAL_DA_ERROR;
				_DEBUG_ERROR("failed in DA");
				goto error;
			}

			appliedStatus = create_applied_status(changedItem->luid, changedItem->change_type, returnResult);
			if (appliedStatus == NULL) {
				_DEBUG_VERBOSE("Transaction_Rollback");
				_DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
//                              sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
				sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);

				err = SE_INTERNAL_NO_MEMORY;
				_DEBUG_ERROR("failed to alloc memory");
				goto error;
			}
			add_sending_status_applied_status(sendingStatus, appliedStatus);
		}

		if (changeItemCount > 0) {
			/*end transaction */
			_DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
//                      sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
			sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
		}

		/* Do not need to send update event to ui when changeItemCount less than 0 */
		/* send update event about add, delete, replace count during this sync command */
		if (changeItemCount >= 0) {
			needToSave = false;
			pServerSyncResult = datastoreinfo_per_content_type[datastoreContentType]->server_sync_result;
			if (pServerSyncResult->number_of_change == pServerSyncResult->received_count) {
				needToSave = true;

				if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL)
					datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->session_result = SYNC_SESSION_SUCCEEDED;
			}

			operation_type_e operation_type;
			if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0)
				operation_type = OPERATION_NOOP;
			else
				operation_type = OPERATION_ADD;
			err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, needToSave, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
			if (err != SE_INTERNAL_OK) {
				_DEBUG_ERROR("failed in process_update");
				goto error;
			}
		}

		(*sync_obj)->sending_status = g_list_append((*sync_obj)->sending_status, sendingStatus);
		sendingStatus = NULL;

		iter = g_list_next(iter);

		(*sync_return_obj)->changed_datastore = g_list_remove((*sync_return_obj)->changed_datastore, pSyncReturnChangedDatastore);
		free_changed_datastore(pSyncReturnChangedDatastore);
		pSyncReturnChangedDatastore = NULL;

		if (tempServerSyncResult != NULL) {
			free(tempServerSyncResult);
			tempServerSyncResult = NULL;
		}
	}

 error:
	sync_agent_free_folder_id_list(f_id_list);

	if (tempServerSyncResult != NULL)
		free(tempServerSyncResult);

	/* for prevent */
	free_sending_status(sendingStatus);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _update_sync_result(int account_id)
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
	int content_type;

	sync_agent_da_last_anchor_s next_anchor_info;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
				continue;

			next_anchor_info.access_name = "Engine";
			next_anchor_info.account_id = account_id;
			next_anchor_info.data_store_id = datastoreinfo_per_content_type[content_type]->datastore_id;
			next_anchor_info.last_anchor_client = datastoreinfo_per_content_type[content_type]->next_anchor_client;
			next_anchor_info.last_anchor_server = datastoreinfo_per_content_type[content_type]->next_anchor_server;

			da_err = sync_agent_update_last_anchor(&next_anchor_info);
			if (da_err != SYNC_AGENT_DA_SUCCESS)
				_DEBUG_ERROR("failed in da_set_last_anchor_internal");

			da_err = sync_agent_set_service_change_point(account_id, datastoreinfo_per_content_type[content_type]->datastore_id);
			if (da_err != SYNC_AGENT_DA_SUCCESS)
				_DEBUG_ERROR("failed in sync_agent_set_service_change_point");
		}
	}

	_INNER_FUNC_EXIT;
	return err;
}

static command_result_e ___convert_return_status(sync_agent_da_return_e da_err)
{
	_INNER_FUNC_ENTER;

	command_result_e returnResult = COMMAND_RESULT_INIT;
	switch (da_err) {
	case 1:		/*SYNC_AGENT_DA_SUCCESS */
		/*never comes this case
		   break; */
	case -813:		/*SYNC_AGENT_DA_ERR_ALREADY_EXIST */
		returnResult = COMMAND_RESULT_ALREADY_EXIST;	/*[A] Already exists exception */
		break;
	case -815:		/*SYNC_AGENT_DA_ERR_MEMORY_FULL *//* oma : 420 (device full exception) */
		returnResult = COMMAND_RESULT_DEVICE_FULL;	/*[AR] Device full exception */
		break;
	case -820:		/*SYNC_AGENT_DA_ERR_NOT_SUPPORTED *//* oma : 415 (unsupported media type or format exception) */
		returnResult = COMMAND_RESULT_UNSUPPORTED_TYPE;	/*[AR] Unsupported media type or format exception */
		break;
	case -819:		/*SYNC_AGENT_DA_ERR_NO_DATA *//* kies : not found, oma : 211 (item not deleted exception) */
		returnResult = COMMAND_RESULT_NOT_EXIST;	/*[D] Item not deleted exception */
		break;
	case -800:		/*SYNC_AGENT_DA_ERRORS */
	case -801:		/*SYNC_AGENT_DA_NOT_FOUND_PLUG_IN */
	case -802:		/*SYNC_AGENT_DA_ERR_OPEN_FAILED */
	case -803:		/*SYNC_AGENT_DA_ERR_CLOSE_FAILED */
	case -804:		/*SYNC_AGENT_DA_ERR_TRANSACTION_FAILED */
	case -805:		/*SYNC_AGENT_DA_ERR_CREATE_TABLE_FAILED */
	case -806:		/*SYNC_AGENT_DA_ERR_DROP_TABLE_FAILED */
	case -807:		/*SYNC_AGENT_DA_ERR_QUERY_FAILED */
	case -808:		/*SYNC_AGENT_DA_ERR_NOT_OPENED */
	case -809:		/*SYNC_AGENT_DA_ERR_ACCOUNT_FULL */
	case -810:		/*SYNC_AGENT_DA_ERR_DELETE_LAST_ACCOUNT */
	case -811:		/*SYNC_AGENT_DA_ERR_PRIMARY_KEY_NOT_UNIQUE */
	case -812:		/*SYNC_AGENT_DA_ERR_DB_HANDLER_MGR */
	case -814:		/*SYNC_AGENT_DA_ERR_INVALID_CONTENT */
	case -816:		/*SYNC_AGENT_DA_ERR_SUB_DATA_EXIST *//* oma : 427 (item not empty) */
	case -817:		/*SYNC_AGENT_DA_ERR_LOCKED *//* kies : cannot access */
	case -818:		/*SYNC_AGENT_DA_ERR_MORE_DATA */
	case -821:		/*SYNC_AGENT_DA_ERR_NOT_EXECUTE *//* kies : ex) sms send command, no network service */
	default:
		returnResult = COMMAND_RESULT_COMMAND_FAIL;	/*[ARD] Command failed exception */
		break;
	}

	_INNER_FUNC_EXIT;
	return returnResult;
}

static char *__convert_cttype_str(int datastore_id)
{
	_INNER_FUNC_ENTER;

	char *cttype = NULL;
	/*FIXME : check type and version (contact : vCard2.1 , calendar : vCalendar 1.0 , memo : plain text) of real item data.. */
	switch (datastore_id) {
	case TYPE_CONTACT:	/*contact (vCard2.1) */
		cttype = ELEMENT_TEXT_VCARD;
		break;
	case TYPE_CALENDAR:	/*calendar (vCalendar2.0) */
		cttype = ELEMENT_TEXT_VCAL;
		break;
	case TYPE_MEMO:	/*note(Memo) */
		cttype = ELEMENT_TEXT_PLAIN;
		break;
	case TYPE_CALLLOG:
		cttype = ELEMENT_TEXT_XCALLLOG;
		break;
	default:
		break;
	}

	_INNER_FUNC_EXIT;
	return cttype;
}

static int ___convert_sync_type_value(char *sync_type_str)
{
	_INNER_FUNC_ENTER;

	int sync_type_value;

	if (strcmp(sync_type_str, DEFINE_ALERT_SLOW_SYNC_STR) == 0)
		sync_type_value = ALERT_SLOW_SYNC;
	else if (strcmp(sync_type_str, DEFINE_ALERT_TWO_WAY_STR) == 0)
		sync_type_value = ALERT_TWO_WAY;
	else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) == 0)
		sync_type_value = ALERT_ONE_WAY_FROM_CLIENT;
	else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) == 0)
		sync_type_value = ALERT_ONE_WAY_FROM_SERVER;
	else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) == 0)
		sync_type_value = ALERT_REFRESH_FROM_SERVER;
	else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) == 0)
		sync_type_value = ALERT_REFRESH_FROM_CLIENT;
	else
		sync_type_value = ALERT_UNKNOWN;

	_INNER_FUNC_EXIT;
	return sync_type_value;
}

static char *__convert_sync_type_str(alert_type_e sync_type)
{
	_INNER_FUNC_ENTER;

	char *sync_Type = NULL;

	switch (sync_type) {
	case ALERT_TWO_WAY:
		sync_Type = DEFINE_ALERT_TWO_WAY_STR;
		break;
	case ALERT_SLOW_SYNC:
		sync_Type = DEFINE_ALERT_SLOW_SYNC_STR;
		break;
	case ALERT_ONE_WAY_FROM_CLIENT:
		sync_Type = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR;
		break;
	case ALERT_REFRESH_FROM_CLIENT:
		sync_Type = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR;
		break;
	case ALERT_ONE_WAY_FROM_SERVER:
		sync_Type = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR;
		break;
	case ALERT_REFRESH_FROM_SERVER:
		sync_Type = DEFINE_ALERT_REFRESH_FROM_SERVER_STR;
		break;
	default:
		sync_Type = DEFINE_ALERT_UNKNOWN_STR;
		break;
	}

	_INNER_FUNC_EXIT;
	return sync_Type;
}

static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status)
{
	_INNER_FUNC_ENTER;

	char *progress = NULL;

	switch (progress_status) {
	case SYNC_PROGRESS_NONE:
		progress = DEFINE_PROGRESS_NONE;
		break;
	case SYNC_PROGRESS_SUCCESS:
		progress = DEFINE_PROGRESS_SUCCESS;
		break;
	case SYNC_FAILED_DB:
		progress = DEFINE_FAILED_DB;
		break;
	case SYNC_FAILED_DB_FORBIDDEN:
		progress = DEFINE_FAILED_DB_FORBIDDEN;
		break;
	case SYNC_FAILED_DB_ITEM:
		progress = DEFINE_FAILED_DB_ITEM;
		break;
	case SYNC_FAILED_DB_CONFIG:
		progress = DEFINE_FAILED_DB_CONFIG;
		break;
	case SYNC_FAILED_DB_DEVICEFULL:
		progress = DEFINE_FAILED_DB_DEVICEFULL;
		break;
	default:
		break;
	}

	_INNER_FUNC_EXIT;
	return progress;
}

static char *___convert_operation_type_str(operation_type_e operation_type)
{
	_INNER_FUNC_ENTER;

	char *operation = NULL;

	switch (operation_type) {
	case OPERATION_NOOP:
		operation = DEFINE_NOOP;
		break;
	case OPERATION_ADD:
		operation = DEFINE_ADD;
		break;
	case OPERATION_DELETE:
		operation = DEFINE_DELETE;
		break;
	case OPERATION_MOVE:
		operation = DEFINE_MOVE;
		break;
	case OPERATION_COPY:
		operation = DEFINE_COPY;
		break;
	case OPERATION_REPLACE:
		operation = DEFINE_REPLACE;
		break;
	default:
		break;
	}

	_INNER_FUNC_EXIT;
	return operation;
}

static char *_convert_sync_progress_str(sync_progress_e process)
{
	_INNER_FUNC_ENTER;

	char *syncProcess = NULL;

	switch (process) {
	case PROGRESS_NONE:
		syncProcess = DEFINE_SYNC_PROGRESS_NONE;
		break;
	case PROGRESS_INIT:
		syncProcess = DEFINE_SYNC_INIT;
		break;
	case PROGRESS_CONNECTING:
		syncProcess = DEFINE_SYNC_CONNECTING;
		break;
	case PROGRESS_AUTHENTICATED:
		syncProcess = DEFINE_SYNC_AUTHENTICATED;
		break;
	case PROGRESS_DONE:
		syncProcess = DEFINE_SYNC_DONE;
		break;
	case PROGRESS_CANCEL:
		syncProcess = DEFINE_SYNC_CANCEL;
		break;
	case PROGRESS_ERROR:
		syncProcess = DEFINE_SYNC_ERROR;
		break;
	default:
		break;
	}

	_INNER_FUNC_EXIT;
	return syncProcess;
}

static char *_convert_sync_error_str(sync_error_e error)
{
	_INNER_FUNC_ENTER;

	char *syncError = NULL;

	switch (error) {
	case ERROR_NONE:
		syncError = DEFINE_ERROR_NONE;
		break;
	case ERROR_CONNECTION:
		syncError = DEFINE_ERROR_CONNECTION;
		break;
	case ERROR_SYNCHDR:
		syncError = DEFINE_ERROR_SYNCHDR;
		break;
	case ERROR_INTERNAL:
		syncError = DEFINE_ERROR_INTERNAL;
		break;
	case ERROR_SUSPENDED:
		syncError = DEFINE_ERROR_SUSPENDED;
		break;
	case ERROR_DB:
		syncError = DEFINE_ERROR_DB;
		break;
	case ERROR_ABORT:
		syncError = DEFINE_ERROR_ABORT;
		break;
	case ERROR_SERVER:
		syncError = DEFINE_ERROR_SERVER;
		break;
	case ERROR_MEMORY_FULL:
		syncError = DEFINE_ERROR_MEMORY_FULL;
		break;
	case ERROR_AUTHENTICATE:
		syncError = DEFINE_ERROR_AUTHENTICATE;
		break;
	case ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:
		syncError = DEFINE_ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
		break;
	case ERROR_LOW_BATTERY:
		syncError = DEFINE_ERROR_LOW_BATTERY;
		break;
	default:
		break;
	}

	_INNER_FUNC_EXIT;
	return syncError;
}

static se_error_type_e _check_low_battery()
{
	_INNER_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_dev_return_e dci_err = SYNC_AGENT_DEV_RETURN_SUCCESS;
	char *battery_level = NULL;
	int int_battery_level = 0;

	dci_err = sync_agent_get_devinfo(1, "Battery", &battery_level);
	if (dci_err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_get_devinfo = %d", dci_err);
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	_DEBUG_TRACE("battery_level =%s", battery_level);

	if (battery_level != NULL) {
		int_battery_level = atoi(battery_level);

		if (int_battery_level < LOW_BATTERY_LEVEL) {
			_DEBUG_ERROR("LOW Battery = %d", int_battery_level);
			err = SE_INTERNAL_LOW_BATTERY;
			goto error;
		}
	}

 error:

	if (battery_level != NULL)
		free(battery_level);

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _open_services()
{
	_EXTERN_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	int content_type;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
				continue;

			da_err = sync_agent_open_service(content_type);
			if (da_err != SYNC_AGENT_DA_SUCCESS) {
				_DEBUG_ERROR("sync_agent_open_service(%d) is failed", content_type);
				err = SE_INTERNAL_DA_ERROR;
				break;
			}
		}
	}

	_INNER_FUNC_EXIT;
	return err;
}

static se_error_type_e _close_services()
{
	_EXTERN_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	int content_type;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
				continue;

			da_err = sync_agent_close_service(content_type);
			if (da_err != SYNC_AGENT_DA_SUCCESS)
				_DEBUG_ERROR("sync_agent_close_service(%d) is failed", content_type);
		}
	}

	_INNER_FUNC_EXIT;
	return err;
}

bool synchronize(int account_id, char *sync_mode, san_package_s * san_package)
{
	_EXTERN_FUNC_ENTER;

	if (account_id < 1) {
		_DEBUG_ERROR("account_id is invalid!!");
		return false;
	}

	_DEBUG_INFO("account_id = %d", account_id);
	_DEBUG_INFO("sync_mode = %s", sync_mode);

	/*FIXME remove msg file */
	int ret_val = remove(OMA_DS_MSG_PATH);
	if (ret_val < 0) {
		_DEBUG_ERROR("failed in synchronize - remove()");
	}

	/*FIXME time check */
	long t, dt;
	t = myclock();

	se_error_type_e err = SE_INTERNAL_OK;
	sync_agent_dev_return_e dci_result = SYNC_AGENT_DEV_RETURN_SUCCESS;
	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	common_error_type_e errorCode = COMMON_OK;
	sync_progress_e process = PROGRESS_NONE;
	sync_error_e error = ERROR_NONE;

	alert_type_e client_sync_type = ALERT_UNKNOWN;
	alert_type_e server_sync_type = ALERT_UNKNOWN;
	int only_from_client = 0;

	int session_time = sync_agent_convert_seconds_to_utc(time(NULL));	/*lastSessionTime for resultView; */
	_DEBUG_INFO("session_time = %d", session_time);

	pre_sync_return_obj_s *pre_sync_return_obj = NULL;
	sync_obj_s *sync_obj = NULL;
	sync_return_obj_s *sync_return_obj = NULL;

	bool cancel_flag = false;
	int content_type;
	char *session_id = NULL;

	char *access_name = NULL;
	bool server_flag = false;

	char *msg = NULL;
	unsigned int msg_size;
	char *recvMsg = NULL;
	unsigned int recvMsg_size;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	sync_agent_fw_account_s *fw_account = NULL;

	dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_lock", 3, LCD_OFF, STAY_CUR_STATE, 0);
	if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_execute_dev_function lock = %d", dci_result);
	}

	/* low battery check */
	err = _check_low_battery();
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("check_low_battery = %d", err);
		goto fail_part;
	}

	da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_open_agent = %d", da_err);
		err = SE_INTERNAL_DA_ERROR;
		goto fail_part;
	}

	/* prepare for sending pre sync */
	err = _prepare_pre_sync(account_id, sync_mode, san_package, &client_sync_type);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __prepare_pre_sync = %d", err);
		goto fail_part;
	}

	/* check cancel flag */
	cancel_flag = sync_agent_check_cancel_flag();
	if (cancel_flag) {
		err = SE_INTERNAL_CANCEL;
		_DEBUG_INFO("cancle flag is on");
		goto cancel_part;
	}

	pre_sync_return_obj = (pre_sync_return_obj_s *) calloc(1, sizeof(pre_sync_return_obj_s));
	if (pre_sync_return_obj == NULL) {
		_DEBUG_ERROR("failed to alloc memory");
		err = SE_INTERNAL_NO_MEMORY;
		goto fail_part;
	}

	acc_err = sync_agent_create_fw_account(&fw_account);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_create_fw_account");
		err = SE_INTERNAL_NO_MEMORY;
		goto fail_part;
	}

	acc_err = sync_agent_get_fw_account(account_id, &fw_account);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_update_fw_account");
		err = SE_INTERNAL_ERROR;
		goto fail_part;
	}

	access_name = strdup(fw_account->access_name);
	if (strcmp(access_name, "DIVE") == 0) {
		server_flag = true;
	}

	/*pkg 1 */
	if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
		_DEBUG_INFO("sessionID = %d", san_package->session_id);
		session_id = g_strdup_printf("%u", san_package->session_id);	/*freed in pre_sync */
		errorCode = pre_sync(TRANSPORT_TYPE, account_id, session_id, server_flag, (void **)&pre_sync_return_obj);
	} else
		errorCode = pre_sync(TRANSPORT_TYPE, account_id, NULL, server_flag, (void **)&pre_sync_return_obj);

	/* check cancel flag */
	cancel_flag = sync_agent_check_cancel_flag();
	if (cancel_flag) {
		err = SE_INTERNAL_CANCEL;
		_DEBUG_INFO("cancle flag is on");
		goto cancel_part;
	}

	_DEBUG_INFO("pre_sync errorCode =[%d]", errorCode);
	if (errorCode != COMMON_OK) {
		err = SE_INTERNAL_SA_ERROR;
		goto fail_part;
	}

	/*execute pre_sync return */
	server_sync_type = client_sync_type;
	err = _execute_pre_sync(account_id, session_time, pre_sync_return_obj, &server_sync_type);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __process_pre_sync = %d", err);
		goto fail_part;
	}

	if (server_sync_type == ALERT_REFRESH_FROM_CLIENT || server_sync_type == ALERT_ONE_WAY_FROM_CLIENT)
		only_from_client = 1;

	_DEBUG_INFO("client_sync_type = %d", client_sync_type);
	_DEBUG_INFO("server_sync_type = %d", server_sync_type);
	_DEBUG_INFO("only_from_client = %d", only_from_client);

	_session_process(account_id, server_sync_type, PROGRESS_AUTHENTICATED, ERROR_NONE);

	/* check cancel flag */
	cancel_flag = sync_agent_check_cancel_flag();
	if (cancel_flag) {
		err = SE_INTERNAL_CANCEL;
		_DEBUG_INFO("cancle flag is on");
		goto cancel_part;
	}

	err = _open_services();
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __prepare_pre_sync = %d", err);
		goto fail_part;
	}

	/* pkg 3 */
	_DEBUG_INFO("pre_sync end pkg3 start");
	sync_obj = (sync_obj_s *) calloc(1, sizeof(sync_obj_s));
	if (sync_obj == NULL) {
		_DEBUG_ERROR("failed to alloc memory");
		err = SE_INTERNAL_NO_MEMORY;
		goto fail_part;
	}

	sync_return_obj = (sync_return_obj_s *) calloc(1, sizeof(sync_return_obj_s));
	if (sync_return_obj == NULL) {
		_DEBUG_ERROR("failed to alloc memory");
		err = SE_INTERNAL_NO_MEMORY;
		goto fail_part;
	}

	err = _assemble_changed_datastores(account_id, server_sync_type, &sync_obj);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __assemble_changeddatastore = %d", err);
		goto fail_part;
	}

	/* check cancel flag */
	cancel_flag = sync_agent_check_cancel_flag();
	if (cancel_flag) {
		err = SE_INTERNAL_CANCEL;
		_DEBUG_INFO("cancle flag is on");
		goto cancel_part;
	}

	int isFinish = 0;
	while (!isFinish) {

		errorCode = generate_msg((void **)&sync_obj, server_flag, &msg, &msg_size);
		if (errorCode != COMMON_OK) {
			_DEBUG_ERROR("Failed in generate_Msg = %d", errorCode);
			err = SE_INTERNAL_SA_ERROR;
			goto fail_part;
		}

		errorCode = exchange_msg(TRANSPORT_TYPE, msg, msg_size, &recvMsg, &recvMsg_size);
		if (errorCode != COMMON_OK) {
			_DEBUG_ERROR("Failed in exchange_Msg = %d", errorCode);
			if (errorCode == COMMON_CANCEL) {
				bool cancel_status = check_cancel_status();
				if (cancel_status) {
					err = SE_INTERNAL_SUSPEND;
					_DEBUG_INFO("cancel_status is on");
					goto suspend_part;
				}
			} else {
				err = SE_INTERNAL_SA_ERROR;
				goto fail_part;
			}
		}

		/* check cancel flag */
		cancel_flag = sync_agent_check_cancel_flag();
		if (cancel_flag) {
			bool cancel_status = check_cancel_status();
			if (cancel_status) {
				err = SE_INTERNAL_SUSPEND;
				_DEBUG_INFO("cancel_status is on");
				goto suspend_part;
			}
		}

		errorCode = process_recv_msg(recvMsg, recvMsg_size, only_from_client, (void **)&sync_return_obj, &isFinish);
		if (errorCode != COMMON_OK) {
			_DEBUG_ERROR("Failed in processRecv_Msg = %d", errorCode);
			err = SE_INTERNAL_SA_ERROR;
			goto fail_part;
		}

		err = _execute_sync(account_id, client_sync_type, server_sync_type, &sync_obj, &sync_return_obj);
		if (err != SE_INTERNAL_OK) {
			_DEBUG_ERROR("failed in __execute_sync = %d", err);
			goto fail_part;
		}

		free_changed_datastores(sync_return_obj->changed_datastore);
		sync_return_obj->changed_datastore = NULL;

		free_applied_statuses(sync_return_obj->status);
		sync_return_obj->status = NULL;

		if (msg != NULL) {
			free(msg);
			msg = NULL;
		}

		if (recvMsg != NULL) {
			free(recvMsg);
			recvMsg = NULL;
		}
	}

	err = _update_sync_result(account_id);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in __update_anchor = %d", err);
		goto fail_part;
	}

	_write_sync_data(account_id, client_sync_type, SYNC_SESSION_SUCCEEDED, session_time, only_from_client);

	goto return_part;

 suspend_part:

	errorCode = suspend_sync(TRANSPORT_TYPE, account_id, server_flag);

	if (errorCode != COMMON_OK) {
		_DEBUG_ERROR("failed in suspend_sync = %d",errorCode);

		err = SE_INTERNAL_SA_ERROR;
		if (errorCode == COMMON_SUSPEND_FAIL)
			_off_resume_flag(account_id);

		goto fail_part;
	}

 cancel_part:

	/*clean up for SA unusual end sync process case */
	clean_up_sa();

	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
				if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
					datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_STOPPED;

				if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
					datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_STOPPED;

			}
		}
	}

	_write_sync_data(account_id, client_sync_type, SYNC_SESSION_STOPPED, session_time, only_from_client);

	goto return_part;

 fail_part:

	/*clean up for SA unusual end sync process case */
	clean_up_sa();

	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
				if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
					datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED;

				if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
					datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED;
			}
		}
	}

	_write_sync_data(account_id, client_sync_type, SYNC_SESSION_FAILED, session_time, only_from_client);

 return_part:

	if (err == SE_INTERNAL_SA_ERROR)
		convert_common_errorcode(errorCode, &process, &error);
	else
		convert_engine_errorcode(err, &process, &error);

	/* off when session finish normal or cancel case(not suspend) */
	if (err != SE_INTERNAL_SUSPEND)
		_off_resume_flag(account_id);

	_off_synchronising_account(account_id);

	_session_process(account_id, server_sync_type, process, error);
	_DEBUG_INFO("server_sync_type = %d", server_sync_type);
	_DEBUG_INFO("process = %d", process);
	_DEBUG_INFO("error = %d", error);

	sync_agent_close_agent();
	_close_services();

	dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_unlock", 2, LCD_OFF, RESET_TIMER);
	if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_execute_dev_function unlock");
	}

	sync_agent_free_fw_account(fw_account);

	if (access_name != NULL) {
		free(access_name);
		access_name = NULL;
	}

	if (msg != NULL) {
		free(msg);
		msg = NULL;
	}

	if (recvMsg != NULL) {
		free(recvMsg);
		recvMsg = NULL;
	}

	if (pre_sync_return_obj != NULL) {
		free_pre_sync_return_obj(pre_sync_return_obj);
		pre_sync_return_obj = NULL;
	}

	if (sync_obj != NULL) {
		free_sync_obj(sync_obj);
		sync_obj = NULL;
	}

	if (sync_return_obj != NULL) {
		free_sync_return_obj(sync_return_obj);
		sync_return_obj = NULL;
	}

	for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
		if (datastoreinfo_per_content_type[content_type] != NULL) {
			free_datastore(datastoreinfo_per_content_type[content_type]);
			datastoreinfo_per_content_type[content_type] = NULL;
		}
	}

	 /*FIXME*/ dt = myclock() - t;
	_DEBUG_ERROR("\n=============TOTAL---- % d . % d sec\n", dt / 1000, dt % 1000);

	_EXTERN_FUNC_EXIT;
	if (err != SE_INTERNAL_OK)
		return false;
	else
		return true;
}

void convert_common_errorcode(common_error_type_e errorCode, sync_progress_e * process, sync_error_e * error)
{
	_EXTERN_FUNC_ENTER;

	switch (errorCode) {
	case COMMON_OK:	/*ERROR_INTERNAL_OK */
		{
			/* Do nothing : Error None
			 * pre_sync : PROGRESS_AUTHENTICATED
			 * sync end : PROGRESS_DONE
			 process = PROGRESS_AUTHENTICATED;
			 process = PROGRESS_DONE;
			 */
			*process = PROGRESS_DONE;
			*error = ERROR_NONE;
		}
		break;
	case COMMON_SUSPEND_FAIL:
	case COMMON_CANCEL:
		{
			*process = PROGRESS_CANCEL;
			*error = ERROR_NONE;
		}
		break;
	case COMMON_MISCONFIGURATION:	/*ERROR_INTERNAL_MISCONFIGURATION : need configure infomation (account_id, id, pw, server_url...) */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_SYNCHDR;
		}
		break;
	case COMMON_AUTHENTICATION_ERROR:	/*ERROR_AUTH_REQUIRED, ERROR_AUTH_REJECTED */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_AUTHENTICATE;
		}
		break;
	case COMMON_NOT_FOUND:	/*ERROR_NOT_FOUND (ERROR_INTERNAL ??) */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_SYNCHDR;
		}
		break;
	case COMMON_NO_MEMORY:	/*ERROR_INTERNAL_NO_MEMORY */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_MEMORY_FULL;
		}
		break;
	case COMMON_INTERNAL_ERROR:	/*ERROR_INTERNAL_NOT_DEFINED || ERROR_INTERNAL_BINDER_ERROR */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_INTERNAL;
		}
		break;
	case COMMON_SERVER_ERROR:	/*ERROR_GENERIC || ERROR_SERVER_FAILURE */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_SERVER;
		}
		break;
	case COMMON_CONNECTION_ERROR:	/*ERROR_INTERNAL_CONNECTION_ERROR */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_CONNECTION;
		}
		break;
	case COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:	/*ERROR_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER */
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
		}
		break;
	default:
		{
			*process = PROGRESS_ERROR;
			*error = ERROR_INTERNAL;	/*?? unknown error */
		}
		break;
	}

	_EXTERN_FUNC_EXIT;
}

void convert_engine_errorcode(se_error_type_e err, sync_progress_e * process, sync_error_e * error)
{
	_EXTERN_FUNC_ENTER;

	switch (err) {
	case SE_INTERNAL_OK:
		*process = PROGRESS_DONE;
		*error = ERROR_NONE;
		break;
	case SE_INTERNAL_SUSPEND:
	case SE_INTERNAL_CANCEL:
		*process = PROGRESS_CANCEL;
		*error = ERROR_NONE;
		break;
	case ERROR_UNKNOWN:
	case SE_INTERNAL_SA_ERROR:
	case SE_INTERNAL_SCHEDULER_ERROR:
	case SE_INTERNAL_ENGINE_CONTROLER_ERROR:
	case SE_INTERNAL_EVENT_ERROR:
	case SE_INTERNAL_NOT_DEFINED:
	case SE_INTERNAL_ERROR:
		*process = PROGRESS_ERROR;
		*error = ERROR_INTERNAL;
		break;
	case SE_INTERNAL_NO_MEMORY:
		*process = PROGRESS_ERROR;
		*error = ERROR_MEMORY_FULL;
		break;
	case SE_INTERNAL_DA_ERROR:
		*process = PROGRESS_ERROR;
		*error = ERROR_DB;
		break;
	case SE_INTERNAL_MISCONFIGURATION:
		*process = PROGRESS_ERROR;
		*error = ERROR_SYNCHDR;
		break;
	case SE_INTERNAL_LOW_BATTERY:
		*process = PROGRESS_ERROR;
		*error = ERROR_LOW_BATTERY;
		break;
	}

	_EXTERN_FUNC_EXIT;
}

se_error_type_e session_process(char *profileDirName, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error)
{
	_EXTERN_FUNC_ENTER;

	_DEBUG_INFO("profileDirName = %s", profileDirName);
	_DEBUG_INFO("process = %d", process);
	_DEBUG_INFO("error = %d", error);

	se_error_type_e err = SE_INTERNAL_OK;

	char *sync_type = NULL;
	char *syncProcess = NULL;
	char *syncError = NULL;
	int sync_type_value = 0;

	sync_type = __convert_sync_type_str(server_sync_type);
	sync_type_value = __convert_alert_to_sync_type_value(server_sync_type);
	_DEBUG_INFO("sync_type_value: [%d]", sync_type_value);

	syncProcess = _convert_sync_progress_str(process);

	syncError = _convert_sync_error_str(error);

	if (sync_type == NULL || syncProcess == NULL || syncError == NULL) {
		err = SE_INTERNAL_NOT_DEFINED;
		goto error;
	}

	err = send_noti_session_process(profileDirName, sync_type_value, syncProcess, syncError);
	if (err != SE_INTERNAL_OK) {
		_DEBUG_ERROR("failed in send_noti_session_process");
		goto error;
	}

	_EXTERN_FUNC_EXIT;

 error:

	return err;
}

se_error_type_e reset_synchronizing_profiles()
{
	_EXTERN_FUNC_ENTER;

	se_error_type_e err = SE_INTERNAL_OK;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	sync_agent_fw_account_s *fw_account = NULL;
	GList *account_info_list = NULL;
	GList *iter = NULL;

	sync_agent_da_return_e da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_open_agent");
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	sync_agent_fw_account_query_s query;
	query.query = ACCOUNT_QUERY_BY_NONE;

	acc_err = sync_agent_query_fw_account(&query, &account_info_list);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("sync_agent_query_fw_account is failed");
		goto error;
	}

	for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
		fw_account = (sync_agent_fw_account_s *) iter->data;

		_DEBUG_INFO("account = %d", fw_account->account_id);
		_off_synchronising_account(fw_account->account_id);

		_DEBUG_INFO("construct_itemTbl From service start");

		int content_type;
		for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) {

			da_err = sync_agent_open_service(content_type);
			if (da_err != SYNC_AGENT_DA_SUCCESS) {
				_DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type);
				continue;
			}

			_DEBUG_INFO("sync_agent_construct_item_tbl_from_service(%d, %d);", fw_account->account_id, content_type);
			sync_agent_construct_item_tbl_from_service(fw_account->account_id, content_type);

			da_err = sync_agent_close_service(content_type);
			if (da_err != SYNC_AGENT_DA_SUCCESS) {
				_DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type);
				continue;
			}
		}

		_DEBUG_INFO("construct_itemTbl From service end");
	}

 error:

	sync_agent_close_agent();

	sync_agent_free_fw_account_list(account_info_list);

	_EXTERN_FUNC_EXIT;

	return err;
}

bool refresh_from_service_all(int account_id)
{
	_EXTERN_FUNC_ENTER;

	if (account_id < 1) {
		_DEBUG_ERROR("account_id is invalid!!");
		return false;
	} else {
		_DEBUG_INFO("account_id: [%d]", account_id);
	}

	se_error_type_e err = SE_INTERNAL_OK;

	sync_agent_da_return_e da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		err = SE_INTERNAL_DA_ERROR;
		goto error;
	}

	int content_type;
	for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) {

		da_err = sync_agent_open_service(content_type);
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type);
			continue;
		}

		_DEBUG_INFO("sync_agent_refresh_item_tbl_from_service(%d, %d);", account_id, content_type);
		sync_agent_refresh_item_tbl_from_service(account_id, content_type);

		da_err = sync_agent_close_service(content_type);
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type);
			continue;
		}
	}

 error:

	sync_agent_close_agent();

	_EXTERN_FUNC_EXIT;

	if (err != SE_INTERNAL_OK)
		return false;
	else
		return true;
}

se_error_type_e cancel_sync_request()
{
	_EXTERN_FUNC_ENTER;
	se_error_type_e err = SE_INTERNAL_OK;

	cancel_connection_sync_request(TRANSPORT_TYPE);

	_EXTERN_FUNC_EXIT;

	return err;
}