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

📄 bthnscreate.cxx

📁 SDP Search and Record generator OVERVIEW: Recognizing the difficulty in creating an SDP service
💻 CXX
📖 第 1 页 / 共 3 页
字号:
    
    ASSERT(g_iIndent == 1);
    if (SUCCEEDED(hr)) {
        #define OUTPUT_PER_LINE    8

        wprintf(L"\tconst int cSdpRecord = 0x%08x\n",dwSize);
        wprintf(L"\tBYTE rgbSdpRecord[] = {\n\t\t",TRUE);
        for (ULONG i = 1; i <= dwSize-1; i++) {
            wprintf(L"0x%02x, ",pSdpRecordStream[i-1]);
            if ((i % OUTPUT_PER_LINE)==0)
                wprintf(L"\n\t\t");
        }
        wprintf(L"0x%02x\n",pSdpRecordStream[dwSize-1]);
        wprintf(L"\t};\n");
    }
    MyPrint(L"#endif /* 0 */\n");

Finished:
    if (pf)
        fclose(pf);

    if (pCFRecord)
        pCFRecord->Release();

    if (pCFContainer)
        pCFContainer->Release();

    if (pRecord)
        pRecord->Release();

    if (pSdpRecordStream)
        CoTaskMemFree(pSdpRecordStream);

    if (g_fVerbose) {
        // free the data structures associated with the record and add it to SDP.
        wprintf(L"\tblob.cbSize    = sizeof(BTHNS_SETBLOB) + dwSize - 1;\n"
                L"\tpSetBlob = (PBTHNS_SETBLOB) LocalAlloc(LPTR,blob.cbSize);\n\n"
                L"\tif (!pSetBlob)\n"
                L"\t\tgoto Finished;\n\n"
                L"\tblob.pBlobData = (PBYTE) pSetBlob;\n"
                L"\tpSetBlob->pRecordHandle   = &recordHandle;\n"
                L"\tpSetBlob->pSdpVersion     = &sdpVersion;\n"
                L"\tpSetBlob->fSecurity       = 0;\n"
                L"\tpSetBlob->fOptions        = 0;\n"
                L"\tpSetBlob->ulRecordLength = dwSize;\n\n"
                L"\tmemcpy(pSetBlob->pRecord,pSdpRecordStream,dwSize);\n"
	            L"\tmemset(&Service,0,sizeof(Service));\n"
                L"\tService.dwSize = sizeof(Service);\n"
                L"\tService.lpBlob = &blob;\n"
                L"\tService.dwNameSpace = NS_BTH;\n\n"
                L"\tBthNsSetService(&Service,RNRSERVICE_REGISTER,0);\n"
                L"Finished:\n"
                L"\tif (pCFRecord)\n"
                L"\t\tpCFRecord->Release();\n\n"
                L"\tif (pCFContainer)\n"
                L"\t\tpCFContainer->Release();\n\n"
                L"\tif (pRecord)\n"
                L"\t\tpRecord->Release();\n\n"
                L"\tif (pSdpRecordStream)\n"
                L"\t\tCoTaskMemFree(pSdpRecordStream);\n\n");
    }

    DO(CoUninitialize(););
    MyPrint(L"return 0;");

    INDENT(-1);
    ASSERT(g_iIndent == 0);
    MyPrint(L"}");
}

// Searching routines
BOOL ReadUUIDs(FILE *pFile, SdpQueryUuid* pUUIDs, unsigned short *pcUUIDs, BOOL fEOF)  {
    char           buffer[2048];
    int            i;
    GUID           guid;
    *pcUUIDs = 0;

    while (EOF != fscanf(pFile, "%s", buffer))  {
        if (ParseHandleComment(pFile,buffer,sizeof(buffer)))
            continue;

        // When we read in GUIDS + Attrib (fEOF=FALSE) then stop reading GUID at "END"
        if (!fEOF && (_stricmp(buffer,"END") == 0))  {
            if (0 == *pcUUIDs) {
                wprintf(L"*** ERROR ***: No UUIDS specified\n");
                return FALSE;
            }
            return TRUE;
        }
        if (*pcUUIDs == MAX_UUIDS_IN_QUERY) {
            wprintf(L"Cannot have > %d UUIDs in ServiceSearch\n",MAX_UUIDS_IN_QUERY);
            return FALSE;
        }

        if (_stricmp(buffer,"UUID16") == 0)  {
            fscanf(pFile, "%x\n", &i);
            pUUIDs[*pcUUIDs].u.uuid16 = (unsigned short) i;
            pUUIDs[*pcUUIDs].uuidType = SDP_ST_UUID16;

            PRINT_TABS();
            wprintf(L"rBlob.uuids[%d].uuidType = SDP_ST_UUID16;\n",*pcUUIDs);
            PRINT_TABS();
            wprintf(L"rBlob.uuids[%d].u.uuid16 = (unsigned short) 0x%x;\n\n",*pcUUIDs,(unsigned short) i);
        }
        else if (_stricmp(buffer,"UUID32") == 0) {
            fscanf(pFile, "%x\n", &i);
            pUUIDs[*pcUUIDs].u.uuid32 = i;
            pUUIDs[*pcUUIDs].uuidType = SDP_ST_UUID32;

            PRINT_TABS();
            wprintf(L"rBlob.uuids[%d].uuidType = SDP_ST_UUID32;\n",*pcUUIDs);
            PRINT_TABS();
            wprintf(L"rBlob.uuids[%d].u.uuid32 = 0x%x;\n\n",*pcUUIDs,i);
        }
        else if (_stricmp(buffer,"UUID128") == 0) {
            memset(&guid,0,0);
            fscanf(pFile,GUID_FORMAT,GUID_ELEMENTS((&guid)));
            pUUIDs[*pcUUIDs].uuidType = SDP_ST_UUID128;
            memcpy(&pUUIDs[*pcUUIDs].u.uuid128,&guid,sizeof(guid));

            PRINT_TABS();
            wprintf(L"rBlob.uuids[%d].uuidType = SDP_ST_UUID128;\n",*pcUUIDs);
            PRINT_TABS();
            wprintf(L"SET_GUID_VALUES(rBlob.uuids[%d].u.uuid128,0x%08x,0x%04x,0x%04x,\n",*pcUUIDs,guid.Data1,guid.Data2,guid.Data3);
            PRINT_TABS();
            wprintf(L"                0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n\n",guid.Data4[0],guid.Data4[1],guid.Data4[2],guid.Data4[3],guid.Data4[4],guid.Data4[5],guid.Data4[6],guid.Data4[7]);
        }
        else {
            wprintf(L"*** ERROR *** GUID must be prefaced by UUID16, UUID32, or UUID128\n");
            return FALSE;
        }
        *pcUUIDs += 1;
    }

    if (0 == *pcUUIDs) {
        wprintf(L"*** ERROR ***: No UUIDS specified\n");
        return FALSE;
    }
    return TRUE;
}

BOOL ReadAttributes(FILE *pFile, SdpAttributeRange *pRange, unsigned long *pcAttributes, BOOL fCountOnly)  {
    CHAR buffer[2048];

    *pcAttributes = 0;

    while (EOF != fscanf(pFile,"%s",buffer))  {
        if (ParseHandleComment(pFile,buffer,sizeof(buffer)))
            continue;

        if (! sscanf(buffer,"%x",(unsigned long*)&pRange[*pcAttributes].minAttribute)) {
            wprintf(L"*** ERROR ***: Min Attrib ID syntax, element #=%d\n",*pcAttributes);
            return FALSE;
        }

        if (EOF == fscanf(pFile,"%s",buffer)) {
            wprintf(L"*** ERROR ***: Max Attrib ID syntax, element #=%d\n",*pcAttributes);
            return FALSE;
        }
        
        if (! sscanf(buffer,"%x",(unsigned long*)&pRange[*pcAttributes].maxAttribute)) {
            wprintf(L"*** ERROR ***: Max Attrib ID syntax, element #=%d\n",*pcAttributes);
            return FALSE;
        }

        if (!fCountOnly) {
            PRINT_TABS();
            wprintf(L"attribRange[%d].minAttribute = 0x%x;\n",*pcAttributes,pRange[*pcAttributes].minAttribute);
            PRINT_TABS();
            wprintf(L"attribRange[%d].maxAttribute = 0x%x;\n\n",*pcAttributes,pRange[*pcAttributes].maxAttribute);
        }

        *pcAttributes = *pcAttributes+1;
    }
    return TRUE;
}

