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

📄 btdriverstreaminit.cpp

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		EnterCriticalSection (&g_CS);
		if ((DWORD)g_pCC != dwClientContext)
		{
			LeaveCriticalSection (&g_CS);
			return FALSE;
		}

		g_pCC->fDriverStarted = FALSE;
		if (g_pCC->iRefCnt == 0)
			break;

		LeaveCriticalSection (&g_CS);
		Sleep (200);
	}

	PWSSBTCLIENT_CONTEXT pcc = g_pCC;
	g_pCC = NULL;

	LeaveCriticalSection (&g_CS);

	
	CloseBTObject(pcc);

	return TRUE;
}

///////////////////////////////////////////////////////////////////
// WBT_Open: An application (the stack actually) wants to start using us
// Initialize the CF card
///////////////////////////////////////////////////////////////////
static DWORD AbortOpen (PWSSBTCLIENT_CONTEXT pcc)	// g_CS TAKEN!
{
	if (pcc == g_pCC)
	{
		pcc->fOpening = FALSE;
		--pcc->iRefCnt;
	}

	LeaveCriticalSection (&g_CS);

	WBT_Close ((DWORD)pcc);

	return 0;
}

DWORD WBT_Open(DWORD dwClientContext, DWORD dwAccess, DWORD dwShareMode)
{
	DEBUGMSG(ZONE_INIT, (L"WceStreamBT: WBT_Open\r\n"));
	
	// Use client context as open context.
	PWSSBTCLIENT_CONTEXT pcc = (PWSSBTCLIENT_CONTEXT)dwClientContext;

	EnterCriticalSection (&g_CS);
	if ((pcc != g_pCC) || pcc->fDriverStarted || pcc->fOpening)
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: Driver already open or not initialized\r\n"));
		LeaveCriticalSection (&g_CS);
		return FALSE;
	}

	pcc->fOpening = TRUE;
	++pcc->iRefCnt;

	pcc->hBTReg = NULL;
	pcc->ioBase = NULL;

	CARD_SOCKET_HANDLE hSocket = pcc->hSocket;
	CARD_CLIENT_HANDLE hCardClient = pcc->hCardClient;

	LeaveCriticalSection (&g_CS);

	//
    // Request a Pcmcia IO window
    //
	CARD_WINDOW_PARMS	Cwp;

	memset (&Cwp, 0, sizeof(Cwp));

    Cwp.hSocket = hSocket;
    Cwp.fAttributes = WIN_ATTR_IO_SPACE | WIN_ATTR_ENABLED;
    Cwp.uWindowSize = BT_REG_LENGTH;
    Cwp.fAccessSpeed = 0x08;
    
	CARD_WINDOW_HANDLE hCardWindow = v_pfnCardRequestWindow(hCardClient, &Cwp);
	if (hCardWindow == NULL) 
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardRequestWindow failed, error=%d\r\n", GetLastError ()));
    }
	else
	{
		DEBUGMSG(ZONE_INIT, (L"WceStreamBT: card window handle=0x%08x\r\n", hCardWindow));
	}

    //
    // Map the Pcmcia IO window
    //
    unsigned char *pPort = hCardWindow ? (unsigned char *)v_pfnCardMapWindow(hCardWindow, gCONFIG_BASE_ADDRESS, BT_REG_LENGTH, NULL) : NULL;
    if (pPort == NULL )
	{
		if (hCardWindow)
		{
			DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardMapWindow failed, error=%d\r\n", GetLastError ()));
		}
    }
	else
	{
		DEBUGMSG(ZONE_INIT, (L"WceStreamBT: base port=0x%08x\r\n", pPort));
	}

	//
    // Use CardRequestConfiguration to place card in I/O mode
    //
	if (pPort)
	{
		CARD_CONFIG_INFO	CfgInfo;

		memset (&CfgInfo, 0, sizeof(CfgInfo));

	    CfgInfo.hSocket = hSocket;
		CfgInfo.fAttributes = CFG_ATTR_IRQ_STEERING;
	    CfgInfo.fInterfaceType = CFG_IFACE_MEMORY_IO;
		CfgInfo.uVcc = 50;
	    CfgInfo.uVpp1 = 0;
		CfgInfo.uVpp2 = 0;
	    CfgInfo.fRegisters = CFG_REGISTER_CONFIG; 
		CfgInfo.uConfigReg = 0x20 ;//| 0x80; //Card enable and soft reset
	    CfgInfo.uStatusReg = 0;
		CfgInfo.uExtendedStatus = 0;

	    STATUS status = v_pfnCardRequestConfiguration(hCardClient, &CfgInfo);

		if (status != CERR_SUCCESS) 
		{
			DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardRequestConfiguration failed, status=0x%08x\r\n", status));
			pPort = NULL;
		}
	}

	//
    // Request the PCMCIA interrupt
    //
	int fReleaseConfig = FALSE;

	if (pPort)
	{
		EnterCriticalSection (&g_CS);
		if (g_pCC == pcc)
		{
			pcc->ioBase = pPort;
			DisableInterrupts(pcc);//Disable our interrupts 
		}
		LeaveCriticalSection (&g_CS);

		DWORD status = v_pfnCardRequestIRQ(
                hCardClient,
                hSocket,
                (CARD_ISR)InterruptServiceRoutine,
                (DWORD)pcc);
		if (status != CERR_SUCCESS)
		{
			DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardRequestIRQ failed, status=0x%08x\r\n", status));
		}
		else
		{
			fReleaseConfig = TRUE;
		}
	}

	EnterCriticalSection (&g_CS);
	if (g_pCC != pcc)
	{
		LeaveCriticalSection (&g_CS);
		return 0;
	}

	pcc->hBTReg = hCardWindow;
	pcc->ioBase = pPort;

	if (! fReleaseConfig)
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: aborting the open\r\n"));

		return AbortOpen (pcc);
	}

	ASSERT (hCardWindow);
	ASSERT (pPort);

	pcc->fReleaseConfig = TRUE;
	pcc->receiveBuffer->Flush();
	pcc->sendBuffer->Flush();

	DWORD status = BTWSSInitCard(pcc);

	if(status == CERR_SUCCESS)
	{
		pcc->fDriverStarted = TRUE;

		DEBUGMSG(ZONE_INIT, (L"Interrupts are enabled\r\n"));
		EnableInterrupts(pcc);
		RxInit(pcc);
	}
	else
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: BTWSSInitCard failed, status=0x%08x\r\n", status));
	}

	LeaveCriticalSection (&g_CS);

	Sleep (250);

	EnterCriticalSection (&g_CS);

	if ((pcc == g_pCC) && pcc->fDriverStarted)
	{
		if (! BTWSSResetCard (pcc))
		{
			return AbortOpen (pcc);
		}
	}
	else
	{
		return AbortOpen (pcc);
	}

	LeaveCriticalSection (&g_CS);

	Sleep (250);

	EnterCriticalSection (&g_CS);

	if ((pcc == g_pCC) && pcc->fDriverStarted)
	{
		if (! BTWSSResetCard (pcc))
		{
			return AbortOpen (pcc);
		}
	}
	else
	{
		return AbortOpen (pcc);
	}

	BTWSSLedStart ((DWORD)pPort);

	LeaveCriticalSection (&g_CS);

	Sleep(500);

	//Now software-reset the card.
	unsigned char HCC_RESET[] = {0x01, 0x03, 0x0c,  0x00};
	if (dwClientContext &&
		(0xffffffff == WBT_Write (dwClientContext, HCC_RESET, sizeof(HCC_RESET))))
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: HCC_RESET failed\r\n"));
		dwClientContext = 0;
	}

	if (dwClientContext &&
		(0xffffffff == WBT_Write (dwClientContext, HCC_RESET, sizeof(HCC_RESET))))
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: HCC_RESET failed\r\n"));
		dwClientContext = 0;
	}

	if (dwClientContext)
	{
		Sleep (500);	// Write for response and discard it.
	}

	EnterCriticalSection (&g_CS);
	if ((pcc != g_pCC) || (! pcc->fDriverStarted) || (! dwClientContext))
	{
		return AbortOpen (pcc);
	}


	pcc->sendBuffer->Flush ();
	pcc->receiveBuffer->Flush ();

	pcc->fOpening = FALSE;
	--pcc->iRefCnt;

	LeaveCriticalSection (&g_CS);

	return dwClientContext;
}

