2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
18 #include "metawatch.h"
19 #include "metawatch_protocol.h"
20 #include "crc16ccitt.h"
24 const char *mw_screen_mode_names[] = {
27 "notification screen",
31 const char *mw_status_string[] = {
39 #define MW_FRAME_DELAY 0x00
41 void dump_frame(unsigned char *frame, int len)
46 fprintf(stderr, "0x%02x ", frame[i]);
47 fprintf(stderr, "\n");
51 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
54 unsigned char frame[64];
55 int tlen = len + 6; /* payload + 6 bytes frameing */
63 if (data != NULL && len > 0)
64 memcpy(frame+4, data, len);
66 crc = crc16ccitt(frame, len+4);
67 *(unsigned short *)(frame+len+4) = crc;
70 dump_frame(frame, tlen);
73 while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
77 usleep(MW_FRAME_DELAY);
79 if (tlen == 0 && ret >= 0)
86 /* ----------------------------------------------------------------------
87 * Host to watch commands
88 * ---------------------------------------------------------------------- */
91 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
96 unsigned char data[32];
99 localtime_r(&mtime, &mtm);
101 year = mtm.tm_year + 1900;
102 data[0] = (year & 0x0f00) >> 8;
103 data[1] = (year & 0x00ff);
104 data[2] = mtm.tm_mon + 1;
105 data[3] = mtm.tm_mday;
106 data[4] = mtm.tm_wday;
107 data[5] = mtm.tm_hour;
108 data[6] = mtm.tm_min;
109 data[7] = mtm.tm_sec;
113 mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
116 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
118 unsigned char mdata[7];
121 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
122 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
124 mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
127 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
129 unsigned char mdata[3];
131 mdata[0] = timeout; /* seconds */
132 mdata[1] = invert; /* 0=normal, 1=invert */
133 mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
136 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
138 mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
141 void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select)
143 mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
147 * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
149 void mw_write_buffer(int mw_fd,
151 unsigned char numlines, /* number of lines, 0=two lines or 1=one line */
152 unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */
153 unsigned char *buffer, int buflen)
155 unsigned char mdata[32];
157 buflen = 12 * (buflen / 12); /* crop to 12 bytes */
158 if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
159 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
162 memset(mdata, 0, 32);
163 mdata[0] = row_offset;
164 memcpy((mdata+1), buffer, 12);
166 mdata[13] = row_offset+1;
167 memcpy((mdata+14), (buffer+12), 12);
169 mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
173 /* ----------------------------------------------------------------------
174 * Watch responses, events or notifications
175 * ---------------------------------------------------------------------- */
178 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
182 unsigned char clk1224, date_fmt;
185 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
189 year = *(unsigned short *)rtcrsp;
190 mtm.tm_year = year - 1900;
191 mtm.tm_mon = rtcrsp[2] - 1;
192 mtm.tm_mday = rtcrsp[3];
193 mtm.tm_wday = rtcrsp[4];
194 mtm.tm_hour = rtcrsp[5];
195 mtm.tm_min = rtcrsp[6];
196 mtm.tm_sec = rtcrsp[7];
198 date_fmt = rtcrsp[9];
200 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");
203 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
205 unsigned short voltage = *(unsigned short *)batrsp;
206 unsigned char power_good = batrsp[2];
207 unsigned char bat_charging = batrsp[3];
209 fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
212 void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
214 fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
218 /* ----------------------------------------------------------------------
220 * ---------------------------------------------------------------------- */
223 int decode_frame(int mw_fd, unsigned char *buf, int len)
226 unsigned char msglen;
227 unsigned char msgtype;
228 unsigned char msgopt;
229 unsigned char *msgdata;
232 crc = *(unsigned short *)(buf+len-2);
233 if (crc != crc16ccitt(buf, len-2)) {
234 fprintf(stderr, "decode frame CRC error\n");
237 fprintf(stderr, "decode frame CRC OK\n");
238 if (buf[0] != MW_SOF) {
239 fprintf(stderr, "decode frame SOF not found\n");
242 fprintf(stderr, "decode frame found SOF\n");
250 case MW_GET_REAL_TIME_CLOCK_RSP:
251 mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
253 case MW_GET_INFORMATION_STRING_RSP:
254 msgdata[len-4-2] = 0;
255 fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
257 case MW_READ_BATTERY_VOLTAGE_RSP:
258 mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
260 case MW_LOW_BATTERY_WARNING_MSG:
261 fprintf(stderr, "Watch battery low, please connect charger\n");
263 case MW_LOW_BATTERY_BT_OFF_MSG:
264 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
266 case MW_STATUS_CHANGE_EVENT:
267 mw_status_change_event(mw_fd, msgopt, msgdata, len-4-2);
270 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
278 /* ----------------------------------------------------------------------
279 * Convenience functions not strictly part of the protocol
280 * ---------------------------------------------------------------------- */
282 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
283 if invert=1 each byte is inverted
285 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
294 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
295 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
298 *buf = invert ? ~tmp : tmp;
304 void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
307 unsigned int i, x, y;
308 unsigned int rowlength;
309 unsigned char mw_buf[24];
311 rowlength = ((width / 8) + 1);
312 if ((height + offset) > 96)
313 height = 96 - offset;
316 fprintf(stderr, "row length = %d bytes\n", rowlength);
317 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
318 fprintf(stderr, "\n");
319 for (y=0; y<height; y++) {
320 for (x=0; x<rowlength; x++) {
322 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
324 fprintf(stderr, "\n");
326 fprintf(stderr, "\n");
329 for (y=0; y<height; y+=2) {
330 memset(mw_buf, 0, 24);
331 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
332 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
333 mw_write_buffer(mw_fd, mode, 0, 31+y, mw_buf, 24);