]> git.kernelconcepts.de Git - metawatch.git/blobdiff - metawatch.c
Makefile cleanup, start implementation of nval commands (not working yet)
[metawatch.git] / metawatch.c
index 5c5b73bfea3c36f3d939cf84d82d251964d56d6d..2d9b929e2da28c83f32b35294fbb42365f92494e 100644 (file)
@@ -130,6 +130,22 @@ int mw_set_rtc(mwdevice_t *mwdevice, unsigned char clk1224, unsigned char date_f
        return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
 }
 
+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 & 0xff00) >> 8;
+       data[1] = (identifier & 0x00ff);
+       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)
 {
        unsigned char mdata[7];
@@ -292,11 +308,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,6 +352,10 @@ 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)
 {
@@ -480,7 +500,7 @@ void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change
  * Protocol handling
  * ---------------------------------------------------------------------- */
 
-int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
+int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
 {
        unsigned short crc;
        unsigned char msglen;
@@ -535,6 +555,29 @@ int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
                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;
@@ -547,6 +590,38 @@ int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
 }
 
 
+int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
+{
+       char msgbuf[64];
+       int tlen;
+
+       if (len <= 0)
+               return -1;
+
+       memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len);
+       mwdevice->pbuf_len += len;
+       
+       while (mwdevice->pbuf_len > 0) {
+               /* scan for MW_SOF */
+               while (mwdevice->pbuf[0] != MW_SOF) {
+                       memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len);
+               }
+               tlen = mwdevice->pbuf[1];
+               /* OK, there is an SOF but the message is too short */
+               if (tlen > mwdevice->pbuf_len) {
+                       /* we have to wait for more data to come in */
+                       break;
+               }
+               /* here we have a complete msg */
+               memcpy(msgbuf, mwdevice->pbuf, tlen);
+               mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen);
+               memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
+               mwdevice->pbuf_len -= tlen;
+       }
+       return 0;
+}
+
+
 /* ----------------------------------------------------------------------
  * General code usage
  * ---------------------------------------------------------------------- */
@@ -555,6 +630,8 @@ int mw_init(mwdevice_t *mwdevice, int mw_fd)
 {
        memset(mwdevice, 0, sizeof(mwdevice_t));
        mwdevice->mw_fd = mw_fd;
+       memset(mwdevice->pbuf, 0, MW_PBUF_LEN);
+       mwdevice->pbuf_len = 0;
 
        /* figure out which device we run with */
        mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);