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>
37 * @SA_Common_Interface.c
39 * @brief This file is the source file of implementation of interface of Service Adapter
42 #include "agent-framework/DACI/DACI_Agent_Handler_Manager.h"
43 #include "agent-framework/DACI/DACI_Service.h"
44 #include "agent-framework/NetworkAccess/NACI.h"
45 #include "agent-framework/EngineController/task_process.h"
46 #include "agent-framework/Utility/fw_log.h"
47 #include "agent-framework/Device/EXT_DCI_DevInfo.h"
48 #include "Common/Common_Vconf.h"
49 #include "Common/Common_Define.h"
50 #include "Common/Common_Util.h"
51 #include "ServiceAdapter/ProtocolBinder/oma_ds_protocol_binder.h"
52 #include "ServiceAdapter/NetworkBinder/NA_Binder.h"
53 #include "ServiceAdapter/SA_Define.h"
54 #include "ServiceAdapter/SA_Common_Interface.h"
55 #include "ServiceAdapter/SA_Elements.h"
56 #include "ServiceAdapter/SA_Elements_Internal.h"
57 #include "ServiceAdapter/SA_Command.h"
58 #include "ServiceAdapter/SA_Command_Internal.h"
59 #include "ServiceAdapter/SA_Session.h"
60 #include "ServiceAdapter/SA_Session_Internal.h"
61 #include "ServiceAdapter/SA_DevInf_Internal.h"
62 #include "ServiceAdapter/SA_Util.h"
63 #include "ServiceAdapter/SA_DevInf.h"
65 #define LOG_TAG "OMA_DS_SA"
67 Session *session = NULL;
69 static SA_ErrorType __get_accountinfo(int accountId, char **id, char **pwd, char **serverUrl, char **nextNonce);
70 static SA_ErrorType __object_binder(SyncML *pSyncML, char **msg, unsigned int *msg_size);
71 static SA_ErrorType __reverse_object_binder(SyncML **pSyncML, char *recv_msg, unsigned int recv_msg_length);
72 static SA_ErrorType __generate_presync_msg(int accountId, char *sessionId, char **msg, unsigned int *msg_size);
73 static SA_ErrorType __generate_msg(void **syncObj, char **msg , unsigned int *msg_size);
74 static SA_ErrorType __generate_msg_status_map_command(Session **pSession, SyncObj **syncObj,
75 GList **commands, GList **commands_last);
76 static SA_ErrorType __generate_msg_exist_sending_map_command(protocol_binder *pBinder, Session **pSession,
77 GList **commands, GList **commands_last);
78 static SA_ErrorType __generate_msg_changes_command(protocol_binder *pBinder, Session **pSession, SyncObj **sync,
79 GList **commands, GList **commands_last, bool *isNeedNextMsg);
80 static SA_ErrorType __generate_autoconfigure_msg(char *id, char *pwd, char *targetUrl, char **msg,
81 unsigned int *msg_size, Session *pSession);
82 static SA_ErrorType __exchange_msg(char *accType, int transportType, char *targetUri, unsigned int session_id,
83 GList *header_info, char *sendMsg, unsigned int sendMsg_length, GList **recv_header,
84 char **recvMsg, unsigned int *recvMsg_length);
85 static SA_ErrorType __process_recv_msg(char *recvMsg, unsigned int recvMsg_length, void **syncReturnObj, int *isFinish);
86 static SA_ErrorType __process_autoconfigure_recv_msg(char *recvMsg, unsigned int recvMsg_length, Session *pSession);
87 static Common_ErrorType __convert_error_type(SA_ErrorType errorType);
88 static void __clean_up_SA();
90 static SA_ErrorType __get_accountinfo(int accountId, char **id, char **pwd, char **serverUrl, char **nextNonce)
94 SA_ErrorType errorType = SA_INTERNAL_OK;
99 errorType = SA_INTERNAL_MISCONFIGURATION;
103 *id = DACI_Get_Account_Email_Address(accountId);
105 errorType = SA_INTERNAL_MISCONFIGURATION;
109 *pwd = DACI_Get_Account_Password(accountId);
111 errorType = SA_INTERNAL_MISCONFIGURATION;
115 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP, &value);
119 FW_LOGE("failed in get_Config");
120 errorType = SA_INTERNAL_DA_ERROR;
124 if (*serverUrl == NULL) {
125 errorType = SA_INTERNAL_MISCONFIGURATION;
129 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE, &value);
133 FW_LOGE("failed in get_Config");
134 errorType = SA_INTERNAL_DA_ERROR;
142 FW_LOGE("error :%d", errorType);
146 static SA_ErrorType __generate_presync_msg(int accountId, char *sessionId, char **msg, unsigned int *msg_size)
150 SA_ErrorType errorType = SA_INTERNAL_OK;
153 char *sourceUrl = NULL;
154 char *targetUrl = NULL;
155 char *nextNonce = NULL;
156 char *credData = NULL;
157 char *decoded_nonce = NULL;
159 /*pkg1 always has final tag*/
162 SyncHdr *pSyncHdr = NULL;
164 DevInf *devInfObj = NULL;
165 SyncML *pSyncML = NULL;
167 /*case SAN, MUST use sessionid from SAN message..*/
169 sessionId = g_strdup_printf("%ld", time(NULL));
170 if (sessionId == NULL) {
171 errorType = SA_INTERNAL_NO_MEMORY;
176 DCI_RETURN err = DCI_Get_DevInfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
177 if (err != DCI_SUCCESS) {
178 FW_LOGE("failed to get devinfo");
179 errorType = SA_INTERNAL_ERROR;
183 errorType = __get_accountinfo(accountId, &id, &pwd, &targetUrl, &nextNonce);
184 if (errorType != SA_INTERNAL_OK)
187 FW_LOGV("id = %s", id);
188 FW_LOGV("pwd = %s", pwd);
189 FW_LOGV("sessionId = %s", sessionId);
190 FW_LOGV("sourceUrl = %s", sourceUrl);
191 FW_LOGV("serverUrl = %s", targetUrl);
193 Chal *pTempChal = NULL;
194 if (session != NULL) {
195 if (session->chal != NULL)
196 pTempChal = dup_chal(session->chal);
198 free_session(session);
202 /* create session info */
203 errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, accountId, sessionId, sourceUrl, targetUrl, &session);
204 if (errorType != SA_INTERNAL_OK) {
205 FW_LOGE("failed to create session");
209 if (pTempChal != NULL) {
210 session->chal = pTempChal;
215 session->pkgStatus = SYNCML_PKG_1;
217 Chal *chal = session->chal;
219 FormatType formatType = FORMAT_TYPE_UNKNOWN;
220 unsigned int nonce_size = 0;
222 FW_LOGV("CHAL DOES NOT EXIST");
224 /*server sent nonce info in previous sync session*/
225 authType = AUTH_TYPE_MD5;
226 decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
227 formatType = FORMAT_TYPE_BASE64;
229 authType = AUTH_TYPE_BASIC;
230 formatType = FORMAT_TYPE_BASE64;
233 errorType = create_cred_string(authType, id, pwd, decoded_nonce, nonce_size, &credData);
235 if (errorType != SA_INTERNAL_OK) {
236 FW_LOGE("failed to create cred string");
240 errorType = create_cred(id, pwd, authType, formatType, credData, &pCred);
242 FW_LOGV("CHAL DOES EXIST");
243 if (chal->type == AUTH_TYPE_MD5) {
244 if (chal->format == FORMAT_TYPE_BASE64)
245 decoded_nonce = (char *)g_base64_decode(chal->nonce_b64, &nonce_size);
247 if (chal->nonce_plain != NULL) {
248 decoded_nonce = strdup(chal->nonce_plain);
249 nonce_size = chal->nonce_length;
254 errorType = create_cred_string(chal->type, id, pwd, decoded_nonce, nonce_size, &credData);
255 if (errorType != SA_INTERNAL_OK) {
256 FW_LOGE("failed to create cred string");
260 errorType = create_cred(id, pwd, chal->type, FORMAT_TYPE_BASE64, credData , &pCred);
263 if (credData != NULL) {
268 if (decoded_nonce != NULL) {
270 decoded_nonce = NULL;
273 if (nextNonce != NULL) {
279 if (errorType != SA_INTERNAL_OK) {
280 FW_LOGE("failed to create cred");
284 set_session_cred(session, pCred);
288 errorType = create_synchdr(session , &pSyncHdr);
289 if (errorType != SA_INTERNAL_OK) {
290 FW_LOGE("failed to create synchdr");
295 errorType = create_devinf(session, &devInfObj);
296 if (errorType != SA_INTERNAL_OK) {
297 FW_LOGE("failed to create devInf");
301 set_session_devinf(session, devInfObj);
304 GList *commands = NULL;
305 GList *commands_last = NULL;
307 char *sourceDevInf = NULL;
308 if (session->protocolVersion == VERSION_10)
309 sourceDevInf = ELEMENT_DEVINF_10;
310 else if (session->protocolVersion == VERSION_11)
311 sourceDevInf = ELEMENT_DEVINF_11;
312 else if (session->protocolVersion == VERSION_12)
313 sourceDevInf = ELEMENT_DEVINF_12;
316 /* create Put command */
318 Command *pPutCommand = NULL;
319 Location *pLocation = NULL;
321 errorType = create_Location(sourceDevInf, NULL, &pLocation);
322 if (errorType != ERROR_INTERNAL_OK)
325 errorType = create_Put_Command(session, pLocation, ELEMENT_DEVINF_XML, devInfObj , &pPutCommand);
326 if (errorType !=ERROR_INTERNAL_OK)
328 putIntoList(&commands, &commands_last, pPutCommand);
332 /* create Get command */
333 Command *pGetCommand = NULL;
334 Location *pLocation = NULL;
335 errorType = create_location(sourceDevInf, NULL, &pLocation);
336 if (errorType != SA_INTERNAL_OK) {
337 FW_LOGE("failed to create location");
341 char *contentType = NULL;
342 if (COMMUNICATION_TYPE == ENCODING_XML)
343 contentType = ELEMENT_DEVINF_XML;
345 contentType = ELEMENT_DEVINF_WBXML;
347 errorType = create_get_command(session, pLocation, contentType, &pGetCommand);
348 if (errorType != SA_INTERNAL_OK) {
349 FW_LOGE("failed to create get command");
352 put_into_list(&commands, &commands_last, pGetCommand);
354 Command *pAlertCommand = NULL;
355 Location *pSourceLocation = NULL;
356 Location *pTargetLocation = NULL;
358 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
359 if (datastoreinfo_per_content_type[content_type]->clientSyncType) {
360 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
361 if (errorType != SA_INTERNAL_OK) {
362 FW_LOGE("failed to create location");
364 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
365 when SA_INTERNAL_NOT_DEFINED error case*/
366 if (errorType == SA_INTERNAL_NOT_DEFINED)
367 errorType = SA_INTERNAL_MISCONFIGURATION;
372 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
373 if (errorType != SA_INTERNAL_OK) {
374 FW_LOGE("failed to create location");
376 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
377 when SA_INTERNAL_NOT_DEFINED error case*/
378 if (errorType == SA_INTERNAL_NOT_DEFINED)
379 errorType = SA_INTERNAL_MISCONFIGURATION;
383 if (datastoreinfo_per_content_type[content_type]->id && datastoreinfo_per_content_type[content_type]->pw) {
385 /*FIXME is there a nextNonce for datastore ??*/
387 /*server sent nonce info in previous sync session*/
388 authType = AUTH_TYPE_MD5;
389 decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
391 authType = AUTH_TYPE_BASIC;
392 formatType = FORMAT_TYPE_BASE64;
395 errorType = create_cred_string(authType, datastoreinfo_per_content_type[content_type]->id,
396 datastoreinfo_per_content_type[content_type]->pw, decoded_nonce, nonce_size, &credData);
397 if (errorType != SA_INTERNAL_OK) {
398 FW_LOGE("failed to create cred string");
402 errorType = create_cred(id, pwd, authType, FORMAT_TYPE_BASE64, credData, &pCred);
404 if (credData != NULL) {
410 errorType = create_alert_command(session, datastoreinfo_per_content_type[content_type]->clientSyncType,
411 pSourceLocation, pTargetLocation, datastoreinfo_per_content_type[content_type]->lastAnchorClient,
412 datastoreinfo_per_content_type[content_type]->nextAnchorClient, pCred, &pAlertCommand);
413 if (errorType != SA_INTERNAL_OK) {
414 FW_LOGE("failed to create alert command");
419 put_into_list(&commands, &commands_last, pAlertCommand);
423 errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
424 if (errorType != SA_INTERNAL_OK) {
425 FW_LOGE("failed to create syncml");
429 session->isSendingfinal = isFinal;
431 session->pkgStatus = SYNCML_PKG_2;
434 errorType = __object_binder(pSyncML, msg, msg_size);
435 if (errorType != SA_INTERNAL_OK) {
436 FW_LOGE("failed in objectBinder");
440 /*free generate structure*/
441 free_syncml(pSyncML);
464 if (credData != NULL)
480 free_syncml(pSyncML);
482 free_synchdr(pSyncHdr);
487 FW_LOGE("error : %d", errorType);
491 static SA_ErrorType __generate_msg(void **syncObj, char **msg , unsigned int *msg_size)
495 SA_ErrorType errorType = SA_INTERNAL_OK;
496 bool isNeedNextMsg = false;
498 Binder_function_set *pBinder_function_set = NULL;
500 PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
502 protocol_binder *pBinder = NULL;
503 pBinder = oma_ds_1_2_binder_init(pBinder, COMMUNICATION_TYPE, false, false, false, true, pBinder_function_set);
506 SyncHdr *pSyncHdr = NULL;
507 errorType = create_synchdr(session , &pSyncHdr);
508 if (errorType != SA_INTERNAL_OK) {
509 FW_LOGE("failed to create synchdr");
513 err = oma_ds_1_2_binder_append(pBinder, PE_SYNCML_START, NULL);
514 err = oma_ds_1_2_binder_append(pBinder, PE_HEADER, pSyncHdr);
515 err = oma_ds_1_2_binder_append(pBinder, PE_BODY_START, NULL);
517 SyncObj **sync = (SyncObj **)syncObj;
518 GList *commands = NULL;
519 GList *commands_last = NULL;
522 errorType = __generate_msg_status_map_command(&session, sync, &commands, &commands_last);
523 if (errorType != SA_INTERNAL_OK) {
524 FW_LOGE("failed in __generate_msg_status_map_command");
528 /*append status command*/
529 GList *status_iter = NULL;
530 Status *pStatus = NULL;
531 for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
532 pStatus = (Status *)status_iter->data;
533 err = oma_ds_1_2_binder_append(pBinder, PE_STATUS, pStatus);
536 /*append results command*/
537 GList *results_iter = NULL;
538 Command *pResultsCommand = NULL;
539 for (results_iter = session->resultsCommand; results_iter != NULL; results_iter = g_list_next(results_iter)) {
540 pResultsCommand = (Command *)results_iter->data;
541 put_into_list(&commands, &commands_last, pResultsCommand);
542 err = oma_ds_1_2_binder_append(pBinder, PE_RESULTS_START, pResultsCommand);
544 if (pResultsCommand->type == COMMAND_TYPE_RESULTS) {
545 if (pResultsCommand->private.results.item) {
546 if (pResultsCommand->private.results.item->dataType == ITEM_DEVINF) {
547 err = oma_ds_1_2_binder_append(pBinder, PE_DEVINF, pResultsCommand->private.results.item);
551 err = oma_ds_1_2_binder_append(pBinder, PE_RESULTS_END, NULL);
555 /*append map command*/
556 GList *map_iter = NULL;
557 Command *pMapCommand = NULL;
558 for (map_iter = session->mapCommand; map_iter != NULL; map_iter = g_list_next(map_iter)) {
559 pMapCommand = (Command *)map_iter->data;
560 err = oma_ds_1_2_binder_append(pBinder, PE_MAP, pMapCommand);
563 /* free sendgStatus structure from SE*/
564 free_sendingstatuses((*sync)->sendingStatus);
565 (*sync)->sendingStatus = NULL;
567 if (session->pkgStatus == SYNCML_PKG_3) {
569 errorType = __generate_msg_exist_sending_map_command(pBinder, &session, &commands, &commands_last);
570 if (errorType != SA_INTERNAL_OK) {
571 FW_LOGE("failed in __generate_msg_status_map_command");
575 errorType = __generate_msg_changes_command(pBinder, &session, sync, &commands, &commands_last, &isNeedNextMsg);
576 if (errorType != SA_INTERNAL_OK) {
577 FW_LOGE("failed in __generate_msg_status_map_command");
585 if (session->pkgStatus == SYNCML_PKG_5 && session->isReceivingFinal)
588 /*if we have to send alert command(222,223 etc)*/
589 GList *alert_iter = NULL;
590 Command *pAlertCommand = NULL;
591 for (alert_iter = session->alertCommand; alert_iter != NULL; alert_iter = g_list_next(alert_iter)) {
592 pAlertCommand = alert_iter->data;
593 put_into_list(&commands, &commands_last, pAlertCommand);
594 err = oma_ds_1_2_binder_append(pBinder, PE_ALERT, pAlertCommand);
598 err = oma_ds_1_2_binder_append(pBinder, PE_FINAL, NULL);
600 session->isSendingfinal = isFinal;
602 err = oma_ds_1_2_binder_append(pBinder, PE_BODY_END, NULL);
603 err = oma_ds_1_2_binder_append(pBinder, PE_SYNCML_END, NULL);
605 oma_ds_1_2_binder_get_stream(pBinder, msg, msg_size);
606 FW_LOGV("final msg size = %d", *msg_size);
608 /*FIXME for debugging*/
610 unsigned int xml_len = 0;
611 if (COMMUNICATION_TYPE == ENCODING_XML)
612 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
614 err = protocol_binder_get_xml_for_debugging(pBinder, &xml, &xml_len);
615 if (err == PROTOCOL_BINDER_OK) {
616 set_xml_to_file(xml, OMA_DS_MSG_PATH);
625 oma_ds_1_2_binder_terminate(pBinder);
627 if (session->pkgStatus == SYNCML_PKG_3 && session->isSendingfinal)
628 session->pkgStatus = SYNCML_PKG_4;
629 else if (session->pkgStatus == SYNCML_PKG_5 && session->isSendingfinal)
630 session->pkgStatus = SYNCML_PKG_6;
632 free_synchdr(pSyncHdr);
633 if (session->status != NULL) {
634 free_statuses(session->status);
635 session->status = NULL;
637 free_commands(commands);
639 session->status = NULL;
640 session->alertCommand = NULL;
641 session->resultsCommand = NULL;
647 FW_LOGE("error : %d", errorType);
649 free_synchdr(pSyncHdr);
650 if (session->status != NULL) {
651 free_statuses(session->status);
652 session->status = NULL;
654 free_commands(commands);
659 static SA_ErrorType __generate_msg_status_map_command(Session **pSession, SyncObj **sync, GList **commands, GList **commands_last)
662 SA_ErrorType errorType = SA_INTERNAL_OK;
663 DACI_RETURN da_err = DACI_SUCCESS;
666 SendingStatus *sendingStatus = NULL;
667 /* send status of sync, add, replace, delete command */
668 for (iter = (*sync)->sendingStatus; iter != NULL; iter = g_list_next(iter)) {
669 sendingStatus = (iter->data);
672 GList *sendingIter = NULL;
673 AppliedStatus *appliedStatus = NULL;
674 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
675 appliedStatus = (sendingIter->data);
677 /* set data in status of add, replace, delete command */
678 GList *statusIter = NULL;
679 Status *status = NULL;
680 for (statusIter = (*pSession)->tempStatus; statusIter != NULL; statusIter = g_list_next(statusIter)) {
681 status = statusIter->data;
683 if (status->type == COMMAND_TYPE_ADD ||
684 status->type == COMMAND_TYPE_REPLACE ||
685 status->type == COMMAND_TYPE_DELETE) {
687 /* if there is a status for add command it have to be checked
688 because map command has to be generated
689 otherwise replace, delete command doesnot need to generate map command*/
690 if (status->type == COMMAND_TYPE_ADD)
693 if (status->sourceRef) {
696 guid = DACI_Get_Mapping_Guid((*pSession)->accountId, appliedStatus->luid);
698 errorType = SA_INTERNAL_DA_ERROR;
699 FW_LOGE("failed to get guid");
702 if (strcmp(guid, get_location_locuri(status->sourceRef)) == 0) {
703 status->data = g_strdup_printf("%i", appliedStatus->status);
704 /*move to status list from tempStatus*/
705 (*pSession)->status = g_list_append((*pSession)->status, status);
706 (*pSession)->tempStatus = g_list_remove((*pSession)->tempStatus, status);
722 if (status->targetRef) {
724 if (strcmp(appliedStatus->luid, get_location_locuri(status->targetRef)) == 0) {
725 status->data = g_strdup_printf("%i", appliedStatus->status);
726 /*move to status list from tempStatus*/
727 (*pSession)->status = g_list_append((*pSession)->status, status);
728 (*pSession)->tempStatus = g_list_remove((*pSession)->tempStatus, status);
737 /* create map command */
738 Command *mapCommand = NULL;
739 Location *pSourceLocation = NULL;
740 Location *pTargetLocation = NULL;
741 errorType = create_location(sendingStatus->source, NULL, &pSourceLocation);
742 if (errorType != SA_INTERNAL_OK) {
743 FW_LOGE("failed to create location");
747 errorType = create_location(sendingStatus->target, NULL, &pTargetLocation);
748 if (errorType != SA_INTERNAL_OK) {
749 FW_LOGE("failed to create location");
753 errorType = create_map_command(*pSession, pSourceLocation, pTargetLocation, &mapCommand);
754 if (errorType != SA_INTERNAL_OK) {
755 FW_LOGE("failed to create map command");
759 GList *sendingIter = NULL;
760 AppliedStatus *appliedStatus = NULL;
762 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
763 appliedStatus = (sendingIter->data);
765 if (appliedStatus->changeType == CHANGE_ADD) {
767 if (appliedStatus->status == ITEM_ADDED) {
768 Item *temp = create_item();
770 FW_LOGE("failed to create item");
771 errorType = SA_INTERNAL_NO_MEMORY;
775 errorType = create_location(appliedStatus->luid, NULL, &pSourceLocation);
776 if (errorType != SA_INTERNAL_OK) {
777 FW_LOGE("failed to create location");
782 guid = DACI_Get_Mapping_Guid((*pSession)->accountId, appliedStatus->luid);
784 errorType = SA_INTERNAL_DA_ERROR;
785 FW_LOGE("failed to get guid");
788 errorType = create_location(guid, NULL, &pTargetLocation);
789 if (errorType != SA_INTERNAL_OK) {
790 FW_LOGE("failed to create location");
799 set_item_source(temp, pSourceLocation);
800 set_item_target(temp, pTargetLocation);
801 set_map_command_item(mapCommand, temp);
803 da_err = DACI_Delete_Mapping_By_Luid((*pSession)->accountId, appliedStatus->luid);
804 if (da_err != DACI_SUCCESS) {
805 errorType = SA_INTERNAL_DA_ERROR;
806 FW_LOGE("failed in DACI_Delete_Mapping_By_Luid");
812 put_into_list(commands, commands_last, mapCommand);
813 (*pSession)->mapCommand = g_list_append((*pSession)->mapCommand, mapCommand);
814 increase_command_refcount(mapCommand);
824 FW_LOGE("error : %d", errorType);
828 static SA_ErrorType __generate_msg_exist_sending_map_command(protocol_binder *pBinder, Session **pSession,
829 GList **commands, GList **commands_last)
832 SA_ErrorType errorType = SA_INTERNAL_OK;
833 PROTOCOL_BINDER_ERROR err = PROTOCOL_BINDER_OK;
835 /*if there is something in the mapping_tbl it have to be sent before sync command*/
836 int existSendingMap = DACI_IsExist_Mapping_By_Account_Id(session->accountId);
838 if (existSendingMap) {
840 daci_mapping *pDaci_mapping = DACI_Get_Mapping_By_Account_Id((*pSession)->accountId, &count);
841 FW_LOGV("count = %d", count);
843 Location *pSourceLocation = NULL;
844 Location *pTargetLocation = NULL;
848 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
849 FW_LOGV("datastoreinfo_per_content_type[%d]->datastore_id = %d", content_type, datastoreinfo_per_content_type[content_type]->datastore_id);
851 if(datastoreinfo_per_content_type[content_type]->clientSyncType){
852 if (pDaci_mapping[0].data_store_id == datastoreinfo_per_content_type[content_type]->datastore_id) {
853 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
854 if (errorType != SA_INTERNAL_OK) {
855 FW_LOGE("failed to create location");
859 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL , &pTargetLocation);
860 if (errorType != SA_INTERNAL_OK) {
861 FW_LOGE("failed to create location");
871 if (exist == false) {
872 FW_LOGV("Mapping data is not equal with any synchronizing datastore id");
876 /* create map command using first item */
877 Command *mapCommand = NULL;
878 errorType = create_map_command(*pSession, pSourceLocation, pTargetLocation, &mapCommand);
879 if (errorType != SA_INTERNAL_OK) {
880 FW_LOGE("failed to create map command");
884 int lastItemTypeId = pDaci_mapping[0].data_store_id;
886 for (i = 0; i < count; i++) {
887 if (lastItemTypeId != pDaci_mapping[i].data_store_id) {
888 err = oma_ds_1_2_binder_append(pBinder, PE_MAP, mapCommand);
889 put_into_list(commands, commands_last, mapCommand);
890 (*pSession)->mapCommand = g_list_append((*pSession)->mapCommand, mapCommand);
891 increase_command_refcount(mapCommand);
895 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
897 if(datastoreinfo_per_content_type[content_type]->clientSyncType){
898 if (pDaci_mapping[0].data_store_id == datastoreinfo_per_content_type[content_type]->datastore_id) {
899 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
900 if (errorType != SA_INTERNAL_OK) {
901 FW_LOGE("failed to create location");
905 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL , &pTargetLocation);
906 if (errorType != SA_INTERNAL_OK) {
907 FW_LOGE("failed to create location");
916 if (exist == false) {
917 FW_LOGE("Mapping data is not equal with any synchronizing datastore id");
921 lastItemTypeId = pDaci_mapping[i].data_store_id;
922 Item *temp = create_item();
924 FW_LOGE("failed to create item");
925 errorType = SA_INTERNAL_NO_MEMORY;
928 errorType = create_location(pDaci_mapping[i].luid, NULL, &pSourceLocation);
929 if (errorType != SA_INTERNAL_OK) {
930 FW_LOGE("failed to create location");
934 errorType = create_location(pDaci_mapping[i].guid, NULL, &pTargetLocation);
935 if (errorType != SA_INTERNAL_OK) {
936 FW_LOGE("failed to create location");
940 set_item_source(temp, pSourceLocation);
941 set_item_target(temp, pTargetLocation);
942 set_map_command_item(mapCommand, temp);
945 DACI_Free_Memory_Mapping(pDaci_mapping, count);
947 err = oma_ds_1_2_binder_append(pBinder, PE_MAP, mapCommand);
948 put_into_list(commands, commands_last, mapCommand);
949 (*pSession)->mapCommand = g_list_append((*pSession)->mapCommand, mapCommand);
950 increase_command_refcount(mapCommand);
959 FW_LOGE("error : %d", errorType);
964 static SA_ErrorType __generate_msg_changes_command(protocol_binder *pBinder, Session **pSession, SyncObj **sync,
965 GList **commands, GList **commands_last, bool *isNeedNextMsg)
968 SA_ErrorType errorType = SA_INTERNAL_OK;
969 DACI_RETURN da_err = DACI_SUCCESS;
970 PROTOCOL_BINDER_ERROR err = PROTOCOL_BINDER_OK;
971 unsigned int stream_size = 0;
973 char *data = NULL; /*have to be freed*/
974 char *sendingData = NULL;
976 /* generate sync , add, replace, delete command when there is a item in ChangedDatastore */
978 Command *syncCommand = NULL;
979 ChangedDatastore *changedDatastore = NULL;
980 bool needNextMsg = false;
981 for (iter = (*sync)->changedDatastore; iter != NULL; iter = g_list_next(iter)) {
982 changedDatastore = (iter->data);
984 if (changedDatastore->needSyncCommand) {
985 /* create sync Command */
986 Location *pSourceLocation = NULL;
987 Location *pTargetLocation = NULL;
988 errorType = create_location(changedDatastore->source, NULL, &pSourceLocation);
989 if (errorType != SA_INTERNAL_OK) {
990 FW_LOGE("failed to create location");
994 errorType = create_location(changedDatastore->target, NULL, &pTargetLocation);
995 if (errorType != SA_INTERNAL_OK) {
996 FW_LOGE("failed to create location");
1000 errorType = create_sync_start_command(*pSession, pSourceLocation, pTargetLocation, &syncCommand);
1001 if (errorType != SA_INTERNAL_OK) {
1002 FW_LOGE("failed to create sync start command");
1006 /*set numberOfChanges */
1007 if (changedDatastore->hasNumberOfChanges) {
1008 /*if it is first sync command set numberOfChanges*/
1009 set_sync_start_command_numberofchanges(syncCommand, changedDatastore->numberOfChanges);
1010 changedDatastore->hasNumberOfChanges = 0;
1013 err = oma_ds_1_2_binder_append(pBinder, PE_SYNC_START, syncCommand);
1014 put_into_list(commands, commands_last, syncCommand);
1016 GList *itemIter = NULL;
1017 Command *changeCommand = NULL;
1018 ChangedItem *changedItem = NULL;
1019 OMA_DS_Protocol_Element protocol_element = PE_UNDEF;
1020 for (itemIter = changedDatastore->changeItem; itemIter != NULL;) {
1021 changedItem = (itemIter->data);
1023 oma_ds_1_2_binder_get_stream_size(pBinder, &stream_size);
1024 FW_LOGV("stream_size = %d", stream_size);
1025 FW_LOGV("(*pSession)->targetMaxMsgSize = %d", (*pSession)->targetMaxMsgSize);
1027 if ((*pSession)->targetMaxMsgSize > stream_size) {
1028 /*there is a space for command*/
1029 if (changedItem->changeType == CHANGE_DELETE) {
1030 /* create delete Command */
1031 errorType = create_delete_command(*pSession, changedItem->changeType, changedItem->luid, changedItem->content_type, &changeCommand);
1032 if (errorType != SA_INTERNAL_OK) {
1033 FW_LOGE("failed to create delete command");
1037 int isFirstLargeObj ;
1038 if (changedItem->data == NULL) {
1039 char *folderId = NULL;
1040 daci_id_list *folder_id_list = DACI_Get_Folder_Folder_Id_By_Folder_Type_Id((*pSession)->accountId,
1041 datastoreinfo_per_content_type[changedItem->indexOfDatastore]->datastore_id,
1042 datastoreinfo_per_content_type[changedItem->indexOfDatastore]->folder_type_id);
1043 if (folder_id_list != NULL) {
1044 if(folder_id_list->count > 0)
1045 folderId = folder_id_list->id[0];
1047 FW_LOGE("failed to get folderId");
1048 errorType = SA_INTERNAL_DA_ERROR;
1052 FW_LOGE("failed in DACI_Get_Folder_Folder_Id_By_Folder_Type_Id");
1053 errorType = SA_INTERNAL_DA_ERROR;
1057 FW_LOGV("datastoreinfo_per_content_type[%d]->datastore_id = %d", changedItem->indexOfDatastore,
1058 datastoreinfo_per_content_type[changedItem->indexOfDatastore]->datastore_id);
1059 FW_LOGV("accountId = %d", (*pSession)->accountId);
1060 FW_LOGV("folderId = %s", folderId);
1061 FW_LOGV("changedItem->luid = %s", changedItem->luid);
1063 da_err = DACI_Service_Get_Item(datastoreinfo_per_content_type[changedItem->indexOfDatastore]->datastore_id,
1064 (*pSession)->accountId, folderId, changedItem->luid, (void **)&data);
1066 FW_LOGV("data = %s", data);
1067 DACI_Free_Memory_Id_List(folder_id_list);
1069 /*if data is NULL(delete item since get change_log or drawing memo)
1070 delete that item from changelog_tbl, item_tbl*/
1071 da_err = DACI_Delete_Item_By_Item_Id(changedItem->luid);
1072 if (da_err != DACI_SUCCESS) {
1073 errorType = SA_INTERNAL_DA_ERROR;
1074 FW_LOGE("failed in DACI_Delete_Item_By_Item_Id = %d", da_err);
1078 da_err = DACI_Delete_Item_Changelog_By_Item_Id((*pSession)->accountId, changedItem->luid);
1079 if (da_err != DACI_SUCCESS) {
1080 errorType = SA_INTERNAL_DA_ERROR;
1081 FW_LOGE("failed in DACI_Delete_Item_Changelog_By_Item_Id = %d", da_err);
1084 itemIter = g_list_next(itemIter);
1085 changedDatastore->changeItem = g_list_remove(changedDatastore->changeItem, changedItem);
1086 free_changeditem(changedItem);
1090 if (da_err != DACI_SUCCESS) {
1091 FW_LOGE("failed in DACI_Service_Get_Item =%d", da_err);
1092 errorType = SA_INTERNAL_DA_ERROR;
1096 isFirstLargeObj = 1;
1098 data = changedItem->data;
1099 changedItem->data = NULL;
1100 isFirstLargeObj = 0;
1103 unsigned int sizeOfData = 0;
1105 sizeOfData = strlen(data);
1107 FW_LOGV("sizeOfData = %d", sizeOfData);
1108 if ((*pSession)->targetMaxMsgSize < stream_size + sizeOfData) {
1110 int availableSize = (*pSession)->targetMaxMsgSize - stream_size;
1111 if ((*pSession)->remoteDevInf->supportsLargeObjs /*&& availableSize > session->targetMaxMsgSize / 50*/) {
1113 /*if server support LargeObj & clearly LargeObj*/
1114 sendingData = calloc(availableSize + 1, sizeof(char));
1115 if (sendingData == NULL) {
1116 FW_LOGE("failed to allocate sendingData");
1117 errorType = SA_INTERNAL_NO_MEMORY;
1120 memcpy(sendingData, data, availableSize);
1121 FW_LOGV("sendingData = %s", sendingData);
1122 FW_LOGV("sendingData size = %d", strlen(sendingData));
1124 char *remainingData = calloc(sizeOfData - availableSize + 1, sizeof(char));
1125 if (remainingData == NULL) {
1126 FW_LOGE("failed to allocate remainingData");
1127 errorType = SA_INTERNAL_NO_MEMORY;
1130 memcpy(remainingData, data+availableSize, sizeOfData - availableSize);
1131 FW_LOGV("sizeOfData - availableSize = %d", sizeOfData - availableSize);
1132 FW_LOGV("remainingData = %s", remainingData);
1133 FW_LOGV("remainingData size = %d", strlen(remainingData));
1135 if (changedItem->data)
1136 free(changedItem->data);
1138 set_changeditem_data(changedItem, remainingData);
1139 free(remainingData);
1141 PendingStatus *pTemp = NULL;
1142 errorType = create_pending_status((*pSession)->msgID, (*pSession)->cmdID, &pTemp);
1143 if (errorType != SA_INTERNAL_OK) {
1144 FW_LOGE("failed to create pending status");
1147 (*pSession)->largeObj = pTemp;
1149 if (changedItem->changeType == CHANGE_ADD) {
1150 /* create add Command */
1151 errorType = create_add_command(*pSession, changedItem->changeType, changedItem->luid,
1152 changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1 , &changeCommand);
1153 } else if (changedItem->changeType == CHANGE_REPLACE) {
1154 /* create replace Command */
1155 errorType = create_replace_command(*pSession, changedItem->changeType, changedItem->luid,
1156 changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1 , &changeCommand);
1159 if (errorType != SA_INTERNAL_OK) {
1160 FW_LOGE("failed to create add(replace) command");
1164 if (sendingData != NULL) {
1173 /*can append more commnad*/
1174 if (changedItem->changeType == CHANGE_ADD) {
1175 /* create add Command */
1176 errorType = create_add_command(*pSession, changedItem->changeType,
1177 changedItem->luid, changedItem->content_type, data, 0, 0 , &changeCommand);
1178 } else if (changedItem->changeType == CHANGE_REPLACE) {
1179 /* create replace Command */
1180 errorType = create_replace_command(*pSession, changedItem->changeType,
1181 changedItem->luid, changedItem->content_type, data, 0, 0 , &changeCommand);
1183 if (errorType != SA_INTERNAL_OK) {
1184 FW_LOGE("failed to create add(replace) command");
1194 if (changeCommand->type == COMMAND_TYPE_ADD)
1195 protocol_element = PE_ADD;
1196 else if (changeCommand->type == COMMAND_TYPE_REPLACE)
1197 protocol_element = PE_REPLACE;
1198 else if (changeCommand->type == COMMAND_TYPE_DELETE)
1199 protocol_element = PE_DELETE;
1202 err = oma_ds_1_2_binder_append(pBinder, protocol_element, changeCommand);
1203 oma_ds_1_2_binder_get_stream_size(pBinder, &stream_size);
1204 FW_LOGV("stream size after Changes Command= %d", stream_size);
1205 put_into_list(commands, commands_last, changeCommand);
1207 if (changedItem->data == NULL) {
1208 itemIter = g_list_next(itemIter);
1209 changedDatastore->sentItem = g_list_append(changedDatastore->sentItem, changedItem);
1210 changedDatastore->changeItem = g_list_remove(changedDatastore->changeItem, changedItem);
1222 /* create sync end Command */
1223 errorType = create_sync_end_command(&syncCommand);
1224 if (errorType != SA_INTERNAL_OK) {
1225 FW_LOGE("failed to create sync end command");
1228 err = oma_ds_1_2_binder_append(pBinder, PE_SYNC_END, syncCommand);
1229 put_into_list(commands, commands_last, syncCommand);
1234 int chageItemCount = g_list_length(changedDatastore->changeItem);
1235 if (chageItemCount == 0)
1236 changedDatastore->needSyncCommand = 0;
1245 *isNeedNextMsg = needNextMsg;
1252 if (sendingData != NULL) {
1262 FW_LOGE("error : %d", errorType);
1266 static SA_ErrorType __exchange_msg(char *accType, int transportType, char *targetUri , unsigned int session_id,
1267 GList *header_info, char *sendMsg, unsigned int sendMsg_length, GList **recv_header, char **recvMsg, unsigned int *recvMsg_length)
1270 NACI_RESULT res = NACI_SUCCESS;
1271 SA_ErrorType errorType = SA_INTERNAL_OK;
1274 if (COMMUNICATION_TYPE == ENCODING_XML)
1279 NA_http_header_binder(accType, targetUri, isXML, &header_info);
1280 res = NACI_Send_Msg(/*acc_info (id, pw, uri, ...),*/
1286 (unsigned char **)recvMsg,
1291 if (res != NACI_SUCCESS) {
1292 FW_LOGE("res = %d", res);
1293 errorType = SA_INTERNAL_CONNECTION_ERROR;
1304 FW_LOGE("error = %d", errorType);
1313 static SA_ErrorType __process_recv_msg(char *recvMsg, unsigned int recvMsg_length, void **syncReturnObj, int *isFinish)
1316 SA_ErrorType errorType = SA_INTERNAL_OK;
1318 reset_cmdID_session(session);
1320 SyncML* pSyncML = (SyncML *)calloc(1, sizeof(SyncML));
1321 if (pSyncML == NULL) {
1322 FW_LOGE("failed to allocate pSyncML");
1323 errorType = SA_INTERNAL_NO_MEMORY;
1327 GList *pReturnStatus = NULL;
1328 GList *pReturnDatastore = NULL;
1330 errorType = __reverse_object_binder(&pSyncML, recvMsg, recvMsg_length);
1331 if (errorType != SA_INTERNAL_OK) {
1332 FW_LOGE("failed in reverseObjectBinder");
1336 /* check receive msg */
1337 errorType = receive_header(session, pSyncML->hdr);
1338 if (errorType != SA_INTERNAL_OK) {
1339 FW_LOGE("failed in receive_Header");
1343 errorType = receive_statuses(session, pSyncML->status, &pReturnStatus);
1344 if (errorType != SA_INTERNAL_OK) {
1345 FW_LOGE("failed in receive_Statuses");
1349 errorType = receive_commands(session, pSyncML->commands, false, &pReturnDatastore);
1350 if (errorType != SA_INTERNAL_OK) {
1351 FW_LOGE("failed in receive_Commands");
1355 if (pSyncML->final) {
1356 session->isSendingfinal = 0;
1357 session->isReceivingFinal = 1;
1359 session->isReceivingFinal = 0;
1361 if (session->pkgStatus == SYNCML_PKG_2) {
1362 PreSyncReturnObj ** pre = (PreSyncReturnObj **)syncReturnObj;
1363 (*pre)->datastoreInfo = pReturnDatastore;
1365 if (session->remoteDevInf) {
1366 if (session->remoteDevInf->devid != NULL)
1367 (*pre)->devID = strdup(session->remoteDevInf->devid);
1371 SyncReturnObj **syncReturn = (SyncReturnObj **)syncReturnObj;
1372 (*syncReturn)->status = pReturnStatus;
1373 (*syncReturn)->changedDatastore = pReturnDatastore;
1376 if (session->pkgStatus == SYNCML_PKG_2 && session->isReceivingFinal)
1377 session->pkgStatus = SYNCML_PKG_3;
1378 else if (session->pkgStatus == SYNCML_PKG_4 && session->isReceivingFinal) {
1379 if (syncType == ALERT_REFRESH_FROM_CLIENT || syncType == ALERT_ONE_WAY_FROM_CLIENT) {
1383 session->pkgStatus = SYNCML_PKG_5;
1384 } else if (session->pkgStatus == SYNCML_PKG_6 && session->isReceivingFinal) {
1392 free_syncml(pSyncML);
1399 FW_LOGE("error : %d", errorType);
1404 free_syncml(pSyncML);
1410 static SA_ErrorType __generate_autoconfigure_msg(char *id, char *pwd, char *targetUrl, char **msg, unsigned int *msg_size, Session *pSession)
1413 FW_LOGV("id = %s, pwd = %s, targetUrl = %s", id, pwd, targetUrl);
1415 SA_ErrorType errorType = SA_INTERNAL_OK;
1418 char *credData = NULL;
1420 SyncHdr *pSyncHdr = NULL;
1421 SyncML *pSyncML = NULL;
1423 GList *commands = NULL;
1424 GList *commands_last = NULL;
1426 errorType = create_cred_string(AUTH_TYPE_BASIC, id, pwd, NULL, 0, &credData);
1427 if (errorType != SA_INTERNAL_OK) {
1428 FW_LOGE("failed to create cred string");
1432 errorType = create_cred(id, pwd, AUTH_TYPE_BASIC, FORMAT_TYPE_BASE64, credData, &pCred);
1433 if (errorType != SA_INTERNAL_OK) {
1434 FW_LOGE("failed to create cred");
1438 if (credData != NULL) {
1443 set_session_cred(pSession, pCred);
1445 errorType = create_synchdr(pSession , &pSyncHdr);
1446 if (errorType != SA_INTERNAL_OK) {
1447 FW_LOGE("failed to create synchdr");
1451 char *sourceDevInf = NULL;
1452 if (pSession->protocolVersion == VERSION_10)
1453 sourceDevInf = ELEMENT_DEVINF_10;
1454 else if (pSession->protocolVersion == VERSION_11)
1455 sourceDevInf = ELEMENT_DEVINF_11;
1456 else if (pSession->protocolVersion == VERSION_12)
1457 sourceDevInf = ELEMENT_DEVINF_12;
1459 Location *pLocation = NULL;
1460 errorType = create_location(sourceDevInf, NULL, &pLocation);
1461 if (errorType != SA_INTERNAL_OK) {
1462 FW_LOGE("failed to create location");
1466 char *contentType = NULL;
1467 if (COMMUNICATION_TYPE == ENCODING_XML)
1468 contentType = ELEMENT_DEVINF_XML;
1470 contentType = ELEMENT_DEVINF_WBXML;
1472 /* create Get command */
1473 Command *pGetCommand = NULL;
1474 errorType = create_get_command(pSession, pLocation, contentType, &pGetCommand);
1475 if (errorType != SA_INTERNAL_OK) {
1476 FW_LOGE("failed to get command");
1479 put_into_list(&commands, &commands_last, pGetCommand);
1482 errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
1483 if (errorType != SA_INTERNAL_OK) {
1484 FW_LOGE("failed to get syncml");
1488 errorType = __object_binder(pSyncML, msg, msg_size);
1489 if (errorType != SA_INTERNAL_OK) {
1490 FW_LOGE("failed in objectBinder");
1494 /*free generate structure*/
1495 free_syncml(pSyncML);
1504 free_synchdr(pSyncHdr);
1507 if (credData != NULL)
1510 free_commands(commands);
1514 FW_LOGE("error : %d", errorType);
1519 static SA_ErrorType __process_autoconfigure_recv_msg(char *recvMsg, unsigned int recvMsg_length, Session *pSession)
1523 SA_ErrorType errorType = SA_INTERNAL_OK;
1525 SyncML* pSyncML = (SyncML *)calloc(1, sizeof(SyncML));
1526 if (pSyncML == NULL) {
1527 FW_LOGE("failed to allocate pSyncML");
1528 errorType = SA_INTERNAL_NO_MEMORY;
1532 errorType = __reverse_object_binder(&pSyncML, recvMsg, recvMsg_length);
1533 if (errorType != SA_INTERNAL_OK) {
1534 FW_LOGE("failed in reverseObjectBinder");
1538 /* check receive msg */
1539 errorType = receive_header(pSession, pSyncML->hdr);
1540 if (errorType != SA_INTERNAL_OK) {
1541 FW_LOGE("failed in receive_Header");
1545 errorType = receive_statuses(pSession, pSyncML->status, NULL);
1546 if (errorType != SA_INTERNAL_OK) {
1547 FW_LOGE("failed in receive_Statuses");
1551 errorType = receive_commands(pSession, pSyncML->commands, true, NULL);
1552 if (errorType != SA_INTERNAL_OK) {
1553 FW_LOGE("failed in receive_Commands");
1559 if (pSyncML != NULL)
1560 free_syncml(pSyncML);
1567 FW_LOGE("error: %d", errorType);
1572 free_syncml(pSyncML);
1576 static SA_ErrorType __object_binder(SyncML *pSyncML, char **msg, unsigned int *msg_size)
1580 SA_ErrorType errorType = SA_INTERNAL_OK;
1582 Binder_function_set *pBinder_function_set = NULL;
1584 PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
1586 protocol_binder *pBinder = NULL;
1588 unsigned int xml_len = 0;
1590 pBinder = oma_ds_1_2_binder_init(pBinder, COMMUNICATION_TYPE, false, false, false, true, pBinder_function_set);
1592 err = oma_ds_1_2_binder_append(pBinder, PE_SYNCML_START, NULL);
1593 err = oma_ds_1_2_binder_append(pBinder, PE_HEADER, pSyncML->hdr);
1594 err = oma_ds_1_2_binder_append(pBinder, PE_BODY_START, NULL);
1596 GList *status_iter = NULL;
1597 Status *pStatus = NULL;
1598 for (status_iter = pSyncML->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
1599 pStatus = status_iter->data;
1600 err = oma_ds_1_2_binder_append(pBinder, PE_STATUS, pStatus);
1604 Command *pCommand = NULL;
1605 OMA_DS_Protocol_Element protocol_element = PE_UNDEF;
1606 for (iter = pSyncML->commands; iter != NULL; iter = g_list_next(iter)) {
1607 pCommand = (Command *)(iter->data);
1609 CommandType type = pCommand->type;
1611 case COMMAND_TYPE_UNKNOWN:
1612 protocol_element = PE_UNDEF;
1614 case COMMAND_TYPE_ALERT:
1615 protocol_element = PE_ALERT;
1617 case COMMAND_TYPE_SYNC_START:
1618 protocol_element = PE_SYNC_START;
1620 case COMMAND_TYPE_SYNC_END:
1621 protocol_element = PE_SYNC_END;
1623 case COMMAND_TYPE_PUT:
1624 protocol_element = PE_PUT_START;
1626 case COMMAND_TYPE_ADD:
1627 protocol_element = PE_ADD;
1629 case COMMAND_TYPE_REPLACE:
1630 protocol_element = PE_REPLACE;
1632 case COMMAND_TYPE_DELETE:
1633 protocol_element = PE_DELETE;
1635 case COMMAND_TYPE_MAP:
1636 protocol_element = PE_MAP;
1638 case COMMAND_TYPE_GET:
1639 protocol_element = PE_GET;
1641 case COMMAND_TYPE_RESULTS:
1642 protocol_element = PE_RESULTS_START;
1644 case COMMAND_TYPE_HEADER:
1645 protocol_element = PE_HEADER;
1648 err = oma_ds_1_2_binder_append(pBinder, protocol_element, pCommand);
1650 if (type == COMMAND_TYPE_RESULTS) {
1651 err = oma_ds_1_2_binder_append(pBinder, PE_RESULTS_END, NULL);
1654 if (type == COMMAND_TYPE_PUT) {
1655 if (pCommand->private.access.item) {
1656 if (pCommand->private.access.item->private.devInf) {
1657 err = oma_ds_1_2_binder_append(pBinder, PE_DEVINF, pCommand->private.access.item);
1660 err = oma_ds_1_2_binder_append(pBinder, PE_PUT_END, NULL);
1664 if (pSyncML->final) {
1665 err = oma_ds_1_2_binder_append(pBinder, PE_FINAL, NULL);
1668 err = oma_ds_1_2_binder_append(pBinder, PE_BODY_END, NULL);
1669 err = oma_ds_1_2_binder_append(pBinder, PE_SYNCML_END, NULL);
1671 oma_ds_1_2_binder_get_stream(pBinder, msg, msg_size);
1673 /*FIXME for debugging*/
1674 if (COMMUNICATION_TYPE == ENCODING_XML)
1675 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
1677 err = protocol_binder_get_xml_for_debugging(pBinder, &xml, &xml_len);
1678 if (err == PROTOCOL_BINDER_OK) {
1679 set_xml_to_file(xml, OMA_DS_MSG_PATH);
1688 oma_ds_1_2_binder_terminate(pBinder);
1690 if (err != PROTOCOL_BINDER_OK)
1691 errorType = SA_INTERNAL_BINDER_ERROR;
1698 static SA_ErrorType __reverse_object_binder(SyncML **pSyncML, char *recv_msg, unsigned int recv_msg_length)
1702 SA_ErrorType errorType = SA_INTERNAL_OK;
1703 Binder_function_set *pBinder_function_set = NULL;
1705 PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
1706 Reverse_protocol_binder *pBinder = NULL;
1708 unsigned int xml_len = 0;
1711 if (COMMUNICATION_TYPE == ENCODING_XML)
1714 dec = DECODING_WBXML;
1716 err = reverse_oma_ds_1_2_binder_init(recv_msg, recv_msg_length,
1717 dec, pBinder_function_set, NULL, &pBinder);
1719 if (err != PROTOCOL_BINDER_OK) {
1720 errorType = SA_INTERNAL_BINDER_ERROR;
1721 FW_LOGE("reverse_oma_ds_1_2_binder_init error =%d", err);
1726 /*FIXME for debugging*/
1727 if (COMMUNICATION_TYPE == ENCODING_XML)
1728 set_xml_to_file(recv_msg, OMA_DS_MSG_PATH);
1730 err = reverse_protocol_binder_get_xml_for_debugging(pBinder, &xml, &xml_len);
1731 if (err == PROTOCOL_BINDER_OK) {
1732 set_xml_to_file(xml, OMA_DS_MSG_PATH);
1742 OMA_DS_Protocol_Element protocol_element = PE_UNDEF;
1743 char *protocol_element_name = NULL;
1744 Content_Ptr pContent = NULL;
1745 Command *tempCommandfordevInf = NULL;
1746 while (err == PROTOCOL_BINDER_OK) {
1747 protocol_element = PE_UNDEF;
1748 err = reverse_oma_ds_1_2_binder_next(pBinder, &protocol_element, &protocol_element_name, &pContent);
1749 switch (protocol_element) {
1751 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1753 case PE_ATOMIC_START:
1757 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1761 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1765 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1766 tempCommandfordevInf = pContent;
1769 case PE_SEQUENCE_START:
1770 case PE_SEQUENCE_END:
1772 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1775 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1776 protocol_element = PE_UNDEF;
1779 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1782 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1784 case PE_RESULTS_START:
1785 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1786 tempCommandfordevInf = pContent;
1789 (*pSyncML)->status = g_list_append((*pSyncML)->status, pContent);
1790 protocol_element = PE_UNDEF;
1793 (*pSyncML)->hdr = pContent;
1794 reverse_protocol_binder_set_user_data(pBinder, (*pSyncML)->hdr);
1800 (*pSyncML)->final = (int)pContent;
1803 if (tempCommandfordevInf) {
1804 tempCommandfordevInf->private.results.item->private.devInf = pContent;
1805 tempCommandfordevInf = NULL;
1812 if (protocol_element_name != NULL) {
1813 free(protocol_element_name);
1814 protocol_element_name = NULL;
1817 if (err == PROTOCOL_BINDER_HAS_NO_MORE_ELEMENT) {
1822 reverse_protocol_binder_terminate(pBinder);
1824 free_oma_ds_1_2_binder_function_set(pBinder_function_set);
1826 if (err != PROTOCOL_BINDER_OK && err != PROTOCOL_BINDER_HAS_NO_MORE_ELEMENT)
1827 errorType = SA_INTERNAL_BINDER_ERROR;
1832 static Common_ErrorType __convert_error_type(SA_ErrorType errorType)
1834 Common_ErrorType sa_errorType = COMMON_OK;
1836 if (errorType == SA_INTERNAL_OK)
1837 sa_errorType = COMMON_OK;
1838 else if (errorType == SA_INTERNAL_MISCONFIGURATION)
1839 sa_errorType = COMMON_MISCONFIGURATION;
1840 else if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR)
1841 sa_errorType = COMMON_AUTHENTICATION_ERROR;
1842 else if (errorType == SA_INTERNAL_NOT_FOUND)
1843 sa_errorType = COMMON_NOT_FOUND;
1844 else if (errorType == SA_INTERNAL_NO_MEMORY)
1845 sa_errorType = COMMON_NO_MEMORY;
1846 else if (errorType == SA_INTERNAL_NOT_DEFINED || errorType == SA_INTERNAL_BINDER_ERROR ||
1847 errorType == SA_INTERNAL_ERROR || errorType == SA_INTERNAL_DA_ERROR)
1848 sa_errorType = COMMON_INTERNAL_ERROR;
1849 else if (errorType == SA_INTERNAL_SERVER_ERROR || errorType == SA_INTERNAL_SERVER_FAILURE)
1850 sa_errorType = COMMON_SERVER_ERROR;
1851 else if (errorType == SA_INTERNAL_CONNECTION_ERROR)
1852 sa_errorType = COMMON_CONNECTION_ERROR;
1853 else if (errorType == SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER)
1854 sa_errorType = COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
1855 else if (errorType == SA_INTERNAL_CANCEL)
1856 sa_errorType = COMMON_CANCEL;
1857 else if (errorType == SA_INTERNAL_BUSY_SIGNALING)
1858 sa_errorType = COMMON_BUSY_SIGNALING;
1861 return sa_errorType;
1863 /*from processRecvMsg
1864 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
1865 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
1866 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
1868 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
1870 1513 -> ERROR_INTERNAL_NOT_DEFINED
1871 1512 -> ERROR_INTERNAL_NO_MEMORY
1872 1503 -> ERROR_INTERNAL_BINDER_ERROR
1874 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
1875 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error)
1877 can be return to engine value
1878 from generatePreSyncMsg
1879 1506 -> ERROR_INTERNAL_MISCONFIGURATION : need configure infomation
1880 1500 -> ERROR_INTERNAL_OK : generatePreSyncMsg has been completed without any error
1881 1512 -> ERROR_INTERNAL_NO_MEMORY : failed to allocate memory
1882 1513 -> ERROR_INTERNAL_NOT_DEFINED : something is not defined that must have
1883 1503 -> ERROR_INTERNAL_BINDER_ERROR : binder error
1889 401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
1890 407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
1891 404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
1893 1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
1895 1513 -> ERROR_INTERNAL_NOT_DEFINED
1896 1512 -> ERROR_INTERNAL_NO_MEMORY
1897 1503 -> ERROR_INTERNAL_BINDER_ERROR
1899 500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
1900 511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error)*/
1904 static void __clean_up_SA()
1908 if (session != NULL) {
1910 if (session->hasOpend) {
1911 NACI_Close_Connection(TRANSPORT_TYPE, session->NACI_sessionID);
1912 session->hasOpend = 0;
1915 free_session(session);
1922 int pre_sync(int transportType, int accountId, char *sessionId, void **preSyncReturnObj)
1926 SA_ErrorType errorType = SA_INTERNAL_OK;
1929 unsigned int msg_size = 0;
1930 GList *recv_header = NULL;
1931 GList *header_info = NULL;
1932 char *recv_msg = NULL;
1933 unsigned int recv_msg_size = 0;
1934 unsigned int session_id;
1936 /* generate preSync Msg */
1937 errorType = __generate_presync_msg(accountId, sessionId, &msg, &msg_size);
1938 if (errorType != SA_INTERNAL_OK)
1940 if (!session->hasOpend) {
1941 NACI_RESULT res = NACI_Open_Connection(transportType, NETWORK_TIMEOUT, &session_id);
1942 if (res != NACI_SUCCESS) {
1943 FW_LOGE("failed in NACI_Open_Connection res = %d", res);
1944 errorType = SA_INTERNAL_CONNECTION_ERROR;
1947 session->NACI_sessionID = session_id;
1948 session->hasOpend = 1;
1949 FW_LOGV("session_id = %d", session_id);
1952 errorType = __exchange_msg(DEFINE_PROTOCOL_TYPE, transportType, session->target->locURI,
1953 session->NACI_sessionID, header_info, msg, msg_size, &recv_header, &recv_msg, &recv_msg_size);
1954 if (errorType != SA_INTERNAL_OK)
1957 errorType = __process_recv_msg(recv_msg, recv_msg_size, preSyncReturnObj, NULL);
1958 if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR) {
1959 /*when errortype is ERROR_AUTH_REQUIRED try request using chal just one more time
1960 it can be only happend when server required MD5 authentication(we send basic authentication every time by default)*/
1961 errorType = __generate_presync_msg(accountId, sessionId, &msg, &msg_size);
1962 if (errorType != SA_INTERNAL_OK)
1965 if (header_info != NULL) {
1967 for (iter = header_info; iter != NULL; iter = g_list_next(iter))
1968 free_header_info(iter->data);
1969 g_list_free(header_info);
1973 if (recv_header != NULL) {
1975 common_header_info *iter_data;
1976 for (iter = recv_header; iter != NULL;) {
1978 iter_data = ((common_header_info *)(iter->data));
1980 iter = g_list_next(iter);
1981 recv_header = g_list_remove(recv_header, iter_data);
1983 if (iter_data->key != NULL)
1984 free(iter_data->key);
1985 if (iter_data->value != NULL)
1986 free(iter_data->value);
1989 g_list_free(recv_header);
1992 errorType = __exchange_msg(DEFINE_PROTOCOL_TYPE, transportType, session->target->locURI,
1993 session->NACI_sessionID, header_info, msg, msg_size, &recv_header, &recv_msg, &recv_msg_size);
1994 if (errorType != SA_INTERNAL_OK)
1997 errorType = __process_recv_msg(recv_msg, recv_msg_size, preSyncReturnObj, NULL);
1998 if (errorType != SA_INTERNAL_OK)
2005 if (header_info != NULL) {
2007 for (iter = header_info; iter != NULL; iter = g_list_next(iter))
2008 free_header_info(iter->data);
2009 g_list_free(header_info);
2013 if (recv_header != NULL) {
2015 common_header_info *iter_data;
2016 for (iter = recv_header; iter != NULL;) {
2018 iter_data = ((common_header_info *)(iter->data));
2020 iter = g_list_next(iter);
2021 recv_header = g_list_remove(recv_header, iter_data);
2023 if (iter_data->key != NULL)
2024 free(iter_data->key);
2025 if (iter_data->value != NULL)
2026 free(iter_data->value);
2029 g_list_free(recv_header);
2032 if (errorType != SA_INTERNAL_OK) {
2033 FW_LOGE("errorType : %d", errorType);
2037 return __convert_error_type(errorType);
2040 int generate_msg(void **syncObj, char **msg, unsigned int *msg_size)
2044 SA_ErrorType errorType = SA_INTERNAL_OK;
2046 errorType = __generate_msg(syncObj, msg, msg_size);
2047 if (errorType != SA_INTERNAL_OK)
2054 if (errorType != SA_INTERNAL_OK) {
2055 FW_LOGE("errorType : %d", errorType);
2059 return __convert_error_type(errorType);
2062 int exchange_msg(int transportType, char *sendMsg, unsigned int sendMsg_length, char **recvMsg, unsigned int *recvMsg_length)
2066 SA_ErrorType errorType = SA_INTERNAL_OK;
2068 GList *header_info = NULL;
2069 GList *recv_header = NULL;
2071 errorType = __exchange_msg(DEFINE_PROTOCOL_TYPE, transportType, session->target->locURI,
2072 session->NACI_sessionID, header_info, sendMsg, sendMsg_length, &recv_header, recvMsg, recvMsg_length);
2073 if (errorType != SA_INTERNAL_OK)
2080 if (header_info != NULL) {
2082 for (iter = header_info; iter != NULL; iter = g_list_next(iter))
2083 free_header_info(iter->data);
2084 g_list_free(header_info);
2088 if (recv_header != NULL) {
2090 common_header_info *iter_data;
2091 for (iter = recv_header; iter != NULL;) {
2093 iter_data = ((common_header_info *)(iter->data));
2095 iter = g_list_next(iter);
2096 recv_header = g_list_remove(recv_header, iter_data);
2098 if (iter_data->key != NULL)
2099 free(iter_data->key);
2100 if (iter_data->value != NULL)
2101 free(iter_data->value);
2104 g_list_free(recv_header);
2107 if (errorType != SA_INTERNAL_OK) {
2108 FW_LOGE("errorType : %d", errorType);
2112 return __convert_error_type(errorType);
2115 int process_recv_msg(char *recvMsg, unsigned int recvMsg_length, void **syncReturnObj, int *isFinish)
2119 SA_ErrorType errorType = SA_INTERNAL_OK;
2121 errorType = __process_recv_msg(recvMsg, recvMsg_length, syncReturnObj, isFinish);
2122 if (errorType != SA_INTERNAL_OK)
2129 if (errorType != SA_INTERNAL_OK) {
2130 FW_LOGE("errorType : %d", errorType);
2134 return __convert_error_type(errorType);
2146 int auto_configure_from_vconf(char *id, char *pwd, char *targetUrl, char *profilePath, GList **configure)
2150 SA_ErrorType errorType = SA_INTERNAL_OK;
2151 Session *pSession = NULL;
2153 char *sourceUrl = NULL;
2154 char *sessionId = NULL;
2157 unsigned int msg_size = 0;
2159 GList *header_info = NULL;
2160 GList *recv_header = NULL;
2161 char *recv_msg = NULL;
2162 unsigned int recv_msg_size = 0;
2164 bool cancel_flag = false;
2165 unsigned int session_id;
2167 sessionId = g_strdup_printf("%ld", time(NULL));
2168 if (sessionId == NULL) {
2169 errorType = SA_INTERNAL_NO_MEMORY;
2173 DCI_RETURN err = DCI_Get_DevInfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
2174 if (err != DCI_SUCCESS) {
2175 FW_LOGV("failed in DCI_Get_DevInfo");
2176 errorType = SA_INTERNAL_ERROR;
2180 errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, sourceUrl, targetUrl, &pSession);
2181 if (errorType != SA_INTERNAL_OK)
2184 if (pSession == NULL) {
2185 FW_LOGV("failed to create session");
2186 errorType = SA_INTERNAL_NO_MEMORY;
2190 errorType = __generate_autoconfigure_msg(id, pwd, targetUrl, &msg, &msg_size , pSession);
2191 if (errorType != SA_INTERNAL_OK){
2192 FW_LOGV("failed in __generate_autoconfigure_msg");
2196 if (!pSession->hasOpend) {
2197 NACI_RESULT res = NACI_Open_Connection(TRANSPORT_TYPE, NETWORK_TIMEOUT, &session_id);
2198 if (res != NACI_SUCCESS) {
2199 FW_LOGE("failed in NACI_Open_Connection res = %d", res);
2200 errorType = SA_INTERNAL_CONNECTION_ERROR;
2203 pSession->NACI_sessionID = session_id;
2204 pSession->hasOpend = 1;
2205 FW_LOGV("session_id = %d", session_id);
2208 cancel_flag = task_process_check_cancel_flag();
2210 errorType = SA_INTERNAL_CANCEL;
2215 errorType = __exchange_msg(DEFINE_PROTOCOL_TYPE, TRANSPORT_TYPE, pSession->target->locURI,
2216 pSession->NACI_sessionID, header_info, msg, msg_size, &recv_header, &recv_msg, &recv_msg_size);
2217 if (errorType != SA_INTERNAL_OK){
2218 FW_LOGV("failed in __exchange_msg");
2222 cancel_flag = task_process_check_cancel_flag();
2224 errorType = SA_INTERNAL_CANCEL;
2228 errorType = __process_autoconfigure_recv_msg(recv_msg, recv_msg_size, pSession);
2229 if (errorType != SA_INTERNAL_OK){
2230 FW_LOGV("failed in __process_autoconfigure_recv_msg");
2234 cancel_flag = task_process_check_cancel_flag();
2236 errorType = SA_INTERNAL_CANCEL;
2240 DevInf *devInf = pSession->remoteDevInf;
2243 GList *tempConfigure = NULL;
2245 GList *contactCandidate = NULL;
2246 GList *calendarCandidate = NULL;
2247 GList *memoCandidate = NULL;
2249 /*set inputted name by user*/
2250 contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI));
2251 char *target = NULL;
2252 target = get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI);
2254 FW_LOGV("Contacts target =%s", target);
2255 if (strcmp(target, "") != 0)
2256 contactCandidate = g_list_append(contactCandidate, strdup(target));
2262 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2263 target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2265 FW_LOGV("Organizer target =%s", target);
2266 if (strcmp(target, "") != 0)
2267 calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2273 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2274 target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2276 FW_LOGV("Memo target =%s", target);
2277 if (strcmp(target, "") != 0)
2278 memoCandidate = g_list_append(memoCandidate, strdup(target));
2285 GList *innerIter = NULL;
2286 DevInfDataStore *devInfDataStore = NULL;
2289 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2290 devInfDataStore = iter->data;
2294 FW_LOGV("devInfDataStore->sourceref = %s", devInfDataStore->sourceref);
2296 if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD) == 0 ||
2297 strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD_30) == 0) {
2299 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2300 candidate = innerIter->data;
2302 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2306 contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->sourceref));
2308 } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCAL) == 0 ||
2309 strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_ICAL) == 0) {
2311 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2312 candidate = innerIter->data;
2314 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2318 calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->sourceref));
2320 } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_PLAIN) == 0) {
2322 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2323 candidate = innerIter->data;
2325 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2329 memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->sourceref));
2333 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2334 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2335 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2337 *configure = tempConfigure;
2339 // *congifure = g_list_append(*congifure, contactCandidate);
2340 // *congifure = g_list_append(*congifure, calendarCandidate);
2341 // *congifure = g_list_append(*congifure, memoCandidate);
2344 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2353 if (pSession != NULL) {
2354 if (pSession->hasOpend)
2355 NACI_Close_Connection(TRANSPORT_TYPE, pSession->NACI_sessionID);
2358 if (header_info != NULL) {
2360 for (iter = header_info; iter != NULL; iter = g_list_next(iter))
2361 free_header_info(iter->data);
2362 g_list_free(header_info);
2366 if (recv_header != NULL) {
2368 common_header_info *iter_data;
2369 for (iter = recv_header; iter != NULL;) {
2371 iter_data = ((common_header_info *)(iter->data));
2373 iter = g_list_next(iter);
2374 recv_header = g_list_remove(recv_header, iter_data);
2376 if (iter_data->key != NULL)
2377 free(iter_data->key);
2378 if (iter_data->value != NULL)
2379 free(iter_data->value);
2382 g_list_free(recv_header);
2392 free_session(pSession);
2396 if (errorType != SA_INTERNAL_OK)
2397 FW_LOGE("errorType =%d", errorType);
2399 return __convert_error_type(errorType);
2403 int auto_configure(char *addr, char *id, char *password, GList **configure)
2407 SA_ErrorType errorType = SA_INTERNAL_OK;
2408 Session *pSession = NULL;
2410 char *sourceUrl = NULL;
2411 char *sessionId = NULL;
2414 unsigned int msg_size = 0;
2416 GList *header_info = NULL;
2417 GList *recv_header = NULL;
2418 char *recv_msg = NULL;
2419 unsigned int recv_msg_size = 0;
2421 bool cancel_flag = false;
2422 unsigned int session_id;
2424 sessionId = g_strdup_printf("%ld", time(NULL));
2425 if (sessionId == NULL) {
2426 errorType = SA_INTERNAL_NO_MEMORY;
2430 DCI_RETURN err = DCI_Get_DevInfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
2431 if (err != DCI_SUCCESS) {
2432 errorType = SA_INTERNAL_ERROR;
2436 errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, sourceUrl, addr, &pSession);
2437 if (errorType != SA_INTERNAL_OK)
2440 if (pSession == NULL) {
2441 errorType = SA_INTERNAL_NO_MEMORY;
2445 errorType = __generate_autoconfigure_msg(id, password, addr, &msg, &msg_size , pSession);
2446 if (errorType != SA_INTERNAL_OK)
2449 if (!pSession->hasOpend) {
2450 NACI_RESULT res = NACI_Open_Connection(TRANSPORT_TYPE, NETWORK_TIMEOUT, &session_id);
2451 if (res != NACI_SUCCESS) {
2452 FW_LOGE("failed in NACI_Open_Connection res = %d", res);
2453 errorType = SA_INTERNAL_CONNECTION_ERROR;
2456 pSession->NACI_sessionID = session_id;
2457 pSession->hasOpend = 1;
2458 FW_LOGV("session_id = %d", session_id);
2461 cancel_flag = task_process_check_cancel_flag();
2463 errorType = SA_INTERNAL_CANCEL;
2468 errorType = __exchange_msg(DEFINE_PROTOCOL_TYPE, TRANSPORT_TYPE, pSession->target->locURI,
2469 pSession->NACI_sessionID, header_info, msg, msg_size, &recv_header, &recv_msg, &recv_msg_size);
2470 if (errorType != SA_INTERNAL_OK)
2473 cancel_flag = task_process_check_cancel_flag();
2475 errorType = SA_INTERNAL_CANCEL;
2479 errorType = __process_autoconfigure_recv_msg(recv_msg, recv_msg_size, pSession);
2480 if (errorType != SA_INTERNAL_OK)
2483 cancel_flag = task_process_check_cancel_flag();
2485 errorType = SA_INTERNAL_CANCEL;
2489 DevInf *devInf = pSession->remoteDevInf;
2492 GList *tempConfigure = NULL;
2494 GList *contactCandidate = NULL;
2495 GList *calendarCandidate = NULL;
2496 GList *memoCandidate = NULL;
2498 /*set inputted name by user*/
2499 contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI));
2501 char *target = NULL;
2502 target= get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI);
2504 FW_LOGV("Contacts target =%s", target);
2505 if (strcmp(target, "") !=0)
2506 contactCandidate = g_list_append(contactCandidate, strdup(target));
2513 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2515 target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2517 FW_LOGV("Organizer target =%s", target);
2518 if (strcmp(target, "") !=0)
2519 calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2526 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2528 target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2530 FW_LOGV("Memo target =%s", target);
2531 if (strcmp(target, "") !=0)
2532 memoCandidate = g_list_append(memoCandidate, strdup(target));
2540 GList *innerIter = NULL;
2541 DevInfDataStore *devInfDataStore = NULL;
2544 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2545 devInfDataStore = iter->data;
2549 FW_LOGV("devInfDataStore->sourceref = %s", devInfDataStore->sourceref);
2551 if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD) == 0 ||
2552 strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD_30) == 0) {
2554 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2555 candidate = innerIter->data;
2557 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2561 contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->sourceref));
2563 } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCAL) == 0 ||
2564 strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_ICAL) == 0) {
2566 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2567 candidate = innerIter->data;
2569 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2573 calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->sourceref));
2575 } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_PLAIN) == 0) {
2577 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2578 candidate = innerIter->data;
2580 if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2584 memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->sourceref));
2588 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2589 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2590 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2592 *configure = tempConfigure;
2595 // *congifure = g_list_append(*congifure, contactCandidate);
2596 // *congifure = g_list_append(*congifure, calendarCandidate);
2597 // *congifure = g_list_append(*congifure, memoCandidate);
2600 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2609 if (pSession != NULL) {
2610 if (pSession->hasOpend)
2611 NACI_Close_Connection(TRANSPORT_TYPE, pSession->NACI_sessionID);
2614 if (header_info != NULL) {
2616 for (iter = header_info; iter != NULL; iter = g_list_next(iter))
2617 free_header_info(iter->data);
2618 g_list_free(header_info);
2622 if (recv_header != NULL) {
2624 common_header_info *iter_data;
2625 for (iter = recv_header; iter != NULL;) {
2627 iter_data = ((common_header_info *)(iter->data));
2629 iter = g_list_next(iter);
2630 recv_header = g_list_remove(recv_header, iter_data);
2632 if (iter_data->key != NULL)
2633 free(iter_data->key);
2634 if (iter_data->value != NULL)
2635 free(iter_data->value);
2638 g_list_free(recv_header);
2648 free_session(pSession);
2652 if (errorType != SA_INTERNAL_OK)
2653 FW_LOGE("errorType =%d", errorType);
2655 return __convert_error_type(errorType);
2658 /*FIXME cancel request to NA */
2659 /*int cancel_connection_sync_request(char *transportType)
2663 SA_ErrorType err = SA_INTERNAL_OK;
2664 NACI_RESULT res = NACI_SUCCESS;
2666 FW_LOGV("sessionId = %d", session->NACI_sessionID);
2668 res = NACI_Cancel_Msg(transportType, session->NACI_sessionID);
2669 if (res != NACI_SUCCESS) {
2670 FW_LOGE("res = %d", res);
2671 err = SA_INTERNAL_CONNECTION_ERROR;
2679 return __convert_error_type(err);