📄 deviceenum.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/**************************************************************************/
/* DeviceEnum.cpp */
/* */
/* This file contains routines to enumerate through devices */
/* */
/* Functions included: */
/* */
/* Other related files: */
/* */
/* */
/**************************************************************************/
#include "common.h"
#include "obexdevice.h"
#include "cobex.h"
#include "DeviceEnum.h"
#include "PropertyBagEnum.h"
#include "ObexStrings.h"
#include "ObexBTHTransport.h"
/*****************************************************************************/
/* CDeviceEnum */
/* construct a device enumerator */
/*****************************************************************************/
CDeviceEnum::CDeviceEnum() :
_refCount(1),
pListHead(0),
pListCurrent(0)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::CDeviceEnum\n"));
}
/*****************************************************************************/
/* CDeviceEnum */
/* destruct a device enumerator by looping through cache calling release */
/*****************************************************************************/
CDeviceEnum::~CDeviceEnum()
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::~CDeviceEnum\n"));
SVSUTIL_ASSERT(_refCount == 0);
DEVICE_NODE *pTemp = pListHead;
while(pTemp)
{
DEVICE_NODE *pDel = pTemp;
pDel->pItem->Release();
pTemp = pTemp->pNext;
delete pDel;
}
}
/*****************************************************************************/
/* CDeviceEnum::Next */
/* return 'celt' items (each return device must be Release() */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::Next (ULONG celt, LPDEVICE *rgelt, ULONG *pceltFetched)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::Next()\n"));
if ((celt == 0) || ((celt > 1) && (pceltFetched == NULL)))
return S_FALSE;
if (pceltFetched)
*pceltFetched = 0;
memset(rgelt, 0, sizeof(LPDEVICE)*celt);
for(ULONG i=0; pListCurrent && (i<celt); i++)
{
rgelt[i] = pListCurrent->pItem;
rgelt[i]->AddRef ();
pListCurrent = pListCurrent->pNext;
}
if (pceltFetched)
*pceltFetched = i;
return (i == celt) ? S_OK : S_FALSE;
}
/*****************************************************************************/
/* CDeviceEnum::Skip */
/* Skip 'celt' items in the list */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::Skip(ULONG celt)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::Skip()\n"));
if(celt == 0)
return S_FALSE;
for(ULONG i=0; pListCurrent && (i<celt); i++)
pListCurrent = pListCurrent->pNext;
return i == celt ? S_OK : S_FALSE ;
}
/*****************************************************************************/
/* CDeviceEnum::Reset */
/* reset the internal iterator to the beginning */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::Reset()
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::Reset()\n"));
pListCurrent = pListHead;
return S_OK;
}
/*****************************************************************************/
/* CDeviceEnum::Clone */
/* clone the enumerator */
/* */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::Clone(IDeviceEnum **ppenum)
{
return E_NOTIMPL;
}
/*****************************************************************************/
/* CDeviceEnum::Insert */
/* insert a device into the enumerator */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::Insert(CObexDevice *pDevice)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::Insert()\n"));
PREFAST_ASSERT(pDevice);
SVSUTIL_ASSERT(gpSynch->IsLocked());
DEVICE_NODE *pNode = new DEVICE_NODE();
if (! pNode)
return E_OUTOFMEMORY;
//addref it only if we are going to insert it into the list
pDevice->AddRef ();
pNode->pItem = pDevice;
pNode->pNext = pListHead;
pListHead = pNode;
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::Insert-Added new device"));
return S_OK;
}
/*****************************************************************************/
/* CDeviceEnum::FindDevice */
/* check to see if the given device (pBag) is already in our list of */
/* devices. if not, return an S_OK and set *pDeviceList=NULL... */
/* otherwise, if it IS in our list, then AddRef() it and put it on */
/* pDeviceList */
/*****************************************************************************/
HRESULT CDeviceEnum::FindDevicesThatMatch(LPPROPERTYBAG2 pBag, DEVICE_LIST **pDeviceList)
{
PREFAST_ASSERT(pDeviceList);
*pDeviceList = NULL;
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::FindDevice()\n"));
VARIANT varTransport;
VARIANT varAddress;
VARIANT varName;
VariantInit(&varTransport);
VariantInit(&varAddress);
VariantInit(&varName);
BOOL fIsIRDA = FALSE;
if(!pBag)
return E_FAIL;
//get the device address from the passed in property bag
HRESULT hr = pBag->Read(c_szDevicePropAddress, &varAddress, NULL);
if (FAILED(hr))
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::FindDevice() : ERROR - no address in the bag?\n"));
return hr;
}
hr = pBag->Read(c_szDevicePropTransport, &varTransport, NULL);
if(FAILED(hr))
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::FindDevice() : ERROR - no transport in the bag?\n"));
VariantClear(&varAddress);
return hr;
}
//
// if the address is an int, we are dealing with IRDA
// since on IRDA the address doesnt always mean something (Palm)
// we will use the name instead
//
if(varAddress.vt == VT_I4)
{
fIsIRDA = TRUE;
hr = pBag->Read(c_szDevicePropName, &varName, NULL);
if (FAILED(hr))
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::FindDevice() : this is irda so we have to use name instead of address\n"));
VariantClear(&varAddress);
VariantClear(&varTransport);
return hr;
}
}
#if defined (DEBUG) || defined (_DEBUG)
DWORD dwNAP=0, dwSAP=0;
if (varAddress.vt == VT_I4)
{
dwSAP = varAddress.lVal;
dwNAP = 0;
}
else
{
BT_ADDR ba;
// move in the device id for the selected device
SVSUTIL_ASSERT(CObexBTHTransport::GetBA(varAddress.bstrVal, &ba));
dwNAP = GET_NAP(ba);
dwSAP = GET_SAP(ba);
}
#endif
SVSUTIL_ASSERT(gpSynch->IsLocked());
HRESULT generated_response = S_FALSE;
//loop through all previously discovered devices
DEVICE_NODE *pNode = pListHead;
DEVICE_LIST *pRetList = NULL;
while(pNode)
{
VARIANT var2;
VariantInit(&var2);
BOOL bEqual = FALSE;
//check transport
if(SUCCEEDED(pNode->pItem->GetDeviceTransport(&var2)))
{
if(0 == wcscmp(var2.bstrVal, varTransport.bstrVal))
bEqual = TRUE;
VariantClear(&var2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -