File: obex_client.c

package info (click to toggle)
libopenobex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 972 kB
  • ctags: 1,552
  • sloc: ansic: 9,988; xml: 407; makefile: 96
file content (354 lines) | stat: -rw-r--r-- 8,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
	\file obex_client.c
	Handle client operations.
	OpenOBEX library - Free implementation of the Object Exchange protocol.

	Copyright (c) 1999-2000 Pontus Fuchs, All Rights Reserved.

	OpenOBEX is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation; either version 2.1 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with OpenOBEX. If not, see <http://www.gnu.org/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "obex_main.h"
#include "obex_object.h"
#include "obex_connect.h"
#include "obex_client.h"
#include "obex_msg.h"
#include "databuffer.h"

#include <stdlib.h>
#include <stdio.h>

static __inline enum obex_rsp msg_get_rsp(obex_t *self)
{
	int opcode = obex_msg_get_opcode(self);

	if (opcode < 0)
		return OBEX_RSP_BAD_REQUEST;
	else
		return opcode & ~OBEX_FINAL;
}

static __inline uint16_t msg_get_len(const buf_t *msg)
{
	if (msg) {
		obex_common_hdr_t *hdr = buf_get(msg);
		return ntohs(hdr->len);
	} else
		return 0;
}

static result_t obex_client_abort_tx(obex_t *self)
{
	self->substate = SUBSTATE_RX;
	return RESULT_SUCCESS;
}

static result_t obex_client_abort_tx_prepare(obex_t *self)
{
	DEBUG(4, "STATE: ABORT/TX_PREPARE\n");

	if (!obex_data_request_init(self))
		return RESULT_ERROR;

	obex_data_request_prepare(self, OBEX_CMD_ABORT);
	self->substate = SUBSTATE_TX;
	return RESULT_SUCCESS;
}

static result_t obex_client_abort_rx(obex_t *self)
{
	int ret = RESULT_SUCCESS;
	enum obex_rsp rsp;
	int event = OBEX_EV_LINKERR;

	DEBUG(4, "STATE: ABORT/RX\n");

	if (!obex_msg_rx_status(self))
		return RESULT_SUCCESS;
	rsp = msg_get_rsp(self);

	if (rsp == OBEX_RSP_SUCCESS)
		event = OBEX_EV_ABORT;
	obex_deliver_event(self, event, self->object->cmd, rsp, true);
	if (event == OBEX_EV_LINKERR)
		ret = RESULT_ERROR;

	self->mode = OBEX_MODE_SERVER;
	self->state = STATE_IDLE;
	return ret;
}

static result_t obex_client_response_tx(obex_t *self)
{
	enum obex_cmd cmd = self->object->cmd;
	enum obex_rsp rsp = OBEX_RSP_CONTINUE;

	obex_deliver_event(self, OBEX_EV_PROGRESS, cmd, rsp, false);
	self->substate = SUBSTATE_RX;

	return RESULT_SUCCESS;
}

static result_t obex_client_response_tx_prepare(obex_t *self)
{
	DEBUG(4, "STATE: RESPONSE/TX_PREPARE\n");

	/* Sending ABORT is allowed even during SRM */
	if (self->object->abort) {
		self->state = STATE_ABORT;
		return obex_client_abort_tx_prepare(self);
	}

	if (self->object->rsp_mode == OBEX_RSP_MODE_NORMAL ||
	    (self->object->rsp_mode == OBEX_RSP_MODE_SINGLE &&
	     self->srm_flags & OBEX_SRM_FLAG_WAIT_REMOTE))
	{
		if (!obex_msg_prepare(self, self->object, TRUE))
			return RESULT_ERROR;

		self->substate = SUBSTATE_TX;

	} else {
		self->substate = SUBSTATE_RX;
	}

	return RESULT_SUCCESS;
}

