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

📄 bthnscreate.cxx

📁 SDP Search and Record generator OVERVIEW: Recognizing the difficulty in creating an SDP service
💻 CXX
📖 第 1 页 / 共 3 页
字号:
//
// 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.


Abstract:
    Windows CE Bluetooth application sample
    Demonstrates creating an SDP record binary value or setting up an
    SDP search.
**/

#if ! defined (UNDER_CE)
#define _WIN32_DCOM
#include <assert.h>
#define ASSERT(x)  assert(x)
#endif

#include <windows.h>
#include "bthapi.h"
#include "bt_sdp.h"
#include "winsock2.h"
#include "bthnscreate.hxx"

#if ! defined (UNDER_CE)
#include <stdio.h>
#endif


BOOL  g_fVerbose;            // Should we be verbose?
int   g_iIndent = 0;         // Number of tabs to insert during a printout.

IClassFactory *pCFContainer = NULL;


#if defined (UNDER_CE)
const CLSID CLSID_SdpRecord = {0xACD02BA7,0x9667,0x4085,{0xA1,0x00,0xCC,0x6A,0xCA,0x96,0x21,0xD6}};
const CLSID CLSID_SdpNodeContainer = {0xD5CA76C5,0x0DEE,0x4453,{0x96,0xA1,0xE6,0x03,0xC2,0x40,0x17,0x66}};
#else
const CLSID CLSID_SdpRecord =        {0x238CACDA,0x2346,0x4748,{0xB3,0xEE,0xF1,0x27,0x82,0x77,0x2D,0xFC}};
const CLSID CLSID_SdpNodeContainer = {0x51002954,0xD4E4,0x4507,{0xB4,0x80,0x1B,0x84,0x54,0x34,0x7C,0xDC}};
#endif

#define GUID_FORMAT     "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n"

#define GUID_ELEMENTS(p) \
    &p->Data1,                 &p->Data2,    &p->Data3,\
    &p->Data4[0], &p->Data4[1], &p->Data4[2], &p->Data4[3],\
    &p->Data4[4], &p->Data4[5], &p->Data4[6], &p->Data4[7]

void CreateSdpRecord(FILE* pf);
void CreateServiceSearch(FILE* pf);
void CreateAttributeSearch(FILE* pf);
void CreateServiceAttributeSearch(FILE* pf);

#define SDP_RECORD_COMMENT_CHAR  ';'

//
// Macros to help generate verbose output
// 
#define PRINT_TABS()  {\
                    if (g_fVerbose) { \
                       for (int _iterator = 0; _iterator < g_iIndent; _iterator++) \
                           wprintf(L"\t"); \
                       }\
                    }

void MyPrint(WCHAR *wszFormat, ...) {
    if (g_fVerbose) {
        PRINT_TABS();

        va_list ap;
        va_start(ap,wszFormat);
        vfwprintf(stdout,wszFormat,ap);
        wprintf(L"\n");
        va_end (ap);
    }
}

#define DO(x) x MyPrint(L#x);
#define INDENT(offset) g_iIndent += offset;

inline void SdpAddEntry(USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont, NodeData *pNdw)
{
    if (pRecord)  { 
        if (ERROR_SUCCESS != pRecord->SetAttribute(attribVal, pNdw)) {
            wprintf(L"*** ERROR *** adding attribute value 0x%x\n",attribVal);
            exit(0); 
        }
        if (g_fVerbose) { 
            PRINT_TABS(); 
            wprintf(L"pRecord->SetAttribute(0x%x, &ndw);\n\n",attribVal); 
        } 
    } 
    else { 
        if (ERROR_SUCCESS != pCont->AppendNode(pNdw)) { 
            wprintf(L"*** ERROR *** pCont->AppendNode fails\n"); 
            exit(0); 
        } 
        if (g_fVerbose) { 
            PRINT_TABS(); 
            wprintf(L"pContainer->AppendNode(&ndw);\n\n");
        } 
   }
}
inline void PrintLineBreak(int iNewLines) {
    if (g_fVerbose) {
        for (int i = 0; i < iNewLines; i++)
            wprintf(L"\n");
    }
}

#define MAX_HANDLES      100
#define MAX_ATTRIBUTES   100

#define SKIP_WHITESPACE(p)   while (*(p) && iswspace(*(p))) ++(p);


void SdpRecordCreatorUsage(void) {
    wprintf(L"bthnscreate [-verbose][-record/-service/-attribute/-service_attribute][file]\n"
            L"           -verbose (optional) generates C code\n"
            L"           [file] is the file containing the SDP paramaters\n"
            L"           -record:             Create SDP service record\n"
            L"           -service:            Create SDP service search\n"
            L"           -attribute           Create SDP attribute search\n"
            L"           -service_attribute:  Create SDP Service Attribute search\n");
}

