diff --git a/StarlinkConnectionTest.sln b/StarlinkConnectionTest.sln new file mode 100644 index 0000000..5489df8 --- /dev/null +++ b/StarlinkConnectionTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32127.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarlinkConnectionTest", "StarlinkConnectionTest\StarlinkConnectionTest.csproj", "{D907657D-3813-4BC2-B4AE-B954D6EA799D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D907657D-3813-4BC2-B4AE-B954D6EA799D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D907657D-3813-4BC2-B4AE-B954D6EA799D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D907657D-3813-4BC2-B4AE-B954D6EA799D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D907657D-3813-4BC2-B4AE-B954D6EA799D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E21C4445-1FE8-4614-8FE3-E8547C7815D4} + EndGlobalSection +EndGlobal diff --git a/StarlinkConnectionTest/Program.cs b/StarlinkConnectionTest/Program.cs new file mode 100644 index 0000000..6de0074 --- /dev/null +++ b/StarlinkConnectionTest/Program.cs @@ -0,0 +1,102 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Sockets; + +internal class Program +{ + static void Main(string[] args) + { + PacketReciever pr = new PacketReciever(); + // This sequence is used to poll either the router (192.168.1.1) or dish (192.168.100.1) for statistics. + byte[] statsSequence = new byte[] { 0, 0, 0, 0, 3, 0xE2, 0x3E, 0 }; + + Timer t = new(new((x) => pr.Update(statsSequence)), null, 2000, 5000); // Fire at a regular interval + Console.ReadLine(); + } + class PacketReciever + { + readonly HttpClient client; + /// + /// List of known data tags + /// + static readonly List KnownTags = new() + { + new byte[] { 0xa5, 0x3f }, // BoresiteElevation + new byte[] { 0x9d, 0x3f }, // Boresite Azimuth + new byte[] { 0x8d, 0x3f }, // Ping + new byte[] { 0x85, 0x3f }, // Upload + new byte[] { 0xfd, 0x3e }, // Download + //new byte[] { 0xfd, 0x3f }, // Unknown + // ??? // Ping loss rate + }; + + public PacketReciever() + { + client = new(); + client.DefaultRequestVersion = HttpVersion.Version11; + client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; + } + + public async void Update(byte[] requestContent) + { + try + { + // Create the request + ByteArrayContent content = new(requestContent); + // gRPC expects the content type to be a gRPC request, else will not respond with valid data + content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/grpc-web+proto"); + // Send the request + HttpResponseMessage r = await client.PostAsync("http://192.168.100.1:9201/SpaceX.API.Device.Device/Handle", content); + Console.Clear(); + Dictionary tagValues = new(); + byte[] data = await r.Content.ReadAsByteArrayAsync(); + int dataIndex = LastIndexOf(data, new byte[] { 0x67, 0x72, 0x70, 0x63, 0x2D, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3A }); + while (dataIndex > -1) // Search for `grpc-status:`; is the known end of data + { + // Valid data packet, parse; + foreach (var item in KnownTags) + { + if (data[dataIndex] == item[0] && data[dataIndex + 1] == item[1]) + { + byte[] dataBytes = new byte[] { data[dataIndex+2], data[dataIndex+3], data[dataIndex+4], data[dataIndex+5] }; + tagValues.Add(string.Format("{0:X}{1:X}", item[0], item[1]), dataBytes); + break; + } + } + dataIndex--; + } + float download = tagValues.ContainsKey("FD3E") ? BitConverter.ToSingle(tagValues["FD3E"]) : 0.00f; + float upload = tagValues.ContainsKey("853F") ? BitConverter.ToSingle(tagValues["853F"]) : 0.00f; + Console.WriteLine(" DL: \t{0} b/s ({1:0.000} {2}B/s)", download, download / (download < 8e6f ? (8.0f * 1024.0f) : (8.0f * 1024.0f * 1024.0f)), download < 8e6 ? "k" : "M"); + Console.WriteLine(" UL: \t{0} b/s ({1:0.000} {2}B/s)", upload, upload / (upload < 8e6f ? (8.0f * 1024.0f) : (8.0f *1024.0f * 1024.0f)), upload < 8e6 ? "k" : "M"); + Console.WriteLine(" Ping: \t{0} ms", tagValues.ContainsKey("8D3F") ? BitConverter.ToSingle(tagValues["8D3F"]) : 0.00); + Console.WriteLine(" Boresite Azimuth: \t{0}°", tagValues.ContainsKey("9D3F") ? BitConverter.ToSingle(tagValues["9D3F"]) : 0.00); + Console.WriteLine("Boresite Elevation: \t{0}°", tagValues.ContainsKey("A53F") ? BitConverter.ToSingle(tagValues["A53F"]) : 0.00); + } + catch { } + } + + public static int LastIndexOf(byte[] src, byte[] searchBytes) + { + if (src.Length <= searchBytes.Length) return -1; + int pos = src.Length - searchBytes.Length; + while (pos > -1 && !exactMatch(src, searchBytes, pos)) + { + pos--; + } + return pos; + } + + static bool exactMatch(byte[] src, byte[] search, int pos) + { + for (int i = 0; i < search.Length; i++) + { + if (src[pos+i] != search[i]) + { + return false; + } + } + return true; + } + } +} \ No newline at end of file diff --git a/StarlinkConnectionTest/StarlinkConnectionTest.csproj b/StarlinkConnectionTest/StarlinkConnectionTest.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/StarlinkConnectionTest/StarlinkConnectionTest.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +