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

📄 convert.c

📁 收集到的orion_ep93xx_wince_bsp_1-3-507红外收发驱动源码,未作测试
💻 C
字号:
/*++
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.
Copyright (c) 1995-2000 Microsoft Corporation.  All rights reserved.

Module Name:

    convert.c

Abstract:

    This file implements the IrDA Serial IR NDIS MAC driver
    functions.  This is provided as a sample to platform writers and is
    expected to be able to be used without modification on most (if not
    all) hardware platforms.

Functions:

    STUFF_BYTE
    NdisToIrPacket

Notes:


--*/

#include "irmacutl.h"

/*++

 Function:       STUFF_BYTE

 Description:    Byte stuffing for an IR frame.

 Arguments:

    lpPut   - Byte stream to copy byte and stuffing into.

    bGet    - Byte to stuff, if required.

 Returns:

    None.

 Comments:

--*/

static __inline void
STUFF_BYTE(
    LPBYTE *lpPut, 
    BYTE    bGet
    )
{
    switch (bGet)
    {
        case SLOW_IR_ESC:
        case SLOW_IR_BOF:
        case SLOW_IR_EOF:
            *(*lpPut)++ = SLOW_IR_ESC;
            *(*lpPut)++ = bGet ^ SLOW_IR_ESC_COMP;
            break;
        default:
            *(*lpPut)++ = bGet;
            break;
    }
}

/*++

 Function:       NdisToIrPacket

 Description:    Convert an NDIS packet to an IR packet.

 Arguments:

    pNdisPacket     - Ndis packet to convert to IR packet.
    
    cbTatBofs       - Number of BOFs to insert into IR packet to implement
                      minium turnaround time.
                      
    pIrPacket       - IR packet buffer to copy IR frame into.
    
    lpcbIrPacket    - IN:  Size of IR packet buffer.
                      OUT: Count of bytes copied into IR packet buffer.

 Returns:

    BOOL 
    
        Success - TRUE.
        
        Failure - FALSE.

 Comments:

 On failure, *lpcbIrPacket will contain the buffer size required to convert 
 the packet, or 0 if a corruption was detected.
 
--*/

BOOL
NdisToIrPacket(
    IN     PNDIS_PACKET pNdisPacket,
    IN     DWORD        cbTatBofs,
       OUT LPBYTE       pIrPacket,
    IN OUT LPDWORD      lpcbIrPacket
    )
{
    BOOL                    status = TRUE;
    DWORD                   cbNdisPacket;
    PNDIS_BUFFER            pNdisBuffer;
    PNDIS_IRDA_PACKET_INFO  pIrdaInfo;
    DWORD                   cbIField;
    SLOW_IR_FCS_TYPE        fcs;
    LPBYTE                  pPut;
    DWORD                   i;

    ASSERT(lpcbIrPacket != NULL);

    //
    // Get irda media specific info.
    //

    pIrdaInfo = GetIrdaPacketInfo(pNdisPacket);

    ASSERT(pIrdaInfo != NULL);
    ASSERT(pIrdaInfo->ExtraBOFs <= MAX_NUM_EXTRA_BOFS);

    //
    // Get size of the NDIS packet.
    //

    NdisQueryPacket(pNdisPacket, NULL, NULL, &pNdisBuffer, &cbNdisPacket);

    // Is the packet large enough? Must be at least size of address and control fields.
    if (cbNdisPacket < (SLOW_IR_ADDR_SIZE + SLOW_IR_CONTROL_SIZE))
    {
        *lpcbIrPacket = 0;
        status = FALSE;
        goto done;
    }

    // Get information field size.
    cbIField = cbNdisPacket - SLOW_IR_ADDR_SIZE - SLOW_IR_CONTROL_SIZE;

    // Is NDIS packet too large?
    if (cbNdisPacket > MAX_IRDA_DATA_SIZE)
    {
        *lpcbIrPacket = 0;
        status = FALSE;
        goto done;
    }

    // Is pIrPacket buffer too small?
    if (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(cbIField) > *lpcbIrPacket)
    {
		//
		// The driver sends a command with zero packet length to figure
		// out the maximum packet size.
		//
        //ASSERT(FALSE);
        *lpcbIrPacket = MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(cbIField);
        status = FALSE;
        goto done;
    }

    //
    // Construct IrPacket.
    // 1) Put in extra BOFs (including Min TAT BOFs).
    // 2) Put in final BOF.
    // 3) Put in stuffed bytes, calculate FCS of unstuffed byte.
    // 4) Compute final FCS and add FCS to buffer (stuffed).
    // 5) Put in EOF.
    //

    pPut = pIrPacket;

    // Extra BOFS and TAT BOFs.
    for (i = 0; i < (pIrdaInfo->ExtraBOFs + cbTatBofs); i++) 
    { 
        *pPut++ = SLOW_IR_EXTRA_BOF; 
    }

    *pPut++ = SLOW_IR_BOF;

    // Put in data.
    fcs = 0xffff;
    while (pNdisBuffer)
    {
        LPBYTE pData;
        LPBYTE pEnd;
        DWORD  cbData;

        NdisQueryBuffer(pNdisBuffer, &pData, &cbData);

        pEnd = pData + cbData;

        while (pData < pEnd)
        {
            fcs = ComputeCumulativeFCS(fcs, *pData);
            STUFF_BYTE(&pPut, *pData);
            pData++;
        }

        NdisGetNextBuffer(pNdisBuffer, &pNdisBuffer);
    }

    // FCS.
    fcs = ~(fcs);
    STUFF_BYTE(&pPut, (BYTE) (fcs & 0xFF));
    STUFF_BYTE(&pPut, (BYTE) (fcs >> 8));

    // EOF.
    *pPut++ = SLOW_IR_EOF;

    // Calculate length.
    *lpcbIrPacket = (pPut - pIrPacket);

done:

    return (status);
}

⌨️ 快捷键说明

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