]> git.kernelconcepts.de Git - metawatch.git/blob - metawatch.c
a87ac7bd2665c2f9a95ab2da1ebeedffec412f9d
[metawatch.git] / metawatch.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <termios.h>
29 #include <ctype.h>
30
31 #include "metawatch.h"
32 #include "metawatch_protocol.h"
33 #include "crc16ccitt.h"
34
35
36 #ifdef DEBUG
37 const char *mw_screen_mode_names[] = {
38         "idle screen",
39         "application screen",
40         "notification screen",
41         "scroll"
42 };
43
44 const char *mw_status_string[] = {
45         "Reserved",
46         "Mode Change",
47         "Display Timeout"
48 };
49 #endif
50
51
52 #define MW_FRAME_DELAY          0x00
53
54 void dump_frame(unsigned char *frame, int len)
55 {
56         int i;
57
58         for (i=0; i<len; i++)
59                 fprintf(stderr, "0x%02x ", frame[i]);
60         fprintf(stderr, "\n");
61 }
62
63
64 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
65 {
66         unsigned short crc;
67         unsigned char frame[64];
68         int tlen = len + 6; /* payload + 6 bytes frameing */
69         int ret;
70
71         memset(frame, 0, 64);
72         frame[0] = MW_SOF;
73         frame[1] = len + 6;
74         frame[2] = msg_type;
75         frame[3] = options;
76         if (data != NULL && len > 0)
77                 memcpy(frame+4, data, len);
78
79         crc = crc16ccitt(frame, len+4);
80         *(unsigned short *)(frame+len+4) = crc;
81
82 #ifdef DEBUG
83         dump_frame(frame, tlen);
84 #endif
85
86         while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
87                 tlen -= ret;
88
89         if (MW_FRAME_DELAY)
90                 usleep(MW_FRAME_DELAY);
91
92         if (tlen == 0 && ret >= 0)
93                 return 0;
94         else
95                 return ret;
96 }
97
98
99 /* ----------------------------------------------------------------------
100  * Host to watch commands
101  * ---------------------------------------------------------------------- */
102
103
104 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
105 {
106         time_t mtime;
107         struct tm mtm;
108         unsigned short year;
109         unsigned char data[32];
110
111         mtime = time(NULL);
112         localtime_r(&mtime, &mtm);
113
114         year = mtm.tm_year + 1900;  
115         data[0] = (year & 0x0f00) >> 8;
116         data[1] = (year & 0x00ff);
117         data[2] = mtm.tm_mon + 1;
118         data[3] = mtm.tm_mday;
119         data[4] = mtm.tm_wday;
120         data[5] = mtm.tm_hour;
121         data[6] = mtm.tm_min;
122         data[7] = mtm.tm_sec;
123         data[8] = clk1224;
124         data[9] = date_fmt;
125
126         mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
127 }
128
129 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
130 {
131         unsigned char mdata[7];
132
133         mdata[0] = enable;
134         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
135         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
136         mdata[5] = cycles;
137         mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
138 }
139
140 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
141 {
142         unsigned char mdata[3];
143
144         mdata[0] = timeout; /* seconds */
145         mdata[1] = invert; /* 0=normal, 1=invert */
146         mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
147 }
148
149 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
150 {
151         mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
152 }
153
154 void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select)
155 {
156         mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
157 }
158
159 /*
160  * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
161  */
162 void mw_write_buffer(int mw_fd,
163                 unsigned char mode,
164                 unsigned char numlines,         /* number of lines, 0=two lines or 1=one line */
165                 unsigned char row_offset,       /* start at row_offset in display, e.g. lower part in idle @31 */
166                 unsigned char *buffer, int buflen)
167 {
168         unsigned char mdata[32];
169
170         buflen = 12 * (buflen / 12);    /* crop to 12 bytes */
171         if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
172                 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
173                 return;
174         };
175         memset(mdata, 0, 32);
176         mdata[0] = row_offset;
177         memcpy((mdata+1), buffer, 12);
178         if (numlines == 0) {
179                 mdata[13] = row_offset+1;
180                 memcpy((mdata+14), (buffer+12), 12);
181         };
182         mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
183 }
184
185
186 /* ----------------------------------------------------------------------
187  * Watch responses, events or notifications
188  * ---------------------------------------------------------------------- */
189
190
191 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
192 {
193         struct tm mtm;
194         unsigned short year;
195         unsigned char clk1224, date_fmt;
196
197         if (len != 10) {
198                 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
199                 return;
200         }
201
202         year = *(unsigned short *)rtcrsp;
203         mtm.tm_year = year - 1900;
204         mtm.tm_mon = rtcrsp[2] - 1;
205         mtm.tm_mday = rtcrsp[3];
206         mtm.tm_wday = rtcrsp[4];
207         mtm.tm_hour = rtcrsp[5];
208         mtm.tm_min = rtcrsp[6];
209         mtm.tm_sec = rtcrsp[7];
210         clk1224 = rtcrsp[8];
211         date_fmt = rtcrsp[9];
212
213         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");
214 }
215
216 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
217 {
218         unsigned short voltage = *(unsigned short *)batrsp;
219         unsigned char power_good = batrsp[2];
220         unsigned char bat_charging = batrsp[3];
221
222         fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
223 }
224
225 void mw_read_light_sensor_response(int mw_fd, unsigned char *lightrsp, int len)
226 {
227         unsigned char power_good = lightrsp[0];
228         unsigned char bat_charging = lightrsp[1];
229         unsigned short voltage = *(unsigned short *)(lightrsp+2);
230
231         fprintf(stderr, "light sensor is at %d, power stat: %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
232 }
233
234 void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
235 {
236 #ifdef DEBUG
237         fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);      
238 #endif
239 }
240
241
242 /* ----------------------------------------------------------------------
243  * Protocol handling
244  * ---------------------------------------------------------------------- */
245
246
247 int decode_frame(int mw_fd, unsigned char *buf, int len)
248 {
249         unsigned short crc;
250         unsigned char msglen;
251         unsigned char msgtype;
252         unsigned char msgopt;
253         unsigned char *msgdata;
254
255         /* check frame */
256         crc = *(unsigned short *)(buf+len-2);
257         if (crc != crc16ccitt(buf, len-2)) {
258                 fprintf(stderr, "decode frame CRC error\n");
259                 return 1;
260         }
261 #ifdef DEBUG
262          else
263                 fprintf(stderr, "decode frame CRC OK\n");
264 #endif
265         if (buf[0] != MW_SOF) {
266                 fprintf(stderr, "decode frame SOF not found\n");
267                 return 1;
268         }
269 #ifdef DEBUG
270          else
271         fprintf(stderr, "decode frame found SOF\n");
272 #endif
273
274         msglen = buf[1];
275         msgtype = buf[2];
276         msgopt = buf[3];
277         msgdata = (buf+4);
278
279         switch (msgtype) {
280                 case MW_GET_DEVICE_TYPE_RSP:
281                         fprintf(stderr, "Got device type ");
282                         switch(msgdata[0]) {
283                                 case 0:
284                                         fprintf(stderr, "Reserved\n");
285                                         break;
286                                 case 1:
287                                         fprintf(stderr, "Ana-Digi\n");
288                                         break;
289                                 case 2:
290                                         fprintf(stderr, "Digital\n");
291                                         break;
292                                 case 3:
293                                         fprintf(stderr, "Development Board Digital\n");
294                                         break;
295                                 case 4:
296                                         fprintf(stderr, "Development Board Ana-Digi\n");
297                                         break;
298                                 default:
299                                         fprintf(stderr, "unknown %d\n", msgdata[0]);
300                                         break;
301                         };
302                         break;
303                 case MW_GET_INFORMATION_STRING_RSP:
304                         msgdata[len-2] = 0;
305                         fprintf(stderr, "Got info string '%s'\n", msgdata);
306                         break;
307                 case MW_GET_REAL_TIME_CLOCK_RSP:
308                         mw_get_real_time_clock_response(mw_fd, msgdata, len-2);
309                         break;
310                 case MW_READ_BATTERY_VOLTAGE_RSP:
311                         mw_get_battery_voltage_response(mw_fd, msgdata, len-2);
312                         break;
313                 case MW_READ_LIGHT_SENSOR_RSP:
314                         mw_read_light_sensor_response(mw_fd, msgdata, len-2);
315                         break;
316                 case MW_LOW_BATTERY_WARNING_MSG:
317                         fprintf(stderr, "Watch battery low, please connect charger\n");
318                         break;
319                 case MW_LOW_BATTERY_BT_OFF_MSG:
320                         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
321                         break;
322                 case MW_STATUS_CHANGE_EVENT:
323                         mw_status_change_event(mw_fd, msgopt, msgdata, len-2);
324                         break;
325                 default:
326                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
327                         break;
328         };
329
330         return 0;
331 }
332
333
334 /* ----------------------------------------------------------------------
335  * Convenience functions not strictly part of the protocol
336  * ---------------------------------------------------------------------- */
337
338 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
339    if invert=1 each byte is inverted
340  */
341 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
342 {
343         int i;
344         unsigned char tmp;
345
346         while (len--) {
347                 tmp = 0;
348                 if (flip) {
349                         for (i=0; i<8; i++)
350                                 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
351                         // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
352                 } else
353                         tmp = *buf;
354                 *buf = invert ? ~tmp : tmp;
355                 buf++;
356         }
357 }
358
359
360 void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
361 {
362 #ifdef DEBUG
363         unsigned int i, x;
364 #endif
365         unsigned int y, rowlength;
366         unsigned char mw_buf[24];
367
368         rowlength = (((width-1) / 8) + 1);
369         if ((height + offset) > 96)
370                 height = 96 - offset;
371
372 #ifdef DEBUG
373         fprintf(stderr, "row length = %d bytes\n", rowlength);
374         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
375         fprintf(stderr, "\n");
376         for (y=0; y<height; y++) {
377                 for (x=0; x<rowlength; x++) {
378                         for (i=0; i<8; i++)
379                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
380                 }
381                 fprintf(stderr, "\n");
382         }
383         fprintf(stderr, "\n");
384 #endif
385
386         for (y=0; y<height; y+=2) {
387                 memset(mw_buf, 0, 24);
388                 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
389                 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
390                 mw_write_buffer(mw_fd, mode, 0, 31+y, mw_buf, 24);
391         }
392 }
393