Attribute VB_Name = "modDeviceControl"
' ----------------------------------------------------------------------------
'  Copyright (C) 2009 Noda (Yohan Lasorsa) - http://nodadev.wordpress.com
' ----------------------------------------------------------------------------
'  This software Is provided 'as-is', without any express or implied
'  warranty. In no event will the author be held liable for any damages
'  arising from the use of this software.
'
'  Permission is granted to anyone to use this software for any purpose,
'  including commercial applications, and to alter it and redistribute it
'  freely, subject to the following restrictions:
'
'  1. The origin of this software must not be misrepresented; you must not
'     claim that you wrote the original software. If you use this software
'     in a product, an acknowledgment in the product documentation would be
'     appreciated but is not required.
'  2. Altered source versions must be plainly marked as such, and must not be
'     misrepresented as being the original software.
'  3. This notice may not be removed or altered from any source distribution.
' ----------------------------------------------------------------------------

Option Explicit

Private Const DEV_ID_LAN As String = """PCI\VEN_10EC&DEV_8136*"""
Private Const DEV_ID_WIFI As String = """PCI\VEN_168C&DEV_001C*"""
Private Const DEV_ID_WEBCAM_A As String = """USB\VID_0C45*"""
Private Const DEV_ID_WEBCAM_B As String = """USB\VID_064E*"""

Public HDAPM As Integer
Public LANActive As Boolean
Public WifiActive As Boolean
Public WebcamActive As Boolean
Private WebcamModel As String

Public Sub DetectWebcamModel()
    Dim res As String

    ' detect webcam model
    res = GetCommandOutput("devcon find " & DEV_ID_WEBCAM_A, True)
    If (InStr(res, "No matching") > 0) Then
        WebcamModel = DEV_ID_WEBCAM_B
    Else
        WebcamModel = DEV_ID_WEBCAM_A
    End If
End Sub

Public Sub SetLANStatus(bActivate As Boolean)
    Dim act As String
    If (bActivate) Then
        act = "enable"
    Else
        act = "disable"
    End If
    LANActive = bActivate
    Shell "devcon " & act & " " & DEV_ID_LAN, vbHide
End Sub

Public Sub SetWebcamStatus(bActivate As Boolean)
    Dim act As String
    If (bActivate) Then
        act = "enable"
    Else
        act = "disable"
    End If
    WebcamActive = bActivate
    Shell "devcon " & act & " " & WebcamModel, vbHide
End Sub

Public Sub SetWifiStatus(bActivate As Boolean)
    Dim act As String
    If (bActivate) Then
        act = "enable"
    Else
        act = "disable"
    End If
    WifiActive = bActivate
    Shell "devcon " & act & " " & DEV_ID_WIFI, vbHide
End Sub

Function GetDevicesStatus() As Boolean
    Dim res As String
    
    ' get LAN card status
    res = GetCommandOutput("devcon status " & DEV_ID_LAN, True)
    If (InStr(res, "disabled") > 0) Then
        LANActive = False
    Else
        LANActive = True
    End If
    
    ' get webcam status
    res = GetCommandOutput("devcon status " & WebcamModel, True)
    If (InStr(res, "disabled") > 0) Then
        WebcamActive = False
    Else
        WebcamActive = True
    End If
    
    ' get wifi status
    res = GetCommandOutput("devcon status " & DEV_ID_WIFI, True)
    If (InStr(res, "disabled") > 0) Then
        WifiActive = False
    Else
        WifiActive = True
    End If
End Function

Public Sub SetHDAPM(iValue As Integer)
    Shell "hdparm -B " & iValue & " hda", vbHide
    HDAPM = iValue
End Sub
