2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
31 #include "metawatch.h"
32 #include "metawatch_protocol.h"
33 #include "crc16ccitt.h"
37 const char *mw_screen_mode_names[] = {
40 "notification screen",
44 const char *mw_status_string[] = {
52 #define MW_FRAME_DELAY 0x00
54 /* ----------------------------------------------------------------------
56 * ---------------------------------------------------------------------- */
58 void dump_frame(unsigned char *frame, int len)
63 fprintf(stderr, "0x%02x ", frame[i]);
64 fprintf(stderr, "\n");
68 int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
71 unsigned char frame[64];
72 int tlen = len + 6; /* payload + 6 bytes frameing */
80 if (data != NULL && len > 0)
81 memcpy(frame+4, data, len);
83 crc = crc16ccitt(frame, len+4);
84 *(unsigned short *)(frame+len+4) = crc;
87 dump_frame(frame, tlen);
90 while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0))
94 usleep(MW_FRAME_DELAY);
96 if (tlen == 0 && ret >= 0)
103 /* ----------------------------------------------------------------------
104 * Host to watch commands
105 * ---------------------------------------------------------------------- */
108 int mw_set_rtc(mwdevice_t *mwdevice)
113 unsigned char data[32];
116 localtime_r(&mtime, &mtm);
118 year = mtm.tm_year + 1900;
119 data[0] = (year & 0x0f00) >> 8;
120 data[1] = (year & 0x00ff);
121 data[2] = mtm.tm_mon + 1;
122 data[3] = mtm.tm_mday;
123 data[4] = mtm.tm_wday;
124 data[5] = mtm.tm_hour;
125 data[6] = mtm.tm_min;
126 data[7] = mtm.tm_sec;
128 return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 8);
131 int mw_nval_operation(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
133 unsigned char data[32];
135 data[0] = (identifier & 0x00ff);
136 data[1] = (identifier & 0xff00) >> 8;
138 if (size > 0 && mdata != NULL)
139 memcpy((data+3), mdata, size);
141 if (operation == MW_NVAL_OPERATION_READ || operation == MW_NVAL_OPERATION_WRITE) {
142 return mw_send_frame(mwdevice, MW_NVAL_OPERATION, operation, data, 3+size);
147 int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
149 unsigned char mdata[7];
152 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
153 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
156 return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6);
159 int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
161 unsigned char mdata[3];
163 mdata[0] = timeout; /* seconds */
164 mdata[1] = invert; /* 0=normal, 1=invert */
166 return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
169 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
171 mwdevice->screen_mode = (mode & 0x0f);
172 return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
175 int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select)
177 return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
181 * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
182 * this is only for digital LCD watch
184 int mw_write_buffer(mwdevice_t *mwdevice,
186 unsigned char numlines, /* number of lines, 0=two lines or 1=one line */
187 unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */
188 unsigned char *buffer, int buflen)
190 unsigned char mdata[32];
192 if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI)
195 buflen = 12 * (buflen / 12); /* crop to 12 bytes */
196 if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
197 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
200 memset(mdata, 0, 32);
201 mdata[0] = row_offset;
202 memcpy((mdata+1), buffer, 12);
204 mdata[13] = row_offset+1;
205 memcpy((mdata+14), (buffer+12), 12);
208 return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
213 B0 : row select, 0 first row, 1 second row
214 B1 : display select, 0 upper OLED, 1 lower OLED
215 B2 : if 1 send an event upon completion
219 B0: First buffer in a chain of scroll buffers
220 B1: Any buffer except first or last
221 B2: Last buffer of a chain
222 B3: reserved / unused
227 int mw_write_oled_buffer(mwdevice_t *mwdevice,
228 unsigned char mode, /* idle or scroll */
229 unsigned char oled, /* which OLED */
230 unsigned char numcols,
231 unsigned char col_index, /* starting index */
232 unsigned char *buffer, int buflen)
234 unsigned char mdata[32];
237 if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI)
240 fprintf(stderr, "write oled buf len = %d\n", buflen);
241 /* lower row first since display wil be updated after completion of upper row */
243 for (i=80; i<buflen; i+=20) {
245 memcpy((mdata+1), (buffer+i), 20);
246 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
249 for (i=0; i<80; i+=20) {
251 memcpy((mdata+1), (buffer+i), 20);
252 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
258 int mw_enable_button(mwdevice_t *mwdevice,
260 unsigned char button_index,
261 unsigned char press_type,
262 unsigned char callback_type,
263 unsigned char callback_option)
265 unsigned char mdata[32];
267 memset(mdata, 0, 32);
269 mdata[1] = button_index;
270 mdata[2] = press_type;
271 mdata[3] = callback_type;
272 mdata[4] = callback_option;
274 return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 5);
277 int mw_disable_button(mwdevice_t *mwdevice,
279 unsigned char button_index,
280 unsigned char press_type)
282 unsigned char mdata[32];
284 memset(mdata, 0, 32);
286 mdata[1] = button_index;
287 mdata[2] = press_type;
289 return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 3);
292 int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned char minutes, unsigned char seconds)
294 unsigned char mdata[4];
307 return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
311 /* ----------------------------------------------------------------------
312 * Watch responses, events or notifications
313 * ---------------------------------------------------------------------- */
315 int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype)
318 fprintf(stderr, "Got device type ");
321 fprintf(stderr, "Reserved\n");
324 fprintf(stderr, "Ana-Digi\n");
327 fprintf(stderr, "Digital\n");
330 fprintf(stderr, "Development Board Digital\n");
333 fprintf(stderr, "Development Board Ana-Digi\n");
336 fprintf(stderr, "unknown %d\n", devtype);
340 mwdevice->devtype = devtype;
341 if (mwdevice->mw_get_device_type_response_cb != NULL)
342 mwdevice->mw_get_device_type_response_cb(mwdevice, devtype, mwdevice->mw_gdtypersp_data);
346 void mw_set_get_device_type_response_cb(mwdevice_t *mwdevice, void (*mw_get_device_type_response_cb) (mwdevice_t *mwdevice, unsigned char devtype, void *user_data), void *user_data)
348 if (mw_get_device_type_response_cb != NULL)
349 mwdevice->mw_get_device_type_response_cb = mw_get_device_type_response_cb;
350 if (user_data != NULL)
351 mwdevice->mw_gdtypersp_data = user_data;
354 int mw_nval_operation_response(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
359 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
365 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
369 year = *(unsigned short *)rtcrsp;
370 mtm.tm_year = year - 1900;
371 mtm.tm_mon = rtcrsp[2] - 1;
372 mtm.tm_mday = rtcrsp[3];
373 mtm.tm_wday = rtcrsp[4];
374 mtm.tm_hour = rtcrsp[5];
375 mtm.tm_min = rtcrsp[6];
376 mtm.tm_sec = rtcrsp[7];
379 fprintf(stderr, "watch RTC is %s\n", asctime(&mtm));
381 if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
382 mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
387 void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_real_time_clock_response_cb) (mwdevice_t *mwdevice, struct tm *mw_tm, void *user_data), void *user_data)
389 if (mw_get_real_time_clock_response_cb != NULL)
390 mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb;
391 if (user_data != NULL)
392 mwdevice->mw_grtcrsp_data = user_data;
396 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
398 unsigned char power_good = batrsp[0];
399 unsigned char bat_charging = batrsp[1];
400 unsigned short voltage = *(unsigned short *)(batrsp+2);
401 unsigned short voltage_average = *(unsigned short *)(batrsp+4);
404 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");
407 if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
408 mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
413 void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data)
415 if (mw_get_battery_voltage_response_cb != NULL)
416 mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
417 if (user_data != NULL)
418 mwdevice->mw_gbatvrsp_data = user_data;
421 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
424 fprintf(stderr, "read button config response\n");
425 fprintf(stderr, "screen mode : 0x%02x\n", btnrsp[0]);
426 fprintf(stderr, "button index : 0x%02x\n", btnrsp[1]);
427 fprintf(stderr, "mask table : 0x%02x (", btnrsp[2]);
428 fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
429 fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
430 fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
431 fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
432 fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
433 fprintf(stderr, ")\n");
434 fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
435 fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
441 void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_button_config_response_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
443 if (mw_read_button_config_response_cb != NULL)
444 mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb;
445 if (user_data != NULL)
446 mwdevice->mw_rbtncnfrsp_data = user_data;
449 int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts)
452 fprintf(stderr, " button %d options %d\n", btnevt[0], opts);
455 if (mwdevice->mw_button_event_message_cb != NULL)
456 mwdevice->mw_button_event_message_cb(mwdevice, btnevt[0], opts, mwdevice->mw_btnevmsg_data);
461 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)
463 if (mw_button_event_message_cb != NULL)
464 mwdevice->mw_button_event_message_cb = mw_button_event_message_cb;
465 if (user_data != NULL)
466 mwdevice->mw_btnevmsg_data = user_data;
469 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
471 unsigned char power_good = lightrsp[0];
472 unsigned char bat_charging = lightrsp[1];
473 unsigned short light_level = *(unsigned short *)(lightrsp+2);
476 fprintf(stderr, "light sensor is at %d, power stat: %s and %s\n", light_level, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
479 if (mwdevice->mw_read_light_sensor_response_cb != NULL)
480 mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data);
485 void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, void *user_data), void *user_data)
487 if (mw_read_light_sensor_response_cb != NULL)
488 mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
489 if (user_data != NULL)
490 mwdevice->mw_rlsrsp_data = user_data;
493 int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len)
495 unsigned char mode = (option & 0x0f);
496 unsigned char status = statrsp[0];
498 fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[mode], mw_status_string[status]);
501 // mwdevice->screen_mode = mode;
502 if (mwdevice->mw_status_change_event_cb != NULL)
503 mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
508 void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change_event_cb) (mwdevice_t *mwdevice, unsigned char *scrmode, unsigned char *status, void *user_data), void *user_data)
510 if (mw_status_change_event_cb != NULL)
511 mwdevice->mw_status_change_event_cb = mw_status_change_event_cb;
512 if (user_data != NULL)
513 mwdevice->mw_stchev_data = user_data;
517 /* ----------------------------------------------------------------------
519 * ---------------------------------------------------------------------- */
521 int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
524 unsigned char msglen;
525 unsigned char msgtype;
526 unsigned char msgopt;
527 unsigned char *msgdata;
528 unsigned char msgdatalen;
531 crc = *(unsigned short *)(buf+len-2);
532 if (crc != crc16ccitt(buf, len-2)) {
533 fprintf(stderr, "decode frame CRC error\n");
536 if (buf[0] != MW_SOF) {
537 fprintf(stderr, "decode frame SOF not found\n");
545 msgdatalen = msglen - 4 - 2;
548 case MW_GET_DEVICE_TYPE_RSP:
549 mw_get_device_type_response(mwdevice, msgdata[0]);
551 case MW_GET_INFORMATION_STRING_RSP:
553 fprintf(stderr, "Got info string '%s'\n", msgdata);
555 case MW_GET_REAL_TIME_CLOCK_RSP:
556 mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen);
558 case MW_READ_BATTERY_VOLTAGE_RSP:
559 mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen);
561 case MW_READ_LIGHT_SENSOR_RSP:
562 mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
564 case MW_LOW_BATTERY_WARNING_MSG:
565 fprintf(stderr, "Watch battery low, please connect charger\n");
567 case MW_READ_BUTTON_CONFIG_RSP:
568 mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
570 case MW_BUTTON_EVENT_MESSAGE:
571 fprintf(stderr, "Button event message\n");
572 mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt);
574 case MW_LOW_BATTERY_BT_OFF_MSG:
575 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
577 case MW_NVAL_OPERATION_RSP:
578 fprintf(stderr, "NVAL operation response - ");
581 fprintf(stderr, "success\n");
584 fprintf(stderr, "failure!\n");
587 fprintf(stderr, "Item Not initialized (Identifier Not found\n");
590 fprintf(stderr, "Operation failed\n");
593 fprintf(stderr, "Bad Item length\n");
596 fprintf(stderr, "unknown status 0x%02x\n", msgopt);
600 case MW_STATUS_CHANGE_EVENT:
601 mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
604 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
612 int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
620 memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len);
621 mwdevice->pbuf_len += len;
623 while (mwdevice->pbuf_len > 0) {
624 /* scan for MW_SOF */
625 while (mwdevice->pbuf[0] != MW_SOF) {
626 memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len);
628 tlen = mwdevice->pbuf[1];
629 /* OK, there is an SOF but the message is too short */
630 if (tlen > mwdevice->pbuf_len) {
631 /* we have to wait for more data to come in */
634 /* here we have a complete msg */
635 memcpy(msgbuf, mwdevice->pbuf, tlen);
636 mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen);
637 memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
638 mwdevice->pbuf_len -= tlen;
644 /* ----------------------------------------------------------------------
646 * ---------------------------------------------------------------------- */
648 int mw_init(mwdevice_t *mwdevice, int mw_fd)
650 memset(mwdevice, 0, sizeof(mwdevice_t));
651 mwdevice->mw_fd = mw_fd;
652 memset(mwdevice->pbuf, 0, MW_PBUF_LEN);
653 mwdevice->pbuf_len = 0;
655 /* figure out which device we run with */
656 mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
661 /* ----------------------------------------------------------------------
662 * Convenience functions not strictly part of the protocol
663 * ---------------------------------------------------------------------- */
665 int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height)
667 if (width == NULL || height == NULL)
670 switch (mwdevice->devtype) {
671 case MW_DEVICE_TYPE_RESERVED:
673 case MW_DEVICE_TYPE_ANA_DIGI:
674 case MW_DEVICE_TYPE_DEVB_ANA_DIGI:
678 case MW_DEVICE_TYPE_DIGITAL:
679 case MW_DEVICE_TYPE_DEVB_DIGI:
691 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
692 if invert=1 each byte is inverted
694 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
703 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
704 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
707 *buf = invert ? ~tmp : tmp;
713 void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
718 unsigned int y, rowlength;
719 unsigned char mw_buf[24];
721 rowlength = (((width-1) / 8) + 1);
722 if ((height + offset) > 96)
723 height = 96 - offset;
726 fprintf(stderr, "row length = %d bytes\n", rowlength);
727 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
728 fprintf(stderr, "\n");
729 for (y=0; y<height; y++) {
730 for (x=0; x<rowlength; x++) {
732 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
734 fprintf(stderr, "\n");
736 fprintf(stderr, "\n");
739 for (y=0; y<height; y+=2) {
740 memset(mw_buf, 0, 24);
741 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
742 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
743 mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24);