... | ... |
@@ -15,12 +15,54 @@ |
15 | 15 |
#include <bluetooth/bluetooth.h> |
16 | 16 |
#include <bluetooth/l2cap.h> |
17 | 17 |
|
18 |
+typedef enum { |
|
19 |
+ CMD_NULL = 0, |
|
20 |
+ CMD_INVAL, |
|
21 |
+ CMD_QUIT, |
|
22 |
+} cmd_t; |
|
23 |
+ |
|
24 |
+// replace "markups" with appropr. chars |
|
25 |
+char *parse_buffer(char *inbuf, int *cmd) |
|
26 |
+{ |
|
27 |
+ int i,o; |
|
28 |
+ static char outbuf[240]; |
|
29 |
+ |
|
30 |
+ memset(outbuf,0,240); |
|
31 |
+ |
|
32 |
+ *cmd = CMD_NULL; |
|
33 |
+ |
|
34 |
+ // if a line starts with a \ then it is an internal command |
|
35 |
+ if (inbuf[0] == '\\') { |
|
36 |
+ switch (inbuf[1]) { |
|
37 |
+ case 'q': |
|
38 |
+ *cmd = CMD_QUIT; |
|
39 |
+ break; |
|
40 |
+ default: |
|
41 |
+ *cmd = CMD_INVAL; |
|
42 |
+ break; |
|
43 |
+ } |
|
44 |
+ return NULL; |
|
45 |
+ } |
|
46 |
+ o=0; |
|
47 |
+ for (i=0; i<strlen(inbuf); i++) { |
|
48 |
+ if (inbuf[i] == '\\') { |
|
49 |
+ i++; |
|
50 |
+ if (inbuf[i] == 'n') |
|
51 |
+ outbuf[o++] = '\n'; |
|
52 |
+ } else |
|
53 |
+ outbuf[o++] = inbuf[i]; |
|
54 |
+ } |
|
55 |
+ return outbuf; |
|
56 |
+} |
|
57 |
+ |
|
58 |
+ |
|
18 | 59 |
int main(int argc, char **argv) |
19 | 60 |
{ |
20 | 61 |
struct sockaddr_l2 addr = { 0 }; |
21 |
- int s, status, len, i; |
|
62 |
+ int s, status, len, i, cmd; |
|
22 | 63 |
char buf[255]; |
23 | 64 |
char dest[18]; |
65 |
+ char *out; |
|
24 | 66 |
fd_set infds; |
25 | 67 |
fd_set efds; |
26 | 68 |
|
... | ... |
@@ -68,10 +110,20 @@ int main(int argc, char **argv) |
68 | 110 |
if (FD_ISSET(0, &infds)) { |
69 | 111 |
len = read(0, buf, 240); |
70 | 112 |
if (len > 0) { |
71 |
- status = write(s, buf, len); |
|
72 |
- if (status < 0) |
|
73 |
- perror("write"); |
|
74 |
- fsync(s); |
|
113 |
+ // status = write(s, buf, len); |
|
114 |
+ out = parse_buffer(buf, &cmd); |
|
115 |
+ if (cmd != CMD_NULL) { |
|
116 |
+ if (cmd == CMD_QUIT) { |
|
117 |
+ close(s); |
|
118 |
+ exit(0); |
|
119 |
+ } |
|
120 |
+ } else { |
|
121 |
+ len = strlen(out); |
|
122 |
+ status = write(s, out, len); |
|
123 |
+ if (status < 0) |
|
124 |
+ perror("write"); |
|
125 |
+ fsync(s); |
|
126 |
+ } |
|
75 | 127 |
} |
76 | 128 |
} else if (FD_ISSET(s, &infds)) { |
77 | 129 |
len = read(s, buf, 240); |
1 | 1 |
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 |
+ |