moel@324: /*
moel@324:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@324:  
moel@344:   Copyright (C) 2011-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@324: */
moel@324: 
moel@324: using System;
moel@324: using System.Collections.Generic;
moel@324: using System.Text;
moel@324: 
moel@324: namespace OpenHardwareMonitor.Hardware.HDD {
moel@324: 
moel@324: #if DEBUG
moel@324: 
moel@324:   internal class DebugSmart : ISmart {
moel@324: 
moel@324:     private Drive[] drives = {
moel@328:       new Drive("KINGSTON SNV425S264GB", null, 16,
moel@324:         @" 01 000000000000 100 100      
moel@324:            02 000000000000 100 100      
moel@324:            03 000000000000 100 100      
moel@324:            05 000000000000 100 100      
moel@324:            07 000000000000 100 100      
moel@324:            08 000000000000 100 100      
moel@324:            09 821E00000000 100 100      
moel@324:            0A 000000000000 100 100      
moel@324:            0C 950200000000 100 100      
moel@324:            A8 000000000000 100 100      
moel@324:            AF 000000000000 100 100      
moel@324:            C0 000000000000 100 100      
moel@324:            C2 290014002B00 100 41       
moel@324:            C5 000000000000 100 100      
moel@324:            F0 000000000000 100 100      
moel@324:            AA 07007B000000 100 100      
moel@324:            AD 0E1E71304919 100 100"),
moel@324: 
moel@328:       new Drive("PLEXTOR  PX-128M2S", "1.03", 16, 
moel@324:         @" 01 000000000000 100 100 0   
moel@324:            03 000000000000 100 100 0   
moel@324:            04 000000000000 100 100 0   
moel@324:            05 000000000000 100 100 0   
moel@324:            09 250100000000 100 100 0   
moel@324:            0A 000000000000 100 100 0   
moel@324:            0C D10000000000 100 100 0   
moel@324:            B2 000000000000 100 100 0   
moel@324:            BB 000000000000 100 100 0   
moel@324:            BE 000000000000 100 100 0   
moel@324:            C0 000000000000 100 100 0   
moel@324:            C1 000000000000 100 100 0   
moel@324:            C2 000000000000 100 100 0   
moel@324:            C3 000000000000 100 100 0   
moel@324:            C5 000000000000 100 100 0   
moel@324:            C6 000000000000 100 100 0   
moel@324:            C7 000000000000 100 100 0"),
moel@324: 
moel@328:       new Drive("OCZ-VERTEX2", "1.25", 16, 
moel@324:         @" 01 DADAD5000000 100 106 50
moel@324:            05 000000000000 100 100 3 
moel@324:            09 DF0900004A2F 100 100 0 
moel@324:            0C FC0100000000 100 100 0 
moel@324:            AB 000000000000 0   0   0 
moel@324:            AC 000000000000 0   0   0 
moel@324:            AE 1F0000000000 0   0   0 
moel@324:            B1 000000000000 0   0   0 
moel@324:            B5 000000000000 0   0   0 
moel@324:            B6 000000000000 0   0   0 
moel@324:            BB 000000000000 100 100 0 
moel@324:            C2 010081007F00 129 1   0 
moel@324:            C3 DADAD5000000 100 106 0 
moel@324:            C4 000000000000 100 100 0 
moel@324:            E7 000000000000 100 100 10
moel@324:            E9 800400000000 0   0   0 
moel@324:            EA 000600000000 0   0   0 
moel@324:            F1 000600000000 0   0   0 
moel@324:            F2 801200000000 0   0   0"),
moel@324:       
moel@328:       new Drive("WDC WD5000AADS-00S9B0", null, 10, 
moel@324:         @" 1   000000000000 200 200         
moel@324:            3   820D00000000 149 150         
moel@324:            4   610800000000 98  98          
moel@324:            5   000000000000 200 200         
moel@324:            7   000000000000 253 100         
moel@324:            9   0F1F00000000 90  90          
moel@324:            10  000000000000 100 100         
moel@324:            11  000000000000 100 100         
moel@324:            12  880200000000 100 100         
moel@324:            192 6B0000000000 200 200         
moel@324:            193 E9CB03000000 118 118         
moel@324:            194 280000000000 94  103         
moel@324:            196 000000000000 200 200         
moel@324:            197 000000000000 200 200         
moel@324:            198 000000000000 200 200         
moel@324:            199 000000000000 200 200         
moel@324:            200 000000000000 200 200         
moel@324:            130 7B0300010002 1   41          
moel@324:            5   000000000000 0   0           
moel@324:            1   000000000000 0   0"),
moel@324: 
moel@328:       new Drive("INTEL SSDSA2M080G2GC", null, 10, 
moel@324:         @" 3   000000000000 100 100         
moel@324:            4   000000000000 100 100         
moel@324:            5   010000000000 100 100         
moel@324:            9   B10B00000000 100 100         
moel@324:            12  DD0300000000 100 100         
moel@324:            192 480000000000 100 100         
moel@324:            225 89DB00000000 200 200         
moel@324:            226 3D1B00000000 100 100         
moel@324:            227 030000000000 100 100         
moel@324:            228 7F85703C0000 100 100         
moel@324:            232 000000000000 99  99          
moel@324:            233 000000000000 98  98          
moel@324:            184 000000000000 100 100         
moel@324:            1   000000000000 0   0"),
moel@324: 
moel@328:       new Drive("OCZ-VERTEX", null, 10, 
moel@324:         @" 1   000000000000 0   8   
moel@324:            9   000000000000 30  99  
moel@324:            12  000000000000 0   15  
moel@324:            184 000000000000 0   7   
moel@324:            195 000000000000 0   0   
moel@324:            196 000000000000 0   2   
moel@324:            197 000000000000 0   0   
moel@324:            198 B9ED00000000 214 176 
moel@324:            199 352701000000 143 185 
moel@324:            200 B10500000000 105 55  
moel@324:            201 F40A00000000 238 194 
moel@324:            202 020000000000 137 35  
moel@324:            203 020000000000 125 63  
moel@324:            204 000000000000 0   0   
moel@324:            205 000000000000 19  136 
moel@324:            206 000000000000 22  54  
moel@324:            207 010000000000 113 226 
moel@324:            208 000000000000 49  232 
moel@324:            209 000000000000 0   98  
moel@324:            211 000000000000 0   0   
moel@324:            212 000000000000 0   0   
moel@324:            213 000000000000 0   0"),
moel@324:  
moel@328:       new Drive("INTEL SSDSA2CW120G3", null, 16,
moel@324:         @"03 000000000000 100 100 0
moel@324:           04 000000000000 100 100 0
moel@324:           05 000000000000 100 100 0
moel@324:           09 830200000000 100 100 0
moel@324:           0C 900100000000 100 100 0
moel@324:           AA 000000000000 100 100 0
moel@324:           AB 000000000000 100 100 0
moel@324:           AC 000000000000 100 100 0
moel@324:           B8 000000000000 100 100 0
moel@324:           BB 000000000000 100 100 0
moel@324:           C0 040000000000 100 100 0
moel@324:           E1 FF4300000000 100 100 0
moel@324:           E2 E57D14000000 100 100 0
moel@324:           E3 000000000000 100 100 0
moel@324:           E4 E39600000000 100 100 0
moel@324:           E8 000000000000 100 100 0
moel@324:           E9 000000000000 100 100 0
moel@324:           F1 FF4300000000 100 100 0
moel@328:           F2 264F00000000 100 100 0"),
moel@328: 
moel@328:      new Drive("CORSAIR CMFSSD-128GBG2D", "VBM19C1Q", 16, 
moel@328:        @"09 100900000000 99  99  0 
moel@328:          0C 560200000000 99  99  0 
moel@328:          AF 000000000000 100 100 10
moel@328:          B0 000000000000 100 100 10
moel@328:          B1 2A0000000000 99  99  17
moel@328:          B2 180000000000 60  60  10
moel@328:          B3 4B0000000000 98  98  10
moel@328:          B4 B50E00000000 98  98  10
moel@328:          B5 000000000000 100 100 10
moel@328:          B6 000000000000 100 100 10
moel@328:          B7 000000000000 100 100 10
moel@328:          BB 000000000000 100 100 0 
moel@328:          C3 000000000000 200 200 0 
moel@328:          C6 000000000000 100 100 0 
moel@328:          C7 810100000000 253 253 0 
moel@328:          E8 240000000000 60  60  10
moel@330:          E9 630594120000 92  92  0"),
moel@330: 
moel@330:       new Drive("Maxtor 6L300R0", null, 10,    
moel@330:         @"3   9E5500000000 183 193         
moel@330:           4   0A0D00000000 252 252         
moel@330:           5   010000000000 253 253         
moel@330:           6   000000000000 253 253         
moel@330:           7   000000000000 252 253         
moel@330:           8   DFA700000000 224 245         
moel@330:           9   CE5700000000 155 155         
moel@330:           10  000000000000 252 253         
moel@330:           11  000000000000 252 253         
moel@330:           12  BA0400000000 250 250         
moel@330:           192 000000000000 253 253         
moel@330:           193 000000000000 253 253         
moel@330:           194 3D0000000000 253 42          
moel@330:           195 5D1F00000000 252 253         
moel@330:           196 000000000000 253 253         
moel@330:           197 010000000000 253 253         
moel@330:           198 000000000000 253 253         
moel@330:           199 030000000000 196 199         
moel@330:           200 000000000000 252 253         
moel@330:           201 000000000000 252 253         
moel@330:           202 000000000000 252 253         
moel@330:           203 000000000000 252 253         
moel@330:           204 000000000000 252 253         
moel@330:           205 000000000000 252 253         
moel@330:           207 000000000000 252 253         
moel@330:           208 000000000000 252 253         
moel@330:           209 EA0000000000 234 234         
moel@330:           210 000000000000 252 253         
moel@330:           211 000000000000 252 253         
moel@330:           212 000000000000 252 253         
moel@330:           130 5B0300010002 1   9           
moel@330:           59  FC3203030100 205 0           
moel@330:           1   000000000000 0   0           
moel@338:           144 000000000000 0   34 "),
moel@338: 
moel@338:         new Drive("M4-CT256M4SSD2", "0309", 16, 
moel@338:           @"01 000000000000 100 100 50     
moel@338:             05 000000000000 100 100 10     
moel@338:             09 AB0100000000 100 100 1      
moel@338:             0C 6E0000000000 100 100 1      
moel@338:             AA 000000000000 100 100 10     
moel@338:             AB 000000000000 100 100 1      
moel@338:             AC 000000000000 100 100 1      
moel@338:             AD 060000000000 100 100 10     
moel@338:             AE 000000000000 100 100 1      
moel@338:             B5 79003D00B700 100 100 1      
moel@338:             B7 000000000000 100 100 1      
moel@338:             B8 000000000000 100 100 50     
moel@338:             BB 000000000000 100 100 1      
moel@338:             BC 000000000000 100 100 1      
moel@338:             BD 5B0000000000 100 100 1      
moel@338:             C2 000000000000 100 100 0      
moel@338:             C3 000000000000 100 100 1      
moel@338:             C4 000000000000 100 100 1      
moel@338:             C5 000000000000 100 100 1      
moel@338:             C6 000000000000 100 100 1      
moel@338:             C7 000000000000 100 100 1      
moel@338:             CA 000000000000 100 100 1      
moel@338:             CE 000000000000 100 100 1 "),
moel@338: 
moel@338:         new Drive("C300-CTFDDAC256MAG", "0007", 16, 
moel@338:           @"01 000000000000 100 100 0  
moel@338:             05 000000000000 100 100 0  
moel@338:             09 4C0A00000000 100 100 0  
moel@338:             0C 0F0100000000 100 100 0  
moel@338:             AA 000000000000 100 100 0  
moel@338:             AB 000000000000 100 100 0  
moel@338:             AC 000000000000 100 100 0  
moel@338:             AD 1B0000000000 100 100 0  
moel@338:             AE 000000000000 100 100 0  
moel@338:             B5 D30357012B05 100 100 0  
moel@338:             B7 000000000000 100 100 0  
moel@338:             B8 000000000000 100 100 0  
moel@338:             BB 000000000000 100 100 0  
moel@338:             BC 000000000000 100 100 0  
moel@338:             BD C60100000000 100 100 0  
moel@338:             C3 000000000000 100 100 0  
moel@338:             C4 000000000000 100 100 0  
moel@338:             C5 000000000000 100 100 0  
moel@338:             C6 000000000000 100 100 0  
moel@338:             C7 000000000000 100 100 0  
moel@338:             CA 000000000000 100 100 0  
moel@358:             CE 000000000000 100 100 0"),
moel@358: 
moel@358:         new Drive("M4-CT064M4SSD2", "0009", 16, 
moel@358:           @"01 000000000000 100 100 50
moel@358:             05 000000000000 100 100 10
moel@358:             09 260000000000 100 100 1 
moel@358:             0C 5A0000000000 100 100 1 
moel@358:             AA 000000000000 100 100 10
moel@358:             AB 000000000000 100 100 1 
moel@358:             AC 000000000000 100 100 1 
moel@358:             AD 010000000000 100 100 10
moel@358:             AE 000000000000 100 100 1 
moel@358:             B5 2B000E003A00 100 100 1 
moel@358:             B7 000000000000 100 100 1 
moel@358:             B8 000000000000 100 100 50
moel@358:             BB 000000000000 100 100 1 
moel@358:             BC 000000000000 100 100 1 
moel@358:             BD 310000000000 100 100 1 
moel@358:             C2 000000000000 100 100 0 
moel@358:             C3 000000000000 100 100 1 
moel@358:             C4 000000000000 100 100 1 
moel@358:             C5 000000000000 100 100 1 
moel@358:             C6 000000000000 100 100 1 
moel@358:             C7 000000000000 100 100 1 
moel@358:             CA 000000000000 100 100 1 
moel@358:             CE 000000000000 100 100 1"),
moel@358: 
moel@358:         new Drive("M4-CT128M4SSD2", "000F", 16, 
moel@358:           @"01 000000000000 100 100 50 
moel@358:             05 000000000000 100 100 10 
moel@358:             09 CA1400000000 100 100 1  
moel@358:             0C A30200000000 100 100 1  
moel@358:             AA 000000000000 100 100 10 
moel@358:             AB 000000000000 100 100 1  
moel@358:             AC 000000000000 100 100 1  
moel@358:             AD 1F0000000000 99  99  10 
moel@358:             AE 140000000000 100 100 1  
moel@358:             B5 12037C028E05 100 100 1  
moel@358:             B7 000000000000 100 100 1  
moel@358:             B8 000000000000 100 100 50 
moel@358:             BB 000000000000 100 100 1  
moel@358:             BC 000000000000 100 100 1  
moel@358:             BD 510000000000 100 100 1  
moel@358:             C2 000000000000 100 100 0  
moel@358:             C3 000000000000 100 100 1  
moel@358:             C4 000000000000 100 100 1  
moel@358:             C5 000000000000 100 100 1  
moel@358:             C6 000000000000 100 100 1  
moel@358:             C7 000000000000 100 100 1  
moel@358:             CA 010000000000 99  99  1  
moel@358:             CE 000000000000 100 100 1 ")};
moel@324: 
moel@324:     public IntPtr OpenDrive(int driveNumber) {
moel@324:       if (driveNumber < drives.Length)
moel@324:         return (IntPtr)driveNumber;
moel@324:       else
moel@324:         return InvalidHandle;
moel@324:     }
moel@324: 
moel@324:     public bool EnableSmart(IntPtr handle, int driveNumber) {
moel@324:       if (handle != (IntPtr)driveNumber)
moel@324:         throw new ArgumentOutOfRangeException();
moel@324: 
moel@324:       return true;
moel@324:     }
moel@324: 
moel@324:     public DriveAttributeValue[] ReadSmartData(IntPtr handle, int driveNumber) {
moel@324:       if (handle != (IntPtr)driveNumber)
moel@324:         throw new ArgumentOutOfRangeException();
moel@324: 
moel@324:       return drives[driveNumber].DriveAttributeValues;
moel@324:     }
moel@324: 
moel@324:     public DriveThresholdValue[] ReadSmartThresholds(IntPtr handle, 
moel@324:       int driveNumber) 
moel@324:     {
moel@324:       if (handle != (IntPtr)driveNumber)
moel@324:         throw new ArgumentOutOfRangeException();
moel@324: 
moel@324:       return drives[driveNumber].DriveThresholdValues;
moel@324:     }
moel@324: 
moel@325:     public bool ReadNameAndFirmwareRevision(IntPtr handle, int driveNumber, 
moel@325:       out string name, out string firmwareRevision) {
moel@324:       if (handle != (IntPtr)driveNumber)
moel@324:         throw new ArgumentOutOfRangeException();
moel@324: 
moel@325:       name = drives[driveNumber].Name;
moel@328:       firmwareRevision = drives[driveNumber].FirmwareVersion;
moel@325:       return true;
moel@324:     }
moel@324: 
moel@324:     public void CloseHandle(IntPtr handle) { }
moel@324: 
moel@324: 
moel@324:     private class Drive {
moel@324: 
moel@328:       public Drive(string name, string firmware, int idBase, string value) {
moel@324:         this.Name = name;
moel@328:         this.FirmwareVersion = firmware;
moel@324: 
moel@324:         string[] lines = value.Split(new[] { '\r', '\n' }, 
moel@324:           StringSplitOptions.RemoveEmptyEntries);
moel@324: 
moel@324:         DriveAttributeValues = new DriveAttributeValue[lines.Length];
moel@324:         List<DriveThresholdValue> thresholds = new List<DriveThresholdValue>();
moel@324: 
moel@324:         for (int i = 0; i < lines.Length; i++) {
moel@324: 
moel@324:           string[] array = lines[i].Split(new[] { ' ' },
moel@324:             StringSplitOptions.RemoveEmptyEntries);
moel@324: 
moel@324:           if (array.Length != 4 && array.Length != 5)
moel@324:             throw new Exception();
moel@324: 
moel@324:           DriveAttributeValue v = new DriveAttributeValue();
moel@324:           v.Identifier = Convert.ToByte(array[0], idBase);
moel@324: 
moel@324:           v.RawValue = new byte[6];
moel@324:           for (int j = 0; j < 6; j++) {
moel@324:             v.RawValue[j] = Convert.ToByte(array[1].Substring(2 * j, 2), 16);
moel@324:           }
moel@324: 
moel@324:           v.WorstValue = Convert.ToByte(array[2], 10);
moel@324:           v.AttrValue = Convert.ToByte(array[3], 10);
moel@324: 
moel@324:           DriveAttributeValues[i] = v;
moel@324: 
moel@324:           if (array.Length == 5) {
moel@324:             DriveThresholdValue t = new DriveThresholdValue();
moel@324:             t.Identifier = v.Identifier;
moel@324:             t.Threshold = Convert.ToByte(array[4], 10);
moel@324:             thresholds.Add(t);
moel@324:           }
moel@324:         }
moel@324: 
moel@324:         DriveThresholdValues = thresholds.ToArray();
moel@324:       }
moel@324: 
moel@324:       public DriveAttributeValue[] DriveAttributeValues { get; private set; }
moel@324: 
moel@324:       public DriveThresholdValue[] DriveThresholdValues { get; private set; }
moel@324: 
moel@324:       public string Name { get; private set; }
moel@328: 
moel@328:       public string FirmwareVersion { get; private set; }
moel@324:     }
moel@324: 
moel@324:     public IntPtr InvalidHandle { get { return (IntPtr)(-1); } }
moel@369: 
moel@369:     public string[] GetLogicalDrives(int driveIndex) {
moel@369:       return new string[0];
moel@369:     }
moel@324:   }
moel@324: 
moel@324: #endif
moel@324: 
moel@324: }