]> git.kernelconcepts.de Git - metawatch.git/blob - mw_main.c
4e871b43d8617af5682c5cd8ded56d8840ba3050
[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 #include <glib.h>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
27
28 /*
29 #include <bluetooth/sdp.h>
30 #include <bluetooth/sdp_lib.h>
31 */
32
33 #include "metawatch.h"
34 #include "crc16ccitt.h"
35 #include "mw_utility.h"
36
37 typedef struct {
38         GMainLoop *mloop;
39         mwdevice_t mwdevice;
40         unsigned char rcvbuf[128];
41         int rcvbuf_pos;
42         int con_fd;             /* console input fd */
43         char cmdline[128];
44         int cmdline_pos;
45 } mwdata_t;
46
47 void bitmap_test(mwdevice_t *mwdevice)
48 {
49         /* a nice checker-board pattern */
50         unsigned char checkbuf[24] = {
51                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
52                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
53                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
54                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
55         };
56         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
57         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
58         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
59         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
60
61         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
62 }
63
64 void bitmap_test2(mwdevice_t *mwdevice)
65 {
66         mw_buffer *mwbuf1, *mwbuf2;
67         unsigned char *bbuf;
68         int len, x;
69
70         mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
71         mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
72
73         mw_buf_clear(mwbuf1, MW_BLACK);
74         mw_buf_clear(mwbuf2, MW_WHITE);
75
76         for (x=0; x<66; x++) {
77                 //mw_buf_clear(mwbuf1, MW_BLACK);
78                 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
79                 mw_dump_mw_buffer(mwbuf1);
80                 bbuf = mw_make_mw_buffer(mwbuf1, &len);
81                 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
82                 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
83                 getchar();
84         };
85
86         mw_free_pbuffer(mwbuf1);
87         mw_free_pbuffer(mwbuf2);
88 }
89
90 void text_test(mwdevice_t *mwdevice)
91 {
92         mw_buffer *mwbuf;
93         unsigned char *bbuf;
94         int len;
95
96         mwbuf = mw_alloc_pbuffer(96, 66, 1);
97         mw_buf_clear(mwbuf, MW_BLACK);
98
99         mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
100         mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
101         mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
102         mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
103
104         bbuf = mw_make_mw_buffer(mwbuf, &len);
105         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
106         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
107         mw_free_pbuffer(mwbuf);
108 }
109
110 void line_test(mwdevice_t *mwdevice)
111 {
112         mw_buffer *mwbuf;
113         unsigned char *bbuf;
114         int len, p;
115
116         mwbuf = mw_alloc_pbuffer(96, 66, 1);
117         mw_buf_clear(mwbuf, MW_BLACK);
118
119         for (p=0; p<=96; p+=8)
120                 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
121
122         bbuf = mw_make_mw_buffer(mwbuf, &len);
123         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
124         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
125         mw_free_pbuffer(mwbuf);
126 }
127
128 void bitmap_read(mwdevice_t *mwdevice)
129 {
130         int ffd, ret;
131         char rbuf[256];
132         unsigned int width, height, i;
133 #ifdef DEBUG
134         unsigned int x, y;
135 #endif
136         unsigned int rowlength;
137         unsigned char *bmapbuf;
138         // unsigned char mw_buf[24];
139
140         ffd = open("test.pbm", O_RDONLY);
141         if (ffd < 0) {
142                 perror("open");
143                 return;
144         };
145         ret = read(ffd, rbuf, 3);
146         if (rbuf[0] != 'P' || rbuf[1] != '4') {
147                 fprintf(stderr, "not a PBM file\n");
148                 return;
149         }
150         memset(rbuf, 0, 256);
151         i = 0;
152         do {
153                 ret = read(ffd, (rbuf+i), 1);
154         } while (!isspace(rbuf[i++]));
155         width = atoi(rbuf);
156
157         memset(rbuf, 0, 256);
158         i = 0;
159         do {
160                 ret = read(ffd, (rbuf+i), 1);
161         } while (!isspace(rbuf[i++]));
162         height = atoi(rbuf);
163
164         rowlength = ((width / 8) + 1);
165
166         bmapbuf = malloc(rowlength * height);
167
168         ret = read(ffd, bmapbuf, rowlength * height);
169         close(ffd);
170
171 #ifdef DEBUG
172         fprintf(stderr, "row length = %d bytes\n", rowlength);
173         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
174         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
175         fprintf(stderr, "\n");
176         for (y=0; y<height; y++) {
177                 for (x=0; x<rowlength; x++) {
178                         for (i=0; i<8; i++)
179                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
180                 }
181                 fprintf(stderr, "\n");
182         }
183         fprintf(stderr, "\n");
184 #endif
185
186         /* reverse bits and invert the bmap */
187         bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
188         /* send the buffer to the watch */
189         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
190         /* update the display */
191         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
192
193         free(bmapbuf);
194 }
195
196 void test_notification(mwdevice_t *mwdevice)
197 {
198         mw_buffer *mwbuf;
199         unsigned char *bbuf;
200         int len;
201
202         mwbuf = mw_alloc_pbuffer(96, 96, 1);
203         mw_buf_clear(mwbuf, MW_BLACK);
204
205         mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
206         mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
207         mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
208         mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
209
210         mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
211         mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
212         mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
213         mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
214
215         mw_buf_print(mwbuf, 4,  4, "012345678901234", 0, MW_WHITE, MW_BLACK);
216         mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
217         mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
218         mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
219         mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
220         mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
221         mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
222         mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
223         mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
224         mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
225
226         bbuf = mw_make_mw_buffer(mwbuf, &len);
227         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
228         mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
229         mw_free_pbuffer(mwbuf);
230
231         mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
232 }
233
234 void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
235 {
236         mw_buffer *mwbuf;
237         unsigned char *bbuf;
238         int len,i,c,r;
239         char sstr[32];
240
241         mwbuf = mw_alloc_pbuffer(96, 96, 1);
242         mw_buf_clear(mwbuf, MW_BLACK);
243
244         mw_buf_print(mwbuf, 0,  0, header, 0, MW_BLACK, MW_WHITE);
245
246         i=0;
247         c=0; r=1;
248         memset(sstr,0,32);
249         while (i<strlen(body)) {
250                 sstr[c++] = body[i++];
251                 if (c>=16 || i>=strlen(body)) {
252                         mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
253                         memset(sstr,0,32);
254                         c=0; r++;
255                         if (r>10)
256                                 break;
257                 };
258         };
259
260         bbuf = mw_make_mw_buffer(mwbuf, &len);
261         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
262         mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
263         mw_free_pbuffer(mwbuf);
264
265         mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
266 }
267
268 void test_application(mwdevice_t *mwdevice)
269 {
270         mw_buffer *mwbuf;
271         unsigned char *bbuf;
272         int len;
273
274         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
275         mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
276
277         mwbuf = mw_alloc_pbuffer(96, 96, 1);
278         mw_buf_clear(mwbuf, MW_BLACK);
279
280         mw_buf_print(mwbuf, 0,  0, "  Application   ", 0, MW_BLACK, MW_WHITE);
281         mw_buf_print(mwbuf, 0,  9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
282         mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
283         mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
284         mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
285         mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
286         mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
287         mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
288         mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
289         mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
290
291         bbuf = mw_make_mw_buffer(mwbuf, &len);
292         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
293         mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
294         mw_free_pbuffer(mwbuf);
295 }
296
297 void test_oled(mwdevice_t *mwdevice, unsigned char oled)
298 {
299         mw_buffer *mwbuf;
300         unsigned char *bbuf;
301         int len, i;
302
303         mwbuf = mw_alloc_pbuffer(80, 16, 1);
304         mw_buf_clear(mwbuf, MW_BLACK);
305
306         mw_buf_print(mwbuf, 0,  0, "Application", 0, MW_WHITE, MW_BLACK);
307         mw_buf_print(mwbuf, 0,  8, "012345678901234", 0, MW_WHITE, MW_BLACK);
308
309         bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
310         mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len);
311
312         mw_free_pbuffer(mwbuf);
313 }
314
315 void print_help(void)
316 {
317 }
318
319 void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
320 {
321         unsigned char mdata[32];
322         unsigned int intopt;
323
324         fprintf(stderr, "command: '%s'\n", cmdline);
325
326         if (strncmp(cmdline, "quit", 4) == 0) {
327                 //close(mw_fd);
328                 //exit(0);
329                 g_main_loop_quit (mwdata->mloop);
330         }
331         if (strncmp(cmdline, "help", 4) == 0) {
332                 print_help();
333         }
334         if (strncmp(cmdline, "ahand", 5) == 0) {
335                 intopt = atoi(cmdline+5);
336                 g_print("Advance analog hands by %d minutes\n", intopt);
337                 mdata[0] = intopt / 60;
338                 mdata[1] = intopt % 60;
339                 mdata[2] = 0;
340                 mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
341         }
342         if (strncmp(cmdline, "srtc", 4) == 0) {
343                 fprintf(stderr, "Setting RTC from system time...");
344                 mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
345                 fprintf(stderr, "OK\n");
346         }
347         if (strncmp(cmdline, "grtc", 4) == 0) {
348                 mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
349         }
350         if (strncmp(cmdline, "gistr", 5) == 0) {
351                 intopt = cmdline[6]-0x30;
352                 if (intopt>=0 && intopt <=3)
353                         mdata[0] = intopt;
354                 else
355                         mdata[0] = 0;
356                 mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
357         }
358         if (strncmp(cmdline, "gdtype", 6) == 0) {
359                 mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
360         }
361         if (strncmp(cmdline, "rvbat", 5) == 0) {
362                 mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
363         }
364         if (strncmp(cmdline, "rlight", 6) == 0) {
365                 mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
366         }
367         if (strncmp(cmdline, "modecfg", 6) == 0) {
368                 mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
369                 mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
370         }
371         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
372                 intopt = cmdline[7]-0x30;
373                 mdata[0] = 0; /* idle screen */
374                 mdata[1] = intopt; /* button index */
375                 /* for reading the config those are unnecessary */
376                 mdata[2] = 0; /* button press type */
377                 mdata[3] = 0; /* callback message type */
378                 mdata[4] = 0; /* callback message option */
379                 mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
380         }
381         if (strncmp(cmdline, "svib", 4) == 0) {
382                 mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
383         }
384         if (strncmp(cmdline, "tbmp", 4) == 0) {
385                 bitmap_test(&mwdata->mwdevice);
386         }
387         if (strncmp(cmdline, "t2bmp", 5) == 0) {
388                 bitmap_test2(&mwdata->mwdevice);
389         }
390         if (strncmp(cmdline, "text", 4) == 0) {
391                 text_test(&mwdata->mwdevice);
392         }
393         if (strncmp(cmdline, "tline", 5) == 0) {
394                 line_test(&mwdata->mwdevice);
395         }
396         if (strncmp(cmdline, "rbmp", 4) == 0) {
397                 bitmap_read(&mwdata->mwdevice);
398         }
399         if (strncmp(cmdline, "tnote", 5) == 0) {
400                 test_notification(&mwdata->mwdevice);
401         }
402         if (strncmp(cmdline, "tapp", 4) == 0) {
403                 test_application(&mwdata->mwdevice);
404         }
405         if (strncmp(cmdline, "eoled", 5) == 0) {
406                 intopt = cmdline[6]-0x30;
407                 if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
408                         mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
409         }
410         if (strncmp(cmdline, "toled", 5) == 0) {
411                 intopt = cmdline[6]-0x30;
412                 test_oled(&mwdata->mwdevice, intopt);
413                 //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
414                 //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
415         }
416 }
417
418
419 int feed_menu(mwdata_t *mdata)
420 {
421         int rcvd;
422
423         rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
424         if (rcvd > 0) {
425                 if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
426                         printf("\n");
427                         mdata->cmdline[mdata->cmdline_pos--] = '\0';
428                         process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
429                         mdata->cmdline_pos = 0;
430                         memset(mdata->cmdline, 0, 128);
431                 } else {
432                         mdata->cmdline_pos++;
433                         if (mdata->cmdline_pos > 75)
434                                 mdata->cmdline_pos = 75;
435                         printf("\r> %s", mdata->cmdline);
436                         fflush(stdout);
437                 }
438         }
439         return 0;
440 }
441
442 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
443 {
444         struct sockaddr_rc addr;
445         int sk, opt;
446         //int f;
447
448         sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
449         if (sk < 0) {
450                 fprintf(stderr, "Can't create socket: %s (%d)\n",
451                         strerror(errno), errno);
452                 return -1;
453         }
454
455 /*
456         f = 1;
457         if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
458                 fprintf(stderr, "Can't set flushable: %s (%d)\n",
459                         strerror(errno), errno);
460                 return -1;
461         }
462 */
463         memset(&addr, 0, sizeof(addr));
464         addr.rc_family = AF_BLUETOOTH;
465         bacpy(&addr.rc_bdaddr, BDADDR_ANY);
466
467         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
468                 fprintf(stderr, "Can't bind socket: %s (%d)\n",
469                                                         strerror(errno), errno);
470                 close(sk);
471                 return -1;
472         }
473
474        /* Set link mode */
475         opt = 0;
476         opt |= RFCOMM_LM_MASTER;
477         opt |= RFCOMM_LM_AUTH;
478 /*
479         opt |= RFCOMM_LM_ENCRYPT;
480         opt |= RFCOMM_LM_SECURE;
481 */
482         if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
483                 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
484                                                         strerror(errno), errno);
485                 close(sk);
486                 return -1;
487         }
488
489         memset(&addr, 0, sizeof(addr));
490         addr.rc_family = AF_BLUETOOTH;
491         bacpy(&addr.rc_bdaddr, bdaddr);
492         addr.rc_channel = channel;
493
494         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
495                 fprintf(stderr, "Can't connect: %s (%d)\n",
496                                                         strerror(errno), errno);
497                 close(sk);
498                 return -1;
499         }
500
501         return sk;
502 }
503
504 void baswap(bdaddr_t *dst, const bdaddr_t *src)
505 {
506         register unsigned char *d = (unsigned char *) dst;
507         register const unsigned char *s = (const unsigned char *) src;
508         register int i;
509
510         for (i = 0; i < 6; i++)
511                 d[i] = s[5-i];
512 }
513
514 int bachk(const char *str)
515 {
516         if (!str)
517                 return -1;
518
519         if (strlen(str) != 17)
520                 return -1;
521
522         while (*str) {
523                 if (!isxdigit(*str++))
524                         return -1;
525
526                 if (!isxdigit(*str++))
527                         return -1;
528
529                 if (*str == 0)
530                         break;
531
532                 if (*str++ != ':')
533                         return -1;
534         }
535
536         return 0;
537 }
538
539 int str2ba(const char *str, bdaddr_t *ba)
540 {
541         bdaddr_t b;
542         int i;
543
544         if (bachk(str) < 0) {
545                 memset(ba, 0, sizeof(*ba));
546                 return -1;
547         }
548
549         for (i = 0; i < 6; i++, str += 3)
550                 b.b[i] = strtol(str, NULL, 16);
551
552         baswap(ba, &b);
553
554         return 0;
555 }
556
557 gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
558 {
559         mwdata_t *mdata = (mwdata_t *)udata;
560         int rcvd;
561
562         rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64);
563 #ifdef DEBUG
564         fprintf(stderr, "read %d bytes:\n", rcvd);
565 #endif
566         if (rcvd > 0) {
567 #ifdef DEBUG
568                 dump_frame(mdata->rcvbuf, rcvd);
569 #endif
570                 decode_frame(&mdata->mwdevice, mdata->rcvbuf, rcvd);
571                 mdata->rcvbuf_pos = 0;
572         }
573
574         return TRUE;
575 }
576
577 gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
578 {
579         mwdata_t *mdata = (mwdata_t *)udata;
580
581         feed_menu(mdata);
582
583         return TRUE;
584 }
585
586 static DBusHandlerResult
587 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
588 {
589         //GMainLoop *loop = user_data;
590         mwdata_t *mdata = (mwdata_t *)user_data;
591         DBusError error;
592         char *app_name, *app_icon, *summary, *body; // **actions;
593         int replace_id; // dict, expire;
594
595         /* A signal from the bus saying we are about to be disconnected */
596         if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
597                 /* Tell the main loop to quit */
598                 g_main_loop_quit (mdata->mloop);
599                 /* We have handled this message, don't pass it on */
600                 return DBUS_HANDLER_RESULT_HANDLED;
601         }
602
603         if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify"))    {
604                 g_print("method call\n");
605                 dbus_error_init (&error);
606                 if (dbus_message_get_args(message, &error,
607                         DBUS_TYPE_STRING, &app_name,
608                         DBUS_TYPE_UINT32, &replace_id,
609                         DBUS_TYPE_STRING, &app_icon,
610                         DBUS_TYPE_STRING, &summary,
611                         DBUS_TYPE_STRING, &body,
612                         /*  G_TYPE_STRV, &actions,
613                         DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
614                         DBUS_TYPE_INT32, &expire, */
615                         DBUS_TYPE_INVALID)) {
616                         g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
617                         mw_send_notify(&mdata->mwdevice, summary, body);
618                         /* we just listen, we do not handle it here */
619                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
620                 } else {
621                         g_print("Notify received, but error getting message: %s\n", error.message);
622                         dbus_error_free (&error);
623                 }
624         } else {
625                 g_print("Not signal received\n");
626                 return DBUS_HANDLER_RESULT_HANDLED;
627         };
628
629         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
630 }
631
632 int main(int argc, char **argv)
633 {
634         int mw_fd;
635         mwdata_t mdata;
636         struct termios tconfd, otconfd, tmwfd;
637         bdaddr_t btaddr;
638         GIOChannel *mw_io, *m_in;
639         DBusConnection *bus;
640         DBusError error;
641
642         if (argc != 2) {
643                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
644                 return 1;
645         };
646   
647         g_type_init();
648         mdata.mloop = g_main_loop_new (NULL, FALSE);
649
650         crc16ccitt_init();
651
652 #if USE_RFCOMM_FILE
653         mw_fd = open(argv[1], O_RDWR);
654         if (mw_fd < 0) {
655                 perror("open");
656                 return 1;
657         };
658 #else
659         if (str2ba(argv[1], &btaddr))
660                 return 1;
661         mw_fd = open_socket(&btaddr, 1);
662         if (mw_fd < 0) {
663                 return 1;
664         } else {
665                 fprintf(stderr, "connected to %s\n", argv[1]);
666         };
667 #endif
668         /* we have a connection, RFCOMM socket is on mw_fd */
669         /* make the tty raw */
670         tcgetattr(mw_fd, &tmwfd);
671         cfmakeraw(&tmwfd);
672         tmwfd.c_oflag |= ONLCR | OPOST;
673         tmwfd.c_lflag |= ISIG;
674         tcsetattr(mw_fd, TCSANOW, &tmwfd);
675
676         mdata.mwdevice.mw_fd = mw_fd;
677         mdata.rcvbuf_pos = 0;
678         memset(mdata.rcvbuf, 0, 128);
679
680         dbus_error_init (&error);
681         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
682
683         if (!bus) {
684                 g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
685                 dbus_error_free (&error);
686                 return 1;
687         }
688
689         dbus_connection_setup_with_g_main (bus, NULL);
690         dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
691
692         dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
693
694         // menu(mw_fd);
695         mw_io = g_io_channel_unix_new(mw_fd);
696         g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
697
698         tcgetattr(0, &tconfd);
699         tcgetattr(0, &otconfd);
700         cfmakeraw(&tconfd);
701         tconfd.c_oflag |= ONLCR | OPOST;
702         tconfd.c_lflag |= ISIG;
703         tcsetattr(0, TCSANOW, &tconfd);
704
705         mdata.con_fd = 0;
706
707         m_in = g_io_channel_unix_new(0); /* stdin for kbd */
708         g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
709
710         memset(mdata.cmdline, 0, 128);
711         mdata.cmdline_pos = 0;
712
713         g_main_loop_run (mdata.mloop);
714
715         fsync(mw_fd);
716         close(mw_fd);
717
718         /* reset the controlling tty again */
719         tcsetattr(0, TCSANOW, &otconfd);
720
721         return 0;
722 };
723