diff --git a/ThunderboltTimeSync.sln b/ThunderboltTimeSync.sln new file mode 100644 index 0000000..c1ad819 --- /dev/null +++ b/ThunderboltTimeSync.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThunderboltTimeSync", "ThunderboltTimeSync\ThunderboltTimeSync.csproj", "{7D970120-F57C-4541-BD54-2602C59EB8BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7D970120-F57C-4541-BD54-2602C59EB8BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D970120-F57C-4541-BD54-2602C59EB8BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D970120-F57C-4541-BD54-2602C59EB8BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D970120-F57C-4541-BD54-2602C59EB8BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/ThunderboltTimeSync/App.config b/ThunderboltTimeSync/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/ThunderboltTimeSync/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ThunderboltTimeSync/FormMain.Designer.cs b/ThunderboltTimeSync/FormMain.Designer.cs new file mode 100644 index 0000000..a9483ef --- /dev/null +++ b/ThunderboltTimeSync/FormMain.Designer.cs @@ -0,0 +1,42 @@ +namespace ThunderboltTimeSync { + partial class FormMain { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.SuspendLayout(); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(312, 316); + this.Name = "FormMain"; + this.Text = "Thunderbolt Time Sync"; + this.ResumeLayout(false); + + } + + #endregion + } +} + diff --git a/ThunderboltTimeSync/FormMain.cs b/ThunderboltTimeSync/FormMain.cs new file mode 100644 index 0000000..36c71d3 --- /dev/null +++ b/ThunderboltTimeSync/FormMain.cs @@ -0,0 +1,30 @@ +using System; +using System.Diagnostics; +using System.Security.Principal; +using System.Windows.Forms; + +namespace ThunderboltTimeSync { + public partial class FormMain : Form { + public FormMain() { + // Check for admin rights + // If running as admin: + // Ask for COM port with dialog + // Connect to COM port + // When time message received: + // If (time in UTC) AND (last time change was more than $MIN_UPDATE_INTERVAL ago) AND (error is less than $ERROR_THRESHOLD) + // Change system time to GPS time + // Else: + // Display message to tell user to run as admin + // Quit + + InitializeComponent(); + + Debug.WriteLine(SystemTimeUtils.GetTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff")); + SystemTimeUtils.SetTime(new DateTime( + 2000, 1, 1, + 10, 0, 0, 0 + )); + Debug.WriteLine(SystemTimeUtils.GetTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff")); + } + } +} diff --git a/ThunderboltTimeSync/FormMain.resx b/ThunderboltTimeSync/FormMain.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ThunderboltTimeSync/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ThunderboltTimeSync/Program.cs b/ThunderboltTimeSync/Program.cs new file mode 100644 index 0000000..cb7f106 --- /dev/null +++ b/ThunderboltTimeSync/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Security.Principal; +using System.Windows.Forms; + +namespace ThunderboltTimeSync { + static class Program { + /// + /// Checks if the application is currently running with administrator privileges. + /// + /// True if the application is running with administrator privileges, false otherwise. + private static bool IsRunningAsAdministrator() { + WindowsIdentity identity = WindowsIdentity.GetCurrent(); + WindowsPrincipal principal = new WindowsPrincipal(identity); + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } + + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + + if (!IsRunningAsAdministrator()) { + MessageBox.Show( + "This application must be run with administrative privileges. Without administrative privileges, the system time cannot be set.\n\nPlease restart the application as administrator.", + "Administrative Privileges Required", + MessageBoxButtons.OK, + MessageBoxIcon.Error + ); + } else { + Application.Run(new FormMain()); + } + } + } +} diff --git a/ThunderboltTimeSync/Properties/AssemblyInfo.cs b/ThunderboltTimeSync/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a9514bd --- /dev/null +++ b/ThunderboltTimeSync/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ThunderboltTimeSync")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ThunderboltTimeSync")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("7d970120-f57c-4541-bd54-2602c59eb8ba")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ThunderboltTimeSync/Properties/Resources.Designer.cs b/ThunderboltTimeSync/Properties/Resources.Designer.cs new file mode 100644 index 0000000..f9a0cd0 --- /dev/null +++ b/ThunderboltTimeSync/Properties/Resources.Designer.cs @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ThunderboltTimeSync.Properties { + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if ((resourceMan == null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ThunderboltTimeSync.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/ThunderboltTimeSync/Properties/Resources.resx b/ThunderboltTimeSync/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ThunderboltTimeSync/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ThunderboltTimeSync/Properties/Settings.Designer.cs b/ThunderboltTimeSync/Properties/Settings.Designer.cs new file mode 100644 index 0000000..60d2121 --- /dev/null +++ b/ThunderboltTimeSync/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ThunderboltTimeSync.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings) (global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/ThunderboltTimeSync/Properties/Settings.settings b/ThunderboltTimeSync/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/ThunderboltTimeSync/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ThunderboltTimeSync/SystemTimeUtils.cs b/ThunderboltTimeSync/SystemTimeUtils.cs new file mode 100644 index 0000000..2578850 --- /dev/null +++ b/ThunderboltTimeSync/SystemTimeUtils.cs @@ -0,0 +1,67 @@ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace ThunderboltTimeSync { + class SystemTimeUtils { + [StructLayout(LayoutKind.Sequential)] + private struct SYSTEMTIME { + public short wYear; + public short wMonth; + public short wDayOfWeek; + public short wDay; + public short wHour; + public short wMinute; + public short wSecond; + public short wMilliseconds; + }; + + [DllImport("kernel32.dll")] + private static extern uint GetLastError(); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool SetSystemTime(ref SYSTEMTIME lpSystemTime); + + [DllImport("kernel32.dll")] + private static extern void GetSystemTime(ref SYSTEMTIME lpSystemTime); + + /// + /// Sets the system time. + /// + /// The date and time to set the system clock to. + public static void SetTime(DateTime dateTime) { + SYSTEMTIME systemTime = new SYSTEMTIME(); + + systemTime.wYear = (short) dateTime.Year; + systemTime.wMonth = (short) dateTime.Month; + // wDayOfWeek is ignored by SetSystemTime + systemTime.wDay = (short) dateTime.Day; + systemTime.wHour = (short) dateTime.Hour; + systemTime.wMinute = (short) dateTime.Minute; + systemTime.wSecond = (short) dateTime.Second; + systemTime.wMilliseconds = (short) dateTime.Millisecond; + + bool setSucceeded = SetSystemTime(ref systemTime); + + if (!setSucceeded) { + Debug.WriteLine(string.Format("Call failed: error = {0}", GetLastError())); + } + } + + /// + /// Retrieves the current system time. + /// + /// The current system time. + public static DateTime GetTime() { + SYSTEMTIME systemTime = new SYSTEMTIME(); + GetSystemTime(ref systemTime); + + DateTime systemDateTime = new DateTime( + systemTime.wYear, systemTime.wMonth, systemTime.wDay, + systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds + ); + + return systemDateTime; + } + } +} diff --git a/ThunderboltTimeSync/ThunderboltTimeSync.csproj b/ThunderboltTimeSync/ThunderboltTimeSync.csproj new file mode 100644 index 0000000..e76789c --- /dev/null +++ b/ThunderboltTimeSync/ThunderboltTimeSync.csproj @@ -0,0 +1,91 @@ + + + + + Debug + AnyCPU + {7D970120-F57C-4541-BD54-2602C59EB8BA} + WinExe + Properties + ThunderboltTimeSync + ThunderboltTimeSync + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + FormMain.cs + + + + + + FormMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + \ No newline at end of file