]> git.kernelconcepts.de Git - metawatch.git/blob - mw_main.c
5dae442aa9f10780cc4e8af3bb9b0ce2ee0dc4b8
[metawatch.git] / mw_main.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * license LGPL
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <termios.h>
16 #include <ctype.h>
17
18 #include <errno.h>
19
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/rfcomm.h>
22
23 /*
24 #include <bluetooth/sdp.h>
25 #include <bluetooth/sdp_lib.h>
26 */
27
28 #include "metawatch.h"
29 #include "crc16ccitt.h"
30
31
32 void bitmap_test(int mw_fd)
33 {
34         /* a nice checker-board pattern */
35         unsigned char checkbuf[24] = {
36                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
37                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
38                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
39                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
40         };
41         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
42         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
43         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
44         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
45
46         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
47 }
48
49
50 void bitmap_read(int mw_fd)
51 {
52         int ffd, ret;
53         char rbuf[256];
54         unsigned int width, height, i, x, y;
55         unsigned int rowlength;
56         unsigned char *bmapbuf;
57         // unsigned char mw_buf[24];
58
59         ffd = open("test.pbm", O_RDONLY);
60         if (ffd < 0) {
61                 perror("open");
62                 return;
63         };
64         ret = read(ffd, rbuf, 3);
65         if (rbuf[0] != 'P' || rbuf[1] != '4') {
66                 fprintf(stderr, "not a PBM file\n");
67                 return;
68         }
69         memset(rbuf, 0, 256);
70         i = 0;
71         do {
72                 ret = read(ffd, (rbuf+i), 1);
73         } while (!isspace(rbuf[i++]));
74         width = atoi(rbuf);
75
76         memset(rbuf, 0, 256);
77         i = 0;
78         do {
79                 ret = read(ffd, (rbuf+i), 1);
80         } while (!isspace(rbuf[i++]));
81         height = atoi(rbuf);
82
83         rowlength = ((width / 8) + 1);
84
85         bmapbuf = malloc(rowlength * height);
86
87         ret = read(ffd, bmapbuf, rowlength * height);
88         close(ffd);
89
90 #ifdef DEBUG
91         fprintf(stderr, "row length = %d bytes\n", rowlength);
92         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
93         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
94         fprintf(stderr, "\n");
95         for (y=0; y<height; y++) {
96                 for (x=0; x<rowlength; x++) {
97                         for (i=0; i<8; i++)
98                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
99                 }
100                 fprintf(stderr, "\n");
101         }
102         fprintf(stderr, "\n");
103 #endif
104
105         /* reverse bits and invert the bmap */
106         bmap_buffer_flipinvert(1, 0, bmapbuf, rowlength * height);
107         /* send the buffer to the watch */
108         mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
109         /* update the display */
110         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
111
112         free(bmapbuf);
113 }
114
115 void print_help(void)
116 {
117 }
118
119 void process_cmd(char *cmdline, int clinep, int mw_fd)
120 {
121         unsigned char mdata[32];
122
123         fprintf(stderr, "command: '%s'\n", cmdline);
124
125         if (strncmp(cmdline, "quit", 4) == 0) {
126                 close(mw_fd);
127                 exit(0);
128         }
129         if (strncmp(cmdline, "help", 4) == 0) {
130                 print_help();
131         }
132         if (strncmp(cmdline, "srtc", 4) == 0) {
133                 fprintf(stderr, "Setting RTC from system time...");
134                 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
135                 fprintf(stderr, "OK\n");
136         }
137         if (strncmp(cmdline, "grtc", 4) == 0) {
138                 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
139         }
140         if (strncmp(cmdline, "gistr", 5) == 0) {
141                 mdata[0] = 0;
142                 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
143         }
144         if (strncmp(cmdline, "gdtype", 6) == 0) {
145                 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
146         }
147         if (strncmp(cmdline, "rvbat", 5) == 0) {
148                 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
149         }
150         if (strncmp(cmdline, "modecfg", 6) == 0) {
151                 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
152                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
153         }
154         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
155                 mdata[0] = 0; /* idle screen */
156                 mdata[1] = 1; /* button index */
157                 mdata[2] = 2; /* button press type */
158                 mdata[3] = 3; /* callback message type */
159                 mdata[4] = 4; /* callback message option */
160                 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
161         }
162         if (strncmp(cmdline, "svib", 4) == 0) {
163                 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
164         }
165         if (strncmp(cmdline, "tbmp", 4) == 0) {
166                 bitmap_test(mw_fd);
167         }
168         if (strncmp(cmdline, "rbmp", 4) == 0) {
169                 bitmap_read(mw_fd);
170         }
171 }
172
173
174 int menu(int mw_fd)
175 {
176         fd_set mfds;
177         struct termios tconfd, tmwfd;
178         char cmdline[128];
179         unsigned char msg_buf[64];
180         unsigned char clinep = 0;
181         int rcvd;
182
183         tcgetattr(0, &tconfd);
184         cfmakeraw(&tconfd);
185         tconfd.c_oflag |= ONLCR | OPOST;
186         tconfd.c_lflag |= ISIG;
187         tcsetattr(0, TCSANOW, &tconfd);
188
189         tcgetattr(mw_fd, &tmwfd);
190         cfmakeraw(&tmwfd);
191         tmwfd.c_oflag |= ONLCR | OPOST;
192         tmwfd.c_lflag |= ISIG;
193         tcsetattr(mw_fd, TCSANOW, &tmwfd);
194
195         FD_ZERO(&mfds);
196         FD_SET(0, &mfds);
197         FD_SET(mw_fd, &mfds);
198
199         memset(cmdline, 0, 128);
200
201         do {
202                 rcvd = 0;
203                 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
204                         if (FD_ISSET(mw_fd, &mfds)) {
205                                 rcvd = read(mw_fd, msg_buf, 64);
206                                 fprintf(stderr, "read %d bytes:\n", rcvd);
207                                 if (rcvd > 0) {
208                                         dump_frame(msg_buf, rcvd);
209                                         decode_frame(mw_fd, msg_buf, rcvd);
210                                 }
211                         };
212                         if (FD_ISSET(0, &mfds)) {
213                                 rcvd = read(0, (cmdline+clinep), 1);
214                                 if (rcvd > 0) {
215                                         if (cmdline[clinep] == '\r') {
216                                                 printf("\n");
217                                                 cmdline[clinep--] = '\0';
218                                                 process_cmd(cmdline, clinep, mw_fd);
219                                                 clinep = 0;
220                                                 memset(cmdline, 0, 128);
221                                         } else {
222                                                 clinep++;
223                                                 if (clinep > 75)
224                                                         clinep = 75;
225                                                 printf("\r> %s", cmdline);
226                                                 fflush(stdout);
227                                         }
228                                 }
229                         };
230                 } else
231                         break;
232                 FD_ZERO(&mfds);
233                 FD_SET(0, &mfds);
234                 FD_SET(mw_fd, &mfds);
235         } while (rcvd > 0);
236
237         return 0;
238 }
239
240 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
241 {
242         struct sockaddr_rc addr;
243         int sk, opt;
244         int f;
245
246         sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
247         if (sk < 0) {
248                 fprintf(stderr, "Can't create socket: %s (%d)\n",
249                         strerror(errno), errno);
250                 return -1;
251         }
252
253 /*
254         f = 1;
255         if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
256                 fprintf(stderr, "Can't set flushable: %s (%d)\n",
257                         strerror(errno), errno);
258                 return -1;
259         }
260 */
261         memset(&addr, 0, sizeof(addr));
262         addr.rc_family = AF_BLUETOOTH;
263         bacpy(&addr.rc_bdaddr, BDADDR_ANY);
264
265         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
266                 fprintf(stderr, "Can't bind socket: %s (%d)\n",
267                                                         strerror(errno), errno);
268                 close(sk);
269                 return -1;
270         }
271
272        /* Set link mode */
273         opt = 0;
274         opt |= RFCOMM_LM_MASTER;
275         opt |= RFCOMM_LM_AUTH;
276 /*
277         opt |= RFCOMM_LM_ENCRYPT;
278         opt |= RFCOMM_LM_SECURE;
279 */
280         if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
281                 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
282                                                         strerror(errno), errno);
283                 close(sk);
284                 return -1;
285         }
286
287         memset(&addr, 0, sizeof(addr));
288         addr.rc_family = AF_BLUETOOTH;
289         bacpy(&addr.rc_bdaddr, bdaddr);
290         addr.rc_channel = channel;
291
292         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
293                 fprintf(stderr, "Can't connect: %s (%d)\n",
294                                                         strerror(errno), errno);
295                 close(sk);
296                 return -1;
297         }
298
299         return sk;
300 }
301
302 void baswap(bdaddr_t *dst, const bdaddr_t *src)
303 {
304         register unsigned char *d = (unsigned char *) dst;
305         register const unsigned char *s = (const unsigned char *) src;
306         register int i;
307
308         for (i = 0; i < 6; i++)
309                 d[i] = s[5-i];
310 }
311 int bachk(const char *str)
312 {
313         if (!str)
314                 return -1;
315
316         if (strlen(str) != 17)
317                 return -1;
318
319         while (*str) {
320                 if (!isxdigit(*str++))
321                         return -1;
322
323                 if (!isxdigit(*str++))
324                         return -1;
325
326                 if (*str == 0)
327                         break;
328
329                 if (*str++ != ':')
330                         return -1;
331         }
332
333         return 0;
334 }
335
336 int str2ba(const char *str, bdaddr_t *ba)
337 {
338         bdaddr_t b;
339         int i;
340
341         if (bachk(str) < 0) {
342                 memset(ba, 0, sizeof(*ba));
343                 return -1;
344         }
345
346         for (i = 0; i < 6; i++, str += 3)
347                 b.b[i] = strtol(str, NULL, 16);
348
349         baswap(ba, &b);
350
351         return 0;
352 }
353
354 int main(int argc, char **argv)
355 {
356         int mw_fd;
357         bdaddr_t btaddr;
358
359         if (argc != 2) {
360                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
361                 return 1;
362         };
363   
364         crc16ccitt_init();
365
366 #if USE_RFCOMM_FILE
367         mw_fd = open(argv[1], O_RDWR);
368         if (mw_fd < 0) {
369                 perror("open");
370                 return 1;
371         };
372 #else
373         if (str2ba(argv[1], &btaddr))
374                 return 1;
375         mw_fd = open_socket(&btaddr, 1);
376         if (mw_fd < 0) {
377                 return 1;
378         };
379 #endif
380
381         menu(mw_fd);
382
383         fsync(mw_fd);
384
385         close(mw_fd);
386
387         return 0;
388 };
389