-
Notifications
You must be signed in to change notification settings - Fork 0
/
setSystemTime.c
108 lines (101 loc) · 2.44 KB
/
setSystemTime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <mpsse.h>
#include <string.h>
#include <pcf2123.h>
#include <time.h>
#define READ_SIZE 16
/* Clock Polarity - Idle = Low 0
Clock Phase - Rising Edge 0
Chip Select -- Active = High
*/
int bcdtodec(unsigned long i);
int main(int ARGC, char **ARGV, char **ENVP)
{
FILE *fp = NULL;
char *data;
/* 1 2 */
/* 01 34 67 90 23 56 78 012 */
/* "00/00/00 00:00:00 AM DOW"*/
char time[] = "00/00/00 00:00:00 AM DOW\n";
char *wday = "SUNMONTUEWEDTHUFRISAT";
int retval = EXIT_FAILURE;
struct mpsse_context *clock = NULL;
time_t t;
struct tm tstruct;
struct timeval tv;
char *tz;
if(clock = OpenClock(0, 0, 0))
{
data=ReadClock(clock, 0, 16);
if (data)
{
if (data[2] & 0x80)
{
fprintf(stderr, "%s: Assertion Failed: Oscillator has been stopped. Clock not reliable.\n", ARGV[0]);
exit(3);
}
tstruct.tm_sec = bcdtodec(data[2] & 0x7f);
tstruct.tm_min = bcdtodec(data[3] & 0x7f);
tstruct.tm_hour = (data[0] & 0x04) ? (bcdtodec(data[4] & 0x3f)) : (12*(data[4] & 0x20) >> 5) + bcdtodec(data[4] & 0x1f);
tstruct.tm_mday = bcdtodec(data[5] & 0x3f);
tstruct.tm_wday = bcdtodec(data[6] & 0x07);
tstruct.tm_mon = bcdtodec(data[7] & 0x1f) - 1; /* Clock counts 1-12, tstruct expects 0-11 */
tstruct.tm_year = bcdtodec(data[8]) + (data[8] < 0x70 ? 100 : 0);
tstruct.tm_isdst=0;
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
t = mktime(&tstruct);
if(tz) setenv("TZ", tz, 1);
else unsetenv("TZ");
tzset();
if(gettimeofday(&tv, NULL))
{
fprintf(stderr, "%s: Error: Cound not get current system time: ", ARGV[0]);
perror(NULL);
exit(4);
}
else
{
tv.tv_sec = t;
tv.tv_usec = 0;
if(settimeofday(&tv, NULL))
{
fprintf(stderr, "%s: Error: Failed to set system time: ", ARGV[0]);
perror(NULL);
printf("Attempted to set: tv.tv_sec = %d tv.tv_usec = %d\n");
exit(5);
}
else
{
printf("%s: Successfully set system time to %s", ARGV[0], asctime(gmtime(&t)));
}
}
}
else
{
fprintf(stderr, "%s: Error: Could not read USB Clock: ", ARGV[0]);
perror(NULL);
exit(2);
}
}
else
{
fprintf(stderr, "%s: Error: Could not open USB Clock: ", ARGV[0]);
perror(NULL);
exit(1);
}
}
int bcdtodec(unsigned long i)
{
int j=1;
int k=0;
while(i)
{
k+=(i & 0xf) * j;
i >>= 4;
j *= 10;
}
return(k);
}