]> git.kernelconcepts.de Git - metawatch.git/blob - metawatch.c
0b33d05196961e936f987ca002384c357fdadd25
[metawatch.git] / metawatch.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber
3  *
4  * license GPL
5  */
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14 #include <termios.h>
15
16 #include "metawatch_protocol.h"
17 #include "crc16ccitt.h"
18
19
20 void dump_frame(unsigned char *frame, int len)
21 {
22         int i;
23
24         for (i=0; i<len; i++)
25                 fprintf(stderr, "0x%02x ", frame[i]);
26         fprintf(stderr, "\n");
27 }
28
29
30 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
31 {
32         unsigned short crc;
33         unsigned char frame[64];
34         int tlen = len + 6; /* payload + 6 bytes frameing */
35         int ret;
36
37         memset(frame, 0, 64);
38         frame[0] = MW_SOF;
39         frame[1] = len + 6;
40         frame[2] = msg_type;
41         frame[3] = options;
42         if (data != NULL && len > 0)
43                 memcpy(frame+4, data, len);
44
45         crc = crc16ccitt(frame, len+4);
46         *(unsigned short *)(frame+len+4) = crc;
47
48         dump_frame(frame, tlen);
49
50         while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
51                 tlen -= ret;
52
53         if (tlen == 0 && ret >= 0)
54                 return 0;
55         else
56                 return ret;
57 }
58
59 /*
60  * host to watch commands
61  */
62
63 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
64 {
65         time_t mtime;
66         struct tm mtm;
67         unsigned short year;
68         unsigned char data[32];
69
70         mtime = time(NULL);
71         localtime_r(&mtime, &mtm);
72
73         year = mtm.tm_year + 1900;  
74         data[0] = (year & 0x0f00) >> 8;
75         data[1] = (year & 0x00ff);
76         data[2] = mtm.tm_mon + 1;
77         data[3] = mtm.tm_mday;
78         data[4] = mtm.tm_wday;
79         data[5] = mtm.tm_hour;
80         data[6] = mtm.tm_min;
81         data[7] = mtm.tm_sec;
82         data[8] = clk1224;
83         data[9] = date_fmt;
84
85         mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
86 }
87
88 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
89 {
90         unsigned char mdata[7];
91
92         mdata[0] = enable;
93         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
94         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
95         mdata[5] = cycles;
96         mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
97 }
98
99 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
100 {
101         unsigned char mdata[3];
102
103         mdata[0] = timeout; /* seconds */
104         mdata[1] = invert; /* 0=normal, 1=invert */
105         mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 3), mdata, 2);
106 }
107
108 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
109 {
110         mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 3), NULL, 0);
111 }
112
113 /*
114  * watch responses, events or notifications
115  */
116 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
117 {
118         struct tm mtm;
119         unsigned short year;
120         unsigned char clk1224, date_fmt;
121
122         if (len != 10) {
123                 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
124                 return;
125         }
126
127         year = *(unsigned short *)rtcrsp;
128         mtm.tm_year = year - 1900;
129         mtm.tm_mon = rtcrsp[2] - 1;
130         mtm.tm_mday = rtcrsp[3];
131         mtm.tm_wday = rtcrsp[4];
132         mtm.tm_hour = rtcrsp[5];
133         mtm.tm_min = rtcrsp[6];
134         mtm.tm_sec = rtcrsp[7];
135         clk1224 = rtcrsp[8];
136         date_fmt = rtcrsp[9];
137
138         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");
139 }
140
141 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
142 {
143         unsigned short voltage = *(unsigned short *)batrsp;
144         unsigned char power_good = batrsp[2];
145         unsigned char bat_charging = batrsp[3];
146
147         fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
148 }
149
150
151 int decode_frame(int mw_fd, unsigned char *buf, int len)
152 {
153         unsigned short crc;
154         unsigned char msglen;
155         unsigned char msgtype;
156         unsigned char msgopt;
157         unsigned char *msgdata;
158
159         /* check frame */
160         crc = *(unsigned short *)(buf+len-2);
161         if (crc != crc16ccitt(buf, len-2)) {
162                 fprintf(stderr, "decode frame CRC error\n");
163                 return 1;
164         } else
165                 fprintf(stderr, "decode frame CRC OK\n");
166         if (buf[0] != MW_SOF) {
167                 fprintf(stderr, "decode frame SOF not found\n");
168                 return 1;
169         } else
170         fprintf(stderr, "decode frame found SOF\n");
171
172         msglen = buf[1];
173         msgtype = buf[2];
174         msgopt = buf[3];
175         msgdata = (buf+4);
176
177         switch (msgtype) {
178                 case MW_GET_REAL_TIME_CLOCK_RSP:
179                         mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
180                         break;
181                 case MW_GET_INFORMATION_STRING_RSP:
182                         msgdata[len-4-2] = 0;
183                         fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
184                         break;
185                 case MW_READ_BATTERY_VOLTAGE_RSP:
186                         mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
187                         break;
188                 case MW_LOW_BATTERY_WARNING_MSG:
189                         fprintf(stderr, "Watch battery low, please connect charger\n");
190                         break;
191                 case MW_LOW_BATTERY_BT_OFF_MSG:
192                         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
193                         break;
194                 default:
195                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
196                         break;
197         };
198
199         return 0;
200 }
201
202
203 void process_cmd(char *cmdline, int clinep, int mw_fd)
204 {
205         unsigned char mdata[32];
206
207         fprintf(stderr, "command: '%s'\n", cmdline);
208
209         if (strncmp(cmdline, "quit", 4) == 0) {
210                 close(mw_fd);
211                 exit(0);
212         }
213
214         if (strncmp(cmdline, "srtc", 4) == 0) {
215                 fprintf(stderr, "Setting RTC from system time...");
216                 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
217                 fprintf(stderr, "OK\n");
218         }
219         if (strncmp(cmdline, "grtc", 4) == 0) {
220                 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
221         }
222         if (strncmp(cmdline, "gistr", 5) == 0) {
223                 mdata[0] = 0;
224                 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
225         }
226         if (strncmp(cmdline, "gdtype", 6) == 0) {
227                 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
228         }
229         if (strncmp(cmdline, "rvbat", 5) == 0) {
230                 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
231         }
232         if (strncmp(cmdline, "modecfg", 6) == 0) {
233                 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
234                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
235         }
236         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
237                 mdata[0] = 0; /* idle screen */
238                 mdata[1] = 1; /* button index */
239                 mdata[2] = 2; /* button press type */
240                 mdata[3] = 3; /* callback message type */
241                 mdata[4] = 4; /* callback message option */
242                 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
243         }
244         if (strncmp(cmdline, "svib", 4) == 0) {
245                 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
246         }
247 }
248
249
250 int menu(int mw_fd)
251 {
252         fd_set mfds;
253         struct termios tconfd;
254         char cmdline[128];
255         unsigned char msg_buf[64];
256         unsigned char clinep = 0;
257         int rcvd;
258
259         tcgetattr(0, &tconfd);
260         cfmakeraw(&tconfd);
261         tconfd.c_oflag |= ONLCR | OPOST;
262         tconfd.c_lflag |= ISIG;
263         tcsetattr(0, TCSANOW, &tconfd);
264
265         FD_ZERO(&mfds);
266         FD_SET(0, &mfds);
267         FD_SET(mw_fd, &mfds);
268
269         memset(cmdline, 0, 128);
270
271         do {
272                 rcvd = 0;
273                 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
274                         if (FD_ISSET(mw_fd, &mfds)) {
275                                 rcvd = read(mw_fd, msg_buf, 64);
276                                 fprintf(stderr, "read %d bytes:\n", rcvd);
277                                 if (rcvd > 0) {
278                                         dump_frame(msg_buf, rcvd);
279                                         decode_frame(mw_fd, msg_buf, rcvd);
280                                 }
281                         };
282                         if (FD_ISSET(0, &mfds)) {
283                                 rcvd = read(0, (cmdline+clinep), 1);
284                                 if (rcvd > 0) {
285                                         if (cmdline[clinep] == '\r') {
286                                                 printf("\n");
287                                                 cmdline[clinep--] = '\0';
288                                                 process_cmd(cmdline, clinep, mw_fd);
289                                                 clinep = 0;
290                                                 memset(cmdline, 0, 128);
291                                         } else {
292                                                 clinep++;
293                                                 if (clinep > 75)
294                                                         clinep = 75;
295                                                 printf("\r> %s", cmdline);
296                                                 fflush(stdout);
297                                         }
298                                 }
299                         };
300                 } else
301                         break;
302                 FD_ZERO(&mfds);
303                 FD_SET(0, &mfds);
304                 FD_SET(mw_fd, &mfds);
305         } while (rcvd > 0);
306
307         return 0;
308 }
309
310 int main(int argc, char **argv)
311 {
312         int mw_fd;
313
314         if (argc != 2) {
315                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
316                 return 1;
317         };
318   
319         crc16ccitt_init();
320
321         mw_fd = open(argv[1], O_RDWR);
322         if (mw_fd < 0) {
323                 perror("open");
324                 return 1;
325         };
326
327         menu(mw_fd);
328
329         fsync(mw_fd);
330
331         close(mw_fd);
332
333         return 0;
334 };
335