📄 ethman.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
#include <windows.h>
#include <ceddk.h>
#include <pmimpl.h>
#include <pmpolicy.h>
#include <ntddndis.h>
#include <nuiouser.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <nkintr.h>
#include <bsp_cfg.h>
#include "zones.h"
#ifndef SHIP_BUILD
#define DBG_ERROR 1
#define DBG_WARNING 2
#define DBG_VERBOSE 4
#define DBG_INIT 8
#define DBG_FUNCTION 16
DBGPARAM dpCurSettings = {
TEXT("Plato WiFi power Manager"),
{
TEXT("Errors"), TEXT("Warnings"), TEXT("Verbose"), TEXT("Init"),
TEXT("Function"), TEXT("Undefined"), TEXT("Undefined"), TEXT("Undefined"),
TEXT("Undefined"), TEXT("Undefined"), TEXT("Undefined"), TEXT("Undefined"),
TEXT("Undefined"), TEXT("Undefined"), TEXT("Undefined"), TEXT("Undefined")
},
DBG_ERROR | DBG_WARNING | DBG_INIT | DBG_VERBOSE
};
#endif // SHIP_BUILD
CEDEVICE_POWER_STATE g_CurrentDx;
HMODULE g_hMod = NULL;
BOOL g_fInitialized;
HANDLE g_hNdisUio;
LPTSTR g_pszDeviceName;
LPTSTR g_pszIPAddress;
DWORD g_dwIpAddress;
BOOL g_HostWakeSet;
ULONG g_ulOrigPowerMode = Ndis802_11PowerModeMAX_PSP;
int SetPowerMode(HANDLE hNDisUIO, LPTSTR pszDeviceName, ULONG nPowerMode);
int SetDeepSleep(HANDLE hNDisUIO, LPTSTR pszDeviceName, BOOL fSleep);
BOOL PrepareForHostSleep(HANDLE hNdisUio, LPTSTR pszDeviceName, DWORD dwIPAddress);
BOOL RecoverFromHostSleep(HANDLE hNdisUio, LPTSTR pszDeviceName);
BOOL GetFirstWirelessNetworkCard(LPTSTR *ppszDeviceName);
DWORD CheckConnectionStatus(LPTSTR pszDeviceName, BOOL *pfConnected, LPTSTR* ppszIpAddress, DWORD* pdwIpAddress);
DWORD WaitForIPNotification(LPTSTR pszDeviceName, BOOL *pfConnected, LPTSTR* ppszIPAddress, DWORD* pdwIpAddress);
DWORD ConfigureWLANFromRegistry(LPTSTR pszWifiCard);
DWORD GetIpAddr(LPDWORD pdwIpAddr)
{
PIP_ADAPTER_ADDRESSES AdapterList = NULL;
TCHAR pszDeviceName[32];
DWORD i, retVal, cbOutSize = 0;
for (i = 0; i < 3; i ++)
{
retVal = GetAdaptersAddresses( AF_INET, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_SKIP_DNS_SERVER,
NULL, AdapterList, &cbOutSize);
if (retVal != ERROR_BUFFER_OVERFLOW)
break;
if (AdapterList != NULL)
free(AdapterList);
AdapterList = (PIP_ADAPTER_ADDRESSES) malloc(cbOutSize);
if (AdapterList == NULL)
{
retVal = GetLastError();
break;
}
}
if (retVal == NO_ERROR)
{
while (AdapterList)
{
mbstowcs_s(NULL, pszDeviceName, 32, AdapterList->AdapterName, _TRUNCATE);
if (_tcsncmp(g_pszDeviceName, pszDeviceName, 32) == 0)
{
if (AdapterList->FirstUnicastAddress) {
*pdwIpAddr = ((struct sockaddr_in *) AdapterList->FirstUnicastAddress->Address.lpSockaddr)->sin_addr.s_addr;
DEBUGMSG(ZONE_VERBOSE, (TEXT("Ethman: Found IP Address 0x%08X for device %s.\r\n"), *pdwIpAddr, pszDeviceName));
break;
}
}
AdapterList = AdapterList->Next;
}
DEBUGMSG(ZONE_ERROR, (TEXT("Ethman: Error - No IP Address Found!\r\n")));
}
else
{
DEBUGMSG(ZONE_ERROR, (TEXT("Ethman: Error retrieving IP Address code 0x%08X\r\n"), retVal));
}
if (AdapterList != NULL)
free(AdapterList);
return retVal;
}
int SetPowerMode(HANDLE hNDisUIO, LPTSTR pszDeviceName, ULONG nPowerMode)
{
BOOL fRetVal = TRUE;
PNDISUIO_SET_OID pSetOid = NULL;
UCHAR SetBuffer[sizeof(NDISUIO_SET_OID) + sizeof(NDIS_802_11_POWER_MODE)];
DWORD dwBytesReturned = 0;
pSetOid = (PNDISUIO_SET_OID) & SetBuffer[0];
pSetOid->ptcDeviceName = (LPTSTR) (LPCTSTR) pszDeviceName;
pSetOid->Oid = OID_802_11_POWER_MODE;
(*(NDIS_802_11_POWER_MODE *) (pSetOid->Data)) = (NDIS_802_11_POWER_MODE) nPowerMode;
fRetVal = DeviceIoControl(hNDisUIO, IOCTL_NDISUIO_SET_OID_VALUE,
(LPVOID) pSetOid, sizeof(NDISUIO_SET_OID) + sizeof(NDIS_802_11_POWER_MODE),
(LPVOID) pSetOid, sizeof(NDISUIO_SET_OID) + sizeof(NDIS_802_11_POWER_MODE),
&dwBytesReturned, NULL);
if (fRetVal == 0)
return STATUS_UNSUCCESSFUL;
return STATUS_SUCCESS;
}
int QueryPowerMode(HANDLE hNDisUIO, LPTSTR pszDeviceName, PULONG pulPowerMode)
{
BOOL fRetVal = TRUE;
PNDISUIO_QUERY_OID pQueryOid = NULL;
UCHAR SetBuffer[sizeof(NDISUIO_QUERY_OID) + sizeof(NDIS_802_11_POWER_MODE)];
DWORD dwBytesReturned = 0;
pQueryOid = (PNDISUIO_QUERY_OID) & SetBuffer[0];
pQueryOid->ptcDeviceName = (LPTSTR) (LPCTSTR) pszDeviceName;
pQueryOid->Oid = OID_802_11_POWER_MODE;
fRetVal = DeviceIoControl(hNDisUIO, IOCTL_NDISUIO_QUERY_OID_VALUE,
(LPVOID) pQueryOid, sizeof(NDISUIO_QUERY_OID) + sizeof(NDIS_802_11_POWER_MODE),
(LPVOID) pQueryOid, sizeof(NDISUIO_QUERY_OID) + sizeof(NDIS_802_11_POWER_MODE),
&dwBytesReturned, NULL);
*((NDIS_802_11_POWER_MODE *) pulPowerMode) = (*(NDIS_802_11_POWER_MODE *) (pQueryOid->Data));
if (fRetVal == 0)
return STATUS_UNSUCCESSFUL;
return STATUS_SUCCESS;
}
// VOIP call monitor thread
DWORD WINAPI CallMonThread(LPVOID param)
{
HANDLE m_hReadMsgQ = NULL;
MSGQUEUEOPTIONS msgOptions = {0};
ULONG ulPModeNew =0xFFFFFFFF;
if (NULL == g_hNdisUio)
return 0;
DEBUGMSG(ZONE_FUNCTION, (TEXT("EthMan - CallMonThread: Start\r\n")));
// Create a message queue for VOIP call notifications.
msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
msgOptions.dwFlags = 0;
msgOptions.dwMaxMessages = 10;
msgOptions.cbMaxMessage = sizeof(DWORD);
msgOptions.bReadAccess = TRUE;
m_hReadMsgQ = CreateMsgQueue(TEXT("Plato_VOIP_Call"), &msgOptions);
if ( m_hReadMsgQ == NULL )
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan - CallMonThread: CreateMsgQueue(Read): error %d\r\n"), GetLastError()));
goto Cleanup;
}
while(1)
{
int iBytesInQueue = 0;
DWORD dwFlags;
DWORD dwCallStatus = 0;
DWORD dwErr = 0;
// Read message from queue.
if ( ! ReadMsgQueue(m_hReadMsgQ, (LPVOID)&dwCallStatus, sizeof(DWORD), &iBytesInQueue, INFINITE, &dwFlags) )
{
dwErr = GetLastError();
if (dwErr == 233)
{
Sleep(500);
}
else
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan - CallMonThread: ReadMsgQueue: ERROR:%d\r\n"), dwErr));
}
}
else
{
if (dwCallStatus == 1)
{
ulPModeNew = Ndis802_11PowerModeCAM;
DEBUGMSG(ZONE_VERBOSE, (TEXT("EthMan - CallMonThread: VOIP call START notification received. Switching adapter to full power.\r\n")));
}
else
{
ulPModeNew = Ndis802_11PowerModeMAX_PSP;
DEBUGMSG(ZONE_VERBOSE, (TEXT("EthMan - CallMonThread: VOIP call STOP notification received. Switching adapter to power save mode.\r\n")));
}
if (STATUS_SUCCESS == SetPowerMode(g_hNdisUio, g_pszDeviceName, ulPModeNew))
{
DEBUGMSG(ZONE_VERBOSE, (TEXT("EthMan: New power mode successfully set\r\n")));
}
else
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan: Error setting new power mode\r\n")));
}
}
}
Cleanup:
if ( m_hReadMsgQ )
{
CloseMsgQueue(m_hReadMsgQ);
m_hReadMsgQ = NULL;
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("EthMan - CallMonThread: Exiting\r\n")));
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
DWORD WINAPI InitThread(LPVOID param)
{
HANDLE hCallMonThread;
LPTSTR pszDeviceName = NULL;
BOOL fConnected = FALSE;
LPTSTR pszIpAddress = NULL;
HANDLE hNdisUio = INVALID_HANDLE_VALUE;
DWORD dwRetVal = NO_ERROR;
// wait for windowmgr, net and comm apis
WaitForAPIReady(SH_WNET, INFINITE);
WaitForAPIReady(SH_COMM, INFINITE);
WaitForAPIReady(SH_WMGR, INFINITE);
DEBUGMSG(ZONE_FUNCTION, (TEXT("EthMan: InitThread - APIs Ready continuing init\r\n")));
if (!GetFirstWirelessNetworkCard(&pszDeviceName))
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan: Error -Could not find the wireless network card.\r\n")));
goto Error;
}
// Wait for a couple of seconds for connection to be established (if it is already configured)
Sleep(2000);
hNdisUio = CreateFile(TEXT("UIO1:"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ |FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hNdisUio == INVALID_HANDLE_VALUE)
{
DEBUGMSG(ZONE_ERROR,(L"EthMan:: NDISUIO CreateFile failed. Err = %d\r\n", GetLastError()));
goto Error;
}
dwRetVal = CheckConnectionStatus(pszDeviceName, &fConnected, &pszIpAddress, &g_dwIpAddress);
if (NO_ERROR != dwRetVal)
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan: Error -Could not check for connection status. Error = %d.\r\n"), dwRetVal));
goto Error;
}
if (!fConnected)
{
dwRetVal = ConfigureWLANFromRegistry(pszDeviceName);
if (ERROR_NOT_AUTHENTICATED == dwRetVal)
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan:Error - WLAN configuration in registry requires a user certificate, but none was found.\r\n")));
MessageBox(GetForegroundWindow(), TEXT("WLAN configuration in registry requires a user certificate, but none was found. Please cradle the device using ActiveSync and enroll for a certificate."), TEXT("WiFi Manager"), MB_OK);
goto Error;
}
else if (NO_ERROR != dwRetVal)
{
DEBUGMSG(ZONE_ERROR, (TEXT("EthMan: Error -Could not configure wifi connection from Registry. Error = %d.\r\n"), dwRetVal));
goto Error;
}
dwRetVal = WaitForIPNotification(pszDeviceName, &fConnected, &pszIpAddress, &g_dwIpAddress);
if (NO_ERROR != dwRetVal)
{
DEBUGMSG(ZONE_ERROR, (TEXT("Ethman:: WaitForIPNotification Failed. Err = %d.\r\n"), dwRetVal));
goto Error;
}
}
if (fConnected)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -