⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaconnect.cpp

📁 270的linux说明
💻 CPP
字号:
/*

Copyright (c) 2008, Intel Corporation. 

All rights reserved.

 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:


    * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.

    * Neither the name of Intel Corporation nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.

*///==============================================================================// mediaconnect.cpp//// Provide the media connecting status check////// //==============================================================================#define _WINSOCKAPI_ //This should keep windows.h from including winsock.h/*#include "stdafx.h"#include <windows.h>#include <winsock2.h>#include <ws2tcpip.h>#include <iphlpapi.h>#include <ntddndis.h>#include <wininet.h>*/#include <string>#include <vector>#include <netdb.h>using namespace std;#include "Instance_Connectivity.h"#include "mediaconnect.h"struct in_addr MCBIN_IPV4_ADDR_LOOPBACK;// = {127, 0, 0, 1};struct in6_addr MCBIN_IPV6_ADDR_LOOPBACK =   {0x0, 0x0,												0x0, 0x0,												0x0, 0x0,												0x0, 0x0,												0x0, 0x0,												0x0, 0x0,												0x0, 0x0,												0x0, 0x1};/* ______________________________________________________________     CMediaConnect	______________________________________________________________ */CMediaConnect::CMediaConnect(){/*///---------///---------The following code is for the implementation for get the network adapter//////---------information directly, not through the Layer Manager							   ///	///--------------------------------------------------------------------------------------------------- ///	//m_Head = 0;	//m_Tail = 0;*/	m_pNetworkAdapterInstanceCollection = NULL;	m_pLayerManger = GetLayer();//initialize the Layer manager	m_bInitialized = false;}/* ______________________________________________________________     ~CMediaConnect	______________________________________________________________ */CMediaConnect::~CMediaConnect(){	m_bInitialized = false;}int CMediaConnect::IsNetPresent(bool *pNetPresent){	BOOL bFoundLocalAddr = FALSE;	char szAddrASCII[MAX_LOCAL_NAME_LEN];		struct addrinfo AddrHints, *pAI, *pAddrInfo;		if (!pNetPresent)		return MEDIACONNECT_FAIL;			if(gethostname(szAddrASCII, MAX_LOCAL_NAME_LEN - 1))	{		      //printf(IntelMobileTEXT("Error getting local host name, error = %d"), WSAGetLastError());		*pNetPresent = false;		return MEDIACONNECT_SUCCESS;	}		memset(&AddrHints, 0, sizeof(AddrHints));	AddrHints.ai_family = PF_UNSPEC;	AddrHints.ai_flags = AI_PASSIVE;		if(getaddrinfo(szAddrASCII, "10", &AddrHints, &pAddrInfo))	{		     // printf(IntelMobileTEXT("getaddrinfo(%hs) error %d"), szAddrASCII, WSAGetLastError());		*pNetPresent = false;		return MEDIACONNECT_SUCCESS;	}			bFoundLocalAddr = TRUE;	for(pAI = pAddrInfo; pAI != NULL && bFoundLocalAddr; pAI = pAI->ai_next)	{		if(pAI->ai_family == PF_INET)		{			if(memcmp(&(((struct sockaddr_in *)(pAI->ai_addr))->sin_addr), &MCBIN_IPV4_ADDR_LOOPBACK, sizeof(MCBIN_IPV4_ADDR_LOOPBACK)) == 0)				bFoundLocalAddr = FALSE;		}		else if(pAI->ai_family == PF_INET6)		{			if(memcmp(&(((struct sockaddr_in6 *)(pAI->ai_addr))->sin6_addr), &MCBIN_IPV6_ADDR_LOOPBACK, sizeof(MCBIN_IPV6_ADDR_LOOPBACK)) == 0)				bFoundLocalAddr = FALSE;  		}	}		freeaddrinfo(pAddrInfo);	if (bFoundLocalAddr)		*pNetPresent = true;	else		*pNetPresent = false;		return MEDIACONNECT_SUCCESS;}/* ______________________________________________________________     CMediaConnect::Initialize	______________________________________________________________ */int CMediaConnect::Initialize(){/*	///---------///---------The following code is for the implementation for get the network adapter//////---------information directly, not through the Layer Manager							   ///	///--------------------------------------------------------------------------------------------------- ///	PIP_ADAPTER_INFO pAdapterInfo = NULL;	PIP_ADAPTER_INFO pTmpAdapterInfo = NULL;	unsigned long OutBufLen = 0;	vector<ADAPTOR_TYPE> *pNewAdaptor = NULL;	ADAPTOR_TYPE *pAdaptor = NULL;	GetAdaptersInfo(pAdapterInfo, &OutBufLen);	pAdapterInfo = (PIP_ADAPTER_INFO) new char[OutBufLen];	pNewAdaptor = new vector<ADAPTOR_TYPE>;	m_pAdaptor[m_Head] = pNewAdaptor;	m_Tail = (m_Tail + 1) % MAX_Q;	if (pAdapterInfo)	{		if (GetAdaptersInfo(pAdapterInfo, &OutBufLen) == ERROR_SUCCESS)		{			pTmpAdapterInfo = pAdapterInfo;			pAdaptor = new ADAPTOR_TYPE;			while (pTmpAdapterInfo != NULL)			{				if (pAdaptor)				{					pAdaptor->Name = pTmpAdapterInfo->AdapterName;					m_pAdaptor[m_Head]->push_back(*pAdaptor);				pTmpAdapterInfo = pTmpAdapterInfo->Next;				}			}			m_bInitialized = TRUE;			delete pAdaptor;		}		delete pAdapterInfo;	}*/	try {		long iChangedIndex = 0; // to act like client		int iEnumIndex = 0;				ServerInstanceCollection* pNetworkAdapterInstanceCollection = NULL;		if (pNetworkAdapterInstanceCollection = ((ServerNetworkAdapterClassObject&)(m_pLayerManger->GetLayerClassObject(IntelMobileText("NetworkAdapter")))).GetInstances()) //Get the Network adapter collection in the system		{			pNetworkAdapterInstanceCollection->Reset(&iChangedIndex);			bool bHasNext;			if ( bHasNext = pNetworkAdapterInstanceCollection->HasNext(iEnumIndex, iChangedIndex )) //There is one Network adapter at least in the network adapter collection  			{				m_pNetworkAdapterInstanceCollection =pNetworkAdapterInstanceCollection;				m_bInitialized = TRUE;				pNetworkAdapterInstanceCollection = NULL; // delete the pointer and release the pointer memeory 				return MEDIACONNECT_SUCCESS; // rrw release tons of memory			}			else				goto GracefulExit;		}		else			goto GracefulExit;	GracefulExit:		//delete pNetworkAdapterInstanceCollection;		pNetworkAdapterInstanceCollection = NULL; // delete the pointer and release the pointer memeory 		return MEDIACONNECT_FAIL;	}	catch(ServerException se) {		if (se.IfServerBaseObjectException()) 		{			ServerException nse(IntelMobileText("80043217"));			nse.SetParent( &se );			throw nse;		}		throw se;	}	catch(...) {	//	THROWIMEXCEPTION(80043217);	}	m_bInitialized = TRUE;}/* ____________________________________________________________________________   CMediaConnect::IsAnyAdapterConnected   ____________________________________________________________________________ */int CMediaConnect::IsAnyAdapterConnected(bool *pAnyAdaptorConnected, unsigned long &ConnSpeed){/*	///---------///---------The following code is for the implementation for get the network adapter//////---------information directly, not through the Layer Manager							   ///	///--------------------------------------------------------------------------------------------------- ///	int i = 0;	bool bConnected = FALSE;	HANDLE hNIC = NULL;	unsigned long uOidCode = 0;	unsigned long uConnState = 0;	unsigned long ReqBuffer = 0;	unsigned long Speed;	string Adapter;	ConnSpeed = 0;    Speed = 0;	for (i = 0; i < (int ) m_pAdaptor[m_Head]->size(); i++)	{		Adapter = ADA_NAME_PREFIX;		Adapter += m_pAdaptor[m_Head]->at(i).Name.c_str();		if ((hNIC = CreateFile(Adapter.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 								NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)		{			uOidCode = OID_GEN_MEDIA_CONNECT_STATUS;			if (DeviceIoControl(hNIC, IOCTL_NDIS_QUERY_GLOBAL_STATS, &uOidCode, sizeof(ULONG), &uConnState, sizeof(ULONG), 								&ReqBuffer, NULL) != 0)			{				if (uConnState == NdisMediaStateConnected)					bConnected = TRUE;			}			if (bConnected)			{				//The OID_GEN_LINK_SPEED OID specifies the maximum speed of the NIC in kbps. 				//The unit of measurement is 100 bps, so a value of 100,000 represents a hardware bit rate of 10 Mbps.				uOidCode = OID_GEN_LINK_SPEED;				DeviceIoControl(hNIC, IOCTL_NDIS_QUERY_GLOBAL_STATS, &uOidCode, sizeof(ULONG), &Speed,								sizeof(unsigned long), &ReqBuffer, NULL);			}			CloseHandle(hNIC);		}		if (bConnected)			break;	}	ConnSpeed = Speed;   *pAnyAdaptorConnected = bConnected;*/	try {		ServerNetworkAdapterInstanceObject *pNetworkAdapterInstance = NULL;		ServerLinkProtocolInstanceObject	*pLinkProtocolInstance = NULL; 		ServerInstanceCollection* pLinkProtocolInstanceCollection = NULL;		long iChangedIndex = 0; // to act like client		int iEnumIndex = 0;		bool bHasNext;		m_pNetworkAdapterInstanceCollection->Reset(&iChangedIndex);		if ( bHasNext = m_pNetworkAdapterInstanceCollection->HasNext(iEnumIndex, iChangedIndex ))			{				// If there are some network adapters (>=1) in the NetworkAdapter Collection, go to the loop				while ( bHasNext )				{					if ( !m_pNetworkAdapterInstanceCollection->Next( (ServerInstanceObject**)&pNetworkAdapterInstance, &iEnumIndex, iChangedIndex ) )						break;// if there is not a network adapter in the NetworkAdapter Collection, the loop is over 					else					{														IntelMobileString sProtocolKey = pNetworkAdapterInstance->GetKey();						if (pLinkProtocolInstance= (ServerLinkProtocolInstanceObject*)((ServerLinkProtocolClassObject&)(m_pLayerManger->GetLayerClassObject(IntelMobileText("LinkProtocol")))).GetInstance(sProtocolKey))//so far the key of the link protocol in a network adapter is as same as the key of the network adapter,the relationship between network adapter and link protocol is 1:1						{							//Get the link protocl on this network adapter							if (pLinkProtocolInstance->MediaPresent.GetValue())							{														// check the link protocol's property: MediaPresent									*pAnyAdaptorConnected = true;								ConnSpeed = (int)pLinkProtocolInstance->DataRateTx.GetValue();								return MEDIACONNECT_SUCCESS;							}      							else							{								// if the MediaPreset for this link protocol on this network adapter is FALSE, the loop should go on								if (!(bHasNext = m_pNetworkAdapterInstanceCollection->HasNext( iEnumIndex, iChangedIndex )))									break;							}						}						else						{							// if there is not protocol set on the network adapter, the loop should go on to check the next network adapter							if (!(bHasNext = m_pNetworkAdapterInstanceCollection->HasNext( iEnumIndex, iChangedIndex )))								break;						}										}				}			}		*pAnyAdaptorConnected = false;		ConnSpeed = 0;		return MEDIACONNECT_FAIL;	}	catch(ServerException se) {		if (se.IfServerBaseObjectException()) 		{			ServerException nse(IntelMobileText("80043218"));			nse.SetParent( &se );			throw nse;		}		throw se;	}	catch(...) {	//	THROWIMEXCEPTION(80043218);	}	*pAnyAdaptorConnected = true;	ConnSpeed = 1000000;}/* ____________________________________________________________________________   CMediaConnect::IsAnyModemConnected   ____________________________________________________________________________ */int CMediaConnect::IsAnyModemConnected(bool *pConnected, unsigned long &ConnSpeed){	// using the Wininet API to check whether or not there is the connection via modem	try {		bool bConnected = false;		unsigned long ConnType = 0;				ConnSpeed = MODEM_SPEED;/*		if (InternetGetConnectedState(&ConnType, 0))		{			if (ConnType & INTERNET_CONNECTION_MODEM)				bConnected = true;		}*/		*pConnected = bConnected;		return MEDIACONNECT_SUCCESS;	}	catch(...) {//		THROWIMEXCEPTION(80043219);	}}/* ____________________________________________________________________________   CMediaConnect::IsNetOn   ____________________________________________________________________________ */int CMediaConnect::IsNetOn(bool *pIsNetOn){	CMediaConnect cConnect;		bool IsNetOn;	int iRet;		iRet = MEDIACONNECT_FAIL;	IsNetOn = false;//nhu comment out these lines/*#ifndef ARMV4	unsigned long Speed;	if (!cConnect.Initialize())	{		iRet = cConnect.IsAnyAdapterConnected(&IsNetOn, Speed);		if (iRet == -1)			goto GracefulExit;				if (!IsNetOn)				{					iRet = cConnect.IsAnyModemConnected(&IsNetOn, Speed);					if (iRet)						goto GracefulExit;				}			}	GracefulExit:#endif*/	iRet = cConnect.IsNetPresent(&IsNetOn);   	*pIsNetOn = IsNetOn;	return iRet;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -