use int64 instead of int32 for CRC/HEC/FEC values (they can get quite large eq FEC...
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / util.cs
CommitLineData
a8e0f728
S
1using Microsoft.Win32;
2using Newtonsoft.Json.Linq;
491f6e3b 3using System;
a3157ac3 4using System.Linq;
a554ec14
S
5using System.Net.NetworkInformation;
6using System.Net.Sockets;
491f6e3b
S
7using System.Security.Cryptography;
8using System.Text;
3a99b353
S
9using System.Threading;
10using System.Windows;
491f6e3b
S
11using System.Windows.Media;
12using System.Xml;
13
a5a62e8d
S
14namespace SpeedportHybridControl.Implementations
15{
681212b8 16 public static class util
a5a62e8d
S
17 {
18 public static byte[] HexToByte(string hex)
19 {
20 return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
21 }
22
23 /**
491f6e3b
S
24 * get sha256
25 *
26 * @param string password
27 * @return string
28 */
a5a62e8d
S
29 public static string sha256(this string password)
30 {
31 SHA256Managed crypt = new SHA256Managed();
32 string hash = BitConverter.ToString(crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password)));
33 crypt = null;
34 return hash.Replace("-", "").ToLower();
35 }
36
37 /**
491f6e3b
S
38 * get pbkdf2
39 *
40 * @param string password
41 * @param string salt
42 * @param int iterations
43 * @param int length
44 * @return string
45 */
a5a62e8d
S
46 public static string pbkdf2(this string password, string salt, int iterations = 1000, int length = 16)
47 {
48 Rfc2898DeriveBytes hash = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), iterations);
49 string derivedk = BitConverter.ToString(hash.GetBytes(length));
50 hash = null;
51 return derivedk.Replace("-", "").ToLower(); ;
52 }
53
54 /**
491f6e3b
S
55 * get specific value from JToken
56 *
57 * @param JToken jArray
58 * @param string varid
59 * @return string
60 */
a5a62e8d
S
61 public static string getVar(this JToken jArray, string varid)
62 {
63 foreach (JToken jToken in jArray)
64 {
65 JToken jVarid = jToken["varid"];
66 if (jVarid.ToString().Equals(varid))
67 {
68 jVarid = null;
69 jArray = null;
70 varid = null;
71 return jToken["varvalue"].ToString();
72 }
73
74 jVarid = null;
75 }
76
77 jArray = null;
78 varid = null;
79
80 return string.Empty;
81 }
82
83 /**
491f6e3b
S
84 * check if string is empty or null
85 *
86 * @param string value
87 * @return bool
88 */
a5a62e8d
S
89 public static bool IsNullOrEmpty(this string value)
90 {
91 return string.IsNullOrEmpty(value);
92 }
491f6e3b 93
a5a62e8d 94 /**
491f6e3b
S
95 * convert string to int
96 *
97 * @param string value
98 * @return int
99 */
a5a62e8d
S
100 public static int ToInt(this string value)
101 {
102 int b = 0;
103 int.TryParse(value, out b);
491f6e3b 104
a5a62e8d
S
105 return b;
106 }
491f6e3b 107
a5a62e8d 108 /**
491f6e3b
S
109 * calculate the background color for rsrp
110 * see http://www.lte-anbieter.info/technik/rsrp.php
111 *
112 * @param int rsrp
113 * @return Brush
114 */
a5a62e8d
S
115 public static Brush getRSRPColor(int rsrp)
116 {
117 if (rsrp >= -65)
118 {
119 return Brushes.DarkGreen;
120 }
121 else if (rsrp <= -66 && rsrp >= -83)
122 {
123 return Brushes.Green;
124 }
125 else if (rsrp <= -84 && rsrp >= -95)
126 {
127
128 return Brushes.Yellow;
129 }
130 else if (rsrp <= -96 && rsrp >= -105)
131 {
132 return Brushes.Orange;
133 }
134 else if (rsrp <= -106 && rsrp >= -125)
135 {
136 return Brushes.Red;
137 }
d12d28e4 138 else if (rsrp <= -126)
a5a62e8d
S
139 {
140 return Brushes.DarkRed;
141 }
142
143 return Brushes.Transparent;
144 }
145
146 /**
491f6e3b
S
147 * calculate the background color for rsrq
148 * see http://www.lte-anbieter.info/technik/rsrq.php
149 *
150 * @param int rsrp
151 * @return Brush
152 */
a5a62e8d
S
153 public static Brush getRSRQColor(int rsrq)
154 {
155 if (rsrq >= -3)
156 {
157 return Brushes.DarkGreen;
158 }
159 else if (rsrq <= -4 && rsrq >= -6)
160 {
161 return Brushes.Green;
162 }
163 else if (rsrq <= -7 && rsrq >= -8)
164 {
165 return Brushes.Yellow;
166 }
167 else if (rsrq <= -9 && rsrq >= -11)
168 {
169 return Brushes.Orange;
170 }
171 else if (rsrq <= -12 && rsrq >= -15)
172 {
173 return Brushes.Red;
174 }
175 else if (rsrq <= -16 && rsrq >= -20)
176 {
177 return Brushes.DarkRed;
178 }
179
180 return Brushes.Transparent;
181 }
182
183 /**
491f6e3b
S
184 * check for update
185 */
a5a62e8d
S
186 public static bool checkUpdate(string currentVersion)
187 {
188 try
189 {
190 XmlDocument xmlDocument = new XmlDocument();
191 xmlDocument.Load("https://stricted.net/version.xml");
192
193 string version = xmlDocument.DocumentElement["version"].InnerText;
194 if (currentVersion.Equals(version).Equals(false))
195 {
196 return true;
197 }
198 }
199 catch (Exception ex)
200 {
201 Console.WriteLine(ex.Message);
202 }
203
204 return false;
205 }
206
207 public static bool checkInstalled(string c_name)
208 {
209 string displayName = string.Empty;
210
211 string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
212 RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
213 if (key != null)
214 {
215 foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
216 {
217 displayName = subkey.GetValue("DisplayName") as string;
218
219 if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
220 {
221 return true;
222 }
223 }
224 key.Close();
225 }
226
227 registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
228 key = Registry.LocalMachine.OpenSubKey(registryKey);
229 if (key != null)
230 {
231 foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
232 {
233 displayName = subkey.GetValue("DisplayName") as string;
234
235 if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
236 {
237 return true;
238 }
239 }
240 key.Close();
241 }
242
243 return false;
244 }
a554ec14
S
245
246 public static bool checkLteModul ()
247 {
248 Ping ping = new Ping();
3a99b353 249 try
a554ec14 250 {
54802b99 251 PingReply reply = ping.Send("172.10.10.1", 2);
3a99b353
S
252
253 if (reply.Status == IPStatus.Success)
254 {
255 return true;
256 }
a554ec14 257 }
681212b8 258 catch (PingException) { }
a554ec14
S
259
260 return false;
261 }
262
e8057c7c 263 public static void sendCommandToLteModul(string Command)
a554ec14 264 {
e8057c7c 265 if (checkLteModul().Equals(true) && Command.IsNullOrEmpty().Equals(false))
a554ec14 266 {
3a99b353
S
267 try
268 {
2a0e9bbf
S
269 TcpClient client = new TcpClient("172.10.10.1", 1280);
270 NetworkStream stream = client.GetStream();
3a99b353 271 byte[] cmd = Encoding.ASCII.GetBytes(Command);
2a0e9bbf
S
272 stream.Write(cmd, 0, cmd.Length);
273 stream.Close();
274 client.Close();
3a99b353
S
275 }
276 catch (Exception)
277 {
681212b8 278 new Thread(() => { MessageBox.Show("couldn't send Command to the LTE Modul", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); }).Start();
3a99b353
S
279 LogManager.WriteToLog("couldn't send Command to LTE Modul");
280 }
a554ec14
S
281 }
282 }
e8057c7c 283
3f1e484e 284 public static void setLteFrequency (LTEBand band)
e8057c7c
S
285 {
286 /**
3f1e484e 287 * possible lte frequency band commands:
e8057c7c
S
288 *
289 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80000,, # 800
290 * AT^SYSCFGEX="03",3FFFFFFF,3,1,4,, # 1800
291 * AT^SYSCFGEX="03",3FFFFFFF,3,1,40,, # 2600
292 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80044,, # 800 | 1800 | 2600
293 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80004,, # 800 | 1800
294 * AT^SYSCFGEX="03",3FFFFFFF,3,1,80040,, # 800 | 2600
295 * AT^SYSCFGEX="03",3FFFFFFF,3,1,44,, # 1800 | 2600
296 */
681212b8 297 string Command = string.Concat("AT^SYSCFGEX=\"03\",3FFFFFFF,3,1,", (int)band, ",,\r");
e8057c7c
S
298
299 sendCommandToLteModul(Command);
300 }
a5a62e8d 301 }
491f6e3b 302}