Browse code

Add Linux Bluetooth test application

Nils Faerber authored on 04/05/2013 22:30:36
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+CFLAGS = -Wall -O2
2
+SRC = l2cap_client.c
3
+PRG = l2cap_client
4
+
5
+OBJS = $(SRC:.c=.o)
6
+
7
+all: $(PRG)
8
+
9
+$(PRG): $(OBJS)
10
+	$(CC) -o $(PRG) $(OBJS) -lbluetooth
11
+
12
+%.o: %c
13
+	$(CC) -c $(CFLAGS) -o $@ $<
14
+
15
+clean:
16
+	rm -f $(OBJS) $(PRG)
0 17
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+
2
+l2cap_client
3
+
4
+Is a small test application to test the Bluetooth connection to
5
+Oswald-MetaWatch. It opens a L2CAP connection to a give Bluetooth address.
6
+After successful connection establishment it output all data received to
7
+stdout and send data received from stdin (keyboard) to the watch. Input via
8
+stdin is not in cbreak mode, i.e. input must be finished with CR before it
9
+is sent to the watch.
10
+
11
+Building
12
+Simply type "make". Depend on libbluetooth and its development files.
13
+
14
+Running
15
+Start with Bluetooth address of the watch as cmdline argument:
16
+	l2cap_client 11:22:33:44:55:66
0 17
new file mode 100644
... ...
@@ -0,0 +1,95 @@
1
+/*
2
+ * (c) 2013 Nils Faerber, Siegen, Germany
3
+ * licensed under GPLv2
4
+ */
5
+
6
+#include <stdio.h>
7
+#include <string.h>
8
+#include <unistd.h>
9
+#include <stdlib.h>
10
+#include <sys/select.h>
11
+#include <sys/time.h>
12
+#include <sys/types.h>
13
+#include <sys/socket.h>
14
+#include <fcntl.h>
15
+#include <bluetooth/bluetooth.h>
16
+#include <bluetooth/l2cap.h>
17
+
18
+int main(int argc, char **argv)
19
+{
20
+	struct sockaddr_l2 addr = { 0 };
21
+	int s, status, len, i;
22
+	char buf[255];
23
+	char dest[18];
24
+	fd_set infds;
25
+	fd_set efds;
26
+
27
+	if (argc < 2) {
28
+		fprintf(stderr, "usage: %s <bt_addr>\n", argv[0]);
29
+		exit(2);
30
+	}
31
+
32
+	strncpy(dest, argv[1], 18);
33
+
34
+	// allocate a socket
35
+	s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
36
+
37
+	// set the connection parameters (who to connect to)
38
+	addr.l2_family = AF_BLUETOOTH;
39
+	addr.l2_psm = htobs(0x1001);
40
+	str2ba( dest, &addr.l2_bdaddr );
41
+
42
+	// connect to server
43
+	status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
44
+
45
+	// send a message
46
+	if ( status != 0 ) {
47
+		perror("open socket");
48
+		close(s);
49
+		return -1;
50
+	}
51
+
52
+	fprintf(stderr, "connected\n");
53
+
54
+	if (fcntl(0, F_SETFL, O_NONBLOCK) != 0)
55
+		perror("stdin nonblock");
56
+	if (fcntl(s, F_SETFL, O_NONBLOCK) != 0)
57
+		perror("socket nonblock");
58
+
59
+	while (1) {
60
+		FD_ZERO(&infds);
61
+		FD_SET(s, &infds);
62
+		FD_SET(0, &infds);
63
+		FD_ZERO(&efds);
64
+		FD_SET(s, &efds);
65
+		status = select(s+1, &infds, NULL, &efds, NULL);
66
+		// fprintf(stderr, "select = %d\n", status);
67
+		if ( status > 0) {
68
+			if (FD_ISSET(0, &infds)) {
69
+				len = read(0, buf, 240);
70
+				if (len > 0) {
71
+					status = write(s, buf, len);
72
+					if (status < 0)
73
+						perror("write");
74
+					fsync(s);
75
+				}
76
+			} else if (FD_ISSET(s, &infds)) {
77
+				len = read(s, buf, 240);
78
+				// fprintf(stderr, "read = %d\n", len);
79
+				for (i=0; i<len; i++)
80
+					fprintf(stderr, "%c", buf[i]);
81
+				// fprintf(stderr, "\n");
82
+			} else if (FD_ISSET(s, &efds)) {
83
+				fprintf(stderr, "e on socket\n");
84
+			} else
85
+				fprintf(stderr, "hu?");
86
+		} else
87
+			perror("select");
88
+	};
89
+
90
+
91
+	close(s);
92
+
93
+	return 0;
94
+}
95
+