File: multi_cobex.c

package info (click to toggle)
obexftp 0.24-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 680 kB
  • ctags: 620
  • sloc: ansic: 7,132; ruby: 229; sh: 89; perl: 43; makefile: 27; python: 16
file content (348 lines) | stat: -rwxr-xr-x 8,245 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
/**
	\file multicobex/multi_cobex.c
	Detect, initiate and run OBEX over custom serial port protocols
	(Siemens, Ericsson, New-Siemens, Motorola, Generic).
	ObexFTP library - language bindings for OBEX file transfer.

	Copyright (c) 2002-2007 Christian W. Zuckschwerdt <zany@triq.net>

	ObexFTP 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 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 ObexFTP. If not, see <http://www.gnu.org/>.
 */

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

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <fcntl.h>
#include <errno.h>

#ifdef _WIN32
#include <windows.h>
#define sleep(t) Sleep((t) * 1000)
#define usleep(t) Sleep((t) < 500 ? 1 : ((t) + 500) / 1000);
#else
#include <sys/ioctl.h>
#include <termios.h>
#endif

#include <openobex/obex.h>
#include "multi_cobex.h"
#include "multi_cobex_private.h"
#include <bfb/bfb.h>
#include <bfb/bfb_io.h>
#include <common.h>

static void cobex_cleanup(cobex_t *c, int force)
{
        return_if_fail (c != NULL);
#ifdef _WIN32
        return_if_fail (c->fd != INVALID_HANDLE_VALUE);
#else
        return_if_fail (c->fd > 0);
#endif

	if (c->type == CT_BFB) {
		/* needed to leave transparent OBEX(3) mode 
		 * and enter RCCP(0) mode. 
		 * DCA-540 needs cable replug, probably problem 
		 * with the linux driver (linux-2.6.17.13) 
		 */
		bfb_write_at(c->fd, "AT^SBFB=0\r");
		sleep(1);
		bfb_io_write(c->fd, "+++", 3);
		sleep(1);
		bfb_io_write(c->fd,"\r", 1);
	}
       	bfb_io_close(c->fd, force);

#ifdef _WIN32
	c->fd = INVALID_HANDLE_VALUE;
#else
	c->fd = -1;
#endif
}

/**
	Called from OBEX-lib to set up a connection.
 */
int cobex_connect(obex_t *self, void *data)
{
	cobex_t *c;
	enum trans_type typeinfo;
        return_val_if_fail (self != NULL, -1);
        return_val_if_fail (data != NULL, -1);
	c = (cobex_t *) data;

	DEBUG(3, "%s() \n", __func__);

	c->fd = bfb_io_open(c->tty, &typeinfo);
	DEBUG(3, "%s() bfb_io_open returned %d, %d\n", __func__, c->fd, typeinfo);
	switch (typeinfo) {
	case TT_BFB:
		c->type = CT_BFB;
		break;
	case TT_ERICSSON:
		c->type = CT_ERICSSON;
		break;
	case TT_SIEMENS:
		c->type = CT_SIEMENS;
		break;
	case TT_MOTOROLA:
		c->type = CT_MOTOROLA;
		break;
	case TT_GENERIC:
		c->type = CT_GENERIC;
		break;
	default:
		c->type = 0; /* invalid */
		return -1;
	}

#ifdef _WIN32
	if(c->fd == INVALID_HANDLE_VALUE)
#else
	if(c->fd == -1)
#endif
		return -1;

	return 1;
}

/**
	Called from OBEX-lib to tear down a connection.
 */
int cobex_disconnect(obex_t *self, void *data)
{
	cobex_t *c;
        return_val_if_fail (self != NULL, -1);
        return_val_if_fail (data != NULL, -1);
	c = (cobex_t *) data;

	DEBUG(3, "%s() \n", __func__);
	cobex_cleanup(c, FALSE);
	return 1;
}

/**
	Called from OBEX-lib when data needs to be written.
 */
int cobex_write(obex_t *self, void *data, uint8_t *buffer, int length)
{
	int written;
	cobex_t *c;
        return_val_if_fail (self != NULL, -1);
        return_val_if_fail (data != NULL, -1);
	c = (cobex_t *) data;
	
	DEBUG(3, "%s() \n", __func__);

	DEBUG(3, "%s() Data %d bytes\n", __func__, length);

	if (c->type != CT_BFB) {
		int retries=0, chunk, fails=0;
		written = 0;
		for (retries = 0; written < length; retries++) {
			chunk = write(c->fd, buffer+written, length-written);
			if (chunk <= 0) {
				if ( ++fails >= 10 ) { // to avoid infinite looping if something is really wrong
					DEBUG(1, "%s() Error writing to port (written %d bytes out of %d, in %d retries)\n", __func__, written, length, retries);
					return written;
				}
				usleep(1); // This mysteriously avoids a resource not available error on write()
			} else {
				written += chunk;
				fails = 0; // Reset error counter on successful write op
			}
		}
		
		if (retries > 0)
			DEBUG(2, "%s() Wrote %d bytes in %d retries\n", __func__, written, retries);
		return written;
	}

	if (c->seq == 0){
		written = bfb_send_first(c->fd, buffer, length);
		DEBUG(2, "%s() Wrote %d first packets (%d bytes)\n", __func__, written, length);
	} else {
		written = bfb_send_next(c->fd, buffer, length, c->seq);
		DEBUG(2, "%s() Wrote %d packets (%d bytes)\n", __func__, written, length);
	}
	c->seq++;

	return written;
}

/**
	Called when input data is needed.
 */
int cobex_handleinput(obex_t *self, void *data, int timeout)
{
#ifdef _WIN32
	DWORD actual;
#else
	struct timeval time;
	fd_set fdset;
	int actual;
#endif
	bfb_frame_t *frame;

	cobex_t *c;

        return_val_if_fail (self != NULL, -1);
        return_val_if_fail (data != NULL, -1);
	c = (cobex_t *) data;

#ifdef _WIN32
	if (!ReadFile(c->fd, &(c->recv[c->recv_len]), sizeof(c->recv) - c->recv_len, &actual, NULL))
		DEBUG(2, "%s() Read error: %ld\n", __func__, actual);

	DEBUG(2, "%s() Read %ld bytes (%d bytes already buffered)\n", __func__, actual, c->recv_len);
	/* FIXME ... */
#else
	time.tv_sec = timeout;
	time.tv_usec = 0;

	/* Add the fd's to the set. */
	FD_ZERO(&fdset);
	FD_SET(c->fd, &fdset);

	/* Wait for input */
	actual = select(c->fd + 1, &fdset, NULL, NULL, &time);

	DEBUG(2, "%s() There is something (%d)\n", __func__, actual);

	/* Check if this is a timeout (0) or error (-1) */
	if(actual <= 0)
		return actual;

	actual = read(c->fd, &(c->recv[c->recv_len]), sizeof(c->recv) - c->recv_len);
	DEBUG(2, "%s() Read %d bytes (%d bytes already buffered)\n", __func__, actual, c->recv_len);
#endif

	if (c->type != CT_BFB) {
		if (actual > 0) {
			OBEX_CustomDataFeed(self, c->recv, actual);
			return 1;
		}
		return actual;
	}

	if ((c->data_buf == NULL) || (c->data_size == 0)) {
		c->data_size = 1024;
		c->data_buf = malloc(c->data_size);
	}

	if (actual > 0) {
		c->recv_len += actual;
		DEBUGBUFFER(c->recv, c->recv_len);

		while ((frame = bfb_read_packets(c->recv, &(c->recv_len)))) {
			DEBUG(2, "%s() Parsed %x (%d bytes remaining)\n", __func__, frame->type, c->recv_len);

			(void) bfb_assemble_data(&c->data_buf, &c->data_size, &c->data_len, frame);

			if (bfb_check_data(c->data_buf, c->data_len) == 1) {
				actual = bfb_send_ack(c->fd);
#ifdef _WIN32
				DEBUG(2, "%s() Wrote ack packet (%ld)\n", __func__, actual);
#else
				DEBUG(2, "%s() Wrote ack packet (%d)\n", __func__, actual);
#endif

				OBEX_CustomDataFeed(self, c->data_buf->data, c->data_len-7);
				/*
				free(c->data);
				c->data = NULL;
				*/
				c->data_len = 0;

				if (c->recv_len > 0) {
					DEBUG(2, "%s() Data remaining after feed, this can't be good.\n", __func__);
					DEBUGBUFFER(c->recv, c->recv_len);
				}

				return 1;
			}
		}

	}
	return actual;
}


/* Well, maybe this should be somewhere in the header */
/*
static obex_ctrans_t _cobex_ctrans = {
	connect: cobex_connect,
	disconnect: cobex_disconnect,
	write: cobex_write,
	listen: NULL,
	handleinput: cobex_handleinput,
};
*/

/**
	Create a new multi cobex instance for a given TTY.

	\param tty the TTY to use. Defaults to the first serial TTY if NULL.
 */
obex_ctrans_t *cobex_ctrans (const char *tty) {
	obex_ctrans_t *ctrans;
	cobex_t *cobex;

	cobex = calloc (1, sizeof(cobex_t));
	if(tty == NULL)
		tty = SERPORT;
	cobex->tty = strdup (tty);

	ctrans = calloc (1, sizeof(obex_ctrans_t));
	ctrans->connect = cobex_connect;
	ctrans->disconnect = cobex_disconnect;
	ctrans->write = cobex_write;
	ctrans->listen = NULL;
	ctrans->handleinput = cobex_handleinput;
	ctrans->customdata = cobex;
	
	return ctrans;
}

/**
	Free all data related to a multi cobex instance.
 */
void cobex_free (obex_ctrans_t *ctrans)
{
	cobex_t *cobex;

	return_if_fail (ctrans != NULL);

	cobex = (cobex_t *)ctrans->customdata;

	return_if_fail (cobex != NULL);

	free (cobex->tty);
	/* maybe there is a bfb_data_t left? */
	/* free(cobex->data); */

	free (cobex);
	free (ctrans);

	return;
}