Add menu
This commit is contained in:
parent
0d6cba4530
commit
5c2abd6bea
2 changed files with 91 additions and 5 deletions
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
# Copyright (C) 2011 Nils Faerber <nils.faerber@kernelconcepts.de>
|
# Copyright (C) 2011 Nils Faerber <nils.faerber@kernelconcepts.de>
|
||||||
# prefix for installation and search path (like icons)
|
# prefix for installation and search path (like icons)
|
||||||
PREFIX = /usr/local/
|
PREFIX = /usr/local/
|
||||||
CFLAGS = $(CCFLAGS)
|
CFLAGS = -Wall -O2 $(CCFLAGS)
|
||||||
|
|
||||||
PRGNAME = metawatch
|
PRGNAME = metawatch
|
||||||
|
|
||||||
|
|
94
metawatch.c
94
metawatch.c
|
@ -7,8 +7,11 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
#include "metawatch_protocol.h"
|
#include "metawatch_protocol.h"
|
||||||
#include "crc16ccitt.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 short crc;
|
||||||
unsigned char frame[64];
|
unsigned char frame[64];
|
||||||
|
int tlen = len + 6; /* payload + 6 bytes frameing */
|
||||||
|
int ret;
|
||||||
|
|
||||||
memset(frame, 0, 64);
|
memset(frame, 0, 64);
|
||||||
frame[0] = MW_SOF;
|
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);
|
crc = crc16ccitt(frame, len+4);
|
||||||
*(unsigned short *)(frame+len+4) = crc;
|
*(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)
|
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 main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int mw_fd;
|
int mw_fd;
|
||||||
|
@ -86,7 +172,7 @@ int main(int argc, char **argv)
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
|
menu(mw_fd);
|
||||||
|
|
||||||
fsync(mw_fd);
|
fsync(mw_fd);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue