182 lines
3.7 KiB
C
182 lines
3.7 KiB
C
/*
|
|
* (c) 2011 Siegen, Germany by Nils Faerber
|
|
*
|
|
* license GPL
|
|
*/
|
|
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <termios.h>
|
|
|
|
#include "metawatch_protocol.h"
|
|
#include "crc16ccitt.h"
|
|
|
|
|
|
void dump_frame(unsigned char *frame, int len)
|
|
{
|
|
int i;
|
|
for (i=0; i<len; i++)
|
|
fprintf(stderr, "0x%02x ", frame[i]);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
|
|
|
|
int mw_send_packet(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
|
|
{
|
|
unsigned short crc;
|
|
unsigned char frame[64];
|
|
int tlen = len + 6; /* payload + 6 bytes frameing */
|
|
int ret;
|
|
|
|
memset(frame, 0, 64);
|
|
frame[0] = MW_SOF;
|
|
frame[1] = len + 6;
|
|
frame[2] = msg_type;
|
|
frame[3] = options;
|
|
memcpy(frame+4, data, len);
|
|
|
|
crc = crc16ccitt(frame, len+4);
|
|
*(unsigned short *)(frame+len+4) = crc;
|
|
|
|
dump_frame(frame, tlen);
|
|
|
|
while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
|
|
tlen -= ret;
|
|
|
|
if (tlen == 0 && ret >= 0)
|
|
return 0;
|
|
else
|
|
return ret;
|
|
}
|
|
|
|
void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
|
|
{
|
|
time_t mtime;
|
|
struct tm mtm;
|
|
unsigned short year;
|
|
unsigned char data[32];
|
|
|
|
mtime = time(NULL);
|
|
localtime_r(&mtime, &mtm);
|
|
|
|
year = mtm.tm_year + 1900;
|
|
data[0] = (year & 0x0f00) >> 8;
|
|
data[1] = (year & 0x00ff);
|
|
data[2] = mtm.tm_mon + 1;
|
|
data[3] = mtm.tm_mday;
|
|
data[4] = mtm.tm_wday;
|
|
data[5] = mtm.tm_hour;
|
|
data[6] = mtm.tm_min;
|
|
data[7] = mtm.tm_sec;
|
|
data[8] = clk1224;
|
|
data[9] = date_fmt;
|
|
|
|
mw_send_packet(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
|
|
}
|
|
|
|
|
|
void process_cmd(char *cmdline, int clinep, int mw_fd)
|
|
{
|
|
fprintf(stderr, "command: '%s'\n", cmdline);
|
|
|
|
if (strncmp(cmdline, "quit", 4) == 0) {
|
|
close(mw_fd);
|
|
exit(0);
|
|
}
|
|
|
|
if (strncmp(cmdline, "srtc", 4) == 0) {
|
|
fprintf(stderr, "Setting RTC from system time...");
|
|
mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
|
|
fprintf(stderr, "OK\n");
|
|
}
|
|
}
|
|
|
|
int menu(int mw_fd)
|
|
{
|
|
fd_set mfds;
|
|
struct termios tconfd;
|
|
char cmdline[128];
|
|
unsigned char msg_buf[64];
|
|
unsigned char clinep = 0;
|
|
int rcvd;
|
|
|
|
tcgetattr(0, &tconfd);
|
|
cfmakeraw(&tconfd);
|
|
tconfd.c_oflag |= ONLCR | OPOST;
|
|
tconfd.c_lflag |= ISIG;
|
|
tcsetattr(0, TCSANOW, &tconfd);
|
|
FD_ZERO(&mfds);
|
|
FD_SET(0, &mfds);
|
|
FD_SET(mw_fd, &mfds);
|
|
|
|
memset(cmdline, 0, 128);
|
|
|
|
do {
|
|
rcvd = 0;
|
|
if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
|
|
if (FD_ISSET(mw_fd, &mfds)) {
|
|
rcvd = read(mw_fd, msg_buf, 64);
|
|
printf("read %d bytes:\n", rcvd);
|
|
if (rcvd > 0) {
|
|
dump_frame(msg_buf, rcvd);
|
|
// decode_message(mw_fd, msg_buf, rcvd);
|
|
}
|
|
};
|
|
if (FD_ISSET(0, &mfds)) {
|
|
rcvd = read(0, (cmdline+clinep), 1);
|
|
if (rcvd > 0) {
|
|
if (cmdline[clinep] == '\r') {
|
|
printf("\n");
|
|
cmdline[clinep--] = '\0';
|
|
process_cmd(cmdline, clinep, mw_fd);
|
|
clinep = 0;
|
|
memset(cmdline, 0, 128);
|
|
} else {
|
|
clinep++;
|
|
if (clinep > 75)
|
|
clinep = 75;
|
|
printf("\r> %s", cmdline);
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
};
|
|
} else
|
|
break;
|
|
FD_ZERO(&mfds);
|
|
FD_SET(0, &mfds);
|
|
FD_SET(mw_fd, &mfds);
|
|
} while (rcvd > 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int mw_fd;
|
|
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
|
|
return 1;
|
|
};
|
|
|
|
crc16ccitt_init();
|
|
|
|
mw_fd = open(argv[1], O_RDWR);
|
|
if (mw_fd < 0) {
|
|
perror("open");
|
|
return 1;
|
|
};
|
|
|
|
menu(mw_fd);
|
|
|
|
fsync(mw_fd);
|
|
|
|
close(mw_fd);
|
|
|
|
return 0;
|
|
};
|