Aff date format setting command, from Oleg Titov
[metawatch.git] / metawatch.c
index 4576d9f..a4ac234 100644 (file)
@@ -105,7 +105,7 @@ int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char op
  * ---------------------------------------------------------------------- */
 
 
-int mw_set_rtc(mwdevice_t *mwdevice, unsigned char clk1224, unsigned char date_fmt)
+int mw_set_rtc(mwdevice_t *mwdevice)
 {
        time_t mtime;
        struct tm mtm;
@@ -124,10 +124,24 @@ int mw_set_rtc(mwdevice_t *mwdevice, unsigned char clk1224, unsigned char date_f
        data[5] = mtm.tm_hour;
        data[6] = mtm.tm_min;
        data[7] = mtm.tm_sec;
-       data[8] = clk1224;
-       data[9] = date_fmt;
 
-       return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
+       return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 8);
+}
+
+int mw_nval_operation(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
+{
+       unsigned char data[32];
+
+       data[0] = (identifier & 0x00ff);
+       data[1] = (identifier & 0xff00) >> 8;
+       data[2] = size;
+       if (size > 0 && mdata != NULL)
+               memcpy((data+3), mdata, size);
+
+       if (operation == MW_NVAL_OPERATION_READ || operation == MW_NVAL_OPERATION_WRITE) {
+               return mw_send_frame(mwdevice, MW_NVAL_OPERATION, operation, data, 3+size);
+       } else
+               return -1;
 }
 
 int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
@@ -154,6 +168,7 @@ int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned c
 
 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
 {
+       mwdevice->screen_mode = (mode & 0x0f);
        return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
 }
 
@@ -292,11 +307,11 @@ int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned c
        return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
 }
 
+
 /* ----------------------------------------------------------------------
  * Watch responses, events or notifications
  * ---------------------------------------------------------------------- */
 
-
 int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype)
 {
 #ifdef DEBUG
@@ -336,14 +351,17 @@ void mw_set_get_device_type_response_cb(mwdevice_t *mwdevice, void (*mw_get_devi
                mwdevice->mw_gdtypersp_data = user_data;
 }
 
+int mw_nval_operation_response(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
+{
+       return -1;
+}
 
 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
 {
        struct tm mtm;
        unsigned short year;
-       unsigned char clk1224, date_fmt;
 
-       if (len != 10) {
+       if (len != 8) {
                fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
                return -1;
        }
@@ -356,11 +374,9 @@ int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp,
        mtm.tm_hour = rtcrsp[5];
        mtm.tm_min = rtcrsp[6];
        mtm.tm_sec = rtcrsp[7];
-       clk1224 = rtcrsp[8];
-       date_fmt = rtcrsp[9];
 
 #ifdef DEBUG
-       fprintf(stderr, "watch RTC is %s, clock format is %s, date format is %s\n", asctime(&mtm), clk1224 ? "24h" : "AM/PM", date_fmt ? "DD/MM" : "MM/DD");
+       fprintf(stderr, "watch RTC is %s\n", asctime(&mtm));
 #endif
        if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
                mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
@@ -379,12 +395,13 @@ void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_
 
 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
 {
-       unsigned short voltage = *(unsigned short *)batrsp;
-       unsigned char power_good = batrsp[2];
-       unsigned char bat_charging = batrsp[3];
+       unsigned char power_good = batrsp[0];
+       unsigned char bat_charging = batrsp[1];
+       unsigned short voltage = *(unsigned short *)(batrsp+2);
+       unsigned short voltage_average = *(unsigned short *)(batrsp+4);
 
 #ifdef DEBUG
-       fprintf(stderr, "battery is at %dmV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
+       fprintf(stderr, "battery is at %dmV (average is %dmV), %s and %s\n", voltage, voltage_average, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
 #endif
 
        if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
@@ -429,6 +446,26 @@ void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_
                mwdevice->mw_rbtncnfrsp_data = user_data;
 }
 
+int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts)
+{
+#ifdef DEBUG
+       fprintf(stderr, "  button %d options %d\n", btnevt[0], opts);
+#endif
+
+       if (mwdevice->mw_button_event_message_cb != NULL)
+               mwdevice->mw_button_event_message_cb(mwdevice, btnevt[0], opts, mwdevice->mw_btnevmsg_data);
+
+       return 0;
+}
+
+void mw_set_button_event_message_cb(mwdevice_t *mwdevice, void (*mw_button_event_message_cb) (mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data), void *user_data)
+{
+       if (mw_button_event_message_cb != NULL)
+               mwdevice->mw_button_event_message_cb = mw_button_event_message_cb;
+       if (user_data != NULL)
+               mwdevice->mw_btnevmsg_data = user_data;
+}
+
 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
 {
        unsigned char power_good = lightrsp[0];
@@ -458,9 +495,10 @@ int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned
        unsigned char mode = (option & 0x0f);
        unsigned char status = statrsp[0];
 #ifdef DEBUG
-       fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);      
+       fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[mode], mw_status_string[status]); 
 #endif
 
+       // mwdevice->screen_mode = mode;
        if (mwdevice->mw_status_change_event_cb != NULL)
                mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
 
@@ -531,10 +569,34 @@ int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
                        break;
                case MW_BUTTON_EVENT_MESSAGE:
                        fprintf(stderr, "Button event message\n");
+                       mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt);
                        break;
                case MW_LOW_BATTERY_BT_OFF_MSG:
                        fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
                        break;
+               case MW_NVAL_OPERATION_RSP:
+                       fprintf(stderr, "NVAL operation response - ");
+                       switch (msgopt) {
+                               case 0x00:
+                                       fprintf(stderr, "success\n");
+                                       break;
+                               case 0x01:
+                                       fprintf(stderr, "failure!\n");
+                                       break;
+                               case 0x09:
+                                       fprintf(stderr, "Item Not initialized (Identifier Not found\n");
+                                       break;
+                               case 0x0a:
+                                       fprintf(stderr, "Operation failed\n");
+                                       break;
+                               case 0x0c:
+                                       fprintf(stderr, "Bad Item length\n");
+                                       break;
+                               default:
+                                       fprintf(stderr, "unknown status 0x%02x\n", msgopt);
+                                       break;
+                       };
+                       break;
                case MW_STATUS_CHANGE_EVENT:
                        mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
                        break;
@@ -571,7 +633,7 @@ int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
                }
                /* here we have a complete msg */
                memcpy(msgbuf, mwdevice->pbuf, tlen);
-               mw_decode_frame(mwdevice, msgbuf, tlen);
+               mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen);
                memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
                mwdevice->pbuf_len -= tlen;
        }