📄 edbgsamp.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
edbgsamp.c
Abstract:
Sample program illustrating debug Ethernet (EDBG) API. This program can be
compiled for CE (UNDER_CE defines) and NT. It implements a simple echo client
server application - Usage (e.g. to run server on device):
On device: edbgsamp -s
On desktop: edbgsamp <DeviceName>
Notes:
--*/
#include <windows.h>
#include <ethdbg.h>
#ifndef UNDER_CE
#include <stdio.h>
#include <tchar.h>
#define RETAILMSG(exp, p) ((exp) ? _tprintf p, 1 : 0)
#endif
// This is the name of the service associated with the EDBGSAMP program.
#define EDBGSAMP_SVCNAME "EDBGSAMP"
DWORD ServerProc();
DWORD ClientProc();
BOOL fLoopbackMode = TRUE;
char szDeviceName[EDBG_MAX_DEV_NAMELEN + 1] = {0};
static void
Usage()
{
#ifdef UNDER_CE
RETAILMSG(1,(TEXT("Usage: edbgsamp [-s]\n")
TEXT("\tOptions are:\n")
TEXT("\t-s Server - (default == client)\n")));
#else
RETAILMSG(1,(TEXT("Usage: edbgsamp [-s] <Device name>\n")
TEXT("\tOptions are:\n")
TEXT("\t-s Server - (default == client)\n")));
#endif
}
void
#ifdef UNDER_CE
main(HINSTANCE hInst, HINSTANCE hInstPrev, LPWSTR pCmdLine,
int nCmdShow)
#else
main()
#endif
{
BOOL fServerMode = FALSE;
DWORD DebugZoneMask = ZONE_INIT|ZONE_WARNING;
#ifndef UNDER_CE
TCHAR *pCmdLine = GetCommandLine();
#else
#endif
// Skip to first space
while (*pCmdLine && (*pCmdLine != TEXT(' ')))
pCmdLine++;
// Parse cmd line args
while (*pCmdLine) {
while (*pCmdLine == TEXT(' '))
pCmdLine++;
switch (*pCmdLine) {
case TEXT('-'):
case TEXT('/'):
++pCmdLine;
if ((*pCmdLine == TEXT('s')) || (*pCmdLine == TEXT('S'))) {
fServerMode = TRUE;
++pCmdLine;
}
else {
Usage();
return;
}
break;
default:
// Treat as a device name, must be last option
#ifdef UNICODE
pCmdLine += wcstombs(szDeviceName,pCmdLine,sizeof(szDeviceName));
#else
strcpy(szDeviceName,pCmdLine);
pCmdLine += strlen(szDeviceName);
#endif
break;
}
}
#ifndef UNDER_CE
if (!szDeviceName[0]) {
RETAILMSG(1,(TEXT("Must specify a device name\n")));
Usage();
return;
}
// Set output debug string function for EDBG.DLL library functions
EdbgSetDebug(DebugZoneMask, printf);
#else
if (szDeviceName[0]) {
Usage();
return;
}
// On the device, EDBG debug output goes to the serial port. Can get very
// verbose if you're running kernel services over debug Ethernet...
EdbgSetDebug(DebugZoneMask);
#endif
if (fServerMode)
ServerProc();
else
ClientProc();
}
// Register with debug ethernet services
BOOL
RegisterAndConnect(UCHAR *pId)
{
#ifdef UNDER_CE
UCHAR *pBufferPool;
// For CE, need to provide buffer pool memory
pBufferPool = LocalAlloc(LMEM_FIXED,EDBG_DFLT_BUFFER_POOL_SIZE);
if (!pBufferPool) {
RETAILMSG(1,(TEXT("Error allocating buffer pool memory\n")));
return FALSE;
}
// This call will block until the peer application is started on the desktop
if (!EdbgRegisterClient(pId, EDBGSAMP_SVCNAME, 0, EDBG_WINDOW_SIZE, pBufferPool)) {
RETAILMSG(1,(TEXT("Error registering client %a\n"),EDBGSAMP_SVCNAME));
return FALSE;
}
#else
// This call will block until the peer application is started on the device
if (!EdbgRegisterClient(pId, EDBGSAMP_SVCNAME, EDBG_CFGFL_MULTIINST, NULL, szDeviceName)) {
RETAILMSG(1,(TEXT("Error registering client %s connecting to device %s\n"),EDBGSAMP_SVCNAME,szDeviceName));
return FALSE;
}
#endif
return TRUE;
}
DWORD
ServerProc()
{
UCHAR Id;
DWORD dwLen,dwRet = 1;
char *TxBuf,*RxBuf;
TxBuf = LocalAlloc(0,EDBG_MAX_DATA_SIZE);
RxBuf = LocalAlloc(0,EDBG_MAX_DATA_SIZE);
if (!TxBuf || !RxBuf) {
RETAILMSG(1,(TEXT("EDBGSAMP: memory allocation failure\n")));
return 1;
}
RETAILMSG(1,(TEXT("EDBGSAMP: Server thread started...\n")));
if (!RegisterAndConnect(&Id)) {
LocalFree(TxBuf);
LocalFree(RxBuf);
return 1;
}
// Wait for messages, echo them back
while (1) {
dwLen = EDBG_MAX_DATA_SIZE;
if (!EdbgRecv(Id, RxBuf,&dwLen , INFINITE)) {
RETAILMSG(1,(TEXT("EDBGSAMP: Error in EdbgRecv\n")));
goto DeregAndExit;
}
RETAILMSG(1,(TEXT("EDBGSAMP: received %u bytes\n"), dwLen));
if (fLoopbackMode) {
if (!EdbgSend(Id, RxBuf, dwLen)) {
RETAILMSG(1,(TEXT("EDBGSAMP: Error in EdbgSend\n")));
goto DeregAndExit;
}
}
}
dwRet = 0;
DeregAndExit:
RETAILMSG(1,(TEXT("EDBGSAMP server exiting\n")));
EdbgDeregisterClient(Id);
LocalFree(TxBuf);
LocalFree(RxBuf);
return dwRet;
}
DWORD
ClientProc()
{
UCHAR Id;
DWORD dwLen, dwRxLen, dwRet=1;
char *TxBuf,*RxBuf;
TxBuf = LocalAlloc(0,EDBG_MAX_DATA_SIZE);
RxBuf = LocalAlloc(0,EDBG_MAX_DATA_SIZE);
if (!TxBuf || !RxBuf) {
RETAILMSG(1,(TEXT("EDBGSAMP: memory allocation failure\n")));
return 1;
}
RETAILMSG(1,(TEXT("EDBGSAMP Client started...\n")));
if (!RegisterAndConnect(&Id)) {
LocalFree(TxBuf);
LocalFree(RxBuf);
return 1;
}
for (dwLen =1; dwLen <= EDBG_MAX_DATA_SIZE; dwLen++) {
memset(TxBuf, (UCHAR)dwLen, dwLen);
if (!EdbgSend(Id, TxBuf, dwLen)) {
RETAILMSG(1,(TEXT("EDBGSAMP: Error in EdbgSend, len: %u\n"),dwLen));
goto DeregAndExit;
}
if (fLoopbackMode) {
// Get response
dwRxLen = EDBG_MAX_DATA_SIZE;
if (!EdbgRecv(Id, RxBuf,&dwRxLen , INFINITE)) {
RETAILMSG(1,(TEXT("EDBGSAMP: Error in EdbgRecv, len: %u\n"),dwLen));
goto DeregAndExit;
}
if (dwRxLen != dwLen) {
RETAILMSG(1,(TEXT("EDBGSAMP: Size mismatch, expecting %u, got %u\n"),dwLen,dwRxLen));
goto DeregAndExit;
}
else if (memcmp(RxBuf,TxBuf,dwLen)) {
RETAILMSG(1,(TEXT("EDBGSAMP: Error: Data mismatch, len: %u\n"),dwLen));
goto DeregAndExit;
}
}
}
RETAILMSG(1,(TEXT("EDBGSAMP: Test completed successfully\n")));
dwRet = 0;
DeregAndExit:
EdbgDeregisterClient(Id);
LocalFree(TxBuf);
LocalFree(RxBuf);
return dwRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -