File: GCUSBTransport.c

package info (click to toggle)
ifd-gempc 1.0.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,136 kB
  • ctags: 307
  • sloc: ansic: 2,961; sh: 278; makefile: 201
file content (71 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 *  $Id: GCUSBTransport.c,v 1.5 2004-07-03 21:42:47 rousseau Exp $
 *  ifd-GemPC
 *
 *  Created by JL Giraud <jl.giraud@free.fr> on Sun Nov 19 2000.
 *  Updated by Ludovic Rousseau <ludovic.rousseau@free.fr>, Oct 2001
 *
 *  Transport level for the GemPC430 of Gemplus.
 *
 *  License:   See file COPYING
 *
 */
 
#include <string.h>

#include "gempc_ifdhandler.h"
#include "Config.h"
#include "GCTransport.h"
#include "usbserial.h"

             
status_t GCSendCommand(DWORD Lun, DWORD nLengthIn,
	const UCHAR pcBufferCmd[], PDWORD pnLengthOut, UCHAR pcBufferOut[])
{
    UCHAR pctr_to_card_buffer[GC_TR_BUF_SIZE];
    status_t creturn_value;
    DWORD nlength;

    creturn_value = STATUS_SUCCESS;

    if (GC_TR_BUF_SIZE <= nLengthIn)
    {
        /* Buffer is too small (should not happen) */
        creturn_value = STATUS_DEVICE_PROTOCOL_ERROR;
        goto finally;
    }
    
    memcpy(pctr_to_card_buffer+1, pcBufferCmd, nLengthIn);
    pctr_to_card_buffer[TR_OFFSET_LNG] = nLengthIn;
    if (WriteUSB(Lun, nLengthIn+1, pctr_to_card_buffer) != STATUS_SUCCESS)
    {        
        creturn_value = STATUS_DEVICE_PROTOCOL_ERROR;
        goto finally;
    }

    nlength = sizeof(pctr_to_card_buffer);
    if (ReadUSB(Lun, &nlength, pctr_to_card_buffer) != STATUS_SUCCESS)
    {
        creturn_value = STATUS_DEVICE_PROTOCOL_ERROR;
        goto finally;
    }
    if ( nlength < 1 )
    {
        /* length byte not received */
        creturn_value = STATUS_DEVICE_PROTOCOL_ERROR;        
        goto finally;
    }
    nlength--;
    *pnLengthOut = (*pnLengthOut<nlength) ? *pnLengthOut : nlength;
    memcpy(pcBufferOut, pctr_to_card_buffer+1, *pnLengthOut);

finally:
    if ( creturn_value != STATUS_SUCCESS )
        *pnLengthOut = 0;

    /* Clear buffer */
    bzero(pctr_to_card_buffer, sizeof(pctr_to_card_buffer));

    return creturn_value;
} /* GCSendCommand */