]> git.kernelconcepts.de Git - metawatch.git/blobdiff - mw_main.c
Add button message callback handling and sample code
[metawatch.git] / mw_main.c
index 10ce47d8ad24d9b5966dd82a79f79fe102c388b1..566b8fbc4264dd3eff8d72e3dc9dfc027e7e6d32 100644 (file)
--- a/mw_main.c
+++ b/mw_main.c
 #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 "crc16ccitt.h"
 #include "mw_utility.h"
 
+#include "bt_helper.h"
+
 typedef struct {
        GMainLoop *mloop;
-       int mw_fd;              /* MetaWatch RFCOMM BT socket */
+       mwdevice_t mwdevice;
        unsigned char rcvbuf[128];
        int rcvbuf_pos;
        int con_fd;             /* console input fd */
        char cmdline[128];
        int cmdline_pos;
-} mwdata;
+       SoupSession *msoup;
+       SoupMessage *smsg;
+} mwdata_t;
 
-void bitmap_test(int mw_fd)
+void bitmap_test(mwdevice_t *mwdevice)
 {
        /* a nice checker-board pattern */
        unsigned char checkbuf[24] = {
@@ -53,15 +62,15 @@ 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(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
 }
 
-void bitmap_test2(int mw_fd)
+void bitmap_test2(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf1, *mwbuf2;
        unsigned char *bbuf;
@@ -78,8 +87,8 @@ void bitmap_test2(int mw_fd)
                mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
                mw_dump_mw_buffer(mwbuf1);
                bbuf = mw_make_mw_buffer(mwbuf1, &len);
-               mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-               mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+               mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+               mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
                getchar();
        };
 
@@ -87,7 +96,7 @@ void bitmap_test2(int mw_fd)
        mw_free_pbuffer(mwbuf2);
 }
 
-void text_test(int mw_fd)
+void text_test(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -102,12 +111,12 @@ void text_test(int mw_fd)
        mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       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 line_test(int mw_fd)
+void line_test(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -120,12 +129,12 @@ void line_test(int mw_fd)
                mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       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 bitmap_read(mwdevice_t *mwdevice)
 {
        int ffd, ret;
        char rbuf[256];
@@ -186,14 +195,14 @@ void bitmap_read(int mw_fd)
        /* reverse bits and invert the bmap */
        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(int mw_fd)
+void test_notification(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -224,14 +233,130 @@ void test_notification(int mw_fd)
        mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 1);
+       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(mw_fd, 1, 300, 300, 3);
+       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,da,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(int mw_fd, char *header, char *body)
+void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -258,21 +383,27 @@ void mw_send_notify(int mw_fd, char *header, char *body)
        };
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_APPLICATION, 1);
+       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(mw_fd, 1, 300, 300, 2);
+       mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
 }
 
-void test_application(int mw_fd)
+void test_application(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
        int len;
 
-       mw_enable_button(mw_fd, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
-       mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
+       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);
@@ -289,16 +420,60 @@ void test_application(int mw_fd)
        mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_APPLICATION, 1);
+       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, i;
+
+       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)
 {
 }
 
-void process_cmd(char *cmdline, int clinep, int mw_fd)
+void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
 {
        unsigned char mdata[32];
        unsigned int intopt;
@@ -306,20 +481,28 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
        fprintf(stderr, "command: '%s'\n", cmdline);
 
        if (strncmp(cmdline, "quit", 4) == 0) {
-               close(mw_fd);
-               exit(0);
-               // g_main_loop_quit (mdata->mloop);
+               //close(mw_fd);
+               //exit(0);
+               g_main_loop_quit (mwdata->mloop);
        }
        if (strncmp(cmdline, "help", 4) == 0) {
                print_help();
        }
+       if (strncmp(cmdline, "ahand", 5) == 0) {
+               intopt = 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);
+       }
        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);
+               mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
                fprintf(stderr, "OK\n");
        }
        if (strncmp(cmdline, "grtc", 4) == 0) {
-               mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
        }
        if (strncmp(cmdline, "gistr", 5) == 0) {
                intopt = cmdline[6]-0x30;
@@ -327,20 +510,20 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
                        mdata[0] = intopt;
                else
                        mdata[0] = 0;
-               mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
+               mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
        }
        if (strncmp(cmdline, "gdtype", 6) == 0) {
-               mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
        }
        if (strncmp(cmdline, "rvbat", 5) == 0) {
-               mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
        }
        if (strncmp(cmdline, "rlight", 6) == 0) {
-               mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 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);
+               mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
+               mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
        }
        if (strncmp(cmdline, "rbtcfg", 6) == 0) {
                intopt = cmdline[7]-0x30;
@@ -350,36 +533,63 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
                mdata[2] = 0; /* button press type */
                mdata[3] = 0; /* callback message type */
                mdata[4] = 0; /* callback message option */
-               mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
        }
        if (strncmp(cmdline, "svib", 4) == 0) {
-               mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
+               mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
        }
        if (strncmp(cmdline, "tbmp", 4) == 0) {
-               bitmap_test(mw_fd);
+               bitmap_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "t2bmp", 5) == 0) {
-               bitmap_test2(mw_fd);
+               bitmap_test2(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "text", 4) == 0) {
-               text_test(mw_fd);
+               text_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "tline", 5) == 0) {
-               line_test(mw_fd);
+               line_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "rbmp", 4) == 0) {
-               bitmap_read(mw_fd);
+               bitmap_read(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "tnote", 5) == 0) {
-               test_notification(mw_fd);
+               // 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(cmdline, "tapp", 4) == 0) {
-               test_application(mw_fd);
+               test_application(&mwdata->mwdevice);
+       }
+       if (strncmp(cmdline, "eoled", 5) == 0) {
+               intopt = cmdline[6]-0x30;
+               if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
+                       mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
+       }
+       if (strncmp(cmdline, "toled", 5) == 0) {
+               intopt = cmdline[6]-0x30;
+               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(cmdline, "cal", 3) == 0) {
+               draw_idle_calendar(&mwdata->mwdevice);
+       }
+       if (strncmp(cmdline, "wet", 3) == 0) {
+               do_weather(mwdata);
+       }
+       if (strncmp(cmdline, "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(cmdline, "g24", 3) == 0) {
+               mdata[0] = 0;
+               mdata[1] = 0;
+               mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 2, mdata);
        }
 }
 
 
-int feed_menu(mwdata *mdata)
+int feed_menu(mwdata_t *mdata)
 {
        int rcvd;
 
@@ -388,7 +598,7 @@ int feed_menu(mwdata *mdata)
                if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
                        printf("\n");
                        mdata->cmdline[mdata->cmdline_pos--] = '\0';
-                       process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata->mw_fd);
+                       process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
                        mdata->cmdline_pos = 0;
                        memset(mdata->cmdline, 0, 128);
                } else {
@@ -402,127 +612,13 @@ int feed_menu(mwdata *mdata)
        return 0;
 }
 
-int open_socket(bdaddr_t *bdaddr, uint8_t channel)
-{
-       struct sockaddr_rc addr;
-       int sk, opt;
-       //int f;
-
-       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;
-        }
-
-/*
-        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;
-}
-
-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)
-{
-        if (!str)
-                return -1;
-
-        if (strlen(str) != 17)
-                return -1;
-
-        while (*str) {
-                if (!isxdigit(*str++))
-                        return -1;
-
-                if (!isxdigit(*str++))
-                        return -1;
-
-                if (*str == 0)
-                        break;
-
-                if (*str++ != ':')
-                        return -1;
-        }
-
-        return 0;
-}
-
-int str2ba(const char *str, bdaddr_t *ba)
-{
-        bdaddr_t b;
-        int i;
-
-        if (bachk(str) < 0) {
-                memset(ba, 0, sizeof(*ba));
-                return -1;
-        }
-
-        for (i = 0; i < 6; i++, str += 3)
-                b.b[i] = strtol(str, NULL, 16);
-
-        baswap(ba, &b);
-
-        return 0;
-}
 
 gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 {
-       mwdata *mdata = (mwdata *)udata;
+       mwdata_t *mdata = (mwdata_t *)udata;
        int rcvd;
 
-       rcvd = read(mdata->mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64);
+       rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf, 64);
 #ifdef DEBUG
        fprintf(stderr, "read %d bytes:\n", rcvd);
 #endif
@@ -530,8 +626,7 @@ gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 #ifdef DEBUG
                dump_frame(mdata->rcvbuf, rcvd);
 #endif
-               decode_frame(mdata->mw_fd, mdata->rcvbuf, rcvd);
-               mdata->rcvbuf_pos = 0;
+               mw_feed_msg_buffer(&mdata->mwdevice, mdata->rcvbuf, rcvd);
        }
 
        return TRUE;
@@ -539,7 +634,7 @@ gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 
 gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
 {
-       mwdata *mdata = (mwdata *)udata;
+       mwdata_t *mdata = (mwdata_t *)udata;
 
        feed_menu(mdata);
 
@@ -550,7 +645,7 @@ static DBusHandlerResult
 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
 {
        //GMainLoop *loop = user_data;
-       mwdata *mdata = (mwdata *)user_data;
+       mwdata_t *mdata = (mwdata_t *)user_data;
        DBusError error;
        char *app_name, *app_icon, *summary, *body; // **actions;
        int replace_id; // dict, expire;
@@ -577,7 +672,7 @@ signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data
                        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->mw_fd, 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 {
@@ -592,10 +687,24 @@ signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
+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 mdata;
+       mwdata_t mdata;
        struct termios tconfd, otconfd, tmwfd;
        bdaddr_t btaddr;
        GIOChannel *mw_io, *m_in;
@@ -636,7 +745,7 @@ int main(int argc, char **argv)
        tmwfd.c_lflag |= ISIG;
        tcsetattr(mw_fd, TCSANOW, &tmwfd);
 
-       mdata.mw_fd = mw_fd;
+       mdata.mwdevice.mw_fd = mw_fd;
        mdata.rcvbuf_pos = 0;
        memset(mdata.rcvbuf, 0, 128);
 
@@ -673,6 +782,9 @@ int main(int argc, char **argv)
        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);