void CommonSearchStart(void) {
    if (!g_fVerbose)
        return;

    CommonIncludes();
    wprintf(L"#include <winsock2.h>\n");

    wprintf(L"void InitWSAQuerySet(WSAQUERYSET *pw, CSADDR_INFO *pcsaBuffer,\n"
            L"                     SOCKADDR_BTH *psockBT, LPBLOB pBlob)\n{\n"
            L"\tmemset(pw,0,sizeof(WSAQUERYSET));\n"
            L"\tpw->dwSize      = sizeof(WSAQUERYSET);\n"
            L"\tpw->dwNameSpace = NS_BTH;\n"
            L"\tpw->lpBlob      = pBlob;\n"
            L"\tpw->lpcsaBuffer = pcsaBuffer;\n\n"
            L"\tmemset(pcsaBuffer,0,sizeof(CSADDR_INFO));\n"
            L"\tpcsaBuffer->RemoteAddr.lpSockaddr       = (LPSOCKADDR) psockBT;\n"
            L"\tpcsaBuffer->RemoteAddr.iSockaddrLength  = sizeof(SOCKADDR_BTH);\n"
            L"}\n\n");

    MyPrint(L"{");
    INDENT(1);
    MyPrint(L"DWORD dwFlags = 0;");
    MyPrint(L"DWORD dwError = WSA_NOT_ENOUGH_MEMORY;");
    MyPrint(L"PWSAQUERYSET pWsaResults = NULL;\n");
    MyPrint(L"CSADDR_INFO csAddr;");
    MyPrint(L"// TODO: Fill in sockBT based on device that is being queried");
    MyPrint(L"SOCKADDR_BTH sockBT;");
    MyPrint(L"HANDLE hLookup = 0;");
    MyPrint(L"DWORD dwBufferLen = 0;");
    MyPrint(L"BLOB blob;");
    MyPrint(L"WSAQUERYSET wsaQuery;");
    MyPrint(L"PBTHNS_RESTRICTIONBLOB prBlob = NULL;");

    PrintLineBreak(2);
}

void CommonSearchEnd(void) {
    if (g_fVerbose) 
        wprintf(L"\treturn 0;\n"
                L"}\n");
}

void CommonSearchMiddle(DWORD dwSearchType) {
    PrintLineBreak(1);

    if (dwSearchType == SDP_SERVICE_SEARCH_REQUEST)  {
        wprintf(L"\tblob.cbSize    = sizeof(BTHNS_RESTRICTIONBLOB);\n"
                L"\tblob.pBlobData = (BYTE*)&rBlob;\n\n");
    }
    else {
        wprintf(L"\tblob.cbSize = sizeof(BTHNS_RESTRICTIONBLOB)\n"
                L"\t              + (cAttributes-1)* sizeof(SdpAttributeRange);\n\n"
                L"\tif (NULL == (blob.pBlobData = (PBYTE) LocalAlloc(LPTR,blob.cbSize))) {\n"
                L"\t\tdwError = WSA_NOT_ENOUGH_MEMORY;\n"
                L"\t\tgoto done;\n" 
                L"\t}\n\n"

                L"\tprBlob = (PBTHNS_RESTRICTIONBLOB) blob.pBlobData;\n"
                L"\tmemcpy(prBlob->pRange,attribRange,sizeof(attribRange));\n"
                L"\tprBlob->numRange = cAttributes;\n");
    }
    if (dwSearchType == SDP_SERVICE_ATTRIBUTE_REQUEST) {
        wprintf(L"\tprBlob->type = SDP_SERVICE_ATTRIBUTE_REQUEST;\n"
                L"\t// prBlob->serviceHandle = ? // TODO: Fill in from other search;\n\n");
    }
    else if (dwSearchType == SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST) {
        wprintf(L"\tprBlob->type = SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST;\n"
                L"\tmemcpy(prBlob->uuids,rBlob.uuids,sizeof(rBlob.uuids));\n\n");
    }

    wprintf(L"\tInitWSAQuerySet(&wsaQuery,&csAddr,&sockBT,&blob);\n\n"
            L"\tdwError = BthNsLookupServiceBegin(&wsaQuery,dwFlags,&hLookup);\n"
            L"\tif (SOCKET_ERROR == dwError)\n"
            L"\t\tgoto done;\n\n"

            L"\tdwError = BthNsLookupServiceNext(hLookup,dwFlags,&dwBufferLen,NULL);\n"
            L"\tif (GetLastError() != WSA_NOT_ENOUGH_MEMORY)\n"
            L"\t\tgoto done;\n\n"

            L"\tif (NULL == (pWsaResults = (PWSAQUERYSET) LocalAlloc(0,dwBufferLen))) {\n"
            L"\t\tdwError = WSA_NOT_ENOUGH_MEMORY;\n"
            L"\t\tgoto done;\n"
            L"\t}\n"

            L"\tdwError = BthNsLookupServiceNext(hLookup,dwFlags,&dwBufferLen,pWsaResults);\n\n"
            L"done:\n");

    if (dwSearchType != SDP_SERVICE_SEARCH_REQUEST) {
        wprintf(L"\tif (prBlob)\n"
                L"\t\tLocalFree(prBlob);\n\n");
    }
    wprintf(L"\tif (pWsaResults && (dwError == ERROR_SUCCESS)) {\n"
            L"\t\t; // TODO: Add your specific code here (successful query)\n\n"
            L"\t}\n\n"
            L"\tif (dwError == SOCKET_ERROR) // convert to WSA err code\n"
            L"\t\tdwError = GetLastError();\n\n"
            L"\tif (pWsaResults)\n"
            L"\t\tLocalFree(pWsaResults);\n\n"
            L"\tif (hLookup)\n"
            L"\t\tBthNsLookupServiceEnd(hLookup);\n\n");
}

void CreateServiceSearch(FILE* pf) {
    CommonSearchStart();

    DO(BTHNS_RESTRICTIONBLOB rBlob;)
    unsigned short cUUIDs;

    DO(memset(&rBlob,0,sizeof(rBlob));)

    PrintLineBreak(1);
    if (! ReadUUIDs(pf,rBlob.uuids,&cUUIDs,TRUE))
        return;

    if (g_fVerbose) {
        wprintf(L"\trBlob.type = SDP_SERVICE_SEARCH_REQUEST;\n");
        CommonSearchMiddle(SDP_SERVICE_SEARCH_REQUEST);
    }

    CommonSearchEnd();
}

void CreateAttributeSearch(FILE* pf) {
    CommonSearchStart();

    SdpAttributeRange  attribRange[MAX_ATTRIBUTES];
    unsigned long      cAttributes;
    fpos_t             fPos = 0;

    if (! ReadAttributes(pf,attribRange,&cAttributes,TRUE))
        return;

    MyPrint(L"SdpAttributeRange attribRange[%d];",cAttributes);
    MyPrint(L"unsigned long      cAttributes = %d;\n",cAttributes);

    if (fsetpos(pf,&fPos)) {
        wprintf(L"*** ERROR *** problems reading file\n");
        return;
    }

    if (! ReadAttributes(pf,attribRange,&cAttributes,FALSE))
        return;

    if (g_fVerbose) {
        CommonSearchMiddle(SDP_SERVICE_ATTRIBUTE_REQUEST);
    }
    CommonSearchEnd();
}

void CreateServiceAttributeSearch(FILE* pf) {
    CommonSearchStart();

    SdpAttributeRange  attribRange[MAX_ATTRIBUTES];
    unsigned long      cAttributes;

    fpos_t             fPos = 0;
    DO(BTHNS_RESTRICTIONBLOB rBlob;)
    DO(memset(&rBlob,0,sizeof(rBlob));)
    unsigned short cUUIDs;

    if (! ReadUUIDs(pf,rBlob.uuids,&cUUIDs,FALSE))
        return;

    if (fgetpos(pf,&fPos)) {
        wprintf(L"*** ERROR *** problems reading file\n");
        return;
    }

    if (! ReadAttributes(pf,attribRange,&cAttributes,TRUE))
        return;

    MyPrint(L"SdpAttributeRange attribRange[%d];",cAttributes);
    MyPrint(L"unsigned long      cAttributes = %d;",cAttributes);

    if (fsetpos(pf,&fPos)) {
        wprintf(L"*** ERROR *** problems reading file\n");
        return;
    }

    if (! ReadAttributes(pf,attribRange,&cAttributes,FALSE))
        return;

    if (g_fVerbose) {
        CommonSearchMiddle(SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST);
    }

    CommonSearchEnd();
}

⌨️ 快捷键说明

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