2 * (c) 2011 Siegen, Germany by Nils Faerber
16 #include "metawatch_protocol.h"
17 #include "crc16ccitt.h"
20 void dump_frame(unsigned char *frame, int len)
25 fprintf(stderr, "0x%02x ", frame[i]);
26 fprintf(stderr, "\n");
30 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
33 unsigned char frame[64];
34 int tlen = len + 6; /* payload + 6 bytes frameing */
42 if (data != NULL && len > 0)
43 memcpy(frame+4, data, len);
45 crc = crc16ccitt(frame, len+4);
46 *(unsigned short *)(frame+len+4) = crc;
48 dump_frame(frame, tlen);
50 while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
53 if (tlen == 0 && ret >= 0)
60 * host to watch commands
63 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
68 unsigned char data[32];
71 localtime_r(&mtime, &mtm);
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;
85 mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
88 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
90 unsigned char mdata[7];
93 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
94 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
96 mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
99 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
101 unsigned char mdata[3];
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);
108 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
110 mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 3), NULL, 0);
114 * watch responses, events or notifications
116 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
120 unsigned char clk1224, date_fmt;
123 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
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];
136 date_fmt = rtcrsp[9];
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");
141 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
143 unsigned short voltage = *(unsigned short *)batrsp;
144 unsigned char power_good = batrsp[2];
145 unsigned char bat_charging = batrsp[3];
147 fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
151 int decode_frame(int mw_fd, unsigned char *buf, int len)
154 unsigned char msglen;
155 unsigned char msgtype;
156 unsigned char msgopt;
157 unsigned char *msgdata;
160 crc = *(unsigned short *)(buf+len-2);
161 if (crc != crc16ccitt(buf, len-2)) {
162 fprintf(stderr, "decode frame CRC error\n");
165 fprintf(stderr, "decode frame CRC OK\n");
166 if (buf[0] != MW_SOF) {
167 fprintf(stderr, "decode frame SOF not found\n");
170 fprintf(stderr, "decode frame found SOF\n");
178 case MW_GET_REAL_TIME_CLOCK_RSP:
179 mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
181 case MW_GET_INFORMATION_STRING_RSP:
182 msgdata[len-4-2] = 0;
183 fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
185 case MW_READ_BATTERY_VOLTAGE_RSP:
186 mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
188 case MW_LOW_BATTERY_WARNING_MSG:
189 fprintf(stderr, "Watch battery low, please connect charger\n");
191 case MW_LOW_BATTERY_BT_OFF_MSG:
192 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
195 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
203 void process_cmd(char *cmdline, int clinep, int mw_fd)
205 unsigned char mdata[32];
207 fprintf(stderr, "command: '%s'\n", cmdline);
209 if (strncmp(cmdline, "quit", 4) == 0) {
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");
219 if (strncmp(cmdline, "grtc", 4) == 0) {
220 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
222 if (strncmp(cmdline, "gistr", 5) == 0) {
224 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
226 if (strncmp(cmdline, "gdtype", 6) == 0) {
227 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
229 if (strncmp(cmdline, "rvbat", 5) == 0) {
230 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
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);
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);
244 if (strncmp(cmdline, "svib", 4) == 0) {
245 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
253 struct termios tconfd;
255 unsigned char msg_buf[64];
256 unsigned char clinep = 0;
259 tcgetattr(0, &tconfd);
261 tconfd.c_oflag |= ONLCR | OPOST;
262 tconfd.c_lflag |= ISIG;
263 tcsetattr(0, TCSANOW, &tconfd);
267 FD_SET(mw_fd, &mfds);
269 memset(cmdline, 0, 128);
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);
278 dump_frame(msg_buf, rcvd);
279 decode_frame(mw_fd, msg_buf, rcvd);
282 if (FD_ISSET(0, &mfds)) {
283 rcvd = read(0, (cmdline+clinep), 1);
285 if (cmdline[clinep] == '\r') {
287 cmdline[clinep--] = '\0';
288 process_cmd(cmdline, clinep, mw_fd);
290 memset(cmdline, 0, 128);
295 printf("\r> %s", cmdline);
304 FD_SET(mw_fd, &mfds);
310 int main(int argc, char **argv)
315 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
321 mw_fd = open(argv[1], O_RDWR);