File: indiusbdevice.cpp

package info (click to toggle)
libindi 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,232 kB
  • sloc: ansic: 17,921; cpp: 17,821; xml: 260; makefile: 12
file content (181 lines) | stat: -rw-r--r-- 4,390 bytes parent folder | download
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
174
175
176
177
178
179
180
181
/*******************************************************************************
  Copyright(c) 2011 Gerry Rozema. All rights reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License version 2 as published by the Free Software Foundation.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
*******************************************************************************/
#include "indiusbdevice.h"

#include <string.h>

INDI::USBDevice::USBDevice()
{
	dev=NULL;
	usb_handle=NULL;
	OutputEndpoint=0;
	InputEndpoint=0;

	usb_init();
	usb_find_busses();
	usb_find_devices();
}


INDI::USBDevice::~USBDevice()
{
}

struct usb_device * INDI::USBDevice::FindDevice(int vendor, int product, int searchindex)
{
    struct usb_device *dev;
    struct usb_bus *usb_bus;
    int index=0;

    for(usb_bus=usb_busses; usb_bus; usb_bus=usb_bus->next) {
        for(dev=usb_bus->devices; dev; dev=dev->next) {
            if(dev->descriptor.idVendor==vendor) {
                if(dev->descriptor.idProduct==product) {
                    if(index==searchindex) {
                        fprintf(stderr,"Device has %d configurations\n",dev->descriptor.bNumConfigurations);
                        return dev;
                    }
                    else index++;
                }
            }
        }
    }
    return NULL;

}

int INDI::USBDevice::Open()
{
	if(dev==NULL) return -1;

	usb_handle=usb_open(dev);
	if(usb_handle != NULL) {
		//printf("Opened ok\n");

		return FindEndpoints();
		//return 0;
	}
	return -1;
}

int INDI::USBDevice::FindEndpoints()
{

	int rc=0;
	struct usb_interface_descriptor *intf;



	intf=&dev->config[0].interface[0].altsetting[0];
	for(int i=0; i<intf->bNumEndpoints; i++) {
		fprintf(stderr,"%04x %04x\n",
			   intf->endpoint[i].bEndpointAddress,
			   intf->endpoint[i].bmAttributes
			   );

		int dir;
		int addr;
		addr=intf->endpoint[i].bEndpointAddress;
		addr = addr & (USB_ENDPOINT_DIR_MASK^0xffff);
		//printf("%02x ",addr);

		int attr;
		int tp;
		attr=intf->endpoint[i].bmAttributes;
		tp=attr&USB_ENDPOINT_TYPE_MASK;
		//if(tp==USB_ENDPOINT_TYPE_BULK) printf("Bulk  ");
		//if(tp==USB_ENDPOINT_TYPE_INTERRUPT) printf("Interrupt ");



		dir=intf->endpoint[i].bEndpointAddress;
		dir=dir&USB_ENDPOINT_DIR_MASK;
		if(dir==USB_ENDPOINT_IN) {
			//printf("Input  ");
			fprintf(stderr,"Got an input endpoint\n");
			InputEndpoint=addr;
			InputType=tp;
		}
		if(dir==USB_ENDPOINT_OUT) {
			//printf("Output ");
			fprintf(stderr,"got an output endpoint\n");
			OutputEndpoint=addr;
			OutputType=tp;
		}
		//printf("\n");
	}

	//printf("claim interface returns %d\n",rc);
	return rc;

}

int INDI::USBDevice::ReadInterrupt(char *buf,int c,int timeout)
{
	int rc;

	rc=usb_interrupt_read(usb_handle,InputEndpoint,buf,c,timeout);
	//rc=usb_bulk_read(usb_handle,InputEndpoint,buf,c,timeout);
	return rc;

}

int INDI::USBDevice::WriteInterrupt(char *buf,int c,int timeout)
{
	int rc;

	//printf("Writing %02x to endpoint %d\n",buf[0],OutputEndpoint);
	rc=usb_interrupt_write(usb_handle,OutputEndpoint,buf,c,timeout);
	return rc;

}

int INDI::USBDevice::ReadBulk(char *buf,int nbytes,int timeout)
{
	int rc;

	//rc=usb_interrupt_read(usb_handle,InputEndpoint,buf,c,timeout);
        rc=usb_bulk_read(usb_handle,InputEndpoint,buf,nbytes,timeout);
	return rc;

}

int INDI::USBDevice::WriteBulk(char *buf,int nbytes,int timeout)
{
	int rc;

	//printf("Writing %02x to endpoint %d\n",buf[0],OutputEndpoint);
	//rc=usb_interrupt_write(usb_handle,OutputEndpoint,buf,c,timeout);
        rc=usb_bulk_write(usb_handle,OutputEndpoint,buf,nbytes,timeout);
	return rc;

}

int INDI::USBDevice::ControlMessage()
{
    char buf[3];
    int rc;

    buf[0]=0;
    buf[1]=0;
    buf[2]=0;

    rc=usb_control_msg(usb_handle,0xc2&USB_ENDPOINT_IN, 0x12,0,0,buf,3,1000);
    fprintf(stderr,"UsbControlMessage returns %d\n",rc);
    return 0;
}