SharpDisplay: Migrating to new robust client scheme.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2011 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
14 using System.IO.Compression;
15 using System.Runtime.InteropServices;
18 namespace OpenHardwareMonitor.Hardware.Mainboard {
20 internal class GigabyteTAMG {
23 private Sensor[] sensors;
25 private struct Sensor {
27 public SensorType Type;
32 private enum SensorType {
39 public GigabyteTAMG(byte[] table) {
41 throw new ArgumentNullException("table");
45 int index = IndexOf(table, Encoding.ASCII.GetBytes("$HEALTH$"), 0);
49 using (MemoryStream m =
50 new MemoryStream(table, index, table.Length - index))
51 using (BinaryReader r = new BinaryReader(m)) {
54 int count = r.ReadInt32();
57 sensors = new Sensor[count];
58 for (int i = 0; i < sensors.Length; i++) {
59 sensors[i].Name = new string(r.ReadChars(32)).TrimEnd('\0');
60 sensors[i].Type = (SensorType)r.ReadByte();
61 sensors[i].Channel = r.ReadInt16();
62 sensors[i].Channel |= r.ReadByte() << 24;
64 int value = r.ReadInt32();
65 switch (sensors[i].Type) {
66 case SensorType.Voltage:
67 sensors[i].Value = 1e-3f * value; break;
69 sensors[i].Value = value; break;
73 } catch (IOException) { sensors = new Sensor[0]; }
76 sensors = new Sensor[0];
80 public static int IndexOf(byte[] array, byte[] pattern, int startIndex) {
81 if (array == null || pattern == null || pattern.Length > array.Length)
84 for (int i = startIndex; i < array.Length - pattern.Length; i++) {
86 for (int j = 0; j < pattern.Length; j++) {
87 if (array[i + j] != pattern[j]) {
98 private string GetCompressedAndEncodedTable() {
100 using (MemoryStream m = new MemoryStream()) {
101 using (GZipStream c = new GZipStream(m, CompressionMode.Compress)) {
102 c.Write(table, 0, table.Length);
104 base64 = Convert.ToBase64String(m.ToArray());
107 StringBuilder r = new StringBuilder();
108 for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
110 for (int j = 0; j < 0x40; j++) {
111 int index = (i << 6) | j;
112 if (index < base64.Length) {
113 r.Append(base64[index]);
122 public string GetReport() {
123 StringBuilder r = new StringBuilder();
125 if (sensors.Length > 0) {
126 r.AppendLine("Gigabyte TAMG Sensors");
129 foreach (Sensor sensor in sensors) {
130 r.AppendFormat(" {0,-10}: {1,8:G6} ({2})", sensor.Name, sensor.Value,
137 if (table.Length > 0) {
138 r.AppendLine("Gigabyte TAMG Table");
140 r.Append(GetCompressedAndEncodedTable());