| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
public class SerialPort { |
| 31 |
|
| 32 |
private static final String TAG = "SerialPort"; |
| 33 |
|
| 34 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
public SerialPort(String name) { |
| 45 |
mName = name; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public void open(ParcelFileDescriptor pfd, int speed) throws IOException { |
| 57 |
native_open(pfd.getFileDescriptor(), speed); |
| 58 |
mFileDescriptor = pfd; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public String getName() { |
| 79 |
return mName; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 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 |
} |