HaikuDepot: notify work status from main window
[haiku.git] / src / kits / device / USBConfiguration.cpp
blobcf158a06e21dd03f8fa2878ebdd3a3fb1b128630
1 /*
2 * Copyright 2007-2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Lotz <mmlr@mlotz.ch>
7 */
9 #include <USBKit.h>
10 #include <usb_raw.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <new>
16 BUSBConfiguration::BUSBConfiguration(BUSBDevice *device, uint32 index, int rawFD)
17 : fDevice(device),
18 fIndex(index),
19 fRawFD(rawFD),
20 fInterfaces(NULL),
21 fConfigurationString(NULL)
23 usb_raw_command command;
24 command.config.descriptor = &fDescriptor;
25 command.config.config_index = fIndex;
26 if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR, &command,
27 sizeof(command)) || command.config.status != B_USB_RAW_STATUS_SUCCESS)
28 memset(&fDescriptor, 0, sizeof(fDescriptor));
30 fInterfaces = new(std::nothrow) BUSBInterface *[
31 fDescriptor.number_interfaces];
32 if (fInterfaces == NULL)
33 return;
35 for (uint32 i = 0; i < fDescriptor.number_interfaces; i++) {
36 fInterfaces[i] = new(std::nothrow) BUSBInterface(this, i,
37 B_USB_RAW_ACTIVE_ALTERNATE, fRawFD);
42 BUSBConfiguration::~BUSBConfiguration()
44 delete[] fConfigurationString;
45 if (fInterfaces != NULL) {
46 for (int32 i = 0; i < fDescriptor.number_interfaces; i++)
47 delete fInterfaces[i];
48 delete[] fInterfaces;
53 uint32
54 BUSBConfiguration::Index() const
56 return fIndex;
60 const BUSBDevice *
61 BUSBConfiguration::Device() const
63 return fDevice;
67 const char *
68 BUSBConfiguration::ConfigurationString() const
70 if (fDescriptor.configuration == 0)
71 return "";
73 if (fConfigurationString)
74 return fConfigurationString;
76 fConfigurationString = Device()->DecodeStringDescriptor(
77 fDescriptor.configuration);
78 if (fConfigurationString == NULL)
79 return "";
81 return fConfigurationString;
85 const usb_configuration_descriptor *
86 BUSBConfiguration::Descriptor() const
88 return &fDescriptor;
92 uint32
93 BUSBConfiguration::CountInterfaces() const
95 return fDescriptor.number_interfaces;
99 const BUSBInterface *
100 BUSBConfiguration::InterfaceAt(uint32 index) const
102 if (index >= fDescriptor.number_interfaces || fInterfaces == NULL)
103 return NULL;
105 return fInterfaces[index];