Add default Smack manifest for oma-ds-service.spec
[pkgs/o/oma-ds-service.git] / ServiceAdapter / SA_Common_Interface.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23
24
25
26
27 /*
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.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @SA_Common_Interface.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of interface of Service Adapter
40  */
41
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"
64
65 #define LOG_TAG "OMA_DS_SA"
66
67 Session *session = NULL;
68
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();
89
90 static SA_ErrorType __get_accountinfo(int accountId, char **id, char **pwd, char **serverUrl, char **nextNonce)
91 {
92         FW_LOGV("start");
93
94         SA_ErrorType errorType = SA_INTERNAL_OK;
95         char *value = NULL;
96         bool result;
97
98         if (!accountId) {
99                 errorType = SA_INTERNAL_MISCONFIGURATION;
100                 goto error;
101         }
102
103         *id = DACI_Get_Account_Email_Address(accountId);
104         if (*id == NULL) {
105                 errorType = SA_INTERNAL_MISCONFIGURATION;
106                 goto error;
107         }
108
109         *pwd = DACI_Get_Account_Password(accountId);
110         if (*pwd == NULL) {
111                 errorType = SA_INTERNAL_MISCONFIGURATION;
112                 goto error;
113         }
114
115         result = get_config(accountId,  DEFINE_CONFIG_KEY_PROFILE_SERVER_IP, &value);
116         if (result == true)
117                 *serverUrl = value;
118         else{
119                 FW_LOGE("failed in get_Config");
120                 errorType = SA_INTERNAL_DA_ERROR;
121                 goto error;
122         }
123
124         if (*serverUrl == NULL) {
125                 errorType = SA_INTERNAL_MISCONFIGURATION;
126                 goto error;
127         }
128
129         result = get_config(accountId,  DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE, &value);
130         if (result == true)
131                 *nextNonce = value;
132         else{
133                 FW_LOGE("failed in get_Config");
134                 errorType = SA_INTERNAL_DA_ERROR;
135                 goto error;
136         }
137
138         FW_LOGV("end");
139         return errorType;
140
141 error:
142         FW_LOGE("error :%d", errorType);
143         return errorType;
144 }
145
146 static SA_ErrorType __generate_presync_msg(int accountId, char *sessionId,  char **msg, unsigned int *msg_size)
147 {
148         FW_LOGV("start");
149
150         SA_ErrorType errorType = SA_INTERNAL_OK;
151         char *id = NULL;
152         char *pwd = NULL;
153         char *sourceUrl = NULL;
154         char *targetUrl = NULL;
155         char *nextNonce = NULL;
156         char *credData = NULL;
157         char *decoded_nonce = NULL;
158
159         /*pkg1  always has final tag*/
160         int isFinal = 1;
161
162         SyncHdr *pSyncHdr = NULL;
163         Cred *pCred = NULL;
164         DevInf *devInfObj = NULL;
165         SyncML *pSyncML = NULL;
166
167         /*case SAN, MUST use sessionid from SAN message..*/
168         if (!sessionId) {
169                 sessionId = g_strdup_printf("%ld", time(NULL));
170                 if (sessionId == NULL) {
171                         errorType = SA_INTERNAL_NO_MEMORY;
172                         goto error;
173                 }
174         }
175
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;
180                 goto error;
181         }
182
183         errorType = __get_accountinfo(accountId, &id, &pwd, &targetUrl, &nextNonce);
184         if (errorType != SA_INTERNAL_OK)
185                 goto error;
186
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);
192
193         Chal *pTempChal = NULL;
194         if (session != NULL) {
195                 if (session->chal != NULL)
196                         pTempChal = dup_chal(session->chal);
197
198                 free_session(session);
199                 session = NULL;
200         }
201
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");
206                 goto error;
207         }
208
209         if (pTempChal != NULL) {
210                 session->chal = pTempChal;
211                 pTempChal = NULL;
212         }
213
214         /* set pkg status */
215         session->pkgStatus = SYNCML_PKG_1;
216
217         Chal *chal = session->chal;
218         AuthType authType;
219         FormatType formatType = FORMAT_TYPE_UNKNOWN;
220         unsigned int nonce_size = 0;
221         if (!chal) {
222                 FW_LOGV("CHAL DOES NOT EXIST");
223                 if (nextNonce) {
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;
228                 } else{
229                         authType = AUTH_TYPE_BASIC;
230                         formatType = FORMAT_TYPE_BASE64;
231                 }
232
233                 errorType = create_cred_string(authType,  id, pwd, decoded_nonce, nonce_size, &credData);
234
235                 if (errorType != SA_INTERNAL_OK) {
236                         FW_LOGE("failed to create cred string");
237                         goto error;
238                 }
239
240                 errorType = create_cred(id, pwd, authType, formatType, credData,  &pCred);
241         } else{
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);
246                         else{
247                                 if (chal->nonce_plain != NULL) {
248                                         decoded_nonce = strdup(chal->nonce_plain);
249                                         nonce_size = chal->nonce_length;
250                                 }
251                         }
252                 }
253
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");
257                         goto error;
258                 }
259
260                 errorType = create_cred(id, pwd, chal->type, FORMAT_TYPE_BASE64, credData , &pCred);
261         }
262
263         if (credData != NULL) {
264                 free(credData);
265                 credData = NULL;
266         }
267
268         if (decoded_nonce != NULL) {
269                 free(decoded_nonce);
270                 decoded_nonce = NULL;
271         }
272
273         if (nextNonce != NULL) {
274                 free(nextNonce);
275                 nextNonce = NULL;
276         }
277         nonce_size = 0;
278
279         if (errorType != SA_INTERNAL_OK) {
280                 FW_LOGE("failed to create cred");
281                 goto error;
282         }
283
284         set_session_cred(session, pCred);
285         pCred = NULL;
286
287         /* create SyncHdr */
288         errorType = create_synchdr(session , &pSyncHdr);
289         if (errorType != SA_INTERNAL_OK) {
290                 FW_LOGE("failed to create synchdr");
291                 goto error;
292         }
293
294         /* create devInf */
295         errorType = create_devinf(session, &devInfObj);
296         if (errorType != SA_INTERNAL_OK) {
297                 FW_LOGE("failed to create devInf");
298                 goto error;
299         }
300
301         set_session_devinf(session, devInfObj);
302         devInfObj = NULL;
303
304         GList *commands = NULL;
305         GList *commands_last = NULL;
306
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;
314
315
316         /* create Put command */
317         /*
318         Command *pPutCommand = NULL;
319         Location *pLocation = NULL;
320
321         errorType = create_Location(sourceDevInf, NULL, &pLocation);
322         if (errorType != ERROR_INTERNAL_OK)
323                 goto error;
324
325         errorType = create_Put_Command(session, pLocation, ELEMENT_DEVINF_XML, devInfObj , &pPutCommand);
326         if (errorType !=ERROR_INTERNAL_OK)
327                 goto error;
328         putIntoList(&commands, &commands_last, pPutCommand);
329         */
330
331
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");
338                 goto error;
339         }
340
341         char *contentType = NULL;
342         if (COMMUNICATION_TYPE == ENCODING_XML)
343                 contentType = ELEMENT_DEVINF_XML;
344         else
345                 contentType = ELEMENT_DEVINF_WBXML;
346
347         errorType = create_get_command(session, pLocation, contentType, &pGetCommand);
348         if (errorType != SA_INTERNAL_OK) {
349                 FW_LOGE("failed to create get command");
350                 goto error;
351         }
352         put_into_list(&commands, &commands_last, pGetCommand);
353
354         Command *pAlertCommand = NULL;
355         Location *pSourceLocation = NULL;
356         Location *pTargetLocation = NULL;
357         int content_type;
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");
363
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;
368
369                                 goto error;
370                         }
371
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");
375
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;
380                                 goto error;
381                         }
382
383                         if (datastoreinfo_per_content_type[content_type]->id && datastoreinfo_per_content_type[content_type]->pw) {
384
385                                 /*FIXME is there a nextNonce for datastore ??*/
386                                 if (nextNonce) {
387                                         /*server sent nonce info in previous sync session*/
388                                         authType = AUTH_TYPE_MD5;
389                                         decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
390                                 } else{
391                                         authType = AUTH_TYPE_BASIC;
392                                         formatType = FORMAT_TYPE_BASE64;
393                                 }
394
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");
399                                         goto error;
400                                 }
401
402                                 errorType = create_cred(id, pwd, authType, FORMAT_TYPE_BASE64, credData,  &pCred);
403
404                                 if (credData != NULL) {
405                                         free(credData);
406                                         credData = NULL;
407                                 }
408                         }
409
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");
415                                 goto error;
416                         }
417                 } else
418                         continue;
419                 put_into_list(&commands, &commands_last, pAlertCommand);
420         }
421
422
423         errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
424         if (errorType != SA_INTERNAL_OK) {
425                 FW_LOGE("failed to create syncml");
426                 goto error;
427         }
428
429         session->isSendingfinal = isFinal;
430         if (isFinal)
431                 session->pkgStatus = SYNCML_PKG_2;
432
433         /* convert Msg */
434         errorType = __object_binder(pSyncML, msg, msg_size);
435         if (errorType != SA_INTERNAL_OK) {
436                 FW_LOGE("failed in objectBinder");
437                 goto error;
438         }
439
440         /*free generate structure*/
441         free_syncml(pSyncML);
442
443         if (id)
444                 free(id);
445         if (pwd)
446                 free(pwd);
447         if (sessionId)
448                 free(sessionId);
449         if (sourceUrl)
450                 free(sourceUrl);
451         if (targetUrl)
452                 free(targetUrl);
453         if (nextNonce)
454                 free(nextNonce);
455         if (pCred != NULL)
456                 free_cred(pCred);
457
458         FW_LOGV("end");
459
460         return errorType;
461
462 error:
463
464         if (credData != NULL)
465                 free(credData);
466
467         if (id)
468                 free(id);
469         if (pwd)
470                 free(pwd);
471         if (sessionId)
472                 free(sessionId);
473         if (sourceUrl)
474                 free(sourceUrl);
475         if (targetUrl)
476                 free(targetUrl);
477         if (nextNonce)
478                 free(nextNonce);
479         if (pSyncML)
480                 free_syncml(pSyncML);
481         if (pSyncHdr)
482                 free_synchdr(pSyncHdr);
483         if (pCred)
484                 free_cred(pCred);
485
486
487         FW_LOGE("error : %d", errorType);
488         return errorType;
489 }
490
491 static SA_ErrorType __generate_msg(void **syncObj, char **msg , unsigned int *msg_size)
492 {
493         FW_LOGV("start");
494
495         SA_ErrorType errorType = SA_INTERNAL_OK;
496         bool isNeedNextMsg = false;
497
498         Binder_function_set *pBinder_function_set = NULL;
499
500         PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
501
502         protocol_binder *pBinder = NULL;
503         pBinder = oma_ds_1_2_binder_init(pBinder, COMMUNICATION_TYPE, false, false, false, true, pBinder_function_set);
504
505         /* create SyncHdr */
506         SyncHdr *pSyncHdr = NULL;
507         errorType = create_synchdr(session , &pSyncHdr);
508         if (errorType != SA_INTERNAL_OK) {
509                 FW_LOGE("failed to create synchdr");
510                 goto error;
511         }
512
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);
516
517         SyncObj **sync = (SyncObj **)syncObj;
518         GList *commands = NULL;
519         GList *commands_last = NULL;
520         int isFinal = 0;
521
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");
525                 goto error;
526         }
527
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);
534         }
535
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);
543
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);
548                                 }
549                         }
550
551                         err = oma_ds_1_2_binder_append(pBinder, PE_RESULTS_END, NULL);
552                 }
553         }
554
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);
561         }
562
563         /* free sendgStatus structure from SE*/
564         free_sendingstatuses((*sync)->sendingStatus);
565         (*sync)->sendingStatus = NULL;
566
567         if (session->pkgStatus == SYNCML_PKG_3) {
568
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");
572                         goto error;
573                 }
574
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");
578                         goto error;
579                 }
580
581                 if (!isNeedNextMsg)
582                         isFinal = 1;
583         }
584
585         if (session->pkgStatus == SYNCML_PKG_5 && session->isReceivingFinal)
586                 isFinal = 1;
587
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);
595         }
596
597         if (isFinal)
598                 err = oma_ds_1_2_binder_append(pBinder, PE_FINAL, NULL);
599
600         session->isSendingfinal = isFinal;
601
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);
604
605         oma_ds_1_2_binder_get_stream(pBinder, msg, msg_size);
606         FW_LOGV("final msg size = %d",   *msg_size);
607
608         /*FIXME for debugging*/
609         char *xml = NULL;
610         unsigned int xml_len = 0;
611         if (COMMUNICATION_TYPE == ENCODING_XML)
612                 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
613         else{
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);
617
618                         if (xml != NULL) {
619                                 free(xml);
620                                 xml = NULL;
621                         }
622                 }
623         }
624
625         oma_ds_1_2_binder_terminate(pBinder);
626
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;
631
632         free_synchdr(pSyncHdr);
633         if (session->status != NULL) {
634                 free_statuses(session->status);
635                 session->status = NULL;
636         }
637         free_commands(commands);
638
639         session->status = NULL;
640         session->alertCommand = NULL;
641         session->resultsCommand = NULL;
642
643         FW_LOGV("end");
644         return errorType;
645
646 error:
647         FW_LOGE("error : %d", errorType);
648
649         free_synchdr(pSyncHdr);
650         if (session->status != NULL) {
651                 free_statuses(session->status);
652                 session->status = NULL;
653         }
654         free_commands(commands);
655
656         return errorType;
657 }
658
659 static SA_ErrorType __generate_msg_status_map_command(Session **pSession, SyncObj **sync, GList **commands, GList **commands_last)
660 {
661         FW_LOGV("start");
662         SA_ErrorType errorType = SA_INTERNAL_OK;
663         DACI_RETURN da_err  = DACI_SUCCESS;
664
665         GList *iter = NULL;
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);
670
671                 int needMap = 0;
672                 GList *sendingIter = NULL;
673                 AppliedStatus *appliedStatus = NULL;
674                 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
675                         appliedStatus = (sendingIter->data);
676
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;
682
683                                 if (status->type == COMMAND_TYPE_ADD ||
684                                                 status->type == COMMAND_TYPE_REPLACE ||
685                                                 status->type == COMMAND_TYPE_DELETE) {
686
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)
691                                                 needMap = 1;
692
693                                         if (status->sourceRef) {
694                                                 /*add command*/
695                                                 char *guid = NULL;
696                                                 guid = DACI_Get_Mapping_Guid((*pSession)->accountId,  appliedStatus->luid);
697                                                 if (guid == NULL) {
698                                                         errorType = SA_INTERNAL_DA_ERROR;
699                                                         FW_LOGE("failed to get guid");
700                                                         goto error;
701                                                 }
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);
707
708                                                         if (guid) {
709                                                                 free(guid);
710                                                                 guid = NULL;
711                                                         }
712
713                                                         break;
714                                                 }
715
716                                                 if (guid) {
717                                                         free(guid);
718                                                         guid = NULL;
719                                                 }
720                                         }
721
722                                         if (status->targetRef) {
723                                                 /*replace, delete*/
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);
729                                                         break;
730                                                 }
731                                         }
732                                 }
733                         }
734                 }
735
736                 if (needMap) {
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");
744                                 goto error;
745                         }
746
747                         errorType = create_location(sendingStatus->target, NULL, &pTargetLocation);
748                         if (errorType != SA_INTERNAL_OK) {
749                                 FW_LOGE("failed to create location");
750                                 goto error;
751                         }
752
753                         errorType = create_map_command(*pSession, pSourceLocation, pTargetLocation, &mapCommand);
754                         if (errorType != SA_INTERNAL_OK) {
755                                 FW_LOGE("failed to create map command");
756                                 goto error;
757                         }
758
759                         GList *sendingIter = NULL;
760                         AppliedStatus *appliedStatus = NULL;
761
762                         for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
763                                 appliedStatus = (sendingIter->data);
764
765                                 if (appliedStatus->changeType == CHANGE_ADD) {
766
767                                         if (appliedStatus->status == ITEM_ADDED) {
768                                                 Item *temp = create_item();
769                                                 if (!temp) {
770                                                         FW_LOGE("failed to create item");
771                                                         errorType = SA_INTERNAL_NO_MEMORY;
772                                                         goto error;
773                                                 }
774
775                                                 errorType = create_location(appliedStatus->luid, NULL, &pSourceLocation);
776                                                 if (errorType != SA_INTERNAL_OK) {
777                                                         FW_LOGE("failed to create location");
778                                                         goto error;
779                                                 }
780
781                                                 char *guid = NULL;
782                                                 guid = DACI_Get_Mapping_Guid((*pSession)->accountId,  appliedStatus->luid);
783                                                 if (guid == NULL) {
784                                                         errorType = SA_INTERNAL_DA_ERROR;
785                                                         FW_LOGE("failed to get guid");
786                                                         goto error;
787                                                 }
788                                                 errorType = create_location(guid, NULL, &pTargetLocation);
789                                                 if (errorType != SA_INTERNAL_OK) {
790                                                         FW_LOGE("failed to create location");
791                                                         goto error;
792                                                 }
793
794                                                 if (guid) {
795                                                         free(guid);
796                                                         guid = NULL;
797                                                 }
798
799                                                 set_item_source(temp, pSourceLocation);
800                                                 set_item_target(temp, pTargetLocation);
801                                                 set_map_command_item(mapCommand, temp);
802                                         } else{
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");
807                                                         goto error;
808                                                 }
809                                         }
810                                 }
811                         }
812                         put_into_list(commands, commands_last, mapCommand);
813                         (*pSession)->mapCommand = g_list_append((*pSession)->mapCommand, mapCommand);
814                         increase_command_refcount(mapCommand);
815                 }
816         }
817
818
819         FW_LOGV("end");
820         return errorType;
821
822 error:
823
824         FW_LOGE("error : %d", errorType);
825         return errorType;
826 }
827
828 static SA_ErrorType __generate_msg_exist_sending_map_command(protocol_binder *pBinder, Session **pSession,
829                                                                                                                                                                                                                 GList **commands, GList **commands_last)
830 {
831         FW_LOGV("start");
832         SA_ErrorType errorType = SA_INTERNAL_OK;
833         PROTOCOL_BINDER_ERROR err = PROTOCOL_BINDER_OK;
834
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);
837
838         if (existSendingMap) {
839                 int count = 0;
840                 daci_mapping *pDaci_mapping = DACI_Get_Mapping_By_Account_Id((*pSession)->accountId, &count);
841                 FW_LOGV("count = %d", count);
842
843                 Location *pSourceLocation = NULL;
844                 Location *pTargetLocation = NULL;
845
846                 bool exist = false;
847                 int content_type;
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);
850
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");
856                                                 goto error;
857                                         }
858
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");
862                                                 goto error;
863                                         }
864
865                                         exist = true;
866                                         break;
867                                 }
868                         }
869                 }
870
871                 if (exist == false) {
872                         FW_LOGV("Mapping data is not equal with any synchronizing datastore id");
873                         goto error;
874                 }
875
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");
881                         goto error;
882                 }
883
884                 int lastItemTypeId = pDaci_mapping[0].data_store_id;
885                 int i;
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);
892
893                                 bool exist = false;
894                                 int content_type;
895                                 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
896
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");
902                                                                 goto error;
903                                                         }
904
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");
908                                                                 goto error;
909                                                         }
910
911                                                         exist = true;
912                                                         break;
913                                                 }
914                                         }
915                                 }
916                                 if (exist == false) {
917                                         FW_LOGE("Mapping data is not equal with any synchronizing datastore id");
918                                         goto error;
919                                 }
920                         }
921                         lastItemTypeId = pDaci_mapping[i].data_store_id;
922                         Item *temp = create_item();
923                         if (!temp) {
924                                 FW_LOGE("failed to create item");
925                                 errorType = SA_INTERNAL_NO_MEMORY;
926                                 goto error;
927                         }
928                         errorType = create_location(pDaci_mapping[i].luid, NULL, &pSourceLocation);
929                         if (errorType != SA_INTERNAL_OK) {
930                                 FW_LOGE("failed to create location");
931                                 goto error;
932                         }
933
934                         errorType = create_location(pDaci_mapping[i].guid, NULL, &pTargetLocation);
935                         if (errorType != SA_INTERNAL_OK) {
936                                 FW_LOGE("failed to create location");
937                                 goto error;
938                         }
939
940                         set_item_source(temp, pSourceLocation);
941                         set_item_target(temp, pTargetLocation);
942                         set_map_command_item(mapCommand, temp);
943                 }
944
945                 DACI_Free_Memory_Mapping(pDaci_mapping, count);
946
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);
951         }
952
953
954         FW_LOGV("end");
955         return errorType;
956
957 error:
958
959         FW_LOGE("error : %d", errorType);
960         return errorType;
961
962 }
963
964 static SA_ErrorType __generate_msg_changes_command(protocol_binder *pBinder, Session **pSession, SyncObj **sync,
965                                                                                                                                                                         GList **commands, GList **commands_last, bool *isNeedNextMsg)
966 {
967         FW_LOGV("start");
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;
972
973         char *data = NULL;      /*have to be freed*/
974         char *sendingData = NULL;
975
976         /* generate sync , add, replace, delete command when there is a item in ChangedDatastore */
977         GList *iter = NULL;
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);
983
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");
991                                 goto error;
992                         }
993
994                         errorType = create_location(changedDatastore->target, NULL, &pTargetLocation);
995                         if (errorType != SA_INTERNAL_OK) {
996                                 FW_LOGE("failed to create location");
997                                 goto error;
998                         }
999
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");
1003                                 goto error;
1004                         }
1005
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;
1011                         }
1012
1013                         err = oma_ds_1_2_binder_append(pBinder, PE_SYNC_START, syncCommand);
1014                         put_into_list(commands, commands_last, syncCommand);
1015
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);
1022
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);
1026
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");
1034                                                         goto error;
1035                                                 }
1036                                         } else{
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];
1046                                                                 else{
1047                                                                         FW_LOGE("failed to get folderId");
1048                                                                         errorType = SA_INTERNAL_DA_ERROR;
1049                                                                         goto error;
1050                                                                 }
1051                                                         }else{
1052                                                                 FW_LOGE("failed in DACI_Get_Folder_Folder_Id_By_Folder_Type_Id");
1053                                                                 errorType = SA_INTERNAL_DA_ERROR;
1054                                                                 goto error;
1055                                                         }
1056
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);
1062
1063                                                         da_err = DACI_Service_Get_Item(datastoreinfo_per_content_type[changedItem->indexOfDatastore]->datastore_id,
1064                                                                                                                 (*pSession)->accountId, folderId, changedItem->luid, (void **)&data);
1065
1066                                                         FW_LOGV("data = %s", data);
1067                                                         DACI_Free_Memory_Id_List(folder_id_list);
1068                                                         if (data == NULL) {
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);
1075                                                                         goto error;
1076                                                                 }
1077
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);
1082                                                                         goto error;
1083                                                                 }
1084                                                                 itemIter  = g_list_next(itemIter);
1085                                                                 changedDatastore->changeItem = g_list_remove(changedDatastore->changeItem, changedItem);
1086                                                                 free_changeditem(changedItem);
1087                                                                 continue;
1088                                                         }
1089
1090                                                         if (da_err != DACI_SUCCESS) {
1091                                                                 FW_LOGE("failed in DACI_Service_Get_Item =%d", da_err);
1092                                                                 errorType = SA_INTERNAL_DA_ERROR;
1093                                                                 goto error;
1094                                                         }
1095
1096                                                         isFirstLargeObj = 1;
1097                                                 } else{
1098                                                         data = changedItem->data;
1099                                                         changedItem->data = NULL;
1100                                                         isFirstLargeObj = 0;
1101                                                 }
1102
1103                                                 unsigned int sizeOfData = 0;
1104                                                 if (data != NULL)
1105                                                         sizeOfData = strlen(data);
1106
1107                                                 FW_LOGV("sizeOfData = %d", sizeOfData);
1108                                                 if ((*pSession)->targetMaxMsgSize < stream_size + sizeOfData) {
1109                                                         /*can be LargeObj*/
1110                                                         int availableSize = (*pSession)->targetMaxMsgSize - stream_size;
1111                                                         if ((*pSession)->remoteDevInf->supportsLargeObjs /*&& availableSize > session->targetMaxMsgSize / 50*/) {
1112
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;
1118                                                                         goto error;
1119                                                                 }
1120                                                                 memcpy(sendingData, data, availableSize);
1121                                                                 FW_LOGV("sendingData = %s",  sendingData);
1122                                                                 FW_LOGV("sendingData size = %d", strlen(sendingData));
1123
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;
1128                                                                         goto error;
1129                                                                 }
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));
1134
1135                                                                 if (changedItem->data)
1136                                                                         free(changedItem->data);
1137
1138                                                                 set_changeditem_data(changedItem, remainingData);
1139                                                                 free(remainingData);
1140
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");
1145                                                                         goto error;
1146                                                                 }
1147                                                                 (*pSession)->largeObj =  pTemp;
1148
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);
1157                                                                 }
1158
1159                                                                 if (errorType != SA_INTERNAL_OK) {
1160                                                                         FW_LOGE("failed to create add(replace) command");
1161                                                                         goto error;
1162                                                                 }
1163
1164                                                                 if (sendingData != NULL) {
1165                                                                         free(sendingData);
1166                                                                         sendingData = NULL;
1167                                                                 }
1168                                                         } else{
1169                                                                 needNextMsg = true;
1170                                                                 break;
1171                                                         }
1172                                                 } else {
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);
1182                                                         }
1183                                                         if (errorType != SA_INTERNAL_OK) {
1184                                                                 FW_LOGE("failed to create add(replace) command");
1185                                                                 goto error;
1186                                                         }
1187                                                 }
1188                                         }
1189                                 } else{
1190                                         needNextMsg = true;
1191                                         break;
1192                                 }
1193
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;
1200
1201
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);
1206
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);
1211                                 } else{
1212                                         needNextMsg = true;
1213                                         break;
1214                                 }
1215
1216                                 if (data) {
1217                                         free(data);
1218                                         data = NULL;
1219                                 }
1220                         }
1221
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");
1226                                 goto error;
1227                         }
1228                         err = oma_ds_1_2_binder_append(pBinder, PE_SYNC_END, syncCommand);
1229                         put_into_list(commands, commands_last, syncCommand);
1230
1231                         if (needNextMsg)
1232                                 break;
1233
1234                         int chageItemCount = g_list_length(changedDatastore->changeItem);
1235                         if (chageItemCount == 0)
1236                                 changedDatastore->needSyncCommand = 0;
1237                 }
1238         }
1239
1240         if (data) {
1241                 free(data);
1242                 data = NULL;
1243         }
1244
1245         *isNeedNextMsg = needNextMsg;
1246
1247         FW_LOGV("end");
1248         return errorType;
1249
1250 error:
1251
1252         if (sendingData != NULL) {
1253                 free(sendingData);
1254                 sendingData = NULL;
1255         }
1256
1257         if (data != NULL) {
1258                 free(data);
1259                 data = NULL;
1260         }
1261
1262         FW_LOGE("error : %d", errorType);
1263         return errorType;
1264 }
1265
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)
1268 {
1269         FW_LOGV("start");
1270         NACI_RESULT res = NACI_SUCCESS;
1271         SA_ErrorType errorType = SA_INTERNAL_OK;
1272         bool isXML;
1273
1274         if (COMMUNICATION_TYPE == ENCODING_XML)
1275                 isXML = true;
1276         else
1277                 isXML = false;
1278
1279         NA_http_header_binder(accType, targetUri, isXML, &header_info);
1280         res = NACI_Send_Msg(/*acc_info (id, pw, uri, ...),*/
1281                                 header_info,
1282                                 transportType,
1283                                 sendMsg,
1284                                 sendMsg_length,
1285                                 recv_header,
1286                                 (unsigned char **)recvMsg,
1287                                 recvMsg_length,
1288                                 SEND_N_RECEIVE,
1289                                 session_id);
1290
1291         if (res != NACI_SUCCESS) {
1292                 FW_LOGE("res = %d", res);
1293                 errorType = SA_INTERNAL_CONNECTION_ERROR;
1294                 goto error;
1295         }
1296
1297         if (sendMsg)
1298                 free(sendMsg);
1299
1300         FW_LOGV("end");
1301         return errorType;
1302
1303 error:
1304         FW_LOGE("error = %d", errorType);
1305
1306         if (sendMsg)
1307                 free(sendMsg);
1308
1309         return errorType;
1310 }
1311
1312
1313 static SA_ErrorType __process_recv_msg(char *recvMsg, unsigned int recvMsg_length, void **syncReturnObj, int *isFinish)
1314 {
1315         FW_LOGV("start");
1316         SA_ErrorType errorType = SA_INTERNAL_OK;
1317
1318         reset_cmdID_session(session);
1319
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;
1324                 goto error;
1325         }
1326
1327         GList *pReturnStatus = NULL;
1328         GList *pReturnDatastore = NULL;
1329
1330         errorType = __reverse_object_binder(&pSyncML, recvMsg, recvMsg_length);
1331         if (errorType != SA_INTERNAL_OK) {
1332                 FW_LOGE("failed in reverseObjectBinder");
1333                 goto error;
1334         }
1335
1336         /* check receive msg */
1337         errorType = receive_header(session, pSyncML->hdr);
1338         if (errorType != SA_INTERNAL_OK) {
1339                 FW_LOGE("failed in receive_Header");
1340                 goto error;
1341         }
1342
1343         errorType = receive_statuses(session, pSyncML->status, &pReturnStatus);
1344         if (errorType != SA_INTERNAL_OK) {
1345                 FW_LOGE("failed in receive_Statuses");
1346                 goto error;
1347         }
1348
1349         errorType = receive_commands(session, pSyncML->commands, false, &pReturnDatastore);
1350         if (errorType != SA_INTERNAL_OK) {
1351                 FW_LOGE("failed in receive_Commands");
1352                 goto error;
1353         }
1354
1355         if (pSyncML->final) {
1356                 session->isSendingfinal = 0;
1357                 session->isReceivingFinal = 1;
1358         } else
1359                 session->isReceivingFinal = 0;
1360
1361         if (session->pkgStatus == SYNCML_PKG_2) {
1362                 PreSyncReturnObj ** pre = (PreSyncReturnObj **)syncReturnObj;
1363                 (*pre)->datastoreInfo = pReturnDatastore;
1364
1365                 if (session->remoteDevInf) {
1366                         if (session->remoteDevInf->devid != NULL)
1367                                 (*pre)->devID = strdup(session->remoteDevInf->devid);
1368                 }
1369
1370         } else {
1371                 SyncReturnObj **syncReturn = (SyncReturnObj **)syncReturnObj;
1372                 (*syncReturn)->status = pReturnStatus;
1373                 (*syncReturn)->changedDatastore = pReturnDatastore;
1374         }
1375
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) {
1380                         *isFinish = 1;
1381                         __clean_up_SA();
1382                 } else
1383                         session->pkgStatus = SYNCML_PKG_5;
1384         } else if (session->pkgStatus == SYNCML_PKG_6 && session->isReceivingFinal) {
1385                 *isFinish = 1;
1386                 __clean_up_SA();
1387         }
1388
1389         if (recvMsg)
1390                 free(recvMsg);
1391         if (pSyncML)
1392                 free_syncml(pSyncML);
1393
1394         FW_LOGV("end");
1395
1396         return errorType;
1397
1398 error:
1399         FW_LOGE("error : %d", errorType);
1400
1401         if (recvMsg)
1402                 free(recvMsg);
1403         if (pSyncML)
1404                 free_syncml(pSyncML);
1405
1406         return errorType;
1407
1408 }
1409
1410 static SA_ErrorType __generate_autoconfigure_msg(char *id, char *pwd, char *targetUrl, char **msg, unsigned int *msg_size, Session *pSession)
1411 {
1412         FW_LOGV("start");
1413         FW_LOGV("id = %s, pwd = %s, targetUrl = %s", id, pwd, targetUrl);
1414
1415         SA_ErrorType errorType = SA_INTERNAL_OK;
1416
1417         int isFinal = 1;
1418         char *credData = NULL;
1419         Cred *pCred = NULL;
1420         SyncHdr *pSyncHdr = NULL;
1421         SyncML *pSyncML = NULL;
1422
1423         GList *commands = NULL;
1424         GList *commands_last = NULL;
1425
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");
1429                 goto error;
1430         }
1431
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");
1435                 goto error;
1436         }
1437
1438         if (credData != NULL) {
1439                 free(credData);
1440                 credData = NULL;
1441         }
1442
1443         set_session_cred(pSession, pCred);
1444
1445         errorType = create_synchdr(pSession , &pSyncHdr);
1446         if (errorType != SA_INTERNAL_OK) {
1447                 FW_LOGE("failed to create synchdr");
1448                 goto error;
1449         }
1450
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;
1458
1459         Location *pLocation  = NULL;
1460         errorType = create_location(sourceDevInf, NULL, &pLocation);
1461         if (errorType != SA_INTERNAL_OK) {
1462                 FW_LOGE("failed to create location");
1463                 goto error;
1464         }
1465
1466         char *contentType = NULL;
1467         if (COMMUNICATION_TYPE == ENCODING_XML)
1468                 contentType = ELEMENT_DEVINF_XML;
1469         else
1470                 contentType = ELEMENT_DEVINF_WBXML;
1471
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");
1477                 goto error;
1478         }
1479         put_into_list(&commands, &commands_last, pGetCommand);
1480
1481
1482         errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
1483         if (errorType != SA_INTERNAL_OK) {
1484                 FW_LOGE("failed to get syncml");
1485                 goto error;
1486         }
1487
1488         errorType = __object_binder(pSyncML, msg, msg_size);
1489         if (errorType != SA_INTERNAL_OK) {
1490                 FW_LOGE("failed in objectBinder");
1491                 goto error;
1492         }
1493
1494         /*free generate structure*/
1495         free_syncml(pSyncML);
1496         pSyncML = NULL;
1497
1498         FW_LOGV("end");
1499
1500         return errorType;
1501
1502 error:
1503         if (pSyncHdr)
1504                 free_synchdr(pSyncHdr);
1505         if (pCred)
1506                 free_cred(pCred);
1507         if (credData != NULL)
1508                 free(credData);
1509         if (commands)
1510                 free_commands(commands);
1511         if (pSyncML)
1512                 free(pSyncML);
1513
1514         FW_LOGE("error : %d", errorType);
1515
1516         return errorType;
1517 }
1518
1519 static SA_ErrorType __process_autoconfigure_recv_msg(char *recvMsg, unsigned int recvMsg_length, Session *pSession)
1520 {
1521         FW_LOGV("start");
1522
1523         SA_ErrorType errorType = SA_INTERNAL_OK;
1524
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;
1529                 goto error;
1530         }
1531
1532         errorType = __reverse_object_binder(&pSyncML, recvMsg, recvMsg_length);
1533         if (errorType != SA_INTERNAL_OK) {
1534                 FW_LOGE("failed in reverseObjectBinder");
1535                 goto error;
1536         }
1537
1538         /* check receive msg */
1539         errorType = receive_header(pSession, pSyncML->hdr);
1540         if (errorType != SA_INTERNAL_OK) {
1541                 FW_LOGE("failed in receive_Header");
1542                 goto error;
1543         }
1544
1545         errorType = receive_statuses(pSession, pSyncML->status, NULL);
1546         if (errorType != SA_INTERNAL_OK) {
1547                 FW_LOGE("failed in receive_Statuses");
1548                 goto error;
1549         }
1550
1551         errorType = receive_commands(pSession,  pSyncML->commands, true, NULL);
1552         if (errorType != SA_INTERNAL_OK) {
1553                 FW_LOGE("failed in receive_Commands");
1554                 goto error;
1555         }
1556
1557         if (recvMsg)
1558                 free(recvMsg);
1559         if (pSyncML != NULL)
1560                 free_syncml(pSyncML);
1561
1562         FW_LOGV("end");
1563
1564         return errorType;
1565
1566 error:
1567         FW_LOGE("error: %d", errorType);
1568
1569         if (recvMsg)
1570                 free(recvMsg);
1571         if (pSyncML)
1572                 free_syncml(pSyncML);
1573         return errorType;
1574 }
1575
1576 static SA_ErrorType __object_binder(SyncML *pSyncML, char **msg, unsigned int *msg_size)
1577 {
1578         FW_LOGV("start");
1579
1580         SA_ErrorType errorType = SA_INTERNAL_OK;
1581
1582         Binder_function_set *pBinder_function_set = NULL;
1583
1584         PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
1585
1586         protocol_binder *pBinder = NULL;
1587         char *xml = NULL;
1588         unsigned int xml_len = 0;
1589
1590         pBinder = oma_ds_1_2_binder_init(pBinder, COMMUNICATION_TYPE, false, false, false, true, pBinder_function_set);
1591
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);
1595
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);
1601         }
1602
1603         GList *iter = NULL;
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);
1608
1609                 CommandType type = pCommand->type;
1610                 switch (type) {
1611                 case COMMAND_TYPE_UNKNOWN:
1612                         protocol_element = PE_UNDEF;
1613                         break;
1614                 case COMMAND_TYPE_ALERT:
1615                         protocol_element = PE_ALERT;
1616                         break;
1617                 case COMMAND_TYPE_SYNC_START:
1618                         protocol_element = PE_SYNC_START;
1619                         break;
1620                 case COMMAND_TYPE_SYNC_END:
1621                         protocol_element = PE_SYNC_END;
1622                         break;
1623                 case COMMAND_TYPE_PUT:
1624                         protocol_element = PE_PUT_START;
1625                         break;
1626                 case COMMAND_TYPE_ADD:
1627                         protocol_element = PE_ADD;
1628                         break;
1629                 case COMMAND_TYPE_REPLACE:
1630                         protocol_element = PE_REPLACE;
1631                         break;
1632                 case COMMAND_TYPE_DELETE:
1633                         protocol_element = PE_DELETE;
1634                         break;
1635                 case COMMAND_TYPE_MAP:
1636                         protocol_element = PE_MAP;
1637                         break;
1638                 case COMMAND_TYPE_GET:
1639                         protocol_element = PE_GET;
1640                         break;
1641                 case COMMAND_TYPE_RESULTS:
1642                         protocol_element = PE_RESULTS_START;
1643                         break;
1644                 case COMMAND_TYPE_HEADER:
1645                         protocol_element = PE_HEADER;
1646                         break;
1647                 }
1648                 err = oma_ds_1_2_binder_append(pBinder, protocol_element, pCommand);
1649
1650                 if (type == COMMAND_TYPE_RESULTS) {
1651                         err = oma_ds_1_2_binder_append(pBinder, PE_RESULTS_END, NULL);
1652                 }
1653
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);
1658                                 }
1659                         }
1660                         err = oma_ds_1_2_binder_append(pBinder, PE_PUT_END, NULL);
1661                 }
1662         }
1663
1664         if (pSyncML->final) {
1665                 err = oma_ds_1_2_binder_append(pBinder, PE_FINAL, NULL);
1666         }
1667
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);
1670
1671         oma_ds_1_2_binder_get_stream(pBinder, msg, msg_size);
1672
1673         /*FIXME for debugging*/
1674         if (COMMUNICATION_TYPE == ENCODING_XML)
1675                 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
1676         else{
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);
1680
1681                         if (xml != NULL) {
1682                                 free(xml);
1683                                 xml = NULL;
1684                         }
1685                 }
1686         }
1687
1688         oma_ds_1_2_binder_terminate(pBinder);
1689
1690         if (err != PROTOCOL_BINDER_OK)
1691                 errorType = SA_INTERNAL_BINDER_ERROR;
1692
1693         FW_LOGV("end");
1694
1695         return errorType;
1696 }
1697
1698 static SA_ErrorType __reverse_object_binder(SyncML **pSyncML, char *recv_msg, unsigned int recv_msg_length)
1699 {
1700         FW_LOGV("start");
1701
1702         SA_ErrorType errorType = SA_INTERNAL_OK;
1703         Binder_function_set *pBinder_function_set = NULL;
1704
1705         PROTOCOL_BINDER_ERROR err = init_oma_ds_1_2_binder_function_set(&pBinder_function_set);
1706         Reverse_protocol_binder *pBinder = NULL;
1707         char *xml = NULL;
1708         unsigned int xml_len = 0;
1709
1710         Decoding dec;
1711         if (COMMUNICATION_TYPE == ENCODING_XML)
1712                 dec = DECODING_XML;
1713         else
1714                 dec = DECODING_WBXML;
1715
1716         err = reverse_oma_ds_1_2_binder_init(recv_msg, recv_msg_length,
1717                                                                                         dec, pBinder_function_set, NULL, &pBinder);
1718
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);
1722                 return errorType;
1723         }
1724
1725
1726         /*FIXME for debugging*/
1727         if (COMMUNICATION_TYPE == ENCODING_XML)
1728                 set_xml_to_file(recv_msg, OMA_DS_MSG_PATH);
1729         else{
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);
1733
1734                         if (xml != NULL) {
1735                                 free(xml);
1736                                 xml = NULL;
1737                         }
1738                 }
1739         }
1740
1741
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) {
1750                         case PE_ADD:
1751                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1752                                 break;
1753                         case PE_ATOMIC_START:
1754                         case PE_ATOMIC_END:
1755                         case PE_COPY:
1756                         case PE_DELETE:
1757                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1758                                 break;
1759                         case PE_EXEC:
1760                         case PE_GET:
1761                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1762                                 break;
1763                         case PE_MAP:
1764                         case PE_PUT_START:
1765                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1766                                 tempCommandfordevInf = pContent;
1767                                 break;
1768                         case PE_SEARCH:
1769                         case PE_SEQUENCE_START:
1770                         case PE_SEQUENCE_END:
1771                         case PE_SYNC_START:
1772                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1773                                 break;
1774                         case PE_SYNC_END:
1775                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1776                                 protocol_element = PE_UNDEF;
1777                                 break;
1778                         case PE_REPLACE:
1779                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1780                                 break;
1781                         case PE_ALERT:
1782                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1783                                 break;
1784                         case PE_RESULTS_START:
1785                                 (*pSyncML)->commands = g_list_append((*pSyncML)->commands, pContent);
1786                                 tempCommandfordevInf = pContent;
1787                                 break;
1788                         case PE_STATUS:
1789                                 (*pSyncML)->status = g_list_append((*pSyncML)->status, pContent);
1790                                 protocol_element = PE_UNDEF;
1791                                 break;
1792                         case PE_HEADER:
1793                                 (*pSyncML)->hdr = pContent;
1794                                 reverse_protocol_binder_set_user_data(pBinder, (*pSyncML)->hdr);
1795                                 break;
1796                         case PE_PUT_GET:
1797                         case PE_CMD_GROUP:
1798                         case PE_GENERIC:
1799                         case PE_FINAL:
1800                                 (*pSyncML)->final = (int)pContent;
1801                                 break;
1802                         case PE_DEVINF:
1803                                 if (tempCommandfordevInf) {
1804                                         tempCommandfordevInf->private.results.item->private.devInf = pContent;
1805                                         tempCommandfordevInf = NULL;
1806                                 }
1807                                 break;
1808                         default:
1809                                 break;
1810                 }
1811
1812                 if (protocol_element_name != NULL) {
1813                         free(protocol_element_name);
1814                         protocol_element_name = NULL;
1815                 }
1816
1817                 if (err == PROTOCOL_BINDER_HAS_NO_MORE_ELEMENT) {
1818                         break;
1819                 }
1820         }
1821
1822         reverse_protocol_binder_terminate(pBinder);
1823
1824         free_oma_ds_1_2_binder_function_set(pBinder_function_set);
1825
1826         if (err != PROTOCOL_BINDER_OK && err != PROTOCOL_BINDER_HAS_NO_MORE_ELEMENT)
1827                 errorType = SA_INTERNAL_BINDER_ERROR;
1828
1829         return errorType;
1830 }
1831
1832 static Common_ErrorType __convert_error_type(SA_ErrorType errorType)
1833 {
1834         Common_ErrorType sa_errorType = COMMON_OK;
1835
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;
1859
1860
1861         return sa_errorType;
1862
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)
1867
1868         1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
1869
1870         1513 -> ERROR_INTERNAL_NOT_DEFINED
1871         1512 -> ERROR_INTERNAL_NO_MEMORY
1872         1503 -> ERROR_INTERNAL_BINDER_ERROR
1873
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)
1876
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
1884
1885
1886         from exchangeMsg
1887
1888         from processRecvMsg
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)
1892
1893         1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
1894
1895         1513 -> ERROR_INTERNAL_NOT_DEFINED
1896         1512 -> ERROR_INTERNAL_NO_MEMORY
1897         1503 -> ERROR_INTERNAL_BINDER_ERROR
1898
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)*/
1901
1902 }
1903
1904 static void __clean_up_SA()
1905 {
1906         FW_LOGV("start");
1907
1908         if (session != NULL) {
1909
1910                 if (session->hasOpend) {
1911                         NACI_Close_Connection(TRANSPORT_TYPE, session->NACI_sessionID);
1912                         session->hasOpend = 0;
1913                 }
1914
1915                 free_session(session);
1916                 session = NULL;
1917         }
1918
1919         FW_LOGV("end");
1920 }
1921
1922 int pre_sync(int transportType, int accountId, char *sessionId,  void **preSyncReturnObj)
1923 {
1924         FW_LOGV("start");
1925
1926         SA_ErrorType errorType = SA_INTERNAL_OK;
1927
1928         char *msg = NULL;
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;
1935
1936         /* generate preSync Msg */
1937         errorType =  __generate_presync_msg(accountId, sessionId, &msg, &msg_size);
1938         if (errorType != SA_INTERNAL_OK)
1939                 goto error;
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;
1945                         goto error;
1946                 }
1947                 session->NACI_sessionID = session_id;
1948                 session->hasOpend = 1;
1949                 FW_LOGV("session_id = %d", session_id);
1950         }
1951         /* exchange Msg */
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)
1955                 goto error;
1956
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)
1963                         goto error;
1964
1965                 if (header_info != NULL) {
1966                         GList *iter = 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);
1970                         header_info = NULL;
1971                 }
1972
1973                 if (recv_header != NULL) {
1974                         GList *iter = NULL;
1975                         common_header_info *iter_data;
1976                         for (iter = recv_header; iter != NULL;) {
1977                                 iter_data = NULL;
1978                                 iter_data = ((common_header_info *)(iter->data));
1979
1980                                 iter = g_list_next(iter);
1981                                 recv_header = g_list_remove(recv_header, iter_data);
1982
1983                                 if (iter_data->key != NULL)
1984                                         free(iter_data->key);
1985                                 if (iter_data->value != NULL)
1986                                         free(iter_data->value);
1987                                 free(iter_data);
1988                         }
1989                         g_list_free(recv_header);
1990                 }
1991
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)
1995                         goto error;
1996
1997                 errorType = __process_recv_msg(recv_msg, recv_msg_size, preSyncReturnObj, NULL);
1998                 if (errorType != SA_INTERNAL_OK)
1999                         goto error;
2000         }
2001
2002         FW_LOGV("end");
2003
2004 error:
2005         if (header_info != NULL) {
2006                 GList *iter = 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);
2010                 header_info = NULL;
2011         }
2012
2013         if (recv_header != NULL) {
2014                 GList *iter = NULL;
2015                 common_header_info *iter_data;
2016                 for (iter = recv_header; iter != NULL;) {
2017                         iter_data = NULL;
2018                         iter_data = ((common_header_info *)(iter->data));
2019
2020                         iter = g_list_next(iter);
2021                         recv_header = g_list_remove(recv_header, iter_data);
2022
2023                         if (iter_data->key != NULL)
2024                                 free(iter_data->key);
2025                         if (iter_data->value != NULL)
2026                                 free(iter_data->value);
2027                         free(iter_data);
2028                 }
2029                 g_list_free(recv_header);
2030         }
2031
2032         if (errorType != SA_INTERNAL_OK) {
2033                 FW_LOGE("errorType : %d", errorType);
2034                 __clean_up_SA();
2035         }
2036
2037         return __convert_error_type(errorType);
2038 }
2039
2040 int generate_msg(void **syncObj, char **msg, unsigned int *msg_size)
2041 {
2042         FW_LOGV("start");
2043
2044         SA_ErrorType errorType = SA_INTERNAL_OK;
2045
2046         errorType = __generate_msg(syncObj, msg, msg_size);
2047         if (errorType != SA_INTERNAL_OK)
2048                 goto error;
2049
2050         FW_LOGV("end");
2051
2052 error:
2053
2054         if (errorType != SA_INTERNAL_OK) {
2055                 FW_LOGE("errorType : %d", errorType);
2056                 __clean_up_SA();
2057         }
2058
2059         return __convert_error_type(errorType);
2060 }
2061
2062 int exchange_msg(int transportType, char *sendMsg, unsigned int sendMsg_length, char **recvMsg, unsigned int *recvMsg_length)
2063 {
2064         FW_LOGV("start");
2065
2066         SA_ErrorType errorType = SA_INTERNAL_OK;
2067
2068         GList *header_info = NULL;
2069         GList *recv_header = NULL;
2070
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)
2074                 goto error;
2075
2076         FW_LOGV("end");
2077
2078 error:
2079
2080         if (header_info != NULL) {
2081                 GList *iter = 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);
2085                 header_info = NULL;
2086         }
2087
2088         if (recv_header != NULL) {
2089                 GList *iter = NULL;
2090                 common_header_info *iter_data;
2091                 for (iter = recv_header; iter != NULL;) {
2092                         iter_data = NULL;
2093                         iter_data = ((common_header_info *)(iter->data));
2094
2095                         iter = g_list_next(iter);
2096                         recv_header = g_list_remove(recv_header, iter_data);
2097
2098                         if (iter_data->key != NULL)
2099                                 free(iter_data->key);
2100                         if (iter_data->value != NULL)
2101                                 free(iter_data->value);
2102                         free(iter_data);
2103                 }
2104                 g_list_free(recv_header);
2105         }
2106
2107          if (errorType != SA_INTERNAL_OK) {
2108                  FW_LOGE("errorType : %d", errorType);
2109                  __clean_up_SA();
2110          }
2111
2112          return __convert_error_type(errorType);
2113 }
2114
2115 int process_recv_msg(char *recvMsg, unsigned int recvMsg_length, void **syncReturnObj, int *isFinish)
2116 {
2117         FW_LOGV("start");
2118
2119         SA_ErrorType errorType = SA_INTERNAL_OK;
2120
2121         errorType = __process_recv_msg(recvMsg, recvMsg_length, syncReturnObj, isFinish);
2122         if (errorType != SA_INTERNAL_OK)
2123                 goto error;
2124
2125         FW_LOGV("end");
2126
2127 error:
2128
2129          if (errorType != SA_INTERNAL_OK) {
2130                  FW_LOGE("errorType : %d", errorType);
2131                  __clean_up_SA();
2132          }
2133
2134          return __convert_error_type(errorType);
2135 }
2136
2137 void clean_up_SA()
2138 {
2139         FW_LOGV("start");
2140
2141         __clean_up_SA();
2142
2143         FW_LOGV("end");
2144 }
2145
2146 int auto_configure_from_vconf(char *id, char *pwd, char *targetUrl, char *profilePath, GList **configure)
2147 {
2148         FW_LOGV("start");
2149
2150         SA_ErrorType errorType = SA_INTERNAL_OK;
2151         Session *pSession = NULL;
2152
2153         char *sourceUrl = NULL;
2154         char *sessionId = NULL;
2155
2156         char *msg = NULL;
2157         unsigned int msg_size = 0;
2158
2159         GList *header_info = NULL;
2160         GList *recv_header = NULL;
2161         char *recv_msg = NULL;
2162         unsigned int recv_msg_size = 0;
2163
2164         bool cancel_flag = false;
2165         unsigned int session_id;
2166
2167         sessionId = g_strdup_printf("%ld", time(NULL));
2168         if (sessionId == NULL) {
2169                 errorType = SA_INTERNAL_NO_MEMORY;
2170                 goto error;
2171         }
2172
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;
2177                 goto error;
2178         }
2179
2180         errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, sourceUrl, targetUrl, &pSession);
2181         if (errorType != SA_INTERNAL_OK)
2182                 goto error;
2183
2184         if (pSession == NULL) {
2185                 FW_LOGV("failed to create session");
2186                 errorType = SA_INTERNAL_NO_MEMORY;
2187                 goto error;
2188         }
2189
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");
2193                 goto error;
2194         }
2195
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;
2201                         goto error;
2202                 }
2203                 pSession->NACI_sessionID = session_id;
2204                 pSession->hasOpend = 1;
2205                 FW_LOGV("session_id = %d", session_id);
2206         }
2207
2208         cancel_flag = task_process_check_cancel_flag();
2209         if (cancel_flag) {
2210                 errorType = SA_INTERNAL_CANCEL;
2211                 goto error;
2212         }
2213
2214         /* exchange Msg */
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");
2219                 goto error;
2220         }
2221
2222         cancel_flag = task_process_check_cancel_flag();
2223         if (cancel_flag) {
2224                 errorType = SA_INTERNAL_CANCEL;
2225                 goto error;
2226         }
2227
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");
2231                 goto error;
2232         }
2233
2234         cancel_flag = task_process_check_cancel_flag();
2235         if (cancel_flag) {
2236                 errorType = SA_INTERNAL_CANCEL;
2237                 goto error;
2238         }
2239
2240         DevInf *devInf = pSession->remoteDevInf;
2241         if (devInf) {
2242
2243                 GList *tempConfigure = NULL;
2244
2245                 GList *contactCandidate = NULL;
2246                 GList *calendarCandidate = NULL;
2247                 GList *memoCandidate = NULL;
2248
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);
2253                 if (target) {
2254                         FW_LOGV("Contacts target =%s",  target);
2255                         if (strcmp(target, "") != 0)
2256                                 contactCandidate = g_list_append(contactCandidate, strdup(target));
2257
2258                         free(target);
2259                         target = NULL;
2260                 }
2261
2262                 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2263                 target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2264                 if (target) {
2265                         FW_LOGV("Organizer target =%s", target);
2266                         if (strcmp(target, "") != 0)
2267                                 calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2268
2269                         free(target);
2270                         target = NULL;
2271                 }
2272
2273                 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2274                 target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2275                 if (target) {
2276                         FW_LOGV("Memo target =%s", target);
2277                         if (strcmp(target, "") != 0)
2278                                 memoCandidate = g_list_append(memoCandidate, strdup(target));
2279
2280                         free(target);
2281                         target = NULL;
2282                 }
2283
2284                 GList *iter = NULL;
2285                 GList *innerIter = NULL;
2286                 DevInfDataStore *devInfDataStore = NULL;
2287                 int exist;
2288                 char *candidate;
2289                 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2290                         devInfDataStore = iter->data;
2291                         exist = 0;
2292                         candidate = NULL;
2293
2294                         FW_LOGV("devInfDataStore->sourceref = %s", devInfDataStore->sourceref);
2295
2296                         if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD) == 0 ||
2297                                         strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD_30) == 0) {
2298
2299                                 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2300                                         candidate = innerIter->data;
2301
2302                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2303                                                 exist = 1;
2304                                 }
2305                                 if (!exist)
2306                                         contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->sourceref));
2307
2308                         } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCAL) == 0 ||
2309                                         strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_ICAL) == 0) {
2310
2311                                 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2312                                         candidate = innerIter->data;
2313
2314                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2315                                                 exist = 1;
2316                                 }
2317                                 if (!exist)
2318                                         calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->sourceref));
2319
2320                         } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_PLAIN) == 0) {
2321
2322                                 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2323                                         candidate = innerIter->data;
2324
2325                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2326                                                 exist = 1;
2327                                 }
2328                                 if (!exist)
2329                                         memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->sourceref));
2330                         }
2331                 }
2332
2333                 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2334                 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2335                 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2336
2337                 *configure = tempConfigure;
2338
2339 //              *congifure = g_list_append(*congifure, contactCandidate);
2340 //              *congifure = g_list_append(*congifure, calendarCandidate);
2341 //              *congifure = g_list_append(*congifure, memoCandidate);
2342
2343         } else{
2344                 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2345                 goto error;
2346         }
2347
2348         FW_LOGV("end");
2349
2350 error:
2351
2352         /*close network*/
2353         if (pSession != NULL) {
2354                 if (pSession->hasOpend)
2355                         NACI_Close_Connection(TRANSPORT_TYPE, pSession->NACI_sessionID);
2356         }
2357
2358         if (header_info != NULL) {
2359                 GList *iter = 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);
2363                 header_info = NULL;
2364         }
2365
2366         if (recv_header != NULL) {
2367                 GList *iter = NULL;
2368                 common_header_info *iter_data;
2369                 for (iter = recv_header; iter != NULL;) {
2370                         iter_data = NULL;
2371                         iter_data = ((common_header_info *)(iter->data));
2372
2373                         iter = g_list_next(iter);
2374                         recv_header = g_list_remove(recv_header, iter_data);
2375
2376                         if (iter_data->key != NULL)
2377                                 free(iter_data->key);
2378                         if (iter_data->value != NULL)
2379                                 free(iter_data->value);
2380                         free(iter_data);
2381                 }
2382                 g_list_free(recv_header);
2383         }
2384
2385         if (sessionId)
2386                 free(sessionId);
2387
2388         if (sourceUrl)
2389                 free(sourceUrl);
2390
2391         if (pSession) {
2392                 free_session(pSession);
2393                 pSession = NULL;
2394         }
2395
2396         if (errorType != SA_INTERNAL_OK)
2397                 FW_LOGE("errorType =%d", errorType);
2398
2399         return __convert_error_type(errorType);
2400 }
2401
2402
2403 int auto_configure(char *addr, char *id, char *password, GList **configure)
2404 {
2405         FW_LOGV("start");
2406
2407         SA_ErrorType errorType = SA_INTERNAL_OK;
2408         Session *pSession = NULL;
2409
2410         char *sourceUrl = NULL;
2411         char *sessionId = NULL;
2412
2413         char *msg = NULL;
2414         unsigned int msg_size = 0;
2415
2416         GList *header_info = NULL;
2417         GList *recv_header = NULL;
2418         char *recv_msg = NULL;
2419         unsigned int recv_msg_size = 0;
2420
2421         bool cancel_flag = false;
2422         unsigned int session_id;
2423
2424         sessionId = g_strdup_printf("%ld", time(NULL));
2425         if (sessionId == NULL) {
2426                 errorType = SA_INTERNAL_NO_MEMORY;
2427                 goto error;
2428         }
2429
2430         DCI_RETURN err = DCI_Get_DevInfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
2431         if (err != DCI_SUCCESS) {
2432                 errorType = SA_INTERNAL_ERROR;
2433                 goto error;
2434         }
2435
2436         errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, sourceUrl, addr, &pSession);
2437         if (errorType != SA_INTERNAL_OK)
2438                 goto error;
2439
2440         if (pSession == NULL) {
2441                 errorType = SA_INTERNAL_NO_MEMORY;
2442                 goto error;
2443         }
2444
2445         errorType = __generate_autoconfigure_msg(id, password, addr, &msg, &msg_size , pSession);
2446         if (errorType != SA_INTERNAL_OK)
2447                 goto error;
2448
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;
2454                         goto error;
2455                 }
2456                 pSession->NACI_sessionID = session_id;
2457                 pSession->hasOpend = 1;
2458                 FW_LOGV("session_id = %d", session_id);
2459         }
2460
2461         cancel_flag = task_process_check_cancel_flag();
2462         if (cancel_flag) {
2463                 errorType = SA_INTERNAL_CANCEL;
2464                 goto error;
2465         }
2466
2467         /* exchange Msg */
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)
2471                 goto error;
2472
2473         cancel_flag = task_process_check_cancel_flag();
2474         if (cancel_flag) {
2475                 errorType = SA_INTERNAL_CANCEL;
2476                 goto error;
2477         }
2478
2479         errorType = __process_autoconfigure_recv_msg(recv_msg, recv_msg_size, pSession);
2480         if (errorType != SA_INTERNAL_OK)
2481                 goto error;
2482
2483         cancel_flag = task_process_check_cancel_flag();
2484         if (cancel_flag) {
2485                 errorType = SA_INTERNAL_CANCEL;
2486                 goto error;
2487         }
2488
2489         DevInf *devInf = pSession->remoteDevInf;
2490         if (devInf) {
2491
2492                 GList *tempConfigure = NULL;
2493
2494                 GList *contactCandidate = NULL;
2495                 GList *calendarCandidate = NULL;
2496                 GList *memoCandidate = NULL;
2497
2498                 /*set inputted name by user*/
2499                 contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI));
2500                 /*
2501                 char *target = NULL;
2502                 target= get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI);
2503                 if (target) {
2504                         FW_LOGV("Contacts target =%s",  target);
2505                         if (strcmp(target, "") !=0)
2506                                 contactCandidate = g_list_append(contactCandidate, strdup(target));
2507
2508                         free(target);
2509                         target = NULL;
2510                 }
2511                 */
2512
2513                 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2514                 /*
2515                 target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2516                 if (target) {
2517                         FW_LOGV("Organizer target =%s", target);
2518                         if (strcmp(target, "") !=0)
2519                                 calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2520
2521                         free(target);
2522                         target = NULL;
2523                 }
2524                 */
2525
2526                 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2527                 /*
2528                 target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2529                 if (target) {
2530                         FW_LOGV("Memo target =%s", target);
2531                         if (strcmp(target, "") !=0)
2532                                 memoCandidate = g_list_append(memoCandidate, strdup(target));
2533
2534                         free(target);
2535                         target = NULL;
2536                 }
2537                 */
2538
2539                 GList *iter = NULL;
2540                 GList *innerIter = NULL;
2541                 DevInfDataStore *devInfDataStore = NULL;
2542                 int exist;
2543                 char *candidate;
2544                 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2545                         devInfDataStore = iter->data;
2546                         exist = 0;
2547                         candidate = NULL;
2548
2549                         FW_LOGV("devInfDataStore->sourceref = %s", devInfDataStore->sourceref);
2550
2551                         if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD) == 0 ||
2552                                         strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCARD_30) == 0) {
2553
2554                                 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2555                                         candidate = innerIter->data;
2556
2557                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2558                                                 exist = 1;
2559                                 }
2560                                 if (!exist)
2561                                         contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->sourceref));
2562
2563                         } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_VCAL) == 0 ||
2564                                         strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_ICAL) == 0) {
2565
2566                                 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2567                                         candidate = innerIter->data;
2568
2569                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2570                                                 exist = 1;
2571                                 }
2572                                 if (!exist)
2573                                         calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->sourceref));
2574
2575                         } else if (strcmp(devInfDataStore->rxPref->cttype, ELEMENT_TEXT_PLAIN) == 0) {
2576
2577                                 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2578                                         candidate = innerIter->data;
2579
2580                                         if (strcmp(candidate, devInfDataStore->sourceref) == 0)
2581                                                 exist = 1;
2582                                 }
2583                                 if (!exist)
2584                                         memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->sourceref));
2585                         }
2586                 }
2587
2588                 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2589                 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2590                 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2591
2592                 *configure = tempConfigure;
2593
2594
2595 //              *congifure = g_list_append(*congifure, contactCandidate);
2596 //              *congifure = g_list_append(*congifure, calendarCandidate);
2597 //              *congifure = g_list_append(*congifure, memoCandidate);
2598
2599         } else{
2600                 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2601                 goto error;
2602         }
2603
2604         FW_LOGV("end");
2605
2606 error:
2607
2608         /*close network*/
2609          if (pSession != NULL) {
2610                  if (pSession->hasOpend)
2611                          NACI_Close_Connection(TRANSPORT_TYPE, pSession->NACI_sessionID);
2612          }
2613
2614          if (header_info != NULL) {
2615                  GList *iter = 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);
2619                  header_info = NULL;
2620          }
2621
2622         if (recv_header != NULL) {
2623                 GList *iter = NULL;
2624                 common_header_info *iter_data;
2625                 for (iter = recv_header; iter != NULL;) {
2626                         iter_data = NULL;
2627                         iter_data = ((common_header_info *)(iter->data));
2628
2629                         iter = g_list_next(iter);
2630                         recv_header = g_list_remove(recv_header, iter_data);
2631
2632                         if (iter_data->key != NULL)
2633                                 free(iter_data->key);
2634                         if (iter_data->value != NULL)
2635                                 free(iter_data->value);
2636                         free(iter_data);
2637                 }
2638                 g_list_free(recv_header);
2639         }
2640
2641         if (sessionId)
2642                 free(sessionId);
2643
2644         if (sourceUrl)
2645                 free(sourceUrl);
2646
2647         if (pSession) {
2648                 free_session(pSession);
2649                 pSession = NULL;
2650         }
2651
2652         if (errorType != SA_INTERNAL_OK)
2653                 FW_LOGE("errorType =%d", errorType);
2654
2655         return __convert_error_type(errorType);
2656 }
2657
2658 /*FIXME cancel request to NA */
2659 /*int cancel_connection_sync_request(char *transportType)
2660 {
2661         FW_LOGV("start");
2662
2663         SA_ErrorType err = SA_INTERNAL_OK;
2664         NACI_RESULT res = NACI_SUCCESS;
2665
2666         FW_LOGV("sessionId = %d", session->NACI_sessionID);
2667
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;
2672                 goto error;
2673         }
2674
2675         FW_LOGV("end");
2676
2677 error:
2678
2679         return __convert_error_type(err);
2680 }*/