int wmain (int argc, WCHAR **argv) {
    int iShift;
    if (argc < 2) {
        SdpRecordCreatorUsage();
        return 0;
    }
    else if (argc < 3 || (0 == wcsicmp(argv[1],L"-help"))) {
        // Print more generic help...
        SdpRecordCreatorUsage();
        return 0;
    }

    if (0 == wcsicmp(argv[1],L"-verbose")) {
        iShift = 2;
        g_fVerbose = TRUE;
    }
    else {
        iShift = 1;
        g_fVerbose = FALSE;
    }

    if (argc - iShift != 2) {
        SdpRecordCreatorUsage();
        return 0;
    }

    FILE *pf = _wfopen(argv[iShift+1], L"r");
    if (!pf) {
        wprintf(L"*** ERROR ***  cannot open %s\n", argv[iShift+1]);
        return 0;
    }

    if (0 == wcsicmp(argv[iShift],L"-record"))
        CreateSdpRecord(pf);
    else if (0 == wcsicmp(argv[iShift],L"-service"))
        CreateServiceSearch(pf);
    else if (0 == wcsicmp(argv[iShift],L"-attribute"))
        CreateAttributeSearch(pf);
    else if (0 == wcsicmp(argv[iShift],L"-service_attribute"))
        CreateServiceAttributeSearch(pf);
    else
        SdpRecordCreatorUsage();

    fclose(pf);
    return 0;
}

typedef void (*PFNPARSE)(FILE*, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
struct ParseEntry {
    PFNPARSE pfnParse;
    char *name;
};

void AddUint8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUint16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUint32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUint64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUint128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddInt8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddInt16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddInt32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddInt64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddInt128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUuid16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUuid32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUuid128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddBoolean(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddNil(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddString(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddUrl(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddSequence(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void AddAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);
void ContainerEnd(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont);

ParseEntry entries[] = {
    {AddUint8, "UINT8"},
    {AddUint16, "UINT16"},
    {AddUint32, "UINT32"},
    {AddUint64, "UINT64"},
    {AddUint128, "UINT128"},
    {AddInt8, "INT8"},
    {AddInt16, "INT16"},
    {AddInt32, "INT32"},
    {AddInt64, "INT64"},
    {AddInt128, "INT128"},
    {AddUuid16, "UUID16"},
    {AddUuid32, "UUID32"},
    {AddUuid128, "UUID128"},
    {AddBoolean, "BOOLEAN"},
    {AddString, "STRING"},
    {AddUrl, "URL"},
    {AddSequence, "SEQUENCE"},
    {AddAlternative, "ALTERNATIVE"},
    {AddNil, "Nil"},
    {ContainerEnd, "END" },
    {NULL, NULL},
};

void AddUint8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Uint8((UCHAR) (i & 0xFF));
    MyPrint(L"ndw.Uint8(0x%x);",(UCHAR) (i & 0xFF));

    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUint16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Uint16((USHORT) (i & 0xFFFF));
    MyPrint(L"ndw.Uint16(0x%x);",(USHORT) (i & 0xFFFF));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUint32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Uint32((UINT) i);
    MyPrint(L"ndw.Uint32(0x%x);",(UINT) i);
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUint64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    ULONGLONG ull;
    fscanf(pf, "%I64x\n", &ull);

    NDW ndw;
    ndw.Uint64(ull);
    MyPrint(L"ndw.Uint64(0x%I64x);",ull);
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUint128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    SDP_ULARGE_INTEGER_16 u128;
    fscanf(pf, "%I64x,",&u128.HighPart);
    fscanf(pf, "%I64x\n",&u128.LowPart);

    NDW ndw;
    ndw.Uint128(&u128);
    MyPrint(L"ulargeInt.LowPart  = 0x%I64x;",u128.LowPart);
    MyPrint(L"ulargeInt.HighPart = 0x%I64x;",u128.HighPart);
    MyPrint(L"ndw.Uint128(&ulargeInt);");
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

static WCHAR szWriteBuf[48];

WCHAR * PrintNumber(int iVal, int iMask) {
	if (iVal > 0)
		wsprintf(szWriteBuf,L"0x%x",(iVal & iMask));
	else {
		iVal *= -1;
		wsprintf(szWriteBuf,L"-0x%x",(iVal & iMask));
	}

	return szWriteBuf;
}

WCHAR * PrintNumber64(__int64 iVal) {
	if (iVal > 0)
		wsprintf(szWriteBuf,L"0x%I64x",iVal);
	else {
		iVal *= -1;
		wsprintf(szWriteBuf,L"-0x%I64x",iVal);
	}

	return szWriteBuf;
}


void AddInt8(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Int8((CHAR) (i & 0xFF));

    MyPrint(L"ndw.Int8(%s);",PrintNumber(i,0xFF));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddInt16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Int16((SHORT) (i & 0xFFFF));
    MyPrint(L"ndw.Int16(%s);",PrintNumber(i,0xFFFF));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddInt32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);

    NDW ndw;
    ndw.Int32((ULONG) i);
    MyPrint(L"ndw.Int32(%s);",PrintNumber(i,0xFFFFFFFF));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddInt64(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    LONGLONG ll;
    fscanf(pf, "%I64x\n", &ll);

    NDW ndw;
    ndw.Int64(ll);
    MyPrint(L"ndw.Int64(%s);",PrintNumber64(ll));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddInt128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    SDP_LARGE_INTEGER_16 u128;

⌨️ 快捷键说明

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