aboutsummaryrefslogtreecommitdiffstats
path: root/io_win32.c
blob: 698e5502bd15caab7d135db7af2aba92c3fe955c (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * io_win32.c
 *
 * Copyright (C) 2005 Erik Gilling, all rights reserved
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 *
 * USB device enumeration modeled from http://www.delcom-eng.com/downloads/USBPRGMNL.pdf
 *
 */

#include "config.h"

#define WINVER 0x0500
#define INITGUID
#include <windows.h>
#include <setupapi.h>
#include <basetyps.h>

#include <stdio.h>

#include "io.h"
#include "samba.h"


// {E6EF7DCD-1795-4a08-9FBF-AA78423C26F0}
DEFINE_GUID(USBIODS_GUID, 
0xe6ef7dcd, 0x1795, 0x4a08, 0x9f, 0xbf, 0xaa, 0x78, 0x42, 0x3c, 0x26, 0xf0);

HANDLE io_pipe_in, io_pipe_out;

int io_init( char *dev )
{
  char *devname;
  
  //  char buff[16];

  HDEVINFO hInfo = SetupDiGetClassDevs(&USBIODS_GUID, NULL,
				       NULL, DIGCF_PRESENT | 
				       DIGCF_INTERFACEDEVICE);

  SP_INTERFACE_DEVICE_DATA Interface_Info;
  Interface_Info.cbSize = sizeof(Interface_Info);
  // Enumerate device
  if( !SetupDiEnumDeviceInterfaces(hInfo, NULL, (LPGUID)
				   &USBIODS_GUID,0, &Interface_Info ) ) {
    printf( "can't find boot agent\n");
    goto error0;
  }

  DWORD needed; // get the required lenght
  SetupDiGetInterfaceDeviceDetail(hInfo, &Interface_Info,
				  NULL, 0, &needed, NULL);
  PSP_INTERFACE_DEVICE_DETAIL_DATA detail =
    (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(needed);
  if( !detail ) {
    printf( "can't find boot agent\n");
    goto error0;
    return(-1);
  }

  // fill the device details
  detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

  if( !SetupDiGetInterfaceDeviceDetail( hInfo,
					&Interface_Info,detail, 
					needed,NULL, NULL )) {
    printf( "can't find boot agent\n");
    goto error1;
  }
  
  devname = (char *) malloc( strlen( detail->DevicePath ) + 7 );

  strcpy( devname, detail->DevicePath );
  strcat( devname, "\\PIPE00" );
  printf( "%s\n", devname );

  // open out

  io_pipe_out = CreateFile( devname, GENERIC_READ | GENERIC_WRITE,
			    FILE_SHARE_READ, NULL, OPEN_EXISTING,
			    0, NULL );

  if( io_pipe_out == INVALID_HANDLE_VALUE ) {
    printf( "can't open output pipe\n" );
	    goto error2;
  }

  strcpy( devname, detail->DevicePath );
  strcat( devname, "\\PIPE01" );
  
  // open in

  io_pipe_in = CreateFile( devname, GENERIC_READ | GENERIC_WRITE,
			    FILE_SHARE_READ, NULL, OPEN_EXISTING,
			    0, NULL );

  if( io_pipe_in == INVALID_HANDLE_VALUE ) {
    printf( "can't open input pipe\n" );
	    goto error3;
  }
  



  free((PVOID) detail);
  free( devname );

  
  
  return samba_init();

  return 0;
  //  return samba_init();

 error3:
  CloseHandle( io_pipe_out );

 error2:
  free( devname );
  
 error1:
  free((PVOID) detail);

 error0:
  SetupDiDestroyDeviceInfoList(hInfo);
  return -1;
}

int io_cleanup( void )
{
  CloseHandle( io_pipe_out );
  CloseHandle( io_pipe_in );

  return 0;
}
int io_write( void *buff, int len ) 
{
  int write_len = 0;
  DWORD bytes_written;
  
  while( write_len < len ) {
    
    if( !WriteFile( io_pipe_out, buff + write_len, 
		   len - write_len, &bytes_written, NULL ) ) {
      return -1;
    }
    write_len += bytes_written;
  }

  return write_len;
}

int io_read( void *buff, int len ) 
{
  int read_len = 0;
  DWORD bytes_read;
  
  while( read_len < len ) {
    
    if( !ReadFile( io_pipe_in, buff + read_len, 
		   len - read_len, &bytes_read, NULL ) ) {
      return -1;
    }
    read_len += bytes_read;
  }

  return read_len;
}