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