• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.os;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.annotation.TestApi;
24 import android.compat.annotation.UnsupportedAppUsage;
25 
26 import dalvik.annotation.optimization.FastNative;
27 
28 import libcore.util.NativeAllocationRegistry;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 
35 /** @hide */
36 @SystemApi
37 @TestApi
38 public class HwParcel {
39     private static final String TAG = "HwParcel";
40 
41     /** @hide */
42     @IntDef(prefix = { "STATUS_" }, value = {
43         STATUS_SUCCESS,
44     })
45     @Retention(RetentionPolicy.SOURCE)
46     public @interface Status {}
47 
48     /**
49      * Success return error for a transaction. Written to parcels
50      * using writeStatus.
51      */
52     public static final int STATUS_SUCCESS      = 0;
53 
54     private static final NativeAllocationRegistry sNativeRegistry;
55 
56     @UnsupportedAppUsage
HwParcel(boolean allocate)57     private HwParcel(boolean allocate) {
58         native_setup(allocate);
59 
60         sNativeRegistry.registerNativeAllocation(
61                 this,
62                 mNativeContext);
63     }
64 
65     /**
66      * Creates an initialized and empty parcel.
67      */
HwParcel()68     public HwParcel() {
69         native_setup(true /* allocate */);
70 
71         sNativeRegistry.registerNativeAllocation(
72                 this,
73                 mNativeContext);
74     }
75 
76     /**
77      * Writes an interface token into the parcel used to verify that
78      * a transaction has made it to the right type of interface.
79      *
80      * @param interfaceName fully qualified name of interface message
81      *     is being sent to.
82      */
83     @FastNative
writeInterfaceToken(String interfaceName)84     public native final void writeInterfaceToken(String interfaceName);
85     /**
86      * Writes a boolean value to the end of the parcel.
87      * @param val to write
88      */
89     @FastNative
writeBool(boolean val)90     public native final void writeBool(boolean val);
91     /**
92      * Writes a byte value to the end of the parcel.
93      * @param val to write
94      */
95     @FastNative
writeInt8(byte val)96     public native final void writeInt8(byte val);
97     /**
98      * Writes a short value to the end of the parcel.
99      * @param val to write
100      */
101     @FastNative
writeInt16(short val)102     public native final void writeInt16(short val);
103     /**
104      * Writes a int value to the end of the parcel.
105      * @param val to write
106      */
107     @FastNative
writeInt32(int val)108     public native final void writeInt32(int val);
109     /**
110      * Writes a long value to the end of the parcel.
111      * @param val to write
112      */
113     @FastNative
writeInt64(long val)114     public native final void writeInt64(long val);
115     /**
116      * Writes a float value to the end of the parcel.
117      * @param val to write
118      */
119     @FastNative
writeFloat(float val)120     public native final void writeFloat(float val);
121     /**
122      * Writes a double value to the end of the parcel.
123      * @param val to write
124      */
125     @FastNative
writeDouble(double val)126     public native final void writeDouble(double val);
127     /**
128      * Writes a String value to the end of the parcel.
129      *
130      * Note, this will be converted to UTF-8 when it is written.
131      *
132      * @param val to write
133      */
134     @FastNative
writeString(String val)135     public native final void writeString(String val);
136     /**
137      * Writes a native handle (without duplicating the underlying
138      * file descriptors) to the end of the parcel.
139      *
140      * @param val to write
141      */
142     @FastNative
writeNativeHandle(@ullable NativeHandle val)143     public native final void writeNativeHandle(@Nullable NativeHandle val);
144 
145     /**
146      * Writes an array of boolean values to the end of the parcel.
147      * @param val to write
148      */
149     @FastNative
writeBoolVector(boolean[] val)150     private native final void writeBoolVector(boolean[] val);
151     /**
152      * Writes an array of byte values to the end of the parcel.
153      * @param val to write
154      */
155     @FastNative
writeInt8Vector(byte[] val)156     private native final void writeInt8Vector(byte[] val);
157     /**
158      * Writes an array of short values to the end of the parcel.
159      * @param val to write
160      */
161     @FastNative
writeInt16Vector(short[] val)162     private native final void writeInt16Vector(short[] val);
163     /**
164      * Writes an array of int values to the end of the parcel.
165      * @param val to write
166      */
167     @FastNative
writeInt32Vector(int[] val)168     private native final void writeInt32Vector(int[] val);
169     /**
170      * Writes an array of long values to the end of the parcel.
171      * @param val to write
172      */
173     @FastNative
writeInt64Vector(long[] val)174     private native final void writeInt64Vector(long[] val);
175     /**
176      * Writes an array of float values to the end of the parcel.
177      * @param val to write
178      */
179     @FastNative
writeFloatVector(float[] val)180     private native final void writeFloatVector(float[] val);
181     /**
182      * Writes an array of double values to the end of the parcel.
183      * @param val to write
184      */
185     @FastNative
writeDoubleVector(double[] val)186     private native final void writeDoubleVector(double[] val);
187     /**
188      * Writes an array of String values to the end of the parcel.
189      *
190      * Note, these will be converted to UTF-8 as they are written.
191      *
192      * @param val to write
193      */
194     @FastNative
writeStringVector(String[] val)195     private native final void writeStringVector(String[] val);
196     /**
197      * Writes an array of native handles to the end of the parcel.
198      *
199      * Individual elements may be null but not the whole array.
200      *
201      * @param val array of {@link NativeHandle} objects to write
202      */
203     @FastNative
writeNativeHandleVector(NativeHandle[] val)204     private native final void writeNativeHandleVector(NativeHandle[] val);
205 
206     /**
207      * Helper method to write a list of Booleans to val.
208      * @param val list to write
209      */
writeBoolVector(ArrayList<Boolean> val)210     public final void writeBoolVector(ArrayList<Boolean> val) {
211         final int n = val.size();
212         boolean[] array = new boolean[n];
213         for (int i = 0; i < n; ++i) {
214             array[i] = val.get(i);
215         }
216 
217         writeBoolVector(array);
218     }
219 
220     /**
221      * Helper method to write a list of Booleans to the end of the parcel.
222      * @param val list to write
223      */
writeInt8Vector(ArrayList<Byte> val)224     public final void writeInt8Vector(ArrayList<Byte> val) {
225         final int n = val.size();
226         byte[] array = new byte[n];
227         for (int i = 0; i < n; ++i) {
228             array[i] = val.get(i);
229         }
230 
231         writeInt8Vector(array);
232     }
233 
234     /**
235      * Helper method to write a list of Shorts to the end of the parcel.
236      * @param val list to write
237      */
writeInt16Vector(ArrayList<Short> val)238     public final void writeInt16Vector(ArrayList<Short> val) {
239         final int n = val.size();
240         short[] array = new short[n];
241         for (int i = 0; i < n; ++i) {
242             array[i] = val.get(i);
243         }
244 
245         writeInt16Vector(array);
246     }
247 
248     /**
249      * Helper method to write a list of Integers to the end of the parcel.
250      * @param val list to write
251      */
writeInt32Vector(ArrayList<Integer> val)252     public final void writeInt32Vector(ArrayList<Integer> val) {
253         final int n = val.size();
254         int[] array = new int[n];
255         for (int i = 0; i < n; ++i) {
256             array[i] = val.get(i);
257         }
258 
259         writeInt32Vector(array);
260     }
261 
262     /**
263      * Helper method to write a list of Longs to the end of the parcel.
264      * @param val list to write
265      */
writeInt64Vector(ArrayList<Long> val)266     public final void writeInt64Vector(ArrayList<Long> val) {
267         final int n = val.size();
268         long[] array = new long[n];
269         for (int i = 0; i < n; ++i) {
270             array[i] = val.get(i);
271         }
272 
273         writeInt64Vector(array);
274     }
275 
276     /**
277      * Helper method to write a list of Floats to the end of the parcel.
278      * @param val list to write
279      */
writeFloatVector(ArrayList<Float> val)280     public final void writeFloatVector(ArrayList<Float> val) {
281         final int n = val.size();
282         float[] array = new float[n];
283         for (int i = 0; i < n; ++i) {
284             array[i] = val.get(i);
285         }
286 
287         writeFloatVector(array);
288     }
289 
290     /**
291      * Helper method to write a list of Doubles to the end of the parcel.
292      * @param val list to write
293      */
writeDoubleVector(ArrayList<Double> val)294     public final void writeDoubleVector(ArrayList<Double> val) {
295         final int n = val.size();
296         double[] array = new double[n];
297         for (int i = 0; i < n; ++i) {
298             array[i] = val.get(i);
299         }
300 
301         writeDoubleVector(array);
302     }
303 
304     /**
305      * Helper method to write a list of Strings to the end of the parcel.
306      * @param val list to write
307      */
writeStringVector(ArrayList<String> val)308     public final void writeStringVector(ArrayList<String> val) {
309         writeStringVector(val.toArray(new String[val.size()]));
310     }
311 
312     /**
313      * Helper method to write a list of native handles to the end of the parcel.
314      * @param val list of {@link NativeHandle} objects to write
315      */
writeNativeHandleVector(@onNull ArrayList<NativeHandle> val)316     public final void writeNativeHandleVector(@NonNull ArrayList<NativeHandle> val) {
317         writeNativeHandleVector(val.toArray(new NativeHandle[val.size()]));
318     }
319 
320     /**
321      * Write a hwbinder object to the end of the parcel.
322      * @param binder value to write
323      */
324     @FastNative
writeStrongBinder(IHwBinder binder)325     public native final void writeStrongBinder(IHwBinder binder);
326 
327     /**
328      * Write a HidlMemory object (without duplicating the underlying file descriptors) to the end
329      * of the parcel.
330      *
331      * @param memory value to write
332      */
333     @FastNative
writeHidlMemory(@onNull HidlMemory memory)334     public native final void writeHidlMemory(@NonNull HidlMemory memory);
335 
336     /**
337      * Checks to make sure that the interface name matches the name written by the parcel
338      * sender by writeInterfaceToken
339      *
340      * @throws SecurityException interface doesn't match
341      */
enforceInterface(String interfaceName)342     public native final void enforceInterface(String interfaceName);
343 
344     /**
345      * Reads a boolean value from the current location in the parcel.
346      * @return value parsed from the parcel
347      * @throws IllegalArgumentException if the parcel has no more data
348      */
349     @FastNative
readBool()350     public native final boolean readBool();
351     /**
352      * Reads a byte value from the current location in the parcel.
353      * @return value parsed from the parcel
354      * @throws IllegalArgumentException if the parcel has no more data
355      */
356     @FastNative
readInt8()357     public native final byte readInt8();
358     /**
359      * Reads a short value from the current location in the parcel.
360      * @return value parsed from the parcel
361      * @throws IllegalArgumentException if the parcel has no more data
362      */
363     @FastNative
readInt16()364     public native final short readInt16();
365     /**
366      * Reads a int value from the current location in the parcel.
367      * @return value parsed from the parcel
368      * @throws IllegalArgumentException if the parcel has no more data
369      */
370     @FastNative
readInt32()371     public native final int readInt32();
372     /**
373      * Reads a long value from the current location in the parcel.
374      * @return value parsed from the parcel
375      * @throws IllegalArgumentException if the parcel has no more data
376      */
377     @FastNative
readInt64()378     public native final long readInt64();
379     /**
380      * Reads a float value from the current location in the parcel.
381      * @return value parsed from the parcel
382      * @throws IllegalArgumentException if the parcel has no more data
383      */
384     @FastNative
readFloat()385     public native final float readFloat();
386     /**
387      * Reads a double value from the current location in the parcel.
388      * @return value parsed from the parcel
389      * @throws IllegalArgumentException if the parcel has no more data
390      */
391     @FastNative
readDouble()392     public native final double readDouble();
393     /**
394      * Reads a String value from the current location in the parcel.
395      * @return value parsed from the parcel
396      * @throws IllegalArgumentException if the parcel has no more data
397      */
398     @FastNative
readString()399     public native final String readString();
400     /**
401      * Reads a native handle (without duplicating the underlying file
402      * descriptors) from the parcel. These file descriptors will only
403      * be open for the duration that the binder window is open. If they
404      * are needed further, you must call {@link NativeHandle#dup()}.
405      *
406      * @return a {@link NativeHandle} instance parsed from the parcel
407      * @throws IllegalArgumentException if the parcel has no more data
408      */
409     @FastNative
readNativeHandle()410     public native final @Nullable NativeHandle readNativeHandle();
411     /**
412      * Reads an embedded native handle (without duplicating the underlying
413      * file descriptors) from the parcel. These file descriptors will only
414      * be open for the duration that the binder window is open. If they
415      * are needed further, you must call {@link NativeHandle#dup()}. You
416      * do not need to call close on the NativeHandle returned from this.
417      *
418      * @param parentHandle handle from which to read the embedded object
419      * @param offset offset into parent
420      * @return a {@link NativeHandle} instance parsed from the parcel
421      * @throws IllegalArgumentException if the parcel has no more data
422      */
423     @FastNative
readEmbeddedNativeHandle( long parentHandle, long offset)424     public native final @Nullable NativeHandle readEmbeddedNativeHandle(
425             long parentHandle, long offset);
426 
427     /**
428      * Reads an array of boolean values from the parcel.
429      * @return array of parsed values
430      * @throws IllegalArgumentException if the parcel has no more data
431      */
432     @FastNative
readBoolVectorAsArray()433     private native final boolean[] readBoolVectorAsArray();
434     /**
435      * Reads an array of byte values from the parcel.
436      * @return array of parsed values
437      * @throws IllegalArgumentException if the parcel has no more data
438      */
439     @FastNative
readInt8VectorAsArray()440     private native final byte[] readInt8VectorAsArray();
441     /**
442      * Reads an array of short values from the parcel.
443      * @return array of parsed values
444      * @throws IllegalArgumentException if the parcel has no more data
445      */
446     @FastNative
readInt16VectorAsArray()447     private native final short[] readInt16VectorAsArray();
448     /**
449      * Reads an array of int values from the parcel.
450      * @return array of parsed values
451      * @throws IllegalArgumentException if the parcel has no more data
452      */
453     @FastNative
readInt32VectorAsArray()454     private native final int[] readInt32VectorAsArray();
455     /**
456      * Reads an array of long values from the parcel.
457      * @return array of parsed values
458      * @throws IllegalArgumentException if the parcel has no more data
459      */
460     @FastNative
readInt64VectorAsArray()461     private native final long[] readInt64VectorAsArray();
462     /**
463      * Reads an array of float values from the parcel.
464      * @return array of parsed values
465      * @throws IllegalArgumentException if the parcel has no more data
466      */
467     @FastNative
readFloatVectorAsArray()468     private native final float[] readFloatVectorAsArray();
469     /**
470      * Reads an array of double values from the parcel.
471      * @return array of parsed values
472      * @throws IllegalArgumentException if the parcel has no more data
473      */
474     @FastNative
readDoubleVectorAsArray()475     private native final double[] readDoubleVectorAsArray();
476     /**
477      * Reads an array of String values from the parcel.
478      * @return array of parsed values
479      * @throws IllegalArgumentException if the parcel has no more data
480      */
481     @FastNative
readStringVectorAsArray()482     private native final String[] readStringVectorAsArray();
483     /**
484      * Reads an array of native handles from the parcel.
485      * @return array of {@link NativeHandle} objects
486      * @throws IllegalArgumentException if the parcel has no more data
487      */
488     @FastNative
readNativeHandleAsArray()489     private native final NativeHandle[] readNativeHandleAsArray();
490 
491     /**
492      * Convenience method to read a Boolean vector as an ArrayList.
493      * @return array of parsed values.
494      * @throws IllegalArgumentException if the parcel has no more data
495      */
readBoolVector()496     public final ArrayList<Boolean> readBoolVector() {
497         Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray());
498 
499         return new ArrayList<Boolean>(Arrays.asList(array));
500     }
501 
502     /**
503      * Convenience method to read a Byte vector as an ArrayList.
504      * @return array of parsed values.
505      * @throws IllegalArgumentException if the parcel has no more data
506      */
readInt8Vector()507     public final ArrayList<Byte> readInt8Vector() {
508         Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray());
509 
510         return new ArrayList<Byte>(Arrays.asList(array));
511     }
512 
513     /**
514      * Convenience method to read a Short vector as an ArrayList.
515      * @return array of parsed values.
516      * @throws IllegalArgumentException if the parcel has no more data
517      */
readInt16Vector()518     public final ArrayList<Short> readInt16Vector() {
519         Short[] array = HwBlob.wrapArray(readInt16VectorAsArray());
520 
521         return new ArrayList<Short>(Arrays.asList(array));
522     }
523 
524     /**
525      * Convenience method to read a Integer vector as an ArrayList.
526      * @return array of parsed values.
527      * @throws IllegalArgumentException if the parcel has no more data
528      */
readInt32Vector()529     public final ArrayList<Integer> readInt32Vector() {
530         Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray());
531 
532         return new ArrayList<Integer>(Arrays.asList(array));
533     }
534 
535     /**
536      * Convenience method to read a Long vector as an ArrayList.
537      * @return array of parsed values.
538      * @throws IllegalArgumentException if the parcel has no more data
539      */
readInt64Vector()540     public final ArrayList<Long> readInt64Vector() {
541         Long[] array = HwBlob.wrapArray(readInt64VectorAsArray());
542 
543         return new ArrayList<Long>(Arrays.asList(array));
544     }
545 
546     /**
547      * Convenience method to read a Float vector as an ArrayList.
548      * @return array of parsed values.
549      * @throws IllegalArgumentException if the parcel has no more data
550      */
readFloatVector()551     public final ArrayList<Float> readFloatVector() {
552         Float[] array = HwBlob.wrapArray(readFloatVectorAsArray());
553 
554         return new ArrayList<Float>(Arrays.asList(array));
555     }
556 
557     /**
558      * Convenience method to read a Double vector as an ArrayList.
559      * @return array of parsed values.
560      * @throws IllegalArgumentException if the parcel has no more data
561      */
readDoubleVector()562     public final ArrayList<Double> readDoubleVector() {
563         Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray());
564 
565         return new ArrayList<Double>(Arrays.asList(array));
566     }
567 
568     /**
569      * Convenience method to read a String vector as an ArrayList.
570      * @return array of parsed values.
571      * @throws IllegalArgumentException if the parcel has no more data
572      */
readStringVector()573     public final ArrayList<String> readStringVector() {
574         return new ArrayList<String>(Arrays.asList(readStringVectorAsArray()));
575     }
576 
577     /**
578      * Convenience method to read a vector of native handles as an ArrayList.
579      * @return array of {@link NativeHandle} objects.
580      * @throws IllegalArgumentException if the parcel has no more data
581      */
readNativeHandleVector()582     public final @NonNull ArrayList<NativeHandle> readNativeHandleVector() {
583         return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray()));
584     }
585 
586     /**
587      * Reads a strong binder value from the parcel.
588      * @return binder object read from parcel or null if no binder can be read
589      * @throws IllegalArgumentException if the parcel has no more data
590      */
591     @FastNative
readStrongBinder()592     public native final IHwBinder readStrongBinder();
593 
594     /**
595      * Reads a HidlMemory value (without duplicating the underlying file
596      * descriptors) from the parcel. These file descriptors will only
597      * be open for the duration that the binder window is open. If they
598      * are needed further, you must call {@link HidlMemory#dup()}, which makes you also
599      * responsible for calling {@link HidlMemory#close()}.
600      *
601      * @return HidlMemory object read from parcel.
602      * @throws IllegalArgumentException if the parcel has no more data or is otherwise corrupt.
603      */
604     @FastNative
605     @NonNull
readHidlMemory()606     public native final HidlMemory readHidlMemory();
607 
608     /**
609      * Reads an embedded HidlMemory (without duplicating the underlying
610      * file descriptors) from the parcel. These file descriptors will only
611      * be open for the duration that the binder window is open. If they
612      * are needed further, you must call {@link HidlMemory#dup()}. You
613      * do not need to call close on the HidlMemory returned from this.
614      *
615      * @param fieldHandle  handle of the field, obtained from the {@link HwBlob}.
616      * @param parentHandle parentHandle from which to read the embedded object
617      * @param offset       offset into parent
618      * @return a {@link HidlMemory} instance parsed from the parcel
619      * @throws IllegalArgumentException if the parcel has no more data
620      */
621     @FastNative
622     @NonNull
623     public native final @Nullable
readEmbeddedHidlMemory(long fieldHandle, long parentHandle, long offset)624     HidlMemory readEmbeddedHidlMemory(long fieldHandle, long parentHandle, long offset);
625 
626     /**
627      * Read opaque segment of data as a blob.
628      * @return blob of size expectedSize
629      * @throws IllegalArgumentException if the parcel has no more data
630      */
631     @FastNative
readBuffer(long expectedSize)632     public native final HwBlob readBuffer(long expectedSize);
633 
634     /**
635      * Read a buffer written using scatter gather.
636      *
637      * @param expectedSize size that buffer should be
638      * @param parentHandle handle from which to read the embedded buffer
639      * @param offset offset into parent
640      * @param nullable whether or not to allow for a null return
641      * @return blob of data with size expectedSize
642      * @throws NoSuchElementException if an embedded buffer is not available to read
643      * @throws IllegalArgumentException if expectedSize < 0
644      * @throws NullPointerException if the transaction specified the blob to be null
645      *    but nullable is false
646      */
647     @FastNative
readEmbeddedBuffer( long expectedSize, long parentHandle, long offset, boolean nullable)648     public native final HwBlob readEmbeddedBuffer(
649             long expectedSize, long parentHandle, long offset,
650             boolean nullable);
651 
652     /**
653      * Write a buffer into the transaction.
654      * @param blob blob to write into the parcel.
655      */
656     @FastNative
writeBuffer(HwBlob blob)657     public native final void writeBuffer(HwBlob blob);
658     /**
659      * Write a status value into the blob.
660      * @param status value to write
661      */
662     @FastNative
writeStatus(int status)663     public native final void writeStatus(int status);
664     /**
665      * @throws IllegalArgumentException if a success vaue cannot be read
666      * @throws RemoteException if success value indicates a transaction error
667      */
668     @FastNative
verifySuccess()669     public native final void verifySuccess();
670     /**
671      * Should be called to reduce memory pressure when this object no longer needs
672      * to be written to.
673      */
674     @FastNative
releaseTemporaryStorage()675     public native final void releaseTemporaryStorage();
676     /**
677      * Should be called when object is no longer needed to reduce possible memory
678      * pressure if the Java GC does not get to this object in time.
679      */
680     @FastNative
release()681     public native final void release();
682 
683     /**
684      * Sends the parcel to the specified destination.
685      */
send()686     public native final void send();
687 
688     // Returns address of the "freeFunction".
native_init()689     private static native final long native_init();
690 
691     @FastNative
native_setup(boolean allocate)692     private native final void native_setup(boolean allocate);
693 
694     static {
695         long freeFunction = native_init();
696 
697         sNativeRegistry = new NativeAllocationRegistry(
698                 HwParcel.class.getClassLoader(),
699                 freeFunction,
700                 128 /* size */);
701     }
702 
703     private long mNativeContext;
704 }
705 
706