Replaced the non-kernel code of WinRing0 with a managed implementation. The new implementation should fix Issue 32 and simplify further work on Issue 46.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Globalization;
43 namespace OpenHardwareMonitor.Hardware.TBalancer {
44 internal class TBalancer : IHardware {
46 private readonly ISettings settings;
47 private readonly int portIndex;
48 private readonly byte protocolVersion;
49 private readonly Sensor[] digitalTemperatures = new Sensor[8];
50 private readonly Sensor[] analogTemperatures = new Sensor[4];
51 private readonly Sensor[] sensorhubTemperatures = new Sensor[6];
52 private readonly Sensor[] sensorhubFlows = new Sensor[2];
53 private readonly Sensor[] fans = new Sensor[4];
54 private readonly Sensor[] controls = new Sensor[4];
55 private readonly Sensor[] miniNGTemperatures = new Sensor[4];
56 private readonly Sensor[] miniNGFans = new Sensor[4];
57 private readonly Sensor[] miniNGControls = new Sensor[4];
58 private readonly List<ISensor> active = new List<ISensor>();
59 private readonly List<ISensor> deactivating = new List<ISensor>();
61 private FT_HANDLE handle;
62 private int[] primaryData = new int[0];
63 private int[] alternativeData = new int[0];
65 public const byte STARTFLAG = 100;
66 public const byte ENDFLAG = 254;
68 private delegate void MethodDelegate();
69 private readonly MethodDelegate alternativeRequest;
71 public TBalancer(int portIndex, byte protocolVersion, ISettings settings) {
72 this.settings = settings;
74 this.portIndex = portIndex;
75 this.protocolVersion = protocolVersion;
77 ParameterDescription[] parameter = new [] {
78 new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
81 for (int i = 0; i < digitalTemperatures.Length; i++)
82 digitalTemperatures[i] = new Sensor("Digital Sensor " + i,
83 offset + i, SensorType.Temperature, this, parameter, settings);
84 offset += digitalTemperatures.Length;
86 for (int i = 0; i < analogTemperatures.Length; i++)
87 analogTemperatures[i] = new Sensor("Analog Sensor " + (i + 1),
88 offset + i, SensorType.Temperature, this, parameter, settings);
89 offset += analogTemperatures.Length;
91 for (int i = 0; i < sensorhubTemperatures.Length; i++)
92 sensorhubTemperatures[i] = new Sensor("Sensorhub Sensor " + i,
93 offset + i, SensorType.Temperature, this, parameter, settings);
94 offset += sensorhubTemperatures.Length;
96 for (int i = 0; i < miniNGTemperatures.Length; i++)
97 miniNGTemperatures[i] = new Sensor("miniNG #" + (i / 2 + 1) +
98 " Sensor " + (i % 2 + 1), offset + i, SensorType.Temperature,
99 this, parameter, settings);
100 offset += miniNGTemperatures.Length;
102 for (int i = 0; i < sensorhubFlows.Length; i++)
103 sensorhubFlows[i] = new Sensor("Flowmeter " + (i + 1),
104 i, SensorType.Flow, this, new [] {
105 new ParameterDescription("Impulse Rate",
106 "The impulse rate of the flowmeter in pulses/L", 509)
109 for (int i = 0; i < controls.Length; i++) {
110 controls[i] = new Sensor("Fan Channel " + i, i, SensorType.Control,
114 for (int i = 0; i < miniNGControls.Length; i++) {
115 miniNGControls[i] = new Sensor("miniNG #" + (i / 2 + 1) +
116 " Fan Channel " + (i % 2 + 1), 4 + i, SensorType.Control, this,
120 alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
126 private void ActivateSensor(Sensor sensor) {
127 deactivating.Remove(sensor);
128 if (!active.Contains(sensor)) {
130 if (SensorAdded != null)
135 private void DeactivateSensor(Sensor sensor) {
136 if (deactivating.Contains(sensor)) {
137 active.Remove(sensor);
138 deactivating.Remove(sensor);
139 if (SensorRemoved != null)
140 SensorRemoved(sensor);
141 } else if (active.Contains(sensor)) {
142 deactivating.Add(sensor);
146 private void ReadminiNG(int[] data, int number) {
147 int offset = 1 + number * 65;
149 if (data[offset + 61] != ENDFLAG)
152 for (int i = 0; i < 2; i++) {
153 Sensor sensor = miniNGTemperatures[number * 2 + i];
154 if (data[offset + 7 + i] > 0) {
155 sensor.Value = 0.5f * data[offset + 7 + i] +
156 sensor.Parameters[0].Value;
157 ActivateSensor(sensor);
159 DeactivateSensor(sensor);
163 for (int i = 0; i < 2; i++) {
164 if (miniNGFans[number * 2 + i] == null)
165 miniNGFans[number * 2 + i] =
166 new Sensor("miniNG #" + (number + 1) + " Fan Channel " + (i + 1),
167 4 + number * 2 + i, SensorType.Fan, this, settings);
169 Sensor sensor = miniNGFans[number * 2 + i];
171 sensor.Value = 20.0f * data[offset + 43 + 2 * i];
172 ActivateSensor(sensor);
175 for (int i = 0; i < 2; i++) {
176 Sensor sensor = miniNGControls[number * 2 + i];
177 sensor.Value = data[offset + 15 + i];
178 ActivateSensor(sensor);
182 private void ReadData() {
183 int[] data = new int[285];
184 for (int i = 0; i < data.Length; i++)
185 data[i] = FTD2XX.ReadByte(handle);
187 if (data[0] != STARTFLAG) {
188 FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX);
192 if (data[1] == 255 || data[1] == 88) { // bigNG
194 if (data[274] != protocolVersion)
197 this.primaryData = data;
199 for (int i = 0; i < digitalTemperatures.Length; i++)
200 if (data[238 + i] > 0) {
201 digitalTemperatures[i].Value = 0.5f * data[238 + i] +
202 digitalTemperatures[i].Parameters[0].Value;
203 ActivateSensor(digitalTemperatures[i]);
205 DeactivateSensor(digitalTemperatures[i]);
208 for (int i = 0; i < analogTemperatures.Length; i++)
209 if (data[260 + i] > 0) {
210 analogTemperatures[i].Value = 0.5f * data[260 + i] +
211 analogTemperatures[i].Parameters[0].Value;
212 ActivateSensor(analogTemperatures[i]);
214 DeactivateSensor(analogTemperatures[i]);
217 for (int i = 0; i < sensorhubTemperatures.Length; i++)
218 if (data[246 + i] > 0) {
219 sensorhubTemperatures[i].Value = 0.5f * data[246 + i] +
220 sensorhubTemperatures[i].Parameters[0].Value;
221 ActivateSensor(sensorhubTemperatures[i]);
223 DeactivateSensor(sensorhubTemperatures[i]);
226 for (int i = 0; i < sensorhubFlows.Length; i++)
227 if (data[231 + i] > 0 && data[234] > 0) {
228 float pulsesPerSecond = (data[231 + i] * 4.0f) / data[234];
229 float pulsesPerLiter = sensorhubFlows[i].Parameters[0].Value;
230 sensorhubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
231 ActivateSensor(sensorhubFlows[i]);
233 DeactivateSensor(sensorhubFlows[i]);
236 for (int i = 0; i < fans.Length; i++) {
237 float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
240 fans[i] = new Sensor("Fan Channel " + i, i, SensorType.Fan,
241 this, new [] { new ParameterDescription("MaxRPM",
242 "Maximum revolutions per minute (RPM) of the fan.", maxRPM)
246 if ((data[136] & (1 << i)) == 0) // pwm mode
247 value = 0.02f * data[137 + i];
249 value = 0.01f * data[141 + i];
251 fans[i].Value = fans[i].Parameters[0].Value * value;
252 ActivateSensor(fans[i]);
254 controls[i].Value = 100 * value;
255 ActivateSensor(controls[i]);
258 } else if (data[1] == 253) { // miniNG #1
259 this.alternativeData = data;
263 if (data[66] == 253) // miniNG #2
268 public HardwareType HardwareType {
269 get { return HardwareType.TBalancer; }
273 get { return "T-Balancer bigNG"; }
276 public Identifier Identifier {
278 return new Identifier("bigng",
279 this.portIndex.ToString(CultureInfo.InvariantCulture));
283 public IHardware[] SubHardware {
284 get { return new IHardware[0]; }
287 public virtual IHardware Parent {
291 public ISensor[] Sensors {
292 get { return active.ToArray(); }
295 public string GetReport() {
296 StringBuilder r = new StringBuilder();
298 r.AppendLine("T-Balancer bigNG");
300 r.Append("Port Index: ");
301 r.AppendLine(portIndex.ToString(CultureInfo.InvariantCulture));
304 r.AppendLine("Primary System Information Answer");
306 r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
308 for (int i = 0; i <= 0x11; i++) {
310 r.Append((i << 4).ToString("X3", CultureInfo.InvariantCulture));
312 for (int j = 0; j <= 0xF; j++) {
313 int index = ((i << 4) | j);
314 if (index < primaryData.Length) {
316 r.Append(primaryData[index].ToString("X2", CultureInfo.InvariantCulture));
323 if (alternativeData.Length > 0) {
324 r.AppendLine("Alternative System Information Answer");
326 r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
328 for (int i = 0; i <= 0x11; i++) {
330 r.Append((i << 4).ToString("X3", CultureInfo.InvariantCulture));
332 for (int j = 0; j <= 0xF; j++) {
333 int index = ((i << 4) | j);
334 if (index < alternativeData.Length) {
336 r.Append(alternativeData[index].ToString("X2", CultureInfo.InvariantCulture));
347 private void DelayedAlternativeRequest() {
348 System.Threading.Thread.Sleep(500);
349 FTD2XX.Write(handle, new byte[] { 0x37 });
353 FTD2XX.FT_Open(portIndex, out handle);
354 FTD2XX.FT_SetBaudRate(handle, 19200);
355 FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
356 FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
358 FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
359 FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
362 public void Update() {
363 while (FTD2XX.BytesToRead(handle) >= 285)
365 if (FTD2XX.BytesToRead(handle) == 1)
366 FTD2XX.ReadByte(handle);
368 FTD2XX.Write(handle, new byte[] { 0x38 });
369 alternativeRequest.BeginInvoke(null, null);
372 public void Close() {
373 FTD2XX.FT_Close(handle);
376 public event SensorEventHandler SensorAdded;
377 public event SensorEventHandler SensorRemoved;
379 public void Accept(IVisitor visitor) {
381 throw new ArgumentNullException("visitor");
382 visitor.VisitHardware(this);
385 public void Traverse(IVisitor visitor) { }