]> git.kernelconcepts.de Git - metawatch.git/blobdiff - mw_main.c
Improve cmd parameter parsing and handling
[metawatch.git] / mw_main.c
index 5dae442aa9f10780cc4e8af3bb9b0ce2ee0dc4b8..a799dd856a42ba230ef8bead1be9b82e0ba91778 100644 (file)
--- a/mw_main.c
+++ b/mw_main.c
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
 
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <libsoup/soup.h>
+
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+
 /*
 #include <bluetooth/sdp.h>
 #include <bluetooth/sdp_lib.h>
 
 #include "metawatch.h"
 #include "crc16ccitt.h"
+#include "mw_utility.h"
 
+#include "bt_helper.h"
 
-void bitmap_test(int mw_fd)
+typedef struct {
+       GMainLoop *mloop;
+       mwdevice_t mwdevice;
+       unsigned char rcvbuf[128];
+       int rcvbuf_pos;
+       int con_fd;             /* console input fd */
+       char cmdline[128];
+       int cmdline_pos;
+       SoupSession *msoup;
+       SoupMessage *smsg;
+} mwdata_t;
+
+void bitmap_test(mwdevice_t *mwdevice)
 {
        /* a nice checker-board pattern */
        unsigned char checkbuf[24] = {
@@ -38,20 +62,86 @@ void bitmap_test(int mw_fd)
                0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
                0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
        };
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
+
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+}
+
+void bitmap_test2(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf1, *mwbuf2;
+       unsigned char *bbuf;
+       int len, x;
+
+       mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
+       mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
+
+       mw_buf_clear(mwbuf1, MW_BLACK);
+       mw_buf_clear(mwbuf2, MW_WHITE);
+
+       for (x=0; x<66; x++) {
+               //mw_buf_clear(mwbuf1, MW_BLACK);
+               mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
+               mw_dump_mw_buffer(mwbuf1);
+               bbuf = mw_make_mw_buffer(mwbuf1, &len);
+               mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+               mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+               getchar();
+       };
 
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_free_pbuffer(mwbuf1);
+       mw_free_pbuffer(mwbuf2);
 }
 
+void text_test(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+       mw_free_pbuffer(mwbuf);
+}
 
-void bitmap_read(int mw_fd)
+void line_test(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len, p;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       for (p=0; p<=96; p+=8)
+               mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+       mw_free_pbuffer(mwbuf);
+}
+
+void bitmap_read(mwdevice_t *mwdevice)
 {
        int ffd, ret;
        char rbuf[256];
-       unsigned int width, height, i, x, y;
+       unsigned int width, height, i;
+#ifdef DEBUG
+       unsigned int x, y;
+#endif
        unsigned int rowlength;
        unsigned char *bmapbuf;
        // unsigned char mw_buf[24];
@@ -103,264 +193,616 @@ void bitmap_read(int mw_fd)
 #endif
 
        /* reverse bits and invert the bmap */
-       bmap_buffer_flipinvert(1, 0, bmapbuf, rowlength * height);
+       bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
        /* send the buffer to the watch */
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
        /* update the display */
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
 
        free(bmapbuf);
 }
 
