Index: apps/lang/english.lang
===================================================================
--- apps/lang/english.lang (revision 14825)
+++ apps/lang/english.lang (working copy)
@@ -4173,6 +4173,23 @@
+ id: LANG_TIMESYNCPC
+ desc: enable or disable time sync with PC
+ user:
+
+ *: none
+ rtc: "Sync Clock to PC"
+
+
+ *: none
+ rtc: "Sync Clock to PC"
+
+
+ *: none
+ rtc: "Sync Clock to PC"
+
+
+
id: LANG_TIME_SET_BUTTON
desc: used in set_time()
user:
Index: apps/settings.h
===================================================================
--- apps/settings.h (revision 14825)
+++ apps/settings.h (working copy)
@@ -449,6 +449,7 @@
int volume_type; /* how volume is displayed: 0=graphic, 1=percent */
int battery_display; /* how battery is displayed: 0=graphic, 1=percent */
int timeformat; /* time format: 0=24 hour clock, 1=12 hour clock */
+ bool timesyncpc; /* Synchronise clock with PC*/
bool playlist_shuffle;
bool play_selected; /* Plays selected file even in shuffle mode */
int ff_rewind_min_step; /* FF/Rewind minimum step size */
Index: apps/menus/settings_menu.c
===================================================================
--- apps/menus/settings_menu.c (revision 14825)
+++ apps/menus/settings_menu.c (working copy)
@@ -246,7 +246,8 @@
MENUITEM_FUNCTION(time_set, 0, ID2P(LANG_SET_TIME),
timedate_set, NULL, NULL, Icon_NOICON);
MENUITEM_SETTING(timeformat, &global_settings.timeformat, NULL);
-MAKE_MENU(time_menu, ID2P(LANG_TIME_MENU), 0, Icon_NOICON, &time_set, &timeformat);
+MENUITEM_SETTING(timesyncpc, &global_settings.timesyncpc, NULL);
+MAKE_MENU(time_menu, ID2P(LANG_TIME_MENU), 0, Icon_NOICON, &time_set, &timeformat, ×yncpc);
#endif
/* System menu */
Index: apps/settings_list.c
===================================================================
--- apps/settings_list.c (revision 14825)
+++ apps/settings_list.c (working copy)
@@ -475,6 +475,7 @@
CHOICE_SETTING(0, timeformat, LANG_TIMEFORMAT, 0,
"time format", "24hour,12hour", NULL, 2,
ID2P(LANG_24_HOUR_CLOCK), ID2P(LANG_12_HOUR_CLOCK)),
+ OFFON_SETTING(0,timesyncpc, LANG_TIMESYNCPC, false,"Sync clock to PC",NULL),
#endif
#endif /* HAVE_LCD_BITMAP */
OFFON_SETTING(0,show_icons, LANG_SHOW_ICONS ,true,"show icons", NULL),
Index: apps/misc.c
===================================================================
--- apps/misc.c (revision 14825)
+++ apps/misc.c (working copy)
@@ -74,6 +74,114 @@
#endif
#endif
+#if CONFIG_RTC
+#define TIMESYNC_TIME_RB ".timerb"
+#define TIMESYNC_TIME_RB_ABS "/.rockbox/.timerb"
+#define TIMESYNC_TIME_PC ".timepc"
+#define TIMESYNC_TIME_PC_ABS "/.rockbox/.timepc"
+#include "logf.h"
+/*Functions to sync rockbox clock to the PCs when USB is connected*/
+int timesync_pre_usb(void)
+{
+ int time_rb_fd;
+ time_rb_fd = creat(TIMESYNC_TIME_RB_ABS);
+ if (time_rb_fd < 0)
+ {
+ return -1;
+ }
+ else
+ {
+ close(time_rb_fd);
+ }
+ return 0;
+}
+
+int timesync_post_usb(void)
+{
+ DIR* dir;
+ struct dirent* entry;
+ long time_rb=-1, time_pc=-1;
+ time_t now;
+ struct tm *tim;
+ int err;
+
+ logf("sync: postusb start");
+
+ /* find rockbox and PC time files in .rockbox, and get their mod times*/
+ dir = opendir("/.rockbox");
+ if(!dir)
+ {
+ logf("sync: Could not open RockBox directory");
+ return -1;
+ }
+ while((entry = readdir(dir)) != 0)
+ {
+ if(strcmp(entry->d_name, TIMESYNC_TIME_RB) == 0)
+ {
+ time_rb = (long)entry->wrtdate<<16 | (long)entry->wrttime;
+ }
+ if(strcmp(entry->d_name, TIMESYNC_TIME_PC) == 0)
+ {
+ time_pc = (long)entry->wrtdate<<16 | (long)entry->wrttime;
+ }
+ }
+ if(time_rb == -1)
+ {
+ logf("sync RockBox timesync file not found");
+ return -2;
+ }
+ if(time_pc == -1)
+ {
+ logf("sync PC timesync file not found");
+ return -3;
+ }
+
+ tim = get_time();
+ now = mktime(tim);
+
+ /*convert FAT times into epoch times*/
+ tim->tm_sec = (time_rb & 0x0000001F) << 1;
+ tim->tm_min = (time_rb & 0x000007E0) >> 5;
+ tim->tm_hour = (time_rb & 0x0000F800) >> 11;
+ tim->tm_mday = (time_rb & 0x001F0000) >> 16;
+ tim->tm_mon = ((time_rb & 0x01E00000) >> 21) - 1;
+ tim->tm_year = ((time_rb & 0xFE000000) >> 25) + 80;
+ tim->tm_wday = 0; /*not used, but initialised just in case*/
+ time_rb = mktime(tim);
+ tim->tm_sec = (time_pc & 0x0000001F) << 1;
+ tim->tm_min = (time_pc & 0x000007E0) >> 5;
+ tim->tm_hour = (time_pc & 0x0000F800) >> 11;
+ tim->tm_mday = (time_pc & 0x001F0000) >> 16;
+ tim->tm_mon = ((time_pc & 0x01E00000) >> 21) - 1;
+ tim->tm_year = ((time_pc & 0xFE000000) >> 25) + 80;
+ tim->tm_wday = 0;
+ time_pc = mktime(tim);
+
+ /*calculate offset and write time*/
+ now = now - (time_rb - time_pc);
+ tim = localtime(&now);
+ err = set_time(tim) ;
+ if (err < 0)
+ {
+ logf("Could not write time: %d",err);
+ err = -4;
+ }
+
+ /*clean up*/
+ if (remove(TIMESYNC_TIME_RB_ABS) < 0)
+ {
+ logf("Could not remove RockBox timesync file");
+ err = -5;
+ }
+ if (remove(TIMESYNC_TIME_PC_ABS) < 0)
+ {
+ logf("Could not remove computer timesync file");
+ err = -6;
+ }
+ return 0;
+}
+#endif /*RTC*/
+
/* Format a large-range value for output, using the appropriate unit so that
* the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
* units) if possible, and 3 significant digits are shown. If a buffer is
@@ -871,7 +979,21 @@
check_bootfile(false); /* gets initial size */
#endif
#endif
+#if CONFIG_RTC
+ if(global_settings.timesyncpc)
+ {
+ timesync_pre_usb();
+ }
+#endif
+
usb_screen();
+
+#if CONFIG_RTC
+ if(global_settings.timesyncpc)
+ {
+ timesync_post_usb();
+ }
+#endif
#ifdef BOOTFILE
#if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
check_bootfile(true);
@@ -1060,4 +1182,3 @@
return 0;
}
#endif /* HAVE_LCD_COLOR */
-