libsyncml  0.5.4
transport_http_client.c
1 /*
2  * libsyncml - A syncml protocol implementation
3  * Copyright (C) 2008-2009 Michael Bell <michael.bell@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "transport.h"
22 #include "libsyncml/sml_error_internals.h"
23 #include "libsyncml/sml_support.h"
24 
25 #include "data_sync_client.h"
26 #include "data_sync_devinf.h"
27 
28 
29 SmlBool smlDataSyncTransportHttpClientInit(
30  SmlDataSyncObject *dsObject,
31  SmlError **error)
32 {
33  smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, dsObject, error);
34  CHECK_ERROR_REF
35 
36  if (dsObject->url != NULL &&
38  dsObject->tsp,
39  SML_TRANSPORT_CONFIG_URL, dsObject->url,
40  error))
41  goto error;
42  if (dsObject->username != NULL &&
44  dsObject->tsp,
45  SML_TRANSPORT_CONFIG_USERNAME, dsObject->username,
46  error))
47  goto error;
48  if (dsObject->password != NULL &&
50  dsObject->tsp,
51  SML_TRANSPORT_CONFIG_PASSWORD, dsObject->password,
52  error))
53  goto error;
54 
55  smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
56  return TRUE;
57 error:
58  smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
59  return FALSE;
60 }
61 
62 static SmlBool smlDataSyncTransportHttpClientInitNewSession (SmlDataSyncObject *dsObject, SmlError **error)
63 {
64  smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, dsObject, error);
65  smlAssert(dsObject->url);
66  smlAssert(dsObject->identifier);
67  smlAssert(dsObject->tsp);
68  smlAssert(dsObject->manager);
69  smlAssert(dsObject->agent);
70 
71  SmlLocation *target = NULL;
72  SmlLocation *source = NULL;
73 
74  /* If there is an old session then this is a bug.
75  * the high level API only support one session per
76  * OMA DS object.
77  */
78  if (dsObject->session) {
79  smlErrorSet(error, SML_ERROR_GENERIC,
80  "Only one SyncML session is allowed per SmlDataSyncObject instance.");
81  goto error;
82  }
83 
84  /* create session */
85 
86  target = smlLocationNew(dsObject->target, NULL, error);
87  if (!target)
88  goto error;
89  source = smlLocationNew(dsObject->identifier, NULL, error);
90  if (!source)
91  goto error;
92 
93  char *sessionString = smlManagerGetNewSessionID(dsObject->manager);
94  dsObject->session = smlSessionNew(SML_SESSION_TYPE_CLIENT,
95  dsObject->useWbxml?SML_MIMETYPE_WBXML:SML_MIMETYPE_XML,
96  dsObject->version,
97  SML_PROTOCOL_SYNCML,
98  target, source,
99  sessionString, 0, error);
100  smlSafeCFree(&sessionString);
101  if (!dsObject->session)
102  goto error;
103 
104  smlLocationUnref(target);
105  target = NULL;
106  smlLocationUnref(source);
107  source = NULL;
108 
109  /* register all the add-ons */
110  if (!smlManagerSessionAdd(dsObject->manager, dsObject->session, NULL, error))
111  goto error;
112  if (!smlDevInfAgentRegisterSession(dsObject->agent, dsObject->manager, dsObject->session, error))
113  goto error;
114 
115  smlTrace(TRACE_EXIT, "%s", __func__);
116  return TRUE;
117 error:
118  if (target) {
119  smlLocationUnref(target);
120  target = NULL;
121  }
122  if (source) {
123  smlLocationUnref(source);
124  source = NULL;
125  }
126  smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
127  return FALSE;
128 }
129 
130 SmlBool smlDataSyncTransportHttpClientConnect(SmlDataSyncObject *dsObject, SmlError **error)
131 {
132  smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, dsObject, error);
133  CHECK_ERROR_REF
134 
135  /* init new session */
136  if (!smlDataSyncTransportHttpClientInitNewSession(dsObject, error))
137  goto error;
138 
139  /* send the device information */
140  if (!smlDataSyncManageDevInf(dsObject, TRUE, error))
141  goto error;
142  smlTrace(TRACE_INTERNAL, "%s: sent devinf", __func__);
143 
144  /* prepare correct alert type */
145  GList *o = dsObject->datastores;
146  for (; o; o = o->next) {
147  SmlDataSyncDatastore *datastore = o->data;
148 
149  if (!smlDataSyncClientSendAlert(datastore, SML_ALERT_UNKNOWN, error))
150  goto error;
151  }
152  smlTrace(TRACE_INTERNAL, "%s: all datastores created their alerts", __func__);
153 
154  /* If no SAN and no server alerted sync is used
155  * then we must set the actual package to 1.
156  * Otherwise the state management thinks the first
157  * received final is part of a SAN package.
158  */
159  dsObject->actualPackage = SML_PACKAGE_1;
160 
161  /* flush package 1 */
162  if (!smlSessionFlush(dsObject->session, TRUE, error))
163  goto error;
164 
165  smlTrace(TRACE_EXIT, "%s", __func__);
166  return TRUE;
167 error:
168  smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
169  return FALSE;
170 }
171