]> git.kernelconcepts.de Git - metawatch.git/blob - mw_main.c
7a0831f06866ce9a4f7a3a02a1f12ea95fc341ef
[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 #include "mw_utility.h"
31
32
33 void bitmap_test(int mw_fd)
34 {
35         /* a nice checker-board pattern */
36         unsigned char checkbuf[24] = {
37                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
38                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
39                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
40                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
41         };
42         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
43         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
44         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
45         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
46
47         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
48 }
49
50 void bitmap_test2(int mw_fd)
51 {
52         mw_buffer *mwbuf1, *mwbuf2;
53         unsigned char *bbuf;
54         int len, x;
55
56         mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
57         mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
58
59         mw_buf_clear(mwbuf1, MW_BLACK);
60         mw_buf_clear(mwbuf2, MW_WHITE);
61
62         for (x=0; x<66; x++) {
63                 //mw_buf_clear(mwbuf1, MW_BLACK);
64                 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
65                 mw_dump_mw_buffer(mwbuf1);
66                 bbuf = mw_make_mw_buffer(mwbuf1, &len);
67                 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
68                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
69                 getchar();
70         };
71
72         mw_free_pbuffer(mwbuf1);
73         mw_free_pbuffer(mwbuf2);
74 }
75
76 void text_test(int mw_fd)
77 {
78         mw_buffer *mwbuf;
79         unsigned char *bbuf;
80         int len;
81
82         mwbuf = mw_alloc_pbuffer(96, 66, 1);
83         mw_buf_clear(mwbuf, MW_BLACK);
84
85         mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
86         mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
87         mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
88         mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
89
90         bbuf = mw_make_mw_buffer(mwbuf, &len);
91         mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
92         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
93         mw_free_pbuffer(mwbuf);
94 }
95
96 void line_test(int mw_fd)
97 {
98         mw_buffer *mwbuf;
99         unsigned char *bbuf;
100         int len, p;
101
102         mwbuf = mw_alloc_pbuffer(96, 66, 1);
103         mw_buf_clear(mwbuf, MW_BLACK);
104
105         for (p=0; p<=96; p+=8)
106                 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
107
108         bbuf = mw_make_mw_buffer(mwbuf, &len);
109         mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
110         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
111         mw_free_pbuffer(mwbuf);
112 }
113
114 void bitmap_read(int mw_fd)
115 {
116         int ffd, ret;
117         char rbuf[256];
118         unsigned int width, height, i;
119 #ifdef DEBUG
120         unsigned int x, y;
121 #endif
122         unsigned int rowlength;
123         unsigned char *bmapbuf;
124         // unsigned char mw_buf[24];
125
126         ffd = open("test.pbm", O_RDONLY);
127         if (ffd < 0) {
128                 perror("open");
129                 return;
130         };
131         ret = read(ffd, rbuf, 3);
132         if (rbuf[0] != 'P' || rbuf[1] != '4') {
133                 fprintf(stderr, "not a PBM file\n");
134                 return;
135         }
136         memset(rbuf, 0, 256);
137         i = 0;
138         do {
139                 ret = read(ffd, (rbuf+i), 1);
140         } while (!isspace(rbuf[i++]));
141         width = atoi(rbuf);
142
143         memset(rbuf, 0, 256);
144         i = 0;
145         do {
146                 ret = read(ffd, (rbuf+i), 1);
147         } while (!isspace(rbuf[i++]));
148         height = atoi(rbuf);
149
150         rowlength = ((width / 8) + 1);
151
152         bmapbuf = malloc(rowlength * height);
153
154         ret = read(ffd, bmapbuf, rowlength * height);
155         close(ffd);
156
157 #ifdef DEBUG
158         fprintf(stderr, "row length = %d bytes\n", rowlength);
159         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
160         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
161         fprintf(stderr, "\n");
162         for (y=0; y<height; y++) {
163                 for (x=0; x<rowlength; x++) {
164                         for (i=0; i<8; i++)
165                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
166                 }
167                 fprintf(stderr, "\n");
168         }
169         fprintf(stderr, "\n");
170 #endif
171
172         /* reverse bits and invert the bmap */
173         bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
174         /* send the buffer to the watch */
175         mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
176         /* update the display */
177         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
178
179         free(bmapbuf);
180 }
181
182 void print_help(void)
183 {
184 }
185
186 void process_cmd(char *cmdline, int clinep, int mw_fd)
187 {
188         unsigned char mdata[32];
189
190         fprintf(stderr, "command: '%s'\n", cmdline);
191
192         if (strncmp(cmdline, "quit", 4) == 0) {
193                 close(mw_fd);
194                 exit(0);
195         }
196         if (strncmp(cmdline, "help", 4) == 0) {
197                 print_help();
198         }
199         if (strncmp(cmdline, "srtc", 4) == 0) {
200                 fprintf(stderr, "Setting RTC from system time...");
201                 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
202                 fprintf(stderr, "OK\n");
203         }
204         if (strncmp(cmdline, "grtc", 4) == 0) {
205                 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
206         }
207         if (strncmp(cmdline, "gistr", 5) == 0) {
208                 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, NULL, 0);
209         }
210         if (strncmp(cmdline, "gdtype", 6) == 0) {
211                 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
212         }
213         if (strncmp(cmdline, "rvbat", 5) == 0) {
214                 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
215         }
216         if (strncmp(cmdline, "rlight", 6) == 0) {
217                 mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
218         }
219         if (strncmp(cmdline, "modecfg", 6) == 0) {
220                 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
221                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
222         }
223         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
224                 mdata[0] = 0; /* idle screen */
225                 mdata[1] = 1; /* button index */
226                 mdata[2] = 2; /* button press type */
227                 mdata[3] = 3; /* callback message type */
228                 mdata[4] = 4; /* callback message option */
229                 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
230         }
231         if (strncmp(cmdline, "svib", 4) == 0) {
232                 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
233         }
234         if (strncmp(cmdline, "tbmp", 4) == 0) {
235                 bitmap_test(mw_fd);
236         }
237         if (strncmp(cmdline, "t2bmp", 5) == 0) {
238                 bitmap_test2(mw_fd);
239         }
240         if (strncmp(cmdline, "text", 4) == 0) {
241                 text_test(mw_fd);
242         }
243         if (strncmp(cmdline, "tline", 5) == 0) {
244                 line_test(mw_fd);
245         }
246         if (strncmp(cmdline, "rbmp", 4) == 0) {
247                 bitmap_read(mw_fd);
248         }
249 }
250
251
252 int menu(int mw_fd)
253 {
254         fd_set mfds;
255         struct termios tconfd, tmwfd;
256         char cmdline[128];
257         unsigned char msg_buf[64];
258         unsigned char clinep = 0;
259         int rcvd;
260
261         tcgetattr(0, &tconfd);
262         cfmakeraw(&tconfd);
263         tconfd.c_oflag |= ONLCR | OPOST;
264         tconfd.c_lflag |= ISIG;
265         tcsetattr(0, TCSANOW, &tconfd);
266
267         tcgetattr(mw_fd, &tmwfd);
268         cfmakeraw(&tmwfd);
269         tmwfd.c_oflag |= ONLCR | OPOST;
270         tmwfd.c_lflag |= ISIG;
271         tcsetattr(mw_fd, TCSANOW, &tmwfd);
272
273         FD_ZERO(&mfds);
274         FD_SET(0, &mfds);
275         FD_SET(mw_fd, &mfds);
276
277         memset(cmdline, 0, 128);
278
279         do {
280                 rcvd = 0;
281                 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
282                         if (FD_ISSET(mw_fd, &mfds)) {
283                                 rcvd = read(mw_fd, msg_buf, 64);
284 #ifdef DEBUG
285                                 fprintf(stderr, "read %d bytes:\n", rcvd);
286 #endif
287                                 if (rcvd > 0) {
288 #ifdef DEBUG
289                                         dump_frame(msg_buf, rcvd);
290 #endif
291                                         decode_frame(mw_fd, msg_buf, rcvd);
292                                 }
293                         };
294                         if (FD_ISSET(0, &mfds)) {
295                                 rcvd = read(0, (cmdline+clinep), 1);
296                                 if (rcvd > 0) {
297                                         if (cmdline[clinep] == '\r') {
298                                                 printf("\n");
299                                                 cmdline[clinep--] = '\0';
300                                                 process_cmd(cmdline, clinep, mw_fd);
301                                                 clinep = 0;
302                                                 memset(cmdline, 0, 128);
303                                         } else {
304                                                 clinep++;
305                                                 if (clinep > 75)
306                                                         clinep = 75;
307                                                 printf("\r> %s", cmdline);
308                                                 fflush(stdout);
309                                         }
310                                 }
311                         };
312                 } else
313                         break;
314                 FD_ZERO(&mfds);
315                 FD_SET(0, &mfds);
316                 FD_SET(mw_fd, &mfds);
317         } while (rcvd > 0);
318
319         return 0;
320 }
321
322 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
323 {
324         struct sockaddr_rc addr;
325         int sk, opt;
326         //int f;
327
328         sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
329         if (sk < 0) {
330                 fprintf(stderr, "Can't create socket: %s (%d)\n",
331                         strerror(errno), errno);
332                 return -1;
333         }
334
335 /*
336         f = 1;
337         if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
338                 fprintf(stderr, "Can't set flushable: %s (%d)\n",
339                         strerror(errno), errno);
340                 return -1;
341         }
342 */
343         memset(&addr, 0, sizeof(addr));
344         addr.rc_family = AF_BLUETOOTH;
345         bacpy(&addr.rc_bdaddr, BDADDR_ANY);
346
347         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
348                 fprintf(stderr, "Can't bind socket: %s (%d)\n",
349                                                         strerror(errno), errno);
350                 close(sk);
351                 return -1;
352         }
353
354        /* Set link mode */
355         opt = 0;
356         opt |= RFCOMM_LM_MASTER;
357         opt |= RFCOMM_LM_AUTH;
358 /*
359         opt |= RFCOMM_LM_ENCRYPT;
360         opt |= RFCOMM_LM_SECURE;
361 */
362         if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
363                 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
364                                                         strerror(errno), errno);
365                 close(sk);
366                 return -1;
367         }
368
369         memset(&addr, 0, sizeof(addr));
370         addr.rc_family = AF_BLUETOOTH;
371         bacpy(&addr.rc_bdaddr, bdaddr);
372         addr.rc_channel = channel;
373
374         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
375                 fprintf(stderr, "Can't connect: %s (%d)\n",
376                                                         strerror(errno), errno);
377                 close(sk);
378                 return -1;
379         }
380
381         return sk;
382 }
383
384 void baswap(bdaddr_t *dst, const bdaddr_t *src)
385 {
386         register unsigned char *d = (unsigned char *) dst;
387         register const unsigned char *s = (const unsigned char *) src;
388         register int i;
389
390         for (i = 0; i < 6; i++)
391                 d[i] = s[5-i];
392 }
393 int bachk(const char *str)
394 {
395         if (!str)
396                 return -1;
397
398         if (strlen(str) != 17)
399                 return -1;
400
401         while (*str) {
402                 if (!isxdigit(*str++))
403                         return -1;
404
405                 if (!isxdigit(*str++))
406                         return -1;
407
408                 if (*str == 0)
409                         break;
410
411                 if (*str++ != ':')
412                         return -1;
413         }
414
415         return 0;
416 }
417
418 int str2ba(const char *str, bdaddr_t *ba)
419 {
420         bdaddr_t b;
421         int i;
422
423         if (bachk(str) < 0) {
424                 memset(ba, 0, sizeof(*ba));
425                 return -1;
426         }
427
428         for (i = 0; i < 6; i++, str += 3)
429                 b.b[i] = strtol(str, NULL, 16);
430
431         baswap(ba, &b);
432
433         return 0;
434 }
435
436 int main(int argc, char **argv)
437 {
438         int mw_fd;
439         bdaddr_t btaddr;
440
441         if (argc != 2) {
442                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
443                 return 1;
444         };
445   
446         crc16ccitt_init();
447
448 #if USE_RFCOMM_FILE
449         mw_fd = open(argv[1], O_RDWR);
450         if (mw_fd < 0) {
451                 perror("open");
452                 return 1;
453         };
454 #else
455         if (str2ba(argv[1], &btaddr))
456                 return 1;
457         mw_fd = open_socket(&btaddr, 1);
458         if (mw_fd < 0) {
459                 return 1;
460         } else {
461                 fprintf(stderr, "connected to %s\n", argv[1]);
462         };
463 #endif
464
465         menu(mw_fd);
466
467         fsync(mw_fd);
468
469         close(mw_fd);
470
471         return 0;
472 };
473