From: Nils Faerber Date: Tue, 19 Jul 2011 12:13:11 +0000 (+0200) Subject: Add menu X-Git-Url: https://git.kernelconcepts.de/?p=metawatch.git;a=commitdiff_plain;h=5c2abd6bea71ec5204486e2a097d60e520f19aac Add menu --- diff --git a/Makefile b/Makefile index 4024ec9..9af0b23 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Copyright (C) 2011 Nils Faerber # prefix for installation and search path (like icons) PREFIX = /usr/local/ -CFLAGS = $(CCFLAGS) +CFLAGS = -Wall -O2 $(CCFLAGS) PRGNAME = metawatch diff --git a/metawatch.c b/metawatch.c index c6914ce..b95675e 100644 --- a/metawatch.c +++ b/metawatch.c @@ -7,8 +7,11 @@ #include #include #include +#include +#include #include #include +#include #include "metawatch_protocol.h" #include "crc16ccitt.h" @@ -23,10 +26,12 @@ void dump_frame(unsigned char *frame, int len) } -void mw_send_packet(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len) +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; @@ -38,9 +43,15 @@ void mw_send_packet(int mw_fd, unsigned char msg_type, unsigned char options, un crc = crc16ccitt(frame, len+4); *(unsigned short *)(frame+len+4) = crc; - dump_frame(frame, len+6); + dump_frame(frame, tlen); - write(mw_fd, frame, len+6); + 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) @@ -69,6 +80,81 @@ void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt) } +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; @@ -86,7 +172,7 @@ int main(int argc, char **argv) return 1; }; - mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM); + menu(mw_fd); fsync(mw_fd);