aboutsummaryrefslogtreecommitdiffstats
path: root/settime.c
blob: 73615994828c9ecd6cb9e295870759b1f5df2a72 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* 
 * Copyright 1996-2002 - Karl R. Hakimian and David Fries
 *
 * This file is part of datalink.
 *
 * Datalink is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Datalink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with datalink (see COPYING); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/* settime: Example of Linux SDK for Timex watch.              */
/* Written by Karl R. Hakimian 10/3/96                         */
/*                                                             */
/* Now warranty expressed or implied, use it as you see fit.   */
 /*
    *
    * Modified by David Fries <dfries@mail.win.org> 7/11/99
    * - Added support for the Timex Datalink Ironman Triathlon
  */

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "datalink.h"

extern char *tzname[];

int set_time(WatchInfoPtr wi)
{
	time_t now;
	struct tm *now_s;
	ListPtr times;
	ItemPtr tp;
	int isdst;

	now = time(NULL) + 9;	/* Offset for sending to watch. */
	now_s = localtime(&now);
	isdst = (now_s->tm_isdst > 0) ? 1 : 0;

	times = dl_new_list();
	times->download = 1;
	tp = dl_new_item(wi, DL_TIME_TYPE);

	tp->data.time.hours = now_s->tm_hour;
	tp->data.time.minutes = now_s->tm_min;
	tp->data.time.seconds = now_s->tm_sec;
	tp->data.time.month = now_s->tm_mon + 1;
	tp->data.time.day = now_s->tm_mday;
	tp->data.time.year = now_s->tm_year;
/* TODO: investigate, see change log */
	tp->data.time.dow = (now_s->tm_wday + 6) % 7;
	tp->data.time.hour_fmt = 1;
	tp->data.time.download = 1;
	tp->data.time.label = tzname[isdst];
	dl_add_to_list(times, tp);

	if (isdst)
		now -= 60 * 60;
	else
		now += 60 * 60;

	now_s = localtime(&now);

	tp = dl_new_item(wi, DL_TIME_TYPE);

	tp->data.time.hours = now_s->tm_hour;
	tp->data.time.minutes = now_s->tm_min;
	tp->data.time.seconds = now_s->tm_sec;
	tp->data.time.month = now_s->tm_mon + 1;
	tp->data.time.day = now_s->tm_mday;
	tp->data.time.year = now_s->tm_year;
/* TODO: investigate, see change log */
	tp->data.time.dow = (now_s->tm_wday + 6) % 7;
	tp->data.time.hour_fmt = 1;
	tp->data.time.download = 1;
	tp->data.time.label = tzname[1 - isdst];
	dl_add_to_list(times, tp);

	dl_init_download(wi, times, NULL, NULL, NULL, NULL, NULL, NULL,
			 NULL, NULL, NULL, NULL);
	return (0);
}

int my_error_proc(char *msg)
{
	fprintf(stderr, "ERROR: %s\n", msg);
	exit(-1);
}

int my_warn_proc(char *msg)
{
	fprintf(stderr, "WARNING: %s\n", msg);
	return (0);
}

int main( int argc, char ** argv)
{
	WatchInfoPtr wi;

/*
 Use my error proc to exit on error and my warn proc to ignore warnings
*/
	dl_set_error(my_error_proc);
	dl_set_warn(my_error_proc);

	if(argc==2)
	{
		if(!strcmp("-model70",argv[1])||!strcmp("-70",argv[1]))
			wi = dl_init_watch(DATALINK_70);
		else
		if(!strcmp("-model150",argv[1])||!strcmp("-150",argv[1]))
			wi = dl_init_watch(DATALINK_150);
		else
		if(!strcmp("-model150s",argv[1])||!strcmp("-150s",argv[1]))
			wi = dl_init_watch(DATALINK_150S);
		else
		if(!strcmp("-ironman",argv[1]))
			wi = dl_init_watch(DATALINK_IRONMAN);
		else
		{
			printf("Usage: %s [ -model70 | -70 | -model70 | "
				"-150 | -model150 | -model150s | -150s |"
				" -ironman]\nVersion "
				"$Id: settime.c,v 1.13 2002/07/10 04:23:23 david Exp $\n", argv[0]);
			printf("The default is -model150 "
				"if none is specified\n");
			exit(1);
		}
	}
	else
	{
		wi = dl_init_watch(DATALINK_IRONMAN);
	}
/*
 Set the time.
*/
	if (set_time(wi))
		exit(-1);
/*
 Send it to the watch
*/
	dl_send_data(wi, SVGA_BLINK);
	return 0;
}