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, unsigned char clk1224, unsigned char date_fmt)
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;
130 return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
133 int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
135 unsigned char mdata[7];
138 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
139 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
142 return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6);
145 int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
147 unsigned char mdata[3];
149 mdata[0] = timeout; /* seconds */
150 mdata[1] = invert; /* 0=normal, 1=invert */
152 return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
155 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
157 return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
160 int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select)
162 return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
166 * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
167 * this is only for digital LCD watch
169 int mw_write_buffer(mwdevice_t *mwdevice,
171 unsigned char numlines, /* number of lines, 0=two lines or 1=one line */
172 unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */
173 unsigned char *buffer, int buflen)
175 unsigned char mdata[32];
177 if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI)
180 buflen = 12 * (buflen / 12); /* crop to 12 bytes */
181 if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
182 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
185 memset(mdata, 0, 32);
186 mdata[0] = row_offset;
187 memcpy((mdata+1), buffer, 12);
189 mdata[13] = row_offset+1;
190 memcpy((mdata+14), (buffer+12), 12);
193 return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
198 B0 : row select, 0 first row, 1 second row
199 B1 : display select, 0 upper OLED, 1 lower OLED
200 B2 : if 1 send an event upon completion
204 B0: First buffer in a chain of scroll buffers
205 B1: Any buffer except first or last
206 B2: Last buffer of a chain
207 B3: reserved / unused
212 int mw_write_oled_buffer(mwdevice_t *mwdevice,
213 unsigned char mode, /* idle or scroll */
214 unsigned char oled, /* which OLED */
215 unsigned char numcols,
216 unsigned char col_index, /* starting index */
217 unsigned char *buffer, int buflen)
219 unsigned char mdata[32];
222 if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI)
225 /* lower row first since display wil be updated after completion of upper row */
227 for (i=80; i<buflen; i+=20) {
229 memcpy((mdata+1), (buffer+i), 20);
230 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
233 for (i=0; i<80; i+=20) {
235 memcpy((mdata+1), (buffer+i), 20);
236 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
241 int mw_enable_button(mwdevice_t *mwdevice,
243 unsigned char button_index,
244 unsigned char press_type,
245 unsigned char callback_type,
246 unsigned char callback_option)
248 unsigned char mdata[32];
250 memset(mdata, 0, 32);
252 mdata[1] = button_index;
253 mdata[2] = press_type;
254 mdata[3] = callback_type;
255 mdata[4] = callback_option;
257 return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 5);
260 int mw_disable_button(mwdevice_t *mwdevice,
262 unsigned char button_index,
263 unsigned char press_type)
265 unsigned char mdata[32];
267 memset(mdata, 0, 32);
269 mdata[1] = button_index;
270 mdata[2] = press_type;
272 return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 3);
275 /* ----------------------------------------------------------------------
276 * Watch responses, events or notifications
277 * ---------------------------------------------------------------------- */
280 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
284 unsigned char clk1224, date_fmt;
287 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
291 year = *(unsigned short *)rtcrsp;
292 mtm.tm_year = year - 1900;
293 mtm.tm_mon = rtcrsp[2] - 1;
294 mtm.tm_mday = rtcrsp[3];
295 mtm.tm_wday = rtcrsp[4];
296 mtm.tm_hour = rtcrsp[5];
297 mtm.tm_min = rtcrsp[6];
298 mtm.tm_sec = rtcrsp[7];
300 date_fmt = rtcrsp[9];
303 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");
305 if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
306 mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
311 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)
313 if (mw_get_real_time_clock_response_cb != NULL)
314 mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb;
315 if (user_data != NULL)
316 mwdevice->mw_grtcrsp_data = user_data;
320 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
322 unsigned short voltage = *(unsigned short *)batrsp;
323 unsigned char power_good = batrsp[2];
324 unsigned char bat_charging = batrsp[3];
327 fprintf(stderr, "battery is at %dmV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
330 if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
331 mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
336 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)
338 if (mw_get_battery_voltage_response_cb != NULL)
339 mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
340 if (user_data != NULL)
341 mwdevice->mw_gbatvrsp_data = user_data;
344 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
347 fprintf(stderr, "read button config response\n");
348 fprintf(stderr, "screen mode : 0x%02x\n", btnrsp[0]);
349 fprintf(stderr, "button index : 0x%02x\n", btnrsp[1]);
350 fprintf(stderr, "mask table : 0x%02x (", btnrsp[2]);
351 fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
352 fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
353 fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
354 fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
355 fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
356 fprintf(stderr, ")\n");
357 fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
358 fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
364 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)
366 if (mw_read_button_config_response_cb != NULL)
367 mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb;
368 if (user_data != NULL)
369 mwdevice->mw_rbtncnfrsp_data = user_data;
372 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
374 unsigned char power_good = lightrsp[0];
375 unsigned char bat_charging = lightrsp[1];
376 unsigned short light_level = *(unsigned short *)(lightrsp+2);
379 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");
382 if (mwdevice->mw_read_light_sensor_response_cb != NULL)
383 mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data);
388 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)
390 if (mw_read_light_sensor_response_cb != NULL)
391 mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
392 if (user_data != NULL)
393 mwdevice->mw_rlsrsp_data = user_data;
396 int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len)
398 unsigned char mode = (option & 0x0f);
399 unsigned char status = statrsp[0];
401 fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
404 if (mwdevice->mw_status_change_event_cb != NULL)
405 mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
410 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)
412 if (mw_status_change_event_cb != NULL)
413 mwdevice->mw_status_change_event_cb = mw_status_change_event_cb;
414 if (user_data != NULL)
415 mwdevice->mw_stchev_data = user_data;
419 /* ----------------------------------------------------------------------
421 * ---------------------------------------------------------------------- */
423 int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
426 unsigned char msglen;
427 unsigned char msgtype;
428 unsigned char msgopt;
429 unsigned char *msgdata;
430 unsigned char msgdatalen;
433 crc = *(unsigned short *)(buf+len-2);
434 if (crc != crc16ccitt(buf, len-2)) {
435 fprintf(stderr, "decode frame CRC error\n");
440 fprintf(stderr, "decode frame CRC OK\n");
442 if (buf[0] != MW_SOF) {
443 fprintf(stderr, "decode frame SOF not found\n");
448 fprintf(stderr, "decode frame found SOF\n");
455 msgdatalen = msglen - 4 - 2;
458 case MW_GET_DEVICE_TYPE_RSP:
459 fprintf(stderr, "Got device type ");
462 fprintf(stderr, "Reserved\n");
465 fprintf(stderr, "Ana-Digi\n");
468 fprintf(stderr, "Digital\n");
471 fprintf(stderr, "Development Board Digital\n");
474 fprintf(stderr, "Development Board Ana-Digi\n");
477 fprintf(stderr, "unknown %d\n", msgdata[0]);
480 mwdevice->devtype = msgdata[0];
482 case MW_GET_INFORMATION_STRING_RSP:
484 fprintf(stderr, "Got info string '%s'\n", msgdata);
486 case MW_GET_REAL_TIME_CLOCK_RSP:
487 mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen);
489 case MW_READ_BATTERY_VOLTAGE_RSP:
490 mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen);
492 case MW_READ_LIGHT_SENSOR_RSP:
493 mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
495 case MW_LOW_BATTERY_WARNING_MSG:
496 fprintf(stderr, "Watch battery low, please connect charger\n");
498 case MW_READ_BUTTON_CONFIG_RSP:
499 mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
501 case MW_BUTTON_EVENT_MESSAGE:
502 fprintf(stderr, "Button event message\n");
504 case MW_LOW_BATTERY_BT_OFF_MSG:
505 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
507 case MW_STATUS_CHANGE_EVENT:
508 mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
511 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
519 /* ----------------------------------------------------------------------
521 * ---------------------------------------------------------------------- */
523 int mw_init(mwdevice_t *mwdevice, int mw_fd)
525 memset(mwdevice, 0, sizeof(mwdevice_t));
526 mwdevice->mw_fd = mw_fd;
528 /* figure out which device we run with */
529 mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
534 /* ----------------------------------------------------------------------
535 * Convenience functions not strictly part of the protocol
536 * ---------------------------------------------------------------------- */
538 int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height)
540 if (width == NULL || height == NULL)
543 switch (mwdevice->devtype) {
544 case MW_DEVICE_TYPE_RESERVED:
546 case MW_DEVICE_TYPE_ANA_DIGI:
547 case MW_DEVICE_TYPE_DEVB_ANA_DIGI:
551 case MW_DEVICE_TYPE_DIGITAL:
552 case MW_DEVICE_TYPE_DEVB_DIGI:
564 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
565 if invert=1 each byte is inverted
567 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
576 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
577 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
580 *buf = invert ? ~tmp : tmp;
586 void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
591 unsigned int y, rowlength;
592 unsigned char mw_buf[24];
594 rowlength = (((width-1) / 8) + 1);
595 if ((height + offset) > 96)
596 height = 96 - offset;
599 fprintf(stderr, "row length = %d bytes\n", rowlength);
600 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
601 fprintf(stderr, "\n");
602 for (y=0; y<height; y++) {
603 for (x=0; x<rowlength; x++) {
605 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
607 fprintf(stderr, "\n");
609 fprintf(stderr, "\n");
612 for (y=0; y<height; y+=2) {
613 memset(mw_buf, 0, 24);
614 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
615 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
616 mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24);