@@ -57,17 +57,24 @@ public class UsbConfiguration implements Parcelable {
5757 @ NotNull
5858 UsbInterface [] interfaces ;
5959
60+ /**
61+ * All interface associations for this config
62+ */
63+ @ NotNull
64+ UsbInterfaceAssociation [] interfaceAssociations ;
65+
6066 final byte [] extra ;
6167
6268 /**
6369 * UsbConfiguration should only be instantiated by UsbService implementation
6470 */
65- public UsbConfiguration (int id , @ Nullable String name , int attributes , int maxPower , UsbInterface [] interfaces , byte [] extra ) {
71+ private UsbConfiguration (int id , @ Nullable String name , int attributes , int maxPower , UsbInterface [] interfaces , UsbInterfaceAssociation [] interfaceAssociations , byte [] extra ) {
6672 this .id = id ;
6773 this .name = name ;
6874 this .attributes = attributes ;
6975 this .maxPower = maxPower ;
7076 this .interfaces = interfaces ;
77+ this .interfaceAssociations = interfaceAssociations ;
7178 this .extra = extra ;
7279 }
7380
@@ -139,6 +146,25 @@ UsbInterface getInterface(int index) {
139146 return interfaces [index ];
140147 }
141148
149+ /**
150+ * Returns the number of {@link UsbInterfaceAssociation}s this configuration contains.
151+ *
152+ * @return the number of endpoints
153+ */
154+ public int getInterfaceAssociationCount () {
155+ return interfaceAssociations .length ;
156+ }
157+
158+ /**
159+ * Returns the {@link UsbInterface} at the given index.
160+ *
161+ * @return the interface
162+ */
163+ public @ NotNull
164+ UsbInterfaceAssociation getInterfaceAssociation (int index ) {
165+ return interfaceAssociations [index ];
166+ }
167+
142168 public byte [] getExtra () {
143169 return extra ;
144170 }
@@ -152,6 +178,7 @@ public String toString() {
152178 ", attributes=" + attributes +
153179 ", maxPower=" + maxPower +
154180 ", interfaces=" + Arrays .toString (interfaces ) +
181+ ", interfaceAssociations=" + Arrays .toString (interfaceAssociations ) +
155182 ", extra=" + Arrays .toString (extra ) +
156183 '}' ;
157184 }
@@ -164,8 +191,9 @@ public UsbConfiguration createFromParcel(Parcel in) {
164191 int attributes = in .readInt ();
165192 int maxPower = in .readInt ();
166193 UsbInterface [] interfaces = (UsbInterface []) in .readParcelableArray (UsbInterface .class .getClassLoader ());
194+ UsbInterfaceAssociation [] interfaceAssociations = (UsbInterfaceAssociation []) in .readParcelableArray (UsbInterfaceAssociation .class .getClassLoader ());
167195 byte [] extra = in .createByteArray ();
168- UsbConfiguration configuration = new UsbConfiguration (id , name , attributes , maxPower , interfaces , extra );
196+ UsbConfiguration configuration = new UsbConfiguration (id , name , attributes , maxPower , interfaces , interfaceAssociations , extra );
169197 return configuration ;
170198 }
171199
@@ -186,6 +214,7 @@ public void writeToParcel(Parcel parcel, int flags) {
186214 parcel .writeInt (attributes );
187215 parcel .writeInt (maxPower );
188216 parcel .writeParcelableArray ((Parcelable []) interfaces , 0 );
217+ parcel .writeParcelableArray ((Parcelable []) interfaceAssociations , 0 );
189218 parcel .writeByteArray (extra );
190219 }
191220
@@ -213,11 +242,26 @@ static UsbConfiguration fromNativeObject(@NotNull UsbDevice device, int configur
213242 List <UsbInterface > usbInterface = UsbInterface .fromNativeObject (device , nativeInterface );
214243 usbInterfaces .addAll (usbInterface );
215244 }
245+ final List <UsbInterfaceAssociation > usbInterfaceAssociations = new ArrayList <>();
246+ // Get the native IAD array. Make sure you free it!
247+ final ByteBuffer associationArray = nativeGetInterfaceAssociationArray (device .getNativeObject (), configuration );
248+ if (associationArray == null ) {
249+ nativeDestroy (nativeObject );
250+ throw new IllegalStateException ("Failed to get Interface Association Descriptor Array" );
251+ }
252+ // using ByteBuffer's length / capacity here is a bit hacky, but native plays along
253+ for (int i = 0 ; i < associationArray .capacity (); ++i ) {
254+ ByteBuffer nativeInterface = nativeGetInterfaceAssociation (associationArray , i );
255+ UsbInterfaceAssociation usbInterface = UsbInterfaceAssociation .fromNativeDescriptor (device , nativeInterface );
256+ usbInterfaceAssociations .add (usbInterface );
257+ }
258+ nativeDestroyInterfaceAssociationArray (associationArray );
216259 ByteBuffer extraTmp = nativeGetExtra (nativeObject );
217260 ByteBuffer extra = ByteBuffer .allocate (extraTmp .capacity ());
218261 extra .put (extraTmp );
219262 final UsbConfiguration usbConfiguration = new UsbConfiguration (id , name , attributes ,
220- maxPower , usbInterfaces .toArray (new UsbInterface [0 ]), extra .array ());
263+ maxPower , usbInterfaces .toArray (new UsbInterface [0 ]),
264+ usbInterfaceAssociations .toArray (new UsbInterfaceAssociation [0 ]), extra .array ());
221265
222266 // Destroy the native configuration object
223267 nativeDestroy (nativeObject );
@@ -234,6 +278,13 @@ static UsbConfiguration fromNativeObject(@NotNull UsbDevice device, int configur
234278 */
235279 private static native ByteBuffer nativeGetInterface (@ NonNull ByteBuffer nativeObject , int interfaceIndex );
236280
281+ @ Nullable
282+ private static native ByteBuffer nativeGetInterfaceAssociationArray (long device , int interfaceIndex );
283+
284+ private static native ByteBuffer nativeGetInterfaceAssociation (@ NonNull ByteBuffer nativeObject , int interfaceIndex );
285+
286+ private static native void nativeDestroyInterfaceAssociationArray (@ NonNull ByteBuffer nativeObject );
287+
237288 private static native ByteBuffer nativeGetExtra (@ NonNull ByteBuffer nativeObject );
238289
239290 private static native void nativeDestroy (@ NonNull ByteBuffer nativeObject );
0 commit comments