Fix last known 'variable set but not used' warning reported from GCC 4.6.0.
[kugel-rb.git] / utils / time-sync / ipod-scsi.c
blob163cfbca8f493be02db00f92b37014e59eef9983
1 /* Copyright (c) 2007, Christophe Fergeau <teuf@gnome.org>
2 * Part of the libgpod project.
3 *
4 * URL: http://www.gtkpod.org/
5 * URL: http://gtkpod.sourceforge.net/
7 * The code contained in this file is free software; you can redistribute
8 * it and/or modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either version
10 * 2.1 of the License, or (at your option) any later version.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this code; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * iTunes and iPod are trademarks of Apple
23 * This product is not supported/written/published by Apple!
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <scsi/sg_cmds.h>
34 #include <endian.h>
35 #include <time.h>
37 static int do_sg_write_buffer (const char *device, void *buffer, size_t len)
39 int fd;
40 size_t i;
42 fd = open (device, O_RDWR);
43 if (fd < 0) {
44 printf ("Couldn't open device %s\n", device);
45 return -1;
48 printf (" Data Payload: ");
49 for (i = 0; i < len; i++) {
50 printf ("%02x ", *((uint8_t *)buffer+i));
52 printf ("\n");
54 if (sg_ll_write_buffer (fd, 1, 0, 0x0c0000, buffer, len, 1, 1) != 0) {
55 close(fd);
56 return -2;
58 close(fd);
60 return 0;
63 int sync_time (const char *device, struct tm *tm)
65 struct iPodTime {
66 uint16_t year;
67 uint16_t days;
68 uint8_t timezone;
69 uint8_t hour;
70 uint8_t minute;
71 uint8_t second;
72 uint8_t dst;
73 uint8_t padding[3];
74 } __attribute__((__packed__));
75 struct iPodTime ipod_time;
77 if (tm == NULL) {
78 time_t current_time;
79 current_time = time (NULL);
80 tm = localtime (&current_time);
83 ipod_time.year = htobe16 (1900+tm->tm_year);
84 ipod_time.days = htobe16 (tm->tm_yday);
85 /* the timezone value is the shift east of UTC in 15 min chunks
86 * so eg. UTC+2 is 2*4 = 8
88 ipod_time.timezone = tm->tm_gmtoff / 900;
89 ipod_time.hour = tm->tm_hour;
90 ipod_time.minute = tm->tm_min;
91 ipod_time.second = tm->tm_sec;
92 if (tm->tm_isdst) {
93 ipod_time.dst = 1;
94 } else {
95 ipod_time.dst = 0;
97 memset (ipod_time.padding, 0, sizeof (ipod_time.padding));
99 return do_sg_write_buffer (device, &ipod_time, sizeof (ipod_time));