2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
18 #include "metawatch_protocol.h"
19 #include "crc16ccitt.h"
22 #define MW_FRAME_DELAY 0x00
24 void dump_frame(unsigned char *frame, int len)
29 fprintf(stderr, "0x%02x ", frame[i]);
30 fprintf(stderr, "\n");
34 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
37 unsigned char frame[64];
38 int tlen = len + 6; /* payload + 6 bytes frameing */
46 if (data != NULL && len > 0)
47 memcpy(frame+4, data, len);
49 crc = crc16ccitt(frame, len+4);
50 *(unsigned short *)(frame+len+4) = crc;
53 dump_frame(frame, tlen);
56 while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
60 usleep(MW_FRAME_DELAY);
62 if (tlen == 0 && ret >= 0)
69 * host to watch commands
72 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
77 unsigned char data[32];
80 localtime_r(&mtime, &mtm);
82 year = mtm.tm_year + 1900;
83 data[0] = (year & 0x0f00) >> 8;
84 data[1] = (year & 0x00ff);
85 data[2] = mtm.tm_mon + 1;
86 data[3] = mtm.tm_mday;
87 data[4] = mtm.tm_wday;
88 data[5] = mtm.tm_hour;
94 mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
97 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
99 unsigned char mdata[7];
102 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
103 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
105 mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
108 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
110 unsigned char mdata[3];
112 mdata[0] = timeout; /* seconds */
113 mdata[1] = invert; /* 0=normal, 1=invert */
114 mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
117 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
119 mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
122 void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select)
124 mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
128 * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
130 void mw_write_buffer(int mw_fd,
132 unsigned char numlines, /* number of lines, 0=two lines or 1=one line */
133 unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */
134 unsigned char *buffer, int buflen)
136 unsigned char mdata[32];
138 buflen = 12 * (buflen / 12); /* crop to 12 bytes */
139 if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
140 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
143 memset(mdata, 0, 32);
144 mdata[0] = row_offset;
145 memcpy((mdata+1), buffer, 12);
147 mdata[13] = row_offset+1;
148 memcpy((mdata+14), (buffer+12), 12);
150 mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
154 /* ------------------------------------------------------------------------ */
158 * watch responses, events or notifications
160 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
164 unsigned char clk1224, date_fmt;
167 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
171 year = *(unsigned short *)rtcrsp;
172 mtm.tm_year = year - 1900;
173 mtm.tm_mon = rtcrsp[2] - 1;
174 mtm.tm_mday = rtcrsp[3];
175 mtm.tm_wday = rtcrsp[4];
176 mtm.tm_hour = rtcrsp[5];
177 mtm.tm_min = rtcrsp[6];
178 mtm.tm_sec = rtcrsp[7];
180 date_fmt = rtcrsp[9];
182 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");
185 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
187 unsigned short voltage = *(unsigned short *)batrsp;
188 unsigned char power_good = batrsp[2];
189 unsigned char bat_charging = batrsp[3];
191 fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
194 void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
196 fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
199 /* ---------------------------------------------------------------------- */
201 int decode_frame(int mw_fd, unsigned char *buf, int len)
204 unsigned char msglen;
205 unsigned char msgtype;
206 unsigned char msgopt;
207 unsigned char *msgdata;
210 crc = *(unsigned short *)(buf+len-2);
211 if (crc != crc16ccitt(buf, len-2)) {
212 fprintf(stderr, "decode frame CRC error\n");
215 fprintf(stderr, "decode frame CRC OK\n");
216 if (buf[0] != MW_SOF) {
217 fprintf(stderr, "decode frame SOF not found\n");
220 fprintf(stderr, "decode frame found SOF\n");
228 case MW_GET_REAL_TIME_CLOCK_RSP:
229 mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
231 case MW_GET_INFORMATION_STRING_RSP:
232 msgdata[len-4-2] = 0;
233 fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
235 case MW_READ_BATTERY_VOLTAGE_RSP:
236 mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
238 case MW_LOW_BATTERY_WARNING_MSG:
239 fprintf(stderr, "Watch battery low, please connect charger\n");
241 case MW_LOW_BATTERY_BT_OFF_MSG:
242 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
244 case MW_STATUS_CHANGE_EVENT:
245 mw_status_change_event(mw_fd, msgopt, msgdata, len-4-2);
248 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
256 void bitmap_test(int mw_fd)
258 /* a nice checker-board pattern */
259 unsigned char checkbuf[24] = {
260 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
261 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
262 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
263 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
265 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
266 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
267 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
268 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
270 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
274 void flip_buffer_bytes(unsigned char invert, unsigned char *buf, int len)
282 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
283 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
284 *buf = invert ? ~tmp : tmp;
289 void bitmap_read(int mw_fd)
293 unsigned int width, height, i, x, y;
294 unsigned int rowlength;
295 unsigned char *bmapbuf;
296 unsigned char mw_buf[24];
298 ffd = open("test.pbm", O_RDONLY);
303 ret = read(ffd, rbuf, 3);
304 if (rbuf[0] != 'P' || rbuf[1] != '4') {
305 fprintf(stderr, "not a PBM file\n");
308 memset(rbuf, 0, 256);
311 ret = read(ffd, (rbuf+i), 1);
312 } while (!isspace(rbuf[i++]));
315 memset(rbuf, 0, 256);
318 ret = read(ffd, (rbuf+i), 1);
319 } while (!isspace(rbuf[i++]));
322 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
324 rowlength = ((width / 8) + 1);
325 fprintf(stderr, "row length = %d bytes\n", rowlength);
327 bmapbuf = malloc(rowlength * height);
329 ret = read(ffd, bmapbuf, rowlength * height);
331 fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
334 fprintf(stderr, "\n");
335 for (y=0; y<height; y++) {
336 for (x=0; x<rowlength; x++) {
338 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
340 fprintf(stderr, "\n");
342 fprintf(stderr, "\n");
345 flip_buffer_bytes(1, bmapbuf, rowlength * height);
347 for (y=0; y<height; y+=2) {
348 memset(mw_buf, 0, 24);
349 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
350 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
351 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31+y, mw_buf, 24);
353 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
359 void process_cmd(char *cmdline, int clinep, int mw_fd)
361 unsigned char mdata[32];
363 fprintf(stderr, "command: '%s'\n", cmdline);
365 if (strncmp(cmdline, "quit", 4) == 0) {
370 if (strncmp(cmdline, "srtc", 4) == 0) {
371 fprintf(stderr, "Setting RTC from system time...");
372 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
373 fprintf(stderr, "OK\n");
375 if (strncmp(cmdline, "grtc", 4) == 0) {
376 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
378 if (strncmp(cmdline, "gistr", 5) == 0) {
380 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
382 if (strncmp(cmdline, "gdtype", 6) == 0) {
383 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
385 if (strncmp(cmdline, "rvbat", 5) == 0) {
386 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
388 if (strncmp(cmdline, "modecfg", 6) == 0) {
389 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
390 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
392 if (strncmp(cmdline, "rbtcfg", 6) == 0) {
393 mdata[0] = 0; /* idle screen */
394 mdata[1] = 1; /* button index */
395 mdata[2] = 2; /* button press type */
396 mdata[3] = 3; /* callback message type */
397 mdata[4] = 4; /* callback message option */
398 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
400 if (strncmp(cmdline, "svib", 4) == 0) {
401 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
403 if (strncmp(cmdline, "tbmp", 4) == 0) {
406 if (strncmp(cmdline, "rbmp", 4) == 0) {
415 struct termios tconfd;
417 unsigned char msg_buf[64];
418 unsigned char clinep = 0;
421 tcgetattr(0, &tconfd);
423 tconfd.c_oflag |= ONLCR | OPOST;
424 tconfd.c_lflag |= ISIG;
425 tcsetattr(0, TCSANOW, &tconfd);
429 FD_SET(mw_fd, &mfds);
431 memset(cmdline, 0, 128);
435 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
436 if (FD_ISSET(mw_fd, &mfds)) {
437 rcvd = read(mw_fd, msg_buf, 64);
438 fprintf(stderr, "read %d bytes:\n", rcvd);
440 dump_frame(msg_buf, rcvd);
441 decode_frame(mw_fd, msg_buf, rcvd);
444 if (FD_ISSET(0, &mfds)) {
445 rcvd = read(0, (cmdline+clinep), 1);
447 if (cmdline[clinep] == '\r') {
449 cmdline[clinep--] = '\0';
450 process_cmd(cmdline, clinep, mw_fd);
452 memset(cmdline, 0, 128);
457 printf("\r> %s", cmdline);
466 FD_SET(mw_fd, &mfds);
472 int main(int argc, char **argv)
477 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
483 mw_fd = open(argv[1], O_RDWR);