3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @SA_Common_Interface.c
21 * @brief This file is the source file of implementation of interface of Service Adapter
24 #include <sync_agent.h>
26 #include "common/common_vconf.h"
27 #include "common/common_define.h"
28 #include "common/common_util.h"
29 #include "service-adapter/protocol-binder/oma_ds_protocol_binder.h"
30 #include "service-adapter/network-binder/na_binder.h"
31 #include "service-adapter/sa_define.h"
32 #include "service-adapter/sa_common_interface.h"
33 #include "service-adapter/sa_elements.h"
34 #include "service-adapter/sa_elements_internal.h"
35 #include "service-adapter/sa_command.h"
36 #include "service-adapter/sa_command_internal.h"
37 #include "service-adapter/sa_session.h"
38 #include "service-adapter/sa_session_internal.h"
39 #include "service-adapter/sa_devinf_internal.h"
40 #include "service-adapter/sa_util.h"
41 #include "service-adapter/sa_devinf.h"
43 #ifndef OMADS_AGENT_LOG
45 #define LOG_TAG "OMA_DS_SA"
48 session_s *static_session = NULL;
50 static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session);
51 static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size);
52 static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size);
53 static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session);
54 static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size);
55 static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length);
57 static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish);
58 static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session);
59 static common_error_type_e _convert_error_type(sa_error_type_e error_type);
61 static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce);
62 static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size);
63 static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length);
64 static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last);
65 static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last);
66 static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg);
67 static void __clean_up_sa(session_s * session);
68 static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header);
69 static sa_error_type_e __check_resume_session(session_s * session, bool * resume);
71 static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce)
75 sa_error_type_e errorType = SA_INTERNAL_OK;
77 sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
78 GList *config_list = NULL;
80 sync_agent_da_config_s *config_data = NULL;
82 sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
83 sync_agent_fw_account_s *fw_account = NULL;
86 errorType = SA_INTERNAL_MISCONFIGURATION;
90 acc_err = sync_agent_create_fw_account(&fw_account);
91 if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
92 _DEBUG_ERROR("failed in sync_agent_create_fw_account");
93 errorType = SA_INTERNAL_NO_MEMORY;
97 acc_err = sync_agent_get_fw_account(account_id, &fw_account);
98 if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
99 _DEBUG_ERROR("failed in sync_agent_update_fw_account");
100 errorType = SA_INTERNAL_ERROR;
104 if (fw_account->email != NULL)
105 *id = strdup(fw_account->email);
107 if (fw_account->password != NULL)
108 *pwd = strdup(fw_account->password);
110 da_err = sync_agent_get_config_list(account_id, &config_list);
111 if (da_err != SYNC_AGENT_DA_SUCCESS) {
112 errorType = SA_INTERNAL_DA_ERROR;
116 for (iter = config_list; iter != NULL; iter = g_list_next(iter)) {
117 config_data = (sync_agent_da_config_s *) iter->data;
119 if (config_data != NULL) {
120 if (config_data->key != NULL) {
121 if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP) == 0) {
122 if (config_data->value != NULL)
123 *server_url = strdup(config_data->value);
126 if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE) == 0) {
127 if (config_data->value != NULL)
128 *next_nonce = strdup(config_data->value);
136 sync_agent_free_fw_account(fw_account);
138 sync_agent_free_config_list(config_list);
144 static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session)
148 sa_error_type_e errorType = SA_INTERNAL_OK;
151 char *targetUrl = NULL;
152 char *sourceUrl = NULL;
153 char *sourceName = NULL;
154 char *nextNonce = NULL;
155 chal_s *pTempChal = NULL;
156 unsigned int sessionID = 0;
158 location_s *pSourceLocation = NULL;
159 location_s *pTargetLocation = NULL;
161 if ((*session) != NULL) {
162 if ((*session)->chal != NULL) {
163 pTempChal = (*session)->chal;
164 (*session)->chal = NULL;
167 if ((*session)->has_opend)
168 sessionID = (*session)->naci_session_id;
170 free_session(*session);
174 sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
175 if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
176 _DEBUG_ERROR("failed to get devinfo");
177 errorType = SA_INTERNAL_ERROR;
181 errorType = __get_account_info(account_id, &id, &pwd, &targetUrl, &nextNonce);
182 if (errorType != SA_INTERNAL_OK) {
183 _DEBUG_ERROR("faied in __get_accountinfo");
187 if (server_flag == true) {
191 errorType = create_location(sourceUrl, sourceName, &pSourceLocation);
192 if (errorType != SA_INTERNAL_OK) {
193 _DEBUG_ERROR("failed in create_location");
197 errorType = create_location(targetUrl, NULL, &pTargetLocation);
198 if (errorType != SA_INTERNAL_OK) {
199 _DEBUG_ERROR("failed in create_location");
203 /* create session info */
204 errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, account_id, session_id, pSourceLocation, pTargetLocation, session);
205 if (errorType != SA_INTERNAL_OK) {
206 _DEBUG_ERROR("failed to create session");
210 if (pTempChal != NULL) {
211 (*session)->chal = pTempChal;
215 (*session)->naci_session_id = sessionID;
223 if (targetUrl != NULL)
225 if (sourceUrl != NULL)
227 if (nextNonce != NULL)
229 if (pTempChal != NULL)
230 free_chal(pTempChal);
236 static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size)
240 sa_error_type_e errorType = SA_INTERNAL_OK;
243 char *targetUrl = NULL;
244 char *nextNonce = NULL;
245 char *credData = NULL;
246 char *decoded_nonce = NULL;
249 alert_type_e alert_type = ALERT_UNKNOWN;
251 /*pkg1 always has final tag */
254 sync_hdr_s *pSyncHdr = NULL;
255 cred_s *pCred = NULL;
256 devinf_s *devInfObj = NULL;
257 syncml_s *syncml = NULL;
259 /*case SAN, MUST use sessionid from SAN message.. */
260 if (session->session_id == NULL) {
261 session->session_id = g_strdup_printf("%ld", time(NULL));
262 if (session->session_id == NULL) {
263 errorType = SA_INTERNAL_NO_MEMORY;
268 errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce);
269 if (errorType != SA_INTERNAL_OK)
272 _DEBUG_TRACE("id = %s", id);
273 _DEBUG_TRACE("pwd = %s", pwd);
274 _DEBUG_TRACE("sessionId = %s", session->session_id);
275 _DEBUG_TRACE("serverUrl = %s", targetUrl);
278 session->pkg_status = SYNCML_PKG_1;
280 chal_s *chal = session->chal;
281 auth_type_e authType;
282 format_type_e formatType = FORMAT_TYPE_UNKNOWN;
283 unsigned int nonce_size = 0;
285 _DEBUG_TRACE("CHAL DOES NOT EXIST");
286 if (nextNonce != NULL) {
287 /*server sent nonce info in previous sync session */
288 authType = AUTH_TYPE_MD5;
289 decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
290 formatType = FORMAT_TYPE_BASE64;
292 authType = AUTH_TYPE_BASIC;
293 formatType = FORMAT_TYPE_BASE64;
296 errorType = create_cred_string(authType, id, pwd, decoded_nonce, nonce_size, &credData);
298 if (errorType != SA_INTERNAL_OK) {
299 _DEBUG_ERROR("failed to create cred string");
303 errorType = create_cred(id, pwd, authType, formatType, credData, &pCred);
305 _DEBUG_TRACE("CHAL DOES EXIST");
306 if (chal->type == AUTH_TYPE_MD5) {
307 if (chal->format == FORMAT_TYPE_BASE64)
308 decoded_nonce = (char *)g_base64_decode(chal->nonce_b64, &nonce_size);
310 if (chal->nonce_plain != NULL) {
311 decoded_nonce = strdup(chal->nonce_plain);
312 nonce_size = chal->nonce_length;
317 errorType = create_cred_string(chal->type, id, pwd, decoded_nonce, nonce_size, &credData);
318 if (errorType != SA_INTERNAL_OK) {
319 _DEBUG_ERROR("failed to create cred string");
323 errorType = create_cred(id, pwd, chal->type, FORMAT_TYPE_BASE64, credData, &pCred);
326 if (credData != NULL) {
331 if (decoded_nonce != NULL) {
333 decoded_nonce = NULL;
336 if (nextNonce != NULL) {
342 if (errorType != SA_INTERNAL_OK) {
343 _DEBUG_ERROR("failed to create cred");
347 set_session_cred(session, pCred);
351 errorType = create_sync_hdr(session, &pSyncHdr);
352 if (errorType != SA_INTERNAL_OK) {
353 _DEBUG_ERROR("failed to create synchdr");
358 errorType = create_devinf(session, &devInfObj);
359 if (errorType != SA_INTERNAL_OK) {
360 _DEBUG_ERROR("failed to create devInf");
364 set_session_devinf(session, devInfObj);
366 errorType = __check_resume_session(session, &resume);
367 if (errorType != SA_INTERNAL_OK) {
368 _DEBUG_ERROR("failed in __check_resume_session");
372 GList *commands = NULL;
373 GList *commands_last = NULL;
375 char *sourceDevInf = NULL;
376 char *contentType = NULL;
378 if (session->protocol_version == VERSION_10)
379 sourceDevInf = ELEMENT_DEVINF_10;
380 else if (session->protocol_version == VERSION_11)
381 sourceDevInf = ELEMENT_DEVINF_11;
382 else if (session->protocol_version == VERSION_12)
383 sourceDevInf = ELEMENT_DEVINF_12;
385 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
386 contentType = ELEMENT_DEVINF_XML;
388 contentType = ELEMENT_DEVINF_WBXML;
390 if (resume != true) {
391 if (server_flag != true) {
392 /* create Put command */
393 command_s *pPutCommand = NULL;
394 location_s *pLocation = NULL;
396 errorType = create_location(sourceDevInf, NULL, &pLocation);
397 if (errorType != SA_INTERNAL_OK)
400 errorType = create_put_command(session, pLocation, contentType, devInfObj, &pPutCommand);
401 if (errorType != SA_INTERNAL_OK)
403 put_into_list(&commands, &commands_last, pPutCommand);
407 /* if it is not dive server */
408 if (server_flag != true) {
409 /* create Get command */
410 command_s *pGetCommand = NULL;
411 location_s *location = NULL;
412 errorType = create_location(sourceDevInf, NULL, &location);
413 if (errorType != SA_INTERNAL_OK) {
414 _DEBUG_ERROR("failed to create location");
418 errorType = create_get_command(session, location, contentType, &pGetCommand);
419 if (errorType != SA_INTERNAL_OK) {
420 _DEBUG_ERROR("failed to create get command");
423 put_into_list(&commands, &commands_last, pGetCommand);
426 command_s *pAlertCommand = NULL;
427 location_s *pSourceLocation = NULL;
428 location_s *pTargetLocation = NULL;
430 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
431 if (datastoreinfo_per_content_type[content_type] != NULL) {
432 if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
433 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
434 if (errorType != SA_INTERNAL_OK) {
435 _DEBUG_ERROR("failed to create location");
437 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
438 when SA_INTERNAL_NOT_DEFINED error case */
439 if (errorType == SA_INTERNAL_NOT_DEFINED)
440 errorType = SA_INTERNAL_MISCONFIGURATION;
445 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
446 if (errorType != SA_INTERNAL_OK) {
447 _DEBUG_ERROR("failed to create location");
449 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
450 when SA_INTERNAL_NOT_DEFINED error case */
451 if (errorType == SA_INTERNAL_NOT_DEFINED)
452 errorType = SA_INTERNAL_MISCONFIGURATION;
456 _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->id = %d", content_type, datastoreinfo_per_content_type[content_type]->id);
457 _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->pw = %d", content_type, datastoreinfo_per_content_type[content_type]->pw);
459 if (datastoreinfo_per_content_type[content_type]->id != NULL && datastoreinfo_per_content_type[content_type]->pw != NULL) {
461 /*FIXME is there a nextNonce for datastore ?? */
462 if (nextNonce != NULL) {
463 /*server sent nonce info in previous sync session */
464 authType = AUTH_TYPE_MD5;
465 decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
467 authType = AUTH_TYPE_BASIC;
468 formatType = FORMAT_TYPE_BASE64;
471 errorType = create_cred_string(authType, datastoreinfo_per_content_type[content_type]->id, datastoreinfo_per_content_type[content_type]->pw, decoded_nonce, nonce_size, &credData);
472 if (errorType != SA_INTERNAL_OK) {
473 _DEBUG_ERROR("failed to create cred string");
477 errorType = create_cred(id, pwd, authType, FORMAT_TYPE_BASE64, credData, &pCred);
479 if (credData != NULL) {
486 alert_type = ALERT_RESUME;
488 alert_type = datastoreinfo_per_content_type[content_type]->client_sync_type;
490 errorType = create_alert_command(session, alert_type,
491 pSourceLocation, pTargetLocation, datastoreinfo_per_content_type[content_type]->last_anchor_client, datastoreinfo_per_content_type[content_type]->next_anchor_client, pCred, &pAlertCommand);
492 if (errorType != SA_INTERNAL_OK) {
493 _DEBUG_ERROR("failed to create alert command");
498 put_into_list(&commands, &commands_last, pAlertCommand);
502 errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml);
503 if (errorType != SA_INTERNAL_OK) {
504 _DEBUG_ERROR("failed to create syncml");
508 session->is_sending_final = isFinal;
510 session->pkg_status = SYNCML_PKG_2;
513 errorType = __object_binder(syncml, server_flag, msg, msg_size);
514 if (errorType != SA_INTERNAL_OK) {
515 _DEBUG_ERROR("failed in objectBinder");
519 /*free generate structure */
526 if (targetUrl != NULL)
528 if (nextNonce != NULL)
540 if (credData != NULL)
547 if (targetUrl != NULL)
549 if (nextNonce != NULL)
555 if (pSyncHdr != NULL)
556 free_sync_hdr(pSyncHdr);
564 static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size)
568 sa_error_type_e errorType = SA_INTERNAL_OK;
569 bool isNeedNextMsg = false;
571 sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
572 sync_agent_pb_protocol_binder_info_s *binder = NULL;
574 sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
576 if (server_flag != true)
577 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set);
579 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set);
582 sync_hdr_s *pSyncHdr = NULL;
583 errorType = create_sync_hdr(session, &pSyncHdr);
584 if (errorType != SA_INTERNAL_OK) {
585 _DEBUG_ERROR("failed to create synchdr");
589 err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL);
590 err = oma_ds_1_2_binder_append(binder, PE_HEADER, pSyncHdr);
591 err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL);
593 sync_obj_s **sync = (sync_obj_s **) sync_obj;
594 GList *commands = NULL;
595 GList *commands_last = NULL;
598 errorType = __generate_msg_status_map_command(&session, sync, &commands, &commands_last);
599 if (errorType != SA_INTERNAL_OK) {
600 _DEBUG_ERROR("failed in __generate_msg_status_map_command");
604 /*append status command */
605 GList *status_iter = NULL;
606 status_s *pStatus = NULL;
607 for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
608 pStatus = (status_s *) status_iter->data;
609 err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus);
612 /*append results command */
613 GList *results_iter = NULL;
614 command_s *pResultsCommand = NULL;
615 for (results_iter = session->results_command; results_iter != NULL; results_iter = g_list_next(results_iter)) {
616 pResultsCommand = (command_s *) results_iter->data;
617 put_into_list(&commands, &commands_last, pResultsCommand);
618 err = oma_ds_1_2_binder_append(binder, PE_RESULTS_START, pResultsCommand);
620 if (pResultsCommand->type == COMMAND_TYPE_RESULTS) {
621 if (pResultsCommand->private.results.item != NULL) {
622 if (pResultsCommand->private.results.item->data_type == ITEM_DEVINF) {
623 err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pResultsCommand->private.results.item);
627 err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL);
631 /*append map command */
632 GList *map_iter = NULL;
633 command_s *pMapCommand = NULL;
634 for (map_iter = session->map_command; map_iter != NULL; map_iter = g_list_next(map_iter)) {
635 pMapCommand = (command_s *) map_iter->data;
636 err = oma_ds_1_2_binder_append(binder, PE_MAP, pMapCommand);
639 /* free sendgStatus structure from SE */
640 free_sending_statuses((*sync)->sending_status);
641 (*sync)->sending_status = NULL;
643 if (session->pkg_status == SYNCML_PKG_3) {
645 errorType = __generate_msg_exist_sending_map_command(binder, &session, &commands, &commands_last);
646 if (errorType != SA_INTERNAL_OK) {
647 _DEBUG_ERROR("failed in __generate_msg_status_map_command");
651 errorType = __generate_msg_changes_command(binder, &session, sync, &commands, &commands_last, &isNeedNextMsg);
652 if (errorType != SA_INTERNAL_OK) {
653 _DEBUG_ERROR("failed in __generate_msg_status_map_command");
661 if (session->pkg_status == SYNCML_PKG_5 && session->is_receiving_final)
664 /*if we have to send alert command(222,223 etc) */
665 GList *alert_iter = NULL;
666 command_s *pAlertCommand = NULL;
667 for (alert_iter = session->alert_command; alert_iter != NULL; alert_iter = g_list_next(alert_iter)) {
668 pAlertCommand = alert_iter->data;
669 put_into_list(&commands, &commands_last, pAlertCommand);
670 err = oma_ds_1_2_binder_append(binder, PE_ALERT, pAlertCommand);
674 err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL);
676 session->is_sending_final = isFinal;
678 err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL);
679 err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL);
681 oma_ds_1_2_binder_get_stream(binder, msg, msg_size);
682 _DEBUG_TRACE("final msg size = %d", *msg_size);
684 /*FIXME for debugging */
686 unsigned int xml_len = 0;
687 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
688 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
690 err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len);
691 if (err == SYNC_AGENT_PB_RETURN_OK) {
692 set_xml_to_file(xml, OMA_DS_MSG_PATH);
701 oma_ds_1_2_binder_terminate(binder);
703 if (session->pkg_status == SYNCML_PKG_3 && session->is_sending_final)
704 session->pkg_status = SYNCML_PKG_4;
705 else if (session->pkg_status == SYNCML_PKG_5 && session->is_sending_final)
706 session->pkg_status = SYNCML_PKG_6;
708 free_sync_hdr(pSyncHdr);
709 if (session->status != NULL) {
710 if (session->suspend_status != NULL) {
711 free_statuses(session->suspend_status);
712 session->suspend_status = NULL;
714 session->suspend_status = static_session->status;
715 session->status = NULL;
717 free_commands(commands);
719 session->alert_command = NULL;
720 session->results_command = NULL;
727 free_sync_hdr(pSyncHdr);
728 if (session->status != NULL) {
729 free_statuses(session->status);
730 session->status = NULL;
732 free_commands(commands);
738 static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last)
742 sa_error_type_e errorType = SA_INTERNAL_OK;
743 sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
746 sending_status_s *sendingStatus = NULL;
747 /* send status of sync, add, replace, delete command */
748 for (iter = (*sync)->sending_status; iter != NULL; iter = g_list_next(iter)) {
749 sendingStatus = (iter->data);
752 GList *sendingIter = NULL;
753 applied_status_s *appliedStatus = NULL;
754 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
755 appliedStatus = (sendingIter->data);
757 /* set data in status of add, replace, delete command */
758 GList *statusIter = NULL;
759 status_s *status = NULL;
760 for (statusIter = (*session)->temp_status; statusIter != NULL; statusIter = g_list_next(statusIter)) {
761 status = statusIter->data;
763 if (status->type == COMMAND_TYPE_ADD || status->type == COMMAND_TYPE_REPLACE || status->type == COMMAND_TYPE_DELETE) {
765 /* if there is a status for add command it have to be checked
766 because map command has to be generated
767 otherwise replace, delete command doesnot need to generate map command */
768 if (status->type == COMMAND_TYPE_ADD)
771 if (status->source_ref != NULL) {
773 sync_agent_da_mapping_s *mapping = NULL;
774 da_err = sync_agent_create_mapping(&mapping);
775 if (da_err != SYNC_AGENT_DA_SUCCESS) {
776 _DEBUG_ERROR("failed in sync_agent_create_mapping !!");
777 errorType = SA_INTERNAL_DA_ERROR;
781 sync_agent_da_get_mapping_query_s query;
782 query.account_id = (*session)->account_id;
783 query.luid = appliedStatus->luid;
785 da_err = sync_agent_get_mapping(&query, &mapping);
786 if (da_err != SYNC_AGENT_DA_SUCCESS) {
787 _DEBUG_ERROR("failed in sync_agent_get_mapping !!");
788 errorType = SA_INTERNAL_DA_ERROR;
789 sync_agent_free_mapping(mapping);
793 if (mapping != NULL) {
794 if (mapping->guid == NULL) {
795 errorType = SA_INTERNAL_DA_ERROR;
796 _DEBUG_ERROR("failed to get guid");
797 sync_agent_free_mapping(mapping);
801 if (strcmp(mapping->guid, get_location_loc_uri(status->source_ref)) == 0) {
802 if (status->data != NULL) {
806 status->data = g_strdup_printf("%d", appliedStatus->status);
807 /*move to status list from tempStatus */
808 (*session)->status = g_list_append((*session)->status, status);
809 (*session)->temp_status = g_list_remove((*session)->temp_status, status);
811 sync_agent_free_mapping(mapping);
816 sync_agent_free_mapping(mapping);
819 _DEBUG_ERROR("mapping is NULL !!");
820 errorType = SA_INTERNAL_DA_ERROR;
821 sync_agent_free_mapping(mapping);
826 if (status->target_ref != NULL) {
828 if (strcmp(appliedStatus->luid, get_location_loc_uri(status->target_ref)) == 0) {
829 if (status->data != NULL) {
833 status->data = g_strdup_printf("%d", appliedStatus->status);
834 /*move to status list from tempStatus */
835 (*session)->status = g_list_append((*session)->status, status);
836 (*session)->temp_status = g_list_remove((*session)->temp_status, status);
845 /* create map command */
846 command_s *mapCommand = NULL;
847 location_s *pSourceLocation = NULL;
848 location_s *pTargetLocation = NULL;
849 errorType = create_location(sendingStatus->source, NULL, &pSourceLocation);
850 if (errorType != SA_INTERNAL_OK) {
851 _DEBUG_ERROR("failed to create location");
855 errorType = create_location(sendingStatus->target, NULL, &pTargetLocation);
856 if (errorType != SA_INTERNAL_OK) {
857 _DEBUG_ERROR("failed to create location");
861 errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand);
862 if (errorType != SA_INTERNAL_OK) {
863 _DEBUG_ERROR("failed to create map command");
867 GList *sendingIter = NULL;
868 applied_status_s *appliedStatus = NULL;
870 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
871 appliedStatus = (sendingIter->data);
873 if (appliedStatus->change_type == CHANGE_ADD) {
875 if (appliedStatus->status == ITEM_ADDED) {
876 item_s *temp = create_item();
878 _DEBUG_ERROR("failed to create item");
879 errorType = SA_INTERNAL_NO_MEMORY;
883 errorType = create_location(appliedStatus->luid, NULL, &pSourceLocation);
884 if (errorType != SA_INTERNAL_OK) {
885 _DEBUG_ERROR("failed to create location");
889 sync_agent_da_mapping_s *mapping = NULL;
890 da_err = sync_agent_create_mapping(&mapping);
891 if (da_err != SYNC_AGENT_DA_SUCCESS) {
892 _DEBUG_ERROR("failed in sync_agent_create_mapping !!");
893 errorType = SA_INTERNAL_DA_ERROR;
897 sync_agent_da_get_mapping_query_s query;
898 query.account_id = (*session)->account_id;
899 query.luid = appliedStatus->luid;
901 da_err = sync_agent_get_mapping(&query, &mapping);
902 if (da_err != SYNC_AGENT_DA_SUCCESS) {
903 _DEBUG_ERROR("failed in sync_agent_get_mapping !!");
904 errorType = SA_INTERNAL_DA_ERROR;
908 if (mapping != NULL) {
909 if (mapping->guid == NULL) {
910 errorType = SA_INTERNAL_DA_ERROR;
911 _DEBUG_ERROR("failed to get guid");
914 errorType = create_location(mapping->guid, NULL, &pTargetLocation);
915 if (errorType != SA_INTERNAL_OK) {
916 _DEBUG_ERROR("failed to create location");
920 if (mapping->guid != NULL) {
922 mapping->guid = NULL;
925 _DEBUG_ERROR("mapping is NULL !!");
926 errorType = SA_INTERNAL_DA_ERROR;
930 set_item_source(temp, pSourceLocation);
931 set_item_target(temp, pTargetLocation);
932 set_map_command_item(mapCommand, temp);
934 sync_agent_da_delete_mapping_query_s query;
935 query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_LUID;
936 query.account_id = (*session)->account_id;
937 query.luid = appliedStatus->luid;
939 da_err = sync_agent_delete_mapping(&query);
940 if (da_err != SYNC_AGENT_DA_SUCCESS) {
941 errorType = SA_INTERNAL_DA_ERROR;
942 _DEBUG_ERROR("failed in sync_agent_delete_mapping !!");
948 put_into_list(commands, commands_last, mapCommand);
949 (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
950 increase_command_ref_count(mapCommand);
960 static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last)
964 sa_error_type_e errorType = SA_INTERNAL_OK;
965 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
966 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
968 /*if there is something in the mapping_tbl it have to be sent before sync command */
969 int existSendingMap = sync_agent_is_exist_mapping_by_account_id((*session)->account_id);
971 if (existSendingMap) {
972 GList *mapping_list = NULL;
973 sync_agent_da_get_mapping_list_query_s query;
974 query.option = SYNC_AGENT_DA_GET_MAPPING_LIST_OPTION_ACCOUNT_ID;
975 query.account_id = (*session)->account_id;
977 ret = sync_agent_get_mapping_list(&query, &mapping_list);
978 if (ret != SYNC_AGENT_DA_SUCCESS) {
979 _DEBUG_ERROR("failed in sync_agent_get_mapping_list !!");
980 errorType = SA_INTERNAL_ERROR;
984 location_s *pSourceLocation = NULL;
985 location_s *pTargetLocation = NULL;
989 sync_agent_da_mapping_s *mapping_data = (sync_agent_da_mapping_s *) ((GList *) (g_list_nth(mapping_list, g_list_length(mapping_list) - 1))->data);
990 int lastItemTypeId = mapping_data->data_store_id;
992 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
993 if (datastoreinfo_per_content_type[content_type] != NULL) {
994 _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", content_type, datastoreinfo_per_content_type[content_type]->datastore_id);
995 if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
996 if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) {
997 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
998 if (errorType != SA_INTERNAL_OK) {
999 _DEBUG_ERROR("failed to create location");
1003 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
1004 if (errorType != SA_INTERNAL_OK) {
1005 _DEBUG_ERROR("failed to create location");
1016 if (exist == false) {
1017 _DEBUG_VERBOSE("Mapping data is not equal with any synchronizing datastore id");
1021 /* create map command using first item */
1022 command_s *mapCommand = NULL;
1023 errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand);
1024 if (errorType != SA_INTERNAL_OK) {
1025 _DEBUG_ERROR("failed to create map command");
1030 sync_agent_da_mapping_s *iter_data;
1031 for (iter = mapping_list; iter != NULL; iter = g_list_next(iter)) {
1033 iter_data = (sync_agent_da_mapping_s *) (iter->data);
1035 if (lastItemTypeId != iter_data->data_store_id) {
1036 err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand);
1037 put_into_list(commands, commands_last, mapCommand);
1038 (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
1039 increase_command_ref_count(mapCommand);
1043 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1044 if (datastoreinfo_per_content_type[content_type] != NULL) {
1045 if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
1046 if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) {
1047 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
1048 if (errorType != SA_INTERNAL_OK) {
1049 _DEBUG_ERROR("failed to create location");
1053 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
1054 if (errorType != SA_INTERNAL_OK) {
1055 _DEBUG_ERROR("failed to create location");
1065 if (exist == false) {
1066 _DEBUG_ERROR("Mapping data is not equal with any synchronizing datastore id");
1071 lastItemTypeId = iter_data->data_store_id;
1072 item_s *temp = create_item();
1074 _DEBUG_ERROR("failed to create item");
1075 errorType = SA_INTERNAL_NO_MEMORY;
1079 errorType = create_location(iter_data->luid, NULL, &pSourceLocation);
1080 if (errorType != SA_INTERNAL_OK) {
1081 _DEBUG_ERROR("failed to create location");
1085 errorType = create_location(iter_data->guid, NULL, &pTargetLocation);
1086 if (errorType != SA_INTERNAL_OK) {
1087 _DEBUG_ERROR("failed to create location");
1091 set_item_source(temp, pSourceLocation);
1092 set_item_target(temp, pTargetLocation);
1093 set_map_command_item(mapCommand, temp);
1096 sync_agent_free_mapping_list(mapping_list);
1098 err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand);
1099 put_into_list(commands, commands_last, mapCommand);
1100 (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
1101 increase_command_ref_count(mapCommand);
1111 static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg)
1115 sa_error_type_e errorType = SA_INTERNAL_OK;
1116 sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
1117 sync_agent_da_service_item_s *service_item = NULL;
1118 sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
1119 unsigned int stream_size = 0;
1121 char *data = NULL; /*have to be freed */
1122 char *sendingData = NULL;
1124 /* generate sync , add, replace, delete command when there is a item in ChangedDatastore */
1126 command_s *syncCommand = NULL;
1127 changed_datastore_s *changedDatastore = NULL;
1128 bool needNextMsg = false;
1129 for (iter = (*sync)->changed_datastore; iter != NULL; iter = g_list_next(iter)) {
1130 changedDatastore = (iter->data);
1132 if (changedDatastore->need_sync_command) {
1133 /* create sync Command */
1134 location_s *pSourceLocation = NULL;
1135 location_s *pTargetLocation = NULL;
1136 errorType = create_location(changedDatastore->source, NULL, &pSourceLocation);
1137 if (errorType != SA_INTERNAL_OK) {
1138 _DEBUG_ERROR("failed to create location");
1142 errorType = create_location(changedDatastore->target, NULL, &pTargetLocation);
1143 if (errorType != SA_INTERNAL_OK) {
1144 _DEBUG_ERROR("failed to create location");
1148 errorType = create_sync_start_command(*session, pSourceLocation, pTargetLocation, &syncCommand);
1149 if (errorType != SA_INTERNAL_OK) {
1150 _DEBUG_ERROR("failed to create sync start command");
1154 /*set numberOfChanges */
1155 if (changedDatastore->has_number_of_changes) {
1156 /*if it is first sync command set numberOfChanges */
1157 set_sync_start_command_number_of_changes(syncCommand, changedDatastore->number_of_changes);
1158 changedDatastore->has_number_of_changes = 0;
1162 /* TODO get current datastore dynamic memory size & recods */
1163 /* And set Mem structure */
1164 /*Mem *mem = create_mem();
1165 set_mem_sharedmem(mem, 1);
1166 set_mem_freemem(mem, 8100);
1167 set_mem_freeid(mem, 81);
1168 set_sync_start_command_mem(syncCommand, mem); */
1170 err = oma_ds_1_2_binder_append(binder, PE_SYNC_START, syncCommand);
1171 put_into_list(commands, commands_last, syncCommand);
1173 GList *itemIter = NULL;
1174 command_s *changeCommand = NULL;
1175 changed_item_s *changedItem = NULL;
1176 oma_ds_protocol_element_e protocol_element = PE_UNDEF;
1177 for (itemIter = changedDatastore->change_item; itemIter != NULL;) {
1178 changedItem = (itemIter->data);
1180 oma_ds_1_2_binder_get_stream_size(binder, &stream_size);
1181 _DEBUG_VERBOSE("stream_size = %d", stream_size);
1182 _DEBUG_VERBOSE("(*session)->targetMaxMsgSize = %d", (*session)->target_max_msg_size);
1184 if ((*session)->target_max_msg_size > stream_size) {
1185 /*there is a space for command */
1186 if (changedItem->change_type == CHANGE_DELETE) {
1187 /* create delete Command */
1188 errorType = create_delete_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, &changeCommand);
1189 if (errorType != SA_INTERNAL_OK) {
1190 _DEBUG_ERROR("failed to create delete command");
1194 int isFirstLargeObj;
1195 if (changedItem->data == NULL) {
1196 char *folderId = NULL;
1198 sync_agent_da_get_folder_id_list_query_s query;
1199 query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID;
1200 query.account_id = (*session)->account_id;
1201 query.item_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id;
1202 query.folder_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->folder_type_id;
1204 da_err = sync_agent_get_folder_id_list(&query, &list);
1205 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1206 _DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!");
1210 if (g_list_length(list) > 0) {
1211 GList *iter = g_list_nth(list, 0);
1212 folderId = (char *)(iter->data);
1214 _DEBUG_ERROR("failed to get folderId");
1215 errorType = SA_INTERNAL_DA_ERROR;
1219 _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", changedItem->index_of_datastore, datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id);
1220 _DEBUG_VERBOSE("account_id = %d", (*session)->account_id);
1221 _DEBUG_VERBOSE("folderId = %s", folderId);
1222 _DEBUG_VERBOSE("changedItem->luid = %s", changedItem->luid);
1224 da_err = sync_agent_create_service_item(&service_item);
1225 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1226 errorType = SA_INTERNAL_DA_ERROR;
1227 _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
1231 da_err = sync_agent_get_service_item(changedItem->luid, &service_item);
1232 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1233 errorType = SA_INTERNAL_DA_ERROR;
1234 _DEBUG_ERROR("failed in sync_agent_get_service_item() = %d", da_err);
1237 data = g_strdup((char *)(service_item->data));
1239 _DEBUG_VERBOSE("data = %s", data);
1240 sync_agent_free_folder_id_list(list);
1242 da_err = sync_agent_delete_item(changedItem->luid, 1);
1243 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1244 _DEBUG_ERROR("failed in sync_agent_delete_item !!");
1245 errorType = SA_INTERNAL_DA_ERROR;
1249 itemIter = g_list_next(itemIter);
1250 changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem);
1251 free_changed_item(changedItem);
1255 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1256 _DEBUG_ERROR("failed in sync_agent_get_service_item =%d", da_err);
1257 errorType = SA_INTERNAL_DA_ERROR;
1261 isFirstLargeObj = 1;
1263 data = changedItem->data;
1264 changedItem->data = NULL;
1265 isFirstLargeObj = 0;
1268 unsigned int sizeOfData = 0;
1270 sizeOfData = strlen(data);
1272 _DEBUG_VERBOSE("sizeOfData = %d", sizeOfData);
1273 if ((*session)->target_max_msg_size < stream_size + sizeOfData) {
1274 /*can be LargeObj */
1275 int availableSize = (*session)->target_max_msg_size - stream_size;
1276 if ((*session)->remote_devinf->supports_large_objs /*&& availableSize > session->targetMaxMsgSize / 50 */ ) {
1278 /*if server support LargeObj & clearly LargeObj */
1279 sendingData = calloc(availableSize + 1, sizeof(char));
1280 if (sendingData == NULL) {
1281 _DEBUG_ERROR("failed to allocate sendingData");
1282 errorType = SA_INTERNAL_NO_MEMORY;
1285 memcpy(sendingData, data, availableSize);
1286 _DEBUG_VERBOSE("sendingData = %s", sendingData);
1287 _DEBUG_VERBOSE("sendingData size = %d", strlen(sendingData));
1289 char *remainingData = calloc(sizeOfData - availableSize + 1, sizeof(char));
1290 if (remainingData == NULL) {
1291 _DEBUG_ERROR("failed to allocate remainingData");
1292 errorType = SA_INTERNAL_NO_MEMORY;
1295 memcpy(remainingData, data + availableSize, sizeOfData - availableSize);
1296 _DEBUG_VERBOSE("sizeOfData - availableSize = %d", sizeOfData - availableSize);
1297 _DEBUG_VERBOSE("remainingData = %s", remainingData);
1298 _DEBUG_VERBOSE("remainingData size = %d", strlen(remainingData));
1300 if (changedItem->data != NULL)
1301 free(changedItem->data);
1303 set_changed_item_data(changedItem, remainingData);
1304 free(remainingData);
1306 command_status_s *pTemp = NULL;
1307 errorType = create_command_status((*session)->msg_id, (*session)->cmd_id, &pTemp);
1308 if (errorType != SA_INTERNAL_OK) {
1309 _DEBUG_ERROR("failed to create pending status");
1312 (*session)->large_obj = pTemp;
1314 if (changedItem->change_type == CHANGE_ADD) {
1315 /* create add Command */
1316 errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand);
1317 } else if (changedItem->change_type == CHANGE_REPLACE) {
1318 /* create replace Command */
1319 errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid,
1320 changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand);
1323 if (errorType != SA_INTERNAL_OK) {
1324 _DEBUG_ERROR("failed to create add(replace) command");
1328 if (sendingData != NULL) {
1337 /*can append more commnad */
1338 if (changedItem->change_type == CHANGE_ADD) {
1339 /* create add Command */
1340 errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand);
1341 } else if (changedItem->change_type == CHANGE_REPLACE) {
1342 /* create replace Command */
1343 errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand);
1345 if (errorType != SA_INTERNAL_OK) {
1346 _DEBUG_ERROR("failed to create add(replace) command");
1356 if (changeCommand->type == COMMAND_TYPE_ADD)
1357 protocol_element = PE_ADD;
1358 else if (changeCommand->type == COMMAND_TYPE_REPLACE)
1359 protocol_element = PE_REPLACE;
1360 else if (changeCommand->type == COMMAND_TYPE_DELETE)
1361 protocol_element = PE_DELETE;
1363 err = oma_ds_1_2_binder_append(binder, protocol_element, changeCommand);
1364 oma_ds_1_2_binder_get_stream_size(binder, &stream_size);
1365 _DEBUG_VERBOSE("stream size after Changes Command= %d", stream_size);
1366 put_into_list(commands, commands_last, changeCommand);
1368 if (changedItem->data == NULL) {
1369 itemIter = g_list_next(itemIter);
1370 changedDatastore->sent_item = g_list_append(changedDatastore->sent_item, changedItem);
1371 changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem);
1383 /* create sync end Command */
1384 errorType = create_sync_end_command(&syncCommand);
1385 if (errorType != SA_INTERNAL_OK) {
1386 _DEBUG_ERROR("failed to create sync end command");
1389 err = oma_ds_1_2_binder_append(binder, PE_SYNC_END, syncCommand);
1390 put_into_list(commands, commands_last, syncCommand);
1395 int chageItemCount = g_list_length(changedDatastore->change_item);
1396 if (chageItemCount == 0)
1397 changedDatastore->need_sync_command = 0;
1401 *is_need_next_msg = needNextMsg;
1405 if (sendingData != NULL) {
1415 if (service_item != NULL) {
1416 sync_agent_free_service_item(service_item);
1423 static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size)
1427 sa_error_type_e errorType = SA_INTERNAL_OK;
1430 char *sourceUrl = NULL;
1431 char *targetUrl = NULL;
1432 char *nextNonce = NULL;
1434 sync_hdr_s *pSyncHdr = NULL;
1435 syncml_s *syncml = NULL;
1437 GList *commands = NULL;
1438 GList *commands_last = NULL;
1440 command_s *pAlertCommand = NULL;
1441 location_s *pSourceLocation = NULL;
1442 location_s *pTargetLocation = NULL;
1444 /* change pkgstatus tp SYNCML_SUSPEND */
1445 session->pkg_status = SYNCML_SUSPEND;
1447 sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
1448 if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
1449 _DEBUG_ERROR("failed to get devinfo");
1450 errorType = SA_INTERNAL_ERROR;
1454 errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce);
1455 if (errorType != SA_INTERNAL_OK)
1458 _DEBUG_TRACE("id = %s", id);
1459 _DEBUG_TRACE("pwd = %s", pwd);
1460 _DEBUG_TRACE("clientUrl = %s", sourceUrl);
1461 _DEBUG_TRACE("serverUrl = %s", targetUrl);
1462 _DEBUG_TRACE("nextNonce = %s", nextNonce);
1464 /* create SyncHdr */
1465 errorType = create_sync_hdr(session, &pSyncHdr);
1466 if (errorType != SA_INTERNAL_OK) {
1467 _DEBUG_ERROR("failed to create synchdr");
1471 /* Command *pMapCommand = NULL;
1473 for (iter = session->mapCommand; iter != NULL; iter = g_list_next(iter)) {
1474 pMapCommand = (Command *)iter->data;
1475 put_into_list(&commands, &commands_last, pMapCommand);
1476 increase_command_refcount(pMapCommand);
1479 errorType = create_location(targetUrl, NULL, &pTargetLocation);
1480 if (errorType != SA_INTERNAL_OK) {
1481 _DEBUG_ERROR("failed to create location");
1483 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
1484 when SA_INTERNAL_NOT_DEFINED error case */
1485 if (errorType == SA_INTERNAL_NOT_DEFINED)
1486 errorType = SA_INTERNAL_MISCONFIGURATION;
1490 errorType = create_location(sourceUrl, NULL, &pSourceLocation);
1491 if (errorType != SA_INTERNAL_OK) {
1492 _DEBUG_ERROR("failed to create location");
1494 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
1495 when SA_INTERNAL_NOT_DEFINED error case */
1496 if (errorType == SA_INTERNAL_NOT_DEFINED)
1497 errorType = SA_INTERNAL_MISCONFIGURATION;
1501 errorType = create_alert_command(session, ALERT_SUSPEND, pSourceLocation, pTargetLocation, NULL, NULL, NULL, &pAlertCommand);
1502 if (errorType != SA_INTERNAL_OK) {
1503 _DEBUG_ERROR("failed to create alert command");
1507 put_into_list(&commands, &commands_last, pAlertCommand);
1509 errorType = create_syncml(pSyncHdr, session->suspend_status, commands, 0, &syncml);
1510 if (errorType != SA_INTERNAL_OK) {
1511 _DEBUG_ERROR("failed to create syncml");
1516 errorType = __object_binder(syncml, server_flag, msg, msg_size);
1517 if (errorType != SA_INTERNAL_OK) {
1518 _DEBUG_ERROR("failed in objectBinder");
1522 /*free generate structure */
1523 free_syncml(syncml);
1524 session->suspend_status = NULL;
1530 if (sourceUrl != NULL)
1532 if (targetUrl != NULL)
1534 if (nextNonce != NULL)
1546 if (sourceUrl != NULL)
1548 if (targetUrl != NULL)
1550 if (nextNonce != NULL)
1553 free_syncml(syncml);
1554 if (pSyncHdr != NULL)
1555 free_sync_hdr(pSyncHdr);
1557 session->suspend_status = NULL;
1564 static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length)
1568 sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
1569 sa_error_type_e errorType = SA_INTERNAL_OK;
1571 char *targetUri = NULL;
1572 char *jsessionId = NULL;
1573 GList *header_info = NULL;
1574 GList *recv_header = NULL;
1576 if (session != NULL) {
1577 if (session->target != NULL)
1578 targetUri = session->target->loc_uri;
1580 if (session->jsession_id != NULL)
1581 jsessionId = session->jsession_id;
1583 _DEBUG_ERROR("session is NULL !");
1584 errorType = SA_INTERNAL_ERROR;
1588 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1593 na_http_header_binder(acc_type, targetUri, isXML, jsessionId, &header_info);
1594 res = sync_agent_send_msg( /*acc_info (id, pw, uri, ...), */
1595 header_info, transport_type, send_msg, send_msg_length, &recv_header, (unsigned char **)recv_msg, recv_msg_length, SYNC_AGENT_NA_SEND_TYPE_SEND_N_RECEIVE, session->naci_session_id);
1597 if (res != SYNC_AGENT_NA_SUCCESS) {
1598 _DEBUG_ERROR("res = %d", res);
1599 if (res == SYNC_AGENT_NA_SEND_MSG_CANCEL)
1600 errorType = SA_INTERNAL_CANCEL;
1602 errorType = SA_INTERNAL_CONNECTION_ERROR;
1606 __process_jssion_id(session, recv_header);
1610 if (recv_header != NULL) {
1612 sync_agent_na_common_header_info_s *iter_data;
1613 for (iter = recv_header; iter != NULL;) {
1615 iter_data = ((sync_agent_na_common_header_info_s *) (iter->data));
1617 iter = g_list_next(iter);
1618 recv_header = g_list_remove(recv_header, iter_data);
1620 if (iter_data->key != NULL)
1621 free(iter_data->key);
1622 if (iter_data->value != NULL)
1623 free(iter_data->value);
1626 g_list_free(recv_header);
1633 static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish)
1637 sa_error_type_e errorType = SA_INTERNAL_OK;
1639 reset_cmd_id_session(session);
1641 syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s));
1642 if (syncml == NULL) {
1643 _DEBUG_ERROR("failed to allocate syncml");
1644 errorType = SA_INTERNAL_NO_MEMORY;
1648 GList *pReturnStatus = NULL;
1649 GList *pReturnDatastore = NULL;
1651 errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length);
1652 if (errorType != SA_INTERNAL_OK) {
1653 _DEBUG_ERROR("failed in reverseObjectBinder");
1657 /* check receive msg */
1658 errorType = receive_header(session, syncml->hdr);
1659 if (errorType != SA_INTERNAL_OK) {
1660 _DEBUG_ERROR("failed in receive_Header");
1664 errorType = receive_statuses(session, syncml->status, &pReturnStatus);
1665 if (errorType != SA_INTERNAL_OK) {
1666 _DEBUG_ERROR("failed in receive_Statuses");
1670 errorType = receive_commands(session, syncml->commands, false, &pReturnDatastore);
1671 if (errorType != SA_INTERNAL_OK) {
1672 _DEBUG_ERROR("failed in receive_Commands");
1676 /* do not need to update pkgstatus when suspend */
1677 if (session->pkg_status == SYNCML_SUSPEND) {
1678 _DEBUG_TRACE("Suspend");
1679 __clean_up_sa(session);
1680 static_session = NULL;
1684 if (syncml->final) {
1685 session->is_sending_final = 0;
1686 session->is_receiving_final = 1;
1688 session->is_receiving_final = 0;
1690 if (session->pkg_status == SYNCML_PKG_2) {
1691 pre_sync_return_obj_s **pre = (pre_sync_return_obj_s **) sync_return_obj;
1694 (*pre)->datastore_info = pReturnDatastore;
1696 if (session->remote_devinf != NULL) {
1697 if (session->remote_devinf->dev_id != NULL)
1698 (*pre)->dev_id = strdup(session->remote_devinf->dev_id);
1701 sync_return_obj_s **syncReturn = (sync_return_obj_s **) sync_return_obj;
1703 if ((*syncReturn) != NULL) {
1704 (*syncReturn)->status = pReturnStatus;
1705 (*syncReturn)->changed_datastore = pReturnDatastore;
1709 if (session->pkg_status == SYNCML_PKG_2 && session->is_receiving_final)
1710 session->pkg_status = SYNCML_PKG_3;
1711 else if (session->pkg_status == SYNCML_PKG_4 && session->is_receiving_final) {
1712 if (only_from_client) {
1714 __clean_up_sa(session);
1715 static_session = NULL;
1717 session->pkg_status = SYNCML_PKG_5;
1718 } else if (session->pkg_status == SYNCML_PKG_6 && session->is_receiving_final) {
1720 __clean_up_sa(session);
1721 static_session = NULL;
1727 free_syncml(syncml);
1734 static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session)
1737 _DEBUG_TRACE("id = %s, pwd = %s, target_url = %s", id, pwd, target_url);
1739 sa_error_type_e errorType = SA_INTERNAL_OK;
1742 char *credData = NULL;
1743 cred_s *pCred = NULL;
1744 sync_hdr_s *pSyncHdr = NULL;
1745 syncml_s *syncml = NULL;
1747 GList *commands = NULL;
1748 GList *commands_last = NULL;
1750 errorType = create_cred_string(AUTH_TYPE_BASIC, id, pwd, NULL, 0, &credData);
1751 if (errorType != SA_INTERNAL_OK) {
1752 _DEBUG_ERROR("failed to create cred string");
1756 errorType = create_cred(id, pwd, AUTH_TYPE_BASIC, FORMAT_TYPE_BASE64, credData, &pCred);
1757 if (errorType != SA_INTERNAL_OK) {
1758 _DEBUG_ERROR("failed to create cred");
1762 if (credData != NULL) {
1767 set_session_cred(session, pCred);
1769 errorType = create_sync_hdr(session, &pSyncHdr);
1770 if (errorType != SA_INTERNAL_OK) {
1771 _DEBUG_ERROR("failed to create synchdr");
1775 char *sourceDevInf = NULL;
1776 if (session->protocol_version == VERSION_10)
1777 sourceDevInf = ELEMENT_DEVINF_10;
1778 else if (session->protocol_version == VERSION_11)
1779 sourceDevInf = ELEMENT_DEVINF_11;
1780 else if (session->protocol_version == VERSION_12)
1781 sourceDevInf = ELEMENT_DEVINF_12;
1783 location_s *pLocation = NULL;
1784 errorType = create_location(sourceDevInf, NULL, &pLocation);
1785 if (errorType != SA_INTERNAL_OK) {
1786 _DEBUG_ERROR("failed to create location");
1790 char *contentType = NULL;
1791 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1792 contentType = ELEMENT_DEVINF_XML;
1794 contentType = ELEMENT_DEVINF_WBXML;
1796 /* create Get command */
1797 command_s *pGetCommand = NULL;
1798 errorType = create_get_command(session, pLocation, contentType, &pGetCommand);
1799 if (errorType != SA_INTERNAL_OK) {
1800 _DEBUG_ERROR("failed to get command");
1803 put_into_list(&commands, &commands_last, pGetCommand);
1805 errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml);
1806 if (errorType != SA_INTERNAL_OK) {
1807 _DEBUG_ERROR("failed to get syncml");
1811 errorType = __object_binder(syncml, false, msg, msg_size);
1812 if (errorType != SA_INTERNAL_OK) {
1813 _DEBUG_ERROR("failed in objectBinder");
1817 /*free generate structure */
1818 free_syncml(syncml);
1825 if (pSyncHdr != NULL)
1826 free_sync_hdr(pSyncHdr);
1829 if (credData != NULL)
1831 if (commands != NULL)
1832 free_commands(commands);
1840 static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session)
1844 sa_error_type_e errorType = SA_INTERNAL_OK;
1846 syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s));
1847 if (syncml == NULL) {
1848 _DEBUG_ERROR("failed to allocate syncml");
1849 errorType = SA_INTERNAL_NO_MEMORY;
1853 errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length);
1854 if (errorType != SA_INTERNAL_OK) {
1855 _DEBUG_ERROR("failed in reverseObjectBinder");
1859 /* check receive msg */
1860 errorType = receive_header(session, syncml->hdr);
1861 if (errorType != SA_INTERNAL_OK) {
1862 _DEBUG_ERROR("failed in receive_Header");
1866 errorType = receive_statuses(session, syncml->status, NULL);
1867 if (errorType != SA_INTERNAL_OK) {
1868 _DEBUG_ERROR("failed in receive_Statuses");
1872 errorType = receive_commands(session, syncml->commands, true, NULL);
1873 if (errorType != SA_INTERNAL_OK) {
1874 _DEBUG_ERROR("failed in receive_Commands");
1881 free_syncml(syncml);
1887 static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size)
1891 sa_error_type_e errorType = SA_INTERNAL_OK;
1893 sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
1895 sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
1897 sync_agent_pb_protocol_binder_info_s *binder = NULL;
1899 unsigned int xml_len = 0;
1901 if (server_flag != true)
1902 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set);
1904 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set);
1906 err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL);
1907 err = oma_ds_1_2_binder_append(binder, PE_HEADER, syncml->hdr);
1908 err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL);
1910 GList *status_iter = NULL;
1911 status_s *pStatus = NULL;
1912 for (status_iter = syncml->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
1913 pStatus = status_iter->data;
1914 err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus);
1918 command_s *pCommand = NULL;
1919 oma_ds_protocol_element_e protocol_element = PE_UNDEF;
1920 for (iter = syncml->commands; iter != NULL; iter = g_list_next(iter)) {
1921 pCommand = (command_s *) (iter->data);
1923 command_type_e type = pCommand->type;
1925 case COMMAND_TYPE_UNKNOWN:
1926 protocol_element = PE_UNDEF;
1928 case COMMAND_TYPE_ALERT:
1929 protocol_element = PE_ALERT;
1931 case COMMAND_TYPE_SYNC_START:
1932 protocol_element = PE_SYNC_START;
1934 case COMMAND_TYPE_SYNC_END:
1935 protocol_element = PE_SYNC_END;
1937 case COMMAND_TYPE_PUT:
1938 protocol_element = PE_PUT_START;
1940 case COMMAND_TYPE_ADD:
1941 protocol_element = PE_ADD;
1943 case COMMAND_TYPE_REPLACE:
1944 protocol_element = PE_REPLACE;
1946 case COMMAND_TYPE_DELETE:
1947 protocol_element = PE_DELETE;
1949 case COMMAND_TYPE_MAP:
1950 protocol_element = PE_MAP;
1952 case COMMAND_TYPE_GET:
1953 protocol_element = PE_GET;
1955 case COMMAND_TYPE_RESULTS:
1956 protocol_element = PE_RESULTS_START;
1958 case COMMAND_TYPE_HEADER:
1959 protocol_element = PE_HEADER;
1962 err = oma_ds_1_2_binder_append(binder, protocol_element, pCommand);
1964 if (type == COMMAND_TYPE_RESULTS) {
1965 err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL);
1968 if (type == COMMAND_TYPE_PUT) {
1969 if (pCommand->private.access.item != NULL) {
1970 if (pCommand->private.access.item->private.devinf != NULL) {
1971 err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pCommand->private.access.item);
1974 err = oma_ds_1_2_binder_append(binder, PE_PUT_END, NULL);
1978 if (syncml->final) {
1979 err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL);
1982 err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL);
1983 err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL);
1985 oma_ds_1_2_binder_get_stream(binder, msg, msg_size);
1987 /*FIXME for debugging */
1988 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1989 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
1991 err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len);
1992 if (err == SYNC_AGENT_PB_RETURN_OK) {
1993 set_xml_to_file(xml, OMA_DS_MSG_PATH);
2002 oma_ds_1_2_binder_terminate(binder);
2004 if (err != SYNC_AGENT_PB_RETURN_OK)
2005 errorType = SA_INTERNAL_BINDER_ERROR;
2011 static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length)
2015 sa_error_type_e errorType = SA_INTERNAL_OK;
2016 sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
2018 sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
2019 sync_agent_pb_protocol_binder_reverse_info_s *binder = NULL;
2021 unsigned int xml_len = 0;
2023 sync_agent_pb_decoding_e dec;
2024 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
2025 dec = SYNC_AGENT_PB_DECODING_XML;
2027 dec = SYNC_AGENT_PB_DECODING_WBXML;
2029 err = reverse_oma_ds_1_2_binder_init(recv_msg, recv_msg_length, dec, binder_function_set, NULL, &binder);
2031 if (err != SYNC_AGENT_PB_RETURN_OK) {
2032 errorType = SA_INTERNAL_BINDER_ERROR;
2033 _DEBUG_ERROR("reverse_oma_ds_1_2_binder_init error =%d", err);
2037 /*FIXME for debugging */
2038 if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
2039 set_xml_to_file(recv_msg, OMA_DS_MSG_PATH);
2041 err = sync_agent_get_xml_from_reverse_protocol_binder(binder, &xml, &xml_len);
2042 if (err == SYNC_AGENT_PB_RETURN_OK) {
2043 set_xml_to_file(xml, OMA_DS_MSG_PATH);
2052 oma_ds_protocol_element_e protocol_element = PE_UNDEF;
2053 char *protocol_element_name = NULL;
2054 Content_Ptr pContent = NULL;
2055 command_s *tempCommandfordevInf = NULL;
2056 while (err == SYNC_AGENT_PB_RETURN_OK) {
2057 protocol_element = PE_UNDEF;
2058 err = reverse_oma_ds_1_2_binder_next(binder, &protocol_element, &protocol_element_name, &pContent);
2059 switch (protocol_element) {
2061 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2063 case PE_ATOMIC_START:
2067 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2071 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2075 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2076 tempCommandfordevInf = pContent;
2079 case PE_SEQUENCE_START:
2080 case PE_SEQUENCE_END:
2082 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2085 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2086 protocol_element = PE_UNDEF;
2089 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2092 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2094 case PE_RESULTS_START:
2095 (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2096 tempCommandfordevInf = pContent;
2099 (*syncml)->status = g_list_append((*syncml)->status, pContent);
2100 protocol_element = PE_UNDEF;
2103 (*syncml)->hdr = pContent;
2104 sync_agent_set_user_data_in_reverse_protocol_binder(binder, (*syncml)->hdr);
2110 (*syncml)->final = (int)pContent;
2113 if (tempCommandfordevInf != NULL) {
2114 tempCommandfordevInf->private.results.item->private.devinf = pContent;
2115 tempCommandfordevInf = NULL;
2122 if (protocol_element_name != NULL) {
2123 free(protocol_element_name);
2124 protocol_element_name = NULL;
2127 if (err == SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT) {
2132 sync_agent_destroy_reverse_protocol_binder(binder);
2134 free_oma_ds_1_2_binder_function_set(binder_function_set);
2136 if (err != SYNC_AGENT_PB_RETURN_OK && err != SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT)
2137 errorType = SA_INTERNAL_BINDER_ERROR;
2143 static common_error_type_e _convert_error_type(sa_error_type_e error_type)
2147 common_error_type_e sa_errorType = COMMON_OK;
2149 if (error_type == SA_INTERNAL_OK)
2150 sa_errorType = COMMON_OK;
2151 else if (error_type == SA_INTERNAL_MISCONFIGURATION)
2152 sa_errorType = COMMON_MISCONFIGURATION;
2153 else if (error_type == SA_INTERNAL_AUTHENTICATION_ERROR)
2154 sa_errorType = COMMON_AUTHENTICATION_ERROR;
2155 else if (error_type == SA_INTERNAL_NOT_FOUND)
2156 sa_errorType = COMMON_NOT_FOUND;
2157 else if (error_type == SA_INTERNAL_NO_MEMORY)
2158 sa_errorType = COMMON_NO_MEMORY;
2159 else if (error_type == SA_INTERNAL_NOT_DEFINED || error_type == SA_INTERNAL_BINDER_ERROR || error_type == SA_INTERNAL_ERROR || error_type == SA_INTERNAL_DA_ERROR)
2160 sa_errorType = COMMON_INTERNAL_ERROR;
2161 else if (error_type == SA_INTERNAL_SERVER_ERROR || error_type == SA_INTERNAL_SERVER_FAILURE)
2162 sa_errorType = COMMON_SERVER_ERROR;
2163 else if (error_type == SA_INTERNAL_CONNECTION_ERROR)
2164 sa_errorType = COMMON_CONNECTION_ERROR;
2165 else if (error_type == SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER)
2166 sa_errorType = COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2167 else if (error_type == SA_INTERNAL_CANCEL)
2168 sa_errorType = COMMON_CANCEL;
2169 else if (error_type == SA_INTERNAL_BUSY_SIGNALING)
2170 sa_errorType = COMMON_BUSY_SIGNALING;
2171 else if (error_type == SA_INTERNAL_SUSPEND_FAIL)
2172 sa_errorType = COMMON_SUSPEND_FAIL;
2175 return sa_errorType;
2177 /*from processRecvMsg
2178 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
2179 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
2180 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
2182 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
2184 1513 -> ERROR_INTERNAL_NOT_DEFINED
2185 1512 -> ERROR_INTERNAL_NO_MEMORY
2186 1503 -> ERROR_INTERNAL_BINDER_ERROR
2188 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
2189 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error)
2191 can be return to engine value
2192 from generatePreSyncMsg
2193 1506 -> ERROR_INTERNAL_MISCONFIGURATION : need configure infomation
2194 1500 -> ERROR_INTERNAL_OK : generatePreSyncMsg has been completed without any error
2195 1512 -> ERROR_INTERNAL_NO_MEMORY : failed to allocate memory
2196 1513 -> ERROR_INTERNAL_NOT_DEFINED : something is not defined that must have
2197 1503 -> ERROR_INTERNAL_BINDER_ERROR : binder error
2202 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
2203 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
2204 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
2206 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
2208 1513 -> ERROR_INTERNAL_NOT_DEFINED
2209 1512 -> ERROR_INTERNAL_NO_MEMORY
2210 1503 -> ERROR_INTERNAL_BINDER_ERROR
2212 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
2213 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error) */
2217 static void __clean_up_sa(session_s * session)
2221 if (session != NULL) {
2223 if (session->has_opend) {
2224 sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id);
2225 session->has_opend = 0;
2228 free_session(session);
2235 static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header)
2239 sa_error_type_e errorType = SA_INTERNAL_OK;
2240 sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
2242 _DEBUG_VERBOSE(" CKECK JSESSION\n");
2243 char *temp_jsessionid = NULL;
2244 char *real_jsessionid = NULL;
2245 res = sync_agent_get_header_info(1, recv_header, "Set-Cookie", &temp_jsessionid);
2246 if (res == SYNC_AGENT_NA_SUCCESS) {
2247 if (temp_jsessionid != NULL) {
2248 real_jsessionid = strstr(temp_jsessionid, "JSESSIONID");
2249 if (real_jsessionid != NULL) {
2250 session->jsession_id = strdup(real_jsessionid);
2251 _DEBUG_VERBOSE("session->jsessionId = %s", session->jsession_id);
2256 _DEBUG_ERROR("failed in sync_agent_get_header_info = %d", res);
2258 if (temp_jsessionid != NULL)
2259 free(temp_jsessionid);
2265 static sa_error_type_e __check_resume_session(session_s * session, bool * resume)
2269 sa_error_type_e errorType = SA_INTERNAL_OK;
2273 result = get_config(session->account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, &value);
2274 _DEBUG_VERBOSE("resume = %s", value);
2275 if (result == false) {
2276 _DEBUG_ERROR("failed in get_config");
2277 errorType = SA_INTERNAL_DA_ERROR;
2281 if (strcmp(value, "1") == 0)
2296 int pre_sync(int transportType, int account_id, char *session_id, int server_flag, void **pre_sync_return_obj)
2300 sa_error_type_e errorType = SA_INTERNAL_OK;
2303 unsigned int msg_size = 0;
2304 char *recv_msg = NULL;
2305 unsigned int recv_msg_size = 0;
2306 unsigned int naci_session_id;
2308 /* create session structure */
2309 errorType = _create_session(account_id, session_id, server_flag, &static_session);
2310 if (errorType != SA_INTERNAL_OK) {
2311 _DEBUG_ERROR("failed in __create_session");
2315 /* generate preSync Msg */
2316 errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size);
2317 if (errorType != SA_INTERNAL_OK) {
2318 _DEBUG_ERROR("failed in __generate_presync_msg");
2322 if (!static_session->has_opend) {
2323 sync_agent_na_result_e res = sync_agent_open_connection(transportType, NETWORK_TIMEOUT, &naci_session_id);
2324 if (res != SYNC_AGENT_NA_SUCCESS) {
2325 _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res);
2326 errorType = SA_INTERNAL_CONNECTION_ERROR;
2329 static_session->naci_session_id = naci_session_id;
2330 static_session->has_opend = 1;
2331 _DEBUG_INFO("naci_session_id = %d", naci_session_id);
2334 errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size);
2335 if (errorType != SA_INTERNAL_OK)
2338 errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL);
2339 if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR) {
2340 /*when errortype is ERROR_AUTH_REQUIRED try request using chal just one more time
2341 it can be only happend when server required MD5 authentication(we send basic authentication every time by default) */
2343 /* create session structure */
2344 errorType = _create_session(account_id, session_id, server_flag, &static_session);
2345 if (errorType != SA_INTERNAL_OK) {
2346 _DEBUG_ERROR("failed in __create_session");
2355 if (recv_msg != NULL) {
2360 errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size);
2361 if (errorType != SA_INTERNAL_OK)
2364 errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size);
2365 if (errorType != SA_INTERNAL_OK)
2368 errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL);
2369 if (errorType != SA_INTERNAL_OK)
2380 if (recv_msg != NULL) {
2385 if (errorType != SA_INTERNAL_OK) {
2386 _DEBUG_ERROR("errorType : %d", errorType);
2387 __clean_up_sa(static_session);
2388 static_session = NULL;
2392 return _convert_error_type(errorType);
2395 int generate_msg(void **syncObj, int server_flag, char **msg, unsigned int *msg_size)
2399 sa_error_type_e errorType = SA_INTERNAL_OK;
2401 errorType = _generate_msg(static_session, syncObj, server_flag, msg, msg_size);
2402 if (errorType != SA_INTERNAL_OK)
2407 if (errorType != SA_INTERNAL_OK) {
2408 _DEBUG_ERROR("errorType : %d", errorType);
2409 __clean_up_sa(static_session);
2410 static_session = NULL;
2414 return _convert_error_type(errorType);
2417 int exchange_msg(int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length)
2421 sa_error_type_e errorType = SA_INTERNAL_OK;
2423 errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, send_msg, send_msg_length, recv_msg, recv_msg_length);
2424 if (errorType != SA_INTERNAL_OK)
2429 if (errorType != SA_INTERNAL_OK && errorType != SA_INTERNAL_CANCEL) {
2430 _DEBUG_ERROR("errorType : %d", errorType);
2431 __clean_up_sa(static_session);
2432 static_session = NULL;
2436 return _convert_error_type(errorType);
2439 int process_recv_msg(char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish)
2443 sa_error_type_e errorType = SA_INTERNAL_OK;
2445 errorType = _process_recv_msg(static_session, recv_msg, recv_msg_length, only_from_client, sync_return_obj, is_finish);
2446 if (errorType != SA_INTERNAL_OK)
2451 if (errorType != SA_INTERNAL_OK) {
2452 _DEBUG_ERROR("errorType : %d", errorType);
2453 __clean_up_sa(static_session);
2454 static_session = NULL;
2458 return _convert_error_type(errorType);
2465 __clean_up_sa(static_session);
2466 static_session = NULL;
2471 int auto_configure(char *addr, char *id, char *password, GList ** configure)
2475 sa_error_type_e errorType = SA_INTERNAL_OK;
2476 session_s *session = NULL;
2478 char *sourceUrl = NULL;
2479 char *sessionId = NULL;
2482 unsigned int msg_size = 0;
2484 char *recv_msg = NULL;
2485 unsigned int recv_msg_size = 0;
2487 bool cancel_flag = false;
2488 unsigned int session_id;
2490 location_s *pSourceLocation = NULL;
2491 location_s *pTargetLocation = NULL;
2493 sessionId = g_strdup_printf("%ld", time(NULL));
2494 if (sessionId == NULL) {
2495 errorType = SA_INTERNAL_NO_MEMORY;
2499 sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
2500 if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
2501 errorType = SA_INTERNAL_ERROR;
2505 errorType = create_location(sourceUrl, NULL, &pSourceLocation);
2506 if (errorType != SA_INTERNAL_OK) {
2507 _DEBUG_ERROR("failed in create_location");
2511 errorType = create_location(addr, NULL, &pTargetLocation);
2512 if (errorType != SA_INTERNAL_OK) {
2513 _DEBUG_ERROR("failed in create_location");
2517 errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, pSourceLocation, pTargetLocation, &session);
2518 if (errorType != SA_INTERNAL_OK) {
2519 _DEBUG_ERROR("failed in create_session");
2523 if (session == NULL) {
2524 errorType = SA_INTERNAL_NO_MEMORY;
2528 errorType = _generate_autoconfigure_msg(id, password, addr, &msg, &msg_size, session);
2529 if (errorType != SA_INTERNAL_OK)
2532 if (!session->has_opend) {
2533 sync_agent_na_result_e res = sync_agent_open_connection(TRANSPORT_TYPE, NETWORK_TIMEOUT, &session_id);
2534 if (res != SYNC_AGENT_NA_SUCCESS) {
2535 _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res);
2536 errorType = SA_INTERNAL_CONNECTION_ERROR;
2539 session->naci_session_id = session_id;
2540 session->has_opend = 1;
2541 _DEBUG_INFO("session_id = %d", session_id);
2544 cancel_flag = sync_agent_check_cancel_flag();
2546 errorType = SA_INTERNAL_CANCEL;
2551 errorType = _exchange_msg(session, DEFINE_PROTOCOL_TYPE, TRANSPORT_TYPE, msg, msg_size, &recv_msg, &recv_msg_size);
2552 if (errorType != SA_INTERNAL_OK)
2555 cancel_flag = sync_agent_check_cancel_flag();
2557 errorType = SA_INTERNAL_CANCEL;
2561 errorType = _process_autoconfigure_recv_msg(recv_msg, recv_msg_size, session);
2562 if (errorType != SA_INTERNAL_OK)
2565 cancel_flag = sync_agent_check_cancel_flag();
2567 errorType = SA_INTERNAL_CANCEL;
2571 devinf_s *devInf = session->remote_devinf;
2572 if (devInf != NULL) {
2574 GList *tempConfigure = NULL;
2576 GList *contactCandidate = NULL;
2577 GList *calendarCandidate = NULL;
2578 GList *memoCandidate = NULL;
2580 /*set inputted name by user */
2581 contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI));
2583 char *target = NULL;
2584 target= get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI);
2585 if (target != NULL) {
2586 _DEBUG_INFO("Contacts target =%s", target);
2587 if (strcmp(target, "") !=0)
2588 contactCandidate = g_list_append(contactCandidate, strdup(target));
2595 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2597 target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2598 if (target != NULL) {
2599 _DEBUG_INFO("Organizer target =%s", target);
2600 if (strcmp(target, "") !=0)
2601 calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2608 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2610 target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2611 if (target != NULL) {
2612 _DEBUG_INFO("Memo target =%s", target);
2613 if (strcmp(target, "") !=0)
2614 memoCandidate = g_list_append(memoCandidate, strdup(target));
2622 GList *innerIter = NULL;
2623 devinf_datastore_s *devInfDataStore = NULL;
2626 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2627 devInfDataStore = iter->data;
2631 _DEBUG_INFO("devInfDataStore->sourceref = %s", devInfDataStore->source_ref);
2633 if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD_30) == 0) {
2635 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2636 candidate = innerIter->data;
2638 if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2642 if (devInfDataStore->source_ref != NULL)
2643 contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->source_ref));
2646 } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCAL) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_ICAL) == 0) {
2648 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2649 candidate = innerIter->data;
2651 if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2655 if (devInfDataStore->source_ref != NULL)
2656 calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->source_ref));
2659 } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_PLAIN) == 0) {
2661 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2662 candidate = innerIter->data;
2664 if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2668 if (devInfDataStore->source_ref != NULL)
2669 memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->source_ref));
2674 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2675 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2676 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2678 *configure = tempConfigure;
2680 // *congifure = g_list_append(*congifure, contactCandidate);
2681 // *congifure = g_list_append(*congifure, calendarCandidate);
2682 // *congifure = g_list_append(*congifure, memoCandidate);
2685 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2692 if (session != NULL) {
2693 if (session->has_opend)
2694 sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id);
2702 if (recv_msg != NULL) {
2707 if (sessionId != NULL)
2710 if (sourceUrl != NULL)
2713 if (session != NULL) {
2714 free_session(session);
2718 if (errorType != SA_INTERNAL_OK)
2719 _DEBUG_ERROR("errorType =%d", errorType);
2722 return _convert_error_type(errorType);
2725 int check_cancel_status()
2729 bool result = false;
2730 retvm_if(static_session == NULL, false, "session is NULL");
2732 if (static_session->pkg_status == SYNCML_PKG_5 || static_session->pkg_status == SYNCML_PKG_6)
2742 int suspend_sync(int transport_type, int account_id, int server_flag)
2746 sa_error_type_e errorType = SA_INTERNAL_OK;
2749 unsigned int msg_size = 0;
2750 char *recv_msg = NULL;
2751 unsigned int recv_msg_size = 0;
2753 /* generate preSync Msg */
2754 errorType = _generate_suspend_msg(static_session, server_flag, &msg, &msg_size);
2755 if (errorType != SA_INTERNAL_OK)
2759 errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, msg, msg_size, &recv_msg, &recv_msg_size);
2760 if (errorType != SA_INTERNAL_OK) {
2761 _DEBUG_ERROR("errorType = %d", errorType);
2765 errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, NULL, NULL);
2766 if (errorType != SA_INTERNAL_OK) {
2767 _DEBUG_ERROR("errorType = %d", errorType);
2778 if (recv_msg != NULL) {
2783 if (errorType != SA_INTERNAL_OK)
2784 _DEBUG_ERROR("errorType =%d", errorType);
2787 return _convert_error_type(errorType);
2790 int cancel_connection_sync_request(int transport_type)
2794 sa_error_type_e err = SA_INTERNAL_OK;
2795 sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
2797 if (static_session != NULL) {
2798 _DEBUG_INFO("sessionId = %d", static_session->naci_session_id);
2800 res = sync_agent_cancel_msg(transport_type, static_session->naci_session_id);
2801 if (res != SYNC_AGENT_NA_SUCCESS) {
2802 _DEBUG_ERROR("res = %d", res);
2803 err = SA_INTERNAL_CONNECTION_ERROR;
2811 return _convert_error_type(err);