Debian Codegraph @

core/java/android/hardware/SerialPort.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2011 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.hardware;
18
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 import android.os.ParcelFileDescriptor;
22
23 import java.io.FileDescriptor;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26
27 /**
28 * @hide
29 */
30 public class SerialPort {
31
32 private static final String TAG = "SerialPort";
33
34 // used by the JNI code
35 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
36 private int mNativeContext;
37 private final String mName;
38 private ParcelFileDescriptor mFileDescriptor;
39
40 /**
41 * SerialPort should only be instantiated by SerialManager
42 * @hide
43 */
44 public SerialPort(String name) {
45 mName = name;
46 }
47
48 /**
49 * SerialPort should only be instantiated by SerialManager
50 * Speed must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600,
51 * 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000,
52 * 1500000, 2000000, 2500000, 3000000, 3500000, 4000000
53 *
54 * @hide
55 */
56 public void open(ParcelFileDescriptor pfd, int speed) throws IOException {
57 native_open(pfd.getFileDescriptor(), speed);
58 mFileDescriptor = pfd;
59 }
60
61 /**
62 * Closes the serial port
63 */
64 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
65 public void close() throws IOException {
66 if (mFileDescriptor != null) {
67 mFileDescriptor.close();
68 mFileDescriptor = null;
69 }
70 native_close();
71 }
72
73 /**
74 * Returns the name of the serial port
75 *
76 * @return the serial port's name
77 */
78 public String getName() {
79 return mName;
80 }
81
82 /**
83 * Reads data into the provided buffer.
84 * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is
85 * unchanged after a call to this method.
86 *
87 * @param buffer to read into
88 * @return number of bytes read
89 */
90 public int read(ByteBuffer buffer) throws IOException {
91 if (buffer.isDirect()) {
92 return native_read_direct(buffer, buffer.remaining());
93 } else if (buffer.hasArray()) {
94 return native_read_array(buffer.array(), buffer.remaining());
95 } else {
96 throw new IllegalArgumentException("buffer is not direct and has no array");
97 }
98 }
99
100 /**
101 * Writes data from provided buffer.
102 * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is
103 * unchanged after a call to this method.
104 *
105 * @param buffer to write
106 * @param length number of bytes to write
107 */
108 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
109 public void write(ByteBuffer buffer, int length) throws IOException {
110 if (buffer.isDirect()) {
111 native_write_direct(buffer, length);
112 } else if (buffer.hasArray()) {
113 native_write_array(buffer.array(), length);
114 } else {
115 throw new IllegalArgumentException("buffer is not direct and has no array");
116 }
117 }
118
119 /**
120 * Sends a stream of zero valued bits for 0.25 to 0.5 seconds
121 */
122 public void sendBreak() {
123 native_send_break();
124 }
125
126 private native void native_open(FileDescriptor pfd, int speed) throws IOException;
127 private native void native_close();
128 private native int native_read_array(byte[] buffer, int length) throws IOException;
129 private native int native_read_direct(ByteBuffer buffer, int length) throws IOException;
130 private native void native_write_array(byte[] buffer, int length) throws IOException;
131 private native void native_write_direct(ByteBuffer buffer, int length) throws IOException;
132 private native void native_send_break();
133 }