4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JuHak Park <juhaki.park@samsung.com>,
7 * JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8 * SunBong Ha <sunbong.ha@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
28 * For any sort of issue you concern as to this software,
29 * you may use following point of contact.
30 * All resources contributed on this software
31 * are orinigally written by S-Core Inc., a member of Samsung Group.
33 * SeongWon Shim <seongwon.shim@samsung.com>
39 * @brief This file is the source file of implementation of functions which process sync request, auto configure etc..
44 #include "agent-framework/DACI/DACI_Agent_Handler_Manager.h"
45 #include "agent-framework/DACI/DACI_Agent_Mgr.h"
46 #include "agent-framework/DACI/DACI_Service.h"
47 #include "agent-framework/DACI/DACI_Luid.h"
48 #include "agent-framework/DACI/DACI_ChangeLog.h"
49 #include "agent-framework/SyncAgent_Info.h"
50 #include "agent-framework/Utility/fw_log.h"
51 #include "agent-framework/EngineController/task_process.h"
52 #include "agent-framework/Utility/fw_time.h"
53 #include "agent-framework/Device/EXT_DCI_DevExecutor.h"
54 #include "ServiceEngine/SE_Sync.h"
55 #include "ServiceEngine/SE_Storage.h"
56 #include "ServiceEngine/SE_Common.h"
57 #include "ServiceEngine/SE_Notification.h"
58 #include "ServiceAdapter/SA_Common_Interface.h"
59 #include "Common/Common_Util.h"
60 #include "Common/Common_Define_Internal.h"
63 #define LOG_TAG "OMA_DS_SE"
65 static SE_ErrorType __session_process(int accountId, SyncProgress process, SyncError error);
66 static SE_ErrorType __process_update(int accountId, SyncProgressStatus progressStatus,
67 OperationType operationType, int content_type, bool isFromServer, bool needToSave, SyncResult *syncResult);
68 static SE_ErrorType __write_sync_data(int accountId, AlertType alertType, SyncSessionResult syncSessionResult, int lastSessionTime);
69 static SE_ErrorType __write_sync_resource_info(int accountId, AlertType alertType, int content_type, int lastSessionTime);
71 static SE_ErrorType __init_datastore_info_array(int accountId);
72 static SE_ErrorType __set_datastore_info_array(int accountId, char *config_key, CONTENT_TYPE content_type);
73 static SE_ErrorType __generate_datastore_info(int accountId, CONTENT_TYPE content_type, Datastore **pDatastore);
74 static SE_ErrorType __init_datastore_info(int accountId, CONTENT_TYPE content_type, Datastore **pDatastore);
75 static SE_ErrorType __set_datastore_config(int accountId, int content_type, Datastore **pDatastore);
77 static SE_ErrorType __on_synchronising_account(int accountId);
78 static SE_ErrorType __off_synchronising_account(int accountId);
80 static SE_ErrorType __assemble_changeddatastores(int accountId, SyncObj **pSyncObj);
81 static SE_ErrorType __prepare_pre_sync(int accountId, char *syncMode, SanPackage *pSanPackage);
82 static SE_ErrorType __set_config_based_on_syncMode(int accountId, char *syncMode, SanPackage *pSanPackage);
83 static SE_ErrorType __execute_pre_sync(int accountId, PreSyncReturnObj *pPreSyncReturnObj, int sessionTime);
84 static SE_ErrorType __execute_pre_sync_set_server_id(int accountId, char *devID);
85 static SE_ErrorType __execute_pre_sync_datastore(int accountId, GList *datastoreInfo, int sessionTime);
86 static SE_ErrorType __execute_sync(int accountId, SyncObj **pSyncObj, SyncReturnObj **SyncReturnObj);
87 static SE_ErrorType __execute_sync_arrange_changelog(int accountId, GList *status);
88 static SE_ErrorType __execute_sync_status(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj);
89 static SE_ErrorType __execute_sync_change(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj);
90 static SE_ErrorType __update_sync_result(int accountId);
92 static CommandResult __convert_return_status(DACI_RETURN da_err);
93 static char *__convert_cttype(int itemTypeId);
94 static int __convert_synctype_value(char *syncType_str);
96 static inline long myclock()
100 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
103 static SE_ErrorType __session_process(int accountId, SyncProgress process, SyncError error)
107 char *profileDirName = NULL;
108 SE_ErrorType err = SE_INTERNAL_OK;
111 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
112 if (result == false) {
113 FW_LOGE("failed in get_Config");
114 err = SE_INTERNAL_DA_ERROR;
118 if (profileDirName != NULL ){
119 err = session_process(profileDirName, process, error);
120 if (err != SE_INTERNAL_OK) {
121 FW_LOGE("failed to send noti");
130 free(profileDirName);
135 static SE_ErrorType __process_update(int accountId, SyncProgressStatus progressStatus,
136 OperationType operationType, int content_type, bool isFromServer, bool needToSave, SyncResult *syncResult)
138 FW_LOGV("start with accountId =%d, progressStatus = %d, operationType = %d, content_type = %d, isFromServer = %d, needToSave = %d ",
139 accountId, progressStatus, operationType, content_type, isFromServer, needToSave);
140 FW_LOGV("numberOfChange = %d, received_count = %d, syncCount = %d" ,
141 syncResult->numberOfChange, syncResult->received_count, syncResult->add_count + syncResult->replace_count + syncResult->delete_count);
143 SE_ErrorType err = SE_INTERNAL_OK;
144 char *profileDirName = NULL;
145 char *profile = NULL;
146 char *progressStatus_str = NULL;
147 char *operationType_str = NULL;
151 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
152 if (result == false) {
153 FW_LOGE("failed in get_Config");
154 err = SE_INTERNAL_DA_ERROR;
158 /* do not need to send update noti to ui */
159 if(profileDirName == NULL){
160 FW_LOGV("profileDirName is NULL");
164 if (needToSave == true) {
165 err = write_sync_statistics(accountId, content_type, isFromServer, syncResult);
166 if (err != SE_INTERNAL_OK) {
167 FW_LOGE("failed in writeSyncStatistics");
171 syncResult->sessionResult = SYNC_SESSION_SUCCEEDED;
174 char *progress = NULL;
175 switch (progressStatus) {
176 case SYNC_PROGRESS_NONE:
177 progress = DEFINE_PROGRESS_NONE;
179 case SYNC_PROGRESS_SUCCESS:
180 progress = DEFINE_PROGRESS_SUCCESS;
183 progress = DEFINE_FAILED_DB;
185 case SYNC_FAILED_DB_FORBIDDEN:
186 progress = DEFINE_FAILED_DB_FORBIDDEN;
188 case SYNC_FAILED_DB_ITEM:
189 progress = DEFINE_FAILED_DB_ITEM;
191 case SYNC_FAILED_DB_CONFIG:
192 progress = DEFINE_FAILED_DB_CONFIG;
194 case SYNC_FAILED_DB_DEVICEFULL:
195 progress = DEFINE_FAILED_DB_DEVICEFULL;
201 char *operation = NULL;
202 switch (operationType) {
204 operation = DEFINE_NOOP;
207 operation = DEFINE_ADD;
209 case OPERATION_DELETE:
210 operation = DEFINE_DELETE;
213 operation = DEFINE_MOVE;
216 operation = DEFINE_COPY;
218 case OPERATION_REPLACE:
219 operation = DEFINE_REPLACE;
225 if (progress == NULL || operation == NULL) {
226 err = SE_INTERNAL_NOT_DEFINED;
230 if (profileDirName != NULL)
231 profile = strdup(profileDirName);
233 progressStatus_str = strdup(progress);
234 operationType_str = strdup(operation);
236 if (datastoreinfo_per_content_type[content_type]->source != NULL)
237 uri = strdup(datastoreinfo_per_content_type[content_type]->source);
239 err = send_noti_process_update(profile, uri , progressStatus_str, operationType_str,
240 isFromServer, 0, 0, syncResult->numberOfChange,
241 syncResult->add_count + syncResult->replace_count + syncResult->delete_count);
242 if (err != SE_INTERNAL_OK) {
243 FW_LOGE("failed in send_noti_process_update");
251 if (profileDirName != NULL)
252 free(profileDirName);
257 if (progressStatus_str != NULL)
258 free(progressStatus_str);
260 if (operationType_str != NULL)
261 free(operationType_str);
269 static SE_ErrorType __write_sync_data(int accountId, AlertType alertType, SyncSessionResult syncSessionResult, int lastSessionTime)
273 SE_ErrorType err = SE_INTERNAL_OK;
275 err = write_profile_data(accountId, alertType, syncSessionResult, lastSessionTime);
276 if (err != SE_INTERNAL_OK) {
277 FW_LOGE("failed in writeProfileData");
289 static SE_ErrorType __write_sync_resource_info(int accountId, AlertType alertType, int content_type , int lastSessionTime)
293 SE_ErrorType err = SE_INTERNAL_OK;
295 err = write_sync_resource_info(accountId, alertType, content_type , lastSessionTime,
296 datastoreinfo_per_content_type[content_type]->clientSyncResult, datastoreinfo_per_content_type[content_type]->serverSyncResult);
297 if (err != SE_INTERNAL_OK) {
298 FW_LOGE("failed in write_sync_resource_info");
309 static SE_ErrorType __init_datastore_info_array(int accountId)
313 SE_ErrorType err = SE_INTERNAL_OK;
315 err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, TYPE_CONTACT);
316 if (err != SE_INTERNAL_OK) {
317 FW_LOGE("failed in set_datastoreinfo_array");
321 err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, TYPE_CALENDAR);
322 if (err != SE_INTERNAL_OK) {
323 FW_LOGE("failed in set_datastoreinfo_array");
327 err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, TYPE_MEMO);
328 if (err != SE_INTERNAL_OK) {
329 FW_LOGE("failed in set_datastoreinfo_array");
333 err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, TYPE_CALLLOG);
334 if (err != SE_INTERNAL_OK) {
335 FW_LOGE("failed in set_datastoreinfo_array");
347 static SE_ErrorType __set_datastore_info_array(int accountId, char *config_key, CONTENT_TYPE content_type)
351 SE_ErrorType err = SE_INTERNAL_OK;
353 Datastore *pDatastoreInfo = NULL;
357 result = get_config(accountId, config_key, &value);
358 if (result == true) {
359 if (strcmp(value, "1") == 0)
360 err = __generate_datastore_info(accountId, content_type, &pDatastoreInfo);
362 err = __init_datastore_info(accountId, content_type, &pDatastoreInfo);
364 err = __init_datastore_info(accountId, content_type , &pDatastoreInfo);
366 if (err != SE_INTERNAL_OK) {
367 FW_LOGE("failed to create or init datastore_info");
371 datastoreinfo_per_content_type[content_type] = pDatastoreInfo;
383 static SE_ErrorType __generate_datastore_info(int accountId, CONTENT_TYPE content_type , Datastore **pDatastore)
385 FW_LOGV("start content_type=[%d]\n", content_type);
387 SE_ErrorType err = SE_INTERNAL_OK;
388 char *clientLastAnchor = NULL;
389 char *clientNextAnchor = NULL;
390 char *serverLastAnchor = NULL;
392 daci_last_anchor *last_anchor_info = NULL;
395 SyncResult *pSyncResult = NULL;
396 Datastore *datastore = NULL;
397 err = __set_datastore_config(accountId, content_type, &datastore);
398 if (err != SE_INTERNAL_OK) {
399 FW_LOGE("failed in set_datastore_config");
403 /* Must get anchor per datastore for synchronization (contact, calendar, memo)
404 * daci_last_anchor *DACI_Get_Last_Anchor_By_Account_Id(int account_id, int *count);
407 last_anchor_info = DACI_Get_Last_Anchor_By_Item_Type_Id(accountId, datastore->datastore_id, &count);
409 if (last_anchor_info != NULL) {
410 clientLastAnchor = last_anchor_info[0].last_anchor_client;
411 serverLastAnchor = last_anchor_info[0].last_anchor_server;
413 daci_last_anchor lastAnchor_daci;
415 lastAnchor_daci.account_id = accountId;
416 lastAnchor_daci.data_store_id = datastore->datastore_id;
417 lastAnchor_daci.last_anchor_server = NULL;
418 lastAnchor_daci.last_anchor_client = NULL;
419 lastAnchor_daci.access_name = "Engine";
421 da_err = DACI_Add_Last_Anchor(&lastAnchor_daci);
422 if (da_err != DACI_SUCCESS) {
423 FW_LOGE("DACI_Add_Last_Anchor is failed %d", da_err);
424 err = SE_INTERNAL_DA_ERROR;
429 clientNextAnchor = g_strdup_printf("%ld", time(NULL));
431 FW_LOGV("clientLastAnchor = %s\n", clientLastAnchor);
432 FW_LOGV("serverLastAnchor = %s\n", serverLastAnchor);
433 FW_LOGV("clientNextAnchor = %s\n", clientNextAnchor);
435 /*slow sync if managed anchor info not exist.. (first sync)*/
436 if (clientLastAnchor == NULL)
437 set_datastore_client_anchor(datastore, clientNextAnchor, clientNextAnchor);
439 set_datastore_client_anchor(datastore, clientLastAnchor, clientNextAnchor);
441 set_datastore_server_anchor(datastore, serverLastAnchor, NULL);
443 pSyncResult = create_sync_result();
444 if (pSyncResult == NULL) {
445 FW_LOGE("create_syncResult is failed");
446 err = SE_INTERNAL_NO_MEMORY;
449 datastore->clientSyncResult = pSyncResult;
451 pSyncResult = create_sync_result();
452 if (pSyncResult == NULL) {
453 FW_LOGE("create_syncResult is failed");
454 err = SE_INTERNAL_NO_MEMORY;
457 datastore->serverSyncResult = pSyncResult;
459 *pDatastore = datastore;
465 if (clientNextAnchor != NULL)
466 free(clientNextAnchor);
468 if (last_anchor_info != NULL)
469 DACI_Free_Memory_Last_Anchor(last_anchor_info, count);
471 if (err != SE_INTERNAL_OK) {
472 if (datastore != NULL) {
473 free_datastore(datastore);
481 static SE_ErrorType __init_datastore_info(int accountId, CONTENT_TYPE content_type , Datastore **pDatastore)
485 SE_ErrorType err = SE_INTERNAL_OK;
486 char *sourceDatastore = NULL;
488 Datastore *datastore = NULL;
489 SyncResult *pSyncResult = NULL;
492 switch (content_type) {
494 sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
497 sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
500 sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
503 sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
506 FW_LOGV("unknown content type = %d", content_type);
510 result = get_config(accountId, key, &sourceDatastore);
511 if (result == false) {
512 FW_LOGE("failed in get_Config");
513 err = SE_INTERNAL_DA_ERROR;
517 datastore = create_datastore(NULL, sourceDatastore);
518 if (datastore == NULL) {
519 FW_LOGE("failed to create_Datastore");
520 err = SE_INTERNAL_NO_MEMORY;
524 pSyncResult = create_sync_result();
525 if (pSyncResult == NULL) {
526 FW_LOGE("create_syncResult is failed");
527 err = SE_INTERNAL_NO_MEMORY;
530 datastore->clientSyncResult = pSyncResult;
532 pSyncResult = create_sync_result();
533 if (pSyncResult == NULL) {
534 FW_LOGE("create_syncResult is failed");
535 err = SE_INTERNAL_NO_MEMORY;
538 datastore->serverSyncResult = pSyncResult;
540 *pDatastore = datastore;
545 if (sourceDatastore != NULL)
546 free(sourceDatastore);
548 if (err != SE_INTERNAL_OK) {
549 if (datastore != NULL) {
550 free_datastore(datastore);
558 static SE_ErrorType __set_datastore_config(int accountId, int content_type, Datastore **pDatastore)
562 Datastore *datastore = NULL;
563 SE_ErrorType err = SE_INTERNAL_OK;
565 char *contentType = NULL;
567 char sourceDatastore_key[128];
568 char targetDatastore_key[128];
572 char *sourceDatastore = NULL;
573 char *targetDatastore = NULL;
574 char *sync_type = NULL;
578 int folder_type_id = 0;
579 switch (content_type) {
583 contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS;
589 contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR;
595 contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO;
601 contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG;
605 FW_LOGV("unknown content type = %d", content_type);
606 err = SE_INTERNAL_ERROR;
610 sprintf(sourceDatastore_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
611 sprintf(targetDatastore_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET);
612 sprintf(id_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID);
613 sprintf(pw_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD);
616 result = get_config(accountId, sourceDatastore_key, &sourceDatastore);
617 if (result == false) {
618 FW_LOGE("failed in get_Config");
619 err = SE_INTERNAL_DA_ERROR;
623 result = get_config(accountId, targetDatastore_key, &targetDatastore);
624 if (result == false) {
625 FW_LOGE("failed in get_Config");
626 err = SE_INTERNAL_DA_ERROR;
630 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNC_TYPE, &sync_type);
631 if (result == false) {
632 FW_LOGE("failed in get_Config");
633 err = SE_INTERNAL_DA_ERROR;
637 syncType = __convert_synctype_value(sync_type);
639 result = get_config(accountId, id_key, &id);
640 if (result == false) {
641 FW_LOGE("failed in get_Config");
642 err = SE_INTERNAL_DA_ERROR;
646 result = get_config(accountId, pw_key, &pw);
647 if (result == false) {
648 FW_LOGE("failed in get_Config");
649 err = SE_INTERNAL_DA_ERROR;
653 FW_LOGV("get_Config result sourceDatastore= %s ", sourceDatastore);
654 FW_LOGV("get_Config result targetDatastore= %s ", targetDatastore);
655 FW_LOGV("get_Config result alertType= %s ", sync_type);
656 FW_LOGV("get_Config result id= %s ", id);
657 FW_LOGV("get_Config result pw= %s ", pw);
659 datastore = create_datastore(targetDatastore, sourceDatastore);
660 if (datastore == NULL) {
661 FW_LOGE("datastore is NULL");
665 set_datastore_contenttype_info(datastore, content_type, folder_type_id);
666 set_datastore_account_info(datastore, id, pw);
667 set_datastore_client_sync_type(datastore, syncType);
669 *pDatastore = datastore;
675 if (sourceDatastore != NULL)
676 free(sourceDatastore);
678 if (targetDatastore != NULL)
679 free(targetDatastore);
681 if (sync_type != NULL)
690 if (err != SE_INTERNAL_OK) {
691 if (datastore != NULL) {
692 free_datastore(datastore);
700 static SE_ErrorType __on_synchronising_account(int accountId)
704 SE_ErrorType err = SE_INTERNAL_OK;
706 bool result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "1", "string", "SE");
707 if (result == false) {
708 FW_LOGE("failed in set_Config");
709 err = SE_INTERNAL_DA_ERROR;
718 FW_LOGE("Error :%d ", err);
722 static SE_ErrorType __off_synchronising_account(int accountId)
726 SE_ErrorType err = SE_INTERNAL_OK;
728 bool result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "string", "SE");
729 if (result == false) {
730 FW_LOGE("failed in set_Config");
731 err = SE_INTERNAL_DA_ERROR;
740 FW_LOGE("Error :%d ", err);
744 static SE_ErrorType __assemble_changeddatastores(int accountId, SyncObj **pSyncObj)
747 SE_ErrorType err = SE_INTERNAL_OK;
748 ChangedDatastore *pChangedDatastore = NULL;
749 DACI_RETURN da_err = DACI_SUCCESS;
752 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
754 if (!datastoreinfo_per_content_type[content_type]->clientSyncType) continue;
756 if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_SLOW_SYNC
757 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_CLIENT
758 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_CLIENT_BY_SERVER) {
760 if( content_type == TYPE_CALLLOG ){
761 FW_LOGV("refresh_Item_Tbl_From_Service for call log");
762 refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
765 /*all item using accountId*/
766 daci_item *pDaci_item = DACI_Get_item_By_Account_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id, &count);
768 pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
769 datastoreinfo_per_content_type[content_type]->target, 1 , count);
770 if (pChangedDatastore == NULL) {
771 DACI_Free_Memory_Item(pDaci_item, count);
772 FW_LOGE("failed in create_ChangedDatastore");
773 err = SE_INTERNAL_NO_MEMORY;
779 for (i = 0; i < count; i++) {
780 char *cttype = __convert_cttype(pDaci_item[i].data_store_id);
781 ChangedItem *pChanged = create_changeditem(CHANGE_ADD, pDaci_item[i].item_id);
782 if (pChanged == NULL) {
784 DACI_Free_Memory_Item(pDaci_item, count);
785 FW_LOGE("failed in create_ChangedDatastore");
786 err = SE_INTERNAL_NO_MEMORY;
790 set_changeditem_contenttype(pChanged, cttype);
791 set_changedItem_indexofdatastore(pChanged, content_type);
793 add_changeddatastore_changeditem(pChangedDatastore, pChanged);
796 DACI_Free_Memory_Item(pDaci_item, count);
798 } else if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_TWO_WAY
799 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_TWO_WAY_BY_SERVER
800 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_CLIENT
801 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
802 FW_LOGV("server sync type is ALERT_TYPE [%d]", datastoreinfo_per_content_type[content_type]->serverSyncType);
805 daci_item_info *pDaci_item_info = DACI_Get_Item_Info_By_Item_Type_Id(accountId,
806 datastoreinfo_per_content_type[content_type]->datastore_id, &count);
808 pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
809 datastoreinfo_per_content_type[content_type]->target, 1, count);
810 if (pChangedDatastore == NULL) {
811 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
812 FW_LOGE("failed in create_ChangedDatastore");
813 err = SE_INTERNAL_NO_MEMORY;
817 /*gathering changelog info*/
819 /*begin agent db transaction*/
820 DACI_Begin_Transaction();
822 char *cttype = __convert_cttype(datastoreinfo_per_content_type[content_type]->datastore_id);
825 for (i = 0; i < count; i++) {
826 ChangedItem *pChanged = create_changeditem(pDaci_item_info[i].operationId - 300, pDaci_item_info[i].itemId);
827 if (pChanged == NULL) {
828 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
829 DACI_End_Transaction(TRANSACTION_ROLLBACK);
830 FW_LOGE("failed in create_ChangedDatastore");
831 err = SE_INTERNAL_NO_MEMORY;
835 set_changeditem_contenttype(pChanged, cttype);
836 set_changedItem_indexofdatastore(pChanged, content_type);
837 add_changeddatastore_changeditem(pChangedDatastore, pChanged);
840 int *itemTypeIdList = (int *)calloc(1, sizeof(int));
841 itemTypeIdList[0] = datastoreinfo_per_content_type[content_type]->datastore_id;
843 da_err = DACI_Set_Item_Changelog_Wait_Status(accountId, NULL, 0, itemTypeIdList, 1);
844 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
845 if (itemTypeIdList != NULL)
846 free(itemTypeIdList);
847 if (da_err != DACI_SUCCESS) {
848 DACI_End_Transaction(TRANSACTION_ROLLBACK);
849 FW_LOGE("failed in DACI_Set_Item_Changelog_Wait_Status");
850 err = SE_INTERNAL_DA_ERROR;
854 DACI_End_Transaction(TRANSACTION_COMMIT);
856 } else if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_SERVER
857 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_SERVER_BY_SERVER
858 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER
859 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
860 /*MUST create empty changedDatastore..*/
861 pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
862 datastoreinfo_per_content_type[content_type]->target, 1, 0);
863 if (pChangedDatastore == NULL) {
864 FW_LOGE("pChangedDatastore is NULL");
865 err = SE_INTERNAL_NO_MEMORY;
869 if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER
870 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
872 /*FW_LOGV("refresh_Item_Tbl_From_Service");
873 refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->plugin_type);*/
875 /* Delete All item (include changelog), before adapting server item data…*/
876 FW_LOGV("DACI_Service_Begin = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
877 DACI_Service_Begin(datastoreinfo_per_content_type[content_type]->datastore_id);
878 DACI_Begin_Transaction();
880 /*delete service item data*/
881 da_err = DACI_Service_Delete_All_Items(datastoreinfo_per_content_type[content_type]->datastore_id, accountId);
882 if (da_err != DACI_SUCCESS) {
884 DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
885 DACI_End_Transaction(TRANSACTION_ROLLBACK);
887 FW_LOGE("failed in DACI_Service_Delete_All_Items");
888 err = SE_INTERNAL_DA_ERROR;
893 FW_LOGV("DACI_Get_item_By_Account_Id");
894 /*get all item by account id*/
895 daci_item *pDaci_item = DACI_Get_item_By_Account_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id, &item_cnt);
896 FW_LOGV("item_cnt = %d", item_cnt);
900 for (j = 0; j < item_cnt ; j++) {
901 daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, pDaci_item[j].item_id, &acc_cnt);
902 FW_LOGV("acc_cnt = %d", acc_cnt);
905 for (i = 0; i < acc_cnt; i++)
906 ChangeLog_Delete_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
907 datastoreinfo_per_content_type[content_type]->datastore_id);
909 DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
912 /*delete item from item_tbl*/
913 FW_LOGV("DACI_Delete_Item_By_Item_Type_Id = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
914 da_err = DACI_Delete_Item_By_Item_Type_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
915 if (da_err != DACI_SUCCESS) {
917 DACI_Free_Memory_Item(pDaci_item, item_cnt);
919 DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
920 DACI_End_Transaction(TRANSACTION_ROLLBACK);
922 FW_LOGE("failed in DACI_Delete_Item_By_Item_Type_Id");
923 err = SE_INTERNAL_DA_ERROR;
927 /*delete changelog data bu account id*/
928 FW_LOGV("DACI_Delete_Item_Changelog_By_Item_Type_Id = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
929 da_err = DACI_Delete_Item_Changelog_By_Item_Type_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
930 if (da_err != DACI_SUCCESS) {
932 DACI_Free_Memory_Item(pDaci_item, item_cnt);
934 DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
935 DACI_End_Transaction(TRANSACTION_ROLLBACK);
937 FW_LOGE("failed in DACI_Delete_Item_Changelog_By_Item_Type_Id");
938 err = SE_INTERNAL_DA_ERROR;
942 FW_LOGV("DACI_Service_End = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
943 DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 1);
944 DACI_End_Transaction(TRANSACTION_COMMIT);
946 DACI_Free_Memory_Item(pDaci_item, item_cnt);
949 if (datastoreinfo_per_content_type[content_type]->clientSyncType) {
950 (*pSyncObj)->changedDatastore = g_list_append((*pSyncObj)->changedDatastore, pChangedDatastore);
952 set_numberofchange(datastoreinfo_per_content_type[content_type]->clientSyncResult, pChangedDatastore->numberOfChanges);
954 OperationType operationType;
955 if (datastoreinfo_per_content_type[content_type]->clientSyncResult->numberOfChange == 0)
956 operationType = OPERATION_NOOP;
958 operationType = OPERATION_ADD;
959 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, content_type, false, false,
960 datastoreinfo_per_content_type[content_type]->clientSyncResult);
961 if (err != SE_INTERNAL_OK) {
962 FW_LOGE("Failed in process_update");
974 if (pChangedDatastore != NULL) {
975 free_changeddatastore(pChangedDatastore);
976 pChangedDatastore = NULL;
982 static SE_ErrorType __prepare_pre_sync(int accountId, char *syncMode, SanPackage *pSanPackage)
985 SE_ErrorType err = SE_INTERNAL_OK;
987 /*set synchronising flag into agent DB*/
988 err = __on_synchronising_account(accountId);
989 if (err != SE_INTERNAL_OK) {
990 FW_LOGE("failed in set_synchronising_account");
994 /*init datastore_info_array*/
995 err = __init_datastore_info_array(accountId);
996 if (err != SE_INTERNAL_OK) {
997 FW_LOGE("failed in init_datastoreInfo_array");
1001 /*set config based on sync mode*/
1002 err = __set_config_based_on_syncMode(accountId, syncMode, pSanPackage);
1003 if (err != SE_INTERNAL_OK) {
1004 FW_LOGE("failed in set_synchronising_account");
1012 FW_LOGE("err = %d", err);
1016 static SE_ErrorType __set_config_based_on_syncMode(int accountId, char *syncMode, SanPackage *pSanPackage)
1019 SE_ErrorType err = SE_INTERNAL_OK;
1023 if (strcmp(syncMode, DEFINE_SYNC_MODE_MANUAL) != 0) {
1024 char *profileDirName = NULL;
1026 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
1027 if (result == false) {
1028 FW_LOGE("failed in get_Config");
1029 err = SE_INTERNAL_DA_ERROR;
1033 if (profileDirName != NULL) {
1034 err = send_noti_sync_status(profileDirName);
1035 if (profileDirName != NULL)
1036 free(profileDirName);
1038 if (err != SE_INTERNAL_OK) {
1039 FW_LOGE("failed in send_noti_sync_status");
1045 if (strcmp(syncMode, DEFINE_SYNC_MODE_PUSH) == 0) {
1046 if (pSanPackage != NULL) {
1047 int count = pSanPackage->cntSyncAlerts;
1048 FW_LOGV("count = %d", count);
1050 for (i = 0 ; i < count ; i++) {
1051 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1053 if (datastoreinfo_per_content_type[content_type]->target == NULL) continue;
1055 if (strcmp(pSanPackage->syncAlerts[i].serverURI, datastoreinfo_per_content_type[content_type]->target) == 0) {
1056 datastoreinfo_per_content_type[content_type]->clientSyncType = pSanPackage->syncAlerts[i].syncType;
1057 FW_LOGV("pSanPackage->syncAlerts[i].syncType = %d", pSanPackage->syncAlerts[i].syncType);
1059 datastoreinfo_per_content_type[content_type]->clientSyncType = ALERT_UNKNOWN;
1070 FW_LOGE("err = %d", err);
1075 static SE_ErrorType __execute_pre_sync(int accountId, PreSyncReturnObj *pPreSyncReturnObj, int sessionTime)
1078 SE_ErrorType err = SE_INTERNAL_OK;
1080 /*if there is no alert command from server check it and goto fail_part*/
1081 if (g_list_length(pPreSyncReturnObj->datastoreInfo) == 0) {
1082 FW_LOGV("alert command list from server is empty");
1083 err = SE_INTERNAL_MISCONFIGURATION;
1087 err = __execute_pre_sync_set_server_id(accountId, pPreSyncReturnObj->devID);
1088 if (err != SE_INTERNAL_OK) {
1089 FW_LOGE("failed in __set_server_id");
1093 err = __execute_pre_sync_datastore(accountId, pPreSyncReturnObj->datastoreInfo, sessionTime);
1094 if (err != SE_INTERNAL_OK) {
1095 FW_LOGE("failed in __process_datastore");
1103 FW_LOGE("err = %d", err);
1108 /*FIXME : set Server_id for SAN*/
1109 static SE_ErrorType __execute_pre_sync_set_server_id(int accountId, char *devID)
1112 SE_ErrorType err = SE_INTERNAL_OK;
1115 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &value);
1116 if (result == true) {
1117 if (value != NULL) {
1118 if (strcmp(value, devID) == 0) {
1121 result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, devID, "string", "SE");
1122 if (result == false) {
1123 FW_LOGE("failed in set_Config");
1124 err = SE_INTERNAL_DA_ERROR;
1129 result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, devID, "string", "SE");
1130 if (result == false) {
1131 FW_LOGE("failed in set_Config");
1132 err = SE_INTERNAL_DA_ERROR;
1137 FW_LOGE("failed in get_Config");
1138 err = SE_INTERNAL_DA_ERROR;
1144 if (value != NULL) {
1153 static SE_ErrorType __execute_pre_sync_datastore(int accountId, GList *datastoreInfo, int sessionTime)
1156 SE_ErrorType err = SE_INTERNAL_OK;
1159 GList *serverDatastore_iter = NULL;
1160 DatastoreInfo *serverDatastoreInfo = NULL;
1161 bool existInServerDatastore;
1162 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1163 existInServerDatastore = false;
1165 if (datastoreinfo_per_content_type[content_type]->clientSyncType) {
1167 for (serverDatastore_iter = datastoreInfo; serverDatastore_iter != NULL; serverDatastore_iter = g_list_next(serverDatastore_iter)) {
1168 serverDatastoreInfo = serverDatastore_iter->data;
1170 if (strcmp(serverDatastoreInfo->source, datastoreinfo_per_content_type[content_type]->target) == 0) {
1171 existInServerDatastore = true;
1173 datastoreinfo_per_content_type[content_type]->serverSyncType = serverDatastoreInfo->syncType;
1174 datastoreinfo_per_content_type[content_type]->nextAnchorServer = strdup(serverDatastoreInfo->nextAnchor);
1176 if (datastoreinfo_per_content_type[content_type]->lastAnchorServer) {
1177 /*if (strcmp(datastoreinfo_per_content_type[content_type]->lastAnchorServer, serverDatastoreInfo->lastAnchor) != 0) {
1178 free(datastoreinfo_per_content_type[content_type]->lastAnchorServer);
1179 datastoreinfo_per_content_type[content_type]->lastAnchorServer = NULL;
1180 datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_SLOW_SYNC;
1183 /* When first synchronize, lastAnchorServer is NULL...
1184 if (datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_SLOW_SYNC
1185 && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_CLIENT
1186 && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_CLIENT_BY_SERVER
1187 && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_SERVER
1188 && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
1189 FW_LOGV("refresh_Item_Tbl_From_Service");
1190 refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->plugin_type);
1197 if (!existInServerDatastore) {
1198 /* datastore config is wrong this datastore is not going to contain sync process from now*/
1199 err = __process_update(accountId, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, false, true,
1200 datastoreinfo_per_content_type[content_type]->clientSyncResult);
1201 if (err != SE_INTERNAL_OK) {
1202 FW_LOGE("failed to process_update");
1206 err = __process_update(accountId, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, true, true,
1207 datastoreinfo_per_content_type[content_type]->serverSyncResult);
1208 if (err != SE_INTERNAL_OK) {
1209 FW_LOGE("failed to process_update");
1213 datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_FAILED;
1214 err = __write_sync_resource_info(accountId, syncType, content_type , sessionTime);
1215 if (err != SE_INTERNAL_OK) {
1216 FW_LOGE("failed to write_SyncResourceInfo");
1220 free_datastore(datastoreinfo_per_content_type[content_type]);
1221 datastoreinfo_per_content_type[content_type] = NULL;
1223 Datastore *pDatastore = NULL;
1224 err = __init_datastore_info(accountId, content_type, &pDatastore);
1225 if (err != SE_INTERNAL_OK) {
1226 FW_LOGE("failed to init_datastore_info");
1230 datastoreinfo_per_content_type[content_type] = pDatastore;
1241 static SE_ErrorType __execute_sync(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1244 SE_ErrorType err = SE_INTERNAL_OK;
1246 if (syncType == ALERT_TWO_WAY || syncType == ALERT_TWO_WAY_BY_SERVER ||
1247 syncType == ALERT_ONE_WAY_FROM_CLIENT || syncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1249 err = __execute_sync_arrange_changelog(accountId , (*pSyncReturnObj)->status);
1250 if (err != SE_INTERNAL_OK) {
1251 FW_LOGE("failed to process_update");
1256 err = __execute_sync_status(accountId, pSyncObj, pSyncReturnObj);
1257 if (err != SE_INTERNAL_OK) {
1258 FW_LOGE("failed in __execute_sync_status");
1262 err = __execute_sync_change(accountId, pSyncObj, pSyncReturnObj);
1263 if (err != SE_INTERNAL_OK) {
1264 FW_LOGE("failed in __execute_sync_status");
1272 FW_LOGE("err = %d", err);
1276 static SE_ErrorType __execute_sync_arrange_changelog(int accountId, GList *status)
1279 SE_ErrorType err = SE_INTERNAL_OK;
1284 int list_length = g_list_length(status);
1285 char **itemIdList = (char **)calloc(list_length, sizeof(char *));
1286 if (itemIdList == NULL) {
1287 FW_LOGE("Failed to alloc memory");
1288 err = SE_INTERNAL_NO_MEMORY;
1292 GList *appliedStatus_iter = NULL;
1293 AppliedStatus *pAppliedStatus = NULL;
1294 for (appliedStatus_iter = status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1295 pAppliedStatus = appliedStatus_iter->data;
1297 switch (pAppliedStatus->status) {
1310 /*clean up change log*/
1311 itemIdList[count++] = strdup(pAppliedStatus->luid);
1313 if (syncType == ALERT_TWO_WAY || syncType == ALERT_TWO_WAY_BY_SERVER ||
1314 syncType == ALERT_ONE_WAY_FROM_CLIENT || syncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1316 if (pAppliedStatus->changeType == CHANGE_DELETE) {
1317 da_err = DACI_Delete_Item_By_Item_Id(pAppliedStatus->luid);
1318 if (da_err != DACI_SUCCESS) {
1319 FW_LOGE("failed in DACI_Delete_Item_By_Item_Id");
1320 err = SE_INTERNAL_DA_ERROR;
1333 daci_item_changelog get_changelog;
1334 get_changelog.item_id = pAppliedStatus->luid;
1336 da_err = DACI_Get_Item_Changelog_By_Item_Id(accountId, &get_changelog);
1338 if (da_err == DACI_ERR_NO_DATA) {
1339 FW_LOGV("%s item does not exist in ChangeLog tbl", pAppliedStatus->luid);
1341 } else if (da_err != DACI_SUCCESS) {
1343 FW_LOGE("failed in DACI_Get_Item_Changelog_By_Item_Id");
1344 err = SE_INTERNAL_DA_ERROR;
1348 if (get_changelog.status != NULL) {
1349 if (strcmp(get_changelog.status, "SYNC_ERROR") == 0)
1350 itemIdList[count++] = strdup(pAppliedStatus->luid);
1352 daci_item_changelog set_changelog;
1354 set_changelog.item_id = pAppliedStatus->luid;
1355 set_changelog.status = "SYNC_ERROR";
1356 set_changelog.access_name = "SE";
1359 da_err = DACI_Set_Item_Changelog_Sync_Status(accountId, &set_changelog);
1361 if (get_changelog.status != NULL)
1362 free(get_changelog.status);
1364 if (get_changelog.access_name != NULL)
1365 free(get_changelog.access_name);
1366 if (da_err != DACI_SUCCESS) {
1367 FW_LOGE("failed in DACI_Delete_Item_By_Item_Id");
1368 err = SE_INTERNAL_DA_ERROR;
1378 if (list_length >= 1) {
1379 FW_LOGV("before DACI_Delete_Item_Changelog_By_Item_Id_List");
1380 FW_LOGV("count = %d", count);
1381 da_err = DACI_Delete_Item_Changelog_By_Item_Id_List(accountId, itemIdList, count);
1382 if (da_err != DACI_SUCCESS) {
1383 FW_LOGE("failed in DACI_Delete_Item_Changelog_By_Item_Id_List");
1384 err = SE_INTERNAL_DA_ERROR;
1394 if (itemIdList != NULL) {
1395 for (i = 0; i < list_length; i++) {
1397 free(itemIdList[i]);
1405 static SE_ErrorType __execute_sync_status(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1408 SE_ErrorType err = SE_INTERNAL_OK;
1411 bool existEqualItem;
1413 SyncResult *pClientSyncResult;
1414 GList *changedDatastore_iter = NULL;
1415 ChangedDatastore *pSyncChangedDatastore = NULL;
1416 for (changedDatastore_iter = (*pSyncObj)->changedDatastore; changedDatastore_iter != NULL;) {
1417 pSyncChangedDatastore = (ChangedDatastore *)changedDatastore_iter->data;
1418 FW_LOGV("pChangedDatastore = %p", pSyncChangedDatastore);
1420 int datastoreContentType = 0;
1421 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1422 if (strcmp(pSyncChangedDatastore->source, datastoreinfo_per_content_type[content_type]->source) == 0) {
1423 datastoreContentType = content_type;
1428 GList *changedItem_iter = NULL;
1429 ChangedItem *changedItem = NULL;
1430 for (changedItem_iter = pSyncChangedDatastore->sentItem; changedItem_iter != NULL;) {
1431 changedItem = changedItem_iter->data;
1433 existEqualItem = false;
1434 GList *appliedStatus_iter = NULL;
1435 AppliedStatus *pAppliedStatus = NULL;
1436 for (appliedStatus_iter = (*pSyncReturnObj)->status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1437 pAppliedStatus = appliedStatus_iter->data;
1439 if (strcmp(pAppliedStatus->luid, changedItem->luid) == 0) {
1440 add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1442 if (pAppliedStatus->status >= 200 && pAppliedStatus->status <= 211) {
1443 switch (changedItem->changeType) {
1445 add_add_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1447 case CHANGE_REPLACE:
1448 add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1451 add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1457 /* if status is not success count it is failed*/
1460 (*pSyncReturnObj)->status = g_list_remove((*pSyncReturnObj)->status, pAppliedStatus);
1461 free_appliedstatus(pAppliedStatus);
1462 pAppliedStatus = NULL;
1464 changedItem_iter = g_list_next(changedItem_iter);
1466 pSyncChangedDatastore->sentItem = g_list_remove(pSyncChangedDatastore->sentItem, changedItem);
1467 free_changeditem(changedItem);
1470 existEqualItem = true;
1474 if (existEqualItem == false)
1475 changedItem_iter = g_list_next(changedItem_iter);
1478 changedDatastore_iter = g_list_next(changedDatastore_iter);
1481 pClientSyncResult = datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult;
1482 if (pClientSyncResult->numberOfChange == pClientSyncResult->received_count) {
1484 /*remove datastore from list and
1485 free current ChangedDatastore it does not need anymore because all item in datastore has been sent and receive status*/
1486 (*pSyncObj)->changedDatastore = g_list_remove((*pSyncObj)->changedDatastore, pSyncChangedDatastore);
1487 free_changeddatastore(pSyncChangedDatastore);
1488 pSyncChangedDatastore = NULL;
1491 OperationType operationType;
1492 if (datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult->numberOfChange == 0)
1493 operationType = OPERATION_NOOP;
1495 operationType = OPERATION_ADD;
1496 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, false, needToSave,
1497 datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult);
1498 if (err != SE_INTERNAL_OK) {
1499 FW_LOGE("failed in process_update");
1508 FW_LOGE("err = %d", err);
1512 static SE_ErrorType __execute_sync_change(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1515 SE_ErrorType err = SE_INTERNAL_OK;
1516 DACI_RETURN da_err = DACI_SUCCESS;
1519 /* process command that from server sended to client(sync, add, replace etc...)*/
1520 ChangedDatastore *pSyncReturnChangedDatastore = NULL;
1521 SendingStatus *sendingStatus = NULL;
1523 for (iter = (*pSyncReturnObj)->changedDatastore; iter != NULL;) {
1524 pSyncReturnChangedDatastore = (ChangedDatastore *)iter->data;
1526 int datastoreContentType = 0;
1527 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1528 if (strcmp(pSyncReturnChangedDatastore->target, datastoreinfo_per_content_type[content_type]->source) == 0) {
1529 datastoreContentType = content_type;
1534 /*noti to UI numberOfChanges from server*/
1535 if (pSyncReturnChangedDatastore->hasNumberOfChanges) {
1536 set_numberofchange(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, pSyncReturnChangedDatastore->numberOfChanges);
1538 OperationType operationType;
1539 if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult->numberOfChange == 0)
1540 operationType = OPERATION_NOOP;
1542 operationType = OPERATION_ADD;
1543 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, true, false,
1544 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1545 if (err != SE_INTERNAL_OK) {
1546 FW_LOGE("failed in process_update");
1551 sendingStatus = create_sendingstatus(pSyncReturnChangedDatastore->target, pSyncReturnChangedDatastore->source);
1552 if (sendingStatus == NULL) {
1553 err = SE_INTERNAL_NO_MEMORY;
1554 FW_LOGE("failed to alloc memory");
1558 SyncResult *tempServerSyncResult = NULL;
1559 int changeItemCount = g_list_length(pSyncReturnChangedDatastore->changeItem);
1560 if (changeItemCount > 0) {
1561 /*begin transaction*/
1562 FW_LOGV("DACI_Service_Begin with datastoreContentType = %d", datastoreContentType);
1563 DACI_Service_Begin(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
1564 DACI_Begin_Transaction();
1566 /*back up syncresult structure for rollback case*/
1567 tempServerSyncResult = dup_syncresult(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1571 char *folderId = NULL;
1572 daci_id_list *folder_id_list = DACI_Get_Folder_Folder_Id_By_Folder_Type_Id(accountId,
1573 datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1574 datastoreinfo_per_content_type[datastoreContentType]->folder_type_id);
1575 if (folder_id_list != NULL) {
1576 folderId = folder_id_list->id[0];
1579 bool needToSave = false;
1580 SyncResult *pServerSyncResult = NULL;
1581 GList *changedIter = NULL;
1582 ChangedItem *changedItem = NULL;
1583 AppliedStatus *appliedStatus = NULL;
1585 for (changedIter = pSyncReturnChangedDatastore->changeItem; changedIter != NULL; changedIter = g_list_next(changedIter)) {
1586 changedItem = (changedIter->data);
1588 FW_LOGV("changedItem->changeType : %d", changedItem->changeType);
1589 FW_LOGV("changedItem->luid : %s", changedItem->luid);
1590 FW_LOGV("changedItem->content_type : %s", changedItem->content_type);
1591 FW_LOGV("changedItem->indexOfDatastore : %d", changedItem->indexOfDatastore);
1592 FW_LOGV("changedItem->data : %s", changedItem->data);
1594 add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1596 bool da_fail = false;
1597 CommandResult returnResult = COMMAND_RESULT_INIT;
1598 switch (changedItem->changeType) {
1601 /* add to service DB*/
1602 if (changedItem->data != NULL) {
1603 char *service_id = NULL;
1604 da_err = DACI_Service_Add_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1605 accountId, folderId, (const void *)(changedItem->data), &service_id);
1606 FW_LOGV("da_err = %d", da_err);
1608 if (da_err == DACI_SUCCESS) {
1610 daci_item daci_item_s;
1611 daci_item_s.account_id = accountId;
1612 daci_item_s.item_id = changedItem->luid;
1613 daci_item_s.folder_id = folderId;
1614 daci_item_s.data_store_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1615 daci_item_s.service_id = service_id;
1616 daci_item_s.access_name = "Engine";
1618 da_err = DACI_Add_Item(&daci_item_s, 1);
1619 FW_LOGV("da_err = %d", da_err);
1620 if (da_err == DACI_SUCCESS) {
1622 daci_folder *pDaci_folder = DACI_Get_Folder_Mapping_Service_Id(accountId, folderId, &acc_cnt);
1623 FW_LOGV("acc_cnt = %d", acc_cnt);
1626 bool success = true;
1628 char **fw_item_id_list = DACI_Generate_Item_Luid(1, acc_cnt);
1629 for (i = 0; i < acc_cnt; i++) {
1630 da_err = ChangeLog_Add_Item(pDaci_folder[i].account_id, fw_item_id_list[i], service_id,
1631 datastoreinfo_per_content_type[content_type]->datastore_id, pDaci_folder[i].folder_id);
1632 free(fw_item_id_list[i]);
1634 FW_LOGV("da_err = %d", da_err);
1636 if (da_err != DACI_SUCCESS) {
1641 if (fw_item_id_list != NULL) {
1642 free(fw_item_id_list);
1645 if (success == true) {
1646 returnResult = COMMAND_RESULT_ADDED;
1647 add_add_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1649 /*this case just fail do rollback and goto fail*/
1650 returnResult = __convert_return_status(da_err);
1653 DACI_Free_Memory_Folder(pDaci_folder, acc_cnt);
1655 /*this case just fail do rollback and goto fail*/
1656 returnResult = __convert_return_status(da_err);
1659 } else if (da_err == DACI_ERR_NOT_SUPPORTED || da_err == DACI_ERR_INVALID_CONTENT || da_err == DACI_ERR_ALREADY_EXIST) {
1660 /*just return error to server*/
1661 returnResult = __convert_return_status(da_err);
1663 /*this case just fail do rollback and goto fail*/
1664 returnResult = __convert_return_status(da_err);
1668 if (service_id != NULL)
1671 returnResult = COMMAND_RESULT_COMMAND_FAIL;
1675 case CHANGE_REPLACE:
1677 if (changedItem->data != NULL) {
1678 /* update from service DB*/
1679 da_err = DACI_Service_Update_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1680 accountId, folderId, changedItem->luid, (const void *)(changedItem->data));
1681 FW_LOGV("da_err = %d", da_err);
1682 if (da_err == DACI_SUCCESS) {
1685 daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, changedItem->luid, &acc_cnt);
1688 bool success = true;
1689 for (i = 0; i < acc_cnt; i++) {
1690 da_err = ChangeLog_Update_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
1691 datastoreinfo_per_content_type[content_type]->datastore_id);
1692 if (da_err != DACI_SUCCESS) {
1698 if (success == true) {
1699 returnResult = COMMAND_RESULT_OK;
1700 add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1702 /*this case just fail do rollback and goto fail*/
1703 returnResult = __convert_return_status(da_err);
1706 DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
1707 } else if (da_err == DACI_ERR_NOT_SUPPORTED || da_err == DACI_ERR_INVALID_CONTENT) {
1708 /*just return error to server*/
1709 returnResult = __convert_return_status(da_err);
1711 /*this case just fail do rollback and goto fail*/
1712 returnResult = __convert_return_status(da_err);
1716 returnResult = COMMAND_RESULT_COMMAND_FAIL;
1722 /* delete from service DB*/
1723 da_err = DACI_Service_Delete_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, accountId, folderId, changedItem->luid);
1724 FW_LOGV("[DACI_Service_Delete_Item] result : %d", da_err);
1725 if (da_err == DACI_SUCCESS) {
1727 daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, changedItem->luid, &acc_cnt);
1730 bool success = true;
1731 for (i = 0; i < acc_cnt; i++) {
1732 da_err = ChangeLog_Delete_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
1733 datastoreinfo_per_content_type[content_type]->datastore_id);
1734 if (da_err != DACI_SUCCESS) {
1740 DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
1741 if (success == true) {
1742 da_err = DACI_Delete_Item_By_Item_Id(changedItem->luid);
1743 if (da_err == DACI_SUCCESS) {
1744 returnResult = COMMAND_RESULT_DELETE_WITHOUT_ARCHIVE;
1745 add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1747 /*this case just fail do rollback and goto fail*/
1748 returnResult = __convert_return_status(da_err);
1752 /*this case just fail do rollback and goto fail*/
1753 returnResult = __convert_return_status(da_err);
1756 } else if (da_err == DACI_ERR_NO_DATA || da_err == DACI_ERR_SUB_DATA_EXIST || da_err == DACI_ERR_INVALID_CONTENT) {
1757 /*just return error to server*/
1758 returnResult = __convert_return_status(da_err);
1760 /*this case just fail do rollback and goto fail*/
1761 returnResult = __convert_return_status(da_err);
1771 if (da_fail == true) {
1773 if (folderId != NULL)
1776 /*replace syncresult when rollback happened*/
1777 if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult != NULL)
1778 free(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1780 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult = tempServerSyncResult;
1782 FW_LOGV("Transaction_Rollback");
1783 FW_LOGV("DACI_Service_End with datastoreContentType = %d", datastoreContentType);
1784 DACI_Service_End(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
1785 DACI_End_Transaction(TRANSACTION_ROLLBACK);
1787 err = SE_INTERNAL_DA_ERROR;
1788 FW_LOGE("failed in DA");
1792 appliedStatus = create_appliedstatus(changedItem->luid, changedItem->changeType, returnResult);
1793 if (appliedStatus == NULL) {
1794 err = SE_INTERNAL_NO_MEMORY;
1795 FW_LOGE("failed to alloc memory");
1798 add_sendingstatus_appliedstatus(sendingStatus, appliedStatus);
1801 if (folderId != NULL)
1804 if (changeItemCount > 0) {
1806 if (tempServerSyncResult != NULL)
1807 free(tempServerSyncResult);
1810 FW_LOGV("DACI_Service_End with datastoreContentType = %d", datastoreContentType);
1811 DACI_Service_End(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
1812 DACI_End_Transaction(TRANSACTION_COMMIT);
1816 pServerSyncResult = datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult;
1817 if (pServerSyncResult->numberOfChange == pServerSyncResult->received_count)
1820 OperationType operationType;
1821 if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult->numberOfChange == 0)
1822 operationType = OPERATION_NOOP;
1824 operationType = OPERATION_ADD;
1825 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, true, needToSave,
1826 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1827 if (err != SE_INTERNAL_OK) {
1828 FW_LOGE("failed in process_update");
1832 (*pSyncObj)->sendingStatus = g_list_append((*pSyncObj)->sendingStatus, sendingStatus);
1834 iter = g_list_next(iter);
1836 (*pSyncReturnObj)->changedDatastore = g_list_remove((*pSyncReturnObj)->changedDatastore, pSyncReturnChangedDatastore);
1837 free_changeddatastore(pSyncReturnChangedDatastore);
1838 pSyncReturnChangedDatastore = NULL;
1845 FW_LOGE("err = %d", err);
1849 static SE_ErrorType __update_sync_result(int accountId)
1852 SE_ErrorType err = SE_INTERNAL_OK;
1853 DACI_RETURN da_err = DACI_SUCCESS;
1856 daci_last_anchor next_anchor_info;
1857 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1858 if (!datastoreinfo_per_content_type[content_type]->clientSyncType) continue;
1860 next_anchor_info.access_name = "Engine";
1861 next_anchor_info.account_id = accountId;
1862 next_anchor_info.data_store_id = datastoreinfo_per_content_type[content_type]->datastore_id;
1863 next_anchor_info.last_anchor_client = datastoreinfo_per_content_type[content_type]->nextAnchorClient;
1864 next_anchor_info.last_anchor_server = datastoreinfo_per_content_type[content_type]->nextAnchorServer;
1866 da_err = DACI_Set_Last_Anchor(&next_anchor_info);
1867 if (da_err != DACI_SUCCESS)
1868 FW_LOGE("failed in DACI_Set_Last_Anchor");
1870 da_err = set_ChangePoint_Service(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
1871 if (da_err != DACI_SUCCESS)
1872 FW_LOGE("failed in set_ChangePoint_Service");
1880 static CommandResult __convert_return_status(DACI_RETURN da_err)
1882 CommandResult returnResult = COMMAND_RESULT_INIT;
1884 case 1: /*DACI_SUCCESS*/
1885 /*never comes this case
1887 case -813: /*DACI_ERR_ALREADY_EXIST*/
1888 returnResult = COMMAND_RESULT_ALREADY_EXIST; /*[A] Already exists exception*/
1890 case -815: /*DACI_ERR_MEMORY_FULL*/ /* oma : 420 (device full exception) */
1891 returnResult = COMMAND_RESULT_DEVICE_FULL; /*[AR] Device full exception*/
1893 case -820: /*DACI_ERR_NOT_SUPPORTED*/ /* oma : 415 (unsupported media type or format exception) */
1894 returnResult = COMMAND_RESULT_UNSUPPORTED_TYPE; /*[AR] Unsupported media type or format exception*/
1896 case -819: /*DACI_ERR_NO_DATA*/ /* kies : not found, oma : 211 (item not deleted exception) */
1897 returnResult = COMMAND_RESULT_NOT_EXIST; /*[D] Item not deleted exception*/
1899 case -800: /*DACI_ERRORS*/
1900 case -801: /*DACI_NOT_FOUND_PLUG_IN*/
1901 case -802: /*DACI_ERR_OPEN_FAILED*/
1902 case -803: /*DACI_ERR_CLOSE_FAILED*/
1903 case -804: /*DACI_ERR_TRANSACTION_FAILED*/
1904 case -805: /*DACI_ERR_CREATE_TABLE_FAILED*/
1905 case -806: /*DACI_ERR_DROP_TABLE_FAILED*/
1906 case -807: /*DACI_ERR_QUERY_FAILED*/
1907 case -808: /*DACI_ERR_NOT_OPENED*/
1908 case -809: /*DACI_ERR_ACCOUNT_FULL*/
1909 case -810: /*DACI_ERR_DELETE_LAST_ACCOUNT*/
1910 case -811: /*DACI_ERR_PRIMARY_KEY_NOT_UNIQUE*/
1911 case -812: /*DACI_ERR_DB_HANDLER_MGR*/
1912 case -814: /*DACI_ERR_INVALID_CONTENT*/
1913 case -816: /*DACI_ERR_SUB_DATA_EXIST*/ /* oma : 427 (item not empty) */
1914 case -817: /*DACI_ERR_LOCKED*/ /* kies : cannot access */
1915 case -818: /*DACI_ERR_MORE_DATA*/
1916 case -821:/*DACI_ERR_NOT_EXECUTE*/ /* kies : ex) sms send command, no network service */
1918 returnResult = COMMAND_RESULT_COMMAND_FAIL; /*[ARD] Command failed exception*/
1922 return returnResult;
1925 static char *__convert_cttype(int datastore_id)
1927 char *cttype = NULL;
1928 /*FIXME : check type and version (contact : vCard2.1 , calendar : vCalendar 1.0 , memo : plain text) of real item data..*/
1929 switch (datastore_id) {
1930 case TYPE_CONTACT: /*contact (vCard2.1)*/
1931 cttype = ELEMENT_TEXT_VCARD;
1933 case TYPE_CALENDAR: /*calendar (vCalendar2.0)*/
1934 cttype = ELEMENT_TEXT_VCAL;
1936 case TYPE_MEMO: /*note(Memo)*/
1937 cttype = ELEMENT_TEXT_PLAIN;
1946 static int __convert_synctype_value(char *syncType_str)
1950 if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) == 0)
1951 syncType_value = ALERT_SLOW_SYNC ;
1952 else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) == 0)
1953 syncType_value = ALERT_TWO_WAY ;
1954 else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) == 0)
1955 syncType_value = ALERT_ONE_WAY_FROM_CLIENT;
1956 else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) == 0)
1957 syncType_value = ALERT_ONE_WAY_FROM_SERVER;
1958 else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) == 0)
1959 syncType_value = ALERT_REFRESH_FROM_SERVER;
1960 else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) == 0)
1961 syncType_value = ALERT_REFRESH_FROM_CLIENT;
1963 syncType_value = ALERT_UNKNOWN;
1965 return syncType_value;
1968 bool synchronize(int accountId, char *syncMode, SanPackage *pSanPackage)
1970 FW_LOGV("accountID = %d", accountId);
1971 FW_LOGV("syncMode = %s", syncMode);
1974 remove(OMA_DS_MSG_PATH);
1980 SE_ErrorType err = SE_INTERNAL_OK;
1981 Common_ErrorType errorCode = COMMON_OK;
1982 SyncProgress process = PROGRESS_NONE;
1983 SyncError error = ERROR_NONE;
1985 int sessionTime = FW_Convert_Seconds_To_UTC(time(NULL)); /*lastSessionTime for resultView;*/
1986 FW_LOGV("sessionTime = %d", sessionTime);
1988 PreSyncReturnObj *pPreSyncReturnObj = NULL;
1989 SyncObj *pSyncObj = NULL;
1990 SyncReturnObj *pSyncReturnObj = NULL;
1992 bool cancel_flag = false;
1994 char *session_id = NULL;
1995 DCI_RETURN dci_result = DCI_SUCCESS;
1996 DACI_RETURN da_err = DACI_SUCCESS;
1998 dci_result = DCI_PM_Lock_State(DEFINE_PLATFORM, LCD_OFF , STAY_CUR_STATE, 0);
1999 if (dci_result != DCI_SUCCESS) {
2000 FW_LOGE("failed in DCI_PM_Lock_State");
2003 da_err = DACI_Open_Agent();
2004 if (da_err != DACI_SUCCESS) {
2005 FW_LOGE("failed in DACI_Open_Agent");
2006 err = SE_INTERNAL_DA_ERROR;
2010 /*prepare for sending pre sync*/
2011 err = __prepare_pre_sync(accountId, syncMode, pSanPackage);
2012 if (err != SE_INTERNAL_OK) {
2013 FW_LOGE("failed in __prepare_pre_sync");
2017 cancel_flag = task_process_check_cancel_flag();
2019 err = SE_INTERNAL_CANCEL;
2023 pPreSyncReturnObj = (PreSyncReturnObj *)calloc(1, sizeof(PreSyncReturnObj));
2024 if (pPreSyncReturnObj == NULL) {
2025 FW_LOGE("failed to alloc memory");
2026 err = SE_INTERNAL_NO_MEMORY;
2031 if (strcmp(syncMode, DEFINE_SYNC_MODE_PUSH) == 0) {
2032 session_id = g_strdup_printf("%u", pSanPackage->sessionID); /*freed in pre_sync*/
2033 errorCode = pre_sync(TRANSPORT_TYPE, accountId, session_id, (void **)&pPreSyncReturnObj);
2035 errorCode = pre_sync(TRANSPORT_TYPE, accountId, NULL, (void **)&pPreSyncReturnObj);
2037 cancel_flag = task_process_check_cancel_flag();
2039 err = SE_INTERNAL_CANCEL;
2043 FW_LOGV("pre_sync errorCode =[%d]", errorCode);
2044 if (errorCode != COMMON_OK) {
2045 err = SE_INTERNAL_SA_ERROR;
2049 /*execute pre_sync return*/
2050 err = __execute_pre_sync(accountId, pPreSyncReturnObj, sessionTime);
2051 if (err != SE_INTERNAL_OK) {
2052 FW_LOGE("failed in __process_pre_sync");
2056 __session_process(accountId, PROGRESS_AUTHENTICATED, ERROR_NONE);
2058 cancel_flag = task_process_check_cancel_flag();
2060 err = SE_INTERNAL_CANCEL;
2065 FW_LOGV("pre_sync end pkg3 start");
2067 unsigned int msg_size;
2068 char *recvMsg = NULL;
2069 unsigned int recvMsg_size;
2071 pSyncObj = (SyncObj *)calloc(1, sizeof(SyncObj));
2072 if (pSyncObj == NULL) {
2073 FW_LOGE("failed to alloc memory");
2074 err = SE_INTERNAL_NO_MEMORY;
2078 pSyncReturnObj = (SyncReturnObj *)calloc(1, sizeof(SyncReturnObj));
2079 if (pSyncReturnObj == NULL) {
2080 FW_LOGE("failed to alloc memory");
2081 err = SE_INTERNAL_NO_MEMORY;
2085 err = __assemble_changeddatastores(accountId, &pSyncObj);
2086 if (err != SE_INTERNAL_OK) {
2087 FW_LOGE("failed in __assemble_changeddatastore");
2091 cancel_flag = task_process_check_cancel_flag();
2093 err = SE_INTERNAL_CANCEL;
2100 errorCode = generate_msg((void **)&pSyncObj, &msg, &msg_size);
2101 if (errorCode != COMMON_OK) {
2102 FW_LOGE("Failed in generate_Msg");
2103 err = SE_INTERNAL_SA_ERROR;
2107 cancel_flag = task_process_check_cancel_flag();
2109 err = SE_INTERNAL_CANCEL;
2113 errorCode = exchange_msg(TRANSPORT_TYPE, msg, msg_size, &recvMsg, &recvMsg_size);
2114 if (errorCode != COMMON_OK) {
2115 FW_LOGE("Failed in exchange_Msg");
2116 err = SE_INTERNAL_SA_ERROR;
2120 cancel_flag = task_process_check_cancel_flag();
2122 err = SE_INTERNAL_CANCEL;
2126 errorCode = process_recv_msg(recvMsg, recvMsg_size, (void **)&pSyncReturnObj, &isFinish);
2127 if (errorCode != COMMON_OK) {
2128 FW_LOGE("Failed in processRecv_Msg");
2129 err = SE_INTERNAL_SA_ERROR;
2133 cancel_flag = task_process_check_cancel_flag();
2135 err = SE_INTERNAL_CANCEL;
2139 err = __execute_sync(accountId, &pSyncObj, &pSyncReturnObj);
2140 if (err != SE_INTERNAL_OK) {
2141 FW_LOGE("failed in __execute_sync");
2145 free_changeddatastores(pSyncReturnObj->changedDatastore);
2146 pSyncReturnObj->changedDatastore = NULL;
2148 free_appliedstatuses(pSyncReturnObj->status);
2149 pSyncReturnObj->status = NULL;
2151 cancel_flag = task_process_check_cancel_flag();
2153 err = SE_INTERNAL_CANCEL;
2158 err = __update_sync_result(accountId);
2159 if (err != SE_INTERNAL_OK) {
2160 FW_LOGE("failed in __update_anchor");
2164 __write_sync_data(accountId, syncType, SYNC_SESSION_SUCCEEDED, sessionTime);
2170 /*clean up for SA unusual end sync process case*/
2173 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2174 if (datastoreinfo_per_content_type[content_type]->clientSyncType)
2175 datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_STOPPED;
2178 __write_sync_data(accountId, syncType, SYNC_SESSION_STOPPED, sessionTime);
2184 /*clean up for SA unusual end sync process case*/
2187 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2188 if (datastoreinfo_per_content_type[content_type]->clientSyncType)
2189 datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_FAILED;
2192 __write_sync_data(accountId, syncType, SYNC_SESSION_FAILED, sessionTime);
2196 if (err == SE_INTERNAL_SA_ERROR)
2197 convert_common_errorcode(errorCode, &process, &error);
2199 convert_engine_errorcode(err, &process, &error);
2201 __off_synchronising_account(accountId);
2203 __session_process(accountId, process, error);
2204 FW_LOGV("process = %d", process);
2205 FW_LOGV("error = %d", error);
2209 dci_result = DCI_PM_Unlock_State(DEFINE_PLATFORM, LCD_OFF , RESET_TIMER);
2210 if (dci_result != DCI_SUCCESS) {
2211 FW_LOGE("failed in DCI_PM_Unlock_State");
2214 if (pPreSyncReturnObj != NULL) {
2215 free_presyncreturnobj(pPreSyncReturnObj);
2216 pPreSyncReturnObj = NULL;
2219 if (pSyncObj != NULL) {
2220 free_syncobj(pSyncObj);
2224 if (pSyncReturnObj != NULL) {
2225 free_syncreturnobj(pSyncReturnObj);
2226 pSyncReturnObj = NULL;
2229 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2230 free_datastore(datastoreinfo_per_content_type[content_type]);
2231 datastoreinfo_per_content_type[content_type] = NULL;
2236 FW_LOGE("\n=============TOTAL---- % d . % d sec\n", dt / 1000, dt % 1000);
2238 if (err != SE_INTERNAL_OK)
2244 void convert_common_errorcode(Common_ErrorType errorCode, SyncProgress *process, SyncError *error)
2246 switch (errorCode) {
2247 case COMMON_OK: /*ERROR_INTERNAL_OK*/
2249 /* Do nothing : Error None
2250 * pre_sync : PROGRESS_AUTHENTICATED
2251 * sync end : PROGRESS_DONE
2252 process = PROGRESS_AUTHENTICATED;
2253 process = PROGRESS_DONE;
2255 *process = PROGRESS_DONE;
2256 *error = ERROR_NONE;
2259 case COMMON_MISCONFIGURATION: /*ERROR_INTERNAL_MISCONFIGURATION : need configure infomation (account_id, id, pw, server_url...)*/
2261 *process = PROGRESS_ERROR;
2262 *error = ERROR_SYNCHDR;
2265 case COMMON_AUTHENTICATION_ERROR: /*ERROR_AUTH_REQUIRED, ERROR_AUTH_REJECTED*/
2267 *process = PROGRESS_ERROR;
2268 *error = ERROR_AUTHENTICATE;
2271 case COMMON_NOT_FOUND: /*ERROR_NOT_FOUND (ERROR_INTERNAL ??)*/
2273 *process = PROGRESS_ERROR;
2274 *error = ERROR_SYNCHDR;
2277 case COMMON_NO_MEMORY: /*ERROR_INTERNAL_NO_MEMORY*/
2279 *process = PROGRESS_ERROR;
2280 *error = ERROR_MEMORY_FULL;
2283 case COMMON_INTERNAL_ERROR: /*ERROR_INTERNAL_NOT_DEFINED || ERROR_INTERNAL_BINDER_ERROR*/
2285 *process = PROGRESS_ERROR;
2286 *error = ERROR_INTERNAL;
2289 case COMMON_SERVER_ERROR: /*ERROR_GENERIC || ERROR_SERVER_FAILURE*/
2291 *process = PROGRESS_ERROR;
2292 *error = ERROR_SERVER;
2295 case COMMON_CONNECTION_ERROR: /*ERROR_INTERNAL_CONNECTION_ERROR*/
2297 *process = PROGRESS_ERROR;
2298 *error = ERROR_CONNECTION;
2301 case COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER: /*ERROR_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER*/
2303 *process = PROGRESS_ERROR;
2304 *error = ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2309 *process = PROGRESS_ERROR;
2310 *error = ERROR_INTERNAL; /*?? unknown error*/
2316 void convert_engine_errorcode(SE_ErrorType err, SyncProgress *process, SyncError *error)
2320 *process = PROGRESS_ERROR;
2321 *error = ERROR_INTERNAL;
2323 case SE_INTERNAL_CANCEL:
2324 case SE_INTERNAL_OK:
2325 *process = PROGRESS_DONE;
2326 *error = ERROR_NONE;
2328 case SE_INTERNAL_ERROR:
2329 *process = PROGRESS_ERROR;
2330 *error = ERROR_INTERNAL;
2332 case SE_INTERNAL_NO_MEMORY:
2333 *process = PROGRESS_ERROR;
2334 *error = ERROR_MEMORY_FULL;
2336 case SE_INTERNAL_NOT_DEFINED:
2337 *process = PROGRESS_ERROR;
2338 *error = ERROR_INTERNAL;
2340 case SE_INTERNAL_DA_ERROR:
2341 *process = PROGRESS_ERROR;
2344 case SE_INTERNAL_EVENT_ERROR:
2345 *process = PROGRESS_ERROR;
2346 *error = ERROR_INTERNAL;
2348 case SE_INTERNAL_ENGINE_CONTROLER_ERROR:
2349 *process = PROGRESS_ERROR;
2350 *error = ERROR_INTERNAL;
2352 case SE_INTERNAL_SCHEDULER_ERROR:
2353 *process = PROGRESS_ERROR;
2354 *error = ERROR_INTERNAL;
2356 case SE_INTERNAL_SA_ERROR:
2358 case SE_INTERNAL_MISCONFIGURATION:
2359 *process = PROGRESS_ERROR;
2360 *error = ERROR_SYNCHDR;
2365 SE_ErrorType session_process(char *profileDirName, SyncProgress process, SyncError error)
2368 FW_LOGV("profileDirName = %s", profileDirName);
2369 FW_LOGV("process = %d", process);
2370 FW_LOGV("error = %d", error);
2372 SE_ErrorType err = SE_INTERNAL_OK;
2374 char *syncProcess = NULL;
2375 char *syncError = NULL;
2376 char *profile = NULL;
2377 char *syncProcess_str = NULL;
2378 char *syncError_str = NULL;
2382 syncProcess = DEFINE_SYNC_PROGRESS_NONE;
2385 syncProcess = DEFINE_SYNC_INIT;
2387 case PROGRESS_CONNECTING:
2388 syncProcess = DEFINE_SYNC_CONNECTING;
2390 case PROGRESS_AUTHENTICATED:
2391 syncProcess = DEFINE_SYNC_AUTHENTICATED;
2394 syncProcess = DEFINE_SYNC_DONE;
2396 case PROGRESS_ERROR:
2397 syncProcess = DEFINE_SYNC_ERROR;
2405 syncError = DEFINE_ERROR_NONE;
2407 case ERROR_CONNECTION:
2408 syncError = DEFINE_ERROR_CONNECTION;
2411 syncError = DEFINE_ERROR_SYNCHDR;
2413 case ERROR_INTERNAL:
2414 syncError = DEFINE_ERROR_INTERNAL;
2416 case ERROR_SUSPENDED:
2417 syncError = DEFINE_ERROR_SUSPENDED;
2420 syncError = DEFINE_ERROR_DB;
2423 syncError = DEFINE_ERROR_ABORT;
2426 syncError = DEFINE_ERROR_SERVER;
2428 case ERROR_MEMORY_FULL:
2429 syncError = DEFINE_ERROR_MEMORY_FULL;
2431 case ERROR_AUTHENTICATE:
2432 syncError = DEFINE_ERROR_AUTHENTICATE;
2434 case ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:
2435 syncError = DEFINE_ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2441 if (syncProcess == NULL || syncError == NULL) {
2442 err = SE_INTERNAL_NOT_DEFINED;
2446 if (profileDirName != NULL )
2447 profile = strdup(profileDirName);
2449 if (syncProcess != NULL )
2450 syncProcess_str = strdup(syncProcess);
2452 if (syncError != NULL )
2453 syncError_str = strdup(syncError);
2455 err = send_noti_session_process(profile, syncProcess_str, syncError_str);
2456 if (err != SE_INTERNAL_OK) {
2457 FW_LOGE("failed in send_noti_session_process");
2467 if (syncProcess_str)
2468 free(syncProcess_str);
2470 free(syncError_str);
2475 SE_ErrorType reset_synchronizing_profiles()
2478 int *account_list = 0;
2479 int account_list_count = 0;
2480 SE_ErrorType err = SE_INTERNAL_OK;
2482 DACI_RETURN da_err = DACI_Open_Agent();
2483 if (da_err != DACI_SUCCESS) {
2484 FW_LOGE("failed in DACI_Open_Agent");
2485 err = SE_INTERNAL_DA_ERROR;
2489 account_list = DACI_Get_Account_Account_Id_List(&account_list_count);
2490 FW_LOGV("account_list_count = %d", account_list_count);
2493 for (i = 0; i < account_list_count; i++) {
2494 FW_LOGV("account = %d", account_list[i]);
2495 __off_synchronising_account(account_list[i]);
2497 FW_LOGV("construct_itemTbl From service start");
2499 /*construct_itemTbl From service*/
2500 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_CONTACT);
2501 if (da_err != DACI_SUCCESS)
2502 FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_CONTACT)");
2504 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_CALENDAR);
2505 if (da_err != DACI_SUCCESS)
2506 FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_CALENDAR)");
2508 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_MEMO);
2509 if (da_err != DACI_SUCCESS)
2510 FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_MEMO)");
2512 FW_LOGV("construct_itemTbl From service end");
2520 if (account_list_count > 0)
2526 bool refresh_from_service_all(int accountId)
2529 SE_ErrorType err = SE_INTERNAL_OK;
2531 DACI_RETURN da_err = DACI_Open_Agent();
2532 if (da_err != DACI_SUCCESS) {
2533 err = SE_INTERNAL_DA_ERROR;
2537 FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, TYPE_CONTACT);
2538 refresh_Item_Tbl_From_Service(accountId, TYPE_CONTACT);
2540 FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, FW_CALENDAR);
2541 refresh_Item_Tbl_From_Service(accountId, TYPE_CALENDAR);
2543 FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, FW_MEMO);
2544 refresh_Item_Tbl_From_Service(accountId, TYPE_MEMO);
2550 if (err != SE_INTERNAL_OK)
2556 /*FIXME cancel request to NA */
2557 /*SE_ErrorType cancel_sync_request()
2560 SE_ErrorType err = SE_INTERNAL_OK;
2562 cancel_connection_sync_request(TRANSPORT_TYPE);