+void test_notification(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mwbuf = mw_alloc_pbuffer(96, 96, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
+
+       mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
+
+       mw_buf_print(mwbuf, 4,  4, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
+       mw_free_pbuffer(mwbuf);
+
+       mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
+}
+
+int days(int m1, int y1)
+{
+       int d;
+       if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
+               d=31;
+       else if(m1==4 || m1==6 || m1==9 || m1==11)
+               d=30;
+       else if((y1%100!=0 && y1%4==0) || y1%400==0)
+               d=29;
+       else
+               d=28;
+       return d;
+}
+
+void draw_idle_calendar(mwdevice_t *mwdevice)
+{
+       long unsigned int t;
+       unsigned int y,y1,m,m1,d,i,j,k;
+       /* const char a[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"}; */
+       const char dnames[7][3] = { "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" };
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len, px, py;
+       char pnum[32];
+       int today;
+       time_t mtime;
+       struct tm mtm;
+                               
+       mtime = time(NULL);
+       localtime_r(&mtime, &mtm);
+
+       today = mtm.tm_mday;
+       y = mtm.tm_year + 1900;
+       m = mtm.tm_mon + 1;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       if (y < 0)
+               y = -y;
+       if (m <= 0 || m >= 13)
+               m = 1;
+       y1 = 0;
+       t = 0;
+       while (y1 < y) {
+               if (((y1 % 100) != 0 && (y1 % 4) == 0) || (y1 % 400) == 0)
+                       t += 366;
+               else
+                       t += 365;
+               y1++;
+       }
+       m1 = 1;
+       while (m1 < m) {
+               d = days(m1, y);
+               t = t + d;
+               m1++;
+       };
+#if 0
+       printf("Year: '%u'\n",y);
+       printf("Month: '%s'\n",a[m-1]);
+
+       printf("%6s%6s%6s%6s%6s%6s%6s\n","Sun","Mon","Tue","Wed","Thu","Fri","Sat");
+#endif
+
+       k = 1;
+       /* 0 = 1st day is Monday, 1 = 1st day is Sunday */
+       d = (t % 7)-1;
+       px=0;
+       py=3;
+       for (i=0; i<7; i++) {
+               snprintf(pnum, 3, "%s", dnames[i]);
+               mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
+               px += 14;
+       };
+       px = 0;
+       py += 10;
+       for (i = 1; i <= days(m,y); i++, k++) {
+               if (i == 1) {
+                       if (d == 0) {
+                               for (j = 1; j < 7; j++, k++) {
+                                       printf("+--"); //printf("%3s","");
+                                       px += 14;
+                               }
+                       } else {
+                               for (j = 1; j < d; j++, k++) {
+                                       px += 14;
+                                       printf("--+"); //printf("%3s","");
+                               }
+                       }
+               }
+               snprintf(pnum, 8, "%2d", i);
+               if (i == today)
+                       mw_buf_print(mwbuf, px, py, pnum, 0, MW_BLACK, MW_WHITE);
+               else
+                       mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
+               px += 12;
+               printf("%2d", i);
+               if ((k % 7) == 0) {
+                       printf("\n");
+                       px = 0;
+                       py += 10;
+               } else {
+                       printf("+");
+                       px += 2;
+               }
+       }
+       // printf("\n");
+
+       //mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+       mw_free_pbuffer(mwbuf);
+}
+
+void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len,i,c,r;
+       char sstr[32];
+
+       mwbuf = mw_alloc_pbuffer(96, 96, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 0,  0, header, 0, MW_BLACK, MW_WHITE);
+
+       i=0;
+       c=0; r=1;
+       memset(sstr,0,32);
+       while (i<strlen(body)) {
+               sstr[c++] = body[i++];
+               if (c>=16 || i>=strlen(body)) {
+                       mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
+                       memset(sstr,0,32);
+                       c=0; r++;
+                       if (r>10)
+                               break;
+               };
+       };
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
+       mw_free_pbuffer(mwbuf);
+
+       mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
+}
+
+void test_application(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_IDLE, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
+
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_B, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_B);
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_C, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_C);
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_E, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_E);
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_F, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_F);
+       mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
+
+       mwbuf = mw_alloc_pbuffer(96, 96, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 0,  0, "  Application   ", 0, MW_BLACK, MW_WHITE);
+       mw_buf_print(mwbuf, 0,  9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
+       mw_free_pbuffer(mwbuf);
+}
+
+void test_oled(mwdevice_t *mwdevice, unsigned char oled)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mwbuf = mw_alloc_pbuffer(80, 16, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 0,  0, "Application", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0,  8, "012345678901234", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
+       mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len);
+
+       mw_free_pbuffer(mwbuf);
+}
+
+static void
+weather_soup_callback (SoupSession *session, SoupMessage *msg, gpointer user_data)
+{
+       xmlDocPtr xdoc;
+
+       /* Handle the response here */
+       if (msg->response_body->data != NULL && msg->response_body->length != 0) {
+               fprintf(stderr, "%s\n", msg->response_body->data);
+               xdoc = xmlReadMemory(msg->response_body->data, msg->response_body->length, "noname.xml", NULL, 0);
+               if (xdoc == NULL) {
+                       fprintf(stderr, "Failed to parse document\n");
+                       return;
+               }
+               xmlFreeDoc(xdoc);
+               xmlCleanupParser();
+       }
+}
+
+void do_weather(mwdata_t *mwdata)
+{
+       mwdata->msoup = soup_session_async_new();
+       mwdata->smsg = soup_message_new ("GET", "http://www.google.com/ig/api?weather=siegen&hl=de");
+       soup_session_send_message (mwdata->msoup, mwdata->smsg);
+       soup_session_queue_message (mwdata->msoup, mwdata->smsg, weather_soup_callback, mwdata);
+}
+
 void print_help(void)
 {
+       g_print("Not yet implemented..\n");
 }
 
