96 lines
1.8 KiB
C
96 lines
1.8 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 <string.h>
|
|
#include <time.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");
|
|
}
|
|
|
|
|
|
void 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];
|
|
|
|
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, len+6);
|
|
|
|
write(mw_fd, frame, len+6);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
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;
|
|
};
|
|
|
|
mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
|
|
|
|
fsync(mw_fd);
|
|
|
|
close(mw_fd);
|
|
|
|
return 0;
|
|
};
|