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)
24 fprintf(stderr, "0x%02x ", frame[i]);
25 fprintf(stderr, "\n");
29 int mw_send_packet(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
32 unsigned char frame[64];
33 int tlen = len + 6; /* payload + 6 bytes frameing */
41 memcpy(frame+4, data, len);
43 crc = crc16ccitt(frame, len+4);
44 *(unsigned short *)(frame+len+4) = crc;
46 dump_frame(frame, tlen);
48 while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
51 if (tlen == 0 && ret >= 0)
57 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
62 unsigned char data[32];
65 localtime_r(&mtime, &mtm);
67 year = mtm.tm_year + 1900;
68 data[0] = (year & 0x0f00) >> 8;
69 data[1] = (year & 0x00ff);
70 data[2] = mtm.tm_mon + 1;
71 data[3] = mtm.tm_mday;
72 data[4] = mtm.tm_wday;
73 data[5] = mtm.tm_hour;
79 mw_send_packet(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
83 void process_cmd(char *cmdline, int clinep, int mw_fd)
85 fprintf(stderr, "command: '%s'\n", cmdline);
87 if (strncmp(cmdline, "quit", 4) == 0) {
92 if (strncmp(cmdline, "srtc", 4) == 0) {
93 fprintf(stderr, "Setting RTC from system time...");
94 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
95 fprintf(stderr, "OK\n");
102 struct termios tconfd;
104 unsigned char msg_buf[64];
105 unsigned char clinep = 0;
108 tcgetattr(0, &tconfd);
110 tconfd.c_oflag |= ONLCR | OPOST;
111 tconfd.c_lflag |= ISIG;
112 tcsetattr(0, TCSANOW, &tconfd);
115 FD_SET(mw_fd, &mfds);
117 memset(cmdline, 0, 128);
121 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
122 if (FD_ISSET(mw_fd, &mfds)) {
123 rcvd = read(mw_fd, msg_buf, 64);
124 printf("read %d bytes:\n", rcvd);
126 dump_frame(msg_buf, rcvd);
127 // decode_message(mw_fd, msg_buf, rcvd);
130 if (FD_ISSET(0, &mfds)) {
131 rcvd = read(0, (cmdline+clinep), 1);
133 if (cmdline[clinep] == '\r') {
135 cmdline[clinep--] = '\0';
136 process_cmd(cmdline, clinep, mw_fd);
138 memset(cmdline, 0, 128);
143 printf("\r> %s", cmdline);
152 FD_SET(mw_fd, &mfds);
158 int main(int argc, char **argv)
163 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
169 mw_fd = open(argv[1], O_RDWR);