-void process_cmd(char *cmdline, int clinep, int mw_fd)
+#define PARAMS_MAX 8
+#define PARAMS_LEN 16
+
+static int separate_params(char *params[], char *pbuf)
+{
+       char *ret;
+       char tbuf[128];
+       int cnt = 0;
+
+       memset(tbuf, 0, 128);
+       strncpy(tbuf, pbuf, strlen(pbuf));
+       // strdump(tbuf, strlen(tbuf));
+       /* we know we do have at least one parameter */
+       ret = strtok(tbuf, " ");
+       while (ret != NULL) {
+               // dbg_printf("tok '%s'\n", ret);
+               strncpy(params[cnt++], ret, PARAMS_LEN-1);
+               if (cnt > PARAMS_MAX)
+                       break;
+               ret = strtok(NULL, " ");
+       }
+
+       return cnt;
+}
+
+static void params_free(char **params)
+{
+       int i;
+       for (i=0; i < PARAMS_MAX; i++)
+               free(params[i]);
+}
+
+void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
 {
        unsigned char mdata[32];
+       unsigned int intopt;
+       char cmd[128], pbuf[128], *params[PARAMS_MAX];
+       int i, pcnt;
+       char *inbuf = cmdline;
 
-       fprintf(stderr, "command: '%s'\n", cmdline);
+       g_print("command: '%s'\n", cmdline);
 
-       if (strncmp(cmdline, "quit", 4) == 0) {
-               close(mw_fd);
-               exit(0);
+       for (i=0; i<PARAMS_MAX; i++) {
+               params[i] = malloc(PARAMS_LEN);
+               memset(params[i], 0, PARAMS_LEN);
+       }
+       memset(cmd, 0, 128);
+       memset(pbuf, 0, 128);
+       i = 0;
+       do {
+               char c;
+               c = *(inbuf++);
+               cmd[i++] = c;
+       } while (*inbuf != 0 && *inbuf != 0x20);
+       i = 0;
+       if (inbuf != NULL && *inbuf == 0x20) {
+               inbuf++;
+               do {
+                       pbuf[i++] = *(inbuf++);
+               } while (*inbuf != 0);
+       };
+       if (strlen(pbuf) > 0)
+               pcnt = separate_params(params, pbuf);
+       else
+               pcnt = 0;
+
+       if (strncmp(cmd, "quit", 4) == 0) {
+               //close(mw_fd);
+               //exit(0);
+               /* just in case we quit and show another screen */
+               mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
+               params_free(params);
+               g_main_loop_quit (mwdata->mloop);
        }
-       if (strncmp(cmdline, "help", 4) == 0) {
+       if (strncmp(cmd, "help", 4) == 0) {
                print_help();
        }
-       if (strncmp(cmdline, "srtc", 4) == 0) {
-               fprintf(stderr, "Setting RTC from system time...");
-               mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
-               fprintf(stderr, "OK\n");
+       if (strncmp(cmd, "ahand", 5) == 0) {
+               if (pcnt > 0) {
+                       intopt = atoi(params[0]); /*atoi(cmdline+5);*/
+                       g_print("Advance analog hands by %d minutes\n", intopt);
+                       mdata[0] = intopt / 60;
+                       mdata[1] = intopt % 60;
+                       mdata[2] = 0;
+                       mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
+               } else
+                       g_print("missing argument\n ahand # - # = no. of minutes\n");
        }
-       if (strncmp(cmdline, "grtc", 4) == 0) {
-               mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
+       if (strncmp(cmd, "srtc", 4) == 0) {
+               time_t mtime;
+               struct tm mtm;
+
+               mtime = time(NULL);
+               localtime_r(&mtime, &mtm);
+
+               g_print("Setting RTC from system time...");
+               mw_set_rtc(&mwdata->mwdevice, &mtm);
+               g_print("OK\n");
        }
-       if (strncmp(cmdline, "gistr", 5) == 0) {
-               mdata[0] = 0;
-               mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
+       if (strncmp(cmd, "grtc", 4) == 0) {
+               mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
        }
-       if (strncmp(cmdline, "gdtype", 6) == 0) {
-               mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
+       if (strncmp(cmd, "gistr", 5) == 0) {
+               if (pcnt > 0)
+                       intopt = atoi(params[0]);
+               if (intopt >= 0 && intopt <= 3)
+                       mdata[0] = intopt;
+               else
+                       mdata[0] = 0;
+               mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
        }
-       if (strncmp(cmdline, "rvbat", 5) == 0) {
-               mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
+       if (strncmp(cmd, "gdtype", 6) == 0) {
+               mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
        }
-       if (strncmp(cmdline, "modecfg", 6) == 0) {
-               mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
-               mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
+       if (strncmp(cmd, "rvbat", 5) == 0) {
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
        }
+       if (strncmp(cmd, "rlight", 6) == 0) {
+               mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
+       }
+       if (strncmp(cmd, "modecfg", 6) == 0) {
+               mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
+               mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
+       }
+#if 0
        if (strncmp(cmdline, "rbtcfg", 6) == 0) {
+               intopt = cmdline[7]-0x30;
                mdata[0] = 0; /* idle screen */
-               mdata[1] = 1; /* button index */
-               mdata[2] = 2; /* button press type */
-               mdata[3] = 3; /* callback message type */
-               mdata[4] = 4; /* callback message option */
-               mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
+               mdata[1] = intopt; /* button index */
+               /* for reading the config those are unnecessary */
+               mdata[2] = 0; /* button press type */
+               mdata[3] = 0; /* callback message type */
+               mdata[4] = 0; /* callback message option */
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
+       }
+#endif
+       if (strncmp(cmd, "svib", 4) == 0) {
+               mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
+       }
+       if (strncmp(cmd, "tbmp", 4) == 0) {
+               bitmap_test(&mwdata->mwdevice);
        }
-       if (strncmp(cmdline, "svib", 4) == 0) {
-               mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
+       if (strncmp(cmd, "t2bmp", 5) == 0) {
+               bitmap_test2(&mwdata->mwdevice);
        }
-       if (strncmp(cmdline, "tbmp", 4) == 0) {
-               bitmap_test(mw_fd);
+       if (strncmp(cmd, "text", 4) == 0) {
+               text_test(&mwdata->mwdevice);
        }
-       if (strncmp(cmdline, "rbmp", 4) == 0) {
-               bitmap_read(mw_fd);
+       if (strncmp(cmd, "tline", 5) == 0) {
+               line_test(&mwdata->mwdevice);
        }
+       if (strncmp(cmd, "rbmp", 4) == 0) {
+               bitmap_read(&mwdata->mwdevice);
+       }
+       if (strncmp(cmd, "tnote", 5) == 0) {
+               // test_notification(&mwdata->mwdevice);
+               mw_do_notification(&mwdata->mwdevice, "TestNotification", "This is a pretty long text that needs to be broken and torn", 1);
+       }
+       if (strncmp(cmd, "tapp", 4) == 0) {
+               test_application(&mwdata->mwdevice);
+       }
+       if (strncmp(cmd, "eoled", 5) == 0) {
+               if (pcnt > 0) {
+                       intopt = atoi(params[0]);
+                       if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
+                               mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
+               }
+       }
+       if (strncmp(cmd, "toled", 5) == 0) {
+               intopt = atoi(params[0]);
+               test_oled(&mwdata->mwdevice, intopt);
+               //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
+               //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
+       }
+       if (strncmp(cmd, "cal", 3) == 0) {
+               draw_idle_calendar(&mwdata->mwdevice);
+       }
+       if (strncmp(cmd, "wet", 3) == 0) {
+               do_weather(mwdata);
+       }
+       if (strncmp(cmd, "c24", 3) == 0) {
+               mdata[0] = MW_RTC_CLOCK_24HR;
+               mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_TIME_FORMAT, 1, mdata);
+       }
+       if (strncmp(cmd, "g24", 3) == 0) {
+               mdata[0] = 0;
+               mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 1, mdata);
+       }
+       if (strncmp(cmd, "sdm", 3) == 0) {
+               mdata[0] = MW_RTC_DATE_DDMM;
+               mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_DATE_FORMAT, 1, mdata);
+       }
+       if (strncmp(cmd, "gdm", 3) == 0) {
+               mdata[0] = 0;
+               mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_DATE_FORMAT, 1, mdata);
+       }
+       params_free(params);
 }
 
-
-int menu(int mw_fd)
+int feed_menu(mwdata_t *mdata)
 {
-       fd_set mfds;
-       struct termios tconfd, tmwfd;
-       char cmdline[128];
-       unsigned char msg_buf[64];
-       unsigned char clinep = 0;
        int rcvd;
 
-       tcgetattr(0, &tconfd);
-       cfmakeraw(&tconfd);
-       tconfd.c_oflag |= ONLCR | OPOST;
-       tconfd.c_lflag |= ISIG;
-       tcsetattr(0, TCSANOW, &tconfd);
-
-       tcgetattr(mw_fd, &tmwfd);
-       cfmakeraw(&tmwfd);
-       tmwfd.c_oflag |= ONLCR | OPOST;
-       tmwfd.c_lflag |= ISIG;
-       tcsetattr(mw_fd, TCSANOW, &tmwfd);
-
-       FD_ZERO(&mfds);
-       FD_SET(0, &mfds);
-       FD_SET(mw_fd, &mfds);
-
-       memset(cmdline, 0, 128);
-
-       do {
-               rcvd = 0;
-               if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
-                       if (FD_ISSET(mw_fd, &mfds)) {
-                               rcvd = read(mw_fd, msg_buf, 64);
-                               fprintf(stderr, "read %d bytes:\n", rcvd);
-                               if (rcvd > 0) {
-                                       dump_frame(msg_buf, rcvd);
-                                       decode_frame(mw_fd, msg_buf, rcvd);
-                               }
-                       };
-                       if (FD_ISSET(0, &mfds)) {
-                               rcvd = read(0, (cmdline+clinep), 1);
-                               if (rcvd > 0) {
-                                       if (cmdline[clinep] == '\r') {
-                                               printf("\n");
-                                               cmdline[clinep--] = '\0';
-                                               process_cmd(cmdline, clinep, mw_fd);
-                                               clinep = 0;
-                                               memset(cmdline, 0, 128);
-                                       } else {
-                                               clinep++;
-                                               if (clinep > 75)
-                                                       clinep = 75;
-                                               printf("\r> %s", cmdline);
-                                               fflush(stdout);
-                                       }
-                               }
-                       };
-               } else
-                       break;
-               FD_ZERO(&mfds);
-               FD_SET(0, &mfds);
-               FD_SET(mw_fd, &mfds);
-       } while (rcvd > 0);
-
+       rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
+       if (rcvd > 0) {
+               if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
+                       printf("\n");
+                       mdata->cmdline[mdata->cmdline_pos--] = '\0';
+                       process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
+                       mdata->cmdline_pos = 0;
+                       memset(mdata->cmdline, 0, 128);
+               } else {
+                       mdata->cmdline_pos++;
+                       if (mdata->cmdline_pos > 75)
+                               mdata->cmdline_pos = 75;
+                       printf("\r> %s", mdata->cmdline);
+                       fflush(stdout);
+               }
+       }
        return 0;
 }
 
-int open_socket(bdaddr_t *bdaddr, uint8_t channel)
+
+gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 {
-       struct sockaddr_rc addr;
-       int sk, opt;
-       int f;
+       mwdata_t *mdata = (mwdata_t *)udata;
+       int rcvd;
 
-       sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
-       if (sk < 0) {
-               fprintf(stderr, "Can't create socket: %s (%d)\n",
-                       strerror(errno), errno);
-                return -1;
-        }
+       rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf, 64);
+#ifdef DEBUG
+       fprintf(stderr, "read %d bytes:\n", rcvd);
+#endif
+       if (rcvd > 0) {
+#ifdef DEBUG
+               dump_frame(mdata->rcvbuf, rcvd);
+#endif
+               mw_feed_msg_buffer(&mdata->mwdevice, mdata->rcvbuf, rcvd);
+       }
 
-/*
-        f = 1;
-        if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
-               fprintf(stderr, "Can't set flushable: %s (%d)\n",
-                       strerror(errno), errno);
-               return -1;
-        }
-*/
-        memset(&addr, 0, sizeof(addr));
-        addr.rc_family = AF_BLUETOOTH;
-        bacpy(&addr.rc_bdaddr, BDADDR_ANY);
-
-        if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-                fprintf(stderr, "Can't bind socket: %s (%d)\n",
-                                                        strerror(errno), errno);
-                close(sk);
-                return -1;
-        }
-
-       /* Set link mode */
-        opt = 0;
-        opt |= RFCOMM_LM_MASTER;
-        opt |= RFCOMM_LM_AUTH;
-/*
-        opt |= RFCOMM_LM_ENCRYPT;
-        opt |= RFCOMM_LM_SECURE;
-*/
-        if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
-                fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
-                                                        strerror(errno), errno);
-                close(sk);
-                return -1;
-        }
-
-        memset(&addr, 0, sizeof(addr));
-        addr.rc_family = AF_BLUETOOTH;
-        bacpy(&addr.rc_bdaddr, bdaddr);
-        addr.rc_channel = channel;
-
-        if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-                fprintf(stderr, "Can't connect: %s (%d)\n",
-                                                        strerror(errno), errno);
-                close(sk);
-                return -1;
-        }
-
-        return sk;
+       return TRUE;
 }
 
