]> git.karo-electronics.de Git - oswald.git/blob - linux-bt/l2cap_client.c
Handle newline
[oswald.git] / linux-bt / l2cap_client.c
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 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
59 int main(int argc, char **argv)
60 {
61         struct sockaddr_l2 addr = { 0 };
62         int s, status, len, i, cmd;
63         char buf[255];
64         char dest[18];
65         char *out;
66         fd_set infds;
67         fd_set efds;
68
69         if (argc < 2) {
70                 fprintf(stderr, "usage: %s <bt_addr>\n", argv[0]);
71                 exit(2);
72         }
73
74         strncpy(dest, argv[1], 18);
75
76         // allocate a socket
77         s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
78
79         // set the connection parameters (who to connect to)
80         addr.l2_family = AF_BLUETOOTH;
81         addr.l2_psm = htobs(0x1001);
82         str2ba( dest, &addr.l2_bdaddr );
83
84         // connect to server
85         status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
86
87         // send a message
88         if ( status != 0 ) {
89                 perror("open socket");
90                 close(s);
91                 return -1;
92         }
93
94         fprintf(stderr, "connected\n");
95
96         if (fcntl(0, F_SETFL, O_NONBLOCK) != 0)
97                 perror("stdin nonblock");
98         if (fcntl(s, F_SETFL, O_NONBLOCK) != 0)
99                 perror("socket nonblock");
100
101         while (1) {
102                 FD_ZERO(&infds);
103                 FD_SET(s, &infds);
104                 FD_SET(0, &infds);
105                 FD_ZERO(&efds);
106                 FD_SET(s, &efds);
107                 status = select(s+1, &infds, NULL, &efds, NULL);
108                 // fprintf(stderr, "select = %d\n", status);
109                 if ( status > 0) {
110                         if (FD_ISSET(0, &infds)) {
111                                 len = read(0, buf, 240);
112                                 if (len > 0) {
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                                         }
127                                 }
128                         } else if (FD_ISSET(s, &infds)) {
129                                 len = read(s, buf, 240);
130                                 // fprintf(stderr, "read = %d\n", len);
131                                 for (i=0; i<len; i++)
132                                         fprintf(stderr, "%c", buf[i]);
133                                 // fprintf(stderr, "\n");
134                         } else if (FD_ISSET(s, &efds)) {
135                                 fprintf(stderr, "e on socket\n");
136                         } else
137                                 fprintf(stderr, "hu?");
138                 } else
139                         perror("select");
140         };
141
142
143         close(s);
144
145         return 0;
146 }
147