Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / service-adapter / sa_common_interface.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  *   @SA_Common_Interface.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of interface of Service Adapter
22  */
23
24 #include <sync_agent.h>
25
26 #include "common/common_vconf.h"
27 #include "common/common_define.h"
28 #include "common/common_util.h"
29 #include "service-adapter/protocol-binder/oma_ds_protocol_binder.h"
30 #include "service-adapter/network-binder/na_binder.h"
31 #include "service-adapter/sa_define.h"
32 #include "service-adapter/sa_common_interface.h"
33 #include "service-adapter/sa_elements.h"
34 #include "service-adapter/sa_elements_internal.h"
35 #include "service-adapter/sa_command.h"
36 #include "service-adapter/sa_command_internal.h"
37 #include "service-adapter/sa_session.h"
38 #include "service-adapter/sa_session_internal.h"
39 #include "service-adapter/sa_devinf_internal.h"
40 #include "service-adapter/sa_util.h"
41 #include "service-adapter/sa_devinf.h"
42
43 #ifndef OMADS_AGENT_LOG
44 #undef LOG_TAG
45 #define LOG_TAG "OMA_DS_SA"
46 #endif
47
48 session_s *static_session = NULL;
49
50 static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session);
51 static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size);
52 static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size);
53 static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session);
54 static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size);
55 static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length);
56
57 static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish);
58 static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session);
59 static common_error_type_e _convert_error_type(sa_error_type_e error_type);
60
61 static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce);
62 static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size);
63 static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length);
64 static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last);
65 static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last);
66 static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg);
67 static void __clean_up_sa(session_s * session);
68 static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header);
69 static sa_error_type_e __check_resume_session(session_s * session, bool * resume);
70
71 static sa_error_type_e __get_account_info(int account_id, char **id, char **pwd, char **server_url, char **next_nonce)
72 {
73         _INNER_FUNC_ENTER;
74
75         sa_error_type_e errorType = SA_INTERNAL_OK;
76
77         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
78         GList *config_list = NULL;
79         GList *iter = NULL;
80         sync_agent_da_config_s *config_data = NULL;
81
82         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
83         sync_agent_fw_account_s *fw_account = NULL;
84
85         if (!account_id) {
86                 errorType = SA_INTERNAL_MISCONFIGURATION;
87                 goto error;
88         }
89
90         acc_err = sync_agent_create_fw_account(&fw_account);
91         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
92                 _DEBUG_ERROR("failed in sync_agent_create_fw_account");
93                 errorType = SA_INTERNAL_NO_MEMORY;
94                 goto error;
95         }
96
97         acc_err = sync_agent_get_fw_account(account_id, &fw_account);
98         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
99                 _DEBUG_ERROR("failed in sync_agent_update_fw_account");
100                 errorType = SA_INTERNAL_ERROR;
101                 goto error;
102         }
103
104         if (fw_account->email != NULL)
105                 *id = strdup(fw_account->email);
106
107         if (fw_account->password != NULL)
108                 *pwd = strdup(fw_account->password);
109
110         da_err = sync_agent_get_config_list(account_id, &config_list);
111         if (da_err != SYNC_AGENT_DA_SUCCESS) {
112                 errorType = SA_INTERNAL_DA_ERROR;
113                 goto error;
114         }
115
116         for (iter = config_list; iter != NULL; iter = g_list_next(iter)) {
117                 config_data = (sync_agent_da_config_s *) iter->data;
118
119                 if (config_data != NULL) {
120                         if (config_data->key != NULL) {
121                                 if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_SERVER_IP) == 0) {
122                                         if (config_data->value != NULL)
123                                                 *server_url = strdup(config_data->value);
124                                 }
125
126                                 if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_NEXT_NONCE) == 0) {
127                                         if (config_data->value != NULL)
128                                                 *next_nonce = strdup(config_data->value);
129                                 }
130                         }
131                 }
132         }
133
134  error:
135
136         sync_agent_free_fw_account(fw_account);
137
138         sync_agent_free_config_list(config_list);
139
140         _INNER_FUNC_EXIT;
141         return errorType;
142 }
143
144 static sa_error_type_e _create_session(int account_id, char *session_id, bool server_flag, session_s ** session)
145 {
146         _INNER_FUNC_ENTER;
147
148         sa_error_type_e errorType = SA_INTERNAL_OK;
149         char *id = NULL;
150         char *pwd = NULL;
151         char *targetUrl = NULL;
152         char *sourceUrl = NULL;
153         char *sourceName = NULL;
154         char *nextNonce = NULL;
155         chal_s *pTempChal = NULL;
156         unsigned int sessionID = 0;
157
158         location_s *pSourceLocation = NULL;
159         location_s *pTargetLocation = NULL;
160
161         if ((*session) != NULL) {
162                 if ((*session)->chal != NULL) {
163                         pTempChal = (*session)->chal;
164                         (*session)->chal = NULL;
165                 }
166
167                 if ((*session)->has_opend)
168                         sessionID = (*session)->naci_session_id;
169
170                 free_session(*session);
171                 *session = NULL;
172         }
173
174         sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
175         if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
176                 _DEBUG_ERROR("failed to get devinfo");
177                 errorType = SA_INTERNAL_ERROR;
178                 goto error;
179         }
180
181         errorType = __get_account_info(account_id, &id, &pwd, &targetUrl, &nextNonce);
182         if (errorType != SA_INTERNAL_OK) {
183                 _DEBUG_ERROR("faied in __get_accountinfo");
184                 goto error;
185         }
186
187         if (server_flag == true) {
188                 sourceName = id;
189         }
190
191         errorType = create_location(sourceUrl, sourceName, &pSourceLocation);
192         if (errorType != SA_INTERNAL_OK) {
193                 _DEBUG_ERROR("failed in create_location");
194                 goto error;
195         }
196
197         errorType = create_location(targetUrl, NULL, &pTargetLocation);
198         if (errorType != SA_INTERNAL_OK) {
199                 _DEBUG_ERROR("failed in create_location");
200                 goto error;
201         }
202
203         /* create session info */
204         errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, account_id, session_id, pSourceLocation, pTargetLocation, session);
205         if (errorType != SA_INTERNAL_OK) {
206                 _DEBUG_ERROR("failed to create session");
207                 goto error;
208         }
209
210         if (pTempChal != NULL) {
211                 (*session)->chal = pTempChal;
212                 pTempChal = NULL;
213         }
214
215         (*session)->naci_session_id = sessionID;
216
217  error:
218
219         if (id != NULL)
220                 free(id);
221         if (pwd != NULL)
222                 free(pwd);
223         if (targetUrl != NULL)
224                 free(targetUrl);
225         if (sourceUrl != NULL)
226                 free(sourceUrl);
227         if (nextNonce != NULL)
228                 free(nextNonce);
229         if (pTempChal != NULL)
230                 free_chal(pTempChal);
231
232         _INNER_FUNC_EXIT;
233         return errorType;
234 }
235
236 static sa_error_type_e _generate_presync_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size)
237 {
238         _INNER_FUNC_ENTER;
239
240         sa_error_type_e errorType = SA_INTERNAL_OK;
241         char *id = NULL;
242         char *pwd = NULL;
243         char *targetUrl = NULL;
244         char *nextNonce = NULL;
245         char *credData = NULL;
246         char *decoded_nonce = NULL;
247         bool resume = false;
248         char *value = NULL;
249         alert_type_e alert_type = ALERT_UNKNOWN;
250
251         /*pkg1  always has final tag */
252         int isFinal = 1;
253
254         sync_hdr_s *pSyncHdr = NULL;
255         cred_s *pCred = NULL;
256         devinf_s *devInfObj = NULL;
257         syncml_s *syncml = NULL;
258
259         /*case SAN, MUST use sessionid from SAN message.. */
260         if (session->session_id == NULL) {
261                 session->session_id = g_strdup_printf("%ld", time(NULL));
262                 if (session->session_id == NULL) {
263                         errorType = SA_INTERNAL_NO_MEMORY;
264                         goto error;
265                 }
266         }
267
268         errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce);
269         if (errorType != SA_INTERNAL_OK)
270                 goto error;
271
272         _DEBUG_TRACE("id = %s", id);
273         _DEBUG_TRACE("pwd = %s", pwd);
274         _DEBUG_TRACE("sessionId = %s", session->session_id);
275         _DEBUG_TRACE("serverUrl = %s", targetUrl);
276
277         /* set pkg status */
278         session->pkg_status = SYNCML_PKG_1;
279
280         chal_s *chal = session->chal;
281         auth_type_e authType;
282         format_type_e formatType = FORMAT_TYPE_UNKNOWN;
283         unsigned int nonce_size = 0;
284         if (chal == NULL) {
285                 _DEBUG_TRACE("CHAL DOES NOT EXIST");
286                 if (nextNonce != NULL) {
287                         /*server sent nonce info in previous sync session */
288                         authType = AUTH_TYPE_MD5;
289                         decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
290                         formatType = FORMAT_TYPE_BASE64;
291                 } else {
292                         authType = AUTH_TYPE_BASIC;
293                         formatType = FORMAT_TYPE_BASE64;
294                 }
295
296                 errorType = create_cred_string(authType, id, pwd, decoded_nonce, nonce_size, &credData);
297
298                 if (errorType != SA_INTERNAL_OK) {
299                         _DEBUG_ERROR("failed to create cred string");
300                         goto error;
301                 }
302
303                 errorType = create_cred(id, pwd, authType, formatType, credData, &pCred);
304         } else {
305                 _DEBUG_TRACE("CHAL DOES  EXIST");
306                 if (chal->type == AUTH_TYPE_MD5) {
307                         if (chal->format == FORMAT_TYPE_BASE64)
308                                 decoded_nonce = (char *)g_base64_decode(chal->nonce_b64, &nonce_size);
309                         else {
310                                 if (chal->nonce_plain != NULL) {
311                                         decoded_nonce = strdup(chal->nonce_plain);
312                                         nonce_size = chal->nonce_length;
313                                 }
314                         }
315                 }
316
317                 errorType = create_cred_string(chal->type, id, pwd, decoded_nonce, nonce_size, &credData);
318                 if (errorType != SA_INTERNAL_OK) {
319                         _DEBUG_ERROR("failed to create cred string");
320                         goto error;
321                 }
322
323                 errorType = create_cred(id, pwd, chal->type, FORMAT_TYPE_BASE64, credData, &pCred);
324         }
325
326         if (credData != NULL) {
327                 free(credData);
328                 credData = NULL;
329         }
330
331         if (decoded_nonce != NULL) {
332                 free(decoded_nonce);
333                 decoded_nonce = NULL;
334         }
335
336         if (nextNonce != NULL) {
337                 free(nextNonce);
338                 nextNonce = NULL;
339         }
340         nonce_size = 0;
341
342         if (errorType != SA_INTERNAL_OK) {
343                 _DEBUG_ERROR("failed to create cred");
344                 goto error;
345         }
346
347         set_session_cred(session, pCred);
348         pCred = NULL;
349
350         /* create SyncHdr */
351         errorType = create_sync_hdr(session, &pSyncHdr);
352         if (errorType != SA_INTERNAL_OK) {
353                 _DEBUG_ERROR("failed to create synchdr");
354                 goto error;
355         }
356
357         /* create devInf */
358         errorType = create_devinf(session, &devInfObj);
359         if (errorType != SA_INTERNAL_OK) {
360                 _DEBUG_ERROR("failed to create devInf");
361                 goto error;
362         }
363
364         set_session_devinf(session, devInfObj);
365
366         errorType = __check_resume_session(session, &resume);
367         if (errorType != SA_INTERNAL_OK) {
368                 _DEBUG_ERROR("failed in __check_resume_session");
369                 goto error;
370         }
371
372         GList *commands = NULL;
373         GList *commands_last = NULL;
374
375         char *sourceDevInf = NULL;
376         char *contentType = NULL;
377
378         if (session->protocol_version == VERSION_10)
379                 sourceDevInf = ELEMENT_DEVINF_10;
380         else if (session->protocol_version == VERSION_11)
381                 sourceDevInf = ELEMENT_DEVINF_11;
382         else if (session->protocol_version == VERSION_12)
383                 sourceDevInf = ELEMENT_DEVINF_12;
384
385         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
386                 contentType = ELEMENT_DEVINF_XML;
387         else
388                 contentType = ELEMENT_DEVINF_WBXML;
389
390         if (resume != true) {
391                 if (server_flag != true) {
392                         /* create Put command */
393                         command_s *pPutCommand = NULL;
394                         location_s *pLocation = NULL;
395
396                         errorType = create_location(sourceDevInf, NULL, &pLocation);
397                         if (errorType != SA_INTERNAL_OK)
398                                 goto error;
399
400                         errorType = create_put_command(session, pLocation, contentType, devInfObj, &pPutCommand);
401                         if (errorType != SA_INTERNAL_OK)
402                                 goto error;
403                         put_into_list(&commands, &commands_last, pPutCommand);
404                 }
405         }
406
407         /* if it is not dive server */
408         if (server_flag != true) {
409                 /* create Get command */
410                 command_s *pGetCommand = NULL;
411                 location_s *location = NULL;
412                 errorType = create_location(sourceDevInf, NULL, &location);
413                 if (errorType != SA_INTERNAL_OK) {
414                         _DEBUG_ERROR("failed to create location");
415                         goto error;
416                 }
417
418                 errorType = create_get_command(session, location, contentType, &pGetCommand);
419                 if (errorType != SA_INTERNAL_OK) {
420                         _DEBUG_ERROR("failed to create get command");
421                         goto error;
422                 }
423                 put_into_list(&commands, &commands_last, pGetCommand);
424         }
425
426         command_s *pAlertCommand = NULL;
427         location_s *pSourceLocation = NULL;
428         location_s *pTargetLocation = NULL;
429         int content_type;
430         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
431                 if (datastoreinfo_per_content_type[content_type] != NULL) {
432                         if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
433                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
434                                 if (errorType != SA_INTERNAL_OK) {
435                                         _DEBUG_ERROR("failed to create location");
436
437                                         /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
438                                            when SA_INTERNAL_NOT_DEFINED error case */
439                                         if (errorType == SA_INTERNAL_NOT_DEFINED)
440                                                 errorType = SA_INTERNAL_MISCONFIGURATION;
441
442                                         goto error;
443                                 }
444
445                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
446                                 if (errorType != SA_INTERNAL_OK) {
447                                         _DEBUG_ERROR("failed to create location");
448
449                                         /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
450                                            when SA_INTERNAL_NOT_DEFINED error case */
451                                         if (errorType == SA_INTERNAL_NOT_DEFINED)
452                                                 errorType = SA_INTERNAL_MISCONFIGURATION;
453                                         goto error;
454                                 }
455
456                                 _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->id = %d", content_type, datastoreinfo_per_content_type[content_type]->id);
457                                 _DEBUG_TRACE("datastoreinfo_per_content_type[%d]->pw = %d", content_type, datastoreinfo_per_content_type[content_type]->pw);
458
459                                 if (datastoreinfo_per_content_type[content_type]->id != NULL && datastoreinfo_per_content_type[content_type]->pw != NULL) {
460
461                                         /*FIXME is there a nextNonce for datastore ?? */
462                                         if (nextNonce != NULL) {
463                                                 /*server sent nonce info in previous sync session */
464                                                 authType = AUTH_TYPE_MD5;
465                                                 decoded_nonce = (char *)g_base64_decode(nextNonce, &nonce_size);
466                                         } else {
467                                                 authType = AUTH_TYPE_BASIC;
468                                                 formatType = FORMAT_TYPE_BASE64;
469                                         }
470
471                                         errorType = create_cred_string(authType, datastoreinfo_per_content_type[content_type]->id, datastoreinfo_per_content_type[content_type]->pw, decoded_nonce, nonce_size, &credData);
472                                         if (errorType != SA_INTERNAL_OK) {
473                                                 _DEBUG_ERROR("failed to create cred string");
474                                                 goto error;
475                                         }
476
477                                         errorType = create_cred(id, pwd, authType, FORMAT_TYPE_BASE64, credData, &pCred);
478
479                                         if (credData != NULL) {
480                                                 free(credData);
481                                                 credData = NULL;
482                                         }
483                                 }
484
485                                 if (resume == true)
486                                         alert_type = ALERT_RESUME;
487                                 else
488                                         alert_type = datastoreinfo_per_content_type[content_type]->client_sync_type;
489
490                                 errorType = create_alert_command(session, alert_type,
491                                                                  pSourceLocation, pTargetLocation, datastoreinfo_per_content_type[content_type]->last_anchor_client, datastoreinfo_per_content_type[content_type]->next_anchor_client, pCred, &pAlertCommand);
492                                 if (errorType != SA_INTERNAL_OK) {
493                                         _DEBUG_ERROR("failed to create alert command");
494                                         goto error;
495                                 }
496                         } else
497                                 continue;
498                         put_into_list(&commands, &commands_last, pAlertCommand);
499                 }
500         }
501
502         errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml);
503         if (errorType != SA_INTERNAL_OK) {
504                 _DEBUG_ERROR("failed to create syncml");
505                 goto error;
506         }
507
508         session->is_sending_final = isFinal;
509         if (isFinal)
510                 session->pkg_status = SYNCML_PKG_2;
511
512         /* convert Msg */
513         errorType = __object_binder(syncml, server_flag, msg, msg_size);
514         if (errorType != SA_INTERNAL_OK) {
515                 _DEBUG_ERROR("failed in objectBinder");
516                 goto error;
517         }
518
519         /*free generate structure */
520         free_syncml(syncml);
521
522         if (id != NULL)
523                 free(id);
524         if (pwd != NULL)
525                 free(pwd);
526         if (targetUrl != NULL)
527                 free(targetUrl);
528         if (nextNonce != NULL)
529                 free(nextNonce);
530         if (value != NULL)
531                 free(value);
532         if (pCred != NULL)
533                 free_cred(pCred);
534
535         _INNER_FUNC_EXIT;
536         return errorType;
537
538  error:
539
540         if (credData != NULL)
541                 free(credData);
542
543         if (id != NULL)
544                 free(id);
545         if (pwd != NULL)
546                 free(pwd);
547         if (targetUrl != NULL)
548                 free(targetUrl);
549         if (nextNonce != NULL)
550                 free(nextNonce);
551         if (value != NULL)
552                 free(value);
553         if (syncml != NULL)
554                 free_syncml(syncml);
555         if (pSyncHdr != NULL)
556                 free_sync_hdr(pSyncHdr);
557         if (pCred != NULL)
558                 free_cred(pCred);
559
560         _INNER_FUNC_EXIT;
561         return errorType;
562 }
563
564 static sa_error_type_e _generate_msg(session_s * session, void **sync_obj, bool server_flag, char **msg, unsigned int *msg_size)
565 {
566         _INNER_FUNC_ENTER;
567
568         sa_error_type_e errorType = SA_INTERNAL_OK;
569         bool isNeedNextMsg = false;
570
571         sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
572         sync_agent_pb_protocol_binder_info_s *binder = NULL;
573
574         sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
575
576         if (server_flag != true)
577                 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set);
578         else
579                 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set);
580
581         /* create SyncHdr */
582         sync_hdr_s *pSyncHdr = NULL;
583         errorType = create_sync_hdr(session, &pSyncHdr);
584         if (errorType != SA_INTERNAL_OK) {
585                 _DEBUG_ERROR("failed to create synchdr");
586                 goto error;
587         }
588
589         err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL);
590         err = oma_ds_1_2_binder_append(binder, PE_HEADER, pSyncHdr);
591         err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL);
592
593         sync_obj_s **sync = (sync_obj_s **) sync_obj;
594         GList *commands = NULL;
595         GList *commands_last = NULL;
596         int isFinal = 0;
597
598         errorType = __generate_msg_status_map_command(&session, sync, &commands, &commands_last);
599         if (errorType != SA_INTERNAL_OK) {
600                 _DEBUG_ERROR("failed in __generate_msg_status_map_command");
601                 goto error;
602         }
603
604         /*append status command */
605         GList *status_iter = NULL;
606         status_s *pStatus = NULL;
607         for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
608                 pStatus = (status_s *) status_iter->data;
609                 err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus);
610         }
611
612         /*append results command */
613         GList *results_iter = NULL;
614         command_s *pResultsCommand = NULL;
615         for (results_iter = session->results_command; results_iter != NULL; results_iter = g_list_next(results_iter)) {
616                 pResultsCommand = (command_s *) results_iter->data;
617                 put_into_list(&commands, &commands_last, pResultsCommand);
618                 err = oma_ds_1_2_binder_append(binder, PE_RESULTS_START, pResultsCommand);
619
620                 if (pResultsCommand->type == COMMAND_TYPE_RESULTS) {
621                         if (pResultsCommand->private.results.item != NULL) {
622                                 if (pResultsCommand->private.results.item->data_type == ITEM_DEVINF) {
623                                         err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pResultsCommand->private.results.item);
624                                 }
625                         }
626
627                         err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL);
628                 }
629         }
630
631         /*append map command */
632         GList *map_iter = NULL;
633         command_s *pMapCommand = NULL;
634         for (map_iter = session->map_command; map_iter != NULL; map_iter = g_list_next(map_iter)) {
635                 pMapCommand = (command_s *) map_iter->data;
636                 err = oma_ds_1_2_binder_append(binder, PE_MAP, pMapCommand);
637         }
638
639         /* free sendgStatus structure from SE */
640         free_sending_statuses((*sync)->sending_status);
641         (*sync)->sending_status = NULL;
642
643         if (session->pkg_status == SYNCML_PKG_3) {
644
645                 errorType = __generate_msg_exist_sending_map_command(binder, &session, &commands, &commands_last);
646                 if (errorType != SA_INTERNAL_OK) {
647                         _DEBUG_ERROR("failed in __generate_msg_status_map_command");
648                         goto error;
649                 }
650
651                 errorType = __generate_msg_changes_command(binder, &session, sync, &commands, &commands_last, &isNeedNextMsg);
652                 if (errorType != SA_INTERNAL_OK) {
653                         _DEBUG_ERROR("failed in __generate_msg_status_map_command");
654                         goto error;
655                 }
656
657                 if (!isNeedNextMsg)
658                         isFinal = 1;
659         }
660
661         if (session->pkg_status == SYNCML_PKG_5 && session->is_receiving_final)
662                 isFinal = 1;
663
664         /*if we have to send alert command(222,223 etc) */
665         GList *alert_iter = NULL;
666         command_s *pAlertCommand = NULL;
667         for (alert_iter = session->alert_command; alert_iter != NULL; alert_iter = g_list_next(alert_iter)) {
668                 pAlertCommand = alert_iter->data;
669                 put_into_list(&commands, &commands_last, pAlertCommand);
670                 err = oma_ds_1_2_binder_append(binder, PE_ALERT, pAlertCommand);
671         }
672
673         if (isFinal)
674                 err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL);
675
676         session->is_sending_final = isFinal;
677
678         err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL);
679         err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL);
680
681         oma_ds_1_2_binder_get_stream(binder, msg, msg_size);
682         _DEBUG_TRACE("final msg size = %d", *msg_size);
683
684         /*FIXME for debugging */
685         char *xml = NULL;
686         unsigned int xml_len = 0;
687         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
688                 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
689         else {
690                 err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len);
691                 if (err == SYNC_AGENT_PB_RETURN_OK) {
692                         set_xml_to_file(xml, OMA_DS_MSG_PATH);
693
694                         if (xml != NULL) {
695                                 free(xml);
696                                 xml = NULL;
697                         }
698                 }
699         }
700
701         oma_ds_1_2_binder_terminate(binder);
702
703         if (session->pkg_status == SYNCML_PKG_3 && session->is_sending_final)
704                 session->pkg_status = SYNCML_PKG_4;
705         else if (session->pkg_status == SYNCML_PKG_5 && session->is_sending_final)
706                 session->pkg_status = SYNCML_PKG_6;
707
708         free_sync_hdr(pSyncHdr);
709         if (session->status != NULL) {
710                 if (session->suspend_status != NULL) {
711                         free_statuses(session->suspend_status);
712                         session->suspend_status = NULL;
713                 }
714                 session->suspend_status = static_session->status;
715                 session->status = NULL;
716         }
717         free_commands(commands);
718
719         session->alert_command = NULL;
720         session->results_command = NULL;
721
722         _INNER_FUNC_EXIT;
723         return errorType;
724
725  error:
726
727         free_sync_hdr(pSyncHdr);
728         if (session->status != NULL) {
729                 free_statuses(session->status);
730                 session->status = NULL;
731         }
732         free_commands(commands);
733
734         _INNER_FUNC_EXIT;
735         return errorType;
736 }
737
738 static sa_error_type_e __generate_msg_status_map_command(session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last)
739 {
740         _INNER_FUNC_ENTER;
741
742         sa_error_type_e errorType = SA_INTERNAL_OK;
743         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
744
745         GList *iter = NULL;
746         sending_status_s *sendingStatus = NULL;
747         /* send status of sync, add, replace, delete command */
748         for (iter = (*sync)->sending_status; iter != NULL; iter = g_list_next(iter)) {
749                 sendingStatus = (iter->data);
750
751                 int needMap = 0;
752                 GList *sendingIter = NULL;
753                 applied_status_s *appliedStatus = NULL;
754                 for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
755                         appliedStatus = (sendingIter->data);
756
757                         /* set data in status of add, replace, delete command */
758                         GList *statusIter = NULL;
759                         status_s *status = NULL;
760                         for (statusIter = (*session)->temp_status; statusIter != NULL; statusIter = g_list_next(statusIter)) {
761                                 status = statusIter->data;
762
763                                 if (status->type == COMMAND_TYPE_ADD || status->type == COMMAND_TYPE_REPLACE || status->type == COMMAND_TYPE_DELETE) {
764
765                                         /* if there is a status for add command it have to be checked
766                                            because map command has to be generated
767                                            otherwise replace, delete command doesnot need to generate map command */
768                                         if (status->type == COMMAND_TYPE_ADD)
769                                                 needMap = 1;
770
771                                         if (status->source_ref != NULL) {
772                                                 /*add command */
773                                                 sync_agent_da_mapping_s *mapping = NULL;
774                                                 da_err = sync_agent_create_mapping(&mapping);
775                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
776                                                         _DEBUG_ERROR("failed in sync_agent_create_mapping !!");
777                                                         errorType = SA_INTERNAL_DA_ERROR;
778                                                         goto error;
779                                                 }
780
781                                                 sync_agent_da_get_mapping_query_s query;
782                                                 query.account_id = (*session)->account_id;
783                                                 query.luid = appliedStatus->luid;
784
785                                                 da_err = sync_agent_get_mapping(&query, &mapping);
786                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
787                                                         _DEBUG_ERROR("failed in sync_agent_get_mapping !!");
788                                                         errorType = SA_INTERNAL_DA_ERROR;
789                                                         sync_agent_free_mapping(mapping);
790                                                         goto error;
791                                                 }
792
793                                                 if (mapping != NULL) {
794                                                         if (mapping->guid == NULL) {
795                                                                 errorType = SA_INTERNAL_DA_ERROR;
796                                                                 _DEBUG_ERROR("failed to get guid");
797                                                                 sync_agent_free_mapping(mapping);
798                                                                 goto error;
799                                                         }
800
801                                                         if (strcmp(mapping->guid, get_location_loc_uri(status->source_ref)) == 0) {
802                                                                 if (status->data != NULL) {
803                                                                         free(status->data);
804                                                                         status->data = NULL;
805                                                                 }
806                                                                 status->data = g_strdup_printf("%d", appliedStatus->status);
807                                                                 /*move to status list from tempStatus */
808                                                                 (*session)->status = g_list_append((*session)->status, status);
809                                                                 (*session)->temp_status = g_list_remove((*session)->temp_status, status);
810
811                                                                 sync_agent_free_mapping(mapping);
812
813                                                                 break;
814                                                         }
815
816                                                         sync_agent_free_mapping(mapping);
817
818                                                 } else {
819                                                         _DEBUG_ERROR("mapping is NULL !!");
820                                                         errorType = SA_INTERNAL_DA_ERROR;
821                                                         sync_agent_free_mapping(mapping);
822                                                         goto error;
823                                                 }
824                                         }
825
826                                         if (status->target_ref != NULL) {
827                                                 /*replace, delete */
828                                                 if (strcmp(appliedStatus->luid, get_location_loc_uri(status->target_ref)) == 0) {
829                                                         if (status->data != NULL) {
830                                                                 free(status->data);
831                                                                 status->data = NULL;
832                                                         }
833                                                         status->data = g_strdup_printf("%d", appliedStatus->status);
834                                                         /*move to status list from tempStatus */
835                                                         (*session)->status = g_list_append((*session)->status, status);
836                                                         (*session)->temp_status = g_list_remove((*session)->temp_status, status);
837                                                         break;
838                                                 }
839                                         }
840                                 }
841                         }
842                 }
843
844                 if (needMap) {
845                         /* create map command */
846                         command_s *mapCommand = NULL;
847                         location_s *pSourceLocation = NULL;
848                         location_s *pTargetLocation = NULL;
849                         errorType = create_location(sendingStatus->source, NULL, &pSourceLocation);
850                         if (errorType != SA_INTERNAL_OK) {
851                                 _DEBUG_ERROR("failed to create location");
852                                 goto error;
853                         }
854
855                         errorType = create_location(sendingStatus->target, NULL, &pTargetLocation);
856                         if (errorType != SA_INTERNAL_OK) {
857                                 _DEBUG_ERROR("failed to create location");
858                                 goto error;
859                         }
860
861                         errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand);
862                         if (errorType != SA_INTERNAL_OK) {
863                                 _DEBUG_ERROR("failed to create map command");
864                                 goto error;
865                         }
866
867                         GList *sendingIter = NULL;
868                         applied_status_s *appliedStatus = NULL;
869
870                         for (sendingIter = sendingStatus->items; sendingIter != NULL; sendingIter = g_list_next(sendingIter)) {
871                                 appliedStatus = (sendingIter->data);
872
873                                 if (appliedStatus->change_type == CHANGE_ADD) {
874
875                                         if (appliedStatus->status == ITEM_ADDED) {
876                                                 item_s *temp = create_item();
877                                                 if (temp == NULL) {
878                                                         _DEBUG_ERROR("failed to create item");
879                                                         errorType = SA_INTERNAL_NO_MEMORY;
880                                                         goto error;
881                                                 }
882
883                                                 errorType = create_location(appliedStatus->luid, NULL, &pSourceLocation);
884                                                 if (errorType != SA_INTERNAL_OK) {
885                                                         _DEBUG_ERROR("failed to create location");
886                                                         goto error;
887                                                 }
888
889                                                 sync_agent_da_mapping_s *mapping = NULL;
890                                                 da_err = sync_agent_create_mapping(&mapping);
891                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
892                                                         _DEBUG_ERROR("failed in sync_agent_create_mapping !!");
893                                                         errorType = SA_INTERNAL_DA_ERROR;
894                                                         goto error;
895                                                 }
896
897                                                 sync_agent_da_get_mapping_query_s query;
898                                                 query.account_id = (*session)->account_id;
899                                                 query.luid = appliedStatus->luid;
900
901                                                 da_err = sync_agent_get_mapping(&query, &mapping);
902                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
903                                                         _DEBUG_ERROR("failed in sync_agent_get_mapping !!");
904                                                         errorType = SA_INTERNAL_DA_ERROR;
905                                                         goto error;
906                                                 }
907
908                                                 if (mapping != NULL) {
909                                                         if (mapping->guid == NULL) {
910                                                                 errorType = SA_INTERNAL_DA_ERROR;
911                                                                 _DEBUG_ERROR("failed to get guid");
912                                                                 goto error;
913                                                         }
914                                                         errorType = create_location(mapping->guid, NULL, &pTargetLocation);
915                                                         if (errorType != SA_INTERNAL_OK) {
916                                                                 _DEBUG_ERROR("failed to create location");
917                                                                 goto error;
918                                                         }
919
920                                                         if (mapping->guid != NULL) {
921                                                                 free(mapping->guid);
922                                                                 mapping->guid = NULL;
923                                                         }
924                                                 } else {
925                                                         _DEBUG_ERROR("mapping is NULL !!");
926                                                         errorType = SA_INTERNAL_DA_ERROR;
927                                                         goto error;
928                                                 }
929
930                                                 set_item_source(temp, pSourceLocation);
931                                                 set_item_target(temp, pTargetLocation);
932                                                 set_map_command_item(mapCommand, temp);
933                                         } else {
934                                                 sync_agent_da_delete_mapping_query_s query;
935                                                 query.option = SYNC_AGENT_DA_DELETE_MAPPING_OPTION_LUID;
936                                                 query.account_id = (*session)->account_id;
937                                                 query.luid = appliedStatus->luid;
938
939                                                 da_err = sync_agent_delete_mapping(&query);
940                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
941                                                         errorType = SA_INTERNAL_DA_ERROR;
942                                                         _DEBUG_ERROR("failed in sync_agent_delete_mapping !!");
943                                                         goto error;
944                                                 }
945                                         }
946                                 }
947                         }
948                         put_into_list(commands, commands_last, mapCommand);
949                         (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
950                         increase_command_ref_count(mapCommand);
951                 }
952         }
953
954  error:
955
956         _INNER_FUNC_EXIT;
957         return errorType;
958 }
959
960 static sa_error_type_e __generate_msg_exist_sending_map_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, GList ** commands, GList ** commands_last)
961 {
962         _INNER_FUNC_ENTER;
963
964         sa_error_type_e errorType = SA_INTERNAL_OK;
965         sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
966         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
967
968         /*if there is something in the mapping_tbl it have to be sent before sync command */
969         int existSendingMap = sync_agent_is_exist_mapping_by_account_id((*session)->account_id);
970
971         if (existSendingMap) {
972                 GList *mapping_list = NULL;
973                 sync_agent_da_get_mapping_list_query_s query;
974                 query.option = SYNC_AGENT_DA_GET_MAPPING_LIST_OPTION_ACCOUNT_ID;
975                 query.account_id = (*session)->account_id;
976
977                 ret = sync_agent_get_mapping_list(&query, &mapping_list);
978                 if (ret != SYNC_AGENT_DA_SUCCESS) {
979                         _DEBUG_ERROR("failed in sync_agent_get_mapping_list !!");
980                         errorType = SA_INTERNAL_ERROR;
981                         goto error;
982                 }
983
984                 location_s *pSourceLocation = NULL;
985                 location_s *pTargetLocation = NULL;
986
987                 bool exist = false;
988                 int content_type;
989                 sync_agent_da_mapping_s *mapping_data = (sync_agent_da_mapping_s *) ((GList *) (g_list_nth(mapping_list, g_list_length(mapping_list) - 1))->data);
990                 int lastItemTypeId = mapping_data->data_store_id;
991
992                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
993                         if (datastoreinfo_per_content_type[content_type] != NULL) {
994                                 _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", content_type, datastoreinfo_per_content_type[content_type]->datastore_id);
995                                 if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
996                                         if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) {
997                                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
998                                                 if (errorType != SA_INTERNAL_OK) {
999                                                         _DEBUG_ERROR("failed to create location");
1000                                                         goto error;
1001                                                 }
1002
1003                                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
1004                                                 if (errorType != SA_INTERNAL_OK) {
1005                                                         _DEBUG_ERROR("failed to create location");
1006                                                         goto error;
1007                                                 }
1008
1009                                                 exist = true;
1010                                                 break;
1011                                         }
1012                                 }
1013                         }
1014                 }
1015
1016                 if (exist == false) {
1017                         _DEBUG_VERBOSE("Mapping data is not equal with any synchronizing datastore id");
1018                         goto error;
1019                 }
1020
1021                 /* create map command using first item */
1022                 command_s *mapCommand = NULL;
1023                 errorType = create_map_command(*session, pSourceLocation, pTargetLocation, &mapCommand);
1024                 if (errorType != SA_INTERNAL_OK) {
1025                         _DEBUG_ERROR("failed to create map command");
1026                         goto error;
1027                 }
1028
1029                 GList *iter = NULL;
1030                 sync_agent_da_mapping_s *iter_data;
1031                 for (iter = mapping_list; iter != NULL; iter = g_list_next(iter)) {
1032                         iter_data = NULL;
1033                         iter_data = (sync_agent_da_mapping_s *) (iter->data);
1034
1035                         if (lastItemTypeId != iter_data->data_store_id) {
1036                                 err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand);
1037                                 put_into_list(commands, commands_last, mapCommand);
1038                                 (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
1039                                 increase_command_ref_count(mapCommand);
1040
1041                                 bool exist = false;
1042                                 int content_type;
1043                                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1044                                         if (datastoreinfo_per_content_type[content_type] != NULL) {
1045                                                 if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
1046                                                         if (lastItemTypeId == datastoreinfo_per_content_type[content_type]->datastore_id) {
1047                                                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->source, NULL, &pSourceLocation);
1048                                                                 if (errorType != SA_INTERNAL_OK) {
1049                                                                         _DEBUG_ERROR("failed to create location");
1050                                                                         goto error;
1051                                                                 }
1052
1053                                                                 errorType = create_location(datastoreinfo_per_content_type[content_type]->target, NULL, &pTargetLocation);
1054                                                                 if (errorType != SA_INTERNAL_OK) {
1055                                                                         _DEBUG_ERROR("failed to create location");
1056                                                                         goto error;
1057                                                                 }
1058
1059                                                                 exist = true;
1060                                                                 break;
1061                                                         }
1062                                                 }
1063                                         }
1064                                 }
1065                                 if (exist == false) {
1066                                         _DEBUG_ERROR("Mapping data is not equal with any synchronizing datastore id");
1067                                         goto error;
1068                                 }
1069                         }
1070
1071                         lastItemTypeId = iter_data->data_store_id;
1072                         item_s *temp = create_item();
1073                         if (temp == NULL) {
1074                                 _DEBUG_ERROR("failed to create item");
1075                                 errorType = SA_INTERNAL_NO_MEMORY;
1076                                 goto error;
1077                         }
1078
1079                         errorType = create_location(iter_data->luid, NULL, &pSourceLocation);
1080                         if (errorType != SA_INTERNAL_OK) {
1081                                 _DEBUG_ERROR("failed to create location");
1082                                 goto error;
1083                         }
1084
1085                         errorType = create_location(iter_data->guid, NULL, &pTargetLocation);
1086                         if (errorType != SA_INTERNAL_OK) {
1087                                 _DEBUG_ERROR("failed to create location");
1088                                 goto error;
1089                         }
1090
1091                         set_item_source(temp, pSourceLocation);
1092                         set_item_target(temp, pTargetLocation);
1093                         set_map_command_item(mapCommand, temp);
1094                 }
1095
1096                 sync_agent_free_mapping_list(mapping_list);
1097
1098                 err = oma_ds_1_2_binder_append(binder, PE_MAP, mapCommand);
1099                 put_into_list(commands, commands_last, mapCommand);
1100                 (*session)->map_command = g_list_append((*session)->map_command, mapCommand);
1101                 increase_command_ref_count(mapCommand);
1102         }
1103
1104  error:
1105
1106         _INNER_FUNC_EXIT;
1107         return errorType;
1108
1109 }
1110
1111 static sa_error_type_e __generate_msg_changes_command(sync_agent_pb_protocol_binder_info_s * binder, session_s ** session, sync_obj_s ** sync, GList ** commands, GList ** commands_last, bool * is_need_next_msg)
1112 {
1113         _INNER_FUNC_ENTER;
1114
1115         sa_error_type_e errorType = SA_INTERNAL_OK;
1116         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
1117         sync_agent_da_service_item_s *service_item = NULL;
1118         sync_agent_pb_error_e err = SYNC_AGENT_PB_RETURN_OK;
1119         unsigned int stream_size = 0;
1120
1121         char *data = NULL;      /*have to be freed */
1122         char *sendingData = NULL;
1123
1124         /* generate sync , add, replace, delete command when there is a item in ChangedDatastore */
1125         GList *iter = NULL;
1126         command_s *syncCommand = NULL;
1127         changed_datastore_s *changedDatastore = NULL;
1128         bool needNextMsg = false;
1129         for (iter = (*sync)->changed_datastore; iter != NULL; iter = g_list_next(iter)) {
1130                 changedDatastore = (iter->data);
1131
1132                 if (changedDatastore->need_sync_command) {
1133                         /* create sync Command */
1134                         location_s *pSourceLocation = NULL;
1135                         location_s *pTargetLocation = NULL;
1136                         errorType = create_location(changedDatastore->source, NULL, &pSourceLocation);
1137                         if (errorType != SA_INTERNAL_OK) {
1138                                 _DEBUG_ERROR("failed to create location");
1139                                 goto error;
1140                         }
1141
1142                         errorType = create_location(changedDatastore->target, NULL, &pTargetLocation);
1143                         if (errorType != SA_INTERNAL_OK) {
1144                                 _DEBUG_ERROR("failed to create location");
1145                                 goto error;
1146                         }
1147
1148                         errorType = create_sync_start_command(*session, pSourceLocation, pTargetLocation, &syncCommand);
1149                         if (errorType != SA_INTERNAL_OK) {
1150                                 _DEBUG_ERROR("failed to create sync start command");
1151                                 goto error;
1152                         }
1153
1154                         /*set numberOfChanges */
1155                         if (changedDatastore->has_number_of_changes) {
1156                                 /*if it is first sync command set numberOfChanges */
1157                                 set_sync_start_command_number_of_changes(syncCommand, changedDatastore->number_of_changes);
1158                                 changedDatastore->has_number_of_changes = 0;
1159                         }
1160
1161                         /*set Mem */
1162                         /* TODO get current datastore dynamic memory size & recods */
1163                         /* And set Mem structure */
1164                         /*Mem *mem = create_mem();
1165                            set_mem_sharedmem(mem, 1);
1166                            set_mem_freemem(mem, 8100);
1167                            set_mem_freeid(mem, 81);
1168                            set_sync_start_command_mem(syncCommand, mem); */
1169
1170                         err = oma_ds_1_2_binder_append(binder, PE_SYNC_START, syncCommand);
1171                         put_into_list(commands, commands_last, syncCommand);
1172
1173                         GList *itemIter = NULL;
1174                         command_s *changeCommand = NULL;
1175                         changed_item_s *changedItem = NULL;
1176                         oma_ds_protocol_element_e protocol_element = PE_UNDEF;
1177                         for (itemIter = changedDatastore->change_item; itemIter != NULL;) {
1178                                 changedItem = (itemIter->data);
1179
1180                                 oma_ds_1_2_binder_get_stream_size(binder, &stream_size);
1181                                 _DEBUG_VERBOSE("stream_size = %d", stream_size);
1182                                 _DEBUG_VERBOSE("(*session)->targetMaxMsgSize = %d", (*session)->target_max_msg_size);
1183
1184                                 if ((*session)->target_max_msg_size > stream_size) {
1185                                         /*there is a space for command */
1186                                         if (changedItem->change_type == CHANGE_DELETE) {
1187                                                 /* create delete Command */
1188                                                 errorType = create_delete_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, &changeCommand);
1189                                                 if (errorType != SA_INTERNAL_OK) {
1190                                                         _DEBUG_ERROR("failed to create delete command");
1191                                                         goto error;
1192                                                 }
1193                                         } else {
1194                                                 int isFirstLargeObj;
1195                                                 if (changedItem->data == NULL) {
1196                                                         char *folderId = NULL;
1197                                                         GList *list = NULL;
1198                                                         sync_agent_da_get_folder_id_list_query_s query;
1199                                                         query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID;
1200                                                         query.account_id = (*session)->account_id;
1201                                                         query.item_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id;
1202                                                         query.folder_type_id = datastoreinfo_per_content_type[changedItem->index_of_datastore]->folder_type_id;
1203
1204                                                         da_err = sync_agent_get_folder_id_list(&query, &list);
1205                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1206                                                                 _DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!");
1207                                                                 goto error;
1208                                                         }
1209
1210                                                         if (g_list_length(list) > 0) {
1211                                                                 GList *iter = g_list_nth(list, 0);
1212                                                                 folderId = (char *)(iter->data);
1213                                                         } else {
1214                                                                 _DEBUG_ERROR("failed to get folderId");
1215                                                                 errorType = SA_INTERNAL_DA_ERROR;
1216                                                                 goto error;
1217                                                         }
1218
1219                                                         _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->datastore_id = %d", changedItem->index_of_datastore, datastoreinfo_per_content_type[changedItem->index_of_datastore]->datastore_id);
1220                                                         _DEBUG_VERBOSE("account_id = %d", (*session)->account_id);
1221                                                         _DEBUG_VERBOSE("folderId = %s", folderId);
1222                                                         _DEBUG_VERBOSE("changedItem->luid = %s", changedItem->luid);
1223
1224                                                         da_err = sync_agent_create_service_item(&service_item);
1225                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1226                                                                 errorType = SA_INTERNAL_DA_ERROR;
1227                                                                 _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
1228                                                                 goto error;
1229                                                         }
1230
1231                                                         da_err = sync_agent_get_service_item(changedItem->luid, &service_item);
1232                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1233                                                                 errorType = SA_INTERNAL_DA_ERROR;
1234                                                                 _DEBUG_ERROR("failed in sync_agent_get_service_item() = %d", da_err);
1235                                                                 goto error;
1236                                                         }
1237                                                         data = g_strdup((char *)(service_item->data));
1238
1239                                                         _DEBUG_VERBOSE("data = %s", data);
1240                                                         sync_agent_free_folder_id_list(list);
1241                                                         if (data == NULL) {
1242                                                                 da_err = sync_agent_delete_item(changedItem->luid, 1);
1243                                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1244                                                                         _DEBUG_ERROR("failed in sync_agent_delete_item !!");
1245                                                                         errorType = SA_INTERNAL_DA_ERROR;
1246                                                                         goto error;
1247                                                                 }
1248
1249                                                                 itemIter = g_list_next(itemIter);
1250                                                                 changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem);
1251                                                                 free_changed_item(changedItem);
1252                                                                 continue;
1253                                                         }
1254
1255                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1256                                                                 _DEBUG_ERROR("failed in sync_agent_get_service_item =%d", da_err);
1257                                                                 errorType = SA_INTERNAL_DA_ERROR;
1258                                                                 goto error;
1259                                                         }
1260
1261                                                         isFirstLargeObj = 1;
1262                                                 } else {
1263                                                         data = changedItem->data;
1264                                                         changedItem->data = NULL;
1265                                                         isFirstLargeObj = 0;
1266                                                 }
1267
1268                                                 unsigned int sizeOfData = 0;
1269                                                 if (data != NULL)
1270                                                         sizeOfData = strlen(data);
1271
1272                                                 _DEBUG_VERBOSE("sizeOfData = %d", sizeOfData);
1273                                                 if ((*session)->target_max_msg_size < stream_size + sizeOfData) {
1274                                                         /*can be LargeObj */
1275                                                         int availableSize = (*session)->target_max_msg_size - stream_size;
1276                                                         if ((*session)->remote_devinf->supports_large_objs /*&& availableSize > session->targetMaxMsgSize / 50 */ ) {
1277
1278                                                                 /*if server support LargeObj & clearly LargeObj */
1279                                                                 sendingData = calloc(availableSize + 1, sizeof(char));
1280                                                                 if (sendingData == NULL) {
1281                                                                         _DEBUG_ERROR("failed to allocate sendingData");
1282                                                                         errorType = SA_INTERNAL_NO_MEMORY;
1283                                                                         goto error;
1284                                                                 }
1285                                                                 memcpy(sendingData, data, availableSize);
1286                                                                 _DEBUG_VERBOSE("sendingData = %s", sendingData);
1287                                                                 _DEBUG_VERBOSE("sendingData size = %d", strlen(sendingData));
1288
1289                                                                 char *remainingData = calloc(sizeOfData - availableSize + 1, sizeof(char));
1290                                                                 if (remainingData == NULL) {
1291                                                                         _DEBUG_ERROR("failed to allocate remainingData");
1292                                                                         errorType = SA_INTERNAL_NO_MEMORY;
1293                                                                         goto error;
1294                                                                 }
1295                                                                 memcpy(remainingData, data + availableSize, sizeOfData - availableSize);
1296                                                                 _DEBUG_VERBOSE("sizeOfData - availableSize  = %d", sizeOfData - availableSize);
1297                                                                 _DEBUG_VERBOSE("remainingData = %s", remainingData);
1298                                                                 _DEBUG_VERBOSE("remainingData size = %d", strlen(remainingData));
1299
1300                                                                 if (changedItem->data != NULL)
1301                                                                         free(changedItem->data);
1302
1303                                                                 set_changed_item_data(changedItem, remainingData);
1304                                                                 free(remainingData);
1305
1306                                                                 command_status_s *pTemp = NULL;
1307                                                                 errorType = create_command_status((*session)->msg_id, (*session)->cmd_id, &pTemp);
1308                                                                 if (errorType != SA_INTERNAL_OK) {
1309                                                                         _DEBUG_ERROR("failed to create pending status");
1310                                                                         goto error;
1311                                                                 }
1312                                                                 (*session)->large_obj = pTemp;
1313
1314                                                                 if (changedItem->change_type == CHANGE_ADD) {
1315                                                                         /* create add Command */
1316                                                                         errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand);
1317                                                                 } else if (changedItem->change_type == CHANGE_REPLACE) {
1318                                                                         /* create replace Command */
1319                                                                         errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid,
1320                                                                                                            changedItem->content_type, sendingData, isFirstLargeObj == 1 ? sizeOfData : 0, 1, &changeCommand);
1321                                                                 }
1322
1323                                                                 if (errorType != SA_INTERNAL_OK) {
1324                                                                         _DEBUG_ERROR("failed to create add(replace) command");
1325                                                                         goto error;
1326                                                                 }
1327
1328                                                                 if (sendingData != NULL) {
1329                                                                         free(sendingData);
1330                                                                         sendingData = NULL;
1331                                                                 }
1332                                                         } else {
1333                                                                 needNextMsg = true;
1334                                                                 break;
1335                                                         }
1336                                                 } else {
1337                                                         /*can append more commnad */
1338                                                         if (changedItem->change_type == CHANGE_ADD) {
1339                                                                 /* create add Command */
1340                                                                 errorType = create_add_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand);
1341                                                         } else if (changedItem->change_type == CHANGE_REPLACE) {
1342                                                                 /* create replace Command */
1343                                                                 errorType = create_replace_command(*session, changedItem->change_type, changedItem->luid, changedItem->content_type, data, 0, 0, &changeCommand);
1344                                                         }
1345                                                         if (errorType != SA_INTERNAL_OK) {
1346                                                                 _DEBUG_ERROR("failed to create add(replace) command");
1347                                                                 goto error;
1348                                                         }
1349                                                 }
1350                                         }
1351                                 } else {
1352                                         needNextMsg = true;
1353                                         break;
1354                                 }
1355
1356                                 if (changeCommand->type == COMMAND_TYPE_ADD)
1357                                         protocol_element = PE_ADD;
1358                                 else if (changeCommand->type == COMMAND_TYPE_REPLACE)
1359                                         protocol_element = PE_REPLACE;
1360                                 else if (changeCommand->type == COMMAND_TYPE_DELETE)
1361                                         protocol_element = PE_DELETE;
1362
1363                                 err = oma_ds_1_2_binder_append(binder, protocol_element, changeCommand);
1364                                 oma_ds_1_2_binder_get_stream_size(binder, &stream_size);
1365                                 _DEBUG_VERBOSE("stream size after Changes Command= %d", stream_size);
1366                                 put_into_list(commands, commands_last, changeCommand);
1367
1368                                 if (changedItem->data == NULL) {
1369                                         itemIter = g_list_next(itemIter);
1370                                         changedDatastore->sent_item = g_list_append(changedDatastore->sent_item, changedItem);
1371                                         changedDatastore->change_item = g_list_remove(changedDatastore->change_item, changedItem);
1372                                 } else {
1373                                         needNextMsg = true;
1374                                         break;
1375                                 }
1376
1377                                 if (data != NULL) {
1378                                         free(data);
1379                                         data = NULL;
1380                                 }
1381                         }
1382
1383                         /* create sync end Command */
1384                         errorType = create_sync_end_command(&syncCommand);
1385                         if (errorType != SA_INTERNAL_OK) {
1386                                 _DEBUG_ERROR("failed to create sync end command");
1387                                 goto error;
1388                         }
1389                         err = oma_ds_1_2_binder_append(binder, PE_SYNC_END, syncCommand);
1390                         put_into_list(commands, commands_last, syncCommand);
1391
1392                         if (needNextMsg)
1393                                 break;
1394
1395                         int chageItemCount = g_list_length(changedDatastore->change_item);
1396                         if (chageItemCount == 0)
1397                                 changedDatastore->need_sync_command = 0;
1398                 }
1399         }
1400
1401         *is_need_next_msg = needNextMsg;
1402
1403  error:
1404
1405         if (sendingData != NULL) {
1406                 free(sendingData);
1407                 sendingData = NULL;
1408         }
1409
1410         if (data != NULL) {
1411                 free(data);
1412                 data = NULL;
1413         }
1414
1415         if (service_item != NULL) {
1416                 sync_agent_free_service_item(service_item);
1417         }
1418
1419         _INNER_FUNC_EXIT;
1420         return errorType;
1421 }
1422
1423 static sa_error_type_e _generate_suspend_msg(session_s * session, bool server_flag, char **msg, unsigned int *msg_size)
1424 {
1425         _INNER_FUNC_ENTER;
1426
1427         sa_error_type_e errorType = SA_INTERNAL_OK;
1428         char *id = NULL;
1429         char *pwd = NULL;
1430         char *sourceUrl = NULL;
1431         char *targetUrl = NULL;
1432         char *nextNonce = NULL;
1433
1434         sync_hdr_s *pSyncHdr = NULL;
1435         syncml_s *syncml = NULL;
1436
1437         GList *commands = NULL;
1438         GList *commands_last = NULL;
1439
1440         command_s *pAlertCommand = NULL;
1441         location_s *pSourceLocation = NULL;
1442         location_s *pTargetLocation = NULL;
1443
1444         /* change pkgstatus tp SYNCML_SUSPEND */
1445         session->pkg_status = SYNCML_SUSPEND;
1446
1447         sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
1448         if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
1449                 _DEBUG_ERROR("failed to get devinfo");
1450                 errorType = SA_INTERNAL_ERROR;
1451                 goto error;
1452         }
1453
1454         errorType = __get_account_info(session->account_id, &id, &pwd, &targetUrl, &nextNonce);
1455         if (errorType != SA_INTERNAL_OK)
1456                 goto error;
1457
1458         _DEBUG_TRACE("id = %s", id);
1459         _DEBUG_TRACE("pwd = %s", pwd);
1460         _DEBUG_TRACE("clientUrl = %s", sourceUrl);
1461         _DEBUG_TRACE("serverUrl = %s", targetUrl);
1462         _DEBUG_TRACE("nextNonce = %s", nextNonce);
1463
1464         /* create SyncHdr */
1465         errorType = create_sync_hdr(session, &pSyncHdr);
1466         if (errorType != SA_INTERNAL_OK) {
1467                 _DEBUG_ERROR("failed to create synchdr");
1468                 goto error;
1469         }
1470
1471 /*      Command *pMapCommand = NULL;
1472         GList *iter = NULL;
1473         for (iter = session->mapCommand; iter != NULL; iter = g_list_next(iter)) {
1474                 pMapCommand = (Command *)iter->data;
1475                 put_into_list(&commands, &commands_last, pMapCommand);
1476                 increase_command_refcount(pMapCommand);
1477         }*/
1478
1479         errorType = create_location(targetUrl, NULL, &pTargetLocation);
1480         if (errorType != SA_INTERNAL_OK) {
1481                 _DEBUG_ERROR("failed to create location");
1482
1483                 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
1484                    when SA_INTERNAL_NOT_DEFINED error case */
1485                 if (errorType == SA_INTERNAL_NOT_DEFINED)
1486                         errorType = SA_INTERNAL_MISCONFIGURATION;
1487                 goto error;
1488         }
1489
1490         errorType = create_location(sourceUrl, NULL, &pSourceLocation);
1491         if (errorType != SA_INTERNAL_OK) {
1492                 _DEBUG_ERROR("failed to create location");
1493
1494                 /* change errorType because locURI is required so need to return SA_INTERNAL_MISCONFIGURATION error
1495                    when SA_INTERNAL_NOT_DEFINED error case */
1496                 if (errorType == SA_INTERNAL_NOT_DEFINED)
1497                         errorType = SA_INTERNAL_MISCONFIGURATION;
1498                 goto error;
1499         }
1500
1501         errorType = create_alert_command(session, ALERT_SUSPEND, pSourceLocation, pTargetLocation, NULL, NULL, NULL, &pAlertCommand);
1502         if (errorType != SA_INTERNAL_OK) {
1503                 _DEBUG_ERROR("failed to create alert command");
1504                 goto error;
1505         }
1506
1507         put_into_list(&commands, &commands_last, pAlertCommand);
1508
1509         errorType = create_syncml(pSyncHdr, session->suspend_status, commands, 0, &syncml);
1510         if (errorType != SA_INTERNAL_OK) {
1511                 _DEBUG_ERROR("failed to create syncml");
1512                 goto error;
1513         }
1514
1515         /* convert Msg */
1516         errorType = __object_binder(syncml, server_flag, msg, msg_size);
1517         if (errorType != SA_INTERNAL_OK) {
1518                 _DEBUG_ERROR("failed in objectBinder");
1519                 goto error;
1520         }
1521
1522         /*free generate structure */
1523         free_syncml(syncml);
1524         session->suspend_status = NULL;
1525
1526         if (id != NULL)
1527                 free(id);
1528         if (pwd != NULL)
1529                 free(pwd);
1530         if (sourceUrl != NULL)
1531                 free(sourceUrl);
1532         if (targetUrl != NULL)
1533                 free(targetUrl);
1534         if (nextNonce != NULL)
1535                 free(nextNonce);
1536
1537         _INNER_FUNC_EXIT;
1538         return errorType;
1539
1540  error:
1541
1542         if (id != NULL)
1543                 free(id);
1544         if (pwd != NULL)
1545                 free(pwd);
1546         if (sourceUrl != NULL)
1547                 free(sourceUrl);
1548         if (targetUrl != NULL)
1549                 free(targetUrl);
1550         if (nextNonce != NULL)
1551                 free(nextNonce);
1552         if (syncml != NULL)
1553                 free_syncml(syncml);
1554         if (pSyncHdr != NULL)
1555                 free_sync_hdr(pSyncHdr);
1556
1557         session->suspend_status = NULL;
1558
1559         _INNER_FUNC_EXIT;
1560         return errorType;
1561
1562 }
1563
1564 static sa_error_type_e _exchange_msg(session_s * session, char *acc_type, int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length)
1565 {
1566         _INNER_FUNC_ENTER;
1567
1568         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
1569         sa_error_type_e errorType = SA_INTERNAL_OK;
1570         bool isXML;
1571         char *targetUri = NULL;
1572         char *jsessionId = NULL;
1573         GList *header_info = NULL;
1574         GList *recv_header = NULL;
1575
1576         if (session != NULL) {
1577                 if (session->target != NULL)
1578                         targetUri = session->target->loc_uri;
1579
1580                 if (session->jsession_id != NULL)
1581                         jsessionId = session->jsession_id;
1582         } else {
1583                 _DEBUG_ERROR("session is NULL !");
1584                 errorType = SA_INTERNAL_ERROR;
1585                 goto error;
1586         }
1587
1588         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1589                 isXML = true;
1590         else
1591                 isXML = false;
1592
1593         na_http_header_binder(acc_type, targetUri, isXML, jsessionId, &header_info);
1594         res = sync_agent_send_msg(      /*acc_info (id, pw, uri, ...), */
1595                                          header_info, transport_type, send_msg, send_msg_length, &recv_header, (unsigned char **)recv_msg, recv_msg_length, SYNC_AGENT_NA_SEND_TYPE_SEND_N_RECEIVE, session->naci_session_id);
1596
1597         if (res != SYNC_AGENT_NA_SUCCESS) {
1598                 _DEBUG_ERROR("res = %d", res);
1599                 if (res == SYNC_AGENT_NA_SEND_MSG_CANCEL)
1600                         errorType = SA_INTERNAL_CANCEL;
1601                 else
1602                         errorType = SA_INTERNAL_CONNECTION_ERROR;
1603                 goto error;
1604         }
1605
1606         __process_jssion_id(session, recv_header);
1607
1608  error:
1609
1610         if (recv_header != NULL) {
1611                 GList *iter = NULL;
1612                 sync_agent_na_common_header_info_s *iter_data;
1613                 for (iter = recv_header; iter != NULL;) {
1614                         iter_data = NULL;
1615                         iter_data = ((sync_agent_na_common_header_info_s *) (iter->data));
1616
1617                         iter = g_list_next(iter);
1618                         recv_header = g_list_remove(recv_header, iter_data);
1619
1620                         if (iter_data->key != NULL)
1621                                 free(iter_data->key);
1622                         if (iter_data->value != NULL)
1623                                 free(iter_data->value);
1624                         free(iter_data);
1625                 }
1626                 g_list_free(recv_header);
1627         }
1628
1629         _INNER_FUNC_EXIT;
1630         return errorType;
1631 }
1632
1633 static sa_error_type_e _process_recv_msg(session_s * session, char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish)
1634 {
1635         _INNER_FUNC_ENTER;
1636
1637         sa_error_type_e errorType = SA_INTERNAL_OK;
1638
1639         reset_cmd_id_session(session);
1640
1641         syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s));
1642         if (syncml == NULL) {
1643                 _DEBUG_ERROR("failed to allocate syncml");
1644                 errorType = SA_INTERNAL_NO_MEMORY;
1645                 goto error;
1646         }
1647
1648         GList *pReturnStatus = NULL;
1649         GList *pReturnDatastore = NULL;
1650
1651         errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length);
1652         if (errorType != SA_INTERNAL_OK) {
1653                 _DEBUG_ERROR("failed in reverseObjectBinder");
1654                 goto error;
1655         }
1656
1657         /* check receive msg */
1658         errorType = receive_header(session, syncml->hdr);
1659         if (errorType != SA_INTERNAL_OK) {
1660                 _DEBUG_ERROR("failed in receive_Header");
1661                 goto error;
1662         }
1663
1664         errorType = receive_statuses(session, syncml->status, &pReturnStatus);
1665         if (errorType != SA_INTERNAL_OK) {
1666                 _DEBUG_ERROR("failed in receive_Statuses");
1667                 goto error;
1668         }
1669
1670         errorType = receive_commands(session, syncml->commands, false, &pReturnDatastore);
1671         if (errorType != SA_INTERNAL_OK) {
1672                 _DEBUG_ERROR("failed in receive_Commands");
1673                 goto error;
1674         }
1675
1676         /* do not need to update pkgstatus when suspend */
1677         if (session->pkg_status == SYNCML_SUSPEND) {
1678                 _DEBUG_TRACE("Suspend");
1679                 __clean_up_sa(session);
1680                 static_session = NULL;
1681                 goto error;
1682         }
1683
1684         if (syncml->final) {
1685                 session->is_sending_final = 0;
1686                 session->is_receiving_final = 1;
1687         } else
1688                 session->is_receiving_final = 0;
1689
1690         if (session->pkg_status == SYNCML_PKG_2) {
1691                 pre_sync_return_obj_s **pre = (pre_sync_return_obj_s **) sync_return_obj;
1692
1693                 if ((*pre) != NULL)
1694                         (*pre)->datastore_info = pReturnDatastore;
1695
1696                 if (session->remote_devinf != NULL) {
1697                         if (session->remote_devinf->dev_id != NULL)
1698                                 (*pre)->dev_id = strdup(session->remote_devinf->dev_id);
1699                 }
1700         } else {
1701                 sync_return_obj_s **syncReturn = (sync_return_obj_s **) sync_return_obj;
1702
1703                 if ((*syncReturn) != NULL) {
1704                         (*syncReturn)->status = pReturnStatus;
1705                         (*syncReturn)->changed_datastore = pReturnDatastore;
1706                 }
1707         }
1708
1709         if (session->pkg_status == SYNCML_PKG_2 && session->is_receiving_final)
1710                 session->pkg_status = SYNCML_PKG_3;
1711         else if (session->pkg_status == SYNCML_PKG_4 && session->is_receiving_final) {
1712                 if (only_from_client) {
1713                         *is_finish = 1;
1714                         __clean_up_sa(session);
1715                         static_session = NULL;
1716                 } else
1717                         session->pkg_status = SYNCML_PKG_5;
1718         } else if (session->pkg_status == SYNCML_PKG_6 && session->is_receiving_final) {
1719                 *is_finish = 1;
1720                 __clean_up_sa(session);
1721                 static_session = NULL;
1722         }
1723
1724  error:
1725
1726         if (syncml != NULL)
1727                 free_syncml(syncml);
1728
1729         _INNER_FUNC_EXIT;
1730         return errorType;
1731
1732 }
1733
1734 static sa_error_type_e _generate_autoconfigure_msg(char *id, char *pwd, char *target_url, char **msg, unsigned int *msg_size, session_s * session)
1735 {
1736         _INNER_FUNC_ENTER;
1737         _DEBUG_TRACE("id = %s, pwd = %s, target_url = %s", id, pwd, target_url);
1738
1739         sa_error_type_e errorType = SA_INTERNAL_OK;
1740
1741         int isFinal = 1;
1742         char *credData = NULL;
1743         cred_s *pCred = NULL;
1744         sync_hdr_s *pSyncHdr = NULL;
1745         syncml_s *syncml = NULL;
1746
1747         GList *commands = NULL;
1748         GList *commands_last = NULL;
1749
1750         errorType = create_cred_string(AUTH_TYPE_BASIC, id, pwd, NULL, 0, &credData);
1751         if (errorType != SA_INTERNAL_OK) {
1752                 _DEBUG_ERROR("failed to create cred string");
1753                 goto error;
1754         }
1755
1756         errorType = create_cred(id, pwd, AUTH_TYPE_BASIC, FORMAT_TYPE_BASE64, credData, &pCred);
1757         if (errorType != SA_INTERNAL_OK) {
1758                 _DEBUG_ERROR("failed to create cred");
1759                 goto error;
1760         }
1761
1762         if (credData != NULL) {
1763                 free(credData);
1764                 credData = NULL;
1765         }
1766
1767         set_session_cred(session, pCred);
1768
1769         errorType = create_sync_hdr(session, &pSyncHdr);
1770         if (errorType != SA_INTERNAL_OK) {
1771                 _DEBUG_ERROR("failed to create synchdr");
1772                 goto error;
1773         }
1774
1775         char *sourceDevInf = NULL;
1776         if (session->protocol_version == VERSION_10)
1777                 sourceDevInf = ELEMENT_DEVINF_10;
1778         else if (session->protocol_version == VERSION_11)
1779                 sourceDevInf = ELEMENT_DEVINF_11;
1780         else if (session->protocol_version == VERSION_12)
1781                 sourceDevInf = ELEMENT_DEVINF_12;
1782
1783         location_s *pLocation = NULL;
1784         errorType = create_location(sourceDevInf, NULL, &pLocation);
1785         if (errorType != SA_INTERNAL_OK) {
1786                 _DEBUG_ERROR("failed to create location");
1787                 goto error;
1788         }
1789
1790         char *contentType = NULL;
1791         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1792                 contentType = ELEMENT_DEVINF_XML;
1793         else
1794                 contentType = ELEMENT_DEVINF_WBXML;
1795
1796         /* create Get command */
1797         command_s *pGetCommand = NULL;
1798         errorType = create_get_command(session, pLocation, contentType, &pGetCommand);
1799         if (errorType != SA_INTERNAL_OK) {
1800                 _DEBUG_ERROR("failed to get command");
1801                 goto error;
1802         }
1803         put_into_list(&commands, &commands_last, pGetCommand);
1804
1805         errorType = create_syncml(pSyncHdr, NULL, commands, isFinal, &syncml);
1806         if (errorType != SA_INTERNAL_OK) {
1807                 _DEBUG_ERROR("failed to get syncml");
1808                 goto error;
1809         }
1810
1811         errorType = __object_binder(syncml, false, msg, msg_size);
1812         if (errorType != SA_INTERNAL_OK) {
1813                 _DEBUG_ERROR("failed in objectBinder");
1814                 goto error;
1815         }
1816
1817         /*free generate structure */
1818         free_syncml(syncml);
1819         syncml = NULL;
1820
1821         _INNER_FUNC_EXIT;
1822         return errorType;
1823
1824  error:
1825         if (pSyncHdr != NULL)
1826                 free_sync_hdr(pSyncHdr);
1827         if (pCred != NULL)
1828                 free_cred(pCred);
1829         if (credData != NULL)
1830                 free(credData);
1831         if (commands != NULL)
1832                 free_commands(commands);
1833         if (syncml != NULL)
1834                 free(syncml);
1835
1836         _INNER_FUNC_EXIT;
1837         return errorType;
1838 }
1839
1840 static sa_error_type_e _process_autoconfigure_recv_msg(char *recv_msg, unsigned int recv_msg_length, session_s * session)
1841 {
1842         _INNER_FUNC_ENTER;
1843
1844         sa_error_type_e errorType = SA_INTERNAL_OK;
1845
1846         syncml_s *syncml = (syncml_s *) calloc(1, sizeof(syncml_s));
1847         if (syncml == NULL) {
1848                 _DEBUG_ERROR("failed to allocate syncml");
1849                 errorType = SA_INTERNAL_NO_MEMORY;
1850                 goto error;
1851         }
1852
1853         errorType = __reverse_object_binder(&syncml, recv_msg, recv_msg_length);
1854         if (errorType != SA_INTERNAL_OK) {
1855                 _DEBUG_ERROR("failed in reverseObjectBinder");
1856                 goto error;
1857         }
1858
1859         /* check receive msg */
1860         errorType = receive_header(session, syncml->hdr);
1861         if (errorType != SA_INTERNAL_OK) {
1862                 _DEBUG_ERROR("failed in receive_Header");
1863                 goto error;
1864         }
1865
1866         errorType = receive_statuses(session, syncml->status, NULL);
1867         if (errorType != SA_INTERNAL_OK) {
1868                 _DEBUG_ERROR("failed in receive_Statuses");
1869                 goto error;
1870         }
1871
1872         errorType = receive_commands(session, syncml->commands, true, NULL);
1873         if (errorType != SA_INTERNAL_OK) {
1874                 _DEBUG_ERROR("failed in receive_Commands");
1875                 goto error;
1876         }
1877
1878  error:
1879
1880         if (syncml != NULL)
1881                 free_syncml(syncml);
1882
1883         _INNER_FUNC_EXIT;
1884         return errorType;
1885 }
1886
1887 static sa_error_type_e __object_binder(syncml_s * syncml, bool server_flag, char **msg, unsigned int *msg_size)
1888 {
1889         _INNER_FUNC_ENTER;
1890
1891         sa_error_type_e errorType = SA_INTERNAL_OK;
1892
1893         sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
1894
1895         sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
1896
1897         sync_agent_pb_protocol_binder_info_s *binder = NULL;
1898         char *xml = NULL;
1899         unsigned int xml_len = 0;
1900
1901         if (server_flag != true)
1902                 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, false, false, true, false, binder_function_set);
1903         else
1904                 binder = oma_ds_1_2_binder_init(binder, COMMUNICATION_TYPE, false, true, false, true, true, binder_function_set);
1905
1906         err = oma_ds_1_2_binder_append(binder, PE_SYNCML_START, NULL);
1907         err = oma_ds_1_2_binder_append(binder, PE_HEADER, syncml->hdr);
1908         err = oma_ds_1_2_binder_append(binder, PE_BODY_START, NULL);
1909
1910         GList *status_iter = NULL;
1911         status_s *pStatus = NULL;
1912         for (status_iter = syncml->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
1913                 pStatus = status_iter->data;
1914                 err = oma_ds_1_2_binder_append(binder, PE_STATUS, pStatus);
1915         }
1916
1917         GList *iter = NULL;
1918         command_s *pCommand = NULL;
1919         oma_ds_protocol_element_e protocol_element = PE_UNDEF;
1920         for (iter = syncml->commands; iter != NULL; iter = g_list_next(iter)) {
1921                 pCommand = (command_s *) (iter->data);
1922
1923                 command_type_e type = pCommand->type;
1924                 switch (type) {
1925                 case COMMAND_TYPE_UNKNOWN:
1926                         protocol_element = PE_UNDEF;
1927                         break;
1928                 case COMMAND_TYPE_ALERT:
1929                         protocol_element = PE_ALERT;
1930                         break;
1931                 case COMMAND_TYPE_SYNC_START:
1932                         protocol_element = PE_SYNC_START;
1933                         break;
1934                 case COMMAND_TYPE_SYNC_END:
1935                         protocol_element = PE_SYNC_END;
1936                         break;
1937                 case COMMAND_TYPE_PUT:
1938                         protocol_element = PE_PUT_START;
1939                         break;
1940                 case COMMAND_TYPE_ADD:
1941                         protocol_element = PE_ADD;
1942                         break;
1943                 case COMMAND_TYPE_REPLACE:
1944                         protocol_element = PE_REPLACE;
1945                         break;
1946                 case COMMAND_TYPE_DELETE:
1947                         protocol_element = PE_DELETE;
1948                         break;
1949                 case COMMAND_TYPE_MAP:
1950                         protocol_element = PE_MAP;
1951                         break;
1952                 case COMMAND_TYPE_GET:
1953                         protocol_element = PE_GET;
1954                         break;
1955                 case COMMAND_TYPE_RESULTS:
1956                         protocol_element = PE_RESULTS_START;
1957                         break;
1958                 case COMMAND_TYPE_HEADER:
1959                         protocol_element = PE_HEADER;
1960                         break;
1961                 }
1962                 err = oma_ds_1_2_binder_append(binder, protocol_element, pCommand);
1963
1964                 if (type == COMMAND_TYPE_RESULTS) {
1965                         err = oma_ds_1_2_binder_append(binder, PE_RESULTS_END, NULL);
1966                 }
1967
1968                 if (type == COMMAND_TYPE_PUT) {
1969                         if (pCommand->private.access.item != NULL) {
1970                                 if (pCommand->private.access.item->private.devinf != NULL) {
1971                                         err = oma_ds_1_2_binder_append(binder, PE_DEVINF, pCommand->private.access.item);
1972                                 }
1973                         }
1974                         err = oma_ds_1_2_binder_append(binder, PE_PUT_END, NULL);
1975                 }
1976         }
1977
1978         if (syncml->final) {
1979                 err = oma_ds_1_2_binder_append(binder, PE_FINAL, NULL);
1980         }
1981
1982         err = oma_ds_1_2_binder_append(binder, PE_BODY_END, NULL);
1983         err = oma_ds_1_2_binder_append(binder, PE_SYNCML_END, NULL);
1984
1985         oma_ds_1_2_binder_get_stream(binder, msg, msg_size);
1986
1987         /*FIXME for debugging */
1988         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
1989                 set_xml_to_file(*msg, OMA_DS_MSG_PATH);
1990         else {
1991                 err = sync_agent_get_xml_from_protocol_binder(binder, &xml, &xml_len);
1992                 if (err == SYNC_AGENT_PB_RETURN_OK) {
1993                         set_xml_to_file(xml, OMA_DS_MSG_PATH);
1994
1995                         if (xml != NULL) {
1996                                 free(xml);
1997                                 xml = NULL;
1998                         }
1999                 }
2000         }
2001
2002         oma_ds_1_2_binder_terminate(binder);
2003
2004         if (err != SYNC_AGENT_PB_RETURN_OK)
2005                 errorType = SA_INTERNAL_BINDER_ERROR;
2006
2007         _INNER_FUNC_EXIT;
2008         return errorType;
2009 }
2010
2011 static sa_error_type_e __reverse_object_binder(syncml_s ** syncml, char *recv_msg, unsigned int recv_msg_length)
2012 {
2013         _INNER_FUNC_ENTER;
2014
2015         sa_error_type_e errorType = SA_INTERNAL_OK;
2016         sync_agent_pb_protocol_binder_function_set_s *binder_function_set = NULL;
2017
2018         sync_agent_pb_error_e err = init_oma_ds_1_2_binder_function_set(&binder_function_set);
2019         sync_agent_pb_protocol_binder_reverse_info_s *binder = NULL;
2020         char *xml = NULL;
2021         unsigned int xml_len = 0;
2022
2023         sync_agent_pb_decoding_e dec;
2024         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
2025                 dec = SYNC_AGENT_PB_DECODING_XML;
2026         else
2027                 dec = SYNC_AGENT_PB_DECODING_WBXML;
2028
2029         err = reverse_oma_ds_1_2_binder_init(recv_msg, recv_msg_length, dec, binder_function_set, NULL, &binder);
2030
2031         if (err != SYNC_AGENT_PB_RETURN_OK) {
2032                 errorType = SA_INTERNAL_BINDER_ERROR;
2033                 _DEBUG_ERROR("reverse_oma_ds_1_2_binder_init error =%d", err);
2034                 return errorType;
2035         }
2036
2037         /*FIXME for debugging */
2038         if (COMMUNICATION_TYPE == SYNC_AGENT_PB_ENCODING_XML)
2039                 set_xml_to_file(recv_msg, OMA_DS_MSG_PATH);
2040         else {
2041                 err = sync_agent_get_xml_from_reverse_protocol_binder(binder, &xml, &xml_len);
2042                 if (err == SYNC_AGENT_PB_RETURN_OK) {
2043                         set_xml_to_file(xml, OMA_DS_MSG_PATH);
2044
2045                         if (xml != NULL) {
2046                                 free(xml);
2047                                 xml = NULL;
2048                         }
2049                 }
2050         }
2051
2052         oma_ds_protocol_element_e protocol_element = PE_UNDEF;
2053         char *protocol_element_name = NULL;
2054         Content_Ptr pContent = NULL;
2055         command_s *tempCommandfordevInf = NULL;
2056         while (err == SYNC_AGENT_PB_RETURN_OK) {
2057                 protocol_element = PE_UNDEF;
2058                 err = reverse_oma_ds_1_2_binder_next(binder, &protocol_element, &protocol_element_name, &pContent);
2059                 switch (protocol_element) {
2060                 case PE_ADD:
2061                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2062                         break;
2063                 case PE_ATOMIC_START:
2064                 case PE_ATOMIC_END:
2065                 case PE_COPY:
2066                 case PE_DELETE:
2067                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2068                         break;
2069                 case PE_EXEC:
2070                 case PE_GET:
2071                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2072                         break;
2073                 case PE_MAP:
2074                 case PE_PUT_START:
2075                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2076                         tempCommandfordevInf = pContent;
2077                         break;
2078                 case PE_SEARCH:
2079                 case PE_SEQUENCE_START:
2080                 case PE_SEQUENCE_END:
2081                 case PE_SYNC_START:
2082                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2083                         break;
2084                 case PE_SYNC_END:
2085                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2086                         protocol_element = PE_UNDEF;
2087                         break;
2088                 case PE_REPLACE:
2089                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2090                         break;
2091                 case PE_ALERT:
2092                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2093                         break;
2094                 case PE_RESULTS_START:
2095                         (*syncml)->commands = g_list_append((*syncml)->commands, pContent);
2096                         tempCommandfordevInf = pContent;
2097                         break;
2098                 case PE_STATUS:
2099                         (*syncml)->status = g_list_append((*syncml)->status, pContent);
2100                         protocol_element = PE_UNDEF;
2101                         break;
2102                 case PE_HEADER:
2103                         (*syncml)->hdr = pContent;
2104                         sync_agent_set_user_data_in_reverse_protocol_binder(binder, (*syncml)->hdr);
2105                         break;
2106                 case PE_PUT_GET:
2107                 case PE_CMD_GROUP:
2108                 case PE_GENERIC:
2109                 case PE_FINAL:
2110                         (*syncml)->final = (int)pContent;
2111                         break;
2112                 case PE_DEVINF:
2113                         if (tempCommandfordevInf != NULL) {
2114                                 tempCommandfordevInf->private.results.item->private.devinf = pContent;
2115                                 tempCommandfordevInf = NULL;
2116                         }
2117                         break;
2118                 default:
2119                         break;
2120                 }
2121
2122                 if (protocol_element_name != NULL) {
2123                         free(protocol_element_name);
2124                         protocol_element_name = NULL;
2125                 }
2126
2127                 if (err == SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT) {
2128                         break;
2129                 }
2130         }
2131
2132         sync_agent_destroy_reverse_protocol_binder(binder);
2133
2134         free_oma_ds_1_2_binder_function_set(binder_function_set);
2135
2136         if (err != SYNC_AGENT_PB_RETURN_OK && err != SYNC_AGENT_PB_RETURN_HAS_NO_MORE_ELEMENT)
2137                 errorType = SA_INTERNAL_BINDER_ERROR;
2138
2139         _INNER_FUNC_EXIT;
2140         return errorType;
2141 }
2142
2143 static common_error_type_e _convert_error_type(sa_error_type_e error_type)
2144 {
2145         _INNER_FUNC_ENTER;
2146
2147         common_error_type_e sa_errorType = COMMON_OK;
2148
2149         if (error_type == SA_INTERNAL_OK)
2150                 sa_errorType = COMMON_OK;
2151         else if (error_type == SA_INTERNAL_MISCONFIGURATION)
2152                 sa_errorType = COMMON_MISCONFIGURATION;
2153         else if (error_type == SA_INTERNAL_AUTHENTICATION_ERROR)
2154                 sa_errorType = COMMON_AUTHENTICATION_ERROR;
2155         else if (error_type == SA_INTERNAL_NOT_FOUND)
2156                 sa_errorType = COMMON_NOT_FOUND;
2157         else if (error_type == SA_INTERNAL_NO_MEMORY)
2158                 sa_errorType = COMMON_NO_MEMORY;
2159         else if (error_type == SA_INTERNAL_NOT_DEFINED || error_type == SA_INTERNAL_BINDER_ERROR || error_type == SA_INTERNAL_ERROR || error_type == SA_INTERNAL_DA_ERROR)
2160                 sa_errorType = COMMON_INTERNAL_ERROR;
2161         else if (error_type == SA_INTERNAL_SERVER_ERROR || error_type == SA_INTERNAL_SERVER_FAILURE)
2162                 sa_errorType = COMMON_SERVER_ERROR;
2163         else if (error_type == SA_INTERNAL_CONNECTION_ERROR)
2164                 sa_errorType = COMMON_CONNECTION_ERROR;
2165         else if (error_type == SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER)
2166                 sa_errorType = COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2167         else if (error_type == SA_INTERNAL_CANCEL)
2168                 sa_errorType = COMMON_CANCEL;
2169         else if (error_type == SA_INTERNAL_BUSY_SIGNALING)
2170                 sa_errorType = COMMON_BUSY_SIGNALING;
2171         else if (error_type == SA_INTERNAL_SUSPEND_FAIL)
2172                 sa_errorType = COMMON_SUSPEND_FAIL;
2173
2174         _INNER_FUNC_EXIT;
2175         return sa_errorType;
2176
2177         /*from processRecvMsg
2178            401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
2179            407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
2180            404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
2181
2182            1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
2183
2184            1513 -> ERROR_INTERNAL_NOT_DEFINED
2185            1512 -> ERROR_INTERNAL_NO_MEMORY
2186            1503 -> ERROR_INTERNAL_BINDER_ERROR
2187
2188            500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
2189            511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error)
2190
2191            can be return to engine value
2192            from generatePreSyncMsg
2193            1506 -> ERROR_INTERNAL_MISCONFIGURATION : need configure infomation
2194            1500 -> ERROR_INTERNAL_OK : generatePreSyncMsg has been completed without any error
2195            1512 -> ERROR_INTERNAL_NO_MEMORY : failed to allocate memory
2196            1513 -> ERROR_INTERNAL_NOT_DEFINED : something is not defined that must have
2197            1503 -> ERROR_INTERNAL_BINDER_ERROR  : binder error
2198
2199            from exchangeMsg
2200
2201            from processRecvMsg
2202            401 ->ERROR_AUTH_REJECTED :wrong password(header, datastore)
2203            407 -> ERROR_AUTH_REQUIRED : try one more request and if failed again return ERROR_AUTH_REQUIRED(header, datastore)
2204            404 -> ERROR_NOT_FOUND -> no datastore (important -> have to be noted to user)
2205
2206            1500 -> ERROR_INTERNAL_OK : processRecvMsg has been completed without any error
2207
2208            1513 -> ERROR_INTERNAL_NOT_DEFINED
2209            1512 -> ERROR_INTERNAL_NO_MEMORY
2210            1503 -> ERROR_INTERNAL_BINDER_ERROR
2211
2212            500 -> ERROR_GENERIC (our problem -> do not ever never receive this error)
2213            511 ->ERROR_SERVER_FAILURE(our problem -> do not ever never receive this error) */
2214
2215 }
2216
2217 static void __clean_up_sa(session_s * session)
2218 {
2219         _INNER_FUNC_ENTER;
2220
2221         if (session != NULL) {
2222
2223                 if (session->has_opend) {
2224                         sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id);
2225                         session->has_opend = 0;
2226                 }
2227
2228                 free_session(session);
2229                 session = NULL;
2230         }
2231
2232         _INNER_FUNC_EXIT;
2233 }
2234
2235 static sa_error_type_e __process_jssion_id(session_s * session, GList * recv_header)
2236 {
2237         _INNER_FUNC_ENTER;
2238
2239         sa_error_type_e errorType = SA_INTERNAL_OK;
2240         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
2241
2242         _DEBUG_VERBOSE(" CKECK JSESSION\n");
2243         char *temp_jsessionid = NULL;
2244         char *real_jsessionid = NULL;
2245         res = sync_agent_get_header_info(1, recv_header, "Set-Cookie", &temp_jsessionid);
2246         if (res == SYNC_AGENT_NA_SUCCESS) {
2247                 if (temp_jsessionid != NULL) {
2248                         real_jsessionid = strstr(temp_jsessionid, "JSESSIONID");
2249                         if (real_jsessionid != NULL) {
2250                                 session->jsession_id = strdup(real_jsessionid);
2251                                 _DEBUG_VERBOSE("session->jsessionId = %s", session->jsession_id);
2252
2253                         }
2254                 }
2255         } else
2256                 _DEBUG_ERROR("failed in sync_agent_get_header_info = %d", res);
2257
2258         if (temp_jsessionid != NULL)
2259                 free(temp_jsessionid);
2260
2261         _INNER_FUNC_EXIT;
2262         return errorType;
2263 }
2264
2265 static sa_error_type_e __check_resume_session(session_s * session, bool * resume)
2266 {
2267         _INNER_FUNC_ENTER;
2268
2269         sa_error_type_e errorType = SA_INTERNAL_OK;
2270         char *value = NULL;
2271         bool result;
2272
2273         result = get_config(session->account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, &value);
2274         _DEBUG_VERBOSE("resume = %s", value);
2275         if (result == false) {
2276                 _DEBUG_ERROR("failed in get_config");
2277                 errorType = SA_INTERNAL_DA_ERROR;
2278                 goto error;
2279         }
2280
2281         if (strcmp(value, "1") == 0)
2282                 *resume = true;
2283         else
2284                 *resume = false;
2285
2286  error:
2287
2288         if (value != NULL)
2289                 free(value);
2290
2291         _INNER_FUNC_EXIT;
2292         return errorType;
2293
2294 }
2295
2296 int pre_sync(int transportType, int account_id, char *session_id, int server_flag, void **pre_sync_return_obj)
2297 {
2298         _EXTERN_FUNC_ENTER;
2299
2300         sa_error_type_e errorType = SA_INTERNAL_OK;
2301
2302         char *msg = NULL;
2303         unsigned int msg_size = 0;
2304         char *recv_msg = NULL;
2305         unsigned int recv_msg_size = 0;
2306         unsigned int naci_session_id;
2307
2308         /* create session structure */
2309         errorType = _create_session(account_id, session_id, server_flag, &static_session);
2310         if (errorType != SA_INTERNAL_OK) {
2311                 _DEBUG_ERROR("failed in __create_session");
2312                 goto error;
2313         }
2314
2315         /* generate preSync Msg */
2316         errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size);
2317         if (errorType != SA_INTERNAL_OK) {
2318                 _DEBUG_ERROR("failed in __generate_presync_msg");
2319                 goto error;
2320         }
2321
2322         if (!static_session->has_opend) {
2323                 sync_agent_na_result_e res = sync_agent_open_connection(transportType, NETWORK_TIMEOUT, &naci_session_id);
2324                 if (res != SYNC_AGENT_NA_SUCCESS) {
2325                         _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res);
2326                         errorType = SA_INTERNAL_CONNECTION_ERROR;
2327                         goto error;
2328                 }
2329                 static_session->naci_session_id = naci_session_id;
2330                 static_session->has_opend = 1;
2331                 _DEBUG_INFO("naci_session_id = %d", naci_session_id);
2332         }
2333         /* exchange Msg */
2334         errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size);
2335         if (errorType != SA_INTERNAL_OK)
2336                 goto error;
2337
2338         errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL);
2339         if (errorType == SA_INTERNAL_AUTHENTICATION_ERROR) {
2340                 /*when errortype is ERROR_AUTH_REQUIRED try request using chal just one more time
2341                    it can be only happend when server required MD5 authentication(we send basic authentication every time by default) */
2342
2343                 /* create session structure */
2344                 errorType = _create_session(account_id, session_id, server_flag, &static_session);
2345                 if (errorType != SA_INTERNAL_OK) {
2346                         _DEBUG_ERROR("failed in __create_session");
2347                         goto error;
2348                 }
2349
2350                 if (msg != NULL) {
2351                         free(msg);
2352                         msg = NULL;
2353                 }
2354
2355                 if (recv_msg != NULL) {
2356                         free(recv_msg);
2357                         recv_msg = NULL;
2358                 }
2359
2360                 errorType = _generate_presync_msg(static_session, server_flag, &msg, &msg_size);
2361                 if (errorType != SA_INTERNAL_OK)
2362                         goto error;
2363
2364                 errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transportType, msg, msg_size, &recv_msg, &recv_msg_size);
2365                 if (errorType != SA_INTERNAL_OK)
2366                         goto error;
2367
2368                 errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, pre_sync_return_obj, NULL);
2369                 if (errorType != SA_INTERNAL_OK)
2370                         goto error;
2371         }
2372
2373  error:
2374
2375         if (msg != NULL) {
2376                 free(msg);
2377                 msg = NULL;
2378         }
2379
2380         if (recv_msg != NULL) {
2381                 free(recv_msg);
2382                 recv_msg = NULL;
2383         }
2384
2385         if (errorType != SA_INTERNAL_OK) {
2386                 _DEBUG_ERROR("errorType : %d", errorType);
2387                 __clean_up_sa(static_session);
2388                 static_session = NULL;
2389         }
2390
2391         _EXTERN_FUNC_EXIT;
2392         return _convert_error_type(errorType);
2393 }
2394
2395 int generate_msg(void **syncObj, int server_flag, char **msg, unsigned int *msg_size)
2396 {
2397         _EXTERN_FUNC_ENTER;
2398
2399         sa_error_type_e errorType = SA_INTERNAL_OK;
2400
2401         errorType = _generate_msg(static_session, syncObj, server_flag, msg, msg_size);
2402         if (errorType != SA_INTERNAL_OK)
2403                 goto error;
2404
2405  error:
2406
2407         if (errorType != SA_INTERNAL_OK) {
2408                 _DEBUG_ERROR("errorType : %d", errorType);
2409                 __clean_up_sa(static_session);
2410                 static_session = NULL;
2411         }
2412
2413         _EXTERN_FUNC_EXIT;
2414         return _convert_error_type(errorType);
2415 }
2416
2417 int exchange_msg(int transport_type, char *send_msg, unsigned int send_msg_length, char **recv_msg, unsigned int *recv_msg_length)
2418 {
2419         _EXTERN_FUNC_ENTER;
2420
2421         sa_error_type_e errorType = SA_INTERNAL_OK;
2422
2423         errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, send_msg, send_msg_length, recv_msg, recv_msg_length);
2424         if (errorType != SA_INTERNAL_OK)
2425                 goto error;
2426
2427  error:
2428
2429         if (errorType != SA_INTERNAL_OK && errorType != SA_INTERNAL_CANCEL) {
2430                 _DEBUG_ERROR("errorType : %d", errorType);
2431                 __clean_up_sa(static_session);
2432                 static_session = NULL;
2433         }
2434
2435         _EXTERN_FUNC_EXIT;
2436         return _convert_error_type(errorType);
2437 }
2438
2439 int process_recv_msg(char *recv_msg, unsigned int recv_msg_length, int only_from_client, void **sync_return_obj, int *is_finish)
2440 {
2441         _EXTERN_FUNC_ENTER;
2442
2443         sa_error_type_e errorType = SA_INTERNAL_OK;
2444
2445         errorType = _process_recv_msg(static_session, recv_msg, recv_msg_length, only_from_client, sync_return_obj, is_finish);
2446         if (errorType != SA_INTERNAL_OK)
2447                 goto error;
2448
2449  error:
2450
2451         if (errorType != SA_INTERNAL_OK) {
2452                 _DEBUG_ERROR("errorType : %d", errorType);
2453                 __clean_up_sa(static_session);
2454                 static_session = NULL;
2455         }
2456
2457         _EXTERN_FUNC_EXIT;
2458         return _convert_error_type(errorType);
2459 }
2460
2461 void clean_up_sa()
2462 {
2463         _EXTERN_FUNC_ENTER;
2464
2465         __clean_up_sa(static_session);
2466         static_session = NULL;
2467
2468         _EXTERN_FUNC_EXIT;
2469 }
2470
2471 int auto_configure(char *addr, char *id, char *password, GList ** configure)
2472 {
2473         _EXTERN_FUNC_ENTER;
2474
2475         sa_error_type_e errorType = SA_INTERNAL_OK;
2476         session_s *session = NULL;
2477
2478         char *sourceUrl = NULL;
2479         char *sessionId = NULL;
2480
2481         char *msg = NULL;
2482         unsigned int msg_size = 0;
2483
2484         char *recv_msg = NULL;
2485         unsigned int recv_msg_size = 0;
2486
2487         bool cancel_flag = false;
2488         unsigned int session_id;
2489
2490         location_s *pSourceLocation = NULL;
2491         location_s *pTargetLocation = NULL;
2492
2493         sessionId = g_strdup_printf("%ld", time(NULL));
2494         if (sessionId == NULL) {
2495                 errorType = SA_INTERNAL_NO_MEMORY;
2496                 goto error;
2497         }
2498
2499         sync_agent_dev_return_e err = sync_agent_get_devinfo(DEFINE_PLATFORM, "DevID", &sourceUrl);
2500         if (err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
2501                 errorType = SA_INTERNAL_ERROR;
2502                 goto error;
2503         }
2504
2505         errorType = create_location(sourceUrl, NULL, &pSourceLocation);
2506         if (errorType != SA_INTERNAL_OK) {
2507                 _DEBUG_ERROR("failed in create_location");
2508                 goto error;
2509         }
2510
2511         errorType = create_location(addr, NULL, &pTargetLocation);
2512         if (errorType != SA_INTERNAL_OK) {
2513                 _DEBUG_ERROR("failed in create_location");
2514                 goto error;
2515         }
2516
2517         errorType = create_session(VERSION_12, PROTOCOL_TYPE_DS, 0, sessionId, pSourceLocation, pTargetLocation, &session);
2518         if (errorType != SA_INTERNAL_OK) {
2519                 _DEBUG_ERROR("failed in create_session");
2520                 goto error;
2521         }
2522
2523         if (session == NULL) {
2524                 errorType = SA_INTERNAL_NO_MEMORY;
2525                 goto error;
2526         }
2527
2528         errorType = _generate_autoconfigure_msg(id, password, addr, &msg, &msg_size, session);
2529         if (errorType != SA_INTERNAL_OK)
2530                 goto error;
2531
2532         if (!session->has_opend) {
2533                 sync_agent_na_result_e res = sync_agent_open_connection(TRANSPORT_TYPE, NETWORK_TIMEOUT, &session_id);
2534                 if (res != SYNC_AGENT_NA_SUCCESS) {
2535                         _DEBUG_ERROR("failed in sync_agent_open_connection res = %d", res);
2536                         errorType = SA_INTERNAL_CONNECTION_ERROR;
2537                         goto error;
2538                 }
2539                 session->naci_session_id = session_id;
2540                 session->has_opend = 1;
2541                 _DEBUG_INFO("session_id = %d", session_id);
2542         }
2543
2544         cancel_flag = sync_agent_check_cancel_flag();
2545         if (cancel_flag) {
2546                 errorType = SA_INTERNAL_CANCEL;
2547                 goto error;
2548         }
2549
2550         /* exchange Msg */
2551         errorType = _exchange_msg(session, DEFINE_PROTOCOL_TYPE, TRANSPORT_TYPE, msg, msg_size, &recv_msg, &recv_msg_size);
2552         if (errorType != SA_INTERNAL_OK)
2553                 goto error;
2554
2555         cancel_flag = sync_agent_check_cancel_flag();
2556         if (cancel_flag) {
2557                 errorType = SA_INTERNAL_CANCEL;
2558                 goto error;
2559         }
2560
2561         errorType = _process_autoconfigure_recv_msg(recv_msg, recv_msg_size, session);
2562         if (errorType != SA_INTERNAL_OK)
2563                 goto error;
2564
2565         cancel_flag = sync_agent_check_cancel_flag();
2566         if (cancel_flag) {
2567                 errorType = SA_INTERNAL_CANCEL;
2568                 goto error;
2569         }
2570
2571         devinf_s *devInf = session->remote_devinf;
2572         if (devInf != NULL) {
2573
2574                 GList *tempConfigure = NULL;
2575
2576                 GList *contactCandidate = NULL;
2577                 GList *calendarCandidate = NULL;
2578                 GList *memoCandidate = NULL;
2579
2580                 /*set inputted name by user */
2581                 contactCandidate = g_list_append(contactCandidate, strdup(DEFINE_SOURCE_CONTACT_URI));
2582                 /*
2583                    char *target = NULL;
2584                    target= get_vconf_str(profilePath, DS_SETTING_CONTACT_TGTURI);
2585                    if (target != NULL) {
2586                    _DEBUG_INFO("Contacts target =%s",  target);
2587                    if (strcmp(target, "") !=0)
2588                    contactCandidate = g_list_append(contactCandidate, strdup(target));
2589
2590                    free(target);
2591                    target = NULL;
2592                    }
2593                  */
2594
2595                 calendarCandidate = g_list_append(calendarCandidate, strdup(DEFINE_SOURCE_CALENDAR_URI));
2596                 /*
2597                    target = get_vconf_str(profilePath, DS_SETTING_CALENDAR_TGTURI);
2598                    if (target != NULL) {
2599                    _DEBUG_INFO("Organizer target =%s", target);
2600                    if (strcmp(target, "") !=0)
2601                    calendarCandidate = g_list_append(calendarCandidate, strdup(target));
2602
2603                    free(target);
2604                    target = NULL;
2605                    }
2606                  */
2607
2608                 memoCandidate = g_list_append(memoCandidate, strdup(DEFINE_SOURCE_MEMO_URI));
2609                 /*
2610                    target = get_vconf_str(profilePath, DS_SETTING_MEMO_TGTURI);
2611                    if (target != NULL) {
2612                    _DEBUG_INFO("Memo target =%s", target);
2613                    if (strcmp(target, "") !=0)
2614                    memoCandidate = g_list_append(memoCandidate, strdup(target));
2615
2616                    free(target);
2617                    target = NULL;
2618                    }
2619                  */
2620
2621                 GList *iter = NULL;
2622                 GList *innerIter = NULL;
2623                 devinf_datastore_s *devInfDataStore = NULL;
2624                 int exist;
2625                 char *candidate;
2626                 for (iter = devInf->datastores; iter != NULL; iter = g_list_next(iter)) {
2627                         devInfDataStore = iter->data;
2628                         exist = 0;
2629                         candidate = NULL;
2630
2631                         _DEBUG_INFO("devInfDataStore->sourceref = %s", devInfDataStore->source_ref);
2632
2633                         if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCARD_30) == 0) {
2634
2635                                 for (innerIter = g_list_next(contactCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2636                                         candidate = innerIter->data;
2637
2638                                         if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2639                                                 exist = 1;
2640                                 }
2641                                 if (!exist) {
2642                                         if (devInfDataStore->source_ref != NULL)
2643                                                 contactCandidate = g_list_append(contactCandidate, strdup(devInfDataStore->source_ref));
2644                                 }
2645
2646                         } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_VCAL) == 0 || strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_ICAL) == 0) {
2647
2648                                 for (innerIter = g_list_next(calendarCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2649                                         candidate = innerIter->data;
2650
2651                                         if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2652                                                 exist = 1;
2653                                 }
2654                                 if (!exist) {
2655                                         if (devInfDataStore->source_ref != NULL)
2656                                                 calendarCandidate = g_list_append(calendarCandidate, strdup(devInfDataStore->source_ref));
2657                                 }
2658
2659                         } else if (strcmp(devInfDataStore->rx_pref->ct_type, ELEMENT_TEXT_PLAIN) == 0) {
2660
2661                                 for (innerIter = g_list_next(memoCandidate); innerIter != NULL; innerIter = g_list_next(innerIter)) {
2662                                         candidate = innerIter->data;
2663
2664                                         if (strcmp(candidate, devInfDataStore->source_ref) == 0)
2665                                                 exist = 1;
2666                                 }
2667                                 if (!exist) {
2668                                         if (devInfDataStore->source_ref != NULL)
2669                                                 memoCandidate = g_list_append(memoCandidate, strdup(devInfDataStore->source_ref));
2670                                 }
2671                         }
2672                 }
2673
2674                 tempConfigure = g_list_append(tempConfigure, contactCandidate);
2675                 tempConfigure = g_list_append(tempConfigure, calendarCandidate);
2676                 tempConfigure = g_list_append(tempConfigure, memoCandidate);
2677
2678                 *configure = tempConfigure;
2679
2680 //              *congifure = g_list_append(*congifure, contactCandidate);
2681 //              *congifure = g_list_append(*congifure, calendarCandidate);
2682 //              *congifure = g_list_append(*congifure, memoCandidate);
2683
2684         } else {
2685                 errorType = SA_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2686                 goto error;
2687         }
2688
2689  error:
2690
2691         /*close network */
2692         if (session != NULL) {
2693                 if (session->has_opend)
2694                         sync_agent_close_connection(TRANSPORT_TYPE, session->naci_session_id);
2695         }
2696
2697         if (msg != NULL) {
2698                 free(msg);
2699                 msg = NULL;
2700         }
2701
2702         if (recv_msg != NULL) {
2703                 free(recv_msg);
2704                 recv_msg = NULL;
2705         }
2706
2707         if (sessionId != NULL)
2708                 free(sessionId);
2709
2710         if (sourceUrl != NULL)
2711                 free(sourceUrl);
2712
2713         if (session != NULL) {
2714                 free_session(session);
2715                 session = NULL;
2716         }
2717
2718         if (errorType != SA_INTERNAL_OK)
2719                 _DEBUG_ERROR("errorType =%d", errorType);
2720
2721         _EXTERN_FUNC_EXIT;
2722         return _convert_error_type(errorType);
2723 }
2724
2725 int check_cancel_status()
2726 {
2727         _EXTERN_FUNC_ENTER;
2728
2729         bool result = false;
2730         retvm_if(static_session == NULL, false, "session is NULL");
2731
2732         if (static_session->pkg_status == SYNCML_PKG_5 || static_session->pkg_status == SYNCML_PKG_6)
2733                 result = false;
2734         else
2735                 result = true;
2736
2737         _EXTERN_FUNC_EXIT;
2738
2739         return result;
2740 }
2741
2742 int suspend_sync(int transport_type, int account_id, int server_flag)
2743 {
2744         _EXTERN_FUNC_ENTER;
2745
2746         sa_error_type_e errorType = SA_INTERNAL_OK;
2747
2748         char *msg = NULL;
2749         unsigned int msg_size = 0;
2750         char *recv_msg = NULL;
2751         unsigned int recv_msg_size = 0;
2752
2753         /* generate preSync Msg */
2754         errorType = _generate_suspend_msg(static_session, server_flag, &msg, &msg_size);
2755         if (errorType != SA_INTERNAL_OK)
2756                 goto error;
2757
2758         /* exchange Msg */
2759         errorType = _exchange_msg(static_session, DEFINE_PROTOCOL_TYPE, transport_type, msg, msg_size, &recv_msg, &recv_msg_size);
2760         if (errorType != SA_INTERNAL_OK) {
2761                 _DEBUG_ERROR("errorType = %d", errorType);
2762                 goto error;
2763         }
2764
2765         errorType = _process_recv_msg(static_session, recv_msg, recv_msg_size, 0, NULL, NULL);
2766         if (errorType != SA_INTERNAL_OK) {
2767                 _DEBUG_ERROR("errorType = %d", errorType);
2768                 goto error;
2769         }
2770
2771  error:
2772
2773         if (msg != NULL) {
2774                 free(msg);
2775                 msg = NULL;
2776         }
2777
2778         if (recv_msg != NULL) {
2779                 free(recv_msg);
2780                 recv_msg = NULL;
2781         }
2782
2783         if (errorType != SA_INTERNAL_OK)
2784                 _DEBUG_ERROR("errorType =%d", errorType);
2785
2786         _EXTERN_FUNC_EXIT;
2787         return _convert_error_type(errorType);
2788 }
2789
2790 int cancel_connection_sync_request(int transport_type)
2791 {
2792         _EXTERN_FUNC_ENTER;
2793
2794         sa_error_type_e err = SA_INTERNAL_OK;
2795         sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
2796
2797         if (static_session != NULL) {
2798                 _DEBUG_INFO("sessionId = %d", static_session->naci_session_id);
2799
2800                 res = sync_agent_cancel_msg(transport_type, static_session->naci_session_id);
2801                 if (res != SYNC_AGENT_NA_SUCCESS) {
2802                         _DEBUG_ERROR("res = %d", res);
2803                         err = SA_INTERNAL_CONNECTION_ERROR;
2804                         goto error;
2805                 }
2806         }
2807
2808  error:
2809
2810         _EXTERN_FUNC_EXIT;
2811         return _convert_error_type(err);
2812 }