-void baswap(bdaddr_t *dst, const bdaddr_t *src)
-{
-        register unsigned char *d = (unsigned char *) dst;
-        register const unsigned char *s = (const unsigned char *) src;
-        register int i;
-
-        for (i = 0; i < 6; i++)
-                d[i] = s[5-i];
-}
-int bachk(const char *str)
+gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 {
-        if (!str)
-                return -1;
-
-        if (strlen(str) != 17)
-                return -1;
-
-        while (*str) {
-                if (!isxdigit(*str++))
-                        return -1;
+       mwdata_t *mdata = (mwdata_t *)udata;
 
-                if (!isxdigit(*str++))
-                        return -1;
+       feed_menu(mdata);
 
-                if (*str == 0)
-                        break;
-
-                if (*str++ != ':')
-                        return -1;
-        }
-
-        return 0;
+       return TRUE;
 }
 
-int str2ba(const char *str, bdaddr_t *ba)
+static DBusHandlerResult
+signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
 {
-        bdaddr_t b;
-        int i;
-
-        if (bachk(str) < 0) {
-                memset(ba, 0, sizeof(*ba));
-                return -1;
-        }
+       //GMainLoop *loop = user_data;
+       mwdata_t *mdata = (mwdata_t *)user_data;
+       DBusError error;
+       char *app_name, *app_icon, *summary, *body; // **actions;
+       int replace_id; // dict, expire;
+
+       /* A signal from the bus saying we are about to be disconnected */
+       if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
+               /* Tell the main loop to quit */
+               g_main_loop_quit (mdata->mloop);
+               /* We have handled this message, don't pass it on */
+               return DBUS_HANDLER_RESULT_HANDLED;
+       }
 
