File: c_example.c

package info (click to toggle)
obexftp 0.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792 kB
  • sloc: ansic: 7,735; ruby: 229; sh: 89; perl: 43; makefile: 39; python: 16
file content (78 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (8)
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
/*
	Minimal ObexFTP C client example
	Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net>
	Compile with:
	gcc -Wall $(pkg-config --libs obexftp) -o c_example c_example.c
 */

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

#include <obexftp/client.h> /*!!!*/

int main(int argc, char *argv[])
{
	char *device;
	int channel;
	char *filename = NULL;
	obexftp_client_t *cli = NULL; /*!!!*/ 
	int ret;

	/* Get device, channel and optional filename */
	if (argc < 3) {
		fprintf(stderr, "Usage: %s <bt_addr> <channel> [<filename>]\n", argv[0]);
		exit(1);
	}
	device = argv[1];
       	channel = atoi(argv[2]);
	if (argc > 3) {
		filename = argv[3];
	}

	/* Open connection */
	cli = obexftp_open(OBEX_TRANS_BLUETOOTH, NULL, NULL, NULL); /*!!!*/ 
	if (cli == NULL) {
		fprintf(stderr, "Error opening obexftp client\n");
		exit(1);
	}

	/* Connect to device */
	ret = obexftp_connect(cli, device, channel); /*!!!*/ 
	if (ret < 0) {
		fprintf(stderr, "Error connecting to obexftp device\n");
		obexftp_close(cli);
		cli = NULL;
		exit(1);
	}

	if (filename == NULL) {
		/* List folder */
		ret = obexftp_list(cli, NULL, "/"); /*!!!*/ 
		if (ret < 0) {
			fprintf(stderr, "Error getting a folder listing\n");
		} else {
			printf("%s\n", cli->buf_data); /*!!!*/ 
		}

	} else {
		/* Get file */
		ret = obexftp_get(cli, NULL, filename); /*!!!*/ 
		if (ret < 0) {
			fprintf(stderr, "Error getting a file\n");
		} else {
			/* do something with cli->buf_data and cli->buf_size */
		}
	}

	/* Disconnect */
	ret = obexftp_disconnect(cli); /*!!!*/ 
       	if (ret < 0) {
       		fprintf(stderr, "Error disconnecting the client\n");
       	}

	/* Close */
	obexftp_close(cli); /*!!!*/
	cli = NULL;

	exit(0);
}