///////////////////////////////////////////////////////////
// WBT_Close: The application (the stack actually) wants to stop using
// us.
// Disable interrupts (shutdown the card?) and unmap the PCMCIA window
////////////////////////////////////////////////////////////

BOOL WBT_Close(DWORD dwOpenContext)
{
	DEBUGMSG(ZONE_INIT, (L"WceStreamBT: WBT_Close\r\n"));

	BTWSSLedStop ();

	PWSSBTCLIENT_CONTEXT  extension = (PWSSBTCLIENT_CONTEXT)dwOpenContext;

	EnterCriticalSection (&g_CS);
	if ((extension != g_pCC) || (! extension->fDriverStarted))
	{
		DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: Driver not open or not initialized\r\n"));
		LeaveCriticalSection (&g_CS);
		return FALSE;
	}

	extension->fDriverStarted = FALSE;

	DisableInterrupts(extension);

	SetEvent (extension->hReadEvent);
	SetEvent (extension->hWriteEvent);
	Sleep (250);

	if (extension->fReleaseConfig)
	{
		STATUS status = v_pfnCardReleaseConfiguration(extension->hCardClient,extension->hSocket);
		if(status != CERR_SUCCESS )
		{
			DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardReleaseConfiguration failed, status=0x%08x\r\n", status));
		}
	}

	if (extension->hBTReg)
	{
		STATUS status = v_pfnCardReleaseWindow(extension->hBTReg);
		if(status != CERR_SUCCESS )
		{
			DEBUGMSG(ZONE_ERROR, (L"WceStreamBT: CardReleaseWindow failed, status=0x%08x\r\n", status));
		}
	}

	LeaveCriticalSection (&g_CS);

	return TRUE;
}


//------------------------------------------------------------------------------

DWORD WBT_Seek(DWORD dwOpenContext, long lDistance, DWORD dwMoveMethod)
{
	return 0;
}

//------------------------------------------------------------------------------

void WBT_PowerUp(void)
{
}

//------------------------------------------------------------------------------

void WBT_PowerDown(void)
{
}

//------------------------------------------------------------------------------

⌨️ 快捷键说明

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