-        for (i = 0; i < 6; i++, str += 3)
-                b.b[i] = strtol(str, NULL, 16);
+       if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify"))    {
+               g_print("method call\n");
+               dbus_error_init (&error);
+               if (dbus_message_get_args(message, &error,
+                       DBUS_TYPE_STRING, &app_name,
+                       DBUS_TYPE_UINT32, &replace_id,
+                       DBUS_TYPE_STRING, &app_icon,
+                       DBUS_TYPE_STRING, &summary,
+                       DBUS_TYPE_STRING, &body,
+                       /*  G_TYPE_STRV, &actions,
+                       DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
+                       DBUS_TYPE_INT32, &expire, */
+                       DBUS_TYPE_INVALID)) {
+                       g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
+                       mw_send_notify(&mdata->mwdevice, summary, body);
+                       /* we just listen, we do not handle it here */
+                       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               } else {
+                       g_print("Notify received, but error getting message: %s\n", error.message);
+                       dbus_error_free (&error);
+               }
+       } else {
+               g_print("Not signal received\n");
+               return DBUS_HANDLER_RESULT_HANDLED;
+       };
 
-        baswap(ba, &b);
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
 
-        return 0;
+void mw_button_event_message_cb(mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data)
+{
+       g_print("button event cb:0x%02x 0x%02x\n", buttons, options);
+       g_print("screen is %d\n", mwdevice->screen_mode);
+       if (mwdevice->screen_mode == MW_SCREEN_MODE_IDLE) {
+               if (buttons == MW_BUTTON_A)
+                       test_application(mwdevice);
+       }
+       if (mwdevice->screen_mode == MW_SCREEN_MODE_APPLICATION) {
+               if (buttons == MW_BUTTON_C)
+                       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 0);
+       }
 }
 
 int main(int argc, char **argv)
 {
        int mw_fd;
+       mwdata_t mdata;
+       struct termios tconfd, otconfd, tmwfd;
        bdaddr_t btaddr;
+       GIOChannel *mw_io, *m_in;
+       DBusConnection *bus;
+       DBusError error;
 
        if (argc != 2) {
                fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
                return 1;
        };
   
+       g_type_init();
+       mdata.mloop = g_main_loop_new (NULL, FALSE);
+
        crc16ccitt_init();
 
 #if USE_RFCOMM_FILE
@@ -375,15 +817,66 @@ int main(int argc, char **argv)
        mw_fd = open_socket(&btaddr, 1);
        if (mw_fd < 0) {
                return 1;
+       } else {
+               fprintf(stderr, "connected to %s\n", argv[1]);
        };
 #endif
+       /* we have a connection, RFCOMM socket is on mw_fd */
+       /* make the tty raw */
+       tcgetattr(mw_fd, &tmwfd);
+       cfmakeraw(&tmwfd);
+       tmwfd.c_oflag |= ONLCR | OPOST;
+       tmwfd.c_lflag |= ISIG;
+       tcsetattr(mw_fd, TCSANOW, &tmwfd);
 
-       menu(mw_fd);
+       mdata.mwdevice.mw_fd = mw_fd;
+       mdata.rcvbuf_pos = 0;
+       memset(mdata.rcvbuf, 0, 128);
 
-       fsync(mw_fd);
+       dbus_error_init (&error);
+       bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
+
+       if (!bus) {
+               g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
+               dbus_error_free (&error);
+               return 1;
+       }
+
+       dbus_connection_setup_with_g_main (bus, NULL);
+       dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
+
+       dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
+
+       // menu(mw_fd);
+       mw_io = g_io_channel_unix_new(mw_fd);
+       g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
+
+       tcgetattr(0, &tconfd);
+       tcgetattr(0, &otconfd);
+       cfmakeraw(&tconfd);
+       tconfd.c_oflag |= ONLCR | OPOST;
+       tconfd.c_lflag |= ISIG;
+       tcsetattr(0, TCSANOW, &tconfd);
+
+       mdata.con_fd = 0;
+
+       m_in = g_io_channel_unix_new(0); /* stdin for kbd */
+       g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
 
+       memset(mdata.cmdline, 0, 128);
+       mdata.cmdline_pos = 0;
+
+       mw_init(&mdata.mwdevice, mw_fd);
+       mw_set_button_event_message_cb(&mdata.mwdevice, mw_button_event_message_cb, NULL);
+
+       g_main_loop_run (mdata.mloop);
+
+       fsync(mw_fd);
        close(mw_fd);
 
+       /* reset the controlling tty again */
+       tcsetattr(0, TCSANOW, &otconfd);
+
        return 0;
 };