static result_t obex_client_response_rx(obex_t *self)
{
	enum obex_rsp rsp;

	DEBUG(4, "STATE: RESPONSE/RX\n");

	if (!obex_msg_rx_status(self))
		return RESULT_SUCCESS;
	rsp = msg_get_rsp(self);

	switch (self->object->cmd) {
	case OBEX_CMD_CONNECT:
		DEBUG(2, "We expect a connect-rsp\n");
		self->object->headeroffset=4;
		break;

	case OBEX_CMD_DISCONNECT:
		/* Response of a CMD_DISCONNECT needs some special treatment.*/
		DEBUG(2, "CMD_DISCONNECT done. Resetting MTU!\n");
		self->mtu_tx = OBEX_MINIMUM_MTU;
		self->rsp_mode = OBEX_RSP_MODE_NORMAL;
		self->srm_flags = 0;
		break;

	default:
		break;
	}

	if (!self->object->abort) {
		/* Receive any headers */
		result_t ret = obex_msg_receive(self, self->object);
		if (ret == RESULT_ERROR) {
			obex_deliver_event(self, OBEX_EV_PARSEERR,
					   self->object->cmd, 0, true);
			self->mode = OBEX_MODE_SERVER;
			self->state = STATE_IDLE;
			obex_data_receive_finished(self);
			return RESULT_ERROR;
		}
	}
	obex_data_receive_finished(self);

	/* Response of a CMD_CONNECT needs some special treatment.*/
	if (self->object->cmd == OBEX_CMD_CONNECT) {
		DEBUG(2, "We expect a connect-rsp\n");
		if (rsp != OBEX_RSP_SUCCESS ||
		    obex_parse_connectframe(self, self->object) < 0)
		{
			obex_deliver_event(self, OBEX_EV_PARSEERR,
					   self->object->cmd, 0, true);
			self->mode = OBEX_MODE_SERVER;
			self->state = STATE_IDLE;
			return RESULT_ERROR;
		}
	}

	/* Are we done yet? */
	if (rsp == OBEX_RSP_CONTINUE) {
		enum obex_cmd cmd = self->object->cmd;

		DEBUG(3, "Continue...\n");
		obex_deliver_event(self, OBEX_EV_CONTINUE, cmd, rsp, false);

		/* Return if the user cancelled the request without sending ABORT.
		 * If ABORT is being sent, we continue as usual and let the normal
		 * tx_prepare() handle it.
		 */
		if (self->object == NULL)
			return RESULT_SUCCESS;

		self->substate = SUBSTATE_TX_PREPARE;
		return obex_client_response_tx_prepare(self);

	} else {
		enum obex_cmd cmd = self->object->cmd;

		/* Notify app that client-operation is done! */
		DEBUG(3, "Done! Rsp=%02x!\n", rsp);
		obex_deliver_event(self, OBEX_EV_REQDONE, cmd, rsp, true);
		self->mode = OBEX_MODE_SERVER;
		self->state = STATE_IDLE;
		return RESULT_SUCCESS;
	}
}

static result_t obex_client_request_tx(obex_t *self)
{
	obex_deliver_event(self, OBEX_EV_PROGRESS, self->object->cmd, 0, false);
	if (obex_object_finished(self->object, TRUE))
		self->state = STATE_RESPONSE;

	self->substate = SUBSTATE_RX;
	return RESULT_SUCCESS;
}

static result_t obex_client_request_tx_prepare(obex_t *self)
{
	DEBUG(4, "STATE: REQUEST/TX_PREPARE\n");

	if (self->object->abort) {
		self->state = STATE_ABORT;
		return obex_client_abort_tx_prepare(self);
	}

	if (!obex_msg_prepare(self, self->object, TRUE))
		return RESULT_ERROR;

	self->substate = SUBSTATE_TX;
	return RESULT_SUCCESS;
}

static result_t obex_client_request_rx(obex_t *self)
{
	enum obex_rsp rsp;

	DEBUG(4, "STATE: REQUEST/RX\n");

	if (!obex_msg_rx_status(self))
		return RESULT_SUCCESS;
	rsp = msg_get_rsp(self);

	/* Any errors from peer? Win2k will send RSP_SUCCESS after
	 * every fragment sent so we have to accept that too.*/
	switch (rsp) {
	case OBEX_RSP_SUCCESS:
	case OBEX_RSP_CONTINUE:
		break;

	default:
		DEBUG(0, "STATE_SEND. request not accepted.\n");
		obex_deliver_event(self, OBEX_EV_REQDONE, self->object->cmd,
								     rsp, true);
		/* This is not an Obex error, it is just that the peer
		 * doesn't accept the request */
		obex_data_receive_finished(self);
		return RESULT_SUCCESS;
	}

	if (!self->object->abort) {
		int ret = obex_msg_receive(self, self->object);
		if (ret < 0) {
			obex_deliver_event(self, OBEX_EV_PARSEERR,
					   self->object->cmd, 0, true);
			self->mode = OBEX_MODE_SERVER;
			self->state = STATE_IDLE;
			obex_data_receive_finished(self);
			return RESULT_ERROR;
		}
	}

	obex_data_receive_finished(self);
	self->substate = SUBSTATE_TX_PREPARE;
	return obex_client_request_tx_prepare(self);
}


/*
 * Function obex_client ()
 *
 *    Handle client operations
 *
 */
result_t obex_client(obex_t *self)
{
	DEBUG(4, "\n");

	switch (self->state) {
	case STATE_REQUEST:
		switch (self->substate) {
		case SUBSTATE_RX:
			return obex_client_request_rx(self);

		case SUBSTATE_TX_PREPARE:
			return obex_client_request_tx_prepare(self);

		case SUBSTATE_TX:
			return obex_client_request_tx(self);

		default:
			break;
		}
		break;

	case STATE_RESPONSE:
		switch (self->substate) {
		case SUBSTATE_RX:
			return obex_client_response_rx(self);

		case SUBSTATE_TX_PREPARE:
			return obex_client_response_tx_prepare(self);

		case SUBSTATE_TX:
			return obex_client_response_tx(self);

		default:
			break;
		}
		break;

	case STATE_ABORT:
		switch (self->substate) {
		case SUBSTATE_RX:
			return obex_client_abort_rx(self);

		case SUBSTATE_TX_PREPARE:
			return obex_client_abort_tx_prepare(self);

		case SUBSTATE_TX:
			return obex_client_abort_tx(self);

		default:
			break;
		}
		break;

	default:
		DEBUG(0, "Unknown state\n");
		break;
	}

	return RESULT_ERROR;
}